/tmp/bitcoin/src/httpserver.cpp
Line | Count | Source |
1 | | // Copyright (c) 2015-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <bitcoin-build-config.h> // IWYU pragma: keep |
6 | | |
7 | | #include <httpserver.h> |
8 | | |
9 | | #include <chainparamsbase.h> |
10 | | #include <common/args.h> |
11 | | #include <common/messages.h> |
12 | | #include <common/url.h> |
13 | | #include <compat/compat.h> |
14 | | #include <logging.h> |
15 | | #include <netbase.h> |
16 | | #include <node/interface_ui.h> |
17 | | #include <rpc/protocol.h> |
18 | | #include <span.h> |
19 | | #include <sync.h> |
20 | | #include <util/check.h> |
21 | | #include <util/signalinterrupt.h> |
22 | | #include <util/sock.h> |
23 | | #include <util/strencodings.h> |
24 | | #include <util/thread.h> |
25 | | #include <util/threadnames.h> |
26 | | #include <util/threadpool.h> |
27 | | #include <util/time.h> |
28 | | #include <util/translation.h> |
29 | | |
30 | | #include <condition_variable> |
31 | | #include <cstdio> |
32 | | #include <cstdlib> |
33 | | #include <deque> |
34 | | #include <memory> |
35 | | #include <optional> |
36 | | #include <span> |
37 | | #include <string> |
38 | | #include <string_view> |
39 | | #include <thread> |
40 | | #include <unordered_map> |
41 | | #include <vector> |
42 | | |
43 | | #include <sys/types.h> |
44 | | #include <sys/stat.h> |
45 | | |
46 | | //! The set of sockets cannot be modified while waiting, so |
47 | | //! the sleep time needs to be small to avoid new sockets stalling. |
48 | | static constexpr auto SELECT_TIMEOUT{50ms}; |
49 | | |
50 | | //! Explicit alias for setting socket option methods. |
51 | | static constexpr int SOCKET_OPTION_TRUE{1}; |
52 | | |
53 | | using common::InvalidPortErrMsg; |
54 | | using http_bitcoin::HTTPRequest; |
55 | | |
56 | | struct HTTPPathHandler |
57 | | { |
58 | | HTTPPathHandler(std::string _prefix, bool _exactMatch, HTTPRequestHandler _handler): |
59 | 2.26k | prefix(_prefix), exactMatch(_exactMatch), handler(_handler) |
60 | 2.26k | { |
61 | 2.26k | } |
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 | | bool HTTPServer::ClientAllowed(const CNetAddr& netaddr) const |
81 | 3.36k | { |
82 | 3.36k | if (!netaddr.IsValid()) |
83 | 0 | return false; |
84 | 3.36k | for(const CSubNet& subnet : m_allow_subnets) |
85 | 3.37k | if (subnet.Match(netaddr)) |
86 | 3.36k | return true; |
87 | 2 | return false; |
88 | 3.36k | } |
89 | | |
90 | | /** Initialize ACL list for HTTP server */ |
91 | | bool HTTPServer::InitHTTPAllowList() |
92 | 1.13k | { |
93 | | // Must be run before StartSocketThreads() because ThreadSocketHandler() |
94 | | // will check m_allow_subnets from the I/O thread. |
95 | 1.13k | Assume(!m_thread_socket_handler.joinable()); |
96 | | |
97 | 1.13k | m_allow_subnets.clear(); |
98 | 1.13k | m_allow_subnets.emplace_back(LookupHost("127.0.0.1", false).value(), 8); // always allow IPv4 local subnet |
99 | 1.13k | m_allow_subnets.emplace_back(LookupHost("::1", false).value()); // always allow IPv6 localhost |
100 | 1.13k | for (const std::string& strAllow : gArgs.GetArgs("-rpcallowip")) { |
101 | 16 | const CSubNet subnet{LookupSubNet(strAllow)}; |
102 | 16 | if (!subnet.IsValid()) { |
103 | 1 | uiInterface.ThreadSafeMessageBox( |
104 | 1 | 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 | 1 | CClientUIInterface::MSG_ERROR); |
106 | 1 | return false; |
107 | 1 | } |
108 | 15 | m_allow_subnets.push_back(subnet); |
109 | 15 | } |
110 | 1.13k | std::string strAllowed; |
111 | 1.13k | for (const CSubNet& subnet : m_allow_subnets) |
112 | 2.29k | strAllowed += subnet.ToString() + " "; |
113 | 1.13k | LogDebug(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed); |
114 | 1.13k | return true; |
115 | 1.13k | } |
116 | | } // namespace http_bitcoin |
117 | | |
118 | | /** HTTP request method as string - use for logging only */ |
119 | | std::string_view RequestMethodString(HTTPRequestMethod m) |
120 | 187k | { |
121 | 187k | switch (m) { |
122 | 0 | using enum HTTPRequestMethod; |
123 | 800 | case GET: return "GET"; |
124 | 186k | case POST: return "POST"; |
125 | 0 | case HEAD: return "HEAD"; |
126 | 0 | case PUT: return "PUT"; |
127 | 5 | case UNKNOWN: return "unknown"; |
128 | 187k | } // no default case, so the compiler can warn about missing cases |
129 | 187k | assert(false); |
130 | 0 | } |
131 | | |
132 | | static void WriteNoStoreErrorReply(HTTPRequest& req, HTTPStatusCode status, std::string_view reply = {}) |
133 | 1.02k | { |
134 | 1.02k | req.WriteHeader("Cache-Control", "no-store"); |
135 | 1.02k | req.WriteReply(status, reply); |
136 | 1.02k | } |
137 | | |
138 | | static void MaybeDispatchRequestToWorker(std::shared_ptr<HTTPRequest> hreq) |
139 | 187k | { |
140 | | // Early reject unknown HTTP methods |
141 | 187k | if (hreq->GetRequestMethod() == HTTPRequestMethod::UNKNOWN) { |
142 | 5 | LogDebug(BCLog::HTTP, "HTTP request from %s rejected: Unknown HTTP request method\n", |
143 | 5 | hreq->GetPeer().ToStringAddrPort()); |
144 | 5 | WriteNoStoreErrorReply(*hreq, HTTP_BAD_METHOD); |
145 | 5 | return; |
146 | 5 | } |
147 | | |
148 | | // Find registered handler for prefix |
149 | 187k | std::string strURI = hreq->GetURI(); |
150 | 187k | std::string path; |
151 | 187k | LOCK(g_httppathhandlers_mutex); |
152 | 187k | std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin(); |
153 | 187k | std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end(); |
154 | 214k | for (; i != iend; ++i) { |
155 | 214k | bool match = false; |
156 | 214k | if (i->exactMatch) |
157 | 187k | match = (strURI == i->prefix); |
158 | 27.0k | else |
159 | 27.0k | match = strURI.starts_with(i->prefix); |
160 | 214k | if (match) { |
161 | 187k | path = strURI.substr(i->prefix.size()); |
162 | 187k | break; |
163 | 187k | } |
164 | 214k | } |
165 | | |
166 | | // Dispatch to worker thread |
167 | 187k | if (i != iend) { |
168 | 187k | if (static_cast<int>(g_threadpool_http.WorkQueueSize()) >= g_max_queue_depth) { |
169 | 1.00k | LogWarning("Request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting"); |
170 | 1.00k | WriteNoStoreErrorReply(*hreq, HTTP_SERVICE_UNAVAILABLE, "Work queue depth exceeded"); |
171 | 1.00k | return; |
172 | 1.00k | } |
173 | | |
174 | 186k | auto item = [req = hreq, in_path = std::move(path), fn = i->handler]() { |
175 | 186k | std::string err_msg; |
176 | 186k | try { |
177 | 186k | fn(req.get(), in_path); |
178 | 186k | return; |
179 | 186k | } 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 | 0 | }; |
191 | | |
192 | 186k | 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 | 186k | } else { |
200 | 12 | WriteNoStoreErrorReply(*hreq, HTTP_NOT_FOUND); |
201 | 12 | } |
202 | 187k | } |
203 | | |
204 | | static void RejectRequest(std::unique_ptr<http_bitcoin::HTTPRequest> hreq) |
205 | 0 | { |
206 | 0 | LogDebug(BCLog::HTTP, "Rejecting request while shutting down"); |
207 | 0 | WriteNoStoreErrorReply(*hreq, HTTP_SERVICE_UNAVAILABLE); |
208 | 0 | } |
209 | | |
210 | | static std::vector<std::pair<std::string, uint16_t>> GetBindAddresses() |
211 | 1.13k | { |
212 | 1.13k | uint16_t http_port{static_cast<uint16_t>(gArgs.GetIntArg("-rpcport", BaseParams().RPCPort()))}; |
213 | 1.13k | 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 | 1.13k | if (gArgs.GetArgs("-rpcallowip").empty() || gArgs.GetArgs("-rpcbind").empty()) { // Default to loopback if not allowing external IPs |
221 | 1.12k | endpoints.emplace_back("::1", http_port); |
222 | 1.12k | endpoints.emplace_back("127.0.0.1", http_port); |
223 | 1.12k | if (!gArgs.GetArgs("-rpcallowip").empty()) { |
224 | 3 | LogWarning("Option -rpcallowip was specified without -rpcbind; this doesn't usually make sense"); |
225 | 3 | } |
226 | 1.12k | if (!gArgs.GetArgs("-rpcbind").empty()) { |
227 | 0 | LogWarning("Option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect"); |
228 | 0 | } |
229 | 1.12k | } else { // Specific bind addresses |
230 | 14 | for (const std::string& strRPCBind : gArgs.GetArgs("-rpcbind")) { |
231 | 14 | uint16_t port{http_port}; |
232 | 14 | std::string host; |
233 | 14 | if (!SplitHostPort(strRPCBind, port, host)) { |
234 | 0 | LogError("%s\n", InvalidPortErrMsg("-rpcbind", strRPCBind).original); |
235 | 0 | return {}; // empty |
236 | 0 | } |
237 | 14 | endpoints.emplace_back(host, port); |
238 | 14 | } |
239 | 9 | } |
240 | 1.13k | return endpoints; |
241 | 1.13k | } |
242 | | |
243 | | void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler) |
244 | 2.26k | { |
245 | 2.26k | LogDebug(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch); |
246 | 2.26k | LOCK(g_httppathhandlers_mutex); |
247 | 2.26k | pathHandlers.emplace_back(prefix, exactMatch, handler); |
248 | 2.26k | } |
249 | | |
250 | | void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch) |
251 | 18.8k | { |
252 | 18.8k | LOCK(g_httppathhandlers_mutex); |
253 | 18.8k | std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin(); |
254 | 18.8k | std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end(); |
255 | 18.8k | for (; i != iend; ++i) |
256 | 2.26k | if (i->prefix == prefix && i->exactMatch == exactMatch) |
257 | 2.26k | break; |
258 | 18.8k | if (i != iend) |
259 | 2.26k | { |
260 | 2.26k | LogDebug(BCLog::HTTP, "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch); |
261 | 2.26k | pathHandlers.erase(i); |
262 | 2.26k | } |
263 | 18.8k | } |
264 | | |
265 | | namespace http_bitcoin { |
266 | | using util::Split; |
267 | | |
268 | | std::optional<std::string> HTTPHeaders::FindFirst(const std::string_view key) const |
269 | 834k | { |
270 | 3.87M | for (const auto& item : m_headers) { |
271 | 3.87M | if (CaseInsensitiveEqual(key, item.first)) { |
272 | 373k | return item.second; |
273 | 373k | } |
274 | 3.87M | } |
275 | 460k | return std::nullopt; |
276 | 834k | } |
277 | | |
278 | | std::vector<std::string_view> HTTPHeaders::FindAll(const std::string_view key) const |
279 | 273k | { |
280 | 273k | std::vector<std::string_view> ret; |
281 | 1.63M | for (const auto& item : m_headers) { |
282 | 1.63M | if (CaseInsensitiveEqual(key, item.first)) { |
283 | 272k | ret.push_back(item.second); |
284 | 272k | } |
285 | 1.63M | } |
286 | 273k | return ret; |
287 | 273k | } |
288 | | |
289 | | void HTTPHeaders::Write(std::string&& key, std::string&& value) |
290 | 2.20M | { |
291 | 2.20M | m_headers.emplace_back(std::move(key), std::move(value)); |
292 | 2.20M | } |
293 | | |
294 | | void HTTPHeaders::RemoveAll(std::string_view key) |
295 | 1.10k | { |
296 | 3.32k | auto moved = std::ranges::remove_if(m_headers, [key] (auto& pair) { |
297 | 3.32k | return CaseInsensitiveEqual(key, pair.first); |
298 | 3.32k | }); |
299 | 1.10k | m_headers.erase(moved.begin(), moved.end()); |
300 | 1.10k | } |
301 | | |
302 | | bool HTTPHeaders::Read(util::LineReader& reader) |
303 | 274k | { |
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 | 1.91M | while (auto maybe_line = reader.ReadLine()) { |
307 | 1.91M | if (reader.Consumed() > MAX_HEADERS_SIZE) throw std::runtime_error("HTTP headers exceed size limit"); |
308 | | |
309 | 1.91M | 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 | 1.91M | 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 | 1.63M | 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 | 1.63M | const size_t pos{line.find(':')}; |
326 | 1.63M | 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 | 1.63M | std::string_view key = line.substr(0, pos); |
331 | 1.63M | 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 | 1.63M | 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 | 1.63M | if (key.empty()) throw std::runtime_error("Empty HTTP header name"); |
339 | | |
340 | 1.63M | Write(std::string(key), std::move(value)); |
341 | 1.63M | } |
342 | | |
343 | 64 | return false; |
344 | 274k | } |
345 | | |
346 | | std::string HTTPHeaders::Stringify() const |
347 | 187k | { |
348 | 187k | std::string out; |
349 | 565k | for (const auto& [key, value] : m_headers) { |
350 | 565k | out += key + ": " + value + "\r\n"; |
351 | 565k | } |
352 | | |
353 | | // Headers are terminated by an empty line |
354 | 187k | out += "\r\n"; |
355 | | |
356 | 187k | return out; |
357 | 187k | } |
358 | | |
359 | | std::string HTTPResponse::StringifyHeaders() const |
360 | 187k | { |
361 | 187k | return strprintf("HTTP/%d.%d %d %s\r\n%s", |
362 | 187k | m_version.major, |
363 | 187k | m_version.minor, |
364 | 187k | m_status, |
365 | 187k | HTTPStatusReasonString(m_status), |
366 | 187k | m_headers.Stringify()); |
367 | 187k | } |
368 | | |
369 | | bool HTTPRequest::LoadControlData(LineReader& reader) |
370 | 274k | { |
371 | 274k | auto maybe_line = reader.ReadLine(); |
372 | 274k | if (!maybe_line) return false; |
373 | 274k | 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 | 274k | 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 | 274k | if (request_line.find('\0') != std::string_view::npos) throw std::runtime_error("Invalid request line contains NUL"); |
383 | | |
384 | 274k | const std::vector<std::string_view> parts{Split<std::string_view>(request_line, " ")}; |
385 | 274k | if (parts.size() != 3) throw std::runtime_error("HTTP request line malformed"); |
386 | | |
387 | 274k | if (parts[0] == "GET") { |
388 | 888 | m_method = HTTPRequestMethod::GET; |
389 | 273k | } else if (parts[0] == "POST") { |
390 | 273k | m_method = HTTPRequestMethod::POST; |
391 | 273k | } else if (parts[0] == "HEAD") { |
392 | 0 | m_method = HTTPRequestMethod::HEAD; |
393 | 6 | } else if (parts[0] == "PUT") { |
394 | 0 | m_method = HTTPRequestMethod::PUT; |
395 | 6 | } else { |
396 | 6 | m_method = HTTPRequestMethod::UNKNOWN; |
397 | 6 | } |
398 | | |
399 | 274k | m_target = parts[1]; |
400 | | |
401 | 274k | 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 | 274k | const std::vector<std::string_view> version_parts{Split<std::string_view>(parts[2].substr(5), ".")}; |
406 | 274k | if (version_parts.size() != 2) throw std::runtime_error("HTTP request line malformed"); |
407 | 274k | if (version_parts[0].size() != 1 || version_parts[1].size() != 1) throw std::runtime_error("HTTP bad version"); |
408 | 274k | auto major = ToIntegral<uint8_t>(version_parts[0]); |
409 | 274k | auto minor = ToIntegral<uint8_t>(version_parts[1]); |
410 | 274k | if (!major || !minor || major != 1 || minor > 9) throw std::runtime_error("HTTP bad version"); |
411 | 274k | m_version.major = major.value(); |
412 | 274k | m_version.minor = minor.value(); |
413 | | |
414 | 274k | return true; |
415 | 274k | } |
416 | | |
417 | | bool HTTPRequest::LoadHeaders(LineReader& reader) |
418 | 274k | { |
419 | 274k | return m_headers.Read(reader); |
420 | 274k | } |
421 | | |
422 | | bool HTTPRequest::LoadBody(LineReader& reader) |
423 | 274k | { |
424 | | // https://httpwg.org/specs/rfc9112.html#message.body |
425 | 274k | auto transfer_encoding_header = m_headers.FindFirst("Transfer-Encoding"); |
426 | 274k | 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 | 1.61k | while (reader.Remaining() > 0) { |
431 | 1.61k | auto maybe_chunk_size = reader.ReadLine(); |
432 | 1.61k | 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 | 1.61k | std::string_view chunk_size_noext{maybe_chunk_size.value()}; |
437 | 1.61k | const auto semicolon_pos = chunk_size_noext.find(';'); |
438 | 1.61k | if (semicolon_pos != chunk_size_noext.npos) { |
439 | 3 | chunk_size_noext.remove_suffix(chunk_size_noext.size() - semicolon_pos); |
440 | 3 | } |
441 | | |
442 | 1.61k | const auto chunk_size{ToIntegral<uint64_t>(util::TrimStringView(chunk_size_noext), /*base=*/16)}; |
443 | 1.61k | if (!chunk_size) throw std::runtime_error("Cannot parse chunk length value"); |
444 | | |
445 | 1.61k | if ((m_body.size() > MAX_BODY_SIZE) || |
446 | 1.61k | (*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 | 1.61k | 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 | 0 | } |
461 | 6 | if (!maybe_trailer) return false; |
462 | 6 | if (maybe_trailer->empty()) break; |
463 | 6 | } |
464 | | // Complete request has been parsed, reader is now pointing |
465 | | // to beginning of next request or end of the buffer. |
466 | 5 | return true; |
467 | 5 | } |
468 | | |
469 | | // We are still expecting more data for this chunk |
470 | 1.60k | if (reader.Remaining() < *chunk_size) { |
471 | 518 | return false; |
472 | 518 | } |
473 | | |
474 | | // Pack chunk onto body |
475 | 1.08k | 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 | 1.08k | auto crlf = reader.ReadLine(); |
481 | 1.08k | if (!crlf) { |
482 | | // CRLF not found before end of buffer: it has not been received by our socket yet. |
483 | 1 | return false; |
484 | 1 | } |
485 | | // CRLF was found but there was unexpected data after the chunk_sized chunk |
486 | 1.08k | if (!crlf.value().empty()) throw std::runtime_error("Improperly terminated chunk"); |
487 | 1.08k | } |
488 | | |
489 | | // We read all the chunks but never got the last chunk, wait for client to send more |
490 | 0 | return false; |
491 | 273k | } else { |
492 | | // No Content-length or Transfer-Encoding header means no body, see libevent evhttp_get_body() |
493 | 273k | auto content_length_values{m_headers.FindAll("Content-Length")}; |
494 | 273k | 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 | 272k | const auto& first_content_length_value{content_length_values[0]}; |
499 | 272k | 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 | 3 | } |
502 | | |
503 | 272k | const auto content_length{ToIntegral<uint64_t>(first_content_length_value)}; |
504 | 272k | if (!content_length) throw std::runtime_error("Cannot parse Content-Length value"); |
505 | | |
506 | 272k | if (*content_length > MAX_BODY_SIZE) throw ContentTooLargeError("Max body size exceeded"); |
507 | | |
508 | | // Not enough data in buffer for expected body |
509 | 272k | if (reader.Remaining() < *content_length) return false; |
510 | | |
511 | 186k | m_body = reader.ReadLength(*content_length); |
512 | | |
513 | 186k | return true; |
514 | 272k | } |
515 | 274k | } |
516 | | |
517 | | void HTTPRequest::WriteReply(HTTPStatusCode status, std::span<const std::byte> reply_body) |
518 | 187k | { |
519 | 187k | HTTPResponse res; |
520 | | |
521 | | // Some response headers are determined in advance and stored in the request |
522 | 187k | res.m_headers = std::move(m_response_headers); |
523 | | |
524 | | // Response version matches request version |
525 | 187k | res.m_version = m_version; |
526 | | |
527 | | // Add response code |
528 | 187k | res.m_status = status; |
529 | | |
530 | | // See libevent evhttp_response_needs_body() |
531 | | // Response headers are different if no body is needed |
532 | 187k | bool needs_body{status != HTTP_NO_CONTENT && (status < 100 || status >= 200)}; |
533 | 187k | bool needs_content_length{false}; |
534 | | |
535 | 187k | bool keep_alive{false}; |
536 | | |
537 | | // See libevent evhttp_make_header_response() |
538 | | // Expected response headers depend on protocol version |
539 | 187k | if (m_version.major == 1) { |
540 | | // HTTP/1.0 |
541 | 187k | 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 | 0 | } |
550 | 0 | } |
551 | | |
552 | | // HTTP/1.1 |
553 | 187k | if (m_version.minor >= 1) { |
554 | 187k | const int64_t now_seconds{TicksSinceEpoch<std::chrono::seconds>(NodeClock::now())}; |
555 | 187k | 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 | 187k | if (needs_body) needs_content_length = true; |
559 | | |
560 | | // Default for HTTP/1.1 |
561 | 187k | keep_alive = true; |
562 | 187k | } |
563 | 187k | } |
564 | | |
565 | 187k | if (needs_content_length) { |
566 | 187k | res.m_headers.Write("Content-Length", util::ToString(reply_body.size())); |
567 | 187k | } |
568 | | |
569 | 187k | if (needs_body && !res.m_headers.FindFirst("Content-Type")) { |
570 | | // Default type from libevent evhttp_new_object() |
571 | 1.08k | res.m_headers.Write("Content-Type", "text/html; charset=ISO-8859-1"); |
572 | 1.08k | } |
573 | | |
574 | 187k | auto connection_header{m_headers.FindFirst("Connection")}; |
575 | 187k | if (connection_header && ToLower(connection_header.value()) == "close") { |
576 | | // Might not exist already but we need to replace it, not append to it |
577 | 1.10k | res.m_headers.RemoveAll("Connection"); |
578 | | |
579 | 1.10k | res.m_headers.Write("Connection", "close"); |
580 | 1.10k | keep_alive = false; |
581 | 1.10k | } |
582 | | |
583 | 187k | m_client->m_keep_alive = keep_alive; |
584 | | |
585 | | // Serialize the response headers |
586 | 187k | const std::string headers{res.StringifyHeaders()}; |
587 | 187k | const auto headers_bytes{std::as_bytes(std::span{headers})}; |
588 | | |
589 | 187k | bool send_buffer_was_empty{false}; |
590 | | // Fill the send buffer with the complete serialized response headers + body |
591 | 187k | { |
592 | 187k | LOCK(m_client->m_send_mutex); |
593 | 187k | send_buffer_was_empty = m_client->m_send_buffer.empty(); |
594 | 187k | 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 | 187k | 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 | 187k | if (!send_buffer_was_empty) m_client->m_send_ready = true; |
610 | 187k | } |
611 | | |
612 | 187k | LogDebug( |
613 | 187k | BCLog::HTTP, |
614 | 187k | "HTTPResponse (status code: %d size: %lld) added to send buffer for client %s (id=%llu)", |
615 | 187k | status, |
616 | 187k | headers_bytes.size() + reply_body.size(), |
617 | 187k | m_client->m_origin, |
618 | 187k | 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 | 187k | if (send_buffer_was_empty) { |
625 | 187k | m_client->MaybeSendBytesFromBuffer(); |
626 | 187k | } |
627 | | |
628 | | // Signal to the I/O loop that we are ready to handle the next request. |
629 | 187k | m_client->m_req_busy = false; |
630 | 187k | } |
631 | | |
632 | | CService HTTPRequest::GetPeer() const |
633 | 185k | { |
634 | 185k | return m_client->m_addr; |
635 | 185k | } |
636 | | |
637 | | std::optional<std::string> HTTPRequest::GetQueryParameter(const std::string_view key) const |
638 | 93 | { |
639 | 93 | return GetQueryParameterFromUri(m_target, key); |
640 | 93 | } |
641 | | |
642 | | // See libevent http.c evhttp_parse_query_impl() |
643 | | // and https://www.rfc-editor.org/rfc/rfc3986#section-3.4 |
644 | | std::optional<std::string> GetQueryParameterFromUri(const std::string_view uri, const std::string_view key) |
645 | 107 | { |
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 | 97 | } |
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 | 124 | for (const std::string_view& param : params) { |
657 | 124 | size_t delim = param.find('='); |
658 | 124 | if (key == UrlDecode(param.substr(0, delim))) { |
659 | 85 | if (delim == std::string::npos) { |
660 | 0 | return ""; |
661 | 85 | } else { |
662 | 85 | return std::string(UrlDecode(param.substr(delim + 1))); |
663 | 85 | } |
664 | 85 | } |
665 | 124 | } |
666 | 12 | return std::nullopt; |
667 | 97 | } |
668 | | |
669 | | std::pair<bool, std::string> HTTPRequest::GetHeader(const std::string_view hdr) const |
670 | 185k | { |
671 | 185k | std::optional<std::string> found{m_headers.FindFirst(hdr)}; |
672 | 185k | return std::pair{found.has_value(), std::move(found).value_or("")}; |
673 | 185k | } |
674 | | |
675 | | void HTTPRequest::WriteHeader(std::string&& hdr, std::string&& value) |
676 | 188k | { |
677 | 188k | m_response_headers.Write(std::move(hdr), std::move(value)); |
678 | 188k | } |
679 | | |
680 | | util::Expected<void, std::string> HTTPServer::BindAndStartListening(const CService& to) |
681 | 2.27k | { |
682 | | // Create socket for listening for incoming connections |
683 | 2.27k | sockaddr_storage storage; |
684 | 2.27k | auto sa = reinterpret_cast<sockaddr*>(&storage); |
685 | 2.27k | socklen_t len{sizeof(storage)}; |
686 | 2.27k | if (!to.GetSockAddr(sa, &len)) { |
687 | 1 | return util::Unexpected{strprintf("Bind address family for %s not supported", to.ToStringAddrPort())}; |
688 | 1 | } |
689 | | |
690 | 2.26k | std::unique_ptr<Sock> sock{CreateSock(to.GetSAFamily(), SOCK_STREAM, IPPROTO_TCP)}; |
691 | 2.26k | if (!sock) { |
692 | 0 | return util::Unexpected{strprintf("Cannot create %s listen socket: %s", |
693 | 0 | to.ToStringAddrPort(), |
694 | 0 | NetworkErrorString(WSAGetLastError()))}; |
695 | 0 | } |
696 | | |
697 | | // Allow binding if the port is still in TIME_WAIT state after |
698 | | // the program was closed and restarted. |
699 | 2.26k | if (sock->SetSockOpt(SOL_SOCKET, SO_REUSEADDR, &SOCKET_OPTION_TRUE, sizeof(SOCKET_OPTION_TRUE)) == SOCKET_ERROR) { |
700 | 0 | LogDebug(BCLog::HTTP, |
701 | 0 | "Cannot set SO_REUSEADDR on %s listen socket: %s, continuing anyway", |
702 | 0 | to.ToStringAddrPort(), |
703 | 0 | NetworkErrorString(WSAGetLastError())); |
704 | 0 | } |
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 | 2.26k | if (to.IsIPv6()) { |
709 | 1.12k | #ifdef IPV6_V6ONLY |
710 | 1.12k | if (sock->SetSockOpt(IPPROTO_IPV6, IPV6_V6ONLY, &SOCKET_OPTION_TRUE, sizeof(SOCKET_OPTION_TRUE)) == SOCKET_ERROR) { |
711 | 0 | LogDebug(BCLog::HTTP, |
712 | 0 | "Cannot set IPV6_V6ONLY on %s listen socket: %s, continuing anyway", |
713 | 0 | to.ToStringAddrPort(), |
714 | 0 | NetworkErrorString(WSAGetLastError())); |
715 | 0 | } |
716 | 1.12k | #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 | 1.12k | } |
730 | | |
731 | 2.26k | 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 | 0 | } 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 | 0 | } |
742 | 0 | } |
743 | | |
744 | | // Listen for incoming connections |
745 | 2.26k | 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 | 0 | } |
750 | | |
751 | 2.26k | m_listen.emplace_back(std::move(sock)); |
752 | | |
753 | 2.26k | return {}; |
754 | 2.26k | } |
755 | | |
756 | | void HTTPServer::StopListening() |
757 | 1.13k | { |
758 | 1.13k | m_listen.clear(); |
759 | 1.13k | } |
760 | | |
761 | | void HTTPServer::StartSocketsThreads() |
762 | 1.12k | { |
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 | 1.12k | Assume(!m_allow_subnets.empty()); |
767 | | |
768 | 1.12k | m_thread_socket_handler = std::thread(&util::TraceThread, |
769 | 1.12k | "http", |
770 | 1.12k | [this] { ThreadSocketHandler(); }); |
771 | 1.12k | } |
772 | | |
773 | | void HTTPServer::JoinSocketsThreads() |
774 | 1.13k | { |
775 | 1.13k | if (m_thread_socket_handler.joinable()) { |
776 | 1.12k | m_thread_socket_handler.join(); |
777 | 1.12k | } |
778 | 1.13k | } |
779 | | |
780 | | std::unique_ptr<Sock> HTTPServer::AcceptConnection(const Sock& listen_sock, CService& addr) |
781 | 3.36k | { |
782 | | // Make sure we only operate on our own listening sockets |
783 | 3.36k | Assume(std::ranges::any_of(m_listen, [&](const auto& sock) { return sock.get() == &listen_sock; })); |
784 | | |
785 | 3.36k | sockaddr_storage storage; |
786 | 3.36k | socklen_t len{sizeof(storage)}; |
787 | 3.36k | auto sa = reinterpret_cast<sockaddr*>(&storage); |
788 | | |
789 | 3.36k | auto sock{listen_sock.Accept(sa, &len)}; |
790 | | |
791 | 3.36k | if (!sock) { |
792 | 0 | const int err{WSAGetLastError()}; |
793 | 0 | if (err != WSAEWOULDBLOCK) { |
794 | 0 | LogDebug(BCLog::HTTP, |
795 | 0 | "Cannot accept new connection: %s", |
796 | 0 | NetworkErrorString(err)); |
797 | 0 | } |
798 | 0 | return {}; |
799 | 0 | } |
800 | | |
801 | | // The OS handed us a valid socket but we can't determine its source address. |
802 | 3.36k | if (!addr.SetSockAddr(sa, len)) { |
803 | 0 | LogDebug(BCLog::HTTP, |
804 | 0 | "Unknown socket family"); |
805 | 0 | } |
806 | | |
807 | | // Early address-based allow check |
808 | 3.36k | if (!ClientAllowed(addr)) { |
809 | 2 | LogDebug(BCLog::HTTP, "Connection from %s rejected: Client network is not allowed HTTP access\n", |
810 | 2 | addr.ToStringAddrPort()); |
811 | | // Socket destroyed, connection aborted |
812 | 2 | return {}; |
813 | 2 | } |
814 | | |
815 | 3.36k | return sock; |
816 | 3.36k | } |
817 | | |
818 | | HTTPServer::Id HTTPServer::GetNewId() |
819 | 3.36k | { |
820 | 3.36k | return m_next_id.fetch_add(1, std::memory_order_relaxed); |
821 | 3.36k | } |
822 | | |
823 | | void HTTPServer::NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& addr) |
824 | 3.36k | { |
825 | 3.36k | if (!sock->IsSelectable()) { |
826 | 0 | LogDebug(BCLog::HTTP, |
827 | 0 | "connection from %s dropped: non-selectable socket", |
828 | 0 | addr.ToStringAddrPort()); |
829 | 0 | return; |
830 | 0 | } |
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 | 3.36k | 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 | 0 | addr.ToStringAddrPort()); |
837 | 0 | } |
838 | | |
839 | 3.36k | const Id id{GetNewId()}; |
840 | | |
841 | 3.36k | m_connected.push_back(std::make_shared<HTTPRemoteClient>(id, addr, std::move(sock))); |
842 | | // Report back to the main thread |
843 | 3.36k | m_connected_size.fetch_add(1, std::memory_order_relaxed); |
844 | | |
845 | 3.36k | LogDebug(BCLog::HTTP, |
846 | 3.36k | "HTTP Connection accepted from %s (id=%llu)", |
847 | 3.36k | addr.ToStringAddrPort(), id); |
848 | 3.36k | } |
849 | | |
850 | | void HTTPServer::SocketHandlerConnected(const IOReadiness& io_readiness) const |
851 | 448k | { |
852 | 1.55M | for (const auto& [sock, events] : io_readiness.events_per_sock) { |
853 | 1.55M | if (m_interrupt_net) { |
854 | 1.11k | return; |
855 | 1.11k | } |
856 | | |
857 | 1.55M | auto it{io_readiness.httpclients_per_sock.find(sock)}; |
858 | 1.55M | if (it == io_readiness.httpclients_per_sock.end()) { |
859 | 894k | continue; |
860 | 894k | } |
861 | 659k | const std::shared_ptr<HTTPRemoteClient>& client{it->second}; |
862 | | |
863 | 659k | bool send_ready = events.occurred & Sock::SendEvent; |
864 | 659k | bool recv_ready = events.occurred & Sock::RecvEvent; |
865 | 659k | bool err_ready = events.occurred & Sock::ErrorEvent; |
866 | | |
867 | 659k | 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 | 13 | if (!client->MaybeSendBytesFromBuffer()) { |
872 | 0 | recv_ready = false; |
873 | 0 | } |
874 | 13 | } |
875 | | |
876 | 659k | if (recv_ready || err_ready) { |
877 | 269k | char buf[0x10000]; // typical socket buffer is 8K-64K |
878 | | |
879 | 269k | const ssize_t nrecv{WITH_LOCK( |
880 | 269k | client->m_sock_mutex, |
881 | 269k | return client->m_sock->Recv(buf, sizeof(buf), MSG_DONTWAIT);)}; |
882 | | |
883 | 269k | if (nrecv < 0) { |
884 | 0 | const int err = WSAGetLastError(); |
885 | 0 | if (IOErrorIsPermanent(err)) { |
886 | 0 | LogDebug( |
887 | 0 | BCLog::HTTP, |
888 | 0 | "Permanent read error from %s (id=%llu): %s", |
889 | 0 | client->m_origin, |
890 | 0 | client->m_id, |
891 | 0 | NetworkErrorString(err)); |
892 | 0 | client->m_disconnect = true; |
893 | 0 | } |
894 | 269k | } else if (nrecv == 0) { |
895 | 2.25k | LogDebug( |
896 | 2.25k | BCLog::HTTP, |
897 | 2.25k | "Received EOF from %s (id=%llu)", |
898 | 2.25k | client->m_origin, |
899 | 2.25k | client->m_id); |
900 | 2.25k | client->m_disconnect = true; |
901 | 267k | } else { |
902 | | // Reset idle timeout |
903 | 267k | client->m_idle_since = Now<SteadySeconds>(); |
904 | | |
905 | | // Prevent disconnect until all requests are completely handled. |
906 | 267k | client->m_connection_busy = true; |
907 | | |
908 | | // Copy data from socket buffer to client receive buffer |
909 | 267k | client->m_recv_buffer.insert( |
910 | 267k | client->m_recv_buffer.end(), |
911 | 267k | buf, |
912 | 267k | buf + nrecv); |
913 | 267k | } |
914 | 269k | } |
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 | 659k | MaybeDispatchRequestsFromClient(client); |
920 | 659k | } |
921 | 448k | } |
922 | | |
923 | | void HTTPServer::SocketHandlerListening(const Sock::EventsPerSock& events_per_sock) |
924 | 448k | { |
925 | 448k | if (m_stop_accepting) return; |
926 | 892k | for (const auto& sock : m_listen) { |
927 | 892k | if (m_interrupt_net) { |
928 | 3 | return; |
929 | 3 | } |
930 | 892k | const auto it = events_per_sock.find(sock); |
931 | 892k | if (it != events_per_sock.end() && it->second.occurred & Sock::RecvEvent) { |
932 | 3.36k | CService addr_accepted; |
933 | | |
934 | 3.36k | auto sock_accepted{AcceptConnection(*sock, addr_accepted)}; |
935 | | |
936 | 3.36k | if (sock_accepted) { |
937 | 3.36k | NewSockAccepted(std::move(sock_accepted), addr_accepted); |
938 | 3.36k | } |
939 | 3.36k | } |
940 | 892k | } |
941 | 446k | } |
942 | | |
943 | | HTTPServer::IOReadiness HTTPServer::GenerateWaitSockets() const |
944 | 448k | { |
945 | 448k | IOReadiness io_readiness; |
946 | | |
947 | 896k | for (const auto& sock : m_listen) { |
948 | 896k | io_readiness.events_per_sock.emplace(sock, Sock::Events{Sock::RecvEvent}); |
949 | 896k | } |
950 | | |
951 | 659k | for (const auto& http_client : m_connected) { |
952 | | // Safely copy the shared pointer to the socket |
953 | 659k | 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 | 659k | const bool send_ready{WITH_LOCK(http_client->m_send_mutex, return http_client->m_send_ready;)}; |
962 | 659k | Sock::Event event = (send_ready ? Sock::SendEvent : Sock::RecvEvent); |
963 | 659k | io_readiness.events_per_sock.emplace(sock, Sock::Events{event}); |
964 | 659k | io_readiness.httpclients_per_sock.emplace(sock, http_client); |
965 | 659k | } |
966 | | |
967 | 448k | return io_readiness; |
968 | 448k | } |
969 | | |
970 | | /// \anchor http |
971 | | void HTTPServer::ThreadSocketHandler() |
972 | 1.12k | { |
973 | 449k | 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 | 448k | auto io_readiness{GenerateWaitSockets()}; |
979 | 448k | 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 | 448k | !io_readiness.events_per_sock.begin()->first->WaitMany(SELECT_TIMEOUT, |
982 | 448k | io_readiness.events_per_sock)) { |
983 | 0 | m_interrupt_net.sleep_for(SELECT_TIMEOUT); |
984 | 0 | } |
985 | | |
986 | | // Service (send/receive) each of the already connected sockets. |
987 | 448k | SocketHandlerConnected(io_readiness); |
988 | | |
989 | | // Accept new connections from listening sockets. |
990 | 448k | SocketHandlerListening(io_readiness.events_per_sock); |
991 | | |
992 | | // Disconnect any clients that have been flagged. |
993 | 448k | DisconnectClients(); |
994 | 448k | } |
995 | 1.12k | } |
996 | | |
997 | | void HTTPServer::MaybeDispatchRequestsFromClient(const std::shared_ptr<HTTPRemoteClient>& client) const |
998 | 659k | { |
999 | | // Try reading (potentially multiple) HTTP requests from the buffer |
1000 | 847k | while (!client->m_recv_buffer.empty()) { |
1001 | | // Create a new request object and try to fill it with data from the receive buffer |
1002 | 274k | auto req = std::make_unique<HTTPRequest>(client); |
1003 | 274k | try { |
1004 | | // Stop reading if we need more data from the client to parse a complete request |
1005 | 274k | if (!client->ReadRequest(*req)) break; |
1006 | 274k | } catch (const ContentTooLargeError& e) { |
1007 | 2 | LogDebug( |
1008 | 2 | BCLog::HTTP, |
1009 | 2 | "HTTP request body too large from client %s (id=%llu): %s", |
1010 | 2 | client->m_origin, |
1011 | 2 | client->m_id, |
1012 | 2 | e.what()); |
1013 | | |
1014 | 2 | WriteNoStoreErrorReply(*req, HTTP_CONTENT_TOO_LARGE); |
1015 | 2 | client->m_disconnect = true; |
1016 | 2 | return; |
1017 | 10 | } catch (const std::runtime_error& e) { |
1018 | 10 | LogDebug( |
1019 | 10 | BCLog::HTTP, |
1020 | 10 | "Error reading HTTP request from client %s (id=%llu): %s", |
1021 | 10 | client->m_origin, |
1022 | 10 | client->m_id, |
1023 | 10 | 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 | 187k | LogDebug( |
1033 | 187k | BCLog::HTTP, |
1034 | 187k | "Received a %s request for %s from %s (id=%llu)", |
1035 | 187k | RequestMethodString(req->m_method), |
1036 | 187k | req->m_target, |
1037 | 187k | client->m_origin, |
1038 | 187k | client->m_id); |
1039 | | |
1040 | | // add request to client queue |
1041 | 187k | client->m_req_queue.push_back(std::move(req)); |
1042 | 187k | } |
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 | 659k | if (client->m_req_busy) return; |
1048 | | |
1049 | | // Otherwise, if there is a pending request in the queue, handle it. |
1050 | 492k | if (!client->m_req_queue.empty()) { |
1051 | 187k | LOCK(m_request_dispatcher_mutex); |
1052 | 187k | client->m_req_busy = true; |
1053 | 187k | m_request_dispatcher(std::move(client->m_req_queue.front())); |
1054 | 187k | client->m_req_queue.pop_front(); |
1055 | 187k | } |
1056 | 492k | } |
1057 | | |
1058 | | void HTTPServer::DisconnectClients() |
1059 | 448k | { |
1060 | 448k | const auto now{Now<SteadySeconds>()}; |
1061 | 448k | size_t erased = std::erase_if(m_connected, |
1062 | 663k | [&](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 | 663k | const bool is_idle{m_rpcservertimeout.count() > 0 && |
1069 | 663k | now - client->m_idle_since.load() > m_rpcservertimeout && |
1070 | 663k | !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 | 663k | if (client->m_disconnect || is_idle) { |
1075 | 2.36k | if (is_idle) { |
1076 | 5 | LogDebug(BCLog::HTTP, |
1077 | 5 | "HTTP client idle timeout %s (id=%llu)", |
1078 | 5 | client->m_origin, |
1079 | 5 | client->m_id); |
1080 | 5 | } |
1081 | 660k | } else { |
1082 | | // Disconnect this client because the server is shutting |
1083 | | // down and we need to disconnect all clients... |
1084 | 660k | if (m_disconnect_all_clients) { |
1085 | | // ...unless we still have data for this client. |
1086 | 993 | 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 | 0 | return false; |
1090 | 993 | } else { |
1091 | | // This is a healthy persistent connection (e.g. keep-alive) |
1092 | | // but it's time to say goodbye. |
1093 | 993 | ; |
1094 | 993 | } |
1095 | 659k | } else { |
1096 | | // No reason to disconnect. |
1097 | 659k | return false; |
1098 | 659k | } |
1099 | 660k | } |
1100 | | // No reason NOT to disconnect, log and remove. |
1101 | 3.36k | LogDebug(BCLog::HTTP, |
1102 | 3.36k | "Disconnecting HTTP client %s (id=%llu)", |
1103 | 3.36k | client->m_origin, |
1104 | 3.36k | client->m_id); |
1105 | 3.36k | return true; |
1106 | 663k | }); |
1107 | 448k | if (erased > 0) { |
1108 | | // Report back to the main thread |
1109 | 3.24k | m_connected_size.fetch_sub(erased, std::memory_order_relaxed); |
1110 | 3.24k | } |
1111 | 448k | } |
1112 | | |
1113 | | void HTTPServer::ClearConnectedClients() |
1114 | 1.13k | { |
1115 | 1.13k | Assume(!m_thread_socket_handler.joinable()); // must be called after JoinSocketsThreads() |
1116 | 1.13k | 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 | 0 | } |
1121 | | |
1122 | | bool HTTPRemoteClient::ReadRequest(HTTPRequest& req) |
1123 | 274k | { |
1124 | 274k | LineReader reader(m_recv_buffer, MAX_HEADERS_SIZE); |
1125 | | |
1126 | 274k | if (!req.LoadControlData(reader)) return false; |
1127 | 274k | if (!req.LoadHeaders(reader)) return false; |
1128 | 274k | 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 | 187k | m_recv_buffer.erase( |
1134 | 187k | m_recv_buffer.begin(), |
1135 | 187k | m_recv_buffer.begin() + reader.Consumed()); |
1136 | | |
1137 | 187k | return true; |
1138 | 274k | } |
1139 | | |
1140 | | bool HTTPRemoteClient::MaybeSendBytesFromBuffer() |
1141 | 187k | { |
1142 | | // Send as much data from this client's buffer as we can |
1143 | 187k | LOCK(m_send_mutex); |
1144 | 187k | 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 | 187k | int flags{MSG_NOSIGNAL | MSG_DONTWAIT}; |
1154 | | |
1155 | | // Try to send bytes through socket |
1156 | 187k | ssize_t bytes_sent; |
1157 | 187k | { |
1158 | 187k | LOCK(m_sock_mutex); |
1159 | 187k | bytes_sent = m_sock->Send(m_send_buffer.data(), |
1160 | 187k | m_send_buffer.size(), |
1161 | 187k | flags); |
1162 | 187k | } |
1163 | | |
1164 | 187k | if (bytes_sent < 0) { |
1165 | | // Something went wrong |
1166 | 3 | const int err{WSAGetLastError()}; |
1167 | 3 | if (!IOErrorIsPermanent(err)) { |
1168 | | // The error can be safely ignored, try the send again on the next I/O loop. |
1169 | 3 | m_send_ready = true; |
1170 | 3 | m_connection_busy = true; |
1171 | 3 | return true; |
1172 | 3 | } else { |
1173 | | // Unrecoverable error, log and disconnect client. |
1174 | 0 | LogDebug( |
1175 | 0 | BCLog::HTTP, |
1176 | 0 | "Error sending HTTP response data to client %s (id=%llu): %s", |
1177 | 0 | m_origin, |
1178 | 0 | m_id, |
1179 | 0 | 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 | 0 | } |
1186 | 3 | } |
1187 | | |
1188 | | // Successful send, remove sent bytes from our local buffer. |
1189 | 187k | Assume(static_cast<size_t>(bytes_sent) <= m_send_buffer.size()); |
1190 | 187k | m_send_buffer.erase(m_send_buffer.begin(), |
1191 | 187k | m_send_buffer.begin() + bytes_sent); |
1192 | | |
1193 | 187k | LogDebug( |
1194 | 187k | BCLog::HTTP, |
1195 | 187k | "Sent %d bytes to client %s (id=%llu)", |
1196 | 187k | bytes_sent, |
1197 | 187k | m_origin, |
1198 | 187k | 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 | 187k | if (m_send_buffer.empty()) { |
1204 | 187k | m_send_ready = false; |
1205 | 187k | m_connection_busy = false; |
1206 | | |
1207 | | // Our work is done here |
1208 | 187k | if (!m_keep_alive) { |
1209 | 1.10k | m_disconnect = true; |
1210 | | // Do not attempt to read from this client. |
1211 | 1.10k | return false; |
1212 | 1.10k | } |
1213 | 187k | } else { |
1214 | | // The send buffer isn't flushed yet, try to push more on the next loop. |
1215 | 10 | m_send_ready = true; |
1216 | 10 | m_connection_busy = true; |
1217 | 10 | } |
1218 | | |
1219 | | // Finally, reset idle timeout |
1220 | 186k | m_idle_since = Now<SteadySeconds>(); |
1221 | 186k | } |
1222 | | |
1223 | 186k | return true; |
1224 | 187k | } |
1225 | | |
1226 | | bool InitHTTPServer() |
1227 | 1.13k | { |
1228 | | // Create HTTPServer |
1229 | 1.13k | g_http_server = std::make_unique<HTTPServer>(MaybeDispatchRequestToWorker); |
1230 | | |
1231 | 1.13k | if (!g_http_server->InitHTTPAllowList()) { |
1232 | 1 | return false; |
1233 | 1 | } |
1234 | | |
1235 | 1.13k | g_http_server->SetServerTimeout(std::chrono::seconds(gArgs.GetIntArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT))); |
1236 | | |
1237 | | // Bind HTTP server to specified addresses |
1238 | 1.13k | std::vector<std::pair<std::string, uint16_t>> endpoints{GetBindAddresses()}; |
1239 | 1.13k | bool bind_success{false}; |
1240 | 2.26k | for (const auto& [address_string, port] : endpoints) { |
1241 | 2.26k | LogInfo("Binding RPC on address %s port %i", address_string, port); |
1242 | 2.26k | const std::optional<CService> addr{Lookup(address_string, port, false)}; |
1243 | 2.26k | if (addr) { |
1244 | 2.26k | if (addr->IsBindAny()) { |
1245 | 0 | LogWarning("The RPC server is not safe to expose to untrusted networks such as the public internet"); |
1246 | 0 | } |
1247 | 2.26k | auto result{g_http_server->BindAndStartListening(addr.value())}; |
1248 | 2.26k | if (!result) { |
1249 | 0 | LogWarning("Binding RPC on address %s failed: %s", addr->ToStringAddrPort(), result.error()); |
1250 | 2.26k | } else { |
1251 | 2.26k | bind_success = true; |
1252 | 2.26k | } |
1253 | 2.26k | } else { |
1254 | 0 | LogWarning("Could not bind RPC on address %s port %i: Address lookup failed.", address_string, port); |
1255 | 0 | } |
1256 | 2.26k | } |
1257 | | |
1258 | 1.13k | if (!bind_success) { |
1259 | 0 | LogError("Unable to bind any endpoint for RPC server"); |
1260 | 0 | return false; |
1261 | 0 | } |
1262 | | |
1263 | 1.13k | LogDebug(BCLog::HTTP, "Initialized HTTP server"); |
1264 | | |
1265 | 1.13k | g_max_queue_depth = std::max(gArgs.GetArg<int>("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1); |
1266 | 1.13k | LogDebug(BCLog::HTTP, "set work queue of depth %d\n", g_max_queue_depth); |
1267 | | |
1268 | 1.13k | return true; |
1269 | 1.13k | } |
1270 | | |
1271 | | void StartHTTPServer() |
1272 | 1.12k | { |
1273 | 1.12k | auto rpcThreads{std::max(gArgs.GetArg<int>("-rpcthreads", DEFAULT_HTTP_THREADS), 1)}; |
1274 | 1.12k | LogInfo("Starting HTTP server with %d worker threads", rpcThreads); |
1275 | 1.12k | g_threadpool_http.Start(rpcThreads); |
1276 | 1.12k | g_http_server->StartSocketsThreads(); |
1277 | 1.12k | } |
1278 | | |
1279 | | void InterruptHTTPServer() |
1280 | 1.17k | { |
1281 | 1.17k | LogDebug(BCLog::HTTP, "Interrupting HTTP server"); |
1282 | 1.17k | if (g_http_server) { |
1283 | | // Reject all new requests |
1284 | 1.13k | g_http_server->SetRequestHandler(RejectRequest); |
1285 | 1.13k | } |
1286 | | |
1287 | | // Interrupt pool after disabling requests |
1288 | 1.17k | g_threadpool_http.Interrupt(); |
1289 | 1.17k | } |
1290 | | |
1291 | | void StopHTTPServer() |
1292 | 1.17k | { |
1293 | 1.17k | LogDebug(BCLog::HTTP, "Stopping HTTP server"); |
1294 | | |
1295 | 1.17k | LogDebug(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n"); |
1296 | 1.17k | g_threadpool_http.Stop(); |
1297 | | |
1298 | 1.17k | if (g_http_server) { |
1299 | | // Must precede DisconnectAllClients(): a connection accepted after |
1300 | | // GetConnectionsCount() returns 0 would survive into the destructor. |
1301 | 1.13k | g_http_server->StopAccepting(); |
1302 | | // Disconnect clients as their remaining responses are flushed |
1303 | 1.13k | g_http_server->DisconnectAllClients(); |
1304 | | // Wait 30 seconds for all disconnections |
1305 | 1.13k | LogDebug(BCLog::HTTP, "Waiting for HTTP clients to disconnect gracefully"); |
1306 | 1.13k | const auto deadline{NodeClock::now() + 30s}; |
1307 | 2.13k | while (g_http_server->GetConnectionsCount() != 0) { |
1308 | 1.00k | if (NodeClock::now() > deadline) { |
1309 | 0 | LogWarning("Timeout waiting for HTTP clients to disconnect gracefully, continuing shutdown"); |
1310 | 0 | break; |
1311 | 0 | } |
1312 | 1.00k | std::this_thread::sleep_for(50ms); |
1313 | 1.00k | } |
1314 | | // Break HTTPServer I/O loop: stop accepting connections, sending and receiving data |
1315 | 1.13k | g_http_server->InterruptNet(); |
1316 | | // Wait for HTTPServer I/O thread to exit |
1317 | 1.13k | g_http_server->JoinSocketsThreads(); |
1318 | | // Force-remove any clients that survived the graceful wait |
1319 | 1.13k | g_http_server->ClearConnectedClients(); |
1320 | | // Close all listening sockets |
1321 | 1.13k | g_http_server->StopListening(); |
1322 | 1.13k | } |
1323 | 1.17k | LogDebug(BCLog::HTTP, "Stopped HTTP server"); |
1324 | 1.17k | } |
1325 | | } // namespace http_bitcoin |