HTTP subsystem refactoring.

This commit is contained in:
XMRig 2020-03-06 12:38:09 +07:00
parent 5486300db7
commit cad5fef1ea
No known key found for this signature in database
GPG key ID: 446A53638BE94409
25 changed files with 474 additions and 216 deletions

View file

@ -29,17 +29,11 @@
#include "base/io/json/Json.h"
#include "base/io/json/JsonRequest.h"
#include "base/io/log/Log.h"
#include "base/net/http/HttpClient.h"
#include "base/net/http/Fetch.h"
#include "base/net/http/HttpData.h"
#include "base/net/stratum/Client.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#ifdef XMRIG_FEATURE_TLS
# include "base/net/http/HttpsClient.h"
#endif
namespace xmrig {
@ -159,7 +153,8 @@ void xmrig::SelfSelectClient::getBlockTemplate()
JsonRequest::create(doc, m_sequence++, "getblocktemplate", params);
send(HTTP_POST, "/json_rpc", doc);
FetchRequest req(HTTP_POST, pool().daemon().host(), pool().daemon().port(), "/json_rpc", doc, pool().daemon().isTLS(), isQuiet());
fetch(std::move(req), m_httpListener);
}
@ -169,44 +164,6 @@ void xmrig::SelfSelectClient::retry()
}
void xmrig::SelfSelectClient::send(int method, const char *url, const char *data, size_t size)
{
LOG_DEBUG("[%s] " MAGENTA_BOLD("\"%s %s\"") BLACK_BOLD_S " send (%zu bytes): \"%.*s\"",
pool().daemon().url().data(),
http_method_str(static_cast<http_method>(method)),
url,
size,
static_cast<int>(size),
data);
HttpClient *client;
# ifdef XMRIG_FEATURE_TLS
if (pool().daemon().isTLS()) {
client = new HttpsClient(method, url, m_httpListener, data, size, String());
}
else
# endif
{
client = new HttpClient(method, url, m_httpListener, data, size);
}
client->setQuiet(isQuiet());
client->connect(pool().daemon().host(), pool().daemon().port());
}
void xmrig::SelfSelectClient::send(int method, const char *url, const rapidjson::Document &doc)
{
using namespace rapidjson;
StringBuffer buffer(nullptr, 512);
Writer<StringBuffer> writer(buffer);
doc.Accept(writer);
send(method, url, buffer.GetString(), buffer.GetSize());
}
void xmrig::SelfSelectClient::setState(State state)
{
if (m_state == state) {
@ -256,7 +213,7 @@ void xmrig::SelfSelectClient::submitBlockTemplate(rapidjson::Value &result)
JsonRequest::create(doc, sequence(), "block_template", params);
send(doc, [this](const rapidjson::Value &result, bool success, uint64_t elapsed) {
send(doc, [this](const rapidjson::Value &result, bool success, uint64_t) {
if (!success) {
if (!isQuiet()) {
LOG_ERR("[%s] error: " RED_BOLD("\"%s\"") RED_S ", code: %d", pool().daemon().url().data(), Json::getString(result, "message"), Json::getInt(result, "code"));
@ -285,8 +242,6 @@ void xmrig::SelfSelectClient::onHttpData(const HttpData &data)
return retry();
}
LOG_DEBUG("[%s] received (%d bytes): \"%.*s\"", pool().daemon().url().data(), static_cast<int>(data.body.size()), static_cast<int>(data.body.size()), data.body.c_str());
rapidjson::Document doc;
if (doc.Parse(data.body.c_str()).HasParseError()) {
if (!isQuiet()) {