Coverage Report

Created: 2026-06-01 18:35

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