Coverage Report

Created: 2026-09-06 13:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/httpserver.cpp
Line
Count
Source
1
// Copyright (c) 2015-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <bitcoin-build-config.h> // IWYU pragma: keep
6
7
#include <httpserver.h>
8
9
#include <chainparamsbase.h>
10
#include <common/args.h>
11
#include <common/messages.h>
12
#include <common/url.h>
13
#include <compat/compat.h>
14
#include <logging.h>
15
#include <netbase.h>
16
#include <node/interface_ui.h>
17
#include <rpc/protocol.h>
18
#include <span.h>
19
#include <sync.h>
20
#include <util/check.h>
21
#include <util/signalinterrupt.h>
22
#include <util/sock.h>
23
#include <util/strencodings.h>
24
#include <util/thread.h>
25
#include <util/threadnames.h>
26
#include <util/threadpool.h>
27
#include <util/time.h>
28
#include <util/translation.h>
29
30
#include <condition_variable>
31
#include <cstdio>
32
#include <cstdlib>
33
#include <memory>
34
#include <optional>
35
#include <span>
36
#include <string>
37
#include <string_view>
38
#include <thread>
39
#include <unordered_map>
40
#include <vector>
41
42
#include <sys/types.h>
43
#include <sys/stat.h>
44
45
//! The set of sockets cannot be modified while waiting, so
46
//! the sleep time needs to be small to avoid new sockets stalling.
47
static constexpr auto SELECT_TIMEOUT{50ms};
48
49
//! Explicit alias for setting socket option methods.
50
static constexpr int SOCKET_OPTION_TRUE{1};
51
52
using common::InvalidPortErrMsg;
53
using util::LineReader;
54
using namespace bitcoin_http;
55
56
struct HTTPPathHandler
57
{
58
    HTTPPathHandler(std::string _prefix, bool _exactMatch, HTTPRequestHandler _handler):
59
2.33k
        prefix(_prefix), exactMatch(_exactMatch), handler(_handler)
60
2.33k
    {
61
2.33k
    }
62
    std::string prefix;
63
    bool exactMatch;
64
    HTTPRequestHandler handler;
65
};
66
67
/** HTTP module state */
68
69
static std::unique_ptr<HTTPServer> g_http_server{nullptr};
70
//! Handlers for (sub)paths
71
static GlobalMutex g_httppathhandlers_mutex;
72
static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY(g_httppathhandlers_mutex);
73
/// \anchor http_pool
74
//! Http thread pool - future: encapsulate in HttpContext
75
static ThreadPool g_threadpool_http("http");
76
static int g_max_queue_depth{100};
77
78
/** Check if a network address is allowed to access the HTTP server */
79
bool HTTPServer::ClientAllowed(const CNetAddr& netaddr) const
80
3.43k
{
81
3.43k
    if (!netaddr.IsValid())
82
0
        return false;
83
3.43k
    for(const CSubNet& subnet : m_allow_subnets)
84
3.45k
        if (subnet.Match(netaddr))
85
3.43k
            return true;
86
2
    return false;
87
3.43k
}
88
89
/** Initialize ACL list for HTTP server */
90
bool HTTPServer::InitHTTPAllowList()
91
1.16k
{
92
    // Must be run before StartSocketThreads() because ThreadSocketHandler()
93
    // will check m_allow_subnets from the I/O thread.
94
1.16k
    Assume(!m_thread_socket_handler.joinable());
95
96
1.16k
    m_allow_subnets.clear();
97
1.16k
    m_allow_subnets.emplace_back(LookupHost("127.0.0.1", false).value(), 8);  // always allow IPv4 local subnet
98
1.16k
    m_allow_subnets.emplace_back(LookupHost("::1", false).value());  // always allow IPv6 localhost
99
1.16k
    for (const std::string& strAllow : gArgs.GetArgs("-rpcallowip")) {
100
16
        const CSubNet subnet{LookupSubNet(strAllow)};
101
16
        if (!subnet.IsValid()) {
102
1
            uiInterface.ThreadSafeMessageBox(
103
1
                Untranslated(strprintf("Invalid -rpcallowip subnet specification: %s. Valid values are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0), a network/CIDR (e.g. 1.2.3.4/24), all ipv4 (0.0.0.0/0), or all ipv6 (::/0). RFC4193 is allowed only if -cjdnsreachable=0.", strAllow)),
104
1
                CClientUIInterface::MSG_ERROR);
105
1
            return false;
106
1
        }
107
15
        m_allow_subnets.push_back(subnet);
108
15
    }
109
1.16k
    std::string strAllowed;
110
1.16k
    for (const CSubNet& subnet : m_allow_subnets)
111
2.33k
        strAllowed += subnet.ToString() + " ";
112
1.16k
    LogDebug(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed);
113
1.16k
    return true;
114
1.16k
}
115
116
/** HTTP request method as string - use for logging only */
117
std::string_view RequestMethodString(HTTPRequestMethod m)
118
183k
{
119
183k
    switch (m) {
120
0
    using enum HTTPRequestMethod;
121
885
    case GET: return "GET";
122
183k
    case POST: return "POST";
123
0
    case HEAD: return "HEAD";
124
0
    case PUT: return "PUT";
125
5
    case UNKNOWN: return "unknown";
126
183k
    } // no default case, so the compiler can warn about missing cases
127
183k
    assert(false);
128
0
}
129
130
static void WriteNoStoreErrorReply(HTTPRequest& req, HTTPStatusCode status, std::string_view reply = {})
131
937
{
132
937
    req.WriteHeader("Cache-Control", "no-store");
133
937
    req.WriteReply(status, reply);
134
937
}
135
136
static void MaybeDispatchRequestToWorker(std::shared_ptr<HTTPRequest> hreq)
137
183k
{
138
    // Early reject unknown HTTP methods
139
183k
    if (hreq->GetRequestMethod() == HTTPRequestMethod::UNKNOWN) {
140
5
        LogDebug(BCLog::HTTP, "HTTP request from %s rejected: Unknown HTTP request method\n",
141
5
                 hreq->GetPeer().ToStringAddrPort());
142
5
        WriteNoStoreErrorReply(*hreq, HTTP_BAD_METHOD);
143
5
        return;
144
5
    }
145
146
    // Find registered handler for prefix
147
183k
    std::string strURI = hreq->GetURI();
148
183k
    std::string path;
149
183k
    LOCK(g_httppathhandlers_mutex);
150
183k
    std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
151
183k
    std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
152
211k
    for (; i != iend; ++i) {
153
211k
        bool match = false;
154
211k
        if (i->exactMatch)
155
183k
            match = (strURI == i->prefix);
156
27.5k
        else
157
27.5k
            match = strURI.starts_with(i->prefix);
158
211k
        if (match) {
159
183k
            path = strURI.substr(i->prefix.size());
160
183k
            break;
161
183k
        }
162
211k
    }
163
164
    // Dispatch to worker thread
165
183k
    if (i != iend) {
166
183k
        if (static_cast<int>(g_threadpool_http.WorkQueueSize()) >= g_max_queue_depth) {
167
903
            LogWarning("Request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting");
168
903
            WriteNoStoreErrorReply(*hreq, HTTP_SERVICE_UNAVAILABLE, "Work queue depth exceeded");
169
903
            return;
170
903
        }
171
172
183k
        auto item = [req = hreq, in_path = std::move(path), fn = i->handler]() {
173
183k
            std::string err_msg;
174
183k
            try {
175
183k
                fn(req.get(), in_path);
176
183k
                return;
177
183k
            } catch (const std::exception& e) {
178
0
                LogWarning("Unexpected error while processing request for '%s'. Error msg: '%s'", req->GetURI(), e.what());
179
0
                err_msg = e.what();
180
0
            } catch (...) {
181
0
                LogWarning("Unknown error while processing request for '%s'", req->GetURI());
182
0
                err_msg = "unknown error";
183
0
            }
184
            // Reply so the client doesn't hang waiting for the response.
185
0
            req->WriteHeader("Connection", "close");
186
            // TODO: Implement specific error formatting for the REST and JSON-RPC servers responses.
187
0
            WriteNoStoreErrorReply(*req, HTTP_INTERNAL_SERVER_ERROR, err_msg);
188
0
        };
189
190
183k
        if (auto res = g_threadpool_http.Submit(std::move(item)); !res.has_value()) {
191
0
            Assume(hreq.use_count() == 1); // ensure request will be deleted
192
            // Both SubmitError::Inactive and SubmitError::Interrupted mean shutdown
193
0
            LogWarning("HTTP request rejected during server shutdown: '%s'", SubmitErrorString(res.error()));
194
0
            WriteNoStoreErrorReply(*hreq, HTTP_SERVICE_UNAVAILABLE, "Request rejected during server shutdown");
195
0
            return;
196
0
        }
197
183k
    } else {
198
12
        WriteNoStoreErrorReply(*hreq, HTTP_NOT_FOUND);
199
12
    }
200
183k
}
201
202
static void RejectRequest(std::unique_ptr<HTTPRequest> hreq)
203
0
{
204
0
    LogDebug(BCLog::HTTP, "Rejecting request while shutting down");
205
0
    WriteNoStoreErrorReply(*hreq, HTTP_SERVICE_UNAVAILABLE);
206
0
}
207
208
static std::vector<std::pair<std::string, uint16_t>> GetBindAddresses()
209
1.15k
{
210
1.15k
    uint16_t http_port{static_cast<uint16_t>(gArgs.GetIntArg("-rpcport", BaseParams().RPCPort()))};
211
1.15k
    std::vector<std::pair<std::string, uint16_t>> endpoints;
212
213
    // Determine what addresses to bind to
214
    // To prevent misconfiguration and accidental exposure of the RPC
215
    // interface, require -rpcallowip and -rpcbind to both be specified
216
    // together. If either is missing, ignore both values, bind to localhost
217
    // instead, and log warnings.
218
1.15k
    if (gArgs.GetArgs("-rpcallowip").empty() || gArgs.GetArgs("-rpcbind").empty()) { // Default to loopback if not allowing external IPs
219
1.15k
        endpoints.emplace_back("::1", http_port);
220
1.15k
        endpoints.emplace_back("127.0.0.1", http_port);
221
1.15k
        if (!gArgs.GetArgs("-rpcallowip").empty()) {
222
3
            LogWarning("Option -rpcallowip was specified without -rpcbind; this doesn't usually make sense");
223
3
        }
224
1.15k
        if (!gArgs.GetArgs("-rpcbind").empty()) {
225
0
            LogWarning("Option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect");
226
0
        }
227
1.15k
    } else { // Specific bind addresses
228
14
        for (const std::string& strRPCBind : gArgs.GetArgs("-rpcbind")) {
229
14
            uint16_t port{http_port};
230
14
            std::string host;
231
14
            if (!SplitHostPort(strRPCBind, port, host)) {
232
0
                LogError("%s\n", InvalidPortErrMsg("-rpcbind", strRPCBind).original);
233
0
                return {}; // empty
234
0
            }
235
14
            endpoints.emplace_back(host, port);
236
14
        }
237
9
    }
238
1.15k
    return endpoints;
239
1.15k
}
240
241
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
242
2.33k
{
243
2.33k
    LogDebug(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
244
2.33k
    LOCK(g_httppathhandlers_mutex);
245
2.33k
    pathHandlers.emplace_back(prefix, exactMatch, handler);
246
2.33k
}
247
248
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
249
19.2k
{
250
19.2k
    LOCK(g_httppathhandlers_mutex);
251
19.2k
    std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
252
19.2k
    std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
253
19.2k
    for (; i != iend; ++i)
254
2.33k
        if (i->prefix == prefix && i->exactMatch == exactMatch)
255
2.33k
            break;
256
19.2k
    if (i != iend)
257
2.33k
    {
258
2.33k
        LogDebug(BCLog::HTTP, "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
259
2.33k
        pathHandlers.erase(i);
260
2.33k
    }
261
19.2k
}
262
263
using util::Split;
264
265
std::optional<std::string> HTTPHeaders::FindFirst(const std::string_view key) const
266
816k
{
267
3.78M
    for (const auto& item : m_headers) {
268
3.78M
        if (CaseInsensitiveEqual(key, item.first)) {
269
366k
            return item.second;
270
366k
        }
271
3.78M
    }
272
449k
    return std::nullopt;
273
816k
}
274
275
std::vector<std::string_view> HTTPHeaders::FindAll(const std::string_view key) const
276
265k
{
277
265k
    std::vector<std::string_view> ret;
278
1.58M
    for (const auto& item : m_headers) {
279
1.58M
        if (CaseInsensitiveEqual(key, item.first)) {
280
264k
            ret.push_back(item.second);
281
264k
        }
282
1.58M
    }
283
265k
    return ret;
284
265k
}
285
286
void HTTPHeaders::Write(std::string&& key, std::string&& value)
287
1.65M
{
288
1.65M
    m_headers.emplace_back(std::move(key), std::move(value));
289
1.65M
}
290
291
void HTTPHeaders::RemoveAll(std::string_view key)
292
1.10k
{
293
3.32k
    auto moved = std::ranges::remove_if(m_headers, [key] (auto& pair) {
294
3.32k
        return CaseInsensitiveEqual(key, pair.first);
295
3.32k
    });
296
1.10k
    m_headers.erase(moved.begin(), moved.end());
297
1.10k
}
298
299
bool HTTPHeaders::Read(util::LineReader& reader, bool write)
300
184k
{
301
    // Headers https://httpwg.org/specs/rfc9110.html#rfc.section.6.3
302
    // A sequence of Field Lines https://httpwg.org/specs/rfc9110.html#rfc.section.5.2
303
184k
    size_t start{reader.Consumed()};
304
1.28M
    while (auto maybe_line = reader.ReadLine()) {
305
1.28M
        if (reader.Consumed() - start + m_consumed > MAX_HEADERS_SIZE) throw std::runtime_error("HTTP headers exceed size limit");
306
307
1.28M
        const std::string_view& line = *maybe_line;
308
309
        // An empty line indicates end of the headers section https://www.rfc-editor.org/rfc/rfc2616#section-4
310
1.28M
        if (line.empty()) {
311
            // Ensure all headers are accounted for in case there is a chunked trailer
312
183k
            m_consumed += reader.Consumed() - start;
313
183k
            return true;
314
183k
        }
315
316
        // "Field values containing CR, LF, or NUL characters are invalid and dangerous"
317
        // https://httpwg.org/specs/rfc9110.html#rfc.section.5.5
318
        // A sender MUST NOT generate a bare CR (a CR character not immediately followed by LF)
319
        // within any protocol elements other than the content.
320
        // A recipient of such a bare CR MUST consider that element to be invalid...
321
        // https://httpwg.org/specs/rfc9112.html#rfc.section.2.2
322
1.10M
        if (line.find_first_of("\r\n\0", 0, 3) != std::string_view::npos) throw std::runtime_error("Header contains invalid character");
323
324
        // Header line must have at least one ":"
325
        // keys are not allowed to have delimiters like ":" but values are
326
        // https://httpwg.org/specs/rfc9110.html#rfc.section.5.6.2
327
1.10M
        const size_t pos{line.find(':')};
328
1.10M
        if (pos == std::string_view::npos) throw std::runtime_error("HTTP header missing colon (:)");
329
330
        // Whitespace is strictly not allowed in the field-name (key)
331
        // https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.2
332
1.10M
        std::string_view key = line.substr(0, pos);
333
1.10M
        if (key.find_first_of(" \t\n\r\f\v") != std::string_view::npos) throw std::runtime_error("Invalid header field-name contains whitespace");
334
        // Whitespace is optional in the value and can be trimmed
335
1.10M
        std::string value = util::TrimString(std::string_view(line).substr(pos + 1));
336
337
        // Header keys are Field Names: https://httpwg.org/specs/rfc9110.html#fields.names
338
        // which consist of "tokens": https://httpwg.org/specs/rfc9110.html#rfc.section.5.6.2
339
        // that can not be empty.
340
1.10M
        if (key.empty()) throw std::runtime_error("Empty HTTP header name");
341
342
1.10M
        if (write) {
343
1.10M
            Write(std::string(key), std::move(value));
344
1.10M
        }
345
1.10M
    }
346
347
    // We have not received all the request headers yet.
348
    // Keep track of how much data we have already consumed to enforce
349
    // the total limit over multiple read operations.
350
14
    m_consumed += reader.Consumed() - start;
351
352
14
    return false;
353
184k
}
354
355
std::string HTTPHeaders::Stringify() const
356
183k
{
357
183k
    std::string out;
358
554k
    for (const auto& [key, value] : m_headers) {
359
554k
        out += key + ": " + value + "\r\n";
360
554k
    }
361
362
    // Headers are terminated by an empty line
363
183k
    out += "\r\n";
364
365
183k
    return out;
366
183k
}
367
368
std::string HTTPResponse::StringifyHeaders() const
369
183k
{
370
183k
    return strprintf("HTTP/%d.%d %d %s\r\n%s",
371
183k
                     version.major,
372
183k
                     version.minor,
373
183k
                     status,
374
183k
                     HTTPStatusReasonString(status),
375
183k
                     headers.Stringify());
376
183k
}
377
378
bool HTTPRequest::LoadControlData(LineReader& reader)
379
183k
{
380
183k
    auto maybe_line = reader.ReadLine();
381
183k
    if (!maybe_line) return false;
382
183k
    const std::string_view& request_line = *maybe_line;
383
384
    // Request Line aka Control Data https://httpwg.org/specs/rfc9110.html#rfc.section.6.2
385
    // Three words separated by spaces, terminated by \n or \r\n
386
183k
    if (request_line.length() < MIN_REQUEST_LINE_LENGTH) throw std::runtime_error("HTTP request line too short");
387
388
    // NUL is not a valid tchar and would silently truncate
389
    // C-string-based parsers rather than being rejected as malformed.
390
    // tchar: https://www.rfc-editor.org/info/rfc7230/#section-3.2.6
391
183k
    if (request_line.find('\0') != std::string_view::npos) throw std::runtime_error("Invalid request line contains NUL");
392
393
183k
    const std::vector<std::string_view> parts{Split<std::string_view>(request_line, " ")};
394
183k
    if (parts.size() != 3) throw std::runtime_error("HTTP request line malformed");
395
396
183k
    if (parts[0] == "GET") {
397
911
        m_method = HTTPRequestMethod::GET;
398
183k
    } else if (parts[0] == "POST") {
399
183k
        m_method = HTTPRequestMethod::POST;
400
183k
    } else if (parts[0] == "HEAD") {
401
0
        m_method = HTTPRequestMethod::HEAD;
402
7
    } else if (parts[0] == "PUT") {
403
0
        m_method = HTTPRequestMethod::PUT;
404
7
    } else {
405
7
        m_method = HTTPRequestMethod::UNKNOWN;
406
7
    }
407
408
183k
    m_target = parts[1];
409
410
183k
    if (parts[2].rfind("HTTP/") != 0) throw std::runtime_error("HTTP request line malformed");
411
412
    // Version is exactly two decimal digits separated by a decimal point
413
    // https://httpwg.org/specs/rfc9110.html#rfc.section.2.5
414
183k
    const std::vector<std::string_view> version_parts{Split<std::string_view>(parts[2].substr(5), ".")};
415
183k
    if (version_parts.size() != 2) throw std::runtime_error("HTTP request line malformed");
416
183k
    if (version_parts[0].size() != 1 || version_parts[1].size() != 1) throw std::runtime_error("HTTP bad version");
417
183k
    auto major = ToIntegral<uint8_t>(version_parts[0]);
418
183k
    auto minor = ToIntegral<uint8_t>(version_parts[1]);
419
183k
    if (!major || !minor || major != 1 || minor > 9) throw std::runtime_error("HTTP bad version");
420
183k
    m_version.major = major.value();
421
183k
    m_version.minor = minor.value();
422
423
183k
    return true;
424
183k
}
425
426
bool HTTPRequest::LoadHeaders(LineReader& reader)
427
183k
{
428
183k
    return m_headers.Read(reader);
429
183k
}
430
431
bool HTTPRequest::LoadBody(LineReader& reader)
432
266k
{
433
    // https://httpwg.org/specs/rfc9112.html#message.body
434
266k
    auto transfer_encoding_header = m_headers.FindFirst("Transfer-Encoding");
435
266k
    if (transfer_encoding_header && ToLower(transfer_encoding_header.value()) == "chunked") {
436
        // Transfer-Encoding: https://datatracker.ietf.org/doc/html/rfc7230.html#section-3.3.1
437
        // Chunked Transfer Coding: https://datatracker.ietf.org/doc/html/rfc7230.html#section-4.1
438
        // see evhttp_handle_chunked_read() in libevent http.c
439
1.08k
        while (reader.Remaining() > 0) {
440
558
            if (!m_chunk_size) {
441
33
                auto maybe_chunk_size = reader.ReadLine();
442
33
                if (!maybe_chunk_size) return false;
443
444
                // Allow (but ignore) Chunk Extensions
445
                // See https://www.rfc-editor.org/rfc/rfc9112.html#name-chunk-extensions
446
33
                std::string_view chunk_size_noext{maybe_chunk_size.value()};
447
33
                const auto semicolon_pos = chunk_size_noext.find(';');
448
33
                if (semicolon_pos != chunk_size_noext.npos) {
449
3
                    chunk_size_noext.remove_suffix(chunk_size_noext.size() - semicolon_pos);
450
3
                }
451
452
33
                m_chunk_size = ToIntegral<uint64_t>(util::TrimStringView(chunk_size_noext), /*base=*/16);
453
33
                if (!m_chunk_size) throw std::runtime_error("Cannot parse chunk length value");
454
455
32
                if ((m_body.size() > MAX_BODY_SIZE) ||
456
32
                    (*m_chunk_size > MAX_BODY_SIZE - m_body.size()))
457
3
                    throw ContentTooLargeError("Chunk will exceed max body size");
458
32
            }
459
460
            // We either just read the chunk size, or we have it saved
461
            // from a prior I/O loop iteration
462
554
            Assume(m_chunk_size);
463
464
            // Last chunk has size 0
465
554
            if (*m_chunk_size == 0) {
466
                // Validate Chunked Trailer section, which is used for
467
                // additional headers sent at the end of the message.
468
                // Data consumed here is counted towards MAX_HEADERS_SIZE
469
                // along with the headers we read in the beginning of the request.
470
                // At this time we ignore and drop these data after validating.
471
                // See https://httpwg.org/specs/rfc9112.html#rfc.section.7.1.2
472
11
                return m_headers.Read(reader, /*write=*/false);
473
11
            }
474
475
            // We have not read the entire chunk from the buffer yet
476
543
            if (m_chunk_read < *m_chunk_size) {
477
                // Get what we can from the buffer
478
542
                const uint64_t chunk_need{*m_chunk_size - m_chunk_read};
479
542
                const uint64_t buffer_has{std::min(chunk_need, static_cast<uint64_t>(reader.Remaining()))};
480
481
                // Pack [partial] chunk onto body and update state
482
542
                m_body += reader.ReadLength(buffer_has);
483
542
                m_chunk_read += buffer_has;
484
542
            }
485
486
            // Even though every chunk size is explicitly declared,
487
            // they are still terminated by a CRLF we don't need,
488
            // just consume it here.
489
543
            if (m_chunk_read == *m_chunk_size) {
490
23
                auto crlf = reader.ReadLine();
491
23
                if (!crlf) {
492
                    // CRLF not found before end of buffer: it has not been received by our socket yet.
493
1
                    return false;
494
1
                }
495
                // CRLF was found but there was unexpected data after the chunk_sized chunk
496
22
                if (!crlf.value().empty()) throw std::runtime_error("Improperly terminated chunk");
497
498
                // Clear state for next chunk
499
21
                m_chunk_size.reset();
500
21
                m_chunk_read = 0;
501
21
            }
502
543
        }
503
504
        // We read all the chunks but never got the last chunk, wait for client to send more
505
524
        return false;
506
265k
    } else {
507
        // No Content-length or Transfer-Encoding header means no body, see libevent evhttp_get_body()
508
265k
        auto content_length_values{m_headers.FindAll("Content-Length")};
509
265k
        if (content_length_values.empty()) return true;
510
511
        // Duplicate Content-Length headers are allowed only if they all have the same value
512
        // https://www.rfc-editor.org/rfc/rfc7230#section-3.3.3
513
264k
        const auto& first_content_length_value{content_length_values[0]};
514
264k
        for (size_t i = 1; i < content_length_values.size(); ++i) {
515
3
            if (content_length_values[i] != first_content_length_value) throw std::runtime_error("Differing Content-Length values");
516
3
        }
517
518
264k
        const auto content_length{ToIntegral<uint64_t>(first_content_length_value)};
519
264k
        if (!content_length) throw std::runtime_error("Cannot parse Content-Length value");
520
521
264k
        if (*content_length > MAX_BODY_SIZE) throw ContentTooLargeError("Max body size exceeded");
522
523
        // A large body may arrive over multiple I/O loop iterations. Copy
524
        // whatever the buffer has now; m_body's size tracks our progress.
525
264k
        const uint64_t body_need{*content_length - m_body.size()};
526
264k
        const uint64_t buffer_has{std::min(body_need, static_cast<uint64_t>(reader.Remaining()))};
527
528
        // Pack [partial] body on and update state
529
264k
        m_body += reader.ReadLength(buffer_has);
530
531
264k
        return m_body.size() == *content_length;
532
264k
    }
533
266k
}
534
535
void HTTPRequest::WriteReply(HTTPStatusCode status, std::span<const std::byte> reply_body)
536
183k
{
537
183k
    HTTPResponse res;
538
539
    // Some response headers are determined in advance and stored in the request
540
183k
    res.headers = std::move(m_response_headers);
541
542
    // Response version matches request version
543
183k
    res.version = m_version;
544
545
    // Add response code
546
183k
    res.status = status;
547
548
    // See libevent evhttp_response_needs_body()
549
    // Response headers are different if no body is needed
550
183k
    bool needs_body{status != HTTP_NO_CONTENT && (status < 100 || status >= 200)};
551
183k
    bool needs_content_length{false};
552
553
183k
    bool keep_alive{false};
554
555
    // See libevent evhttp_make_header_response()
556
    // Expected response headers depend on protocol version
557
183k
    if (m_version.major == 1) {
558
        // HTTP/1.0
559
183k
        if (m_version.minor == 0) {
560
5
            auto connection_header{m_headers.FindFirst("Connection")};
561
5
            if (connection_header && ToLower(connection_header.value()) == "keep-alive") {
562
0
                res.headers.Write("Connection", "keep-alive");
563
0
                keep_alive = true;
564
                // HTTP/1.0 connections are closed by default so EOF is sufficient
565
                // to indicate end of the body. Adding Content-Length a special case.
566
0
                if (needs_body) needs_content_length = true;
567
0
            }
568
5
        }
569
570
        // HTTP/1.1
571
183k
        if (m_version.minor >= 1) {
572
183k
            const int64_t now_seconds{TicksSinceEpoch<std::chrono::seconds>(NodeClock::now())};
573
183k
            res.headers.Write("Date", FormatRFC1123DateTime(now_seconds));
574
575
            // HTTP/1.1 connections are kept alive by default and always require Content-Length.
576
183k
            if (needs_body) needs_content_length = true;
577
578
            // Default for HTTP/1.1
579
183k
            keep_alive = true;
580
183k
        }
581
183k
    }
582
583
183k
    if (needs_content_length) {
584
183k
        res.headers.Write("Content-Length", util::ToString(reply_body.size()));
585
183k
    }
586
587
183k
    if (needs_body && !res.headers.FindFirst("Content-Type")) {
588
        // Default type from libevent evhttp_new_object()
589
1.07k
        res.headers.Write("Content-Type", "text/html; charset=ISO-8859-1");
590
1.07k
    }
591
592
183k
    auto connection_header{m_headers.FindFirst("Connection")};
593
183k
    if (connection_header && ToLower(connection_header.value()) == "close") {
594
        // Might not exist already but we need to replace it, not append to it
595
1.10k
        res.headers.RemoveAll("Connection");
596
597
1.10k
        res.headers.Write("Connection", "close");
598
1.10k
        keep_alive = false;
599
1.10k
    }
600
601
183k
    if (std::shared_ptr client{m_client.lock()}) {
602
183k
        client->Send(res, reply_body, keep_alive);
603
183k
    }
604
183k
}
605
606
void HTTPRemoteClient::Send(const HTTPResponse& res, std::span<const std::byte> reply_body, bool keep_alive)
607
183k
{
608
183k
    m_keep_alive = keep_alive;
609
610
    // Serialize the response headers
611
183k
    const std::string headers{res.StringifyHeaders()};
612
183k
    const auto headers_bytes{std::as_bytes(std::span{headers})};
613
614
183k
    bool send_buffer_was_empty{false};
615
    // Fill the send buffer with the complete serialized response headers + body
616
183k
    {
617
183k
        LOCK(m_send_mutex);
618
183k
        send_buffer_was_empty = m_send_buffer.empty();
619
183k
        m_send_buffer.insert(m_send_buffer.end(), headers_bytes.begin(), headers_bytes.end());
620
621
        // We've been using std::span up until now but it is finally time to copy
622
        // data. The original data will go out of scope when WriteReply() returns.
623
        // This is analogous to the memcpy() in libevent's evbuffer_add()
624
183k
        m_send_buffer.insert(m_send_buffer.end(), reply_body.begin(), reply_body.end());
625
626
        // If the buffer already held data, the I/O thread is (or soon will be)
627
        // draining it, so flag that there is more data to send. This must happen
628
        // while holding m_send_mutex and while the buffer is known non-empty:
629
        // setting m_send_ready after releasing the lock would race with the I/O
630
        // thread draining the buffer to empty and clearing m_send_ready in
631
        // between, leaving m_send_ready set on an empty buffer. The I/O loop would
632
        // then only ever poll the socket for writeability, never read the client's
633
        // next request, and wedge the connection.
634
183k
        if (!send_buffer_was_empty) m_send_ready = true;
635
183k
    }
636
637
183k
    LogDebug(
638
183k
        BCLog::HTTP,
639
183k
        "HTTPResponse (status code: %d size: %lld) added to send buffer for client %s (id=%llu)",
640
183k
        res.status,
641
183k
        headers_bytes.size() + reply_body.size(),
642
183k
        m_origin,
643
183k
        m_id);
644
645
    // If the send buffer was empty before we wrote this reply, we can try an
646
    // optimistic send akin to CConnman::PushMessage() in which we
647
    // push the data directly out the socket to client right now, instead
648
    // of waiting for the next iteration of the I/O loop.
649
183k
    if (send_buffer_was_empty) {
650
183k
        MaybeSendBytesFromBuffer();
651
183k
    }
652
653
    // Signal to the I/O loop that we are ready to handle the next request.
654
183k
    m_req_busy = false;
655
183k
}
656
657
CService HTTPRequest::GetPeer() const
658
182k
{
659
182k
    if (std::shared_ptr c{m_client.lock()}) {
660
182k
        return c->GetPeer();
661
182k
    } else {
662
1
        return {};
663
1
    }
664
182k
}
665
666
std::optional<std::string> HTTPRequest::GetQueryParameter(const std::string_view key) const
667
93
{
668
93
    return GetQueryParameterFromUri(m_target, key);
669
93
}
670
671
// See libevent http.c evhttp_parse_query_impl()
672
// and https://www.rfc-editor.org/rfc/rfc3986#section-3.4
673
std::optional<std::string> GetQueryParameterFromUri(const std::string_view uri, const std::string_view key)
674
107
{
675
    // find query in URI
676
107
    size_t start = uri.find('?');
677
107
    if (start == std::string::npos) return std::nullopt;
678
97
    size_t end = uri.find('#', start);
679
97
    if (end == std::string::npos) {
680
97
        end = uri.length();
681
97
    }
682
97
    const std::string_view query{uri.data() + start + 1, end - start - 1};
683
    // find requested parameter in query
684
97
    const std::vector<std::string_view> params{Split<std::string_view>(query, "&")};
685
124
    for (const std::string_view& param : params) {
686
124
        size_t delim = param.find('=');
687
124
        if (key == UrlDecode(param.substr(0, delim))) {
688
85
            if (delim == std::string::npos) {
689
0
                return "";
690
85
            } else {
691
85
                return std::string(UrlDecode(param.substr(delim + 1)));
692
85
            }
693
85
        }
694
124
    }
695
12
    return std::nullopt;
696
97
}
697
698
std::optional<std::string> HTTPRequest::GetHeader(const std::string_view hdr) const
699
182k
{
700
182k
    return m_headers.FindFirst(hdr);
701
182k
}
702
703
void HTTPRequest::WriteHeader(std::string&& hdr, std::string&& value)
704
184k
{
705
184k
    m_response_headers.Write(std::move(hdr), std::move(value));
706
184k
}
707
708
util::Expected<void, std::string> HTTPServer::BindAndStartListening(const CService& to)
709
2.31k
{
710
    // Create socket for listening for incoming connections
711
2.31k
    sockaddr_storage storage;
712
2.31k
    auto sa = reinterpret_cast<sockaddr*>(&storage);
713
2.31k
    socklen_t len{sizeof(storage)};
714
2.31k
    if (!to.GetSockAddr(sa, &len)) {
715
1
        return util::Unexpected{strprintf("Bind address family for %s not supported", to.ToStringAddrPort())};
716
1
    }
717
718
2.31k
    std::unique_ptr<Sock> sock{CreateSock(to.GetSAFamily(), SOCK_STREAM, IPPROTO_TCP)};
719
2.31k
    if (!sock) {
720
0
        return util::Unexpected{strprintf("Cannot create %s listen socket: %s",
721
0
                                          to.ToStringAddrPort(),
722
0
                                          NetworkErrorString(WSAGetLastError()))};
723
0
    }
724
725
#ifdef WIN32
726
    // Prevent another application from binding to the same address and port and
727
    // intercepting RPC credentials.
728
    // SO_REUSEADDR on Windows is non-exclusive so another process could bind to
729
    // the same port.
730
    if (sock->SetSockOpt(SOL_SOCKET, SO_EXCLUSIVEADDRUSE, &SOCKET_OPTION_TRUE, sizeof(SOCKET_OPTION_TRUE)) == SOCKET_ERROR) {
731
        return util::Unexpected{strprintf("Cannot set SO_EXCLUSIVEADDRUSE on %s listen socket: %s",
732
                                          to.ToStringAddrPort(),
733
                                          NetworkErrorString(WSAGetLastError()))};
734
    }
735
#else
736
    // Allow binding if the port is still in TIME_WAIT state after
737
    // the program was closed and restarted.
738
2.31k
    if (sock->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, &SOCKET_OPTION_TRUE, sizeof(SOCKET_OPTION_TRUE)) == SOCKET_ERROR) {
739
0
        LogDebug(BCLog::HTTP,
740
0
                 "Cannot set SO_REUSEADDR on %s listen socket: %s, continuing anyway",
741
0
                 to.ToStringAddrPort(),
742
0
                 NetworkErrorString(WSAGetLastError()));
743
0
    }
744
2.31k
#endif
745
746
    // some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
747
    // and enable it by default or not. Try to enable it, if possible.
748
2.31k
    if (to.IsIPv6()) {
749
1.15k
#ifdef IPV6_V6ONLY
750
1.15k
        if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, &SOCKET_OPTION_TRUE, sizeof(SOCKET_OPTION_TRUE)) == SOCKET_ERROR) {
751
0
            LogDebug(BCLog::HTTP,
752
0
                     "Cannot set IPV6_V6ONLY on %s listen socket: %s, continuing anyway",
753
0
                     to.ToStringAddrPort(),
754
0
                     NetworkErrorString(WSAGetLastError()));
755
0
        }
756
1.15k
#endif
757
#ifdef WIN32
758
        int prot_level{PROTECTION_LEVEL_UNRESTRICTED};
759
        if (sock->SetSockOpt(IPPROTO_IPV6,
760
                             IPV6_PROTECTION_LEVEL,
761
                             &prot_level,
762
                             sizeof(prot_level)) == SOCKET_ERROR) {
763
            LogDebug(BCLog::HTTP,
764
                     "Cannot set IPV6_PROTECTION_LEVEL on %s listen socket: %s, continuing anyway",
765
                     to.ToStringAddrPort(),
766
                     NetworkErrorString(WSAGetLastError()));
767
        }
768
#endif
769
1.15k
    }
