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