From 86046e52f0c817708ca20eb438eb4110b9854f8d Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 21 Dec 2023 17:15:59 +0000 Subject: [PATCH] One less dynamic http allocation. Also one less crash. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4689 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/http.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/http.c b/src/http.c index f8d7ca6c..afefdc9e 100644 --- a/src/http.c +++ b/src/http.c @@ -19,6 +19,7 @@ typedef struct _tf_http_connection_t { tf_http_t* http; uv_tcp_t tcp; + uv_shutdown_t shutdown; const char* method; const char* path; @@ -295,7 +296,7 @@ static void _http_on_connection(uv_stream_t* stream, int status) return; } - http->connections = tf_realloc(http->connections, http->connections_count + 1); + http->connections = tf_realloc(http->connections, sizeof(tf_http_connection_t*) * (http->connections_count + 1)); http->connections[http->connections_count++] = connection; } @@ -403,7 +404,7 @@ static const char* _http_status_text(int status) static void _http_on_shutdown(uv_shutdown_t* request, int status) { - tf_free(request); + request->data = NULL; } void tf_http_respond(tf_http_request_t* request, int status, const char** headers, int headers_count, const void* body, size_t content_length) @@ -457,11 +458,11 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header tf_printf("uv_write: %s\n", uv_strerror(r)); } - if (request->connection->connection_close) + if (request->connection->connection_close && + !request->connection->shutdown.data) { - uv_shutdown_t* shutdown_request = tf_malloc(sizeof(uv_shutdown_t)); - *shutdown_request = (uv_shutdown_t) { .data = request }; - uv_shutdown(shutdown_request, (uv_stream_t*)&request->connection->tcp, _http_on_shutdown); + request->connection->shutdown.data = request->connection; + uv_shutdown(&request->connection->shutdown, (uv_stream_t*)&request->connection->tcp, _http_on_shutdown); } tf_free(request); }