770
771
2.31k
    if (sock->Bind(sa, len) == SOCKET_ERROR) {
772
0
        const int err{WSAGetLastError()};
773
0
        if (err == WSAEADDRINUSE) {
774
0
            return util::Unexpected{strprintf("Unable to bind to %s on this computer. %s is probably already running.",
775
0
                                              to.ToStringAddrPort(),
776
0
                                              CLIENT_NAME)};
777
0
        } else {
778
0
            return util::Unexpected{strprintf("Unable to bind to %s on this computer (bind returned error %s)",
779
0
                                              to.ToStringAddrPort(),
780
0
                                              NetworkErrorString(err))};
781
0
        }
782
0
    }
783
784
    // Listen for incoming connections
785
2.31k
    if (sock->Listen(SOMAXCONN) == SOCKET_ERROR) {
786
0
        return util::Unexpected{strprintf("Cannot listen on %s: %s",
787
0
                                          to.ToStringAddrPort(),
788
0
                                          NetworkErrorString(WSAGetLastError()))};
789
0
    }
790
791
2.31k
    m_listen.emplace_back(std::move(sock));
792
793
2.31k
    return {};
794
2.31k
}
795
796
void HTTPServer::StopListening()
797
1.16k
{
798
1.16k
    m_listen.clear();
799
1.16k
}
800
801
void HTTPServer::StartSocketsThreads()
802
1.15k
{
803
    // The socket handler reads m_allow_subnets in ClientAllowed(). InitHTTPAllowList()
804
    // must have populated it first; localhost entries are always added, so an empty
805
    // list means it was never called and every connection is rejected.
806
1.15k
    Assume(!m_allow_subnets.empty());
807
808
1.15k
    m_thread_socket_handler = std::thread(&util::TraceThread,
809
1.15k
                                          "http",
810
1.15k
                                          [this] { ThreadSocketHandler(); });
811
1.15k
}
812
813
void HTTPServer::JoinSocketsThreads()
814
1.16k
{
815
1.16k
    if (m_thread_socket_handler.joinable()) {
816
1.15k
        m_thread_socket_handler.join();
817
1.15k
    }
818
1.16k
}
819
820
std::unique_ptr<Sock> HTTPServer::AcceptConnection(const Sock& listen_sock, CService& addr)
821
6.78k
{
822
    // Make sure we only operate on our own listening sockets
823
6.78k
    Assume(std::ranges::any_of(m_listen, [&](const auto& sock) { return sock.get() == &listen_sock; }));
824
825
6.78k
    sockaddr_storage storage;
826
6.78k
    socklen_t len{sizeof(storage)};
827
6.78k
    auto sa = reinterpret_cast<sockaddr*>(&storage);
828
829
6.78k
    auto sock{listen_sock.Accept(sa, &len)};
830
831
6.78k
    if (!sock) {
832
3.35k
        const int err{WSAGetLastError()};
833
3.35k
        if (err != WSAEWOULDBLOCK) {
834
0
            LogDebug(BCLog::HTTP,
835
0
                     "Cannot accept new connection: %s",
836
0
                     NetworkErrorString(err));
837
0
        }
838
3.35k
        return {};
839
3.35k
    }
840
841
    // The OS handed us a valid socket but we can't determine its source address.
842
3.43k
    if (!addr.SetSockAddr(sa, len)) {
843
0
        LogDebug(BCLog::HTTP,
844
0
                 "Unknown socket family");
845
0
    }
846
847
    // Early address-based allow check
848
3.43k
    if (!ClientAllowed(addr)) {
849
2
        LogDebug(BCLog::HTTP, "Connection from %s rejected: Client network is not allowed HTTP access\n",
850
2
                 addr.ToStringAddrPort());
851
        // Socket destroyed, connection aborted
852
2
        return {};
853
2
    }
854
855
3.43k
    return sock;
856
3.43k
}
857
858
HTTPServer::Id HTTPServer::GetNewId()
859
3.43k
{
860
3.43k
    return m_next_id.fetch_add(1, std::memory_order_relaxed);
861
3.43k
}
862
863
void HTTPServer::NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& addr)
864
3.43k
{
865
3.43k
    if (!sock->IsSelectable()) {
866
0
        LogDebug(BCLog::HTTP,
867
0
                 "connection from %s dropped: non-selectable socket",
868
0
                 addr.ToStringAddrPort());
869
0
        return;
870
0
    }
871
872
    // According to the internet TCP_NODELAY is not carried into accepted sockets
873
    // on all platforms.  Set it again here just to be sure.
874
3.43k
    if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &SOCKET_OPTION_TRUE, sizeof(SOCKET_OPTION_TRUE)) == SOCKET_ERROR) {
875
0
        LogDebug(BCLog::HTTP, "connection from %s: unable to set TCP_NODELAY, continuing anyway",
876
0
                 addr.ToStringAddrPort());
877
0
    }
