From 5c72a0cb6099a7c879f4f4fa52df937dcd42ba4f Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Thu, 29 Dec 2022 10:27:54 +0100 Subject: [PATCH] Request handler --- src/client.c | 9 +++++---- src/client.h | 2 +- src/worker/request_handler.c | 12 ++++++++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/client.c b/src/client.c index 5c1f777..fd9f191 100644 --- a/src/client.c +++ b/src/client.c @@ -52,7 +52,8 @@ void client_terminate(int _) { } */ -int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long client_num, unsigned int req_num, const char *restrict log_client_prefix) { +int client_request_handler(client_ctx_t *cctx) { + sock *client = &cctx->socket; struct timespec begin, end; long ret; @@ -140,7 +141,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien strcpy(host, host_ptr); } - sprintf(log_req_prefix, "[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, log_client_prefix); + sprintf(log_req_prefix, "[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, cctx->log_prefix); logger_set_prefix(log_req_prefix); info(BLD_STR "%s %s", req.method, req.uri); @@ -378,7 +379,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien http_add_header_field(&res.hdr, "Last-Modified", last_modified); res.status = http_get_status(200); - if (fastcgi_init(&fcgi_conn, mode, client_num, req_num, client, &req, &uri) != 0) { + if (fastcgi_init(&fcgi_conn, mode, 0 /* TODO */, cctx->req_num, client, &req, &uri) != 0) { res.status = http_get_status(503); sprintf(err_msg, "Unable to communicate with FastCGI socket."); goto respond; @@ -783,7 +784,7 @@ int client_connection_handler(client_ctx_t *ctx, unsigned long client_num, const ctx->s_keep_alive = 1; ctx->c_keep_alive = 1; while (ctx->c_keep_alive && ctx->s_keep_alive && req_num < REQ_PER_CONNECTION) { - ret = client_request_handler(ctx, client, client_num, req_num++, log_client_prefix); + ret = client_request_handler(ctx); logger_set_prefix(log_conn_prefix); } diff --git a/src/client.h b/src/client.h index d2ce3cd..b9685fa 100644 --- a/src/client.h +++ b/src/client.h @@ -27,6 +27,6 @@ typedef struct { host_config_t *get_host_config(const char *host); -void *client_handler(client_ctx_t *client); +int client_request_handler(client_ctx_t *cctx); #endif //SESIMOS_CLIENT_H diff --git a/src/worker/request_handler.c b/src/worker/request_handler.c index 684324e..4124d6a 100644 --- a/src/worker/request_handler.c +++ b/src/worker/request_handler.c @@ -4,6 +4,8 @@ #include "../lib/mpmc.h" #include "../lib/utils.h" #include "tcp_closer.h" +#include "../async.h" +#include "../server.h" static mpmc_t mpmc_ctx; @@ -26,6 +28,12 @@ void request_handler_destroy(void) { } static void request_handler_func(client_ctx_t *ctx) { - // TODO - tcp_close(ctx); + client_request_handler(ctx); + + if (ctx->c_keep_alive && ctx->s_keep_alive && ctx->req_num < REQ_PER_CONNECTION) { + async(ctx->socket.socket, POLLIN, 0, (void (*)(void *)) handle_request, ctx, (void (*)(void *)) tcp_close, ctx); + logger_set_prefix(ctx->log_prefix); + } else { + tcp_close(ctx); + } }