Bitcoin Core 31.99.0
P2P Digital Currency
Classes | Functions | Variables
httpserver.cpp File Reference
#include <bitcoin-build-config.h>
#include <httpserver.h>
#include <chainparamsbase.h>
#include <common/args.h>
#include <common/messages.h>
#include <common/url.h>
#include <compat/compat.h>
#include <logging.h>
#include <netbase.h>
#include <node/interface_ui.h>
#include <rpc/protocol.h>
#include <span.h>
#include <sync.h>
#include <util/check.h>
#include <util/signalinterrupt.h>
#include <util/sock.h>
#include <util/strencodings.h>
#include <util/thread.h>
#include <util/threadnames.h>
#include <util/threadpool.h>
#include <util/time.h>
#include <util/translation.h>
#include <condition_variable>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <thread>
#include <unordered_map>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
Include dependency graph for httpserver.cpp:

Go to the source code of this file.

Classes

struct  HTTPPathHandler
 

Functions

static std::vector< HTTPPathHandler > pathHandlers GUARDED_BY (g_httppathhandlers_mutex)
 
std::string_view RequestMethodString (HTTPRequestMethod m)
 HTTP request method as string - use for logging only. More...
 
static void WriteNoStoreErrorReply (HTTPRequest &req, HTTPStatusCode status, std::string_view reply={})
 
static void MaybeDispatchRequestToWorker (std::shared_ptr< HTTPRequest > hreq)
 
static void RejectRequest (std::unique_ptr< HTTPRequest > hreq)
 
static std::vector< std::pair< std::string, uint16_t > > GetBindAddresses ()
 
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...
 
std::optional< std::string > GetQueryParameterFromUri (const std::string_view uri, const std::string_view key)
 
bool InitHTTPServer ()
 Initialize HTTP server. More...
 
void StartHTTPServer ()
 Start HTTP server. More...
 
void InterruptHTTPServer ()
 Interrupt HTTP server threads. More...
 
void StopHTTPServer ()
 Stop HTTP server. More...
 

Variables

static constexpr auto SELECT_TIMEOUT {50ms}
 The set of sockets cannot be modified while waiting, so the sleep time needs to be small to avoid new sockets stalling. More...
 
static constexpr int SOCKET_OPTION_TRUE {1}
 Explicit alias for setting socket option methods. More...
 
static std::unique_ptr< HTTPServerg_http_server {nullptr}
 HTTP module state. More...
 
static GlobalMutex g_httppathhandlers_mutex
 Handlers for (sub)paths. More...
 
static ThreadPool g_threadpool_http ("http")
 Http thread pool - future: encapsulate in HttpContext More...
 
static int g_max_queue_depth {100}
 

Function Documentation

◆ GetBindAddresses()

static std::vector< std::pair< std::string, uint16_t > > GetBindAddresses ( )
static

Definition at line 208 of file httpserver.cpp.

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

◆ GetQueryParameterFromUri()

std::optional< std::string > GetQueryParameterFromUri ( const std::string_view  uri,
const std::string_view  key 
)

Definition at line 673 of file httpserver.cpp.

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

◆ GUARDED_BY()

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

◆ InitHTTPServer()

bool InitHTTPServer ( )

Initialize HTTP server.

Call this before RegisterHTTPHandler or EventBase().

Definition at line 1298 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 1352 of file httpserver.cpp.

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

◆ MaybeDispatchRequestToWorker()

static void MaybeDispatchRequestToWorker ( std::shared_ptr< HTTPRequest hreq)
static

Definition at line 136 of file httpserver.cpp.

Here is the call graph for this function:
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 241 of file httpserver.cpp.

Here is the caller graph for this function:

◆ RejectRequest()

static void RejectRequest ( std::unique_ptr< HTTPRequest hreq)
static

Definition at line 202 of file httpserver.cpp.

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

◆ RequestMethodString()

std::string_view RequestMethodString ( HTTPRequestMethod  m)

HTTP request method as string - use for logging only.

Definition at line 117 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 1344 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 1364 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 248 of file httpserver.cpp.

Here is the caller graph for this function:

◆ WriteNoStoreErrorReply()

static void WriteNoStoreErrorReply ( HTTPRequest req,
HTTPStatusCode  status,
std::string_view  reply = {} 
)
static

Definition at line 130 of file httpserver.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ g_http_server

std::unique_ptr<HTTPServer> g_http_server {nullptr}
static

HTTP module state.

Definition at line 69 of file httpserver.cpp.

◆ g_httppathhandlers_mutex

GlobalMutex g_httppathhandlers_mutex
static

Handlers for (sub)paths.

Definition at line 71 of file httpserver.cpp.

◆ g_max_queue_depth

int g_max_queue_depth {100}
static

Definition at line 76 of file httpserver.cpp.

◆ g_threadpool_http

ThreadPool g_threadpool_http("http") ( "http"  )
static

Http thread pool - future: encapsulate in HttpContext

◆ SELECT_TIMEOUT

constexpr auto SELECT_TIMEOUT {50ms}
staticconstexpr

The set of sockets cannot be modified while waiting, so the sleep time needs to be small to avoid new sockets stalling.

Definition at line 47 of file httpserver.cpp.

◆ SOCKET_OPTION_TRUE

constexpr int SOCKET_OPTION_TRUE {1}
staticconstexpr

Explicit alias for setting socket option methods.

Definition at line 50 of file httpserver.cpp.