diff --git a/src/api/Httpd.cpp b/src/api/Httpd.cpp index f3cd7d9fb..c1ad1151b 100644 --- a/src/api/Httpd.cpp +++ b/src/api/Httpd.cpp @@ -102,8 +102,10 @@ void xmrig::Httpd::onConfigChanged(Config *config, Config *previousConfig) } -void xmrig::Httpd::onHttp(const HttpRequest &req, HttpResponse &res) +void xmrig::Httpd::onHttpRequest(const HttpRequest &req) { + HttpResponse res(req.id()); + LOG_INFO(GREEN_BOLD_S "OK"); res.end(); } diff --git a/src/api/Httpd.h b/src/api/Httpd.h index 1c01fbdd4..6a6a1baec 100644 --- a/src/api/Httpd.h +++ b/src/api/Httpd.h @@ -52,7 +52,7 @@ public: protected: void onConfigChanged(Config *config, Config *previousConfig) override; - void onHttp(const HttpRequest &req, HttpResponse &res) override; + void onHttpRequest(const HttpRequest &req) override; private: Controller *m_controller; diff --git a/src/base/kernel/interfaces/IHttpListener.h b/src/base/kernel/interfaces/IHttpListener.h index 73feff54c..d4d905ca6 100644 --- a/src/base/kernel/interfaces/IHttpListener.h +++ b/src/base/kernel/interfaces/IHttpListener.h @@ -38,7 +38,7 @@ class IHttpListener public: virtual ~IHttpListener() = default; - virtual void onHttp(const HttpRequest &req, HttpResponse &res) = 0; + virtual void onHttpRequest(const HttpRequest &req) = 0; }; diff --git a/src/base/net/http/HttpContext.cpp b/src/base/net/http/HttpContext.cpp index e125b5b91..29dfde7ac 100644 --- a/src/base/net/http/HttpContext.cpp +++ b/src/base/net/http/HttpContext.cpp @@ -32,11 +32,22 @@ #include "base/net/http/HttpContext.h" +namespace xmrig { + +static uint64_t SEQUENCE = 0; +std::map HttpContext::m_storage; + +} // namespace xmrig + + xmrig::HttpContext::HttpContext(int parser_type, IHttpListener *listener) : + HttpRequest(SEQUENCE++), listener(listener), connect(nullptr), m_wasHeaderValue(false) { + m_storage[id()] = this; + parser = new http_parser; tcp = new uv_tcp_t; @@ -55,6 +66,16 @@ xmrig::HttpContext::~HttpContext() } +xmrig::HttpContext *xmrig::HttpContext::get(uint64_t id) +{ + if (m_storage.count(id) == 0) { + return nullptr; + } + + return m_storage[id]; +} + + void xmrig::HttpContext::attach(http_parser_settings *settings) { settings->on_message_begin = nullptr; @@ -93,7 +114,13 @@ void xmrig::HttpContext::attach(http_parser_settings *settings) void xmrig::HttpContext::close(uv_handle_t* handle) { - delete reinterpret_cast(handle->data); + HttpContext *ctx = reinterpret_cast(handle->data); + auto it = m_storage.find(ctx->id()); + if (it != m_storage.end()) { + m_storage.erase(it); + } + + delete ctx; } diff --git a/src/base/net/http/HttpContext.h b/src/base/net/http/HttpContext.h index 06c66e3bb..5e9c234aa 100644 --- a/src/base/net/http/HttpContext.h +++ b/src/base/net/http/HttpContext.h @@ -54,6 +54,7 @@ public: inline uv_stream_t *stream() const { return reinterpret_cast(tcp); } inline uv_handle_t *handle() const { return reinterpret_cast(tcp); } + static HttpContext *get(uint64_t id); static void attach(http_parser_settings *settings); static void close(uv_handle_t* handle); @@ -71,6 +72,8 @@ private: bool m_wasHeaderValue; std::string m_lastHeaderField; std::string m_lastHeaderValue; + + static std::map m_storage; }; diff --git a/src/base/net/http/HttpRequest.h b/src/base/net/http/HttpRequest.h index 2f2f22a87..25f77aa54 100644 --- a/src/base/net/http/HttpRequest.h +++ b/src/base/net/http/HttpRequest.h @@ -39,10 +39,17 @@ namespace xmrig { class HttpRequest { public: + inline HttpRequest(uint64_t id) : m_id(id) {} + + inline uint64_t id() const { return m_id; } + std::string url; std::string method; std::stringstream body; std::map headers; + +private: + const uint64_t m_id; }; diff --git a/src/base/net/http/HttpResponse.cpp b/src/base/net/http/HttpResponse.cpp index ffccf1c97..f53eb894e 100644 --- a/src/base/net/http/HttpResponse.cpp +++ b/src/base/net/http/HttpResponse.cpp @@ -40,18 +40,23 @@ static const char *kTransferEncoding = "Transfer-Encoding"; } // namespace xmrig -xmrig::HttpResponse::HttpResponse() : - parser(nullptr), +xmrig::HttpResponse::HttpResponse(uint64_t id) : statusCode(HTTP_STATUS_OK), body(""), statusAdjective("OK"), // FIXME - m_writtenOrEnded(false) + m_writtenOrEnded(false), + m_id(id) { } void xmrig::HttpResponse::writeOrEnd(const std::string &str, bool end) { + HttpContext *context = HttpContext::get(m_id); + if (!context) { + return; + } + std::stringstream ss; if (!m_writtenOrEnded) { @@ -84,8 +89,6 @@ void xmrig::HttpResponse::writeOrEnd(const std::string &str, bool end) uv_buf_t resbuf = uv_buf_init(const_cast(out.c_str()), out.size()); # endif - HttpContext* context = static_cast(parser->data); - uv_try_write(context->stream(), &resbuf, 1); if (end) { diff --git a/src/base/net/http/HttpResponse.h b/src/base/net/http/HttpResponse.h index 09a5b6fa1..4757bd891 100644 --- a/src/base/net/http/HttpResponse.h +++ b/src/base/net/http/HttpResponse.h @@ -33,16 +33,13 @@ #include -typedef struct http_parser http_parser; - - namespace xmrig { class HttpResponse { public: - HttpResponse(); + HttpResponse(uint64_t id); inline void end() { writeOrEnd("", true); } inline void end(const std::string &str) { writeOrEnd(str, true); } @@ -50,7 +47,6 @@ public: inline void setStatus(int code) { statusCode = code; } inline void write(const std::string &str) { writeOrEnd(str, false); } - http_parser *parser; int statusCode; std::map headers; std::string body; @@ -60,6 +56,7 @@ private: void writeOrEnd(const std::string &str, bool end); bool m_writtenOrEnded = false; + const uint64_t m_id; }; diff --git a/src/base/net/http/HttpServer.cpp b/src/base/net/http/HttpServer.cpp index 818b212e2..245451c2d 100644 --- a/src/base/net/http/HttpServer.cpp +++ b/src/base/net/http/HttpServer.cpp @@ -97,11 +97,9 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t) int xmrig::HttpServer::onComplete(http_parser *parser) { HttpContext *context = reinterpret_cast(parser->data); + HttpResponse res(context->id()); - HttpResponse res; - res.parser = parser; - - context->listener->onHttp(*context, res); + context->listener->onHttpRequest(*context); return 0; }