Add async support for FastCGI
This commit is contained in:
70
src/worker/fastcgi_frame_handler.c
Normal file
70
src/worker/fastcgi_frame_handler.c
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* sesimos - secure, simple, modern web server
|
||||
* @brief FastCGI frame handler
|
||||
* @file src/worker/fcti_frame_handler.c
|
||||
* @author Lorenz Stechauner
|
||||
* @date 2023-01-22
|
||||
*/
|
||||
|
||||
#include "func.h"
|
||||
#include "../lib/fastcgi.h"
|
||||
#include "../logger.h"
|
||||
#include "../workers.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <memory.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void fastcgi_frame_handler_func(fastcgi_ctx_t *ctx) {
|
||||
logger_set_prefix("[%*s]%s", ADDRSTRLEN, ctx->client->socket.s_addr, ctx->client->log_prefix);
|
||||
|
||||
switch (fastcgi_recv_frame(&ctx->cnx)) {
|
||||
case FCGI_STDOUT:
|
||||
case FCGI_STDERR:
|
||||
fastcgi_handle_frame(ctx);
|
||||
break;
|
||||
case -1:
|
||||
error("Unable to receive FastCGI frame");
|
||||
ctx->client->s_keep_alive = 0;
|
||||
fastcgi_close(ctx);
|
||||
break;
|
||||
default:
|
||||
// end of request received
|
||||
write(ctx->cnx.fd_out, "\0\0\0\0\0\0\0\0\r\n", 10);
|
||||
fastcgi_close(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
int fastcgi_handle_connection(client_ctx_t *ctx, fastcgi_cnx_t **cnx) {
|
||||
sock_set_timeout(&(*cnx)->socket, FASTCGI_TIMEOUT);
|
||||
sock_set_socket_timeout(&(*cnx)->socket, 1);
|
||||
|
||||
fastcgi_ctx_t *a = malloc(sizeof(fastcgi_ctx_t));
|
||||
a->closed = 0;
|
||||
a->client = ctx;
|
||||
memcpy(&a->cnx, *cnx, sizeof(fastcgi_cnx_t));
|
||||
ctx->fcgi_cnx = a;
|
||||
fastcgi_handle_frame(a);
|
||||
*cnx = &a->cnx;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fastcgi_close(fastcgi_ctx_t *ctx) {
|
||||
if (ctx->closed == 0) {
|
||||
ctx->closed++;
|
||||
return;
|
||||
}
|
||||
|
||||
logger_set_prefix("[%*s]%s", ADDRSTRLEN, ctx->client->socket.s_addr, ctx->client->log_prefix);
|
||||
|
||||
if (ctx->cnx.app_status != 0)
|
||||
error("FastCGI app terminated with exit code %i", ctx->cnx.app_status);
|
||||
|
||||
debug("Closing FastCGI connection");
|
||||
|
||||
fastcgi_close_cnx(&ctx->cnx);
|
||||
ctx->client->fcgi_cnx = NULL;
|
||||
free(ctx);
|
||||
errno = 0;
|
||||
}
|
@ -9,40 +9,48 @@
|
||||
#include "func.h"
|
||||
#include "../logger.h"
|
||||
#include "../lib/utils.h"
|
||||
#include "../lib/compress.h"
|
||||
#include "../workers.h"
|
||||
#include "../lib/fastcgi.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx);
|
||||
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) {
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, ADDRSTRLEN, ctx->req_host, CLR_STR, ctx->log_prefix);
|
||||
|
||||
fastcgi_cnx_t fcgi_cnx;
|
||||
int ret = fastcgi_handler_1(ctx, &fcgi_cnx);
|
||||
respond(ctx);
|
||||
if (ret == 0) fastcgi_handler_2(ctx, &fcgi_cnx);
|
||||
request_complete(ctx);
|
||||
if (!ctx->chunks_transferred) {
|
||||
fastcgi_cnx_t *fcgi_cnx = NULL;
|
||||
int ret = fastcgi_handler_1(ctx, &fcgi_cnx);
|
||||
respond(ctx);
|
||||
if (ret == 0) {
|
||||
switch (fastcgi_handler_2(ctx, fcgi_cnx)) {
|
||||
case 1: return;
|
||||
case 2: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
request_complete(ctx);
|
||||
handle_request(ctx);
|
||||
}
|
||||
|
||||
static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
|
||||
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;
|
||||
char *err_msg = ctx->err_msg;
|
||||
|
||||
fcgi_cnx->socket.socket = 0;
|
||||
fcgi_cnx->socket.enc = 0;
|
||||
fcgi_cnx->req_id = 0;
|
||||
fcgi_cnx->r_addr = ctx->socket.addr;
|
||||
fcgi_cnx->r_host = (ctx->host[0] != 0) ? ctx->host : NULL;
|
||||
fastcgi_cnx_t fcgi_cnx_buf;
|
||||
(*fcgi_cnx) = &fcgi_cnx_buf;
|
||||
sock_init(&(*fcgi_cnx)->socket, 0, 0);
|
||||
(*fcgi_cnx)->req_id = 0;
|
||||
(*fcgi_cnx)->r_addr = ctx->socket.addr;
|
||||
(*fcgi_cnx)->r_host = (ctx->host[0] != 0) ? ctx->host : NULL;
|
||||
|
||||
char buf[1024];
|
||||
|
||||
@ -61,19 +69,21 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
|
||||
http_add_header_field(&res->hdr, "Last-Modified", last_modified);
|
||||
|
||||
res->status = http_get_status(200);
|
||||
if (fastcgi_init(fcgi_cnx, mode, ctx->req_num, client, req, uri) != 0) {
|
||||
if (fastcgi_init(*fcgi_cnx, mode, ctx->req_num, client, req, uri) != 0) {
|
||||
res->status = http_get_status(503);
|
||||
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
|
||||
return 2;
|
||||
}
|
||||
|
||||
fastcgi_handle_connection(ctx, fcgi_cnx);
|
||||
|
||||
const char *client_content_length = http_get_header_field(&req->hdr, "Content-Length");
|
||||
const char *client_transfer_encoding = http_get_header_field(&req->hdr, "Transfer-Encoding");
|
||||
if (client_content_length != NULL) {
|
||||
unsigned long client_content_len = strtoul(client_content_length, NULL, 10);
|
||||
ret = fastcgi_receive(fcgi_cnx, client, client_content_len);
|
||||
ret = fastcgi_receive(*fcgi_cnx, client, client_content_len);
|
||||
} else if (strcontains(client_transfer_encoding, "chunked")) {
|
||||
ret = fastcgi_receive_chunked(fcgi_cnx, client);
|
||||
ret = fastcgi_receive_chunked(*fcgi_cnx, client);
|
||||
} else {
|
||||
ret = 0;
|
||||
}
|
||||
@ -86,10 +96,11 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
|
||||
res->status = http_get_status(502);
|
||||
return 2;
|
||||
}
|
||||
fastcgi_close_stdin(fcgi_cnx);
|
||||
fastcgi_close_stdin(*fcgi_cnx);
|
||||
|
||||
ret = fastcgi_header(fcgi_cnx, res, err_msg);
|
||||
ret = fastcgi_header(*fcgi_cnx, res, err_msg);
|
||||
if (ret != 0) {
|
||||
res->status = http_get_status(502);
|
||||
return (ret < 0) ? -1 : 1;
|
||||
}
|
||||
|
||||
@ -121,7 +132,7 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
|
||||
ctx->content_length != -1 &&
|
||||
ctx->content_length <= sizeof(ctx->msg_content) - 1)
|
||||
{
|
||||
fastcgi_dump(fcgi_cnx, ctx->msg_content, sizeof(ctx->msg_content));
|
||||
fastcgi_dump(*fcgi_cnx, ctx->msg_content, sizeof(ctx->msg_content));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -135,19 +146,43 @@ static int fastcgi_handler_1(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void fastcgi_next_cb(chunk_ctx_t *ctx) {
|
||||
if(ctx->client->fcgi_cnx) {
|
||||
fastcgi_close(ctx->client->fcgi_cnx);
|
||||
ctx->client->fcgi_cnx = NULL;
|
||||
}
|
||||
|
||||
fastcgi_handle(ctx->client);
|
||||
}
|
||||
|
||||
static void fastcgi_error_cb(chunk_ctx_t *ctx) {
|
||||
if (ctx->client->chunks_transferred)
|
||||
return;
|
||||
|
||||
logger_set_prefix("[%s%*s%s]%s", BLD_STR, ADDRSTRLEN, ctx->client->req_host, CLR_STR, ctx->client->log_prefix);
|
||||
|
||||
warning("Closing connection due to FastCGI error");
|
||||
if(ctx->client->fcgi_cnx) {
|
||||
fastcgi_close(ctx->client->fcgi_cnx);
|
||||
ctx->client->fcgi_cnx = NULL;
|
||||
}
|
||||
|
||||
tcp_close(ctx->client);
|
||||
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
static int fastcgi_handler_2(client_ctx_t *ctx, fastcgi_cnx_t *fcgi_cnx) {
|
||||
int chunked = strcontains(http_get_header_field(&ctx->res.hdr, "Transfer-Encoding"), "chunked");
|
||||
|
||||
int flags = (chunked ? FASTCGI_CHUNKED : 0);
|
||||
int ret = fastcgi_send(fcgi_cnx, &ctx->socket, flags);
|
||||
if (ret < 0) {
|
||||
ctx->c_keep_alive = 0;
|
||||
errno = 0;
|
||||
if (chunked) {
|
||||
handle_chunks(ctx, &fcgi_cnx->out, SOCK_CHUNKED, fastcgi_next_cb, fastcgi_error_cb);
|
||||
return 1;
|
||||
} else {
|
||||
fastcgi_send(fcgi_cnx, &ctx->socket);
|
||||
fastcgi_close(ctx->fcgi_cnx);
|
||||
ctx->fcgi_cnx = NULL;
|
||||
fastcgi_handle(ctx);
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (fcgi_cnx->socket.socket != 0) {
|
||||
sock_close(&fcgi_cnx->socket);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
#include "../lib/uri.h"
|
||||
#include "../lib/config.h"
|
||||
#include "../lib/proxy.h"
|
||||
#include "../lib/fastcgi.h"
|
||||
|
||||
typedef struct {
|
||||
sock socket;
|
||||
@ -35,6 +36,7 @@ typedef struct {
|
||||
long content_length;
|
||||
char *msg_buf, *msg_buf_ptr, msg_content[1024];
|
||||
proxy_ctx_t *proxy;
|
||||
void *fcgi_cnx;
|
||||
} client_ctx_t;
|
||||
|
||||
typedef struct {
|
||||
@ -43,6 +45,12 @@ typedef struct {
|
||||
void *other;
|
||||
} ws_ctx_t;
|
||||
|
||||
typedef struct {
|
||||
int closed:2;
|
||||
client_ctx_t *client;
|
||||
fastcgi_cnx_t cnx;
|
||||
} fastcgi_ctx_t;
|
||||
|
||||
typedef struct {
|
||||
client_ctx_t *client;
|
||||
sock *socket;
|
||||
@ -59,6 +67,8 @@ void local_handler_func(client_ctx_t *ctx);
|
||||
|
||||
void fastcgi_handler_func(client_ctx_t *ctx);
|
||||
|
||||
void fastcgi_frame_handler_func(fastcgi_ctx_t *ctx);
|
||||
|
||||
void proxy_handler_func(client_ctx_t *ctx);
|
||||
|
||||
void ws_frame_handler_func(ws_ctx_t *ctx);
|
||||
@ -79,4 +89,8 @@ void ws_close(ws_ctx_t *ctx);
|
||||
|
||||
int handle_chunks(client_ctx_t *ctx, sock *socket, int flags, void (*next_cb)(chunk_ctx_t *), void (*err_cb)(chunk_ctx_t *));
|
||||
|
||||
int fastcgi_handle_connection(client_ctx_t *ctx, fastcgi_cnx_t **cnx);
|
||||
|
||||
void fastcgi_close(fastcgi_ctx_t *ctx);
|
||||
|
||||
#endif //SESIMOS_FUNC_H
|
||||
|
@ -49,6 +49,7 @@ static void init_ctx(client_ctx_t *ctx) {
|
||||
ctx->proxy = NULL;
|
||||
ctx->use_fastcgi = 0;
|
||||
ctx->chunks_transferred = 0;
|
||||
ctx->fcgi_cnx = NULL;
|
||||
ctx->use_proxy = 0;
|
||||
ctx->ws_close = 0;
|
||||
ctx->proxy = NULL;
|
||||
|
Reference in New Issue
Block a user