878
879
3.43k
    const Id id{GetNewId()};
880
881
3.43k
    m_connected.push_back(std::make_shared<HTTPRemoteClient>(id, addr, std::move(sock)));
882
    // Report back to the main thread
883
3.43k
    m_connected_size.fetch_add(1, std::memory_order_relaxed);
884
885
3.43k
    LogDebug(BCLog::HTTP,
886
3.43k
             "HTTP Connection accepted from %s (id=%llu)",
887
3.43k
             addr.ToStringAddrPort(), id);
888
3.43k
}
889
890
void HTTPServer::SocketHandlerConnected(const IOReadiness& io_readiness) const
891
450k
{
892
1.60M
    for (const auto& [sock, events] : io_readiness.events_per_sock) {
893
1.60M
        if (m_interrupt_net) {
894
1.13k
            return;
895
1.13k
        }
896
897
1.60M
        auto it{io_readiness.httpclients_per_sock.find(sock)};
898
1.60M
        if (it == io_readiness.httpclients_per_sock.end()) {
899
897k
            continue;
900
897k
        }
901
702k
        const std::shared_ptr<HTTPRemoteClient>& client{it->second};
902
903
702k
        bool send_ready = events.occurred & Sock::SendEvent;
904
702k
        bool recv_ready = events.occurred & Sock::RecvEvent;
905
702k
        bool err_ready = events.occurred & Sock::ErrorEvent;
906
907
702k
        if (send_ready) {
908
            // Try to send as much data as is ready for this client.
909
            // If there's an error we can skip the receive phase for this client
910
            // because we need to disconnect.
911
378
            if (!client->MaybeSendBytesFromBuffer()) {
912
0
                recv_ready = false;
913
0
            }
914
378
        }
915
916
702k
        if (recv_ready || err_ready) {
917
268k
            client->Receive();
918
268k
        }
919
        // Process as much received data as we can.
920
        // This executes for every client whether or not reading or writing
921
        // took place because it also (might) parse a request we have already
922
        // received and pass it to a worker thread.
923
702k
        if (std::unique_ptr<HTTPRequest> request{HTTPRemoteClient::TryReadRequest(client)})
924
183k
        {
925
183k
            LOCK(m_request_dispatcher_mutex);
926
183k
            m_request_dispatcher(std::move(request));
927
183k
        }
928
702k
    }
929
450k
}
930
931
void HTTPRemoteClient::Receive()
932
268k
{
933
268k
    char buf[0x10000]; // typical socket buffer is 8K-64K
934
935
268k
    const ssize_t nrecv{WITH_LOCK(
936
268k
        m_sock_mutex,
937
268k
        return m_sock->Recv(buf, sizeof(buf), MSG_DONTWAIT);)};
938
939
268k
    if (nrecv < 0) {
940
0
        const int err = WSAGetLastError();
941
0
        if (IOErrorIsPermanent(err)) {
942
0
            LogDebug(
943
0
                BCLog::HTTP,
944
0
                "Permanent read error from %s (id=%llu): %s",
945
0
                m_origin,
946
0
                m_id,
947
0
                NetworkErrorString(err));
948
0
            m_disconnect = true;
949
0
        }
950
268k
    } else if (nrecv == 0) {
951
2.28k
        LogDebug(
952
2.28k
            BCLog::HTTP,
953
2.28k
            "Received EOF from %s (id=%llu)",
954
2.28k
            m_origin,
955
2.28k
            m_id);
956
2.28k
        m_disconnect = true;
957
266k
    } else {
958
        // Reset idle timeout
959
266k
        m_idle_since = Now<SteadySeconds>();
960
961
        // Prevent disconnect until all requests are completely handled.
962
266k
        m_connection_busy = true;
963
964
        // Copy data from socket buffer to client receive buffer
965
266k
        m_recv_buffer.insert(
966
266k
            m_recv_buffer.end(),
967
266k
            buf,
968
266k
            buf + nrecv);
969
266k
    }
970
268k
}
971
972
void HTTPServer::SocketHandlerListening(const Sock::EventsPerSock& events_per_sock)
973
450k
{
974
450k
    if (m_stop_accepting) return;
975
896k
    for (const auto& sock : m_listen) {
976
896k
        if (m_interrupt_net) {
977
3
            return;
978
3
        }
979
896k
        const auto it = events_per_sock.find(sock);
980
896k
        if (it != events_per_sock.end() && it->second.occurred & Sock::RecvEvent) {
981
            // Drain all pending connections from this socket up to the limit.
982
            // Stop early if the kernel queue is empty (AcceptConnection returns null)
983
            // or if accepting the last connection brought us to the limit.
984
6.79k
            while (GetConnectionsCount() < static_cast<size_t>(m_rpcmaxconnections)) {
985
6.78k
                CService addr_accepted;
986
6.78k
                auto sock_accepted{AcceptConnection(*sock, addr_accepted)};
987
6.78k
                if (!sock_accepted) break;
988
3.43k
                NewSockAccepted(std::move(sock_accepted), addr_accepted);
989
3.43k
            }
990
3.36k
        }
991
896k
    }
992
448k
}
993
994
HTTPServer::IOReadiness HTTPServer::GenerateWaitSockets() const
995
450k
{
996
450k
    IOReadiness io_readiness;
997
998
    // If the server is already handling its max connected clients count,
999
    // don't bother checking the listening sockets for new inbound connections.
1000
    // Leave them in the kernel's queue until space in the application opens
1001
    // up (or the client times out on its own).
1002
450k
    if (GetConnectionsCount() < static_cast<size_t>(m_rpcmaxconnections)) {
1003
900k
        for (const auto& sock : m_listen) {
1004
900k
            io_readiness.events_per_sock.emplace(sock, Sock::Events{Sock::RecvEvent});
1005
900k
        }
1006
450k
    }
1007
1008
702k
    for (const auto& http_client : m_connected) {
1009
        // Safely copy the shared pointer to the socket
1010
702k
        std::shared_ptr<Sock> sock{http_client->GetSock()};
1011
1012
        // Check if client is ready to send data. Don't try to receive again
1013
        // until the send buffer is cleared (all data sent to client).
1014
        // Keep this as a separate critical section from the m_sock_mutex one above:
1015
        // never hold m_sock_mutex and m_send_mutex at the same time here.
1016
        // MaybeSendBytesFromBuffer() locks m_send_mutex then m_sock_mutex, so nesting
1017
        // them in the opposite order here would risk a lock-order inversion deadlock.
1018
702k
        Sock::Event event{0};
1019
702k
        if (http_client->ReadyToSend()) {
1020
378
            event = Sock::SendEvent;
1021
702k
        } else if (http_client->GetRequest() != nullptr || http_client->ReceiveBufferEmpty()) {
1022
            // Read from the socket when the parser has an incomplete request in
1023
            // progress (needs more bytes) or when the buffer is empty. If the
1024
            // buffer is non-empty but no parse is in progress, leave event=0:
1025
            // the client stays in the I/O map so TryReadRequest() runs first to
1026
            // consume buffered bytes before admitting more socket data. Excess
1027
            // pipelined data then backs up in the kernel socket buffer, applying
1028
            // TCP backpressure instead of accumulating without bound in m_recv_buffer.
1029
689k
            event = Sock::RecvEvent;
1030
689k
        }
1031
1032
702k
        io_readiness.events_per_sock.emplace(sock, Sock::Events{event});
1033
702k
        io_readiness.httpclients_per_sock.emplace(sock, http_client);
1034
702k
    }
1035
1036
450k
    return io_readiness;
1037
450k
}
1038
1039
/// \anchor http
1040
void HTTPServer::ThreadSocketHandler()
1041
1.15k
{
1042
451k
    while (!m_interrupt_net) {
1043
        // Check for the readiness of the already connected sockets and the
1044
        // listening sockets in one call ("readiness" as in poll(2) or
1045
        // select(2)). If none are ready, wait for a short while and return
1046
        // empty sets.
1047
450k
        auto io_readiness{GenerateWaitSockets()};
1048
450k
        if (io_readiness.events_per_sock.empty() ||
1049
            // WaitMany() may as well be a static method, the context of the first Sock in the vector is not relevant.
1050
450k
            !io_readiness.events_per_sock.begin()->first->WaitMany(SELECT_TIMEOUT,
1051
450k
                                                                   io_readiness.events_per_sock)) {
1052
0
            m_interrupt_net.sleep_for(SELECT_TIMEOUT);
1053
0
        }
1054
1055
        // Service (send/receive) each of the already connected sockets.
1056
450k
        SocketHandlerConnected(io_readiness);
1057
1058
        // Accept new connections from listening sockets.
1059
450k
        SocketHandlerListening(io_readiness.events_per_sock);
1060
1061
        // Disconnect any clients that have been flagged.
1062
450k
        DisconnectClients();
1063
450k
    }
1064
1.15k
}
1065
1066
std::unique_ptr<HTTPRequest> HTTPRemoteClient::TryReadRequest(const std::shared_ptr<HTTPRemoteClient>& client)
1067
702k
{
1068
    // If we are already handling a request from
1069
    // this client, do nothing. We'll check again on the next I/O
1070
    // loop iteration.
1071
702k
    if (client->m_req_busy) return nullptr;
1072
1073
525k
    if (!client->m_req) {
1074
187k
        client->m_req = std::make_unique<HTTPRequest>(client);
1075
187k
    }
1076
1077
525k
    try {
1078
        // Read data from the buffer into the current request
1079
525k
        client->ReadRequest(*client->m_req);
1080
525k
    } catch (const ContentTooLargeError& e) {
1081
3
        LogDebug(
1082
3
            BCLog::HTTP,
1083
3
            "HTTP request body too large from client %s (id=%llu): %s",
1084
3
            client->m_origin,
1085
3
            client->m_id,
1086
3
            e.what());
1087
1088
3
        WriteNoStoreErrorReply(*client->m_req, HTTP_CONTENT_TOO_LARGE);
1089
3
        client->m_disconnect = true;
1090
3
        return nullptr;
1091
14
    } catch (const std::runtime_error& e) {
1092
14
        LogDebug(
1093
14
            BCLog::HTTP,
1094
14
            "Error reading HTTP request from client %s (id=%llu): %s",
1095
14
            client->m_origin,
1096
14
            client->m_id,
1097
14
            e.what());
1098
1099
        // We failed to read a complete request from the buffer
1100
14
        WriteNoStoreErrorReply(*client->m_req, HTTP_BAD_REQUEST);
1101
14
        client->m_disconnect = true;
1102
14
        return nullptr;
1103
14
    }
1104
1105
    // If the request is ready, hand it to a worker.
1106
525k
    if (client->m_req->GetState() == HTTPRequest::State::Complete) {
1107
183k
        LogDebug(
1108
183k
            BCLog::HTTP,
1109
183k
            "Received a %s request for %s from %s (id=%llu)",
1110
183k
            RequestMethodString(client->m_req->GetRequestMethod()),
1111
183k
            client->m_req->GetURI(),
1112
183k
            client->m_origin,
1113
183k
            client->m_id);
1114
1115
183k
        client->m_req_busy = true;
1116
183k
        return std::move(client->m_req);
1117
183k
    }
1118
1119
341k
    return nullptr;
1120
525k
}
1121
1122
void HTTPServer::DisconnectClients()
1123
450k
{
1124
450k
    const auto now{Now<SteadySeconds>()};
1125
450k
    size_t erased = std::erase_if(m_connected,
1126
706k
                                  [&](auto& client) {
1127
706k
                                      return client->MaybeDisconnect(now,
1128
706k
                                                                     m_rpcservertimeout,
1129
706k
                                                                     /*disconnect_all=*/m_disconnect_all_clients);
1130
706k
                                  });
1131
450k
    if (erased > 0) {
1132
        // Report back to the main thread
1133
3.22k
        m_connected_size.fetch_sub(erased, std::memory_order_relaxed);
1134
3.22k
    }
1135
450k
}
1136
1137
bool HTTPRemoteClient::MaybeDisconnect(std::chrono::time_point<SteadyClock> now, std::chrono::seconds rpcservertimeout, bool disconnect_all)
1138
706k
{
1139
    // First check for idle timeout. We reset the timer when we send and receive data,
1140
    // but if the server is busy handling a request we should ignore the timeout until
1141
    // the reply is sent. If we did erase the shared_ptr<HTTPRemoteClient> reference in m_connected
1142
    // while the server is busy with a request, it might be prematurely dropped before
1143
    // the response has been sent, or if the HTTPRequest was holding a temporary shared_ptr
1144
    // client on a worker thread - it would keep the socket open even after "disconnecting".
1145
706k
    const bool is_idle{rpcservertimeout.count() > 0 &&
1146
706k
                       now - m_idle_since.load() > rpcservertimeout &&
1147
706k
                       !m_req_busy};
1148
1149
    // Disconnect this client due to error, end of communication, or idle timeout.
1150
    // May drop unsent data if we are closing due to error.
1151
706k
    if (m_disconnect || is_idle) {
1152
2.41k
        if (is_idle) {
1153
5
            LogDebug(BCLog::HTTP,
1154
5
                     "HTTP client idle timeout %s (id=%llu)",
1155
5
                     m_origin,
1156
5
                     m_id);
1157
5
        }
1158
703k
    } else {
1159
        // Disconnect this client because the server is shutting
1160
        // down and we need to disconnect all clients...
1161
703k
        if (disconnect_all) {
1162
            // ...unless we still have data for this client.
1163
1.01k
            if (m_connection_busy) {
1164
                // There is still data for this healthy-connected client.
1165
                // Continue the I/O loop until all data is sent or an error is encountered.
1166
0
                return false;
1167
1.01k
            } else {
1168
                // This is a healthy persistent connection (e.g. keep-alive)
1169
                // but it's time to say goodbye.
1170
1.01k
                ;
1171
1.01k
            }
1172
702k
        } else {
1173
            // No reason to disconnect.
1174
702k
            return false;
1175
702k
        }
1176
703k
    }
1177
    // No reason NOT to disconnect, log and remove.
1178
3.43k
    LogDebug(BCLog::HTTP,
1179
3.43k
             "Disconnecting HTTP client %s (id=%llu)",
1180
3.43k
             m_origin,
1181
3.43k
             m_id);
1182
3.43k
    return true;
1183
706k
}
1184
1185
void HTTPServer::ClearConnectedClients()
1186
1.16k
{
1187
1.16k
    Assume(!m_thread_socket_handler.joinable()); // must be called after JoinSocketsThreads()
1188
1.16k
    if (m_connected.empty()) return;
1189
0
    LogWarning("Force-disconnecting %d HTTP client(s) that did not disconnect gracefully", m_connected.size());
1190
0
    m_connected_size.fetch_sub(m_connected.size(), std::memory_order_relaxed);
1191
0
    m_connected.clear();
1192
0
}
1193
1194
void HTTPRemoteClient::ReadRequest(HTTPRequest& req)
1195
525k
{
1196
525k
    if (m_recv_buffer.empty()) return;
1197
1198
266k
    LineReader reader(m_recv_buffer, MAX_HEADERS_SIZE);
1199
1200
266k
    try {
1201
266k
        switch (req.GetState()) {
1202
183k
        case HTTPRequest::State::Init:
1203
183k
            if (!req.LoadControlData(reader)) break;
1204
183k
            req.SetState(HTTPRequest::State::NeedsHeaders);
1205
183k
            [[fallthrough]];
1206
1207
183k
        case HTTPRequest::State::NeedsHeaders:
1208
183k
            if (!req.LoadHeaders(reader)) break;
1209
183k
            req.SetState(HTTPRequest::State::NeedsBody);
1210
183k
            [[fallthrough]];
1211
1212
266k
        case HTTPRequest::State::NeedsBody:
1213
266k
            if (!req.LoadBody(reader)) break;
1214
183k
            req.SetState(HTTPRequest::State::Complete);
1215
183k
            [[fallthrough]];
1216
1217
183k
        case HTTPRequest::State::Complete:
1218
183k
            break;
1219
1220
1
        case HTTPRequest::State::Error:
1221
1
            break;
1222
266k
        }
1223
266k
    } catch (...) {
1224
        // Don't try to read any more data for this request
1225
17
        req.SetState(HTTPRequest::State::Error);
1226
        // Clear the memory allocated to this client, caller must disconnect
1227
17
        m_recv_buffer.clear();
1228
17
        throw;
1229
17
    }
1230
1231
    // Remove the bytes read out of the buffer.
1232
266k
    m_recv_buffer.erase(
1233
266k
        m_recv_buffer.begin(),
1234
266k
        m_recv_buffer.begin() + reader.Consumed());
1235
266k
}
1236
1237
bool HTTPRemoteClient::MaybeSendBytesFromBuffer()
1238
184k
{
1239
    // Send as much data from this client's buffer as we can
1240
184k
    LOCK(m_send_mutex);
1241
184k
    if (!m_send_buffer.empty()) {
1242
        // Socket flags (See kernel docs for send(2) and tcp(7) for more details).
1243
        // MSG_NOSIGNAL: If the remote end of the connection is closed,
1244
        //               fail with EPIPE (an error) as opposed to triggering
1245
        //               SIGPIPE which terminates the process.
1246
        // MSG_DONTWAIT: Makes the send operation non-blocking regardless of socket blocking mode.
1247
        // MSG_MORE:     We do not set this flag here because http responses are usually
1248
        //               small and we want the kernel to send them right away. Setting MSG_MORE
1249
        //               would "cork" the socket to prevent sending out partial frames.
1250
184k
        int flags{MSG_NOSIGNAL | MSG_DONTWAIT};
1251
1252
        // Try to send bytes through socket
1253
184k
        ssize_t bytes_sent;
1254
184k
        {
1255
184k
            LOCK(m_sock_mutex);
1256
184k
            bytes_sent = m_sock->Send(m_send_buffer.data(),
1257
184k
                                      m_send_buffer.size(),
1258
184k
                                      flags);
1259
184k
        }
1260
1261
184k
        if (bytes_sent < 0) {
1262
            // Something went wrong
1263
369
            const int err{WSAGetLastError()};
1264
369
            if (!IOErrorIsPermanent(err)) {
1265
                // The error can be safely ignored, try the send again on the next I/O loop.
1266
369
                m_send_ready = true;
1267
369
                m_connection_busy = true;
1268
369
                return true;
1269
369
            } else {
1270
                // Unrecoverable error, log and disconnect client.
1271
0
                LogDebug(
1272
0
                    BCLog::HTTP,
1273
0
                    "Error sending HTTP response data to client %s (id=%llu): %s",
1274
0
                    m_origin,
1275
0
                    m_id,
1276
0
                    NetworkErrorString(err));
1277
0
                m_send_ready = false;
1278
0
                m_disconnect = true;
1279
1280
                // Do not attempt to read from this client.
1281
0
                return false;
1282
0
            }
1283
369
        }
1284
1285
        // Successful send, remove sent bytes from our local buffer.
1286
183k
        Assume(static_cast<size_t>(bytes_sent) <= m_send_buffer.size());
1287
183k
        m_send_buffer.erase(m_send_buffer.begin(),
1288
183k
                            m_send_buffer.begin() + bytes_sent);
1289
1290
183k
        LogDebug(
1291
183k
            BCLog::HTTP,
1292
183k
            "Sent %d bytes to client %s (id=%llu)",
1293
183k
            bytes_sent,
1294
183k
            m_origin,
1295
183k
            m_id);
1296
1297
        // This check is inside the if(!empty) block meaning "there was data but now its gone".
1298
        // We wouldn't want to change the flags if MaybeSendBytesFromBuffer() was called
1299
        // on an already-empty m_send_buffer because the connection might have just been opened.
1300
183k
        if (m_send_buffer.empty()) {
1301
183k
            m_send_ready = false;
1302
183k
            m_connection_busy = false;
1303
1304
            // Our work is done here
1305
183k
            if (!m_keep_alive) {
1306
1.10k
                m_disconnect = true;
1307
                // Do not attempt to read from this client.
1308
1.10k
                return false;
1309
1.10k
            }
1310
183k
        } else {
1311
            // The send buffer isn't flushed yet, try to push more on the next loop.
1312
9
            m_send_ready = true;
1313
9
            m_connection_busy = true;
1314
9
        }
1315
1316
        // Finally, reset idle timeout
1317
182k
        m_idle_since = Now<SteadySeconds>();
1318
182k
    }
1319
1320
182k
    return true;
1321
184k
}
1322
1323
bool InitHTTPServer()
1324
1.16k
{
1325
    // Create HTTPServer
1326
1.16k
    g_http_server = std::make_unique<HTTPServer>(MaybeDispatchRequestToWorker);
1327
1328
1.16k
    if (!g_http_server->InitHTTPAllowList()) {
1329
1
        return false;
1330
1
    }
1331
1332
1.15k
    g_http_server->SetServerTimeout(std::chrono::seconds(gArgs.GetIntArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT)));
