mirror of
https://github.com/xmrig/xmrig.git
synced 2025-04-16 11:41:55 +00:00
Change HttpResponse creation method.
This commit is contained in:
parent
01ad6bf2d9
commit
9daa5874f5
9 changed files with 55 additions and 18 deletions
src
api
base
kernel/interfaces
net/http
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -32,11 +32,22 @@
|
|||
#include "base/net/http/HttpContext.h"
|
||||
|
||||
|
||||
namespace xmrig {
|
||||
|
||||
static uint64_t SEQUENCE = 0;
|
||||
std::map<uint64_t, HttpContext *> 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<HttpContext*>(handle->data);
|
||||
HttpContext *ctx = reinterpret_cast<HttpContext*>(handle->data);
|
||||
auto it = m_storage.find(ctx->id());
|
||||
if (it != m_storage.end()) {
|
||||
m_storage.erase(it);
|
||||
}
|
||||
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ public:
|
|||
inline uv_stream_t *stream() const { return reinterpret_cast<uv_stream_t *>(tcp); }
|
||||
inline uv_handle_t *handle() const { return reinterpret_cast<uv_handle_t *>(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<uint64_t, HttpContext *> m_storage;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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<const std::string, const std::string> headers;
|
||||
|
||||
private:
|
||||
const uint64_t m_id;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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<char *>(out.c_str()), out.size());
|
||||
# endif
|
||||
|
||||
HttpContext* context = static_cast<HttpContext*>(parser->data);
|
||||
|
||||
uv_try_write(context->stream(), &resbuf, 1);
|
||||
|
||||
if (end) {
|
||||
|
|
|
@ -33,16 +33,13 @@
|
|||
#include <string>
|
||||
|
||||
|
||||
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<const std::string, const std::string> 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;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -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<HttpContext*>(parser->data);
|
||||
HttpResponse res(context->id());
|
||||
|
||||
HttpResponse res;
|
||||
res.parser = parser;
|
||||
|
||||
context->listener->onHttp(*context, res);
|
||||
context->listener->onHttpRequest(*context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue