diff --git a/src/http.c b/src/http.c index c8252a6a..35283f08 100644 --- a/src/http.c +++ b/src/http.c @@ -156,17 +156,30 @@ static void _http_connection_on_close(uv_handle_t* handle) _http_connection_destroy(connection, "handle closed"); } +static void _http_request_destroy(tf_http_request_t* request) +{ + tf_http_close_callback* on_close = request->on_close; + if (on_close) + { + request->on_close = NULL; + tf_trace_begin(request->http->trace, request->connection && request->connection->trace_name ? request->connection->trace_name : "websocket"); + on_close(request); + tf_trace_end(request->http->trace); + } +} + static void _http_connection_destroy(tf_http_connection_t* connection, const char* reason) { connection->is_shutting_down = true; - if (connection->request && connection->request->on_close) + if (connection->request) { - tf_http_close_callback* on_close = connection->request->on_close; - connection->request->on_close = NULL; - tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "websocket"); - on_close(connection->request); - tf_trace_end(connection->http->trace); + _http_request_destroy(connection->request); + if (connection->request->ref_count == 0) + { + tf_free(connection->request); + } + connection->request = NULL; } if (connection->tcp.data && !uv_is_closing((uv_handle_t*)&connection->tcp)) @@ -949,29 +962,28 @@ void tf_http_request_ref(tf_http_request_t* request) void tf_http_request_unref(tf_http_request_t* request) { - bool connection_destroyed = false; - - if (--request->connection->ref_count == 0) - { - if (request->connection->http->is_shutting_down) - { - _http_connection_destroy(request->connection, "unref during shutdown"); - connection_destroyed = true; - } - else if (!request->connection->is_websocket) - { - _http_reset_connection(request->connection); - } - } - + tf_http_connection_t* connection = request->connection; if (--request->ref_count == 0) { - if (!connection_destroyed) + _http_request_destroy(request); + if (connection) { - request->connection->request = NULL; + connection->request = NULL; } tf_free(request); } + + if (--connection->ref_count == 0) + { + if (connection->http->is_shutting_down) + { + _http_connection_destroy(connection, "unref during shutdown"); + } + else if (!connection->is_websocket) + { + _http_reset_connection(connection); + } + } } static const char* _http_connection_get_header(const tf_http_connection_t* connection, const char* name)