Dash Core  0.12.2.1
P2P Digital Currency
httpserver.cpp File Reference
#include "httpserver.h"
#include "chainparamsbase.h"
#include "compat.h"
#include "util.h"
#include "netbase.h"
#include "rpc/protocol.h"
#include "sync.h"
#include "ui_interface.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/thread.h>
#include <event2/buffer.h>
#include <event2/util.h>
#include <event2/keyvalq_struct.h>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/foreach.hpp>
#include <boost/scoped_ptr.hpp>

Go to the source code of this file.

Classes

class  HTTPWorkItem
 
class  WorkQueue< WorkItem >
 
class  WorkQueue< WorkItem >::ThreadCounter
 
struct  HTTPPathHandler
 

Macros

#define EVENT_LOG_WARN   _EVENT_LOG_WARN
 

Functions

static bool ClientAllowed (const CNetAddr &netaddr)
 
static bool InitHTTPAllowList ()
 
static std::string RequestMethodString (HTTPRequest::RequestMethod m)
 
static void http_request_cb (struct evhttp_request *req, void *arg)
 
static void http_reject_request_cb (struct evhttp_request *req, void *)
 
static void ThreadHTTP (struct event_base *base, struct evhttp *http)
 
static bool HTTPBindAddresses (struct evhttp *http)
 
static void HTTPWorkQueueRun (WorkQueue< HTTPClosure > *queue)
 
static void libevent_log_cb (int severity, const char *msg)
 
bool InitHTTPServer ()
 
bool StartHTTPServer ()
 
void InterruptHTTPServer ()
 
void StopHTTPServer ()
 
struct event_base * EventBase ()
 
static void httpevent_callback_fn (evutil_socket_t, short, void *data)
 
void RegisterHTTPHandler (const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
 
void UnregisterHTTPHandler (const std::string &prefix, bool exactMatch)
 

Variables

static const size_t MAX_HEADERS_SIZE = 8192
 
static struct event_base * eventBase = 0
 libevent event loop More...
 
struct evhttp * eventHTTP = 0
 HTTP server. More...
 
static std::vector< CSubNetrpc_allow_subnets
 List of subnets to allow RPC connections from. More...
 
static WorkQueue< HTTPClosure > * workQueue = 0
 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...
 
boost::thread threadHTTP
 

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

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

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

Referenced by InterruptHTTPServer().

◆ http_request_cb()

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

HTTP request callback

Definition at line 259 of file httpserver.cpp.

Referenced by InitHTTPServer().

◆ HTTPBindAddresses()

static bool HTTPBindAddresses ( struct evhttp *  http)
static

Bind HTTP server to specified addresses

Definition at line 326 of file httpserver.cpp.

Referenced by InitHTTPServer().

◆ httpevent_callback_fn()

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

Definition at line 528 of file httpserver.cpp.

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

Referenced by StartHTTPServer().

◆ InitHTTPAllowList()

static bool InitHTTPAllowList ( )
static

Initialize ACL list for HTTP server

Definition at line 207 of file httpserver.cpp.

Referenced by InitHTTPServer().

◆ InitHTTPServer()

bool InitHTTPServer ( )

Initialize HTTP server. Call this before RegisterHTTPHandler or EventBase().

Definition at line 384 of file httpserver.cpp.

Referenced by AppInitServers().

◆ InterruptHTTPServer()

void InterruptHTTPServer ( )

Interrupt HTTP server threads

Definition at line 465 of file httpserver.cpp.

Referenced by Interrupt().

◆ libevent_log_cb()

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

libevent event log callback

Definition at line 372 of file httpserver.cpp.

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

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

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

Referenced by AppInitServers().

◆ StopHTTPServer()

void StopHTTPServer ( )

Stop HTTP server

Definition at line 480 of file httpserver.cpp.

Referenced by PrepareShutdown().

◆ ThreadHTTP()

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

Event dispatcher thread

Definition at line 316 of file httpserver.cpp.

Referenced by StartHTTPServer().

◆ UnregisterHTTPHandler()

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

Unregister handler for prefix

Definition at line 671 of file httpserver.cpp.

Referenced by StopHTTPRPC(), and StopREST().

Variable Documentation

◆ boundSockets

std::vector<evhttp_bound_socket *> boundSockets

Bound listening sockets.

Definition at line 193 of file httpserver.cpp.

Referenced by HTTPBindAddresses(), and InterruptHTTPServer().

◆ eventBase

struct event_base* eventBase = 0
static

libevent event loop

HTTP module state

Definition at line 183 of file httpserver.cpp.

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

◆ eventHTTP

struct evhttp* eventHTTP = 0

HTTP server.

Definition at line 185 of file httpserver.cpp.

Referenced by InitHTTPServer(), InterruptHTTPServer(), 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 42 of file httpserver.cpp.

Referenced by InitHTTPServer().

◆ pathHandlers

std::vector<HTTPPathHandler> pathHandlers

Handlers for (sub)paths.

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

Referenced by ClientAllowed(), and InitHTTPAllowList().

◆ threadHTTP

boost::thread threadHTTP

Definition at line 451 of file httpserver.cpp.

Referenced by StartHTTPServer(), and StopHTTPServer().

◆ workQueue

WorkQueue<HTTPClosure>* workQueue = 0
static

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

Definition at line 189 of file httpserver.cpp.

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