Bitcoin Core 31.99.0
P2P Digital Currency
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
HTTPRemoteClient Class Reference

#include <httpserver.h>

Collaboration diagram for HTTPRemoteClient:
[legend]

Public Member Functions

 HTTPRemoteClient (HTTPServer::Id id, const CService &addr, std::unique_ptr< Sock > socket)
 
 HTTPRemoteClient (const HTTPRemoteClient &)=delete
 
HTTPRemoteClientoperator= (const HTTPRemoteClient &)=delete
 
const std::string & GetOrigin () const
 
const CServiceGetPeer () const
 
std::shared_ptr< SockGetSock () EXCLUSIVE_LOCKS_REQUIRED(!m_sock_mutex)
 
bool ReadyToSend () const EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
 
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. More...
 
const std::string & GetRecvBuffer () const
 Used for tests. More...
 
const HTTPRequestGetRequest () const
 

Static Public Member Functions

static std::unique_ptr< HTTPRequestTryReadRequest (const std::shared_ptr< HTTPRemoteClient > &client)
 Try to read an HTTPRequest from a client's receive buffer. More...
 

Public Attributes

void !m_sock_mutex
 
bool !m_sock_mutex
 

Protected Member Functions

std::string & MutateRecvBuffer ()
 Used for tests. More...
 

Private Member Functions

void ReadRequest (HTTPRequest &req)
 Try to read an HTTP request from the receive buffer. More...
 
bool m_send_ready GUARDED_BY (m_send_mutex)
 Set true by worker threads after writing a response to m_send_buffer. More...
 
std::shared_ptr< Sock > m_sock GUARDED_BY (m_sock_mutex)
 Underlying socket. More...
 

Private Attributes

const HTTPServer::Id m_id
 ID provided by HTTPServer upon connection and instantiation. More...
 
const CService m_addr
 Remote address of connected client. More...
 
const std::string m_origin
 IP:port of connected client, cached for logging purposes. More...
 
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. More...
 
std::unique_ptr< HTTPRequestm_req
 Requests from a client must be processed in the order in which they were received, blocking on a per-client basis. More...
 
std::atomic_bool m_req_busy {false}
 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. More...
 
Mutex m_sock_mutex
 Mutex that serializes the Send() and Recv() calls on m_sock. More...
 
std::atomic_bool m_connection_busy {true}
 Initialized to true while server waits for first request from client. More...
 
std::atomic_bool m_keep_alive {false}
 Client has requested to keep the connection open after all requests have been responded to. More...
 
std::atomic_bool m_disconnect {false}
 Flag this client for disconnection on next loop. More...
 
std::atomic< SteadySecondsm_idle_since
 Timestamp of last send or receive activity, used for -rpcservertimeout. More...
 
Mutex m_send_mutex
 Response data destined for this client. More...
 
std::vector< std::byte > m_send_buffer GUARDED_BY (m_send_mutex)
 

Detailed Description

Definition at line 491 of file httpserver.h.

Constructor & Destructor Documentation

◆ HTTPRemoteClient() [1/2]

HTTPRemoteClient::HTTPRemoteClient ( HTTPServer::Id  id,
const CService addr,
std::unique_ptr< Sock socket 
)
inlineexplicit

Definition at line 494 of file httpserver.h.

◆ HTTPRemoteClient() [2/2]

HTTPRemoteClient::HTTPRemoteClient ( const HTTPRemoteClient )
delete

Member Function Documentation

◆ GetOrigin()

const std::string & HTTPRemoteClient::GetOrigin ( ) const
inline

Definition at line 501 of file httpserver.h.

◆ GetPeer()

const CService & HTTPRemoteClient::GetPeer ( ) const
inline

Definition at line 502 of file httpserver.h.

◆ GetRecvBuffer()

const std::string & HTTPRemoteClient::GetRecvBuffer ( ) const
inline

Used for tests.

Definition at line 527 of file httpserver.h.

◆ GetRequest()

const HTTPRequest * HTTPRemoteClient::GetRequest ( ) const
inline

Definition at line 528 of file httpserver.h.

◆ GetSock()

std::shared_ptr< Sock > HTTPRemoteClient::GetSock ( )
inline

Definition at line 503 of file httpserver.h.

◆ GUARDED_BY() [1/3]

std::vector< std::byte > m_send_buffer HTTPRemoteClient::GUARDED_BY ( m_send_mutex  )
private

◆ GUARDED_BY() [2/3]

bool m_send_ready HTTPRemoteClient::GUARDED_BY ( m_send_mutex  )
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 586 of file httpserver.h.

