From fa0f7ee6dba3ba14a6fc1172e04a161f514d94b0 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Tue, 27 Oct 2026 03:05:43 +0100 Subject: Revive http keep alive to save tcp connection setup costs --- ot_http.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'ot_http.c') diff --git a/ot_http.c b/ot_http.c index 36819c7..14952b5 100644 --- a/ot_http.c +++ b/ot_http.c @@ -727,6 +727,24 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) { /* Tell subroutines where to put reply data */ ws->reply = ws->outbuf + SUCCESS_HTTP_HEADER_LENGTH; + /* Find out if the client wants to keep this connection alive, before any + subroutine gets to answer: an error reply finishes with the connection + on its way out, and ws outlives the connection it was serving */ + ws->keep_alive = 0; +#ifdef WANT_KEEPALIVE + if ((write_ptr = http_header(ws->request, ws->header_size, "connection")) && (*write_ptr == 'K' || *write_ptr == 'k')) + ws->keep_alive = 1; +#endif + { + struct http_data *connection = fdstate_cookie(sock); + if (connection) { + if (ws->keep_alive) + connection->flag |= STRUCT_HTTP_FLAG_KEEPALIVE; + else + connection->flag &= ~STRUCT_HTTP_FLAG_KEEPALIVE; + } + } + /* This one implicitely tests strlen < 5, too -- remember, it is \n terminated */ if (memcmp(read_ptr, "GET /", 5)) HTTPERROR_400; @@ -763,14 +781,6 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) { else HTTPERROR_404; - /* Find out if the client wants to keep this connection alive */ - ws->keep_alive = 0; -#ifdef WANT_KEEPALIVE - read_ptr = http_header(ws->request, ws->header_size, "connection"); - if (read_ptr && (*read_ptr == 'K' || *read_ptr == 'k')) - ws->keep_alive = 1; -#endif - /* If routines handled sending themselves, just return */ if (ws->reply_size == -2) return 0; -- cgit v1.3