1333
1.15k
    g_http_server->SetMaxConnections(std::max(gArgs.GetArg<int>("-rpcmaxconnections", DEFAULT_MAX_HTTP_CONNECTIONS), 1));
1334
1335
    // Bind HTTP server to specified addresses
1336
1.15k
    std::vector<std::pair<std::string, uint16_t>> endpoints{GetBindAddresses()};
1337
1.15k
    bool bind_success{false};
1338
2.31k
    for (const auto& [address_string, port] : endpoints) {
1339
2.31k
        LogInfo("Binding RPC on address %s port %i", address_string, port);
1340
2.31k
        const std::optional<CService> addr{Lookup(address_string, port, false)};
1341
2.31k
        if (addr) {
1342
2.31k
            if (addr->IsBindAny()) {
1343
0
                LogWarning("The RPC server is not safe to expose to untrusted networks such as the public internet");
1344
0
            }
1345
2.31k
            auto result{g_http_server->BindAndStartListening(addr.value())};
1346
2.31k
            if (!result) {
1347
0
                LogWarning("Binding RPC on address %s failed: %s", addr->ToStringAddrPort(), result.error());
1348
2.31k
            } else {
1349
2.31k
                bind_success = true;
1350
2.31k
            }
1351
2.31k
        } else {
1352
0
            LogWarning("Could not bind RPC on address %s port %i: Address lookup failed.", address_string, port);
1353
0
        }
1354
2.31k
    }
