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