![]() |
Bitcoin Core 32.99.0
P2P Digital Currency
|
#include <httpserver.h>
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. | |
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. | |
| 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. | |
| void | StopListening () |
| Stop listening by closing all listening sockets. | |
| size_t | GetListeningSocketCount () const |
| Get the number of sockets the server is bound to and listening on. | |
| size_t | GetConnectionsCount () const |
| Get the number of HTTPRemoteClients we are connected to. | |
| void | StartSocketsThreads () |
| Start the necessary threads for sockets IO. | |
| void | JoinSocketsThreads () |
Join (wait for) the threads started by StartSocketsThreads() to exit. | |
| void | InterruptNet () |
| Stop network activity. | |
| void | DisconnectAllClients () |
| Start disconnecting clients when possible in the I/O loop. | |
| void | SetRequestHandler (std::function< void(std::unique_ptr< HTTPRequest > &&)> func) EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex) |
| Update the request handler method. | |
| void | StopAccepting () |
| Stop accepting new connections in the I/O loop. | |
| void | SetServerTimeout (std::chrono::seconds seconds) |
| Set the idle client timeout (-rpcservertimeout) | |
| void | SetMaxConnections (int max_conn) |
| Set the maximum amount of connected HTTPClients (-rpcmaxconnections) | |
| void | ClearConnectedClients () |
| Force-remove all remaining clients from m_connected without waiting for graceful disconnection. | |
Private Member Functions | |
| bool | ClientAllowed (const CNetAddr &netaddr) const |
| Check an incoming connection's source IP against the allow list. | |
| std::unique_ptr< Sock > | AcceptConnection (const Sock &listen_sock, CService &addr) |
| Accept a connection. | |
| Id | GetNewId () |
| Generate an id for a newly created connection. | |
| 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. | |
| 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. | |
| void | SocketHandlerListening (const Sock::EventsPerSock &events_per_sock) |
| Accept incoming connections, one from each read-ready listening socket. | |
| IOReadiness | GenerateWaitSockets () const |
| Generate a collection of sockets to check for IO readiness. | |
| void | ThreadSocketHandler () EXCLUSIVE_LOCKS_REQUIRED(!m_request_dispatcher_mutex) |
| Check connected and listening sockets for IO readiness and process them accordingly. | |
| void | DisconnectClients () |
| Close underlying socket connections for flagged clients by removing their shared pointer from m_connected. | |
Private Attributes | |
| std::vector< std::shared_ptr< Sock > > | m_listen |
| List of listening sockets. | |
| std::atomic< Id > | m_next_id {0} |
| The id to assign to the next created connection. | |
| std::vector< std::shared_ptr< HTTPRemoteClient > > | m_connected |
| List of HTTPRemoteClients with connected sockets. | |
| std::atomic_bool | m_stop_accepting {false} |
| Flag used during shutdown to stop accepting new connections. | |
| std::atomic_bool | m_disconnect_all_clients {false} |
| Flag used during shutdown. | |
| std::atomic< size_t > | m_connected_size {0} |
| The number of connected sockets. | |
| CThreadInterrupt | m_interrupt_net |
| This is signaled when network activity should cease. | |
| std::thread | m_thread_socket_handler |
| Thread that sends to and receives from sockets and accepts connections. | |
| std::chrono::seconds | m_rpcservertimeout {DEFAULT_HTTP_SERVER_TIMEOUT} |
| Idle timeout after which clients are disconnected. | |
| std::vector< CSubNet > | m_allow_subnets |
| List of subnets to allow HTTP connections from. | |
| int | m_rpcmaxconnections {DEFAULT_MAX_HTTP_CONNECTIONS} |
| Maximum amount of concurrent connections. | |
| Mutex | m_request_dispatcher_mutex |
| std::function< void(std::unique_ptr< HTTPRequest > &&)> m_request_dispatcher | GUARDED_BY (m_request_dispatcher_mutex) |
Definition at line 230 of file httpserver.h.
Each connection is assigned an unique id of this type.
Definition at line 236 of file httpserver.h.
|
inlineexplicit |
Definition at line 238 of file httpserver.h.
|
inlinevirtual |
Definition at line 241 of file httpserver.h.
|
private |
Accept a connection.
| [in] | listen_sock | Socket on which to accept the connection. |
| [out] | addr | Address of the peer that was accepted. |
Definition at line 820 of file httpserver.cpp.
| 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.
| [in] | to | Where to bind. |
Definition at line 708 of file httpserver.cpp.
| 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 1204 of file httpserver.cpp.
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.
|
inline |
Start disconnecting clients when possible in the I/O loop.
Definition at line 293 of file httpserver.h.
|
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 1150 of file httpserver.cpp.
|
private |
Generate a collection of sockets to check for IO readiness.
Definition at line 1004 of file httpserver.cpp.
|
inline |
Get the number of HTTPRemoteClients we are connected to.
Definition at line 273 of file httpserver.h.
|
inline |
Get the number of sockets the server is bound to and listening on.
Definition at line 268 of file httpserver.h.
|
private |
Generate an id for a newly created connection.
Definition at line 858 of file httpserver.cpp.
|
private |
| 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.
|
inline |
Stop network activity.
Definition at line 288 of file httpserver.h.
| void HTTPServer::JoinSocketsThreads | ( | ) |
Join (wait for) the threads started by StartSocketsThreads() to exit.
Definition at line 813 of file httpserver.cpp.
After a new socket with a client has been created, configure its flags, make a new HTTPRemoteClient and Id and save its shared pointer.
| [in] | sock | The newly created socket. |
| [in] | addr | Address of the new peer. |
Definition at line 863 of file httpserver.cpp.
Set the maximum amount of connected HTTPClients (-rpcmaxconnections)
Definition at line 322 of file httpserver.h.
|
inline |
Update the request handler method.
Used for shutdown to reject new requests.
Definition at line 299 of file httpserver.h.
|
inline |
Set the idle client timeout (-rpcservertimeout)
Definition at line 317 of file httpserver.h.
|
private |
Do the read/write for connected sockets that are ready for IO.
| [in] | io_readiness | Which sockets are ready and their corresponding HTTPRemoteClients. |
Definition at line 890 of file httpserver.cpp.
|
private |
Accept incoming connections, one from each read-ready listening socket.
| [in] | events_per_sock | Sockets that are ready for IO. |
Definition at line 982 of file httpserver.cpp.
| void HTTPServer::StartSocketsThreads | ( | ) |
Start the necessary threads for sockets IO.
Definition at line 801 of file httpserver.cpp.
|
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 312 of file httpserver.h.
| void HTTPServer::StopListening | ( | ) |
Stop listening by closing all listening sockets.
Definition at line 796 of file httpserver.cpp.
|
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 1059 of file httpserver.cpp.
|
private |
List of subnets to allow HTTP connections from.
Definition at line 422 of file httpserver.h.
|
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 347 of file httpserver.h.
|
private |
The number of connected sockets.
Updated from the I/O thread but safely readable from the main thread without locks.
Definition at line 367 of file httpserver.h.
|
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 360 of file httpserver.h.
|
private |
This is signaled when network activity should cease.
Definition at line 396 of file httpserver.h.
|
private |
List of listening sockets.
Definition at line 334 of file httpserver.h.
|
private |
The id to assign to the next created connection.
Definition at line 339 of file httpserver.h.
|
mutableprivate |
Definition at line 410 of file httpserver.h.
|
private |
Maximum amount of concurrent connections.
Definition at line 432 of file httpserver.h.
|
private |
Idle timeout after which clients are disconnected.
Definition at line 417 of file httpserver.h.
|
private |
Flag used during shutdown to stop accepting new connections.
Set by main thread and read by the I/O thread.
Definition at line 353 of file httpserver.h.
|
private |
Thread that sends to and receives from sockets and accepts connections.
Executes the I/O loop of the server.
Definition at line 402 of file httpserver.h.