Use static http_parser_settings.

This commit is contained in:
XMRig 2019-03-30 15:27:09 +07:00
parent c9f9e6787c
commit 106e149324
3 changed files with 39 additions and 44 deletions

View file

@ -29,6 +29,7 @@
#include "3rdparty/http-parser/http_parser.h" #include "3rdparty/http-parser/http_parser.h"
#include "base/kernel/interfaces/IHttpListener.h"
#include "base/net/http/HttpContext.h" #include "base/net/http/HttpContext.h"
@ -78,6 +79,10 @@ xmrig::HttpContext *xmrig::HttpContext::get(uint64_t id)
void xmrig::HttpContext::attach(http_parser_settings *settings) void xmrig::HttpContext::attach(http_parser_settings *settings)
{ {
if (settings->on_message_complete != nullptr) {
return;
}
settings->on_message_begin = nullptr; settings->on_message_begin = nullptr;
settings->on_status = nullptr; settings->on_status = nullptr;
settings->on_chunk_header = nullptr; settings->on_chunk_header = nullptr;
@ -109,6 +114,14 @@ void xmrig::HttpContext::attach(http_parser_settings *settings)
return 0; return 0;
}; };
settings->on_message_complete = [](http_parser *parser) -> int
{
const HttpContext *ctx = reinterpret_cast<const HttpContext*>(parser->data);
ctx->listener->onHttpRequest(*ctx);
return 0;
};
} }

View file

@ -35,48 +35,32 @@
#include "base/net/http/HttpServer.h" #include "base/net/http/HttpServer.h"
namespace xmrig {
static http_parser_settings http_settings;
} // namespace xmrig
xmrig::HttpServer::HttpServer(IHttpListener *listener) : xmrig::HttpServer::HttpServer(IHttpListener *listener) :
m_listener(listener) m_listener(listener)
{ {
m_settings = new http_parser_settings(); HttpContext::attach(&http_settings);
HttpContext::attach(m_settings);
m_settings->on_message_complete = HttpServer::onComplete;
} }
xmrig::HttpServer::~HttpServer() xmrig::HttpServer::~HttpServer()
{ {
delete m_settings; memset(&http_settings, 0, sizeof (http_settings));
} }
void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t) void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t)
{ {
static std::function<void(uv_stream_t *socket, int status)> onConnect; HttpContext *ctx = new HttpContext(HTTP_REQUEST, m_listener);
static std::function<void(uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)> onRead; uv_accept(stream, ctx->stream());
HttpContext *context = new HttpContext(HTTP_REQUEST, m_listener); uv_read_start(ctx->stream(),
uv_accept(stream, context->stream());
onRead = [&](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf) {
HttpContext* context = static_cast<HttpContext*>(tcp->data);
if (nread >= 0) {
const size_t size = static_cast<size_t>(nread);
const size_t parsed = http_parser_execute(context->parser, m_settings, buf->base, size);
if (parsed < size) {
uv_close(context->handle(), HttpContext::close);
}
} else {
uv_close(context->handle(), HttpContext::close);
}
delete [] buf->base;
};
uv_read_start(context->stream(),
[](uv_handle_t *, size_t suggested_size, uv_buf_t *buf) [](uv_handle_t *, size_t suggested_size, uv_buf_t *buf)
{ {
buf->base = new char[suggested_size]; buf->base = new char[suggested_size];
@ -89,18 +73,19 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t)
}, },
[](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf) [](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
{ {
onRead(tcp, nread, buf); HttpContext *ctx = static_cast<HttpContext*>(tcp->data);
if (nread >= 0) {
const size_t size = static_cast<size_t>(nread);
const size_t parsed = http_parser_execute(ctx->parser, &http_settings, buf->base, size);
if (parsed < size) {
uv_close(ctx->handle(), HttpContext::close);
}
} else {
uv_close(ctx->handle(), HttpContext::close);
}
delete [] buf->base;
}); });
} }
int xmrig::HttpServer::onComplete(http_parser *parser)
{
HttpContext *context = reinterpret_cast<HttpContext*>(parser->data);
HttpResponse res(context->id());
context->listener->onHttpRequest(*context);
return 0;
}

View file

@ -51,9 +51,6 @@ protected:
void onConnection(uv_stream_t *stream, uint16_t port) override; void onConnection(uv_stream_t *stream, uint16_t port) override;
private: private:
static int onComplete(http_parser *parser);
http_parser_settings *m_settings;
IHttpListener *m_listener; IHttpListener *m_listener;
}; };