Add chunk_handler

This commit is contained in:
2023-01-24 00:03:24 +01:00
parent df7dfb5107
commit e721e542f3
5 changed files with 87 additions and 9 deletions

View File

@ -0,0 +1,50 @@
/**
* 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 "../logger.h"
#include "../workers.h"
#include <errno.h>
void chunk_handler_func(chunk_ctx_t *ctx) {
logger_set_prefix("[%*s]%s", ADDRSTRLEN, ctx->client->socket.s_addr, ctx->client->log_prefix);
char buf[CHUNK_SIZE];
long sent = sock_splice_chunked(&ctx->client->socket, ctx->socket, buf, sizeof(buf), ctx->flags | SOCK_SINGLE_CHUNK);
if (sent < 0) {
// error
error("Unable to splice chunk");
errno = 0;
ctx->err_cb(ctx->client);
} else if (sent == 0) {
// last chunk
ctx->client->chunks_transferred = 1;
ctx->next_cb(ctx);
} else {
// next chunk
handle_chunk(ctx);
return;
}
free(ctx);
}
int handle_chunks(client_ctx_t *ctx, sock *socket, int flags, void (*next_cb)(chunk_ctx_t *), void (*err_cb)(chunk_ctx_t *)) {
chunk_ctx_t *a = malloc(sizeof(chunk_ctx_t));
a->client = ctx;
a->socket = socket;
a->flags = flags;
a->next_cb = (void (*)(void *)) next_cb;
a->err_cb = (void (*)(void *)) err_cb;
handle_chunk(a);
return 0;
}

View File

@ -19,7 +19,7 @@
typedef struct {
sock socket;
int req_num;
unsigned char s_keep_alive:1, c_keep_alive:1, use_fastcgi:4, use_proxy:2, ws_close:2;
unsigned char s_keep_alive:1, c_keep_alive:1, use_fastcgi:4, use_proxy:2, ws_close:2, chunks_transferred:1;
char cc[3], host[256];
char req_host[256], err_msg[256];
char log_prefix[128];
@ -43,6 +43,14 @@ typedef struct {
void *other;
} ws_ctx_t;
typedef struct {
client_ctx_t *client;
sock *socket;
int flags;
void (*next_cb)(void *);
void (*err_cb)(void *);
} chunk_ctx_t;
void tcp_acceptor_func(client_ctx_t *ctx);
void request_handler_func(client_ctx_t *ctx);
@ -55,6 +63,8 @@ void proxy_handler_func(client_ctx_t *ctx);
void ws_frame_handler_func(ws_ctx_t *ctx);
void chunk_handler_func(chunk_ctx_t *ctx);
int respond(client_ctx_t *ctx);
void request_complete(client_ctx_t *ctx);