Bitcoin Core 31.99.0
P2P Digital Currency
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
HTTPServer Class Reference

#include <httpserver.h>

Collaboration diagram for HTTPServer:
[legend]

Classes

struct  IOReadiness
 Info about which socket has which event ready and a reverse map back to the HTTPRemoteClient that owns the socket. More...
 

Public Types

using Id = uint64_t
 Each connection is assigned an unique id of this type. More...
 

Public Member Functions

 HTTPServer (std::function< void(std::unique_ptr< HTTPRequest > &&)> func)
 
virtual ~HTTPServer ()
 
bool InitHTTPAllowList ()
 Parse the user's -rpcallowip settings and populate m_allow_subnets. More...
 
util::Expected< void, std::string > BindAndStartListening (const CService &to)
 Bind to a new address:port, start listening and add the listen socket to m_listen. More...
 
void StopListening ()
 Stop listening by closing all listening sockets. More...
 
size_t GetListeningSocketCount () const
 Get the number of sockets the server is bound to and listening on. More...
 
size_t GetConnectionsCount () const
 Get the number of HTTPRemoteClients we are connected to. More...
 
void StartSocketsThreads ()
 Start the necessary threads for sockets IO. More...
 
void JoinSocketsThreads ()
 Join (wait for) the threads started by StartSocketsThreads() to exit. More...
 
void InterruptNet ()
 Stop network activity. More...
 
void DisconnectAllClients ()
 Start disconnecting clients when possible in the I/O loop. More...
 
void SetRequestHandler (std::function< void(std::unique_ptr< HTTPRequest > &&)> func) EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex)
 Update the request handler method. More...
 
void StopAccepting ()
 Stop accepting new connections in the I/O loop. More...
 
void SetServerTimeout (std::chrono::seconds seconds)
 Set the idle client timeout (-rpcservertimeout) More...
 
void SetMaxConnections (int max_conn)
 Set the maximum amount of connected HTTPClients (-rpcmaxconnections) More...
 
void ClearConnectedClients ()
 Force-remove all remaining clients from m_connected without waiting for graceful disconnection. More...
 

Private Member Functions

bool ClientAllowed (const CNetAddr &netaddr) const
 Check an incoming connection's source IP against the allow list. More...
 
std::unique_ptr< SockAcceptConnection (const Sock &listen_sock, CService &addr)
 Accept a connection. More...
 
Id GetNewId ()
 Generate an id for a newly created connection. More...
 
void NewSockAccepted (std::unique_ptr< Sock > &&sock, const CService &addr)
 After a new socket with a client has been created, configure its flags, make a new HTTPRemoteClient and Id and save its shared pointer. More...
 
void SocketHandlerConnected (const IOReadiness &io_readiness) const EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex)
 Do the read/write for connected sockets that are ready for IO. More...
 
void SocketHandlerListening (const Sock::EventsPerSock &events_per_sock)
 Accept incoming connections, one from each read-ready listening socket. More...
 
IOReadiness GenerateWaitSockets () const
 Generate a collection of sockets to check for IO readiness. More...
 
void ThreadSocketHandler () EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex)
 Check connected and listening sockets for IO readiness and process them accordingly. More...
 
void DisconnectClients ()
 Close underlying socket connections for flagged clients by removing their shared pointer from m_connected. More...
 

Private Attributes

std::vector< std::shared_ptr< Sock > > m_listen
 List of listening sockets. More...
 
std::atomic< Idm_next_id {0}
 The id to assign to the next created connection. More...
 
std::vector< std::shared_ptr< HTTPRemoteClient > > m_connected
 List of HTTPRemoteClients with connected sockets. More...
 
std::atomic_bool m_stop_accepting {false}
 Flag used during shutdown to stop accepting new connections. More...
 
std::atomic_bool m_disconnect_all_clients {false}
 Flag used during shutdown. More...
 
std::atomic< size_t > m_connected_size {0}
 The number of connected sockets. More...
 
CThreadInterrupt m_interrupt_net
 This is signaled when network activity should cease. More...
 
std::thread m_thread_socket_handler
 Thread that sends to and receives from sockets and accepts connections. More...
 
std::chrono::seconds m_rpcservertimeout {DEFAULT_HTTP_SERVER_TIMEOUT}
 Idle timeout after which clients are disconnected. More...
 
std::vector< CSubNetm_allow_subnets
 List of subnets to allow HTTP connections from. More...
 
int m_rpcmaxconnections {DEFAULT_MAX_HTTP_CONNECTIONS}
 Maximum amount of concurrent connections. More...
 
Mutex m_request_dispatcher_mutex
 
std::function< void(std::unique_ptr< HTTPRequest > &&)> m_request_dispatcher GUARDED_BY (m_request_dispatcher_mutex)
 

Detailed Description

Definition at line 229 of file httpserver.h.

Member Typedef Documentation

