![]() |
Bitcoin Core 32.1.0
P2P Digital Currency
|
#include <httpserver.h>
Public Member Functions | |
| HTTPRemoteClient (HTTPServer::Id id, const CService &addr, std::unique_ptr< Sock > socket) | |
| HTTPRemoteClient (const HTTPRemoteClient &)=delete | |
| HTTPRemoteClient & | operator= (const HTTPRemoteClient &)=delete |
| const std::string & | GetOrigin () const |
| const CService & | GetPeer () const |
| std::shared_ptr< Sock > | GetSock () EXCLUSIVE_LOCKS_REQUIRED(!m_sock_mutex) |
| bool | ReadyToSend () const EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex) |
| bool | ReceiveBufferEmpty () const |
| void | Send (const HTTPResponse &res, std::span< const std::byte > reply_body, bool keep_alive) EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex |
| void | Receive () EXCLUSIVE_LOCKS_REQUIRED(!m_sock_mutex) |
| bool | MaybeDisconnect (std::chrono::time_point< SteadyClock > now, std::chrono::seconds rpcservertimeout, bool disconnect_all) |
| bool | MaybeSendBytesFromBuffer () EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex |
| Push data (if there is any) from client's m_send_buffer to the connected socket. | |
| const HTTPRequest * | GetRequest () const |
| Used to determine if an incomplete request is in progress. | |
| const std::string & | GetRecvBuffer () const |
| Used for tests. | |
Static Public Member Functions | |
| static std::unique_ptr< HTTPRequest > | TryReadRequest (const std::shared_ptr< HTTPRemoteClient > &client) EXCLUSIVE_LOCKS_REQUIRED(!client -> m_send_mutex) |
| Try to read an HTTPRequest from a client's receive buffer. | |
Public Attributes | |
| void | !m_sock_mutex |
| bool | !m_sock_mutex |
Protected Member Functions | |
| std::string & | MutateRecvBuffer () |
| Used for tests. | |
Private Member Functions | |
| void | ReadRequest (HTTPRequest &req) |
| Try to read an HTTP request from the receive buffer. | |
| bool m_send_ready | GUARDED_BY (m_send_mutex) |
| Set true by worker threads after writing a response to m_send_buffer. | |
| std::shared_ptr< Sock > m_sock | GUARDED_BY (m_sock_mutex) |
| Underlying socket. | |
Private Attributes | |
| const HTTPServer::Id | m_id |
| ID provided by HTTPServer upon connection and instantiation. | |
| const CService | m_addr |
| Remote address of connected client. | |
| const std::string | m_origin |
| IP:port of connected client, cached for logging purposes. | |
| std::string | m_recv_buffer {} |
| In lieu of an intermediate transport class like p2p uses, we copy data from the socket buffer to the client object and attempt to read HTTP requests from here. | |
| uint64_t | m_total_bytes_received {0} |
| Running total of bytes read from this client's socket. | |
| std::unique_ptr< HTTPRequest > | m_req |
| std::atomic_bool | m_req_busy {false} |
| Mutex | m_sock_mutex |
| Mutex that serializes the Send() and Recv() calls on m_sock. | |
| std::atomic_bool | m_connection_busy {true} |
| std::atomic_bool | m_keep_alive {false} |
| std::atomic_bool | m_disconnect {false} |
| std::atomic< SteadySeconds > | m_idle_since |
| Mutex | m_send_mutex |
| Response data destined for this client. | |
| std::vector< std::byte > m_send_buffer | GUARDED_BY (m_send_mutex) |
Definition at line 492 of file httpserver.h.
|
inlineexplicit |
Definition at line 495 of file httpserver.h.
|
delete |
|
inline |
Definition at line 502 of file httpserver.h.
|
inline |
Definition at line 503 of file httpserver.h.
|
inline |
Used for tests.
Definition at line 535 of file httpserver.h.
|
inline |
Used to determine if an incomplete request is in progress.
Definition at line 532 of file httpserver.h.
|
inline |
Definition at line 504 of file httpserver.h.
|
inlineprivate |
Set true by worker threads after writing a response to m_send_buffer.
Set false by the HTTPServer I/O thread after flushing m_send_buffer. Checked in the HTTPServer I/O loop to decide whether to poll the socket for writeability or readability. Guarded by m_send_mutex so it stays consistent with m_send_buffer's emptiness: the two must always be updated together under the same lock.
Definition at line 596 of file httpserver.h.
|
private |
|
private |
Underlying socket.
shared_ptr (instead of unique_ptr) is used to avoid premature close of the underlying file descriptor by one thread while another thread is poll(2)-ing it for activity.
| bool HTTPRemoteClient::MaybeDisconnect | ( | std::chrono::time_point< SteadyClock > | now, |
| std::chrono::seconds | rpcservertimeout, | ||
| bool | disconnect_all ) |
| bool HTTPRemoteClient::MaybeSendBytesFromBuffer | ( | ) |
Push data (if there is any) from client's m_send_buffer to the connected socket.
Definition at line 1265 of file httpserver.cpp.
|
inlineprotected |
Used for tests.
Definition at line 539 of file httpserver.h.
|
delete |
|
private |
Try to read an HTTP request from the receive buffer.
Updates HTTPRequest.m_state and drains buffer on error.
| [in] | req | A HTTPRequest to read into |
| std::runtime_error | if request is unreadable or violates protocol |
Definition at line 1222 of file httpserver.cpp.
|
inline |
Definition at line 505 of file httpserver.h.
| void HTTPRemoteClient::Receive | ( | ) |
Definition at line 931 of file httpserver.cpp.
|
inline |
Definition at line 506 of file httpserver.h.
| void HTTPRemoteClient::Send | ( | const HTTPResponse & | res, |
| std::span< const std::byte > | reply_body, | ||
| bool | keep_alive ) |
Definition at line 606 of file httpserver.cpp.
|
static |
Try to read an HTTPRequest from a client's receive buffer.
Only complete requests are returned, incomplete requests are left in the buffer to wait for more data. Some read errors will mark this client for disconnection.
Definition at line 1085 of file httpserver.cpp.
| bool HTTPRemoteClient::!m_sock_mutex |
Definition at line 525 of file httpserver.h.
| void HTTPRemoteClient::!m_sock_mutex |
Definition at line 508 of file httpserver.h.
|
private |
Remote address of connected client.
Definition at line 554 of file httpserver.h.
|
private |
Initialized to true while server waits for first request from client. Set to false after data is written to m_send_buffer and then that buffer is flushed to client. Reset to true when we receive new request data from client. Checked during DisconnectClients() and set by read/write operations called in either the HTTPServer I/O loop or by a worker thread during an "optimistic send". m_connection_busy=true can be overridden by m_disconnect=true (we disconnect).
Definition at line 620 of file httpserver.h.
|
private |
Flag this client for disconnection on next loop. Either we have encountered a permanent error, or both sides of the socket are done with the connection, e.g. our reply to a "Connection: close" request has been sent. Might be set in a worker thread or in the I/O thread. When set to true we disconnect, possibly overriding all other disconnect flags.
Definition at line 632 of file httpserver.h.
|
private |
ID provided by HTTPServer upon connection and instantiation.
Definition at line 551 of file httpserver.h.
|
private |
Timestamp of last send or receive activity, used for -rpcservertimeout. Due to optimistic sends it may be updated in either a worker thread or in the I/O thread. It is checked in the I/O thread to disconnect idle clients.
Definition at line 637 of file httpserver.h.
|
private |
Client has requested to keep the connection open after all requests have been responded to. Set by (potentially multiple) worker threads and checked in the HTTPServer I/O loop. m_keep_alive=true can be overridden by HTTPServer.m_disconnect_all_clients (we disconnect).
Definition at line 625 of file httpserver.h.
|
private |
IP:port of connected client, cached for logging purposes.
Definition at line 557 of file httpserver.h.
|
private |
In lieu of an intermediate transport class like p2p uses, we copy data from the socket buffer to the client object and attempt to read HTTP requests from here.
Definition at line 564 of file httpserver.h.
|
private |
Requests from a client must be processed in the order in which they were received, blocking on a per-client basis. We read one request at a time from the socket buffer then pass it to a worker.
Definition at line 572 of file httpserver.h.
|
private |
Set to true by the I/O thread when a request is popped off and passed to a worker thread, reset to false by the worker thread. Only one request per connection is ever in flight.
Definition at line 577 of file httpserver.h.
|
mutableprivate |
Response data destined for this client.
Written to by http worker threads, read and erased by HTTPServer I/O thread
Definition at line 584 of file httpserver.h.
|
private |
Mutex that serializes the Send() and Recv() calls on m_sock.
Reading from the client occurs in the I/O thread but writing back to a client may occur in a worker thread.
Definition at line 603 of file httpserver.h.
|
private |
Running total of bytes read from this client's socket.
Definition at line 567 of file httpserver.h.