summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-10-27 03:05:43 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-10-27 03:05:43 +0100
commitfa0f7ee6dba3ba14a6fc1172e04a161f514d94b0 (patch)
treef418d15698caf353e25557f8139b3d87a1513b32
parent0d6e0044a104176d6d6f2111fda1d7c1f160a583 (diff)
Revive http keep alive to save tcp connection setup costs
-rw-r--r--opentracker.c17
-rw-r--r--ot_http.c26
2 files changed, 30 insertions, 13 deletions
diff --git a/opentracker.c b/opentracker.c
index 21eeb42..11da486 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -177,12 +177,13 @@ static void handle_read(const int64_t sock, struct ot_workstruct *ws) {
177} 177}
178 178
179static void handle_write(const int64_t sock) { 179static void handle_write(const int64_t sock) {
180 struct http_data *cookie = fdstate_cookie(sock); 180 struct http_data *cookie = fdstate_cookie(sock);
181 int chunked = 0; 181 int chunked = 0;
182 int keep_alive = 0;
182 183
183 if (cookie) { 184 if (cookie) {
184 if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) 185 chunked = (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) != 0;
185 chunked = 1; 186 keep_alive = (cookie->flag & STRUCT_HTTP_FLAG_KEEPALIVE) != 0;
186 187
187 if (cookie->batch) { 188 if (cookie->batch) {
188 if (batch_send(sock, cookie->batch) == BATCH_FAILED) { 189 if (batch_send(sock, cookie->batch) == BATCH_FAILED) {
@@ -197,7 +198,13 @@ static void handle_write(const int64_t sock) {
197 /* In a chunked transfer, wait for the next piece once everything queued has gone out */ 198 /* In a chunked transfer, wait for the next piece once everything queued has gone out */
198 if (chunked) 199 if (chunked)
199 http_interest_drop(sock, cookie, EVENT_OUT); 200 http_interest_drop(sock, cookie, EVENT_OUT);
200 else 201 else if (keep_alive) {
202 /* The reply is out and the client asked to keep the connection, so stop
203 waiting to write and go back to waiting for the next request */
204 http_interest_drop(sock, cookie, EVENT_OUT);
205 http_interest_add(sock, cookie, EVENT_IN);
206 fdstate_set_expiry(sock, g_now_seconds + OT_CLIENT_TIMEOUT);
207 } else
201 handle_dead(sock); 208 handle_dead(sock);
202} 209}
203 210
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) {
727 /* Tell subroutines where to put reply data */ 727 /* Tell subroutines where to put reply data */
728 ws->reply = ws->outbuf + SUCCESS_HTTP_HEADER_LENGTH; 728 ws->reply = ws->outbuf + SUCCESS_HTTP_HEADER_LENGTH;
729 729
730 /* Find out if the client wants to keep this connection alive, before any
731 subroutine gets to answer: an error reply finishes with the connection
732 on its way out, and ws outlives the connection it was serving */
733 ws->keep_alive = 0;
734#ifdef WANT_KEEPALIVE
735 if ((write_ptr = http_header(ws->request, ws->header_size, "connection")) && (*write_ptr == 'K' || *write_ptr == 'k'))
736 ws->keep_alive = 1;
737#endif
738 {
739 struct http_data *connection = fdstate_cookie(sock);
740 if (connection) {
741 if (ws->keep_alive)
742 connection->flag |= STRUCT_HTTP_FLAG_KEEPALIVE;
743 else
744 connection->flag &= ~STRUCT_HTTP_FLAG_KEEPALIVE;
745 }
746 }
747
730 /* This one implicitely tests strlen < 5, too -- remember, it is \n terminated */ 748 /* This one implicitely tests strlen < 5, too -- remember, it is \n terminated */
731 if (memcmp(read_ptr, "GET /", 5)) 749 if (memcmp(read_ptr, "GET /", 5))
732 HTTPERROR_400; 750 HTTPERROR_400;
@@ -763,14 +781,6 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) {
763 else 781 else
764 HTTPERROR_404; 782 HTTPERROR_404;
765 783
766 /* Find out if the client wants to keep this connection alive */
767 ws->keep_alive = 0;
768#ifdef WANT_KEEPALIVE
769 read_ptr = http_header(ws->request, ws->header_size, "connection");
770 if (read_ptr && (*read_ptr == 'K' || *read_ptr == 'k'))
771 ws->keep_alive = 1;
772#endif
773
774 /* If routines handled sending themselves, just return */ 784 /* If routines handled sending themselves, just return */
775 if (ws->reply_size == -2) 785 if (ws->reply_size == -2)
776 return 0; 786 return 0;