◆ GUARDED_BY() [3/3]

std::shared_ptr< Sock > m_sock HTTPRemoteClient::GUARDED_BY ( m_sock_mutex  )
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.

See also
https://github.com/bitcoin/bitcoin/issues/21744 for details.

◆ MaybeDisconnect()

bool HTTPRemoteClient::MaybeDisconnect ( std::chrono::time_point< SteadyClock now,
std::chrono::seconds  rpcservertimeout,
bool  disconnect_all 
)

Definition at line 1112 of file httpserver.cpp.

◆ MaybeSendBytesFromBuffer()

bool HTTPRemoteClient::MaybeSendBytesFromBuffer ( )

Push data (if there is any) from client's m_send_buffer to the connected socket.

Returns
false if we are done with this client and HTTPServer can skip the next read operation from it.

Definition at line 1212 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MutateRecvBuffer()

std::string & HTTPRemoteClient::MutateRecvBuffer ( )
inlineprotected

Used for tests.

Definition at line 533 of file httpserver.h.

◆ operator=()

HTTPRemoteClient & HTTPRemoteClient::operator= ( const HTTPRemoteClient )
delete

◆ ReadRequest()

void HTTPRemoteClient::ReadRequest ( HTTPRequest req)
private

Try to read an HTTP request from the receive buffer.

Updates HTTPRequest.m_state and drains buffer on error.

Parameters
[in]reqA HTTPRequest to read into
Exceptions
std::runtime_errorif request is unreadable or violates protocol

Definition at line 1169 of file httpserver.cpp.

Here is the call graph for this function:

◆ ReadyToSend()

bool HTTPRemoteClient::ReadyToSend ( ) const
inline

Definition at line 504 of file httpserver.h.

◆ Receive()

void HTTPRemoteClient::Receive ( )

Definition at line 919 of file httpserver.cpp.

Here is the call graph for this function:

◆ Send()

void HTTPRemoteClient::Send ( const HTTPResponse res,
std::span< const std::byte >  reply_body,
bool  keep_alive 
)

Definition at line 606 of file httpserver.cpp.

Here is the call graph for this function:

◆ TryReadRequest()

std::unique_ptr< HTTPRequest > HTTPRemoteClient::TryReadRequest ( const std::shared_ptr< HTTPRemoteClient > &  client)
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 1041 of file httpserver.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ !m_sock_mutex [1/2]

void HTTPRemoteClient::!m_sock_mutex

Definition at line 506 of file httpserver.h.

◆ !m_sock_mutex [2/2]

bool HTTPRemoteClient::!m_sock_mutex

Definition at line 523 of file httpserver.h.

◆ m_addr

const CService HTTPRemoteClient::m_addr
private

Remote address of connected client.

Definition at line 548 of file httpserver.h.

◆ m_connection_busy

std::atomic_bool HTTPRemoteClient::m_connection_busy {true}
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 610 of file httpserver.h.

◆ m_disconnect

std::atomic_bool HTTPRemoteClient::m_disconnect {false}
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 622 of file httpserver.h.

◆ m_id

const HTTPServer::Id HTTPRemoteClient::m_id
private

ID provided by HTTPServer upon connection and instantiation.

Definition at line 545 of file httpserver.h.

◆ m_idle_since

std::atomic<SteadySeconds> HTTPRemoteClient::m_idle_since
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 627 of file httpserver.h.

◆ m_keep_alive

std::atomic_bool HTTPRemoteClient::m_keep_alive {false}
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 615 of file httpserver.h.

◆ m_origin

const std::string HTTPRemoteClient::m_origin
private

IP:port of connected client, cached for logging purposes.

Definition at line 551 of file httpserver.h.

◆ m_recv_buffer

std::string HTTPRemoteClient::m_recv_buffer {}
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 558 of file httpserver.h.

◆ m_req

std::unique_ptr<HTTPRequest> HTTPRemoteClient::m_req
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 563 of file httpserver.h.

◆ m_req_busy

std::atomic_bool HTTPRemoteClient::m_req_busy {false}
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.

Definition at line 567 of file httpserver.h.

◆ m_send_mutex

Mutex HTTPRemoteClient::m_send_mutex
mutableprivate

Response data destined for this client.

Written to by http worker threads, read and erased by HTTPServer I/O thread

Definition at line 574 of file httpserver.h.

◆ m_sock_mutex

Mutex HTTPRemoteClient::m_sock_mutex
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 593 of file httpserver.h.


The documentation for this class was generated from the following files: