Stratum: fixed response to HTTP GET/HEAD requests
This commit is contained in:
+21
-12
@@ -1167,12 +1167,19 @@ bool StratumServer::StratumClient::on_read(char* data, uint32_t size)
|
|||||||
char* line_start = m_stratumReadBuf;
|
char* line_start = m_stratumReadBuf;
|
||||||
for (char *e = line_start + m_stratumReadBufBytes, *c = e - size; c < e; ++c) {
|
for (char *e = line_start + m_stratumReadBufBytes, *c = e - size; c < e; ++c) {
|
||||||
if (*c == '\n') {
|
if (*c == '\n') {
|
||||||
// Check if the line starts with "GET " (an HTTP request)
|
// Check if the line starts with "GET " or "HEAD" (an HTTP request)
|
||||||
if ((c - line_start >= 4) && (read_unaligned(reinterpret_cast<uint32_t*>(line_start)) == 0x20544547U)) {
|
if (c - line_start >= 4) {
|
||||||
LOGINFO(5, "client " << log::Gray() << static_cast<const char*>(m_addrString) << log::NoColor() << " sent an HTTP request");
|
const uint32_t line_start_data = read_unaligned(reinterpret_cast<uint32_t*>(line_start));
|
||||||
send_http_response();
|
|
||||||
close();
|
const bool is_http_get = (line_start_data == 0x20544547U);
|
||||||
return true;
|
const bool is_http_head = (line_start_data == 0x44414548U);
|
||||||
|
|
||||||
|
if (is_http_get || is_http_head) {
|
||||||
|
LOGINFO(5, "client " << log::Gray() << static_cast<const char*>(m_addrString) << log::NoColor() << " sent an HTTP " << (is_http_get ? "GET" : "HEAD") << " request");
|
||||||
|
send_http_response(is_http_get);
|
||||||
|
close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*c = '\0';
|
*c = '\0';
|
||||||
@@ -1374,22 +1381,24 @@ bool StratumServer::StratumClient::process_submit(rapidjson::Document& doc, uint
|
|||||||
return static_cast<StratumServer*>(m_owner)->on_submit(this, id, job_id.GetString(), nonce.GetString(), result.GetString());
|
return static_cast<StratumServer*>(m_owner)->on_submit(this, id, job_id.GetString(), nonce.GetString(), result.GetString());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StratumServer::StratumClient::send_http_response()
|
bool StratumServer::StratumClient::send_http_response(bool send_content)
|
||||||
{
|
{
|
||||||
return m_owner->send(this, [](uint8_t *buf, size_t buf_size) -> size_t {
|
return m_owner->send(this, [send_content](uint8_t *buf, size_t buf_size) -> size_t {
|
||||||
static constexpr uint8_t data[] =
|
static constexpr uint8_t data[] =
|
||||||
"HTTP/1.1 200 OK\r\n"
|
"HTTP/1.1 200 OK\r\n"
|
||||||
"Content-Length: 21\r\n"
|
"Content-Length: 21\r\n"
|
||||||
"Content-Type: text/html\r\n"
|
"Content-Type: text/plain\r\n"
|
||||||
"Connection: Closed\r\n\r\n"
|
"Connection: Closed\r\n\r\n"
|
||||||
"P2Pool Stratum online";
|
"P2Pool Stratum online";
|
||||||
|
|
||||||
if (buf_size < sizeof(data)) {
|
const size_t data_size = send_content ? (sizeof(data) - 1) : (sizeof(data) - 1 - 21);
|
||||||
|
|
||||||
|
if (buf_size < data_size) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(buf, data, sizeof(data));
|
memcpy(buf, data, data_size);
|
||||||
return sizeof(data);
|
return data_size;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public:
|
|||||||
[[nodiscard]] bool process_login(rapidjson::Document& doc, uint32_t id);
|
[[nodiscard]] bool process_login(rapidjson::Document& doc, uint32_t id);
|
||||||
[[nodiscard]] bool process_submit(rapidjson::Document& doc, uint32_t id);
|
[[nodiscard]] bool process_submit(rapidjson::Document& doc, uint32_t id);
|
||||||
|
|
||||||
bool send_http_response();
|
bool send_http_response(bool send_content);
|
||||||
|
|
||||||
alignas(8) char m_rawReadBuf[STRATUM_BUF_SIZE];
|
alignas(8) char m_rawReadBuf[STRATUM_BUF_SIZE];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user