Added HttpClient class.
This commit is contained in:
parent
35d868fb48
commit
241383068c
17 changed files with 421 additions and 153 deletions
|
@ -28,7 +28,7 @@
|
||||||
#include "api/Httpd.h"
|
#include "api/Httpd.h"
|
||||||
#include "base/io/log/Log.h"
|
#include "base/io/log/Log.h"
|
||||||
#include "base/net/http/HttpApiResponse.h"
|
#include "base/net/http/HttpApiResponse.h"
|
||||||
#include "base/net/http/HttpRequest.h"
|
#include "base/net/http/HttpData.h"
|
||||||
#include "base/net/http/HttpServer.h"
|
#include "base/net/http/HttpServer.h"
|
||||||
#include "base/net/tools/TcpServer.h"
|
#include "base/net/tools/TcpServer.h"
|
||||||
#include "core/config/Config.h"
|
#include "core/config/Config.h"
|
||||||
|
@ -128,49 +128,49 @@ void xmrig::Httpd::onConfigChanged(Config *config, Config *previousConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void xmrig::Httpd::onHttpRequest(const HttpRequest &req)
|
void xmrig::Httpd::onHttpData(const HttpData &data)
|
||||||
{
|
{
|
||||||
if (req.method == HTTP_OPTIONS) {
|
if (data.method == HTTP_OPTIONS) {
|
||||||
return HttpApiResponse(req.id()).end();
|
return HttpApiResponse(data.id()).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method == HTTP_GET && req.url == "/favicon.ico") {
|
if (data.method == HTTP_GET && data.url == "/favicon.ico") {
|
||||||
# ifdef _WIN32
|
# ifdef _WIN32
|
||||||
if (favicon != nullptr) {
|
if (favicon != nullptr) {
|
||||||
HttpResponse response(req.id());
|
HttpResponse response(data.id());
|
||||||
response.setHeader("Content-Type", "image/x-icon");
|
response.setHeader("Content-Type", "image/x-icon");
|
||||||
|
|
||||||
return response.end(favicon, faviconSize);
|
return response.end(favicon, faviconSize);
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
return HttpResponse(req.id(), 404).end();
|
return HttpResponse(data.id(), 404).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method > 4) {
|
if (data.method > 4) {
|
||||||
return HttpApiResponse(req.id(), HTTP_STATUS_METHOD_NOT_ALLOWED).end();
|
return HttpApiResponse(data.id(), HTTP_STATUS_METHOD_NOT_ALLOWED).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
const int status = auth(req);
|
const int status = auth(data);
|
||||||
if (status != HTTP_STATUS_OK) {
|
if (status != HTTP_STATUS_OK) {
|
||||||
return HttpApiResponse(req.id(), status).end();
|
return HttpApiResponse(data.id(), status).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method != HTTP_GET) {
|
if (data.method != HTTP_GET) {
|
||||||
if (m_base->config()->http().isRestricted()) {
|
if (m_base->config()->http().isRestricted()) {
|
||||||
return HttpApiResponse(req.id(), HTTP_STATUS_FORBIDDEN).end();
|
return HttpApiResponse(data.id(), HTTP_STATUS_FORBIDDEN).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!req.headers.count(kContentType) || req.headers.at(kContentType) != "application/json") {
|
if (!data.headers.count(kContentType) || data.headers.at(kContentType) != "application/json") {
|
||||||
return HttpApiResponse(req.id(), HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE).end();
|
return HttpApiResponse(data.id(), HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE).end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_base->api()->request(req);
|
m_base->api()->request(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int xmrig::Httpd::auth(const HttpRequest &req) const
|
int xmrig::Httpd::auth(const HttpData &req) const
|
||||||
{
|
{
|
||||||
const Http &config = m_base->config()->http();
|
const Http &config = m_base->config()->http();
|
||||||
|
|
||||||
|
|
|
@ -52,10 +52,10 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void onConfigChanged(Config *config, Config *previousConfig) override;
|
void onConfigChanged(Config *config, Config *previousConfig) override;
|
||||||
void onHttpRequest(const HttpRequest &req) override;
|
void onHttpData(const HttpData &data) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int auth(const HttpRequest &req) const;
|
int auth(const HttpData &req) const;
|
||||||
|
|
||||||
Base *m_base;
|
Base *m_base;
|
||||||
HttpServer *m_http;
|
HttpServer *m_http;
|
||||||
|
|
|
@ -24,11 +24,11 @@
|
||||||
|
|
||||||
|
|
||||||
#include "api/requests/HttpApiRequest.h"
|
#include "api/requests/HttpApiRequest.h"
|
||||||
#include "base/net/http/HttpRequest.h"
|
#include "base/net/http/HttpData.h"
|
||||||
#include "rapidjson/error/en.h"
|
#include "rapidjson/error/en.h"
|
||||||
|
|
||||||
|
|
||||||
xmrig::HttpApiRequest::HttpApiRequest(const HttpRequest &req, bool restricted) :
|
xmrig::HttpApiRequest::HttpApiRequest(const HttpData &req, bool restricted) :
|
||||||
ApiRequest(SOURCE_HTTP, restricted),
|
ApiRequest(SOURCE_HTTP, restricted),
|
||||||
m_parsed(false),
|
m_parsed(false),
|
||||||
m_req(req),
|
m_req(req),
|
||||||
|
|
|
@ -35,13 +35,13 @@
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
class HttpRequest;
|
class HttpData;
|
||||||
|
|
||||||
|
|
||||||
class HttpApiRequest : public ApiRequest
|
class HttpApiRequest : public ApiRequest
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HttpApiRequest(const HttpRequest &req, bool restricted);
|
HttpApiRequest(const HttpData &req, bool restricted);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline rapidjson::Document &doc() override { return m_res.doc(); }
|
inline rapidjson::Document &doc() override { return m_res.doc(); }
|
||||||
|
@ -55,7 +55,7 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_parsed;
|
bool m_parsed;
|
||||||
const HttpRequest &m_req;
|
const HttpData &m_req;
|
||||||
HttpApiResponse m_res;
|
HttpApiResponse m_res;
|
||||||
rapidjson::Document m_body;
|
rapidjson::Document m_body;
|
||||||
String m_url;
|
String m_url;
|
||||||
|
|
|
@ -100,8 +100,9 @@ if (WITH_HTTPD)
|
||||||
src/base/kernel/interfaces/IJsonReader.h
|
src/base/kernel/interfaces/IJsonReader.h
|
||||||
src/base/kernel/interfaces/ITcpServerListener.h
|
src/base/kernel/interfaces/ITcpServerListener.h
|
||||||
src/base/net/http/HttpApiResponse.h
|
src/base/net/http/HttpApiResponse.h
|
||||||
|
src/base/net/http/HttpClient.h
|
||||||
src/base/net/http/HttpContext.h
|
src/base/net/http/HttpContext.h
|
||||||
src/base/net/http/HttpRequest.h
|
src/base/net/http/HttpData.h
|
||||||
src/base/net/http/HttpResponse.h
|
src/base/net/http/HttpResponse.h
|
||||||
src/base/net/http/HttpServer.h
|
src/base/net/http/HttpServer.h
|
||||||
src/base/net/tools/TcpServer.h
|
src/base/net/tools/TcpServer.h
|
||||||
|
@ -110,6 +111,7 @@ if (WITH_HTTPD)
|
||||||
set(SOURCES_BASE_HTTP
|
set(SOURCES_BASE_HTTP
|
||||||
src/3rdparty/http-parser/http_parser.c
|
src/3rdparty/http-parser/http_parser.c
|
||||||
src/base/net/http/HttpApiResponse.cpp
|
src/base/net/http/HttpApiResponse.cpp
|
||||||
|
src/base/net/http/HttpClient.cpp
|
||||||
src/base/net/http/HttpContext.cpp
|
src/base/net/http/HttpContext.cpp
|
||||||
src/base/net/http/HttpResponse.cpp
|
src/base/net/http/HttpResponse.cpp
|
||||||
src/base/net/http/HttpServer.cpp
|
src/base/net/http/HttpServer.cpp
|
||||||
|
|
|
@ -29,7 +29,7 @@
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
class HttpRequest;
|
class HttpData;
|
||||||
class HttpResponse;
|
class HttpResponse;
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ class IHttpListener
|
||||||
public:
|
public:
|
||||||
virtual ~IHttpListener() = default;
|
virtual ~IHttpListener() = default;
|
||||||
|
|
||||||
virtual void onHttpRequest(const HttpRequest &req) = 0;
|
virtual void onHttpData(const HttpData &data) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -144,7 +144,11 @@ void xmrig::Dns::onResolved(int status, addrinfo *res)
|
||||||
ptr = ptr->ai_next;
|
ptr = ptr->ai_next;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_listener->onResolved(*this, status);
|
if (isEmpty()) {
|
||||||
|
m_status = UV_EAI_NONAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_listener->onResolved(*this, m_status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,7 @@ public:
|
||||||
~Dns();
|
~Dns();
|
||||||
|
|
||||||
inline bool isEmpty() const { return m_ipv4.empty() && m_ipv6.empty(); }
|
inline bool isEmpty() const { return m_ipv4.empty() && m_ipv6.empty(); }
|
||||||
|
inline const String &host() const { return m_host; }
|
||||||
inline int status() const { return m_status; }
|
inline int status() const { return m_status; }
|
||||||
|
|
||||||
bool resolve(const String &host);
|
bool resolve(const String &host);
|
||||||
|
|
198
src/base/net/http/HttpClient.cpp
Normal file
198
src/base/net/http/HttpClient.cpp
Normal file
|
@ -0,0 +1,198 @@
|
||||||
|
/* XMRig
|
||||||
|
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||||
|
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||||
|
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||||
|
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||||
|
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||||
|
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||||
|
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
|
||||||
|
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||||
|
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
#include "3rdparty/http-parser/http_parser.h"
|
||||||
|
#include "base/io/log/Log.h"
|
||||||
|
#include "base/net/dns/Dns.h"
|
||||||
|
#include "base/net/http/HttpClient.h"
|
||||||
|
#include "base/tools/Baton.h"
|
||||||
|
#include "common/Platform.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace xmrig {
|
||||||
|
|
||||||
|
static const char *kCRLF = "\r\n";
|
||||||
|
|
||||||
|
|
||||||
|
class WriteBaton : public Baton<uv_write_t>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline WriteBaton(const std::stringstream &ss, std::string &&body) :
|
||||||
|
m_body(body),
|
||||||
|
m_header(ss.str())
|
||||||
|
{
|
||||||
|
bufs[0].len = m_header.size();
|
||||||
|
bufs[0].base = const_cast<char *>(m_header.c_str());
|
||||||
|
|
||||||
|
if (!m_body.empty()) {
|
||||||
|
bufs[1].len = m_body.size();
|
||||||
|
bufs[1].base = const_cast<char *>(m_body.c_str());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
bufs[1].base = nullptr;
|
||||||
|
bufs[1].len = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline size_t count() const { return bufs[1].base == nullptr ? 1 : 2; }
|
||||||
|
inline size_t size() const { return bufs[0].len + bufs[1].len; }
|
||||||
|
inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast<WriteBaton *>(req->data); }
|
||||||
|
|
||||||
|
|
||||||
|
uv_buf_t bufs[2];
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_body;
|
||||||
|
std::string m_header;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
|
xmrig::HttpClient::HttpClient(const String &host, uint16_t port, int method, const String &url, IHttpListener *listener, const char *data, size_t size) :
|
||||||
|
HttpContext(HTTP_RESPONSE, listener),
|
||||||
|
m_port(port)
|
||||||
|
{
|
||||||
|
this->method = method;
|
||||||
|
this->url = url;
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
body = size ? std::string(data, size) : data;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_dns = new Dns(this);
|
||||||
|
|
||||||
|
status = m_dns->resolve(host);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
xmrig::HttpClient::~HttpClient()
|
||||||
|
{
|
||||||
|
delete m_dns;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void xmrig::HttpClient::onResolved(const Dns &dns, int status)
|
||||||
|
{
|
||||||
|
this->status = status;
|
||||||
|
|
||||||
|
if (status < 0 && dns.isEmpty()) {
|
||||||
|
LOG_ERR("[%s:%d] DNS error: \"%s\"", dns.host().data(), m_port, uv_strerror(status));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sockaddr *addr = dns.get().addr(m_port);
|
||||||
|
|
||||||
|
uv_connect_t *req = new uv_connect_t;
|
||||||
|
req->data = this;
|
||||||
|
|
||||||
|
uv_tcp_connect(req, m_tcp, addr, onConnect);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void xmrig::HttpClient::send()
|
||||||
|
{
|
||||||
|
headers.insert({ "Host", m_dns->host().data() });
|
||||||
|
headers.insert({ "Connection", "close" });
|
||||||
|
headers.insert({ "User-Agent", Platform::userAgent() });
|
||||||
|
|
||||||
|
if (body.size()) {
|
||||||
|
headers.insert({ "Content-Length", std::to_string(body.size()) });
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << http_method_str(static_cast<http_method>(method)) << " " << url << " HTTP/1.1" << kCRLF;
|
||||||
|
|
||||||
|
for (auto &header : headers) {
|
||||||
|
ss << header.first << ": " << header.second << kCRLF;
|
||||||
|
}
|
||||||
|
|
||||||
|
ss << kCRLF;
|
||||||
|
|
||||||
|
headers.clear();
|
||||||
|
|
||||||
|
WriteBaton *baton = new WriteBaton(ss, std::move(body));
|
||||||
|
uv_write(&baton->req, stream(), baton->bufs, baton->count(), WriteBaton::onWrite);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
|
||||||
|
{
|
||||||
|
HttpClient *client = static_cast<HttpClient *>(req->data);
|
||||||
|
if (!client) {
|
||||||
|
delete req;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status < 0) {
|
||||||
|
LOG_ERR("[%s:%d] connect error: \"%s\"", client->m_dns->host().data(), client->m_port, uv_strerror(status));
|
||||||
|
|
||||||
|
delete req;
|
||||||
|
client->close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uv_read_start(client->stream(),
|
||||||
|
[](uv_handle_t *, size_t suggested_size, uv_buf_t *buf)
|
||||||
|
{
|
||||||
|
buf->base = new char[suggested_size];
|
||||||
|
|
||||||
|
# ifdef _WIN32
|
||||||
|
buf->len = static_cast<unsigned int>(suggested_size);
|
||||||
|
# else
|
||||||
|
buf->len = suggested_size;
|
||||||
|
# endif
|
||||||
|
},
|
||||||
|
[](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
|
||||||
|
{
|
||||||
|
HttpClient *client = static_cast<HttpClient*>(tcp->data);
|
||||||
|
|
||||||
|
if (nread >= 0) {
|
||||||
|
const size_t size = static_cast<size_t>(nread);
|
||||||
|
const size_t parsed = client->parse(buf->base, size);
|
||||||
|
|
||||||
|
if (parsed < size) {
|
||||||
|
client->close();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (nread != UV_EOF) {
|
||||||
|
LOG_ERR("[%s:%d] read error: \"%s\"", client->m_dns->host().data(), client->m_port, uv_strerror(static_cast<int>(nread)));
|
||||||
|
}
|
||||||
|
|
||||||
|
client->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
delete [] buf->base;
|
||||||
|
});
|
||||||
|
|
||||||
|
client->send();
|
||||||
|
}
|
64
src/base/net/http/HttpClient.h
Normal file
64
src/base/net/http/HttpClient.h
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
/* XMRig
|
||||||
|
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
|
||||||
|
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
|
||||||
|
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
|
||||||
|
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
|
||||||
|
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
|
||||||
|
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
|
||||||
|
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
|
||||||
|
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
|
||||||
|
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef XMRIG_HTTPCLIENT_H
|
||||||
|
#define XMRIG_HTTPCLIENT_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "base/net/http/HttpContext.h"
|
||||||
|
#include "base/kernel/interfaces/IDnsListener.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
|
class String;
|
||||||
|
|
||||||
|
|
||||||
|
class HttpClient : public HttpContext, public IDnsListener
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
HttpClient(const String &host, uint16_t port, int method, const String &url, IHttpListener *listener, const char *data = nullptr, size_t size = 0);
|
||||||
|
~HttpClient();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void onResolved(const Dns &dns, int status);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void send();
|
||||||
|
|
||||||
|
static void onConnect(uv_connect_t *req, int status);
|
||||||
|
|
||||||
|
Dns *m_dns;
|
||||||
|
uint16_t m_port;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
|
#endif // XMRIG_HTTPCLIENT_H
|
||||||
|
|
|
@ -35,43 +35,72 @@
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
|
static http_parser_settings http_settings;
|
||||||
|
static std::map<uint64_t, HttpContext *> storage;
|
||||||
static uint64_t SEQUENCE = 0;
|
static uint64_t SEQUENCE = 0;
|
||||||
std::map<uint64_t, HttpContext *> HttpContext::m_storage;
|
|
||||||
|
|
||||||
} // namespace xmrig
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
xmrig::HttpContext::HttpContext(int parser_type, IHttpListener *listener) :
|
xmrig::HttpContext::HttpContext(int parser_type, IHttpListener *listener) :
|
||||||
HttpRequest(SEQUENCE++),
|
HttpData(SEQUENCE++),
|
||||||
listener(listener),
|
m_wasHeaderValue(false),
|
||||||
connect(nullptr),
|
m_listener(listener)
|
||||||
m_wasHeaderValue(false)
|
|
||||||
{
|
{
|
||||||
m_storage[id()] = this;
|
storage[id()] = this;
|
||||||
|
|
||||||
parser = new http_parser;
|
m_parser = new http_parser;
|
||||||
tcp = new uv_tcp_t;
|
m_tcp = new uv_tcp_t;
|
||||||
|
|
||||||
uv_tcp_init(uv_default_loop(), tcp);
|
uv_tcp_init(uv_default_loop(), m_tcp);
|
||||||
http_parser_init(parser, static_cast<http_parser_type>(parser_type));
|
uv_tcp_nodelay(m_tcp, 1);
|
||||||
|
|
||||||
parser->data = tcp->data = this;
|
http_parser_init(m_parser, static_cast<http_parser_type>(parser_type));
|
||||||
|
|
||||||
|
m_parser->data = m_tcp->data = this;
|
||||||
|
|
||||||
|
if (http_settings.on_message_complete == nullptr) {
|
||||||
|
attach(&http_settings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
xmrig::HttpContext::~HttpContext()
|
xmrig::HttpContext::~HttpContext()
|
||||||
{
|
{
|
||||||
delete connect;
|
delete m_tcp;
|
||||||
delete tcp;
|
delete m_parser;
|
||||||
delete parser;
|
}
|
||||||
|
|
||||||
|
|
||||||
|
size_t xmrig::HttpContext::parse(const char *data, size_t size)
|
||||||
|
{
|
||||||
|
return http_parser_execute(m_parser, &http_settings, data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string xmrig::HttpContext::ip() const
|
||||||
|
{
|
||||||
|
char ip[46] = {};
|
||||||
|
sockaddr_storage addr = {};
|
||||||
|
int size = sizeof(addr);
|
||||||
|
|
||||||
|
uv_tcp_getpeername(m_tcp, reinterpret_cast<sockaddr*>(&addr), &size);
|
||||||
|
if (reinterpret_cast<sockaddr_in *>(&addr)->sin_family == AF_INET6) {
|
||||||
|
uv_ip6_name(reinterpret_cast<sockaddr_in6*>(&addr), ip, 45);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
uv_ip4_name(reinterpret_cast<sockaddr_in*>(&addr), ip, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void xmrig::HttpContext::close()
|
void xmrig::HttpContext::close()
|
||||||
{
|
{
|
||||||
auto it = m_storage.find(id());
|
auto it = storage.find(id());
|
||||||
if (it != m_storage.end()) {
|
if (it != storage.end()) {
|
||||||
m_storage.erase(it);
|
storage.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!uv_is_closing(handle())) {
|
if (!uv_is_closing(handle())) {
|
||||||
|
@ -82,65 +111,17 @@ void xmrig::HttpContext::close()
|
||||||
|
|
||||||
xmrig::HttpContext *xmrig::HttpContext::get(uint64_t id)
|
xmrig::HttpContext *xmrig::HttpContext::get(uint64_t id)
|
||||||
{
|
{
|
||||||
if (m_storage.count(id) == 0) {
|
if (storage.count(id) == 0) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return m_storage[id];
|
return storage[id];
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void xmrig::HttpContext::attach(http_parser_settings *settings)
|
|
||||||
{
|
|
||||||
if (settings->on_message_complete != nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
settings->on_message_begin = nullptr;
|
|
||||||
settings->on_status = nullptr;
|
|
||||||
settings->on_chunk_header = nullptr;
|
|
||||||
settings->on_chunk_complete = nullptr;
|
|
||||||
|
|
||||||
settings->on_url = [](http_parser *parser, const char *at, size_t length) -> int
|
|
||||||
{
|
|
||||||
static_cast<HttpContext*>(parser->data)->url = std::string(at, length);
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
settings->on_header_field = onHeaderField;
|
|
||||||
settings->on_header_value = onHeaderValue;
|
|
||||||
|
|
||||||
settings->on_headers_complete = [](http_parser* parser) -> int {
|
|
||||||
HttpContext *ctx = static_cast<HttpContext*>(parser->data);
|
|
||||||
ctx->method = parser->method;
|
|
||||||
|
|
||||||
if (!ctx->m_lastHeaderField.empty()) {
|
|
||||||
ctx->setHeader();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
settings->on_body = [](http_parser *parser, const char *at, size_t len) -> int
|
|
||||||
{
|
|
||||||
static_cast<HttpContext*>(parser->data)->body += std::string(at, len);
|
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void xmrig::HttpContext::closeAll()
|
void xmrig::HttpContext::closeAll()
|
||||||
{
|
{
|
||||||
for (auto kv : m_storage) {
|
for (auto kv : storage) {
|
||||||
if (!uv_is_closing(kv.second->handle())) {
|
if (!uv_is_closing(kv.second->handle())) {
|
||||||
uv_close(kv.second->handle(), [](uv_handle_t *handle) -> void { delete reinterpret_cast<HttpContext*>(handle->data); });
|
uv_close(kv.second->handle(), [](uv_handle_t *handle) -> void { delete reinterpret_cast<HttpContext*>(handle->data); });
|
||||||
}
|
}
|
||||||
|
@ -182,6 +163,54 @@ int xmrig::HttpContext::onHeaderValue(http_parser *parser, const char *at, size_
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void xmrig::HttpContext::attach(http_parser_settings *settings)
|
||||||
|
{
|
||||||
|
settings->on_message_begin = nullptr;
|
||||||
|
settings->on_status = nullptr;
|
||||||
|
settings->on_chunk_header = nullptr;
|
||||||
|
settings->on_chunk_complete = nullptr;
|
||||||
|
|
||||||
|
settings->on_url = [](http_parser *parser, const char *at, size_t length) -> int
|
||||||
|
{
|
||||||
|
static_cast<HttpContext*>(parser->data)->url = std::string(at, length);
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
settings->on_header_field = onHeaderField;
|
||||||
|
settings->on_header_value = onHeaderValue;
|
||||||
|
|
||||||
|
settings->on_headers_complete = [](http_parser* parser) -> int {
|
||||||
|
HttpContext *ctx = static_cast<HttpContext*>(parser->data);
|
||||||
|
ctx->status = parser->status_code;
|
||||||
|
|
||||||
|
if (parser->type == HTTP_REQUEST) {
|
||||||
|
ctx->method = parser->method;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ctx->m_lastHeaderField.empty()) {
|
||||||
|
ctx->setHeader();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
settings->on_body = [](http_parser *parser, const char *at, size_t len) -> int
|
||||||
|
{
|
||||||
|
static_cast<HttpContext*>(parser->data)->body += std::string(at, len);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
settings->on_message_complete = [](http_parser *parser) -> int
|
||||||
|
{
|
||||||
|
const HttpContext *ctx = static_cast<const HttpContext*>(parser->data);
|
||||||
|
ctx->m_listener->onHttpData(*ctx);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void xmrig::HttpContext::setHeader()
|
void xmrig::HttpContext::setHeader()
|
||||||
{
|
{
|
||||||
std::transform(m_lastHeaderField.begin(), m_lastHeaderField.end(), m_lastHeaderField.begin(), ::tolower);
|
std::transform(m_lastHeaderField.begin(), m_lastHeaderField.end(), m_lastHeaderField.begin(), ::tolower);
|
||||||
|
|
|
@ -36,7 +36,7 @@ typedef struct uv_stream_s uv_stream_t;
|
||||||
typedef struct uv_tcp_s uv_tcp_t;
|
typedef struct uv_tcp_s uv_tcp_t;
|
||||||
|
|
||||||
|
|
||||||
#include "base/net/http/HttpRequest.h"
|
#include "base/net/http/HttpData.h"
|
||||||
|
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
@ -45,37 +45,37 @@ namespace xmrig {
|
||||||
class IHttpListener;
|
class IHttpListener;
|
||||||
|
|
||||||
|
|
||||||
class HttpContext : public HttpRequest
|
class HttpContext : public HttpData
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
HttpContext(int parser_type, IHttpListener *listener);
|
HttpContext(int parser_type, IHttpListener *listener);
|
||||||
~HttpContext();
|
virtual ~HttpContext();
|
||||||
|
|
||||||
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 *>(m_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 *>(m_tcp); }
|
||||||
|
|
||||||
|
size_t parse(const char *data, size_t size);
|
||||||
|
std::string ip() const;
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
static HttpContext *get(uint64_t id);
|
static HttpContext *get(uint64_t id);
|
||||||
static void attach(http_parser_settings *settings);
|
|
||||||
static void closeAll();
|
static void closeAll();
|
||||||
|
|
||||||
http_parser *parser;
|
protected:
|
||||||
IHttpListener *listener;
|
uv_tcp_t *m_tcp;
|
||||||
uv_connect_t *connect;
|
|
||||||
uv_tcp_t *tcp;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static int onHeaderField(http_parser *parser, const char *at, size_t length);
|
static int onHeaderField(http_parser *parser, const char *at, size_t length);
|
||||||
static int onHeaderValue(http_parser *parser, const char *at, size_t length);
|
static int onHeaderValue(http_parser *parser, const char *at, size_t length);
|
||||||
|
static void attach(http_parser_settings *settings);
|
||||||
|
|
||||||
void setHeader();
|
void setHeader();
|
||||||
|
|
||||||
bool m_wasHeaderValue;
|
bool m_wasHeaderValue;
|
||||||
|
http_parser *m_parser;
|
||||||
|
IHttpListener *m_listener;
|
||||||
std::string m_lastHeaderField;
|
std::string m_lastHeaderField;
|
||||||
std::string m_lastHeaderValue;
|
std::string m_lastHeaderValue;
|
||||||
|
|
||||||
static std::map<uint64_t, HttpContext *> m_storage;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,26 +24,26 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef XMRIG_HTTPREQUEST_H
|
#ifndef XMRIG_HTTPDATA_H
|
||||||
#define XMRIG_HTTPREQUEST_H
|
#define XMRIG_HTTPDATA_H
|
||||||
|
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
class HttpRequest
|
class HttpData
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
inline HttpRequest(uint64_t id) : method(0), m_id(id) {}
|
inline HttpData(uint64_t id) : method(0), status(0), m_id(id) {}
|
||||||
|
|
||||||
inline uint64_t id() const { return m_id; }
|
inline uint64_t id() const { return m_id; }
|
||||||
|
|
||||||
int method;
|
int method;
|
||||||
|
int status;
|
||||||
std::map<const std::string, const std::string> headers;
|
std::map<const std::string, const std::string> headers;
|
||||||
std::string body;
|
std::string body;
|
||||||
std::string url;
|
std::string url;
|
||||||
|
@ -56,5 +56,5 @@ private:
|
||||||
} // namespace xmrig
|
} // namespace xmrig
|
||||||
|
|
||||||
|
|
||||||
#endif // XMRIG_HTTPREQUEST_H
|
#endif // XMRIG_HTTPDATA_H
|
||||||
|
|
|
@ -49,7 +49,6 @@ public:
|
||||||
m_ctx(ctx),
|
m_ctx(ctx),
|
||||||
m_header(ss.str())
|
m_header(ss.str())
|
||||||
{
|
{
|
||||||
req.data = this;
|
|
||||||
bufs[0].len = m_header.size();
|
bufs[0].len = m_header.size();
|
||||||
bufs[0].base = const_cast<char *>(m_header.c_str());
|
bufs[0].base = const_cast<char *>(m_header.c_str());
|
||||||
|
|
||||||
|
@ -138,20 +137,9 @@ void xmrig::HttpResponse::end(const char *data, size_t size)
|
||||||
# endif
|
# endif
|
||||||
{
|
{
|
||||||
const bool err = statusCode() >= 400;
|
const bool err = statusCode() >= 400;
|
||||||
char ip[46] = {};
|
|
||||||
sockaddr_storage addr = {};
|
|
||||||
int aSize = sizeof(addr);
|
|
||||||
|
|
||||||
uv_tcp_getpeername(ctx->tcp, reinterpret_cast<sockaddr*>(&addr), &aSize);
|
|
||||||
if (reinterpret_cast<sockaddr_in *>(&addr)->sin_family == AF_INET6) {
|
|
||||||
uv_ip6_name(reinterpret_cast<sockaddr_in6*>(&addr), ip, 45);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
uv_ip4_name(reinterpret_cast<sockaddr_in*>(&addr), ip, 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log::print(err ? Log::ERR : Log::INFO, CYAN("%s ") CLEAR MAGENTA_BOLD("%s") WHITE_BOLD(" %s ") CSI "1;%dm%d " CLEAR WHITE_BOLD("%zu ") BLACK_BOLD("\"%s\""),
|
Log::print(err ? Log::ERR : Log::INFO, CYAN("%s ") CLEAR MAGENTA_BOLD("%s") WHITE_BOLD(" %s ") CSI "1;%dm%d " CLEAR WHITE_BOLD("%zu ") BLACK_BOLD("\"%s\""),
|
||||||
ip,
|
ctx->ip().c_str(),
|
||||||
http_method_str(static_cast<http_method>(ctx->method)),
|
http_method_str(static_cast<http_method>(ctx->method)),
|
||||||
ctx->url.c_str(),
|
ctx->url.c_str(),
|
||||||
err ? 31 : 32,
|
err ? 31 : 32,
|
||||||
|
|
|
@ -35,17 +35,9 @@
|
||||||
#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)
|
||||||
{
|
{
|
||||||
HttpContext::attach(&http_settings);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,7 +69,7 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t)
|
||||||
|
|
||||||
if (nread >= 0) {
|
if (nread >= 0) {
|
||||||
const size_t size = static_cast<size_t>(nread);
|
const size_t size = static_cast<size_t>(nread);
|
||||||
const size_t parsed = http_parser_execute(ctx->parser, &http_settings, buf->base, size);
|
const size_t parsed = ctx->parse(buf->base, size);
|
||||||
|
|
||||||
if (parsed < size) {
|
if (parsed < size) {
|
||||||
ctx->close();
|
ctx->close();
|
||||||
|
|
|
@ -284,14 +284,6 @@ void xmrig::Client::onResolved(const Dns &dns, int status)
|
||||||
return reconnect();
|
return reconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dns.isEmpty()) {
|
|
||||||
if (!isQuiet()) {
|
|
||||||
LOG_ERR("[%s] DNS error: \"No IPv4 (A) or IPv6 (AAAA) records found\"", url());
|
|
||||||
}
|
|
||||||
|
|
||||||
return reconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
const DnsRecord &record = dns.get();
|
const DnsRecord &record = dns.get();
|
||||||
m_ip = record.ip();
|
m_ip = record.ip();
|
||||||
|
|
||||||
|
@ -574,8 +566,6 @@ void xmrig::Client::connect(sockaddr *addr)
|
||||||
{
|
{
|
||||||
setState(ConnectingState);
|
setState(ConnectingState);
|
||||||
|
|
||||||
reinterpret_cast<sockaddr_in*>(addr)->sin_port = htons(m_pool.port());
|
|
||||||
|
|
||||||
uv_connect_t *req = new uv_connect_t;
|
uv_connect_t *req = new uv_connect_t;
|
||||||
req->data = m_storage.ptr(m_key);
|
req->data = m_storage.ptr(m_key);
|
||||||
|
|
||||||
|
@ -589,7 +579,7 @@ void xmrig::Client::connect(sockaddr *addr)
|
||||||
uv_tcp_keepalive(m_socket, 1, 60);
|
uv_tcp_keepalive(m_socket, 1, 60);
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
uv_tcp_connect(req, m_socket, reinterpret_cast<const sockaddr*>(addr), Client::onConnect);
|
uv_tcp_connect(req, m_socket, addr, onConnect);
|
||||||
|
|
||||||
delete addr;
|
delete addr;
|
||||||
}
|
}
|
||||||
|
@ -970,7 +960,7 @@ void xmrig::Client::onConnect(uv_connect_t *req, int status)
|
||||||
client->m_stream->data = req->data;
|
client->m_stream->data = req->data;
|
||||||
client->setState(ConnectedState);
|
client->setState(ConnectedState);
|
||||||
|
|
||||||
uv_read_start(client->m_stream, Client::onAllocBuffer, Client::onRead);
|
uv_read_start(client->m_stream, onAllocBuffer, onRead);
|
||||||
delete req;
|
delete req;
|
||||||
|
|
||||||
client->handshake();
|
client->handshake();
|
||||||
|
|
|
@ -29,13 +29,13 @@
|
||||||
namespace xmrig {
|
namespace xmrig {
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename REQ>
|
||||||
class Baton
|
class Baton
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
inline Baton() { req.data = this; }
|
inline Baton() { req.data = this; }
|
||||||
|
|
||||||
T req;
|
REQ req;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue