Changeset View
Changeset View
Standalone View
Standalone View
src/httpserver.cpp
Show First 20 Lines • Show All 149 Lines • ▼ Show 20 Lines | struct HTTPPathHandler { | ||||
std::string prefix; | std::string prefix; | ||||
bool exactMatch; | bool exactMatch; | ||||
HTTPRequestHandler handler; | HTTPRequestHandler handler; | ||||
}; | }; | ||||
/** HTTP module state */ | /** HTTP module state */ | ||||
//! libevent event loop | //! libevent event loop | ||||
static struct event_base *eventBase = 0; | static struct event_base *eventBase = nullptr; | ||||
//! HTTP server | //! HTTP server | ||||
struct evhttp *eventHTTP = 0; | struct evhttp *eventHTTP = nullptr; | ||||
//! List of subnets to allow RPC connections from | //! List of subnets to allow RPC connections from | ||||
static std::vector<CSubNet> rpc_allow_subnets; | static std::vector<CSubNet> rpc_allow_subnets; | ||||
//! Work queue for handling longer requests off the event loop thread | //! Work queue for handling longer requests off the event loop thread | ||||
static WorkQueue<HTTPClosure> *workQueue = 0; | static WorkQueue<HTTPClosure> *workQueue = nullptr; | ||||
//! Handlers for (sub)paths | //! Handlers for (sub)paths | ||||
std::vector<HTTPPathHandler> pathHandlers; | std::vector<HTTPPathHandler> pathHandlers; | ||||
//! Bound listening sockets | //! Bound listening sockets | ||||
std::vector<evhttp_bound_socket *> boundSockets; | std::vector<evhttp_bound_socket *> boundSockets; | ||||
/** Check if a network address is allowed to access the HTTP server */ | /** Check if a network address is allowed to access the HTTP server */ | ||||
static bool ClientAllowed(const CNetAddr &netaddr) { | static bool ClientAllowed(const CNetAddr &netaddr) { | ||||
if (!netaddr.IsValid()) return false; | if (!netaddr.IsValid()) return false; | ||||
▲ Show 20 Lines • Show All 327 Lines • ▼ Show 20 Lines | if (eventBase) { | ||||
LogPrintf("HTTP event loop did not exit within allotted time, " | LogPrintf("HTTP event loop did not exit within allotted time, " | ||||
"sending loopbreak\n"); | "sending loopbreak\n"); | ||||
event_base_loopbreak(eventBase); | event_base_loopbreak(eventBase); | ||||
} | } | ||||
threadHTTP.join(); | threadHTTP.join(); | ||||
} | } | ||||
if (eventHTTP) { | if (eventHTTP) { | ||||
evhttp_free(eventHTTP); | evhttp_free(eventHTTP); | ||||
eventHTTP = 0; | eventHTTP = nullptr; | ||||
} | } | ||||
if (eventBase) { | if (eventBase) { | ||||
event_base_free(eventBase); | event_base_free(eventBase); | ||||
eventBase = 0; | eventBase = nullptr; | ||||
} | } | ||||
LogPrint(BCLog::HTTP, "Stopped HTTP server\n"); | LogPrint(BCLog::HTTP, "Stopped HTTP server\n"); | ||||
} | } | ||||
struct event_base *EventBase() { | struct event_base *EventBase() { | ||||
return eventBase; | return eventBase; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | void HTTPRequest::WriteReply(int nStatus, const std::string &strReply) { | ||||
// Send event to main http thread to send reply message | // Send event to main http thread to send reply message | ||||
struct evbuffer *evb = evhttp_request_get_output_buffer(req); | struct evbuffer *evb = evhttp_request_get_output_buffer(req); | ||||
assert(evb); | assert(evb); | ||||
evbuffer_add(evb, strReply.data(), strReply.size()); | evbuffer_add(evb, strReply.data(), strReply.size()); | ||||
HTTPEvent *ev = | HTTPEvent *ev = | ||||
new HTTPEvent(eventBase, true, std::bind(evhttp_send_reply, req, | new HTTPEvent(eventBase, true, std::bind(evhttp_send_reply, req, | ||||
nStatus, (const char *)nullptr, | nStatus, (const char *)nullptr, | ||||
(struct evbuffer *)nullptr)); | (struct evbuffer *)nullptr)); | ||||
ev->trigger(0); | ev->trigger(nullptr); | ||||
replySent = true; | replySent = true; | ||||
// transferred back to main thread. | // transferred back to main thread. | ||||
req = 0; | req = nullptr; | ||||
} | } | ||||
CService HTTPRequest::GetPeer() { | CService HTTPRequest::GetPeer() { | ||||
evhttp_connection *con = evhttp_request_get_connection(req); | evhttp_connection *con = evhttp_request_get_connection(req); | ||||
CService peer; | CService peer; | ||||
if (con) { | if (con) { | ||||
// evhttp retains ownership over returned address string | // evhttp retains ownership over returned address string | ||||
const char *address = ""; | const char *address = ""; | ||||
▲ Show 20 Lines • Show All 47 Lines • Show Last 20 Lines |