◆ Id

using HTTPServer::Id = uint64_t

Each connection is assigned an unique id of this type.

Definition at line 235 of file httpserver.h.

Constructor & Destructor Documentation

◆ HTTPServer()

HTTPServer::HTTPServer ( std::function< void(std::unique_ptr< HTTPRequest > &&)>  func)
inlineexplicit

Definition at line 237 of file httpserver.h.

◆ ~HTTPServer()

virtual HTTPServer::~HTTPServer ( )
inlinevirtual

Definition at line 240 of file httpserver.h.

Member Function Documentation

◆ AcceptConnection()

std::unique_ptr< Sock > HTTPServer::AcceptConnection ( const Sock listen_sock,
CService addr 
)
private

Accept a connection.

Parameters
[in]listen_sockSocket on which to accept the connection.
[out]addrAddress of the peer that was accepted.
Returns
Newly created socket for the accepted connection.

Definition at line 808 of file httpserver.cpp.

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

◆ BindAndStartListening()

util::Expected< void, std::string > HTTPServer::BindAndStartListening ( const CService to)

Bind to a new address:port, start listening and add the listen socket to m_listen.

Parameters
[in]toWhere to bind.
Returns
{} or the reason for failure.

Definition at line 708 of file httpserver.cpp.

Here is the call graph for this function:

◆ ClearConnectedClients()

void HTTPServer::ClearConnectedClients ( )

Force-remove all remaining clients from m_connected without waiting for graceful disconnection.

Must only be called after JoinSocketsThreads().

Definition at line 1160 of file httpserver.cpp.

◆ ClientAllowed()

bool HTTPServer::ClientAllowed ( const CNetAddr netaddr) const
private

Check an incoming connection's source IP against the allow list.

Check if a network address is allowed to access the HTTP server.

Definition at line 79 of file httpserver.cpp.

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

◆ DisconnectAllClients()

void HTTPServer::DisconnectAllClients ( )
inline

Start disconnecting clients when possible in the I/O loop.

Definition at line 292 of file httpserver.h.

◆ DisconnectClients()

void HTTPServer::DisconnectClients ( )
private

Close underlying socket connections for flagged clients by removing their shared pointer from m_connected.

If an HTTPRemoteClient is busy in a worker thread, its connection will be closed once that job is done.

Definition at line 1097 of file httpserver.cpp.

Here is the caller graph for this function:

◆ GenerateWaitSockets()

HTTPServer::IOReadiness HTTPServer::GenerateWaitSockets ( ) const
private

Generate a collection of sockets to check for IO readiness.

Returns
Sockets to check for readiness plus an aux map to find the corresponding HTTPRemoteClient given a socket.

Definition at line 982 of file httpserver.cpp.

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

◆ GetConnectionsCount()

size_t HTTPServer::GetConnectionsCount ( ) const
inline

Get the number of HTTPRemoteClients we are connected to.

Definition at line 272 of file httpserver.h.

Here is the caller graph for this function:

◆ GetListeningSocketCount()

size_t HTTPServer::GetListeningSocketCount ( ) const
inline

Get the number of sockets the server is bound to and listening on.

Definition at line 267 of file httpserver.h.

◆ GetNewId()

HTTPServer::Id HTTPServer::GetNewId ( )
private

Generate an id for a newly created connection.

Definition at line 846 of file httpserver.cpp.

Here is the caller graph for this function:

◆ GUARDED_BY()

std::function< void(std::unique_ptr< HTTPRequest > &&)> m_request_dispatcher HTTPServer::GUARDED_BY ( m_request_dispatcher_mutex  )
private

◆ InitHTTPAllowList()

bool HTTPServer::InitHTTPAllowList ( )

Parse the user's -rpcallowip settings and populate m_allow_subnets.

Initialize ACL list for HTTP server.

Definition at line 90 of file httpserver.cpp.

Here is the call graph for this function:

◆ InterruptNet()

void HTTPServer::InterruptNet ( )
inline

Stop network activity.

Definition at line 287 of file httpserver.h.

◆ JoinSocketsThreads()

void HTTPServer::JoinSocketsThreads ( )

Join (wait for) the threads started by StartSocketsThreads() to exit.

Definition at line 801 of file httpserver.cpp.

◆ NewSockAccepted()

void HTTPServer::NewSockAccepted ( std::unique_ptr< Sock > &&  sock,
const CService addr 
)
private

After a new socket with a client has been created, configure its flags, make a new HTTPRemoteClient and Id and save its shared pointer.

Parameters
[in]sockThe newly created socket.
[in]addrAddress of the new peer.

Definition at line 851 of file httpserver.cpp.

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

◆ SetMaxConnections()

void HTTPServer::SetMaxConnections ( int  max_conn)
inline

Set the maximum amount of connected HTTPClients (-rpcmaxconnections)

Definition at line 321 of file httpserver.h.

