From ab05842b9fad52d1e722d9f0691425da91040f86 Mon Sep 17 00:00:00 2001 From: userhdisk <57322652+userhdisk@users.noreply.github.com> Date: Sun, 31 Jan 2021 20:04:55 +0100 Subject: [PATCH 1/2] ~ bugfixes grwprintf_t size if the grwprintf_t capacity is to small and the first snprintf failed do not count the first (snprintf) bytes as written --- httpserver.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpserver.h b/httpserver.h index 92787b0..c9a4138 100644 --- a/httpserver.h +++ b/httpserver.h @@ -1457,7 +1457,7 @@ void grwprintf(grwprintf_t* ctx, char const * fmt, ...) { *ctx->memused += ctx->capacity; ctx->buf = (char*)realloc(ctx->buf, ctx->capacity); assert(ctx->buf != NULL); - bytes += vsnprintf(ctx->buf + ctx->size, ctx->capacity - ctx->size, fmt, args); + bytes = vsnprintf(ctx->buf + ctx->size, ctx->capacity - ctx->size, fmt, args); } ctx->size += bytes; From e82a029f9829aff1d95078b4f887e97b9db0ec94 Mon Sep 17 00:00:00 2001 From: Philipp Czerner Date: Thu, 2 Dec 2021 20:42:46 +0100 Subject: [PATCH 2/2] Fix broken connection after interrupted write. When writing the answer to an HTTP request into the socket, the write might have to proceed in multiple parts. (E.g. if the receiver is not able to receive the data fast enough.) To deal with this situation, the server continues execution after a partial write, but changes the flag on the request's epoll object to trigger once the request's socket becomes writable again. However, once the response is fully written, the flags are not changed back, so that subsequent requests (for a keep-alive connection) do not trigger epoll, resulting in the server missing requests. --- httpserver.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/httpserver.h b/httpserver.h index 92787b0..a384804 100644 --- a/httpserver.h +++ b/httpserver.h @@ -525,6 +525,7 @@ void hs_server_init(struct http_server_s* serv); void hs_delete_events(struct http_request_s* request); void hs_add_events(struct http_request_s* request); void hs_add_write_event(struct http_request_s* request); +void hs_add_read_event(struct http_request_s* request); void hs_process_tokens(http_request_t* request); #ifdef KQUEUE @@ -1093,6 +1094,7 @@ void hs_write_response(http_request_t* request) { request->state = HTTP_SESSION_INIT; hs_free_buffer(request); hs_reset_timeout(request, HTTP_KEEP_ALIVE_TIMEOUT); + hs_add_read_event(request); } else { HTTP_FLAG_SET(request->flags, HTTP_END_SESSION); } @@ -1754,6 +1756,13 @@ void hs_add_write_event(http_request_t* request) { epoll_ctl(request->server->loop, EPOLL_CTL_MOD, request->socket, &ev); } +void hs_add_read_event(http_request_t* request) { + struct epoll_event ev; + ev.events = EPOLLIN | EPOLLET; + ev.data.ptr = request; + epoll_ctl(request->server->loop, EPOLL_CTL_MOD, request->socket, &ev); +} + #endif #endif