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