mirror of
https://github.com/xmrig/xmrig.git
synced 2024-11-05 16:07:42 +00:00
Change HttpResponse creation method.
This commit is contained in:
parent
01ad6bf2d9
commit
9daa5874f5
9 changed files with 55 additions and 18 deletions
|
@ -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");
|
LOG_INFO(GREEN_BOLD_S "OK");
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void onConfigChanged(Config *config, Config *previousConfig) override;
|
void onConfigChanged(Config *config, Config *previousConfig) override;
|
||||||
void onHttp(const HttpRequest &req, HttpResponse &res) override;
|
void onHttpRequest(const HttpRequest &req) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Controller *m_controller;
|
Controller *m_controller;
|
||||||
|
|
|
@ -38,7 +38,7 @@ class IHttpListener
|
||||||
public:
|
public:
|
||||||
virtual ~IHttpListener() = default;
|
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"
|
#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) :
|
xmrig::HttpContext::HttpContext(int parser_type, IHttpListener *listener) :
|
||||||
|
HttpRequest(SEQUENCE++),
|
||||||
listener(listener),
|
listener(listener),
|
||||||
connect(nullptr),
|
connect(nullptr),
|
||||||
m_wasHeaderValue(false)
|
m_wasHeaderValue(false)
|
||||||
{
|
{
|
||||||
|
m_storage[id()] = this;
|
||||||
|
|
||||||
parser = new http_parser;
|
parser = new http_parser;
|
||||||
tcp = new uv_tcp_t;
|
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)
|
void xmrig::HttpContext::attach(http_parser_settings *settings)
|
||||||
{
|
{
|
||||||
settings->on_message_begin = nullptr;
|
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)
|
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_stream_t *stream() const { return reinterpret_cast<uv_stream_t *>(tcp); }
|
||||||
inline uv_handle_t *handle() const { return reinterpret_cast<uv_handle_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 attach(http_parser_settings *settings);
|
||||||
static void close(uv_handle_t* handle);
|
static void close(uv_handle_t* handle);
|
||||||
|
|
||||||
|
@ -71,6 +72,8 @@ private:
|
||||||
bool m_wasHeaderValue;
|
bool m_wasHeaderValue;
|
||||||
std::string m_lastHeaderField;
|
std::string m_lastHeaderField;
|
||||||
std::string m_lastHeaderValue;
|
std::string m_lastHeaderValue;
|
||||||
|
|
||||||
|
static std::map<uint64_t, HttpContext *> m_storage;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -39,10 +39,17 @@ namespace xmrig {
|
||||||
class HttpRequest
|
class HttpRequest
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
inline HttpRequest(uint64_t id) : m_id(id) {}
|
||||||
|
|
||||||
|
inline uint64_t id() const { return m_id; }
|
||||||
|
|
||||||
std::string url;
|
std::string url;
|
||||||
std::string method;
|
std::string method;
|
||||||
std::stringstream body;
|
std::stringstream body;
|
||||||
std::map<const std::string, const std::string> headers;
|
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
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
xmrig::HttpResponse::HttpResponse() :
|
xmrig::HttpResponse::HttpResponse(uint64_t id) :
|
||||||
parser(nullptr),
|
|
||||||
statusCode(HTTP_STATUS_OK),
|
statusCode(HTTP_STATUS_OK),
|
||||||
body(""),
|
body(""),
|
||||||
statusAdjective("OK"), // FIXME
|
statusAdjective("OK"), // FIXME
|
||||||
m_writtenOrEnded(false)
|
m_writtenOrEnded(false),
|
||||||
|
m_id(id)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void xmrig::HttpResponse::writeOrEnd(const std::string &str, bool end)
|
void xmrig::HttpResponse::writeOrEnd(const std::string &str, bool end)
|
||||||
{
|
{
|
||||||
|
HttpContext *context = HttpContext::get(m_id);
|
||||||
|
if (!context) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
if (!m_writtenOrEnded) {
|
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());
|
uv_buf_t resbuf = uv_buf_init(const_cast<char *>(out.c_str()), out.size());
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
HttpContext* context = static_cast<HttpContext*>(parser->data);
|
|
||||||
|
|
||||||
uv_try_write(context->stream(), &resbuf, 1);
|
uv_try_write(context->stream(), &resbuf, 1);
|
||||||
|
|
||||||
if (end) {
|
if (end) {
|
||||||
|
|
|
@ -33,16 +33,13 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
typedef struct http_parser http_parser;
|
|
||||||
|
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
class HttpResponse
|
class HttpResponse
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HttpResponse();
|
HttpResponse(uint64_t id);
|
||||||
|
|
||||||
inline void end() { writeOrEnd("", true); }
|
inline void end() { writeOrEnd("", true); }
|
||||||
inline void end(const std::string &str) { writeOrEnd(str, 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 setStatus(int code) { statusCode = code; }
|
||||||
inline void write(const std::string &str) { writeOrEnd(str, false); }
|
inline void write(const std::string &str) { writeOrEnd(str, false); }
|
||||||
|
|
||||||
http_parser *parser;
|
|
||||||
int statusCode;
|
int statusCode;
|
||||||
std::map<const std::string, const std::string> headers;
|
std::map<const std::string, const std::string> headers;
|
||||||
std::string body;
|
std::string body;
|
||||||
|
@ -60,6 +56,7 @@ private:
|
||||||
void writeOrEnd(const std::string &str, bool end);
|
void writeOrEnd(const std::string &str, bool end);
|
||||||
|
|
||||||
bool m_writtenOrEnded = false;
|
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)
|
int xmrig::HttpServer::onComplete(http_parser *parser)
|
||||||
{
|
{
|
||||||
HttpContext *context = reinterpret_cast<HttpContext*>(parser->data);
|
HttpContext *context = reinterpret_cast<HttpContext*>(parser->data);
|
||||||
|
HttpResponse res(context->id());
|
||||||
|
|
||||||
HttpResponse res;
|
context->listener->onHttpRequest(*context);
|
||||||
res.parser = parser;
|
|
||||||
|
|
||||||
context->listener->onHttp(*context, res);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue