From c13bea5ab48dba3920d846f5a1792297647eda65 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Tue, 3 Jan 2023 12:58:18 +0100 Subject: [PATCH] Refactor proxy a bit --- src/lib/proxy.h | 3 +-- src/server.c | 3 +++ src/worker/func.h | 5 +++-- src/worker/proxy_handler.c | 20 ++++++++++++++++++++ src/worker/request_handler.c | 25 +++---------------------- 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/lib/proxy.h b/src/lib/proxy.h index 4477b0c..5fb4a10 100644 --- a/src/lib/proxy.h +++ b/src/lib/proxy.h @@ -22,8 +22,7 @@ #include "config.h" typedef struct { - unsigned char initialized:1; - unsigned char in_use:1; + unsigned char initialized:1, in_use:1; sock proxy; char *host; } proxy_ctx_t; diff --git a/src/server.c b/src/server.c index 2e7a452..f2395cd 100644 --- a/src/server.c +++ b/src/server.c @@ -130,6 +130,9 @@ static void terminate_gracefully(int sig) { sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); + // TODO close client connections + // TODO close proxy connections + workers_stop(); workers_destroy(); diff --git a/src/worker/func.h b/src/worker/func.h index 86f2ece..83a7b79 100644 --- a/src/worker/func.h +++ b/src/worker/func.h @@ -18,7 +18,7 @@ typedef struct { sock socket; int req_num; - unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1; + unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1, use_fastcgi:4, use_proxy:2, ws_close:2; char cc[3], host[256]; char req_host[256], err_msg[256]; char log_prefix[128]; @@ -29,7 +29,6 @@ typedef struct { http_uri uri; http_status_ctx status; http_status custom_status; - int use_fastcgi, use_proxy; host_config_t *conf; FILE *file; long content_length; @@ -60,4 +59,6 @@ void request_complete(client_ctx_t *ctx); void tcp_close(client_ctx_t *ctx); +void proxy_close(proxy_ctx_t *ctx); + #endif //SESIMOS_FUNC_H diff --git a/src/worker/proxy_handler.c b/src/worker/proxy_handler.c index 519b5fa..a2a0240 100644 --- a/src/worker/proxy_handler.c +++ b/src/worker/proxy_handler.c @@ -14,6 +14,7 @@ #include "../workers.h" #include +#include static int proxy_handler_1(client_ctx_t *ctx); static int proxy_handler_2(client_ctx_t *ctx); @@ -23,6 +24,17 @@ void proxy_handler_func(client_ctx_t *ctx) { proxy_handler_1(ctx); respond(ctx); + + if (ctx->use_proxy == 2) { + // WebSocket + info("Upgrading connection to WebSocket connection"); + if (ws_handle_connection(&ctx->socket, &ctx->proxy->proxy) != 0) { + ctx->c_keep_alive = 0; + proxy_close(ctx->proxy); + } + info("WebSocket connection closed"); + } + proxy_handler_2(ctx); request_complete(ctx); @@ -126,3 +138,11 @@ static int proxy_handler_2(client_ctx_t *ctx) { return ret; } + +void proxy_close(proxy_ctx_t *ctx) { + info(BLUE_STR "Closing proxy connection"); + sock_close(&ctx->proxy); + + memset(ctx, 0, sizeof(*ctx)); + errno = 0; +} diff --git a/src/worker/request_handler.c b/src/worker/request_handler.c index ecd8bfb..354af74 100644 --- a/src/worker/request_handler.c +++ b/src/worker/request_handler.c @@ -316,10 +316,7 @@ int respond(client_ctx_t *ctx) { } } - int close_proxy = 0; if (ctx->use_proxy != 2) { - const char *conn = http_get_header_field(&res->hdr, "Connection"); - close_proxy = (conn == NULL || (strstr(conn, "keep-alive") == NULL && strstr(conn, "Keep-Alive") == NULL)); http_remove_header_field(&res->hdr, "Connection", HTTP_REMOVE_ALL); http_remove_header_field(&res->hdr, "Keep-Alive", HTTP_REMOVE_ALL); if (ctx->s_keep_alive && ctx->c_keep_alive) { @@ -340,16 +337,8 @@ int respond(client_ctx_t *ctx) { // TODO access/error log file - if (ctx->use_proxy == 2) { - // WebSocket - info("Upgrading connection to WebSocket connection"); - ret = ws_handle_connection(client, &ctx->proxy->proxy); - if (ret != 0) { - ctx->c_keep_alive = 0; - close_proxy = 1; - } - info("WebSocket connection closed"); - } else if (ctx->use_proxy) { + if (ctx->use_proxy) { + // reverse proxy return 3; } else if (strcmp(req->method, "HEAD") != 0) { // default response @@ -376,21 +365,13 @@ int respond(client_ctx_t *ctx) { return 2; } - if (ret < 0) { - ctx->c_keep_alive = 0; - } + if (ret < 0) ctx->c_keep_alive = 0; } return 0; } void 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]; ctx->req_e = clock_micros(); info("Transfer complete: %s", format_duration(ctx->req_e - ctx->req_s, buf));