1355
1356
1.15k
    if (!bind_success) {
1357
0
        LogError("Unable to bind any endpoint for RPC server");
1358
0
        return false;
1359
0
    }
1360
1361
1.15k
    LogDebug(BCLog::HTTP, "Initialized HTTP server");
1362
1363
1.15k
    g_max_queue_depth = std::max(gArgs.GetArg<int>("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1);
1364
1.15k
    LogDebug(BCLog::HTTP, "set work queue of depth %d\n", g_max_queue_depth);
1365
1366
1.15k
    return true;
1367
1.15k
}
1368
1369
void StartHTTPServer()
1370
1.14k
{
1371
1.14k
    auto rpcThreads{std::max(gArgs.GetArg<int>("-rpcthreads", DEFAULT_HTTP_THREADS), 1)};
1372
1.14k
    LogInfo("Starting HTTP server with %d worker threads", rpcThreads);
1373
1.14k
    g_threadpool_http.Start(rpcThreads);
1374
1.14k
    g_http_server->StartSocketsThreads();
1375
1.14k
}
1376
1377
void InterruptHTTPServer()
1378
1.20k
{
1379
1.20k
    LogDebug(BCLog::HTTP, "Interrupting HTTP server");
1380
1.20k
    if (g_http_server) {
1381
        // Reject all new requests
1382
1.16k
        g_http_server->SetRequestHandler(RejectRequest);
1383
1.16k
    }
1384
1385
    // Interrupt pool after disabling requests
1386
1.20k
    g_threadpool_http.Interrupt();
1387
1.20k
}
1388
1389
void StopHTTPServer()
1390
1.20k
{
1391
1.20k
    LogDebug(BCLog::HTTP, "Stopping HTTP server");
1392
1393
1.20k
    LogDebug(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
1394
1.20k
    g_threadpool_http.Stop();
1395
1396
1.20k
    if (g_http_server) {
1397
        // Must precede DisconnectAllClients(): a connection accepted after
1398
        // GetConnectionsCount() returns 0 would survive into the destructor.
1399
1.16k
        g_http_server->StopAccepting();
1400
        // Disconnect clients as their remaining responses are flushed
1401
1.16k
        g_http_server->DisconnectAllClients();
1402
        // Wait 30 seconds for all disconnections
1403
1.16k
        LogDebug(BCLog::HTTP, "Waiting for HTTP clients to disconnect gracefully");
1404
1.16k
        const auto deadline{NodeClock::now() + 30s};
1405
2.18k
        while (g_http_server->GetConnectionsCount() != 0) {
1406
1.02k
            if (NodeClock::now() > deadline) {
1407
0
                LogWarning("Timeout waiting for HTTP clients to disconnect gracefully, continuing shutdown");
1408
0
                break;
1409
0
            }
1410
1.02k
            std::this_thread::sleep_for(50ms);
1411
1.02k
        }
1412
        // Break HTTPServer I/O loop: stop accepting connections, sending and receiving data
1413
1.16k
        g_http_server->InterruptNet();
1414
        // Wait for HTTPServer I/O thread to exit
1415
1.16k
        g_http_server->JoinSocketsThreads();
1416
        // Force-remove any clients that survived the graceful wait
1417
1.16k
        g_http_server->ClearConnectedClients();
1418
        // Close all listening sockets
1419
1.16k
        g_http_server->StopListening();
1420
1.16k
    }
1421
1.20k
    LogDebug(BCLog::HTTP, "Stopped HTTP server");
1422
1.20k
}