Allow proxies to delay chunks

This commit is contained in:
2023-01-26 12:46:30 +01:00
parent 8053439212
commit f7a6214dbc
6 changed files with 43 additions and 80 deletions

View File

@ -21,7 +21,7 @@ void chunk_handler_func(chunk_ctx_t *ctx) {
// error
error("Unable to splice chunk");
errno = 0;
ctx->err_cb(ctx->client);
ctx->err_cb(ctx);
} else if (sent == 0) {
// last chunk
ctx->client->chunks_transferred = 1;

View File

@ -77,4 +77,6 @@ int ws_handle_connection(client_ctx_t *ctx);
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 *));
#endif //SESIMOS_FUNC_H

View File

@ -28,14 +28,15 @@ void proxy_handler_func(client_ctx_t *ctx) {
if (ret == 1) {
proxy_unlock_ctx(ctx->proxy);
ctx->proxy->client = NULL;
ctx->proxy = NULL;
} else if (ctx->use_proxy == 0) {
proxy_close(ctx->proxy);
} else if (ctx->use_proxy == 1) {
proxy_handler_2(ctx);
if (proxy_handler_2(ctx) == 1) {
// chunked
return;
}
proxy_unlock_ctx(ctx->proxy);
ctx->proxy->client = NULL;
ctx->proxy = NULL;
} else if (ctx->use_proxy == 2) {
// WebSocket
@ -107,6 +108,19 @@ static int proxy_handler_1(client_ctx_t *ctx) {
return streq(ctx->req.method, "HEAD") ? 1 : 0;
}
static void proxy_chunk_next_cb(chunk_ctx_t *ctx) {
proxy_unlock_ctx(ctx->client->proxy);
ctx->client->proxy = NULL;
request_complete(ctx->client);
handle_request(ctx->client);
}
static void proxy_chunk_err_cb(chunk_ctx_t *ctx) {
ctx->client->c_keep_alive = 0;
proxy_chunk_next_cb(ctx);
}
static int proxy_handler_2(client_ctx_t *ctx) {
const char *transfer_encoding = http_get_header_field(&ctx->res.hdr, "Transfer-Encoding");
int chunked = strcontains(transfer_encoding, "chunked");
@ -114,10 +128,13 @@ static int proxy_handler_2(client_ctx_t *ctx) {
const char *content_len = http_get_header_field(&ctx->res.hdr, "Content-Length");
unsigned long len_to_send = (content_len != NULL) ? strtol(content_len, NULL, 10) : 0;
int flags = (chunked ? PROXY_CHUNKED : 0);
int ret = proxy_send(ctx->proxy, &ctx->socket, len_to_send, flags);
if (chunked) {
handle_chunks(ctx, &ctx->proxy->proxy, SOCK_CHUNKED, proxy_chunk_next_cb, proxy_chunk_err_cb);
return 1;
}
if (ret < 0) {
int ret;
if ((ret = proxy_send(ctx->proxy, &ctx->socket, len_to_send, 0)) == -1) {
ctx->c_keep_alive = 0;
}