Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

httpserver.cpp File Reference
#include <httpserver.h>
#include <init.h>
#include <chainparamsbase.h>
#include <compat.h>
#include <util.h>
#include <utilstrencodings.h>
#include <netbase.h>
#include <rpc/protocol.h>
#include <sync.h>
#include <ui_interface.h>
#include <memory>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <event2/thread.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
#include <support/events.h>
#include <thread>
#include <mutex>
#include <condition_variable>
+ 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
 

Macros

#define EVENT_LOG_WARN   _EVENT_LOG_WARN
 

Functions

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...
 
static 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 bool ThreadHTTP (struct event_base *base, struct evhttp *http)
 Event dispatcher thread. More...
 
static bool HTTPBindAddresses (struct evhttp *http)
 Bind HTTP server to specified addresses. More...
 
static void HTTPWorkQueueRun (WorkQueue< HTTPClosure > *queue)
 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 ()
 Initialize HTTP server. More...
 
bool UpdateHTTPServerLogging (bool enable)
 Change logging level for libevent. More...
 
bool 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)
 
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::string urlDecode (const std::string &urlEncoded)
 

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...
 
struct evhttp * eventHTTP = nullptr
 HTTP server. More...
 
static std::vector< CSubNetrpc_allow_subnets
 List of subnets to allow RPC connections from. More...
 
static WorkQueue< HTTPClosure > * workQueue = nullptr
 Work queue for handling longer requests off the event loop thread. More...
 
std::vector< HTTPPathHandlerpathHandlers
 Handlers for (sub)paths. More...
 
std::vector< evhttp_bound_socket * > boundSockets
 Bound listening sockets. More...
 
std::thread threadHTTP
 
static std::vector< std::thread > g_thread_http_workers
 

Macro Definition Documentation

◆ EVENT_LOG_WARN

#define EVENT_LOG_WARN   _EVENT_LOG_WARN

Referenced by libevent_log_cb().

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 157 of file httpserver.cpp.

References CNetAddr::IsValid(), and rpc_allow_subnets.

Referenced by http_request_cb().

◆ EventBase()

struct event_base* EventBase ( )

Return evhttp event base.

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

Definition at line 487 of file httpserver.cpp.

References eventBase.

Referenced by StartHTTPRPC().

◆ 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 279 of file httpserver.cpp.

References BCLog::HTTP, and LogPrint.

Referenced by InterruptHTTPServer().

◆ http_request_cb()

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

HTTP request callback.

Definition at line 217 of file httpserver.cpp.

References ClientAllowed(), BCLog::HTTP, HTTP_FORBIDDEN, LogPrint, LogPrintf, pathHandlers, RequestMethodString(), HTTPRequest::UNKNOWN, and workQueue.

Referenced by InitHTTPServer().

◆ HTTPBindAddresses()

static bool HTTPBindAddresses ( struct evhttp *  http)
static

Bind HTTP server to specified addresses.

Definition at line 297 of file httpserver.cpp.

References BaseParams(), boundSockets, gArgs, ArgsManager::GetArg(), ArgsManager::GetArgs(), BCLog::HTTP, ArgsManager::IsArgSet(), LogPrint, LogPrintf, and SplitHostPort().

Referenced by InitHTTPServer().

◆ httpevent_callback_fn()

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

Definition at line 492 of file httpserver.cpp.

References HTTPEvent::handler.

Referenced by HTTPEvent::HTTPEvent().

◆ HTTPWorkQueueRun()

static void HTTPWorkQueueRun ( WorkQueue< HTTPClosure > *  queue)
static

Simple wrapper to set thread name and run work queue.

Definition at line 335 of file httpserver.cpp.

References RenameThread(), and WorkQueue< WorkItem >::Run().

Referenced by StartHTTPServer().

◆ InitHTTPAllowList()

static bool InitHTTPAllowList ( )
static

◆ InitHTTPServer()

◆ InterruptHTTPServer()

void InterruptHTTPServer ( )

Interrupt HTTP server threads.

Definition at line 443 of file httpserver.cpp.

References eventHTTP, BCLog::HTTP, http_reject_request_cb(), LogPrint, and workQueue.

Referenced by Interrupt().

◆ libevent_log_cb()

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

libevent event log callback

Definition at line 342 of file httpserver.cpp.

References EVENT_LOG_WARN, and LogPrintf.

Referenced by InitHTTPServer().

◆ 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 645 of file httpserver.cpp.

