Bitcoin Core  27.99.0
P2P Digital Currency
Classes | Functions | Variables
httpserver.cpp File Reference
#include <config/bitcoin-config.h>
#include <httpserver.h>
#include <chainparamsbase.h>
#include <common/args.h>
#include <compat/compat.h>
#include <logging.h>
#include <netbase.h>
#include <node/interface_ui.h>
#include <rpc/protocol.h>
#include <sync.h>
#include <util/check.h>
#include <util/signalinterrupt.h>
#include <util/strencodings.h>
#include <util/threadnames.h>
#include <util/translation.h>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <unordered_map>
#include <sys/types.h>
#include <sys/stat.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/keyvalq_struct.h>
#include <event2/thread.h>
#include <event2/util.h>
#include <support/events.h>
Include dependency graph for httpserver.cpp:

Go to the source code of this file.

Classes

class  HTTPWorkItem
 HTTP request work item. More...
 
class  WorkQueue< WorkItem >
 Simple work queue for distributing work over multiple threads. More...
 
struct  HTTPPathHandler
 
class  HTTPRequestTracker
 Helps keep track of open evhttp_connections with active evhttp_requests More...
 

Functions

static std::vector< HTTPPathHandler > pathHandlers GUARDED_BY (g_httppathhandlers_mutex)
 
static bool ClientAllowed (const CNetAddr &netaddr)
 Check if a network address is allowed to access the HTTP server. More...
 
static bool InitHTTPAllowList ()
 Initialize ACL list for HTTP server. More...
 
std::string RequestMethodString (HTTPRequest::RequestMethod m)
 HTTP request method as string - use for logging only. More...
 
static void http_request_cb (struct evhttp_request *req, void *arg)
 HTTP request callback. More...
 
static void http_reject_request_cb (struct evhttp_request *req, void *)
 Callback to reject HTTP requests after shutdown. More...
 
static void ThreadHTTP (struct event_base *base)
 Event dispatcher thread. More...
 
static bool HTTPBindAddresses (struct evhttp *http)
 Bind HTTP server to specified addresses. More...
 
static void HTTPWorkQueueRun (WorkQueue< HTTPClosure > *queue, int worker_num)
 Simple wrapper to set thread name and run work queue. More...
 
static void libevent_log_cb (int severity, const char *msg)
 libevent event log callback More...
 
bool InitHTTPServer (const util::SignalInterrupt &interrupt)
 Initialize HTTP server. More...
 
void UpdateHTTPServerLogging (bool enable)
 Change logging level for libevent. More...
 
void StartHTTPServer ()
 Start HTTP server. More...
 
void InterruptHTTPServer ()
 Interrupt HTTP server threads. More...
 
void StopHTTPServer ()
 Stop HTTP server. More...
 
struct event_base * EventBase ()
 Return evhttp event base. More...
 
static void httpevent_callback_fn (evutil_socket_t, short, void *data)
 
std::optional< std::string > GetQueryParameterFromUri (const char *uri, const std::string &key)
 Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not found. More...
 
