Outsource from request_handler

This commit is contained in:
2022-12-30 16:48:48 +01:00
parent 204317f46a
commit 28d7cf68df
7 changed files with 103 additions and 45 deletions

View File

@ -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;

View File

@ -14,24 +14,34 @@
#include <string.h>
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;
}

View File

@ -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

View File

@ -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:

View File

@ -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 <string.h>
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;
}

View File

@ -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;
}

View File

@ -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;
}