References handler, BCLog::HTTP, LogPrint, pathHandlers, and prefix.

Referenced by StartHTTPRPC(), and StartREST().

◆ RequestMethodString()

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

HTTP request method as string - use for logging only.

Definition at line 196 of file httpserver.cpp.

References HTTPRequest::GET, HTTPRequest::HEAD, HTTPRequest::POST, and HTTPRequest::PUT.

Referenced by http_request_cb().

◆ StartHTTPServer()

bool 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 430 of file httpserver.cpp.

References DEFAULT_HTTP_THREADS, eventBase, eventHTTP, g_thread_http_workers, gArgs, ArgsManager::GetArg(), BCLog::HTTP, HTTPWorkQueueRun(), L, LogPrint, LogPrintf, ThreadHTTP(), threadHTTP, and workQueue.

Referenced by AppInitServers().

◆ StopHTTPServer()

void StopHTTPServer ( )

Stop HTTP server.

Definition at line 454 of file httpserver.cpp.

References boundSockets, eventBase, eventHTTP, g_thread_http_workers, BCLog::HTTP, LogPrint, threadHTTP, and workQueue.

Referenced by PrepareShutdown().

◆ ThreadHTTP()

static bool ThreadHTTP ( struct event_base *  base,
struct evhttp *  http 
)
static

Event dispatcher thread.

Definition at line 286 of file httpserver.cpp.

References BCLog::HTTP, LogPrint, and RenameThread().

Referenced by StartHTTPServer().

◆ UnregisterHTTPHandler()

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

Unregister handler for prefix.

Definition at line 651 of file httpserver.cpp.

References BCLog::HTTP, LogPrint, pathHandlers, and prefix.

Referenced by StopHTTPRPC(), and StopREST().

◆ UpdateHTTPServerLogging()

bool UpdateHTTPServerLogging ( bool  enable)

Change logging level for libevent.

Removes BCLog::LIBEVENT from logCategories if libevent doesn't support debug logging.

Definition at line 413 of file httpserver.cpp.

Referenced by InitHTTPServer(), and logging().

◆ urlDecode()

std::string urlDecode ( const std::string &  urlEncoded)

Definition at line 665 of file httpserver.cpp.

Referenced by GetWalletForJSONRPCRequest().

Variable Documentation

◆ boundSockets

std::vector<evhttp_bound_socket *> boundSockets

Bound listening sockets.

Definition at line 154 of file httpserver.cpp.

Referenced by HTTPBindAddresses(), and StopHTTPServer().

◆ eventBase

struct event_base* eventBase = nullptr
static

HTTP module state.

libevent event loop

Definition at line 144 of file httpserver.cpp.

Referenced by EventBase(), InitHTTPServer(), StartHTTPServer(), StopHTTPServer(), and HTTPRequest::WriteReply().

◆ eventHTTP

struct evhttp* eventHTTP = nullptr

HTTP server.

Definition at line 146 of file httpserver.cpp.

Referenced by InitHTTPServer(), InterruptHTTPServer(), StartHTTPServer(), and StopHTTPServer().

◆ g_thread_http_workers

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

Definition at line 428 of file httpserver.cpp.

Referenced by StartHTTPServer(), and StopHTTPServer().

◆ MAX_HEADERS_SIZE

const size_t MAX_HEADERS_SIZE = 8192
static

Maximum size of http request (request line + headers)

Definition at line 46 of file httpserver.cpp.

Referenced by InitHTTPServer().

◆ pathHandlers

std::vector<HTTPPathHandler> pathHandlers

Handlers for (sub)paths.

Definition at line 152 of file httpserver.cpp.

Referenced by http_request_cb(), RegisterHTTPHandler(), and UnregisterHTTPHandler().

◆ rpc_allow_subnets

std::vector<CSubNet> rpc_allow_subnets
static

List of subnets to allow RPC connections from.

Definition at line 148 of file httpserver.cpp.

Referenced by ClientAllowed(), and InitHTTPAllowList().

◆ threadHTTP

std::thread threadHTTP

Definition at line 427 of file httpserver.cpp.

Referenced by StartHTTPServer(), and StopHTTPServer().

◆ workQueue

WorkQueue<HTTPClosure>* workQueue = nullptr
static

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

Definition at line 150 of file httpserver.cpp.

Referenced by http_request_cb(), InitHTTPServer(), InterruptHTTPServer(), StartHTTPServer(), and StopHTTPServer().

Released under the MIT license