◆ SetRequestHandler()

void HTTPServer::SetRequestHandler ( std::function< void(std::unique_ptr< HTTPRequest > &&)>  func)
inline

Update the request handler method.

Used for shutdown to reject new requests.

Definition at line 298 of file httpserver.h.

◆ SetServerTimeout()

void HTTPServer::SetServerTimeout ( std::chrono::seconds  seconds)
inline

Set the idle client timeout (-rpcservertimeout)

Definition at line 316 of file httpserver.h.

◆ SocketHandlerConnected()

void HTTPServer::SocketHandlerConnected ( const IOReadiness io_readiness) const
private

Do the read/write for connected sockets that are ready for IO.

Parameters
[in]io_readinessWhich sockets are ready and their corresponding HTTPRemoteClients.

Definition at line 878 of file httpserver.cpp.

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

◆ SocketHandlerListening()

void HTTPServer::SocketHandlerListening ( const Sock::EventsPerSock events_per_sock)
private

Accept incoming connections, one from each read-ready listening socket.

Parameters
[in]events_per_sockSockets that are ready for IO.

Definition at line 960 of file httpserver.cpp.

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

◆ StartSocketsThreads()

void HTTPServer::StartSocketsThreads ( )

Start the necessary threads for sockets IO.

Definition at line 789 of file httpserver.cpp.

Here is the call graph for this function:

◆ StopAccepting()

void HTTPServer::StopAccepting ( )
inline

Stop accepting new connections in the I/O loop.

Must be called first in StopHTTPServer() before DisconnectAllClients(). A connection accepted after the "wait for 0 connections" loop exits would remain in m_connected when the destructor is called.

Definition at line 311 of file httpserver.h.

◆ StopListening()

void HTTPServer::StopListening ( )

Stop listening by closing all listening sockets.

Definition at line 784 of file httpserver.cpp.

◆ ThreadSocketHandler()

void HTTPServer::ThreadSocketHandler ( )
private

Check connected and listening sockets for IO readiness and process them accordingly.

This is the main I/O loop of the server.

Definition at line 1015 of file httpserver.cpp.

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

Member Data Documentation

◆ m_allow_subnets

std::vector<CSubNet> HTTPServer::m_allow_subnets
private

List of subnets to allow HTTP connections from.

Definition at line 421 of file httpserver.h.

◆ m_connected

std::vector<std::shared_ptr<HTTPRemoteClient> > HTTPServer::m_connected
private

List of HTTPRemoteClients with connected sockets.

Connections will only be added and removed in the I/O thread, but weak pointers may be passed to worker threads to handle requests and send replies.

Definition at line 346 of file httpserver.h.

◆ m_connected_size

std::atomic<size_t> HTTPServer::m_connected_size {0}
private

The number of connected sockets.

Updated from the I/O thread but safely readable from the main thread without locks.

Definition at line 366 of file httpserver.h.

◆ m_disconnect_all_clients

std::atomic_bool HTTPServer::m_disconnect_all_clients {false}
private

Flag used during shutdown.

Overrides HTTPRemoteClient flags m_keep_alive and m_connection_busy. Set by main thread and read by the I/O thread.

Definition at line 359 of file httpserver.h.

◆ m_interrupt_net

CThreadInterrupt HTTPServer::m_interrupt_net
private

This is signaled when network activity should cease.

Definition at line 395 of file httpserver.h.

◆ m_listen

std::vector<std::shared_ptr<Sock> > HTTPServer::m_listen
private

List of listening sockets.

Definition at line 333 of file httpserver.h.

◆ m_next_id

std::atomic<Id> HTTPServer::m_next_id {0}
private

The id to assign to the next created connection.

Definition at line 338 of file httpserver.h.

◆ m_request_dispatcher_mutex

Mutex HTTPServer::m_request_dispatcher_mutex
mutableprivate

Definition at line 409 of file httpserver.h.

◆ m_rpcmaxconnections

int HTTPServer::m_rpcmaxconnections {DEFAULT_MAX_HTTP_CONNECTIONS}
private

Maximum amount of concurrent connections.

Definition at line 431 of file httpserver.h.

◆ m_rpcservertimeout

std::chrono::seconds HTTPServer::m_rpcservertimeout {DEFAULT_HTTP_SERVER_TIMEOUT}
private

Idle timeout after which clients are disconnected.

Definition at line 416 of file httpserver.h.

◆ m_stop_accepting

std::atomic_bool HTTPServer::m_stop_accepting {false}
private

Flag used during shutdown to stop accepting new connections.

Set by main thread and read by the I/O thread.

Definition at line 352 of file httpserver.h.

◆ m_thread_socket_handler

std::thread HTTPServer::m_thread_socket_handler
private

Thread that sends to and receives from sockets and accepts connections.

Executes the I/O loop of the server.

Definition at line 401 of file httpserver.h.


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