void RegisterHTTPHandler (const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
 Register handler for prefix. More...
 
void UnregisterHTTPHandler (const std::string &prefix, bool exactMatch)
 Unregister handler for prefix. More...
 

Variables

static const size_t MAX_HEADERS_SIZE = 8192
 Maximum size of http request (request line + headers) More...
 
static struct event_base * eventBase = nullptr
 HTTP module state. More...
 
static struct evhttp * eventHTTP = nullptr
 HTTP server. More...
 
static std::vector< CSubNetrpc_allow_subnets
 List of subnets to allow RPC connections from. More...
 
static std::unique_ptr< WorkQueue< HTTPClosure > > g_work_queue {nullptr}
 Work queue for handling longer requests off the event loop thread. More...
 
static GlobalMutex g_httppathhandlers_mutex
 Handlers for (sub)paths. More...
 
static std::vector< evhttp_bound_socket * > boundSockets
 Bound listening sockets. More...
 
static HTTPRequestTracker g_requests
 Track active requests. More...
 
static std::thread g_thread_http
 
static std::vector< std::thread > g_thread_http_workers
 

Function Documentation

◆ ClientAllowed()

static bool ClientAllowed ( const CNetAddr netaddr)
static

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

Definition at line 209 of file httpserver.cpp.

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

◆ EventBase()

struct event_base* EventBase ( )

Return evhttp event base.

This can be used by submodules to queue timers or custom events.

Definition at line 549 of file httpserver.cpp.

Here is the caller graph for this function:

◆ GetQueryParameterFromUri()

std::optional<std::string> GetQueryParameterFromUri ( const char *  uri,
const std::string &  key 
)

Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not found.

If the query string contains duplicate keys, the first value is returned. Many web frameworks would instead parse this as an array of values, but this is not (yet) implemented as it is currently not needed in any of the endpoints.

Helper function for HTTPRequest::GetQueryParameter.

Parameters
[in]uriis the entire request uri
[in]keyrepresents the query parameter of which the value is returned

Definition at line 716 of file httpserver.cpp.

Here is the caller graph for this function:

◆ GUARDED_BY()

static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY ( g_httppathhandlers_mutex  )
static

◆ http_reject_request_cb()

static void http_reject_request_cb ( struct evhttp_request *  req,
void *   
)
static

Callback to reject HTTP requests after shutdown.

Definition at line 341 of file httpserver.cpp.

Here is the caller graph for this function:

◆ http_request_cb()

static void http_request_cb ( struct evhttp_request *  req,
void *  arg 
)
static

HTTP request callback.

Definition at line 261 of file httpserver.cpp.

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

◆ HTTPBindAddresses()

static bool HTTPBindAddresses ( struct evhttp *  http)
static

Bind HTTP server to specified addresses.

Definition at line 358 of file httpserver.cpp.

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

◆ httpevent_callback_fn()

static void httpevent_callback_fn ( evutil_socket_t  ,
short  ,
void *  data 
)
static

Definition at line 554 of file httpserver.cpp.

Here is the caller graph for this function:

◆ HTTPWorkQueueRun()

static void HTTPWorkQueueRun ( WorkQueue< HTTPClosure > *  queue,
int  worker_num 
)
static

Simple wrapper to set thread name and run work queue.

Definition at line 400 of file httpserver.cpp.

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

◆ InitHTTPAllowList()

static bool InitHTTPAllowList ( )
static

Initialize ACL list for HTTP server.

Definition at line 220 of file httpserver.cpp.

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

◆ InitHTTPServer()

bool InitHTTPServer ( const util::SignalInterrupt interrupt)

Initialize HTTP server.

Call this before RegisterHTTPHandler or EventBase().

Definition at line 427 of file httpserver.cpp.

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

◆ InterruptHTTPServer()

void InterruptHTTPServer ( )

Interrupt HTTP server threads.

Definition at line 496 of file httpserver.cpp.

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

◆ libevent_log_cb()

static void libevent_log_cb ( int  severity,
const char *  msg 
)
static

libevent event log callback

Definition at line 407 of file httpserver.cpp.

Here is the caller graph for this function:

◆ RegisterHTTPHandler()

void RegisterHTTPHandler ( const std::string &  prefix,
bool  exactMatch,
const HTTPRequestHandler handler 
)

Register handler for prefix.

If multiple handlers match a prefix, the first-registered one will be invoked.

Definition at line 743 of file httpserver.cpp.

Here is the caller graph for this function:

◆ RequestMethodString()

std::string RequestMethodString ( HTTPRequest::RequestMethod  m)

HTTP request method as string - use for logging only.

Definition at line 243 of file httpserver.cpp.

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

◆ StartHTTPServer()

void StartHTTPServer ( )

Start HTTP server.

This is separate from InitHTTPServer to give users race-condition-free time to register their handlers between InitHTTPServer and StartHTTPServer.

Definition at line 485 of file httpserver.cpp.

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

◆ StopHTTPServer()

void StopHTTPServer ( )

Stop HTTP server.

Definition at line 508 of file httpserver.cpp.

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

◆ ThreadHTTP()

static void ThreadHTTP ( struct event_base *  base)
static

Event dispatcher thread.

Definition at line 348 of file httpserver.cpp.

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

◆ UnregisterHTTPHandler()

void UnregisterHTTPHandler ( const std::string &  prefix,
bool  exactMatch 
)

Unregister handler for prefix.

Definition at line 750 of file httpserver.cpp.

Here is the caller graph for this function:

◆ UpdateHTTPServerLogging()

void UpdateHTTPServerLogging ( bool  enable)

Change logging level for libevent.

Definition at line 474 of file httpserver.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ boundSockets

std::vector<evhttp_bound_socket *> boundSockets
static

Bound listening sockets.

Definition at line 151 of file httpserver.cpp.

◆ eventBase

struct event_base* eventBase = nullptr
static

HTTP module state.

libevent event loop

Definition at line 140 of file httpserver.cpp.

◆ eventHTTP

struct evhttp* eventHTTP = nullptr
static

HTTP server.

Definition at line 142 of file httpserver.cpp.

◆ g_httppathhandlers_mutex

GlobalMutex g_httppathhandlers_mutex
static

Handlers for (sub)paths.

Definition at line 148 of file httpserver.cpp.

◆ g_requests

HTTPRequestTracker g_requests
static

Track active requests.

Definition at line 206 of file httpserver.cpp.

◆ g_thread_http

std::thread g_thread_http
static

Definition at line 482 of file httpserver.cpp.

◆ g_thread_http_workers

std::vector<std::thread> g_thread_http_workers
static

Definition at line 483 of file httpserver.cpp.

◆ g_work_queue

std::unique_ptr<WorkQueue<HTTPClosure> > g_work_queue {nullptr}
static

Work queue for handling longer requests off the event loop thread.

Definition at line 146 of file httpserver.cpp.

◆ MAX_HEADERS_SIZE

const size_t MAX_HEADERS_SIZE = 8192
static

Maximum size of http request (request line + headers)

Definition at line 47 of file httpserver.cpp.

◆ rpc_allow_subnets

std::vector<CSubNet> rpc_allow_subnets
static

List of subnets to allow RPC connections from.

Definition at line 144 of file httpserver.cpp.