From 28d7cf68df5a73e99f75d64ecd305c40fb459a24 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Fri, 30 Dec 2022 16:48:48 +0100 Subject: [PATCH] Outsource from request_handler --- src/server.h | 1 - src/worker/fastcgi_handler.c | 37 +++++++++++++++++++++++--- src/worker/func.h | 8 ++++-- src/worker/local_handler.c | 1 + src/worker/proxy_handler.c | 32 +++++++++++++++++++--- src/worker/request_handler.c | 50 +++++++++++------------------------ src/worker/ws_frame_handler.c | 19 +++++++++++++ 7 files changed, 103 insertions(+), 45 deletions(-) create mode 100644 src/worker/ws_frame_handler.c diff --git a/src/server.h b/src/server.h index b453439..d1a2b20 100644 --- a/src/server.h +++ b/src/server.h @@ -50,7 +50,6 @@ typedef struct { host_config_t *conf; FILE *file; long content_length; - fastcgi_cnx_t fcgi_cnx; char msg_buf[8192], msg_content[1024]; } client_ctx_t; diff --git a/src/worker/fastcgi_handler.c b/src/worker/fastcgi_handler.c index a2c2917..5caf220 100644 --- a/src/worker/fastcgi_handler.c +++ b/src/worker/fastcgi_handler.c @@ -14,24 +14,34 @@ #include -static int fastcgi_handler(client_ctx_t *ctx); +static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx); +static int fastcgi_handler_2(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx); void fastcgi_handler_func(client_ctx_t *ctx) { + fastcgi_cnx_t fcgi_cnx; + logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix); // TODO - fastcgi_handler(ctx); + fastcgi_handler_1(ctx, &fcgi_cnx); respond(ctx); + fastcgi_handler_2(ctx, &fcgi_cnx); + request_complete(ctx); + handle_request(ctx); } -static int fastcgi_handler(client_ctx_t *ctx) { +static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) { http_res *res = &ctx->res; http_req *req = &ctx->req; http_uri *uri = &ctx->uri; sock *client = &ctx->socket; - fastcgi_cnx_t *fcgi_cnx = &ctx->fcgi_cnx; char *err_msg = ctx->err_msg; + fcgi_cnx->socket = 0; + fcgi_cnx->req_id = 0; + fcgi_cnx->r_addr = ctx->addr; + fcgi_cnx->r_host = (ctx->host[0] != 0) ? ctx->host : NULL; + char buf[1024]; int mode, ret; @@ -143,3 +153,22 @@ static int fastcgi_handler(client_ctx_t *ctx) { return 0; } + +static int fastcgi_handler_2(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) { + const char *transfer_encoding = http_get_header_field(&ctx->res.hdr, "Transfer-Encoding"); + int chunked = (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL); + + int flags = (chunked ? FASTCGI_CHUNKED : 0) | (ctx->use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD)); + int ret = fastcgi_send(fcgi_cnx, &ctx->socket, flags); + + if (ret < 0) { + ctx->c_keep_alive = 0; + } + + if (fcgi_cnx->socket != 0) { + close(fcgi_cnx->socket); + fcgi_cnx->socket = 0; + } + + return ret; +} diff --git a/src/worker/func.h b/src/worker/func.h index ee68529..ce0607d 100644 --- a/src/worker/func.h +++ b/src/worker/func.h @@ -17,12 +17,16 @@ void tcp_closer_func(client_ctx_t *ctx); void request_handler_func(client_ctx_t *ctx); -int respond(client_ctx_t *ctx); - void local_handler_func(client_ctx_t *ctx); void fastcgi_handler_func(client_ctx_t *ctx); void proxy_handler_func(client_ctx_t *ctx); +void ws_frame_handler_func(client_ctx_t *ctx); + +int respond(client_ctx_t *ctx); + +int request_complete(client_ctx_t *ctx); + #endif //SESIMOS_FUNC_H diff --git a/src/worker/local_handler.c b/src/worker/local_handler.c index 11f8c1b..8c5fe53 100644 --- a/src/worker/local_handler.c +++ b/src/worker/local_handler.c @@ -22,6 +22,7 @@ void local_handler_func(client_ctx_t *ctx) { switch (local_handler(ctx)) { case 0: respond(ctx); + request_complete(ctx); handle_request(ctx); break; case 1: diff --git a/src/worker/proxy_handler.c b/src/worker/proxy_handler.c index fbd1e7b..c9e1ad8 100644 --- a/src/worker/proxy_handler.c +++ b/src/worker/proxy_handler.c @@ -1,7 +1,7 @@ /** * sesimos - secure, simple, modern web server * @brief Proxy handler - * @file src/worker/proxy_handler.c + * @file src/worker/proxy_handler_1.c * @author Lorenz Stechauner * @date 2022-12-29 */ @@ -15,17 +15,21 @@ #include -static int proxy_handler(client_ctx_t *ctx); +static int proxy_handler_1(client_ctx_t *ctx); +static int proxy_handler_2(client_ctx_t *ctx); void proxy_handler_func(client_ctx_t *ctx) { logger_set_prefix("[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix); - proxy_handler(ctx); + proxy_handler_1(ctx); respond(ctx); + proxy_handler_2(ctx); + request_complete(ctx); + handle_request(ctx); } -static int proxy_handler(client_ctx_t *ctx) { +static int proxy_handler_1(client_ctx_t *ctx) { http_res *res = &ctx->res; http_req *req = &ctx->req; http_uri *uri = &ctx->uri; @@ -104,3 +108,23 @@ static int proxy_handler(client_ctx_t *ctx) { return 0; } + +static int proxy_handler_2(client_ctx_t *ctx) { + const char *transfer_encoding = http_get_header_field(&ctx->res.hdr, "Transfer-Encoding"); + int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL; + + const char *content_len = http_get_header_field(&ctx->res.hdr, "Content-Length"); + unsigned long len_to_send = 0; + if (content_len != NULL) { + len_to_send = strtol(content_len, NULL, 10); + } + + int flags = (chunked ? PROXY_CHUNKED : 0) | (ctx->use_proxy & PROXY_COMPRESS); + int ret = proxy_send(&ctx->socket, len_to_send, flags); + + if (ret < 0) { + ctx->c_keep_alive = 0; + } + + return ret; +} diff --git a/src/worker/request_handler.c b/src/worker/request_handler.c index 9517472..b7a0a01 100644 --- a/src/worker/request_handler.c +++ b/src/worker/request_handler.c @@ -65,11 +65,6 @@ static int request_handler(client_ctx_t *ctx) { status->origin = NONE; status->ws_key = NULL; - ctx->fcgi_cnx.socket = 0; - ctx->fcgi_cnx.req_id = 0; - ctx->fcgi_cnx.r_addr = ctx->addr; - ctx->fcgi_cnx.r_host = (ctx->host[0] != 0) ? ctx->host : NULL; - clock_gettime(CLOCK_MONOTONIC, &ctx->begin); //ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000); @@ -202,7 +197,6 @@ int respond(client_ctx_t *ctx) { http_res *res = &ctx->res; sock *client = &ctx->socket; http_status_ctx *status = &ctx->status; - fastcgi_cnx_t *fcgi_cnx = &ctx->fcgi_cnx; char *err_msg = ctx->err_msg; long ret = 0; @@ -339,23 +333,9 @@ int respond(client_ctx_t *ctx) { snd_len += ret; } } else if (ctx->use_fastcgi) { - const char *transfer_encoding = http_get_header_field(&res->hdr, "Transfer-Encoding"); - int chunked = (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL); - - int flags = (chunked ? FASTCGI_CHUNKED : 0) | (ctx->use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD)); - ret = fastcgi_send(fcgi_cnx, client, flags); + return 2; } else if (ctx->use_proxy) { - const char *transfer_encoding = http_get_header_field(&res->hdr, "Transfer-Encoding"); - int chunked = transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL; - - const char *content_len = http_get_header_field(&res->hdr, "Content-Length"); - unsigned long len_to_send = 0; - if (content_len != NULL) { - len_to_send = strtol(content_len, NULL, 10); - } - - int flags = (chunked ? PROXY_CHUNKED : 0) | (ctx->use_proxy & PROXY_COMPRESS); - ret = proxy_send(client, len_to_send, flags); + return 3; } if (ret < 0) { @@ -363,22 +343,24 @@ int respond(client_ctx_t *ctx) { } } - if (close_proxy && proxy.socket != 0) { - info(BLUE_STR "Closing proxy connection"); - sock_close(&proxy); - } + return 0; +} +int request_complete(client_ctx_t *ctx) { + // FIXME + //if (close_proxy && proxy.socket != 0) { + // info(BLUE_STR "Closing proxy connection"); + // sock_close(&proxy); + //} + + char buf[32]; clock_gettime(CLOCK_MONOTONIC, &ctx->end); - micros = (ctx->end.tv_nsec - ctx->begin.tv_nsec) / 1000 + (ctx->end.tv_sec - ctx->begin.tv_sec) * 1000000; - info("Transfer complete: %s", format_duration(micros, buf0)); + long micros = (ctx->end.tv_nsec - ctx->begin.tv_nsec) / 1000 + (ctx->end.tv_sec - ctx->begin.tv_sec) * 1000000; + info("Transfer complete: %s", format_duration(micros, buf)); uri_free(&ctx->uri); - if (fcgi_cnx->socket != 0) { - close(fcgi_cnx->socket); - fcgi_cnx->socket = 0; - } - http_free_req(req); - http_free_res(res); + http_free_req(&ctx->req); + http_free_res(&ctx->res); return 0; } diff --git a/src/worker/ws_frame_handler.c b/src/worker/ws_frame_handler.c new file mode 100644 index 0000000..cd3a208 --- /dev/null +++ b/src/worker/ws_frame_handler.c @@ -0,0 +1,19 @@ +/** + * sesimos - secure, simple, modern web server + * @brief WebSocket frame handler + * @file src/worker/ws_frame_handler.c + * @author Lorenz Stechauner + * @date 2022-12-30 + */ + +#include "func.h" + +static int ws_frame_handler(client_ctx_t *ctx); + +void ws_frame_handler_func(client_ctx_t *ctx) { + +} + +static int ws_frame_handler(client_ctx_t *ctx) { + return 0; +}