Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-06-01 16:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/zip/work/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
0
{
83
0
    if (!netaddr.IsValid())
84
0
        return false;
85
0
    for(const CSubNet& subnet : rpc_allow_subnets)
86
0
        if (subnet.Match(netaddr))
87
0
            return true;
88
0
    return false;
89
0
}
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")) {
98
0
        const CSubNet subnet{LookupSubNet(strAllow)};
99
0
        if (!subnet.IsValid()) {
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)),
Line
Count
Source
1172
0
#define strprintf tfm::format
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)
109
0
        strAllowed += subnet.ToString() + " ";
110
0
    LogDebug(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed);
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
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
55
{
117
55
    switch (m) {
118
0
    using enum HTTPRequestMethod;
119
1
    case GET: return "GET";
120
0
    case POST: return "POST";
121
1
    case HEAD: return "HEAD";
122
0
    case PUT: return "PUT";
123
53
    case UNKNOWN: return "unknown";
124
55
    } // no default case, so the compiler can warn about missing cases
125
55
    assert(false);
126
0
}
127
128
static void MaybeDispatchRequestToWorker(std::shared_ptr<HTTPRequest> hreq)
129
0
{
130
    // Early address-based allow check
131
0
    if (!ClientAllowed(hreq->GetPeer())) {
132
0
        LogDebug(BCLog::HTTP, "HTTP request from %s rejected: Client network is not allowed RPC access\n",
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
133
0
                 hreq->GetPeer().ToStringAddrPort());
134
0
        hreq->WriteReply(HTTP_FORBIDDEN);
135
0
        return;
136
0
    }
137
138
    // Early reject unknown HTTP methods
139
0
    if (hreq->GetRequestMethod() == HTTPRequestMethod::UNKNOWN) {
140
0
        LogDebug(BCLog::HTTP, "HTTP request from %s rejected: Unknown HTTP request method\n",
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
141
0
                 hreq->GetPeer().ToStringAddrPort());
142
0
        hreq->WriteReply(HTTP_BAD_METHOD);
143
0
        return;
144
0
    }
145
146
    // Find registered handler for prefix
147
0
    std::string strURI = hreq->GetURI();
148
0
    std::string path;
149
0
    LOCK(g_httppathhandlers_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
150
0
    std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
151
0
    std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
152
0
    for (; i != iend; ++i) {
153
0
        bool match = false;
154
0
        if (i->exactMatch)
155
0
            match = (strURI == i->prefix);
156
0
        else
157
0
            match = strURI.starts_with(i->prefix);
158
0
        if (match) {
159
0
            path = strURI.substr(i->prefix.size());
160
0
            break;
161
0
        }
162
0
    }
163
164
    // Dispatch to worker thread
165
0
    if (i != iend) {
166
0
        if (static_cast<int>(g_threadpool_http.WorkQueueSize()) >= g_max_queue_depth) {
167
0
            LogWarning("Request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
168
0
            hreq->WriteReply(HTTP_SERVICE_UNAVAILABLE, "Work queue depth exceeded");
169
0
            return;
170
0
        }
171
172
0
        auto item = [req = hreq, in_path = std::move(path), fn = i->handler]() {
173
0
            std::string err_msg;
174
0
            try {
175
0
                fn(req.get(), in_path);
176
0
                return;
177
0
            } catch (const std::exception& e) {
178
0
                LogWarning("Unexpected error while processing request for '%s'. Error msg: '%s'", req->GetURI(), e.what());
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
179
0
                err_msg = e.what();
180
0
            } catch (...) {
181
0
                LogWarning("Unknown error while processing request for '%s'", req->GetURI());
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
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
0
        if (auto res = g_threadpool_http.Submit(std::move(item)); !res.has_value()) {
191
0
            Assume(hreq.use_count() == 1); // ensure request will be deleted
Line
Count
Source
128
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
192
            // Both SubmitError::Inactive and SubmitError::Interrupted mean shutdown
193
0
            LogWarning("HTTP request rejected during server shutdown: '%s'", SubmitErrorString(res.error()));
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
194
0
            hreq->WriteReply(HTTP_SERVICE_UNAVAILABLE, "Request rejected during server shutdown");
195
0
            return;
196
0
        }
197
0
    } else {
198
0
        hreq->WriteReply(HTTP_NOT_FOUND);
199
0
    }
200
0
}
201
202
static void RejectRequest(std::unique_ptr<http_bitcoin::HTTPRequest> hreq)
203
0
{
204
0
    LogDebug(BCLog::HTTP, "Rejecting request while shutting down");
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
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
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()) {
222
0
            LogWarning("Option -rpcallowip was specified without -rpcbind; this doesn't usually make sense");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
223
0
        }
224
0
        if (!gArgs.GetArgs("-rpcbind").empty()) {
225
0
            LogWarning("Option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
226
0
        }
227
0
    } else { // Specific bind addresses
228
0
        for (const std::string& strRPCBind : gArgs.GetArgs("-rpcbind")) {
229
0
            uint16_t port{http_port};
230
0
            std::string host;
231
0
            if (!SplitHostPort(strRPCBind, port, host)) {
232
0
                LogError("%s\n", InvalidPortErrMsg("-rpcbind", strRPCBind).original);
Line
Count
Source
127
0
#define LogError(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Error, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
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);
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
244
0
    LOCK(g_httppathhandlers_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
245
0
    pathHandlers.emplace_back(prefix, exactMatch, handler);
246
0
}
247
248
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
249
0
{
250
0
    LOCK(g_httppathhandlers_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
251
0
    std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
252
0
    std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
253
0
    for (; i != iend; ++i)
254
0
        if (i->prefix == prefix && i->exactMatch == exactMatch)
255
0
            break;
256
0
    if (i != iend)
257
0
    {
258
0
        LogDebug(BCLog::HTTP, "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
259
0
        pathHandlers.erase(i);
260
0
    }
261
0
}
262
263
namespace http_bitcoin {
264
using util::Split;
265
266
std::optional<std::string> HTTPHeaders::FindFirst(const std::string_view key) const
267
220
{
268
7.40k
    for (const auto& item : m_headers) {
269
7.40k
        if (CaseInsensitiveEqual(key, item.first)) {
270
22
            return item.second;
271
22
        }
272
7.40k
    }
273
198
    return std::nullopt;
274
220
}
275
276
std::vector<std::string_view> HTTPHeaders::FindAll(const std::string_view key) const
277
55
{
278
55
    std::vector<std::string_view> ret;
279
1.95k
    for (const auto& item : m_headers) {
280
1.95k
        if (CaseInsensitiveEqual(key, item.first)) {
281
0
            ret.push_back(item.second);
282
0
        }
283
1.95k
    }
284
55
    return ret;
285
55
}
286
287
void HTTPHeaders::Write(std::string&& key, std::string&& value)
288
2.50k
{
289
2.50k
    m_headers.emplace_back(std::move(key), std::move(value));
290
2.50k
}
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
86
{
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
2.53k
    while (auto maybe_line = reader.ReadLine()) {
305
2.52k
        if (reader.Consumed() > MAX_HEADERS_SIZE) 
throw std::runtime_error("HTTP headers exceed size limit")0
;
306
307
2.52k
        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
2.52k
        if (line.empty()) 
return true55
;
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
2.47k
        if (line.find_first_of("\r\n\0", 0, 3) != std::string_view::npos) 
throw std::runtime_error("Header contains invalid character")3
;
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
2.46k
        const size_t pos{line.find(':')};
324
2.46k
        if (pos == std::string_view::npos) 
throw std::runtime_error("HTTP header missing colon (:)")6
;
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
2.46k
        std::string_view key = line.substr(0, pos);
329
2.46k
        if (key.find_first_of(" \t") != std::string_view::npos) 
throw std::runtime_error("Invalid header field-name contains whitespace")1
;
330
        // Whitespace is optional in the value and can be trimmed
331
2.46k
        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
2.46k
        if (key.empty()) 
throw std::runtime_error("Empty HTTP header name")13
;
337
338
2.44k
        Write(std::string(key), std::move(value));
339
2.44k
    }
340
341
8
    return false;
342
86
}
343
344
std::string HTTPHeaders::Stringify() const
345
0
{
346
0
    std::string out;
347
0
    for (const auto& [key, value] : m_headers) {
348
0
        out += key + ": " + value + "\r\n";
349
0
    }
350
351
    // Headers are terminated by an empty line
352
0
    out += "\r\n";
353
354
0
    return out;
355
0
}
356
357
std::string HTTPResponse::StringifyHeaders() const
358
0
{
359
0
    return strprintf("HTTP/%d.%d %d %s\r\n%s", m_version_major, m_version_minor, m_status, m_reason, m_headers.Stringify());
Line
Count
Source
1172
0
#define strprintf tfm::format
360
0
}
361
362
bool HTTPRequest::LoadControlData(LineReader& reader)
363
182
{
364
182
    auto maybe_line = reader.ReadLine();
365
182
    if (!maybe_line) 
return false29
;
366
153
    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
153
    if (request_line.length() < MIN_REQUEST_LINE_LENGTH) 
throw std::runtime_error("HTTP request line too short")4
;
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
149
    if (request_line.find('\0') != std::string_view::npos) 
throw std::runtime_error("Invalid request line contains NUL")5
;
375
376
144
    const std::vector<std::string_view> parts{Split<std::string_view>(request_line, " ")};
377
144
    if (parts.size() != 3) 
throw std::runtime_error("HTTP request line malformed")24
;
378
379
120
    if (parts[0] == "GET") {
380
3
        m_method = HTTPRequestMethod::GET;
381
117
    } else if (parts[0] == "POST") {
382
1
        m_method = HTTPRequestMethod::POST;
383
116
    } else if (parts[0] == "HEAD") {
384
1
        m_method = HTTPRequestMethod::HEAD;
385
115
    } else if (parts[0] == "PUT") {
386
1
        m_method = HTTPRequestMethod::PUT;
387
114
    } else {
388
114
        m_method = HTTPRequestMethod::UNKNOWN;
389
114
    }
390
391
120
    m_target = parts[1];
392
393
120
    if (parts[2].rfind("HTTP/") != 0) 
throw std::runtime_error("HTTP request line malformed")5
;
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
115
    const std::vector<std::string_view> version_parts{Split<std::string_view>(parts[2].substr(5), ".")};
398
115
    if (version_parts.size() != 2) 
throw std::runtime_error("HTTP request line malformed")5
;
399
110
    if (version_parts[0].size() != 1 || 
version_parts[1].size() != 1109
)
throw std::runtime_error("HTTP bad version")2
;
400
108
    auto major = ToIntegral<uint8_t>(version_parts[0]);
401
108
    auto minor = ToIntegral<uint8_t>(version_parts[1]);
402
108
    if (!major || 
!minor106
||
major != 1106
||
minor > 986
)
throw std::runtime_error("HTTP bad version")22
;
403
86
    m_version_major = major.value();
404
86
    m_version_minor = minor.value();
405
406
86
    return true;
407
108
}
408
409
bool HTTPRequest::LoadHeaders(LineReader& reader)
410
86
{
411
86
    return m_headers.Read(reader);
412
86
}
413
414
bool HTTPRequest::LoadBody(LineReader& reader)
415
55
{
416
    // https://httpwg.org/specs/rfc9112.html#message.body
417
55
    auto transfer_encoding_header = m_headers.FindFirst("Transfer-Encoding");
418
55
    if (transfer_encoding_header && 
ToLower(transfer_encoding_header.value()) == "chunked"0
) {
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
0
        while (reader.Remaining() > 0) {
423
0
            auto maybe_chunk_size = reader.ReadLine();
424
0
            if (!maybe_chunk_size) return false;
425
426
            // Allow (but ignore) Chunk Extensions
427
            // See https://www.rfc-editor.org/rfc/rfc9112.html#name-chunk-extensions
428
0
            std::string_view chunk_size_noext{maybe_chunk_size.value()};
429
0
            const auto semicolon_pos = chunk_size_noext.find(';');
430
0
            if (semicolon_pos != chunk_size_noext.npos) {
431
0
                chunk_size_noext.remove_suffix(chunk_size_noext.size() - semicolon_pos);
432
0
            }
433
434
0
            const auto chunk_size{ToIntegral<uint64_t>(util::TrimStringView(chunk_size_noext), /*base=*/16)};
435
0
            if (!chunk_size) throw std::runtime_error("Cannot parse chunk length value");
436
437
0
            if ((m_body.size() > MAX_BODY_SIZE) ||
438
0
                (*chunk_size > MAX_BODY_SIZE - m_body.size()))
439
0
                throw ContentTooLargeError("Chunk will exceed max body size");
440
441
            // Last chunk has size 0
442
0
            if (*chunk_size == 0) {
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
0
                const size_t trailer_start{reader.Consumed()};
448
0
                while (true) {
449
0
                    auto maybe_trailer = reader.ReadLine();
450
0
                    if (reader.Consumed() - trailer_start > MAX_HEADERS_SIZE) {
451
0
                        throw std::runtime_error("HTTP chunked trailer exceeds size limit");
452
0
                    }
453
0
                    if (!maybe_trailer) return false;
454
0
                    if (maybe_trailer->empty()) break;
455
0
                }
456
                // Complete request has been parsed, reader is now pointing
457
                // to beginning of next request or end of the buffer.
458
0
                return true;
459
0
            }
460
461
            // We are still expecting more data for this chunk
462
0
            if (reader.Remaining() < *chunk_size) {
463
0
                return false;
464
0
            }
465
466
            // Pack chunk onto body
467
0
            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
0
            auto crlf = reader.ReadLine();
473
0
            if (!crlf) {
474
                // CRLF not found before end of buffer: it has not been received by our socket yet.
475
0
                return false;
476
0
            }
477
            // CRLF was found but there was unexpected data after the chunk_sized chunk
478
0
            if (!crlf.value().empty()) throw std::runtime_error("Improperly terminated chunk");
479
0
        }
480
481
        // We read all the chunks but never got the last chunk, wait for client to send more
482
0
        return false;
483
55
    } else {
484
        // No Content-length or Transfer-Encoding header means no body, see libevent evhttp_get_body()
485
55
        auto content_length_values{m_headers.FindAll("Content-Length")};
486
55
        if (content_length_values.empty()) return true;
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
0
        const auto& first_content_length_value{content_length_values[0]};
491
0
        for (size_t i = 1; i < content_length_values.size(); ++i) {
492
0
            if (content_length_values[i] != first_content_length_value) throw std::runtime_error("Differing Content-Length values");
493
0
        }
494
495
0
        const auto content_length{ToIntegral<uint64_t>(first_content_length_value)};
496
0
        if (!content_length) throw std::runtime_error("Cannot parse Content-Length value");
497
498
0
        if (*content_length > MAX_BODY_SIZE) throw ContentTooLargeError("Max body size exceeded");
499
500
        // Not enough data in buffer for expected body
501
0
        if (reader.Remaining() < *content_length) return false;
502
503
0
        m_body = reader.ReadLength(*content_length);
504
505
0
        return true;
506
0
    }
507
55
}
508
509
void HTTPRequest::WriteReply(HTTPStatusCode status, std::span<const std::byte> reply_body)
510
0
{
511
0
    HTTPResponse res;
512
513
    // Some response headers are determined in advance and stored in the request
514
0
    res.m_headers = std::move(m_response_headers);
515
516
    // Response version matches request version
517
0
    res.m_version_major = m_version_major;
518
0
    res.m_version_minor = m_version_minor;
519
520
    // Add response code and look up reason string
521
0
    res.m_status = status;
522
0
    res.m_reason = HTTPStatusReasonString(status);
523
524
    // See libevent evhttp_response_needs_body()
525
    // Response headers are different if no body is needed
526
0
    bool needs_body{status != HTTP_NO_CONTENT && (status < 100 || status >= 200)};
527
0
    bool needs_content_length{false};
528
529
    // See libevent evhttp_make_header_response()
530
    // Expected response headers depend on protocol version
531
0
    if (m_version_major == 1) {
532
        // HTTP/1.0
533
0
        if (m_version_minor == 0) {
534
0
            auto connection_header{m_headers.FindFirst("Connection")};
535
0
            if (connection_header && ToLower(connection_header.value()) == "keep-alive") {
536
0
                res.m_headers.Write("Connection", "keep-alive");
537
0
                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
0
                if (needs_body) needs_content_length = true;
541
0
            }
542
0
        }
543
544
        // HTTP/1.1
545
0
        if (m_version_minor >= 1) {
546
0
            const int64_t now_seconds{TicksSinceEpoch<std::chrono::seconds>(NodeClock::now())};
547
0
            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
0
            if (needs_body) needs_content_length = true;
551
552
            // Default for HTTP/1.1
553
0
            res.m_keep_alive = true;
554
0
        }
555
0
    }
556
557
0
    if (needs_content_length) {
558
0
        res.m_headers.Write("Content-Length", util::ToString(reply_body.size()));
559
0
    }
560
561
0
    if (needs_body && !res.m_headers.FindFirst("Content-Type")) {
562
        // Default type from libevent evhttp_new_object()
563
0
        res.m_headers.Write("Content-Type", "text/html; charset=ISO-8859-1");
564
0
    }
565
566
0
    auto connection_header{m_headers.FindFirst("Connection")};
567
0
    if (connection_header && ToLower(connection_header.value()) == "close") {
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
0
    m_client->m_keep_alive = res.m_keep_alive;
576
577
    // Serialize the response headers
578
0
    const std::string headers{res.StringifyHeaders()};
579
0
    const auto headers_bytes{std::as_bytes(std::span{headers})};
580
581
0
    bool send_buffer_was_empty{false};
582
    // Fill the send buffer with the complete serialized response headers + body
583
0
    {
584
0
        LOCK(m_client->m_send_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
585
0
        send_buffer_was_empty = m_client->m_send_buffer.empty();
586
0
        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
0
        m_client->m_send_buffer.insert(m_client->m_send_buffer.end(), reply_body.begin(), reply_body.end());
592
0
    }
593
594
0
    LogDebug(
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
595
0
        BCLog::HTTP,
596
0
        "HTTPResponse (status code: %d size: %lld) added to send buffer for client %s (id=%lld)",
597
0
        status,
598
0
        headers_bytes.size() + reply_body.size(),
599
0
        m_client->m_origin,
600
0
        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
0
    if (send_buffer_was_empty) {
607
0
        m_client->MaybeSendBytesFromBuffer();
608
0
    } 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
0
    m_client->m_req_busy = false;
616
0
}
617
618
CService HTTPRequest::GetPeer() const
619
0
{
620
0
    return m_client->m_addr;
621
0
}
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;
638
0
    size_t end = decoded_uri.find('#', start);
639
0
    if (end == std::string::npos) {
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) {
646
0
        size_t delim = param.find('=');
647
0
        if (key == param.substr(0, delim)) {
648
0
            if (delim == std::string::npos) {
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
165
{
660
165
    std::optional<std::string> found{m_headers.FindFirst(hdr)};
661
165
    return std::pair{found.has_value(), std::move(found).value_or("")};
662
165
}
663
664
void HTTPRequest::WriteHeader(std::string&& hdr, std::string&& value)
665
55
{
666
55
    m_response_headers.Write(std::move(hdr), std::move(value));
667
55
}
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)) {
676
0
        return util::Unexpected{strprintf("Bind address family for %s not supported", to.ToStringAddrPort())};
Line
Count
Source
1172
0
#define strprintf tfm::format
677
0
    }
678
679
0
    std::unique_ptr<Sock> sock{CreateSock(to.GetSAFamily(), SOCK_STREAM, IPPROTO_TCP)};
680
0
    if (!sock) {
681
0
        return util::Unexpected{strprintf("Cannot create %s listen socket: %s",
Line
Count
Source
1172
0
#define strprintf tfm::format
682
0
                                          to.ToStringAddrPort(),
683
0
                                          NetworkErrorString(WSAGetLastError()))};
Line
Count
Source
59
0
#define WSAGetLastError()   errno
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) {
Line
Count
Source
68
0
#define SOCKET_ERROR        -1
689
0
        LogDebug(BCLog::HTTP,
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
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()) {
698
0
#ifdef IPV6_V6ONLY
699
0
        if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, &SOCKET_OPTION_TRUE, sizeof(SOCKET_OPTION_TRUE)) == SOCKET_ERROR) {
Line
Count
Source
68
0
#define SOCKET_ERROR        -1
700
0
            LogDebug(BCLog::HTTP,
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
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) {
Line
Count
Source
68
0
#define SOCKET_ERROR        -1
721
0
        const int err{WSAGetLastError()};
Line
Count
Source
59
0
#define WSAGetLastError()   errno
722
0
        if (err == WSAEADDRINUSE) {
Line
Count
Source
66
0
#define WSAEADDRINUSE       EADDRINUSE
723
0
            return util::Unexpected{strprintf("Unable to bind to %s on this computer. %s is probably already running.",
Line
Count
Source
1172
0
#define strprintf tfm::format
724
0
                                              to.ToStringAddrPort(),
725
0
                                              CLIENT_NAME)};
Line
Count
Source
98
0
#define CLIENT_NAME "Bitcoin Core"
726
0
        } else {
727
0
            return util::Unexpected{strprintf("Unable to bind to %s on this computer (bind returned error %s)",
Line
Count
Source
1172
0
#define strprintf tfm::format
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) {
Line
Count
Source
68
0
#define SOCKET_ERROR        -1
735
0
        return util::Unexpected{strprintf("Cannot listen on %s: %s",
Line
Count
Source
1172
0
#define strprintf tfm::format
736
0
                                          to.ToStringAddrPort(),
737
0
                                          NetworkErrorString(WSAGetLastError()))};
Line
Count
Source
59
0
#define WSAGetLastError()   errno
738
0
    }
739
740
0
    m_listen.emplace_back(std::move(sock));
741
742
0
    return {};
743
0
}
744
745
void HTTPServer::StopListening()
746
0
{
747
0
    m_listen.clear();
748
0
}
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
0
{
759
0
    if (m_thread_socket_handler.joinable()) {
760
0
        m_thread_socket_handler.join();
761
0
    }
762
0
}
763
764
std::unique_ptr<Sock> HTTPServer::AcceptConnection(const Sock& listen_sock, CService& addr)
765
0
{
766
    // Make sure we only operate on our own listening sockets
767
0
    Assume(std::ranges::any_of(m_listen, [&](const auto& sock) { return sock.get() == &listen_sock; }));
Line
Count
Source
128
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
768
769
0
    sockaddr_storage storage;
770
0
    socklen_t len{sizeof(storage)};
771
0
    auto sa = reinterpret_cast<sockaddr*>(&storage);
772
773
0
    auto sock{listen_sock.Accept(sa, &len)};
774
775
0
    if (!sock) {
776
0
        const int err{WSAGetLastError()};
Line
Count
Source
59
0
#define WSAGetLastError()   errno
777
0
        if (err != WSAEWOULDBLOCK) {
Line
Count
Source
61
0
#define WSAEWOULDBLOCK      EWOULDBLOCK
778
0
            LogDebug(BCLog::HTTP,
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
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
0
    if (!addr.SetSockAddr(sa, len)) {
789
0
        LogDebug(BCLog::HTTP,
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
790
0
                 "Unknown socket family");
791
0
    }
792
793
0
    return sock;
794
0
}
795
796
HTTPServer::Id HTTPServer::GetNewId()
797
0
{
798
0
    return m_next_id.fetch_add(1, std::memory_order_relaxed);
799
0
}
800
801
void HTTPServer::NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& addr)
802
0
{
803
0
    if (!sock->IsSelectable()) {
804
0
        LogDebug(BCLog::HTTP,
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
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
0
    if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &SOCKET_OPTION_TRUE, sizeof(SOCKET_OPTION_TRUE)) == SOCKET_ERROR) {
Line
Count
Source
68
0
#define SOCKET_ERROR        -1
813
0
        LogDebug(BCLog::HTTP, "connection from %s: unable to set TCP_NODELAY, continuing anyway",
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
814
0
                 addr.ToStringAddrPort());
815
0
    }
816
817
0
    const Id id{GetNewId()};
818
819
0
    m_connected.push_back(std::make_shared<HTTPRemoteClient>(id, addr, std::move(sock)));
820
    // Report back to the main thread
821
0
    m_connected_size.fetch_add(1, std::memory_order_relaxed);
822
823
0
    LogDebug(BCLog::HTTP,
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
824
0
             "HTTP Connection accepted from %s (id=%d)",
825
0
             addr.ToStringAddrPort(), id);
826
0
}
827
828
void HTTPServer::SocketHandlerConnected(const IOReadiness& io_readiness) const
829
0
{
830
0
    for (const auto& [sock, events] : io_readiness.events_per_sock) {
831
0
        if (m_interrupt_net) {
832
0
            return;
833
0
        }
834
835
0
        auto it{io_readiness.httpclients_per_sock.find(sock)};
836
0
        if (it == io_readiness.httpclients_per_sock.end()) {
837
0
            continue;
838
0
        }
839
0
        const std::shared_ptr<HTTPRemoteClient>& client{it->second};
840
841
0
        bool send_ready = events.occurred & Sock::SEND;
842
0
        bool recv_ready = events.occurred & Sock::RECV;
843
0
        bool err_ready = events.occurred & Sock::ERR;
844
845
0
        if (send_ready) {
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()) {
850
0
                recv_ready = false;
851
0
            }
852
0
        }
853
854
0
        if (recv_ready || err_ready) {
855
0
            std::byte buf[0x10000]; // typical socket buffer is 8K-64K
856
857
0
            const ssize_t nrecv{WITH_LOCK(
Line
Count
Source
299
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
858
0
                client->m_sock_mutex,
859
0
                return client->m_sock->Recv(buf, sizeof(buf), MSG_DONTWAIT);)};
860
861
0
            if (nrecv < 0) {
862
0
                const int err = WSAGetLastError();
Line
Count
Source
59
0
#define WSAGetLastError()   errno
863
0
                if (IOErrorIsPermanent(err)) {
864
0
                    LogDebug(
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
865
0
                        BCLog::HTTP,
866
0
                        "Permanent read error from %s (id=%lld): %s",
867
0
                        client->m_origin,
868
0
                        client->m_id,
869
0
                        NetworkErrorString(err));
870
0
                    client->m_disconnect = true;
871
0
                }
872
0
            } else if (nrecv == 0) {
873
0
                LogDebug(
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
874
0
                    BCLog::HTTP,
875
0
                    "Received EOF from %s (id=%lld)",
876
0
                    client->m_origin,
877
0
                    client->m_id);
878
0
                client->m_disconnect = true;
879
0
            } else {
880
                // Reset idle timeout
881
0
                client->m_idle_since = Now<SteadySeconds>();
882
883
                // Prevent disconnect until all requests are completely handled.
884
0
                client->m_connection_busy = true;
885
886
                // Copy data from socket buffer to client receive buffer
887
0
                client->m_recv_buffer.insert(
888
0
                    client->m_recv_buffer.end(),
889
0
                    buf,
890
0
                    buf + nrecv);
891
0
            }
892
0
        }
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
0
        MaybeDispatchRequestsFromClient(client);
898
0
    }
899
0
}
900
901
void HTTPServer::SocketHandlerListening(const Sock::EventsPerSock& events_per_sock)
902
0
{
903
0
    if (m_stop_accepting) return;
904
0
    for (const auto& sock : m_listen) {
905
0
        if (m_interrupt_net) {
906
0
            return;
907
0
        }
908
0
        const auto it = events_per_sock.find(sock);
909
0
        if (it != events_per_sock.end() && it->second.occurred & Sock::RECV) {
910
0
            CService addr_accepted;
911
912
0
            auto sock_accepted{AcceptConnection(*sock, addr_accepted)};
913
914
0
            if (sock_accepted) {
915
0
                NewSockAccepted(std::move(sock_accepted), addr_accepted);
916
0
            }
917
0
        }
918
0
    }
919
0
}
920
921
HTTPServer::IOReadiness HTTPServer::GenerateWaitSockets() const
922
0
{
923
0
    IOReadiness io_readiness;
924
925
0
    for (const auto& sock : m_listen) {
926
0
        io_readiness.events_per_sock.emplace(sock, Sock::Events{Sock::RECV});
927
0
    }
928
929
0
    for (const auto& http_client : m_connected) {
930
        // Safely copy the shared pointer to the socket
931
0
        std::shared_ptr<Sock> sock{WITH_LOCK(http_client->m_sock_mutex, return http_client->m_sock;)};
Line
Count
Source
299
0
#define WITH_LOCK(cs, code) (MaybeCheckNotHeld(cs), [&]() -> decltype(auto) { LOCK(cs); code; }())
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
0
        Sock::Event event = (http_client->m_send_ready ? Sock::SEND : Sock::RECV);
936
0
        io_readiness.events_per_sock.emplace(sock, Sock::Events{event});
937
0
        io_readiness.httpclients_per_sock.emplace(sock, http_client);
938
0
    }
939
940
0
    return io_readiness;
941
0
}
942
943
/// \anchor http
944
void HTTPServer::ThreadSocketHandler()
945
0
{
946
0
    while (!m_interrupt_net) {
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
0
        auto io_readiness{GenerateWaitSockets()};
952
0
        if (io_readiness.events_per_sock.empty() ||
953
            // WaitMany() may as well be a static method, the context of the first Sock in the vector is not relevant.
954
0
            !io_readiness.events_per_sock.begin()->first->WaitMany(SELECT_TIMEOUT,
955
0
                                                                   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
0
        SocketHandlerConnected(io_readiness);
961
962
        // Accept new connections from listening sockets.
963
0
        SocketHandlerListening(io_readiness.events_per_sock);
964
965
        // Disconnect any clients that have been flagged.
966
0
        DisconnectClients();
967
0
    }
968
0
}
969
970
void HTTPServer::MaybeDispatchRequestsFromClient(const std::shared_ptr<HTTPRemoteClient>& client) const
971
0
{
972
    // Try reading (potentially multiple) HTTP requests from the buffer
973
0
    while (!client->m_recv_buffer.empty()) {
974
        // Create a new request object and try to fill it with data from the receive buffer
975
0
        auto req = std::make_unique<HTTPRequest>(client);
976
0
        try {
977
            // Stop reading if we need more data from the client to parse a complete request
978
0
            if (!client->ReadRequest(req)) break;
979
0
        } catch (const ContentTooLargeError& e) {
980
0
            LogDebug(
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
981
0
                BCLog::HTTP,
982
0
                "HTTP request body too large from client %s (id=%lld): %s",
983
0
                client->m_origin,
984
0
                client->m_id,
985
0
                e.what());
986
987
0
            req->WriteReply(HTTP_CONTENT_TOO_LARGE);
988
0
            client->m_disconnect = true;
989
0
            break;
990
0
        } catch (const std::runtime_error& e) {
991
0
            LogDebug(
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
992
0
                BCLog::HTTP,
993
0
                "Error reading HTTP request from client %s (id=%lld): %s",
994
0
                client->m_origin,
995
0
                client->m_id,
996
0
                e.what());
997
998
            // We failed to read a complete request from the buffer
999
0
            req->WriteReply(HTTP_BAD_REQUEST);
1000
0
            client->m_disconnect = true;
1001
0
            break;
1002
0
        }
1003
1004
        // We read a complete request from the buffer into the queue
1005
0
        LogDebug(
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1006
0
            BCLog::HTTP,
1007
0
            "Received a %s request for %s from %s (id=%lld)",
1008
0
            RequestMethodString(req->m_method),
1009
0
            req->m_target,
1010
0
            client->m_origin,
1011
0
            client->m_id);
1012
1013
        // add request to client queue
1014
0
        client->m_req_queue.push_back(std::move(req));
1015
0
    }
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
0
    if (client->m_req_busy) return;
1021
1022
    // Otherwise, if there is a pending request in the queue, handle it.
1023
0
    if (!client->m_req_queue.empty()) {
1024
0
        LOCK(m_request_dispatcher_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1025
0
        client->m_req_busy = true;
1026
0
        m_request_dispatcher(std::move(client->m_req_queue.front()));
1027
0
        client->m_req_queue.pop_front();
1028
0
    }
1029
0
}
1030
1031
void HTTPServer::DisconnectClients()
1032
0
{
1033
0
    const auto now{Now<SteadySeconds>()};
1034
0
    size_t erased = std::erase_if(m_connected,
1035
0
                                  [&](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
0
                                        bool is_idle{false};
1042
0
                                        if (m_rpcservertimeout.count() > 0) {
1043
0
                                            is_idle = now - client->m_idle_since.load() > m_rpcservertimeout && !client->m_req_busy;
1044
0
                                        }
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
0
                                        if (client->m_disconnect || is_idle) {
1049
0
                                            if (is_idle) {
1050
0
                                                LogDebug(BCLog::HTTP,
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1051
0
                                                         "HTTP client idle timeout %s (id=%d)",
1052
0
                                                         client->m_origin,
1053
0
                                                         client->m_id);
1054
0
                                            }
1055
0
                                        } else {
1056
                                            // Disconnect this client because the server is shutting
1057
                                            // down and we need to disconnect all clients...
1058
0
                                            if (m_disconnect_all_clients) {
1059
                                                // ...unless we still have data for this client.
1060
0
                                                if (client->m_connection_busy) {
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
0
                                            } else {
1070
                                                // No reason to disconnect.
1071
0
                                                return false;
1072
0
                                            }
1073
0
                                        }
1074
                                        // No reason NOT to disconnect, log and remove.
1075
0
                                        LogDebug(BCLog::HTTP,
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1076
0
                                                 "Disconnecting HTTP client %s (id=%d)",
1077
0
                                                 client->m_origin,
1078
0
                                                 client->m_id);
1079
0
                                        return true;
1080
0
                                    });
1081
0
    if (erased > 0) {
1082
        // Report back to the main thread
1083
0
        m_connected_size.fetch_sub(erased, std::memory_order_relaxed);
1084
0
    }
1085
0
}
1086
1087
void HTTPServer::ClearConnectedClients()
1088
0
{
1089
0
    Assume(!m_thread_socket_handler.joinable()); // must be called after JoinSocketsThreads()
Line
Count
Source
128
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
1090
0
    if (m_connected.empty()) return;
1091
0
    LogWarning("Force-disconnecting %d HTTP client(s) that did not disconnect gracefully", m_connected.size());
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
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
0
{
1098
0
    LineReader reader(m_recv_buffer, MAX_HEADERS_SIZE);
1099
1100
0
    if (!req->LoadControlData(reader)) return false;
1101
0
    if (!req->LoadHeaders(reader)) return false;
1102
0
    if (!req->LoadBody(reader)) return false;
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
0
    m_recv_buffer.erase(
1108
0
        m_recv_buffer.begin(),
1109
0
        m_recv_buffer.begin() + reader.Consumed());
1110
1111
0
    return true;
1112
0
}
1113
1114
bool HTTPRemoteClient::MaybeSendBytesFromBuffer()
1115
0
{
1116
    // Send as much data from this client's buffer as we can
1117
0
    LOCK(m_send_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1118
0
    if (!m_send_buffer.empty()) {
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
0
        int flags{MSG_NOSIGNAL | MSG_DONTWAIT};
1128
1129
        // Try to send bytes through socket
1130
0
        ssize_t bytes_sent;
1131
0
        {
1132
0
            LOCK(m_sock_mutex);
Line
Count
Source
268
0
#define LOCK(cs) UniqueLock UNIQUE_NAME(criticalblock)(MaybeCheckNotHeld(cs), #cs, __FILE__, __LINE__)
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
1133
0
            bytes_sent = m_sock->Send(m_send_buffer.data(),
1134
0
                                      m_send_buffer.size(),
1135
0
                                      flags);
1136
0
        }
1137
1138
0
        if (bytes_sent < 0) {
1139
            // Something went wrong
1140
0
            const int err{WSAGetLastError()};
Line
Count
Source
59
0
#define WSAGetLastError()   errno
1141
0
            if (!IOErrorIsPermanent(err)) {
1142
                // The error can be safely ignored, try the send again on the next I/O loop.
1143
0
                return true;
1144
0
            } else {
1145
                // Unrecoverable error, log and disconnect client.
1146
0
                LogDebug(
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1147
0
                    BCLog::HTTP,
1148
0
                    "Error sending HTTP response data to client %s (id=%lld): %s",
1149
0
                    m_origin,
1150
0
                    m_id,
1151
0
                    NetworkErrorString(err));
1152
0
                m_send_ready = false;
1153
0
                m_disconnect = true;
1154
1155
                // Do not attempt to read from this client.
1156
0
                return false;
1157
0
            }
1158
0
        }
1159
1160
        // Successful send, remove sent bytes from our local buffer.
1161
0
        Assume(static_cast<size_t>(bytes_sent) <= m_send_buffer.size());
Line
Count
Source
128
0
#define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val)
1162
0
        m_send_buffer.erase(m_send_buffer.begin(),
1163
0
                            m_send_buffer.begin() + bytes_sent);
1164
1165
0
        LogDebug(
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1166
0
            BCLog::HTTP,
1167
0
            "Sent %d bytes to client %s (id=%lld)",
1168
0
            bytes_sent,
1169
0
            m_origin,
1170
0
            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
0
        if (m_send_buffer.empty()) {
1176
0
            m_send_ready = false;
1177
0
            m_connection_busy = false;
1178
1179
            // Our work is done here
1180
0
            if (!m_keep_alive) {
1181
0
                m_disconnect = true;
1182
                // Do not attempt to read from this client.
1183
0
                return false;
1184
0
            }
1185
0
        } 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
0
        m_idle_since = Now<SteadySeconds>();
1193
0
    }
1194
1195
0
    return true;
1196
0
}
1197
1198
bool InitHTTPServer()
1199
0
{
1200
0
    if (!InitHTTPAllowList()) {
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) {
1213
0
        LogInfo("Binding RPC on address %s port %i", address_string, port);
Line
Count
Source
125
0
#define LogInfo(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Info, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
1214
0
        const std::optional<CService> addr{Lookup(address_string, port, false)};
1215
0
        if (addr) {
1216
0
            if (addr->IsBindAny()) {
1217
0
                LogWarning("The RPC server is not safe to expose to untrusted networks such as the public internet");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
1218
0
            }
1219
0
            auto result{g_http_server->BindAndStartListening(addr.value())};
1220
0
            if (!result) {
1221
0
                LogWarning("Binding RPC on address %s failed: %s", addr->ToStringAddrPort(), result.error());
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
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);
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
1227
0
        }
1228
0
    }
1229
1230
0
    if (!bind_success) {
1231
0
        LogError("Unable to bind any endpoint for RPC server");
Line
Count
Source
127
0
#define LogError(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Error, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
1232
0
        return false;
1233
0
    }
1234
1235
0
    LogDebug(BCLog::HTTP, "Initialized HTTP server");
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
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);
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
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);
Line
Count
Source
125
0
#define LogInfo(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Info, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
1247
0
    g_threadpool_http.Start(rpcThreads);
1248
0
    g_http_server->StartSocketsThreads();
1249
0
}
1250
1251
void InterruptHTTPServer()
1252
0
{
1253
0
    LogDebug(BCLog::HTTP, "Interrupting HTTP server");
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1254
0
    if (g_http_server) {
1255
        // Reject all new requests
1256
0
        g_http_server->SetRequestHandler(RejectRequest);
1257
0
    }
1258
1259
    // Interrupt pool after disabling requests
1260
0
    g_threadpool_http.Interrupt();
1261
0
}
1262
1263
void StopHTTPServer()
1264
0
{
1265
0
    LogDebug(BCLog::HTTP, "Stopping HTTP server");
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1266
1267
0
    LogDebug(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1268
0
    g_threadpool_http.Stop();
1269
1270
0
    if (g_http_server) {
1271
        // Must precede DisconnectAllClients(): a connection accepted after
1272
        // GetConnectionsCount() returns 0 would survive into the destructor.
1273
0
        g_http_server->StopAccepting();
1274
        // Disconnect clients as their remaining responses are flushed
1275
0
        g_http_server->DisconnectAllClients();
1276
        // Wait 30 seconds for all disconnections
1277
0
        LogDebug(BCLog::HTTP, "Waiting for HTTP clients to disconnect gracefully");
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1278
0
        const auto deadline{NodeClock::now() + 30s};
1279
0
        while (g_http_server->GetConnectionsCount() != 0) {
1280
0
            if (NodeClock::now() > deadline) {
1281
0
                LogWarning("Timeout waiting for HTTP clients to disconnect gracefully, continuing shutdown");
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
1282
0
                break;
1283
0
            }
1284
0
            std::this_thread::sleep_for(50ms);
1285
0
        }
1286
        // Break HTTPServer I/O loop: stop accepting connections, sending and receiving data
1287
0
        g_http_server->InterruptNet();
1288
        // Wait for HTTPServer I/O thread to exit
1289
0
        g_http_server->JoinSocketsThreads();
1290
        // Force-remove any clients that survived the graceful wait
1291
0
        g_http_server->ClearConnectedClients();
1292
        // Close all listening sockets
1293
0
        g_http_server->StopListening();
1294
0
    }
1295
0
    LogDebug(BCLog::HTTP, "Stopped HTTP server");
Line
Count
Source
143
0
#define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
Line
Count
Source
136
0
    do {                                                                                      \
137
0
        if (shouldlog(category)) {                                                            \
138
0
            detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
139
0
        }                                                                                     \
140
0
    } while (0)
1296
0
}
1297
} // namespace http_bitcoin