Coverage Report

Created: 2026-08-25 19:22

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