Changeset View
Changeset View
Standalone View
Standalone View
src/httpserver.cpp
Show First 20 Lines • Show All 212 Lines • ▼ Show 20 Lines | static bool InitHTTPAllowList() { | ||||
return true; | return true; | ||||
} | } | ||||
/** HTTP request method as string - use for logging only */ | /** HTTP request method as string - use for logging only */ | ||||
static std::string RequestMethodString(HTTPRequest::RequestMethod m) { | static std::string RequestMethodString(HTTPRequest::RequestMethod m) { | ||||
switch (m) { | switch (m) { | ||||
case HTTPRequest::GET: | case HTTPRequest::GET: | ||||
return "GET"; | return "GET"; | ||||
break; | |||||
case HTTPRequest::POST: | case HTTPRequest::POST: | ||||
return "POST"; | return "POST"; | ||||
break; | |||||
case HTTPRequest::HEAD: | case HTTPRequest::HEAD: | ||||
return "HEAD"; | return "HEAD"; | ||||
break; | |||||
case HTTPRequest::PUT: | case HTTPRequest::PUT: | ||||
return "PUT"; | return "PUT"; | ||||
break; | case HTTPRequest::OPTIONS: | ||||
return "OPTIONS"; | |||||
default: | default: | ||||
return "unknown"; | return "unknown"; | ||||
} | } | ||||
} | } | ||||
/** HTTP request callback */ | /** HTTP request callback */ | ||||
static void http_request_cb(struct evhttp_request *req, void *arg) { | static void http_request_cb(struct evhttp_request *req, void *arg) { | ||||
Config &config = *reinterpret_cast<Config *>(arg); | Config &config = *reinterpret_cast<Config *>(arg); | ||||
▲ Show 20 Lines • Show All 184 Lines • ▼ Show 20 Lines | #endif | ||||
evhttp_set_timeout( | evhttp_set_timeout( | ||||
http, gArgs.GetArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT)); | http, gArgs.GetArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT)); | ||||
evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE); | evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE); | ||||
evhttp_set_max_body_size( | evhttp_set_max_body_size( | ||||
http, MIN_SUPPORTED_BODY_SIZE + 2 * config.GetMaxBlockSize()); | http, MIN_SUPPORTED_BODY_SIZE + 2 * config.GetMaxBlockSize()); | ||||
evhttp_set_gencb(http, http_request_cb, &config); | evhttp_set_gencb(http, http_request_cb, &config); | ||||
// Only POST and OPTIONS are supported, but we return HTTP 405 for the | |||||
// others | |||||
evhttp_set_allowed_methods(http, | |||||
EVHTTP_REQ_GET | EVHTTP_REQ_POST | | |||||
EVHTTP_REQ_HEAD | EVHTTP_REQ_PUT | | |||||
EVHTTP_REQ_DELETE | EVHTTP_REQ_OPTIONS); | |||||
if (!HTTPBindAddresses(http)) { | if (!HTTPBindAddresses(http)) { | ||||
LogPrintf("Unable to bind any endpoint for RPC server\n"); | LogPrintf("Unable to bind any endpoint for RPC server\n"); | ||||
evhttp_free(http); | evhttp_free(http); | ||||
event_base_free(base); | event_base_free(base); | ||||
return false; | return false; | ||||
} | } | ||||
LogPrint(BCLog::HTTP, "Initialized HTTP server\n"); | LogPrint(BCLog::HTTP, "Initialized HTTP server\n"); | ||||
▲ Show 20 Lines • Show All 188 Lines • ▼ Show 20 Lines | |||||
std::string HTTPRequest::GetURI() { | std::string HTTPRequest::GetURI() { | ||||
return evhttp_request_get_uri(req); | return evhttp_request_get_uri(req); | ||||
} | } | ||||
HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod() { | HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod() { | ||||
switch (evhttp_request_get_command(req)) { | switch (evhttp_request_get_command(req)) { | ||||
case EVHTTP_REQ_GET: | case EVHTTP_REQ_GET: | ||||
return GET; | return GET; | ||||
break; | |||||
case EVHTTP_REQ_POST: | case EVHTTP_REQ_POST: | ||||
return POST; | return POST; | ||||
break; | |||||
case EVHTTP_REQ_HEAD: | case EVHTTP_REQ_HEAD: | ||||
return HEAD; | return HEAD; | ||||
break; | |||||
case EVHTTP_REQ_PUT: | case EVHTTP_REQ_PUT: | ||||
return PUT; | return PUT; | ||||
break; | case EVHTTP_REQ_OPTIONS: | ||||
return OPTIONS; | |||||
default: | default: | ||||
return UNKNOWN; | return UNKNOWN; | ||||
break; | |||||
} | } | ||||
} | } | ||||
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, | void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, | ||||
const HTTPRequestHandler &handler) { | const HTTPRequestHandler &handler) { | ||||
LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", | LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", | ||||
prefix, exactMatch); | prefix, exactMatch); | ||||
pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler)); | pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler)); | ||||
Show All 14 Lines |