/root/bitcoin/src/httpserver.h
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 | | #ifndef BITCOIN_HTTPSERVER_H |
6 | | #define BITCOIN_HTTPSERVER_H |
7 | | |
8 | | #include <atomic> |
9 | | #include <deque> |
10 | | #include <functional> |
11 | | #include <memory> |
12 | | #include <optional> |
13 | | #include <span> |
14 | | #include <string> |
15 | | #include <vector> |
16 | | |
17 | | #include <netaddress.h> |
18 | | #include <rpc/protocol.h> |
19 | | #include <util/expected.h> |
20 | | #include <util/sock.h> |
21 | | #include <util/strencodings.h> |
22 | | #include <util/string.h> |
23 | | #include <util/time.h> |
24 | | |
25 | | namespace util { |
26 | | class SignalInterrupt; |
27 | | } // namespace util |
28 | | |
29 | | /** |
30 | | * The default value for `-rpcthreads`. This number of threads will be created at startup. |
31 | | */ |
32 | | static const int DEFAULT_HTTP_THREADS=16; |
33 | | |
34 | | /** |
35 | | * The default value for `-rpcworkqueue`. This is the maximum depth of the work queue, |
36 | | * we don't allocate this number of work queue items upfront. |
37 | | */ |
38 | | static const int DEFAULT_HTTP_WORKQUEUE=64; |
39 | | |
40 | | static const int DEFAULT_HTTP_SERVER_TIMEOUT=30; |
41 | | |
42 | | enum class HTTPRequestMethod { |
43 | | UNKNOWN, |
44 | | GET, |
45 | | POST, |
46 | | HEAD, |
47 | | PUT |
48 | | }; |
49 | | |
50 | | namespace http_bitcoin { |
51 | | |
52 | | using util::LineReader; |
53 | | |
54 | | //! Shortest valid request line, used by libevent in evhttp_parse_request_line() |
55 | | constexpr size_t MIN_REQUEST_LINE_LENGTH = std::string_view("GET / HTTP/1.0").size(); |
56 | | |
57 | | //! Maximum size of each headers line in an HTTP request. |
58 | | //! See https://github.com/bitcoin/bitcoin/pull/6859 |
59 | | //! And libevent http.c evhttp_parse_headers_() |
60 | | constexpr size_t MAX_HEADERS_SIZE{8192}; |
61 | | |
62 | | class HTTPHeaders |
63 | | { |
64 | | public: |
65 | | /** |
66 | | * @param[in] key The field-name of the header to search for |
67 | | * @returns The value of the first header that matches the provided key |
68 | | * nullopt if key is not found |
69 | | */ |
70 | | std::optional<std::string> FindFirst(std::string_view key) const; |
71 | | void Write(std::string&& key, std::string&& value); |
72 | | /** |
73 | | * @param[in] key The field-name of the header to search for and delete |
74 | | */ |
75 | | void RemoveAll(std::string_view key); |
76 | | /** |
77 | | * @returns false if LineReader hits the end of the buffer before reading an |
78 | | * \n, meaning that we are still waiting on more data from the client. |
79 | | * true after reading an entire HTTP headers section, terminated |
80 | | * by an empty line and \n. |
81 | | * @throws on exceeded read limit and on bad headers syntax (e.g. no ":" in a line) |
82 | | */ |
83 | | bool Read(util::LineReader& reader); |
84 | | std::string Stringify() const; |
85 | | |
86 | | private: |
87 | | /** |
88 | | * Headers can have duplicate field names, so we use a vector of key-value pairs instead of a map. |
89 | | * https://httpwg.org/specs/rfc9110.html#rfc.section.5.2 |
90 | | */ |
91 | | std::vector<std::pair<std::string, std::string>> m_headers; |
92 | | }; |
93 | | |
94 | | class HTTPResponse |
95 | | { |
96 | | public: |
97 | | uint8_t m_version_major; |
98 | | uint8_t m_version_minor; |
99 | | HTTPStatusCode m_status; |
100 | | std::string m_reason; |
101 | | HTTPHeaders m_headers; |
102 | | std::vector<std::byte> m_body; |
103 | | bool m_keep_alive{false}; |
104 | | |
105 | | std::string StringifyHeaders() const; |
106 | | }; |
107 | | |
108 | | class HTTPClient; |
109 | | |
110 | | class HTTPRequest |
111 | | { |
112 | | public: |
113 | | HTTPRequestMethod m_method; |
114 | | std::string m_target; |
115 | | |
116 | | /** |
117 | | * Default HTTP protocol version 1.1 is used by error responses |
118 | | * when a request is unreadable. |
119 | | */ |
120 | | /// @{ |
121 | | uint8_t m_version_major{1}; |
122 | | uint8_t m_version_minor{1}; |
123 | | /// @} |
124 | | |
125 | | HTTPHeaders m_headers; |
126 | | std::string m_body; |
127 | | |
128 | | //! Pointer to the client that made the request so we know who to respond to. |
129 | | std::shared_ptr<HTTPClient> m_client; |
130 | | |
131 | | //! Response headers may be set in advance before response body is known |
132 | | HTTPHeaders m_response_headers; |
133 | | |
134 | 0 | explicit HTTPRequest(std::shared_ptr<HTTPClient> client) : m_client{std::move(client)} {} |
135 | | //! Construct with a null client for unit tests |
136 | 24.9k | explicit HTTPRequest() : m_client{} {} |
137 | | |
138 | | /** |
139 | | * Methods that attempt to parse HTTP request fields line-by-line |
140 | | * from a receive buffer. |
141 | | * @param[in] reader A LineReader object constructed over a span of data. |
142 | | * @returns true If the request field was parsed. |
143 | | * false If there was not enough data in the buffer to complete the field. |
144 | | * @throws std::runtime_error if data is invalid. |
145 | | */ |
146 | | /// @{ |
147 | | bool LoadControlData(LineReader& reader); |
148 | | bool LoadHeaders(LineReader& reader); |
149 | | bool LoadBody(LineReader& reader); |
150 | | /// @} |
151 | | |
152 | | void WriteReply(HTTPStatusCode status, std::span<const std::byte> reply_body = {}); |
153 | | void WriteReply(HTTPStatusCode status, std::string_view reply_body_view) |
154 | 0 | { |
155 | 0 | WriteReply(status, std::as_bytes(std::span{reply_body_view})); |
156 | 0 | } |
157 | | |
158 | | // These methods reimplement the API from http_libevent::HTTPRequest |
159 | | // for downstream JSONRPC and REST modules. |
160 | 12.4k | std::string GetURI() const { return m_target; } |
161 | | CService GetPeer() const; |
162 | 12.4k | HTTPRequestMethod GetRequestMethod() const { return m_method; } |
163 | | std::optional<std::string> GetQueryParameter(std::string_view key) const; |
164 | | std::pair<bool, std::string> GetHeader(std::string_view hdr) const; |
165 | 12.4k | std::string ReadBody() const { return m_body; } |
166 | | void WriteHeader(std::string&& hdr, std::string&& value); |
167 | | }; |
168 | | |
169 | | class HTTPServer |
170 | | { |
171 | | public: |
172 | | /** |
173 | | * Each connection is assigned an unique id of this type. |
174 | | */ |
175 | | using Id = int64_t; |
176 | | |
177 | 0 | explicit HTTPServer(std::function<void(std::unique_ptr<HTTPRequest>&&)> func) : m_request_dispatcher{std::move(func)} {} |
178 | | |
179 | | virtual ~HTTPServer() |
180 | 0 | { |
181 | 0 | Assume(!m_thread_socket_handler.joinable()); // Missing call to JoinSocketsThreads() Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
182 | 0 | Assume(m_connected.empty()); // Missing call to DisconnectClients(), or disconnect flags not set Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
183 | 0 | Assume(m_listen.empty()); // Missing call to StopListening() Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
184 | 0 | } |
185 | | |
186 | | /** |
187 | | * Bind to a new address:port, start listening and add the listen socket to `m_listen`. |
188 | | * @param[in] to Where to bind. |
189 | | * @returns {} or the reason for failure. |
190 | | */ |
191 | | util::Expected<void, std::string> BindAndStartListening(const CService& to); |
192 | | |
193 | | /** |
194 | | * Stop listening by closing all listening sockets. |
195 | | */ |
196 | | void StopListening(); |
197 | | |
198 | | /** |
199 | | * Get the number of sockets the server is bound to and listening on |
200 | | */ |
201 | 0 | size_t GetListeningSocketCount() const { return m_listen.size(); } |
202 | | |
203 | | /** |
204 | | * Get the number of HTTPClients we are connected to |
205 | | */ |
206 | 0 | size_t GetConnectionsCount() const { return m_connected_size.load(std::memory_order_acquire); } |
207 | | |
208 | | /** |
209 | | * Start the necessary threads for sockets IO. |
210 | | */ |
211 | | void StartSocketsThreads(); |
212 | | |
213 | | /** |
214 | | * Join (wait for) the threads started by `StartSocketsThreads()` to exit. |
215 | | */ |
216 | | void JoinSocketsThreads(); |
217 | | |
218 | | /** |
219 | | * Stop network activity |
220 | | */ |
221 | 0 | void InterruptNet() { m_interrupt_net(); } |
222 | | |
223 | | /** |
224 | | * Update the request handler method. |
225 | | * Used for shutdown to reject new requests. |
226 | | */ |
227 | 0 | void SetRequestHandler(std::function<void(std::unique_ptr<HTTPRequest>&&)> func) { m_request_dispatcher = func; } |
228 | | |
229 | | /** |
230 | | * Start disconnecting clients when possible in the I/O loop |
231 | | */ |
232 | 0 | void DisconnectAllClients() { m_disconnect_all_clients = true; } |
233 | | |
234 | | /** |
235 | | * Set the idle client timeout (-rpcservertimeout) |
236 | | */ |
237 | 0 | void SetServerTimeout(std::chrono::seconds seconds) { m_rpcservertimeout = seconds; } |
238 | | |
239 | | private: |
240 | | /** |
241 | | * List of listening sockets. |
242 | | */ |
243 | | std::vector<std::shared_ptr<Sock>> m_listen; |
244 | | |
245 | | /** |
246 | | * The id to assign to the next created connection. |
247 | | */ |
248 | | std::atomic<Id> m_next_id{0}; |
249 | | |
250 | | /** |
251 | | * List of HTTPClients with connected sockets. |
252 | | * Connections will only be added and removed in the I/O thread, but |
253 | | * shared pointers may be passed to worker threads to handle requests |
254 | | * and send replies. |
255 | | */ |
256 | | std::vector<std::shared_ptr<HTTPClient>> m_connected; |
257 | | |
258 | | /** |
259 | | * Flag used during shutdown. |
260 | | * Overrides HTTPClient flags m_keep_alive and m_connection_busy. |
261 | | * Set by main thread and read by the I/O thread. |
262 | | */ |
263 | | std::atomic_bool m_disconnect_all_clients{false}; |
264 | | |
265 | | /** |
266 | | * The number of connected sockets. |
267 | | * Updated from the I/O thread but safely readable from |
268 | | * the main thread without locks. |
269 | | */ |
270 | | std::atomic<size_t> m_connected_size{0}; |
271 | | |
272 | | /** |
273 | | * Info about which socket has which event ready and a reverse map |
274 | | * back to the HTTPClient that owns the socket. |
275 | | */ |
276 | | struct IOReadiness { |
277 | | /** |
278 | | * Map of socket -> socket events. For example: |
279 | | * socket1 -> { requested = SEND|RECV, occurred = RECV } |
280 | | * socket2 -> { requested = SEND, occurred = SEND } |
281 | | */ |
282 | | Sock::EventsPerSock events_per_sock; |
283 | | |
284 | | /** |
285 | | * Map of socket -> HTTPClient. For example: |
286 | | * socket1 -> HTTPClient{ id=23 } |
287 | | * socket2 -> HTTPClient{ id=56 } |
288 | | */ |
289 | | std::unordered_map<Sock::EventsPerSock::key_type, |
290 | | std::shared_ptr<HTTPClient>, |
291 | | Sock::HashSharedPtrSock, |
292 | | Sock::EqualSharedPtrSock> |
293 | | httpclients_per_sock; |
294 | | }; |
295 | | |
296 | | /** |
297 | | * This is signaled when network activity should cease. |
298 | | */ |
299 | | CThreadInterrupt m_interrupt_net; |
300 | | |
301 | | /** |
302 | | * Thread that sends to and receives from sockets and accepts connections. |
303 | | * Executes the I/O loop of the server. |
304 | | */ |
305 | | std::thread m_thread_socket_handler; |
306 | | |
307 | | /* |
308 | | * What to do with HTTP requests once received, validated and parsed |
309 | | */ |
310 | | std::function<void(std::unique_ptr<HTTPRequest>&&)> m_request_dispatcher; |
311 | | |
312 | | /** |
313 | | * Idle timeout after which clients are disconnected |
314 | | */ |
315 | | std::chrono::seconds m_rpcservertimeout{DEFAULT_HTTP_SERVER_TIMEOUT}; |
316 | | |
317 | | /** |
318 | | * Accept a connection. |
319 | | * @param[in] listen_sock Socket on which to accept the connection. |
320 | | * @param[out] addr Address of the peer that was accepted. |
321 | | * @return Newly created socket for the accepted connection. |
322 | | */ |
323 | | std::unique_ptr<Sock> AcceptConnection(const Sock& listen_sock, CService& addr); |
324 | | |
325 | | /** |
326 | | * Generate an id for a newly created connection. |
327 | | */ |
328 | | Id GetNewId(); |
329 | | |
330 | | /** |
331 | | * After a new socket with a client has been created, configure its flags, |
332 | | * make a new HTTPClient and Id and save its shared pointer. |
333 | | * @param[in] sock The newly created socket. |
334 | | * @param[in] them Address of the new peer. |
335 | | */ |
336 | | void NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& them); |
337 | | |
338 | | /** |
339 | | * Do the read/write for connected sockets that are ready for IO. |
340 | | * @param[in] io_readiness Which sockets are ready and their corresponding HTTPClients. |
341 | | */ |
342 | | void SocketHandlerConnected(const IOReadiness& io_readiness) const; |
343 | | |
344 | | /** |
345 | | * Accept incoming connections, one from each read-ready listening socket. |
346 | | * @param[in] events_per_sock Sockets that are ready for IO. |
347 | | */ |
348 | | void SocketHandlerListening(const Sock::EventsPerSock& events_per_sock); |
349 | | |
350 | | /** |
351 | | * Generate a collection of sockets to check for IO readiness. |
352 | | * @return Sockets to check for readiness plus an aux map to find the |
353 | | * corresponding HTTPClient given a socket. |
354 | | */ |
355 | | IOReadiness GenerateWaitSockets() const; |
356 | | |
357 | | /** |
358 | | * Check connected and listening sockets for IO readiness and process them accordingly. |
359 | | * This is the main I/O loop of the server. |
360 | | */ |
361 | | void ThreadSocketHandler(); |
362 | | |
363 | | /** |
364 | | * Try to read HTTPRequests from a client's receive buffer. |
365 | | * Complete requests are dispatched, incomplete requests are |
366 | | * left in the buffer to wait for more data. Some read errors |
367 | | * will mark this client for disconnection. |
368 | | * @param[in] client The HTTPClient to read requests from |
369 | | */ |
370 | | void MaybeDispatchRequestsFromClient(const std::shared_ptr<HTTPClient>& client) const; |
371 | | |
372 | | /** |
373 | | * Close underlying socket connections for flagged clients |
374 | | * by removing their shared pointer from m_connected. If an HTTPClient |
375 | | * is busy in a worker thread, its connection will be closed once that |
376 | | * job is done and the HTTPRequest is out of scope. |
377 | | */ |
378 | | void DisconnectClients(); |
379 | | }; |
380 | | |
381 | | std::optional<std::string> GetQueryParameterFromUri(std::string_view uri, std::string_view key); |
382 | | |
383 | | class HTTPClient |
384 | | { |
385 | | public: |
386 | | //! ID provided by HTTPServer upon connection and instantiation |
387 | | const HTTPServer::Id m_id; |
388 | | |
389 | | //! Remote address of connected client |
390 | | const CService m_addr; |
391 | | |
392 | | //! IP:port of connected client, cached for logging purposes |
393 | | const std::string m_origin; |
394 | | |
395 | | /** |
396 | | * In lieu of an intermediate transport class like p2p uses, |
397 | | * we copy data from the socket buffer to the client object |
398 | | * and attempt to read HTTP requests from here. |
399 | | */ |
400 | | std::vector<std::byte> m_recv_buffer{}; |
401 | | |
402 | | //! Requests from a client must be processed in the order in which |
403 | | //! they were received, blocking on a per-client basis. We won't |
404 | | //! process the next request in the queue if we are currently busy |
405 | | //! handling a previous request. |
406 | | std::deque<std::unique_ptr<HTTPRequest>> m_req_queue; |
407 | | |
408 | | //! Set to true by the I/O thread when a request is popped off |
409 | | //! and passed to a worker thread, reset to false by the worker thread. |
410 | | std::atomic_bool m_req_busy{false}; |
411 | | |
412 | | /** |
413 | | * Response data destined for this client. |
414 | | * Written to by http worker threads, read and erased by HTTPServer I/O thread |
415 | | */ |
416 | | /// @{ |
417 | | Mutex m_send_mutex; |
418 | | std::vector<std::byte> m_send_buffer GUARDED_BY(m_send_mutex); |
419 | | /// @} |
420 | | |
421 | | /** |
422 | | * Set true by worker threads after writing a response to m_send_buffer. |
423 | | * Set false by the HTTPServer I/O thread after flushing m_send_buffer. |
424 | | * Checked in the HTTPServer I/O loop to avoid locking m_send_mutex if there's nothing to send. |
425 | | */ |
426 | | std::atomic_bool m_send_ready{false}; |
427 | | |
428 | | /** |
429 | | * Mutex that serializes the Send() and Recv() calls on `m_sock`. Reading |
430 | | * from the client occurs in the I/O thread but writing back to a client |
431 | | * may occur in a worker thread. |
432 | | */ |
433 | | Mutex m_sock_mutex; |
434 | | |
435 | | /** |
436 | | * Underlying socket. |
437 | | * `shared_ptr` (instead of `unique_ptr`) is used to avoid premature close of the |
438 | | * underlying file descriptor by one thread while another thread is poll(2)-ing |
439 | | * it for activity. |
440 | | * @see https://github.com/bitcoin/bitcoin/issues/21744 for details. |
441 | | */ |
442 | | std::shared_ptr<Sock> m_sock GUARDED_BY(m_sock_mutex); |
443 | | |
444 | | //! Initialized to true while server waits for first request from client. |
445 | | //! Set to false after data is written to m_send_buffer and then that buffer is flushed to client. |
446 | | //! Reset to true when we receive new request data from client. |
447 | | //! Checked during DisconnectClients() and set by read/write operations |
448 | | //! called in either the HTTPServer I/O loop or by a worker thread during an "optimistic send". |
449 | | //! `m_connection_busy=true` can be overridden by `m_disconnect=true` (we disconnect). |
450 | | std::atomic_bool m_connection_busy{true}; |
451 | | |
452 | | //! Client has requested to keep the connection open after all requests have been responded to. |
453 | | //! Set by (potentially multiple) worker threads and checked in the HTTPServer I/O loop. |
454 | | //! `m_keep_alive=true` can be overriden `by HTTPServer.m_disconnect_all_clients` (we disconnect). |
455 | | std::atomic_bool m_keep_alive{false}; |
456 | | |
457 | | //! Flag this client for disconnection on next loop. |
458 | | //! Either we have encountered a permanent error, or both sides of the socket are done |
459 | | //! with the connection, e.g. our reply to a "Connection: close" request has been sent. |
460 | | //! Might be set in a worker thread or in the I/O thread. When set to `true` we disconnect, |
461 | | //! possibly overriding all other disconnect flags. |
462 | | std::atomic_bool m_disconnect{false}; |
463 | | |
464 | | //! Timestamp of last send or receive activity, used for -rpcservertimeout. |
465 | | //! Due to optimistic sends it may be updated in either a worker thread or in the |
466 | | //! I/O thread. It is checked in the I/O thread to disconnect idle clients. |
467 | | Mutex m_idle_since_mutex; |
468 | | SteadySeconds m_idle_since GUARDED_BY(m_idle_since_mutex); |
469 | | |
470 | | explicit HTTPClient(HTTPServer::Id id, const CService& addr, std::unique_ptr<Sock> socket) |
471 | 0 | : m_id(id), m_addr(addr), m_origin(addr.ToStringAddrPort()), m_sock{std::move(socket)}, m_idle_since{Now<SteadySeconds>()} {} |
472 | | |
473 | | // Disable copies (should only be used as shared pointers) |
474 | | HTTPClient(const HTTPClient&) = delete; |
475 | | HTTPClient& operator=(const HTTPClient&) = delete; |
476 | | |
477 | | /** |
478 | | * Try to read an HTTP request from the receive buffer. |
479 | | * @param[in] req A pointer to an HTTPRequest to read |
480 | | * @returns true on success, false on failure. |
481 | | */ |
482 | | bool ReadRequest(const std::unique_ptr<HTTPRequest>& req); |
483 | | |
484 | | /** |
485 | | * Push data (if there is any) from client's m_send_buffer to the connected socket. |
486 | | * @returns false if we are done with this client and HTTPServer can skip the next read operation from it. |
487 | | */ |
488 | | bool MaybeSendBytesFromBuffer() EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex, !m_sock_mutex, !m_idle_since_mutex); |
489 | | }; |
490 | | |
491 | | /** Initialize HTTP server. |
492 | | * Call this before RegisterHTTPHandler or EventBase(). |
493 | | */ |
494 | | bool InitHTTPServer(); |
495 | | |
496 | | /** Start HTTP server. |
497 | | * This is separate from InitHTTPServer to give users race-condition-free time |
498 | | * to register their handlers between InitHTTPServer and StartHTTPServer. |
499 | | */ |
500 | | void StartHTTPServer(); |
501 | | |
502 | | /** Interrupt HTTP server threads */ |
503 | | void InterruptHTTPServer(); |
504 | | |
505 | | /** Stop HTTP server */ |
506 | | void StopHTTPServer(); |
507 | | } // namespace http_bitcoin |
508 | | |
509 | | /** Handler for requests to a certain HTTP path */ |
510 | | using HTTPRequestHandler = std::function<bool(http_bitcoin::HTTPRequest* req, const std::string&)>; |
511 | | |
512 | | /** Register handler for prefix. |
513 | | * If multiple handlers match a prefix, the first-registered one will |
514 | | * be invoked. |
515 | | */ |
516 | | void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler); |
517 | | |
518 | | /** Unregister handler for prefix */ |
519 | | void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch); |
520 | | |
521 | | #endif // BITCOIN_HTTPSERVER_H |