![]() |
Bitcoin Core 31.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. 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< Sock > | AcceptConnection (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< Id > | m_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< CSubNet > | m_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) |
Definition at line 229 of file httpserver.h.
| using HTTPServer::Id = uint64_t |
Each connection is assigned an unique id of this type.
Definition at line 235 of file httpserver.h.
|
inlineexplicit |
Definition at line 237 of file httpserver.h.
|
inlinevirtual |
Definition at line 240 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 808 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 1160 of file httpserver.cpp.
|
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.
|
inline |
Start disconnecting clients when possible in the I/O loop.
Definition at line 292 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 1097 of file httpserver.cpp.
|
private |
Generate a collection of sockets to check for IO readiness.
Definition at line 982 of file httpserver.cpp.
|
inline |
Get the number of HTTPRemoteClients we are connected to.
Definition at line 272 of file httpserver.h.
|
inline |
Get the number of sockets the server is bound to and listening on.
Definition at line 267 of file httpserver.h.
|
private |
Generate an id for a newly created connection.
Definition at line 846 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 287 of file httpserver.h.
| void HTTPServer::JoinSocketsThreads | ( | ) |
Join (wait for) the threads started by StartSocketsThreads() to exit.
Definition at line 801 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 851 of file httpserver.cpp.
|
inline |
Set the maximum amount of connected HTTPClients (-rpcmaxconnections)
Definition at line 321 of file httpserver.h.
|
inline |
Update the request handler method.
Used for shutdown to reject new requests.
Definition at line 298 of file httpserver.h.
|
inline |
Set the idle client timeout (-rpcservertimeout)
Definition at line 316 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 878 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 960 of file httpserver.cpp.
| void HTTPServer::StartSocketsThreads | ( | ) |
Start the necessary threads for sockets IO.
Definition at line 789 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 311 of file httpserver.h.
| void HTTPServer::StopListening | ( | ) |
Stop listening by closing all listening sockets.
Definition at line 784 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 1015 of file httpserver.cpp.
|
private |
List of subnets to allow HTTP connections from.
Definition at line 421 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 346 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 366 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 359 of file httpserver.h.
|
private |
This is signaled when network activity should cease.
Definition at line 395 of file httpserver.h.
|
private |
List of listening sockets.
Definition at line 333 of file httpserver.h.
|
private |
The id to assign to the next created connection.
Definition at line 338 of file httpserver.h.
|
mutableprivate |
Definition at line 409 of file httpserver.h.
|
private |
Maximum amount of concurrent connections.
Definition at line 431 of file httpserver.h.
|
private |
Idle timeout after which clients are disconnected.
Definition at line 416 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 352 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 401 of file httpserver.h.