diff --git a/src/base/net/http/HttpContext.cpp b/src/base/net/http/HttpContext.cpp index 65331d3a..de32f34d 100644 --- a/src/base/net/http/HttpContext.cpp +++ b/src/base/net/http/HttpContext.cpp @@ -67,6 +67,19 @@ xmrig::HttpContext::~HttpContext() } +void xmrig::HttpContext::close() +{ + auto it = m_storage.find(id()); + if (it != m_storage.end()) { + m_storage.erase(it); + } + + if (!uv_is_closing(handle())) { + uv_close(handle(), [](uv_handle_t *handle) -> void { delete reinterpret_cast(handle->data); }); + } +} + + xmrig::HttpContext *xmrig::HttpContext::get(uint64_t id) { if (m_storage.count(id) == 0) { @@ -125,15 +138,13 @@ void xmrig::HttpContext::attach(http_parser_settings *settings) } -void xmrig::HttpContext::close(uv_handle_t* handle) +void xmrig::HttpContext::closeAll() { - HttpContext *ctx = reinterpret_cast(handle->data); - auto it = m_storage.find(ctx->id()); - if (it != m_storage.end()) { - m_storage.erase(it); + for (auto kv : m_storage) { + if (!uv_is_closing(kv.second->handle())) { + uv_close(kv.second->handle(), [](uv_handle_t *handle) -> void { delete reinterpret_cast(handle->data); }); + } } - - delete ctx; } diff --git a/src/base/net/http/HttpContext.h b/src/base/net/http/HttpContext.h index 5e9c234a..3b2dead3 100644 --- a/src/base/net/http/HttpContext.h +++ b/src/base/net/http/HttpContext.h @@ -54,9 +54,11 @@ public: inline uv_stream_t *stream() const { return reinterpret_cast(tcp); } inline uv_handle_t *handle() const { return reinterpret_cast(tcp); } + void close(); + static HttpContext *get(uint64_t id); static void attach(http_parser_settings *settings); - static void close(uv_handle_t* handle); + static void closeAll(); http_parser *parser; IHttpListener *listener; diff --git a/src/base/net/http/HttpResponse.cpp b/src/base/net/http/HttpResponse.cpp index c9c51de7..77431a62 100644 --- a/src/base/net/http/HttpResponse.cpp +++ b/src/base/net/http/HttpResponse.cpp @@ -103,7 +103,5 @@ void xmrig::HttpResponse::end(const char *data, size_t size) HttpContext *ctx = HttpContext::get(m_id); uv_try_write(ctx->stream(), bufs, data ? 2 : 1); - if (!uv_is_closing(ctx->handle())) { - uv_close(ctx->handle(), HttpContext::close); - } + ctx->close(); } diff --git a/src/base/net/http/HttpServer.cpp b/src/base/net/http/HttpServer.cpp index 4addee3b..c6e02e44 100644 --- a/src/base/net/http/HttpServer.cpp +++ b/src/base/net/http/HttpServer.cpp @@ -51,7 +51,7 @@ xmrig::HttpServer::HttpServer(IHttpListener *listener) : xmrig::HttpServer::~HttpServer() { - memset(&http_settings, 0, sizeof (http_settings)); + HttpContext::closeAll(); } @@ -80,10 +80,10 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t) const size_t parsed = http_parser_execute(ctx->parser, &http_settings, buf->base, size); if (parsed < size) { - uv_close(ctx->handle(), HttpContext::close); + ctx->close(); } } else { - uv_close(ctx->handle(), HttpContext::close); + ctx->close(); } delete [] buf->base;