Refactor proxy a bit

This commit is contained in:
2023-01-03 12:58:18 +01:00
parent fd1630a554
commit c13bea5ab4
5 changed files with 30 additions and 26 deletions

View File

@ -22,8 +22,7 @@
#include "config.h" #include "config.h"
typedef struct { typedef struct {
unsigned char initialized:1; unsigned char initialized:1, in_use:1;
unsigned char in_use:1;
sock proxy; sock proxy;
char *host; char *host;
} proxy_ctx_t; } proxy_ctx_t;

View File

@ -130,6 +130,9 @@ static void terminate_gracefully(int sig) {
sigaction(SIGINT, &act, NULL); sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL); sigaction(SIGTERM, &act, NULL);
// TODO close client connections
// TODO close proxy connections
workers_stop(); workers_stop();
workers_destroy(); workers_destroy();

View File

@ -18,7 +18,7 @@
typedef struct { typedef struct {
sock socket; sock socket;
int req_num; 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 cc[3], host[256];
char req_host[256], err_msg[256]; char req_host[256], err_msg[256];
char log_prefix[128]; char log_prefix[128];
@ -29,7 +29,6 @@ typedef struct {
http_uri uri; http_uri uri;
http_status_ctx status; http_status_ctx status;
http_status custom_status; http_status custom_status;
int use_fastcgi, use_proxy;
host_config_t *conf; host_config_t *conf;
FILE *file; FILE *file;
long content_length; long content_length;
@ -60,4 +59,6 @@ void request_complete(client_ctx_t *ctx);
void tcp_close(client_ctx_t *ctx); void tcp_close(client_ctx_t *ctx);
void proxy_close(proxy_ctx_t *ctx);
#endif //SESIMOS_FUNC_H #endif //SESIMOS_FUNC_H

View File

@ -14,6 +14,7 @@
#include "../workers.h" #include "../workers.h"
#include <string.h> #include <string.h>
#include <errno.h>
static int proxy_handler_1(client_ctx_t *ctx); static int proxy_handler_1(client_ctx_t *ctx);
static int proxy_handler_2(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); proxy_handler_1(ctx);
respond(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); proxy_handler_2(ctx);
request_complete(ctx); request_complete(ctx);
@ -126,3 +138,11 @@ static int proxy_handler_2(client_ctx_t *ctx) {
return ret; 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;
}

View File

@ -316,10 +316,7 @@ int respond(client_ctx_t *ctx) {
} }
} }
int close_proxy = 0;
if (ctx->use_proxy != 2) { 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, "Connection", HTTP_REMOVE_ALL);
http_remove_header_field(&res->hdr, "Keep-Alive", HTTP_REMOVE_ALL); http_remove_header_field(&res->hdr, "Keep-Alive", HTTP_REMOVE_ALL);
if (ctx->s_keep_alive && ctx->c_keep_alive) { if (ctx->s_keep_alive && ctx->c_keep_alive) {
@ -340,16 +337,8 @@ int respond(client_ctx_t *ctx) {
// TODO access/error log file // TODO access/error log file
if (ctx->use_proxy == 2) { if (ctx->use_proxy) {
// WebSocket // reverse proxy
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) {
return 3; return 3;
} else if (strcmp(req->method, "HEAD") != 0) { } else if (strcmp(req->method, "HEAD") != 0) {
// default response // default response
@ -376,21 +365,13 @@ int respond(client_ctx_t *ctx) {
return 2; return 2;
} }
if (ret < 0) { if (ret < 0) ctx->c_keep_alive = 0;
ctx->c_keep_alive = 0;
}
} }
return 0; return 0;
} }
void request_complete(client_ctx_t *ctx) { 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]; char buf[32];
ctx->req_e = clock_micros(); ctx->req_e = clock_micros();
info("Transfer complete: %s", format_duration(ctx->req_e - ctx->req_s, buf)); info("Transfer complete: %s", format_duration(ctx->req_e - ctx->req_s, buf));