mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
httpserver: delete libevent!
This commit is contained in:
parent
53ac965791
commit
6a6285d268
4 changed files with 4 additions and 659 deletions
|
@ -35,22 +35,9 @@
|
|||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <event2/buffer.h>
|
||||
#include <event2/bufferevent.h>
|
||||
#include <event2/http.h>
|
||||
#include <event2/http_struct.h>
|
||||
#include <event2/keyvalq_struct.h>
|
||||
#include <event2/thread.h>
|
||||
#include <event2/util.h>
|
||||
|
||||
#include <support/events.h>
|
||||
|
||||
using common::InvalidPortErrMsg;
|
||||
using http_bitcoin::HTTPRequest;
|
||||
|
||||
/** Maximum size of http request (request line + headers) */
|
||||
static const size_t MAX_HEADERS_SIZE = 8192;
|
||||
|
||||
/** HTTP request work item */
|
||||
class HTTPWorkItem final : public HTTPClosure
|
||||
{
|
||||
|
@ -141,10 +128,6 @@ struct HTTPPathHandler
|
|||
|
||||
/** HTTP module state */
|
||||
|
||||
//! libevent event loop
|
||||
static struct event_base* eventBase = nullptr;
|
||||
//! HTTP server
|
||||
static struct evhttp* eventHTTP = nullptr;
|
||||
static std::unique_ptr<http_bitcoin::HTTPServer> g_http_server{nullptr};
|
||||
//! List of subnets to allow RPC connections from
|
||||
static std::vector<CSubNet> rpc_allow_subnets;
|
||||
|
@ -153,63 +136,6 @@ static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr};
|
|||
//! Handlers for (sub)paths
|
||||
static GlobalMutex g_httppathhandlers_mutex;
|
||||
static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY(g_httppathhandlers_mutex);
|
||||
//! Bound listening sockets
|
||||
static std::vector<evhttp_bound_socket *> boundSockets;
|
||||
|
||||
/**
|
||||
* @brief Helps keep track of open `evhttp_connection`s with active `evhttp_requests`
|
||||
*
|
||||
*/
|
||||
class HTTPRequestTracker
|
||||
{
|
||||
private:
|
||||
mutable Mutex m_mutex;
|
||||
mutable std::condition_variable m_cv;
|
||||
//! For each connection, keep a counter of how many requests are open
|
||||
std::unordered_map<const evhttp_connection*, size_t> m_tracker GUARDED_BY(m_mutex);
|
||||
|
||||
void RemoveConnectionInternal(const decltype(m_tracker)::iterator it) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
|
||||
{
|
||||
m_tracker.erase(it);
|
||||
if (m_tracker.empty()) m_cv.notify_all();
|
||||
}
|
||||
public:
|
||||
//! Increase request counter for the associated connection by 1
|
||||
void AddRequest(evhttp_request* req) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||
{
|
||||
const evhttp_connection* conn{Assert(evhttp_request_get_connection(Assert(req)))};
|
||||
WITH_LOCK(m_mutex, ++m_tracker[conn]);
|
||||
}
|
||||
//! Decrease request counter for the associated connection by 1, remove connection if counter is 0
|
||||
void RemoveRequest(evhttp_request* req) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||
{
|
||||
const evhttp_connection* conn{Assert(evhttp_request_get_connection(Assert(req)))};
|
||||
LOCK(m_mutex);
|
||||
auto it{m_tracker.find(conn)};
|
||||
if (it != m_tracker.end() && it->second > 0) {
|
||||
if (--(it->second) == 0) RemoveConnectionInternal(it);
|
||||
}
|
||||
}
|
||||
//! Remove a connection entirely
|
||||
void RemoveConnection(const evhttp_connection* conn) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||
{
|
||||
LOCK(m_mutex);
|
||||
auto it{m_tracker.find(Assert(conn))};
|
||||
if (it != m_tracker.end()) RemoveConnectionInternal(it);
|
||||
}
|
||||
size_t CountActiveConnections() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||
{
|
||||
return WITH_LOCK(m_mutex, return m_tracker.size());
|
||||
}
|
||||
//! Wait until there are no more connections with active requests in the tracker
|
||||
void WaitUntilEmpty() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
||||
{
|
||||
WAIT_LOCK(m_mutex, lock);
|
||||
m_cv.wait(lock, [this]() EXCLUSIVE_LOCKS_REQUIRED(m_mutex) { return m_tracker.empty(); });
|
||||
}
|
||||
};
|
||||
//! Track active requests
|
||||
static HTTPRequestTracker g_requests;
|
||||
|
||||
/** Check if a network address is allowed to access the HTTP server */
|
||||
static bool ClientAllowed(const CNetAddr& netaddr)
|
||||
|
@ -320,57 +246,6 @@ static void RejectAllRequests(std::unique_ptr<http_bitcoin::HTTPRequest> hreq)
|
|||
hreq->WriteReply(HTTP_SERVICE_UNAVAILABLE);
|
||||
}
|
||||
|
||||
/** HTTP request callback */
|
||||
static void http_request_cb(struct evhttp_request* req, void* arg)
|
||||
{
|
||||
evhttp_connection* conn{evhttp_request_get_connection(req)};
|
||||
// Track active requests
|
||||
{
|
||||
g_requests.AddRequest(req);
|
||||
evhttp_request_set_on_complete_cb(req, [](struct evhttp_request* req, void*) {
|
||||
g_requests.RemoveRequest(req);
|
||||
}, nullptr);
|
||||
evhttp_connection_set_closecb(conn, [](evhttp_connection* conn, void* arg) {
|
||||
g_requests.RemoveConnection(conn);
|
||||
}, nullptr);
|
||||
}
|
||||
|
||||
// Disable reading to work around a libevent bug, fixed in 2.1.9
|
||||
// See https://github.com/libevent/libevent/commit/5ff8eb26371c4dc56f384b2de35bea2d87814779
|
||||
// and https://github.com/bitcoin/bitcoin/pull/11593.
|
||||
if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02010900) {
|
||||
if (conn) {
|
||||
bufferevent* bev = evhttp_connection_get_bufferevent(conn);
|
||||
if (bev) {
|
||||
bufferevent_disable(bev, EV_READ);
|
||||
}
|
||||
}
|
||||
}
|
||||
auto hreq{std::make_unique<http_libevent::HTTPRequest>(req, *static_cast<const util::SignalInterrupt*>(arg))};
|
||||
|
||||
// Disabled now that http_libevent is deprecated, or code won't compile.
|
||||
// This line is currently unreachable and will be cleaned up in a future commit.
|
||||
// MaybeDispatchRequestToWorker(std::move(hreq));
|
||||
Assume(false);
|
||||
}
|
||||
|
||||
/** Callback to reject HTTP requests after shutdown. */
|
||||
static void http_reject_request_cb(struct evhttp_request* req, void*)
|
||||
{
|
||||
LogDebug(BCLog::HTTP, "Rejecting request while shutting down\n");
|
||||
evhttp_send_error(req, HTTP_SERVUNAVAIL, nullptr);
|
||||
}
|
||||
|
||||
/** Event dispatcher thread */
|
||||
static void ThreadHTTP(struct event_base* base)
|
||||
{
|
||||
util::ThreadRename("http");
|
||||
LogDebug(BCLog::HTTP, "Entering http event loop\n");
|
||||
event_base_dispatch(base);
|
||||
// Event loop will be interrupted by InterruptHTTPServer()
|
||||
LogDebug(BCLog::HTTP, "Exited http event loop\n");
|
||||
}
|
||||
|
||||
static std::vector<std::pair<std::string, uint16_t>> GetBindAddresses()
|
||||
{
|
||||
uint16_t http_port{static_cast<uint16_t>(gArgs.GetIntArg("-rpcport", BaseParams().RPCPort()))};
|
||||
|
@ -411,374 +286,6 @@ static void HTTPWorkQueueRun(WorkQueue<HTTPClosure>* queue, int worker_num)
|
|||
queue->Run();
|
||||
}
|
||||
|
||||
/** libevent event log callback */
|
||||
static void libevent_log_cb(int severity, const char *msg)
|
||||
{
|
||||
BCLog::Level level;
|
||||
switch (severity) {
|
||||
case EVENT_LOG_DEBUG:
|
||||
level = BCLog::Level::Debug;
|
||||
break;
|
||||
case EVENT_LOG_MSG:
|
||||
level = BCLog::Level::Info;
|
||||
break;
|
||||
case EVENT_LOG_WARN:
|
||||
level = BCLog::Level::Warning;
|
||||
break;
|
||||
default: // EVENT_LOG_ERR and others are mapped to error
|
||||
level = BCLog::Level::Error;
|
||||
break;
|
||||
}
|
||||
LogPrintLevel(BCLog::LIBEVENT, level, "%s\n", msg);
|
||||
}
|
||||
|
||||
namespace http_libevent {
|
||||
/** Bind HTTP server to specified addresses */
|
||||
static bool HTTPBindAddresses(struct evhttp* http)
|
||||
{
|
||||
std::vector<std::pair<std::string, uint16_t>> endpoints{GetBindAddresses()};
|
||||
for (std::vector<std::pair<std::string, uint16_t> >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) {
|
||||
LogPrintf("Binding RPC on address %s port %i\n", i->first, i->second);
|
||||
evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? nullptr : i->first.c_str(), i->second);
|
||||
if (bind_handle) {
|
||||
const std::optional<CNetAddr> addr{LookupHost(i->first, false)};
|
||||
if (i->first.empty() || (addr.has_value() && addr->IsBindAny())) {
|
||||
LogPrintf("WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet\n");
|
||||
}
|
||||
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
|
||||
evutil_socket_t fd = evhttp_bound_socket_get_fd(bind_handle);
|
||||
int one = 1;
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (sockopt_arg_type)&one, sizeof(one)) == SOCKET_ERROR) {
|
||||
LogInfo("WARNING: Unable to set TCP_NODELAY on RPC server socket, continuing anyway\n");
|
||||
}
|
||||
boundSockets.push_back(bind_handle);
|
||||
} else {
|
||||
LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second);
|
||||
}
|
||||
}
|
||||
return !boundSockets.empty();
|
||||
}
|
||||
|
||||
bool InitHTTPServer(const util::SignalInterrupt& interrupt)
|
||||
{
|
||||
if (!InitHTTPAllowList())
|
||||
return false;
|
||||
|
||||
// Redirect libevent's logging to our own log
|
||||
event_set_log_callback(&libevent_log_cb);
|
||||
// Update libevent's log handling.
|
||||
UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT));
|
||||
|
||||
#ifdef WIN32
|
||||
evthread_use_windows_threads();
|
||||
#else
|
||||
evthread_use_pthreads();
|
||||
#endif
|
||||
|
||||
raii_event_base base_ctr = obtain_event_base();
|
||||
|
||||
/* Create a new evhttp object to handle requests. */
|
||||
raii_evhttp http_ctr = obtain_evhttp(base_ctr.get());
|
||||
struct evhttp* http = http_ctr.get();
|
||||
if (!http) {
|
||||
LogPrintf("couldn't create evhttp. Exiting.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
evhttp_set_timeout(http, gArgs.GetIntArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT));
|
||||
evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE);
|
||||
evhttp_set_max_body_size(http, MAX_SIZE);
|
||||
evhttp_set_gencb(http, http_request_cb, (void*)&interrupt);
|
||||
|
||||
if (!HTTPBindAddresses(http)) {
|
||||
LogPrintf("Unable to bind any endpoint for RPC server\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
LogDebug(BCLog::HTTP, "Initialized HTTP server\n");
|
||||
int workQueueDepth = std::max((long)gArgs.GetIntArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
|
||||
LogDebug(BCLog::HTTP, "creating work queue of depth %d\n", workQueueDepth);
|
||||
|
||||
g_work_queue = std::make_unique<WorkQueue<HTTPClosure>>(workQueueDepth);
|
||||
// transfer ownership to eventBase/HTTP via .release()
|
||||
eventBase = base_ctr.release();
|
||||
eventHTTP = http_ctr.release();
|
||||
return true;
|
||||
}
|
||||
|
||||
void UpdateHTTPServerLogging(bool enable) {
|
||||
if (enable) {
|
||||
event_enable_debug_logging(EVENT_DBG_ALL);
|
||||
} else {
|
||||
event_enable_debug_logging(EVENT_DBG_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
static std::thread g_thread_http;
|
||||
static std::vector<std::thread> g_thread_http_workers;
|
||||
|
||||
void StartHTTPServer()
|
||||
{
|
||||
int rpcThreads = std::max((long)gArgs.GetIntArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
|
||||
LogInfo("Starting HTTP server with %d worker threads\n", rpcThreads);
|
||||
g_thread_http = std::thread(ThreadHTTP, eventBase);
|
||||
|
||||
for (int i = 0; i < rpcThreads; i++) {
|
||||
g_thread_http_workers.emplace_back(HTTPWorkQueueRun, g_work_queue.get(), i);
|
||||
}
|
||||
}
|
||||
|
||||
void InterruptHTTPServer()
|
||||
{
|
||||
LogDebug(BCLog::HTTP, "Interrupting HTTP server\n");
|
||||
if (eventHTTP) {
|
||||
// Reject requests on current connections
|
||||
evhttp_set_gencb(eventHTTP, http_reject_request_cb, nullptr);
|
||||
}
|
||||
if (g_work_queue) {
|
||||
g_work_queue->Interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
void StopHTTPServer()
|
||||
{
|
||||
LogDebug(BCLog::HTTP, "Stopping HTTP server\n");
|
||||
if (g_work_queue) {
|
||||
LogDebug(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
|
||||
for (auto& thread : g_thread_http_workers) {
|
||||
thread.join();
|
||||
}
|
||||
g_thread_http_workers.clear();
|
||||
}
|
||||
// Unlisten sockets, these are what make the event loop running, which means
|
||||
// that after this and all connections are closed the event loop will quit.
|
||||
for (evhttp_bound_socket *socket : boundSockets) {
|
||||
evhttp_del_accept_socket(eventHTTP, socket);
|
||||
}
|
||||
boundSockets.clear();
|
||||
{
|
||||
if (const auto n_connections{g_requests.CountActiveConnections()}; n_connections != 0) {
|
||||
LogDebug(BCLog::HTTP, "Waiting for %d connections to stop HTTP server\n", n_connections);
|
||||
}
|
||||
g_requests.WaitUntilEmpty();
|
||||
}
|
||||
if (eventHTTP) {
|
||||
// Schedule a callback to call evhttp_free in the event base thread, so
|
||||
// that evhttp_free does not need to be called again after the handling
|
||||
// of unfinished request connections that follows.
|
||||
event_base_once(eventBase, -1, EV_TIMEOUT, [](evutil_socket_t, short, void*) {
|
||||
evhttp_free(eventHTTP);
|
||||
eventHTTP = nullptr;
|
||||
}, nullptr, nullptr);
|
||||
}
|
||||
if (eventBase) {
|
||||
LogDebug(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
|
||||
if (g_thread_http.joinable()) g_thread_http.join();
|
||||
event_base_free(eventBase);
|
||||
eventBase = nullptr;
|
||||
}
|
||||
g_work_queue.reset();
|
||||
LogDebug(BCLog::HTTP, "Stopped HTTP server\n");
|
||||
}
|
||||
} // namespace http_libevent
|
||||
|
||||
struct event_base* EventBase()
|
||||
{
|
||||
return eventBase;
|
||||
}
|
||||
|
||||
static void httpevent_callback_fn(evutil_socket_t, short, void* data)
|
||||
{
|
||||
// Static handler: simply call inner handler
|
||||
HTTPEvent *self = static_cast<HTTPEvent*>(data);
|
||||
self->handler();
|
||||
if (self->deleteWhenTriggered)
|
||||
delete self;
|
||||
}
|
||||
|
||||
HTTPEvent::HTTPEvent(struct event_base* base, bool _deleteWhenTriggered, const std::function<void()>& _handler):
|
||||
deleteWhenTriggered(_deleteWhenTriggered), handler(_handler)
|
||||
{
|
||||
ev = event_new(base, -1, 0, httpevent_callback_fn, this);
|
||||
assert(ev);
|
||||
}
|
||||
HTTPEvent::~HTTPEvent()
|
||||
{
|
||||
event_free(ev);
|
||||
}
|
||||
void HTTPEvent::trigger(struct timeval* tv)
|
||||
{
|
||||
if (tv == nullptr)
|
||||
event_active(ev, 0, 0); // immediately trigger event in main thread
|
||||
else
|
||||
evtimer_add(ev, tv); // trigger after timeval passed
|
||||
}
|
||||
|
||||
namespace http_libevent {
|
||||
HTTPRequest::HTTPRequest(struct evhttp_request* _req, const util::SignalInterrupt& interrupt, bool _replySent)
|
||||
: req(_req), m_interrupt(interrupt), replySent(_replySent)
|
||||
{
|
||||
}
|
||||
|
||||
HTTPRequest::~HTTPRequest()
|
||||
{
|
||||
if (!replySent) {
|
||||
// Keep track of whether reply was sent to avoid request leaks
|
||||
LogPrintf("%s: Unhandled request\n", __func__);
|
||||
WriteReply(HTTP_INTERNAL_SERVER_ERROR, "Unhandled request");
|
||||
}
|
||||
// evhttpd cleans up the request, as long as a reply was sent.
|
||||
}
|
||||
|
||||
std::pair<bool, std::string> HTTPRequest::GetHeader(const std::string& hdr) const
|
||||
{
|
||||
const struct evkeyvalq* headers = evhttp_request_get_input_headers(req);
|
||||
assert(headers);
|
||||
const char* val = evhttp_find_header(headers, hdr.c_str());
|
||||
if (val)
|
||||
return std::make_pair(true, val);
|
||||
else
|
||||
return std::make_pair(false, "");
|
||||
}
|
||||
|
||||
std::string HTTPRequest::ReadBody()
|
||||
{
|
||||
struct evbuffer* buf = evhttp_request_get_input_buffer(req);
|
||||
if (!buf)
|
||||
return "";
|
||||
size_t size = evbuffer_get_length(buf);
|
||||
/** Trivial implementation: if this is ever a performance bottleneck,
|
||||
* internal copying can be avoided in multi-segment buffers by using
|
||||
* evbuffer_peek and an awkward loop. Though in that case, it'd be even
|
||||
* better to not copy into an intermediate string but use a stream
|
||||
* abstraction to consume the evbuffer on the fly in the parsing algorithm.
|
||||
*/
|
||||
const char* data = (const char*)evbuffer_pullup(buf, size);
|
||||
if (!data) // returns nullptr in case of empty buffer
|
||||
return "";
|
||||
std::string rv(data, size);
|
||||
evbuffer_drain(buf, size);
|
||||
return rv;
|
||||
}
|
||||
|
||||
void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
|
||||
{
|
||||
struct evkeyvalq* headers = evhttp_request_get_output_headers(req);
|
||||
assert(headers);
|
||||
evhttp_add_header(headers, hdr.c_str(), value.c_str());
|
||||
}
|
||||
|
||||
/** Closure sent to main thread to request a reply to be sent to
|
||||
* a HTTP request.
|
||||
* Replies must be sent in the main loop in the main http thread,
|
||||
* this cannot be done from worker threads.
|
||||
*/
|
||||
void HTTPRequest::WriteReply(int nStatus, std::span<const std::byte> reply)
|
||||
{
|
||||
assert(!replySent && req);
|
||||
if (m_interrupt) {
|
||||
WriteHeader("Connection", "close");
|
||||
}
|
||||
// Send event to main http thread to send reply message
|
||||
struct evbuffer* evb = evhttp_request_get_output_buffer(req);
|
||||
assert(evb);
|
||||
evbuffer_add(evb, reply.data(), reply.size());
|
||||
auto req_copy = req;
|
||||
HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus]{
|
||||
evhttp_send_reply(req_copy, nStatus, nullptr, nullptr);
|
||||
// Re-enable reading from the socket. This is the second part of the libevent
|
||||
// workaround above.
|
||||
if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02010900) {
|
||||
evhttp_connection* conn = evhttp_request_get_connection(req_copy);
|
||||
if (conn) {
|
||||
bufferevent* bev = evhttp_connection_get_bufferevent(conn);
|
||||
if (bev) {
|
||||
bufferevent_enable(bev, EV_READ | EV_WRITE);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
ev->trigger(nullptr);
|
||||
replySent = true;
|
||||
req = nullptr; // transferred back to main thread
|
||||
}
|
||||
|
||||
CService HTTPRequest::GetPeer() const
|
||||
{
|
||||
evhttp_connection* con = evhttp_request_get_connection(req);
|
||||
CService peer;
|
||||
if (con) {
|
||||
// evhttp retains ownership over returned address string
|
||||
const char* address = "";
|
||||
uint16_t port = 0;
|
||||
|
||||
#ifdef HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
|
||||
evhttp_connection_get_peer(con, &address, &port);
|
||||
#else
|
||||
evhttp_connection_get_peer(con, (char**)&address, &port);
|
||||
#endif // HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
|
||||
|
||||
peer = MaybeFlipIPv6toCJDNS(LookupNumeric(address, port));
|
||||
}
|
||||
return peer;
|
||||
}
|
||||
|
||||
std::string HTTPRequest::GetURI() const
|
||||
{
|
||||
return evhttp_request_get_uri(req);
|
||||
}
|
||||
|
||||
HTTPRequestMethod HTTPRequest::GetRequestMethod() const
|
||||
{
|
||||
switch (evhttp_request_get_command(req)) {
|
||||
case EVHTTP_REQ_GET:
|
||||
return HTTPRequestMethod::GET;
|
||||
case EVHTTP_REQ_POST:
|
||||
return HTTPRequestMethod::POST;
|
||||
case EVHTTP_REQ_HEAD:
|
||||
return HTTPRequestMethod::HEAD;
|
||||
case EVHTTP_REQ_PUT:
|
||||
return HTTPRequestMethod::PUT;
|
||||
default:
|
||||
return HTTPRequestMethod::UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> HTTPRequest::GetQueryParameter(const std::string& key) const
|
||||
{
|
||||
const char* uri{evhttp_request_get_uri(req)};
|
||||
|
||||
return GetQueryParameterFromUri(uri, key);
|
||||
}
|
||||
|
||||
std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key)
|
||||
{
|
||||
evhttp_uri* uri_parsed{evhttp_uri_parse(uri)};
|
||||
if (!uri_parsed) {
|
||||
throw std::runtime_error("URI parsing failed, it likely contained RFC 3986 invalid characters");
|
||||
}
|
||||
const char* query{evhttp_uri_get_query(uri_parsed)};
|
||||
std::optional<std::string> result;
|
||||
|
||||
if (query) {
|
||||
// Parse the query string into a key-value queue and iterate over it
|
||||
struct evkeyvalq params_q;
|
||||
evhttp_parse_query_str(query, ¶ms_q);
|
||||
|
||||
for (struct evkeyval* param{params_q.tqh_first}; param != nullptr; param = param->next.tqe_next) {
|
||||
if (param->key == key) {
|
||||
result = param->value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
evhttp_clear_headers(¶ms_q);
|
||||
}
|
||||
evhttp_uri_free(uri_parsed);
|
||||
|
||||
return result;
|
||||
}
|
||||
} // namespace http_libevent
|
||||
|
||||
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
|
||||
{
|
||||
LogDebug(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
|
||||
|
@ -801,7 +308,6 @@ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
namespace http_bitcoin {
|
||||
using util::SplitString;
|
||||
|
||||
|
|
145
src/httpserver.h
145
src/httpserver.h
|
@ -34,10 +34,6 @@ static const int DEFAULT_HTTP_WORKQUEUE=64;
|
|||
|
||||
static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
|
||||
|
||||
struct evhttp_request;
|
||||
struct event_base;
|
||||
class CService;
|
||||
|
||||
enum HTTPRequestMethod {
|
||||
UNKNOWN,
|
||||
GET,
|
||||
|
@ -46,124 +42,6 @@ enum HTTPRequestMethod {
|
|||
PUT
|
||||
};
|
||||
|
||||
namespace http_bitcoin {};
|
||||
|
||||
namespace http_libevent {
|
||||
class HTTPRequest;
|
||||
|
||||
/** Initialize HTTP server.
|
||||
* Call this before RegisterHTTPHandler or EventBase().
|
||||
*/
|
||||
bool InitHTTPServer(const util::SignalInterrupt& interrupt);
|
||||
/** Start HTTP server.
|
||||
* This is separate from InitHTTPServer to give users race-condition-free time
|
||||
* to register their handlers between InitHTTPServer and StartHTTPServer.
|
||||
*/
|
||||
void StartHTTPServer();
|
||||
/** Interrupt HTTP server threads */
|
||||
void InterruptHTTPServer();
|
||||
/** Stop HTTP server */
|
||||
void StopHTTPServer();
|
||||
|
||||
/** Change logging level for libevent. */
|
||||
void UpdateHTTPServerLogging(bool enable);
|
||||
} // namespace http_libevent
|
||||
|
||||
/** Return evhttp event base. This can be used by submodules to
|
||||
* queue timers or custom events.
|
||||
*/
|
||||
struct event_base* EventBase();
|
||||
|
||||
namespace http_libevent {
|
||||
/** In-flight HTTP request.
|
||||
* Thin C++ wrapper around evhttp_request.
|
||||
*/
|
||||
class HTTPRequest
|
||||
{
|
||||
private:
|
||||
struct evhttp_request* req;
|
||||
const util::SignalInterrupt& m_interrupt;
|
||||
bool replySent;
|
||||
|
||||
public:
|
||||
explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false);
|
||||
~HTTPRequest();
|
||||
|
||||
/** Get requested URI.
|
||||
*/
|
||||
std::string GetURI() const;
|
||||
|
||||
/** Get CService (address:ip) for the origin of the http request.
|
||||
*/
|
||||
CService GetPeer() const;
|
||||
|
||||
/** Get request method.
|
||||
*/
|
||||
HTTPRequestMethod GetRequestMethod() const;
|
||||
|
||||
/** Get the query parameter value from request uri for a specified key, or std::nullopt if the
|
||||
* key is not found.
|
||||
*
|
||||
* If the query string contains duplicate keys, the first value is returned. Many web frameworks
|
||||
* would instead parse this as an array of values, but this is not (yet) implemented as it is
|
||||
* currently not needed in any of the endpoints.
|
||||
*
|
||||
* @param[in] key represents the query parameter of which the value is returned
|
||||
*/
|
||||
std::optional<std::string> GetQueryParameter(const std::string& key) const;
|
||||
|
||||
/**
|
||||
* Get the request header specified by hdr, or an empty string.
|
||||
* Return a pair (isPresent,string).
|
||||
*/
|
||||
std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
|
||||
|
||||
/**
|
||||
* Read request body.
|
||||
*
|
||||
* @note As this consumes the underlying buffer, call this only once.
|
||||
* Repeated calls will return an empty string.
|
||||
*/
|
||||
std::string ReadBody();
|
||||
|
||||
/**
|
||||
* Write output header.
|
||||
*
|
||||
* @note call this before calling WriteErrorReply or Reply.
|
||||
*/
|
||||
void WriteHeader(const std::string& hdr, const std::string& value);
|
||||
|
||||
/**
|
||||
* Write HTTP reply.
|
||||
* nStatus is the HTTP status code to send.
|
||||
* reply is the body of the reply. Keep it empty to send a standard message.
|
||||
*
|
||||
* @note Can be called only once. As this will give the request back to the
|
||||
* main thread, do not call any other HTTPRequest methods after calling this.
|
||||
*/
|
||||
void WriteReply(int nStatus, std::string_view reply = "")
|
||||
{
|
||||
WriteReply(nStatus, std::as_bytes(std::span{reply}));
|
||||
}
|
||||
void WriteReply(int nStatus, std::span<const std::byte> reply);
|
||||
};
|
||||
|
||||
|
||||
/** Get the query parameter value from request uri for a specified key, or std::nullopt if the key
|
||||
* is not found.
|
||||
*
|
||||
* If the query string contains duplicate keys, the first value is returned. Many web frameworks
|
||||
* would instead parse this as an array of values, but this is not (yet) implemented as it is
|
||||
* currently not needed in any of the endpoints.
|
||||
*
|
||||
* Helper function for HTTPRequest::GetQueryParameter.
|
||||
*
|
||||
* @param[in] uri is the entire request uri
|
||||
* @param[in] key represents the query parameter of which the value is returned
|
||||
*/
|
||||
std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key);
|
||||
} // namespace http_libevent
|
||||
|
||||
/** Event handler closure.
|
||||
*/
|
||||
class HTTPClosure
|
||||
|
@ -173,29 +51,6 @@ public:
|
|||
virtual ~HTTPClosure() = default;
|
||||
};
|
||||
|
||||
/** Event class. This can be used either as a cross-thread trigger or as a timer.
|
||||
*/
|
||||
class HTTPEvent
|
||||
{
|
||||
public:
|
||||
/** Create a new event.
|
||||
* deleteWhenTriggered deletes this event object after the event is triggered (and the handler called)
|
||||
* handler is the handler to call when the event is triggered.
|
||||
*/
|
||||
HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
|
||||
~HTTPEvent();
|
||||
|
||||
/** Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger it after
|
||||
* the given time has elapsed.
|
||||
*/
|
||||
void trigger(struct timeval* tv);
|
||||
|
||||
bool deleteWhenTriggered;
|
||||
std::function<void()> handler;
|
||||
private:
|
||||
struct event* ev;
|
||||
};
|
||||
|
||||
namespace http_bitcoin {
|
||||
using util::LineReader;
|
||||
using NodeId = SockMan::Id;
|
||||
|
|
|
@ -240,24 +240,16 @@ static RPCHelpMan logging()
|
|||
},
|
||||
RPCExamples{
|
||||
HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"")
|
||||
+ HelpExampleRpc("logging", "[\"all\"], [\"libevent\"]")
|
||||
+ HelpExampleRpc("logging", "[\"all\"], [\"walletdb\"]")
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
BCLog::CategoryMask original_log_categories = LogInstance().GetCategoryMask();
|
||||
if (request.params[0].isArray()) {
|
||||
EnableOrDisableLogCategories(request.params[0], true);
|
||||
}
|
||||
if (request.params[1].isArray()) {
|
||||
EnableOrDisableLogCategories(request.params[1], false);
|
||||
}
|
||||
BCLog::CategoryMask updated_log_categories = LogInstance().GetCategoryMask();
|
||||
BCLog::CategoryMask changed_log_categories = original_log_categories ^ updated_log_categories;
|
||||
|
||||
// Update libevent logging if BCLog::LIBEVENT has changed.
|
||||
if (changed_log_categories & BCLog::LIBEVENT) {
|
||||
http_libevent::UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT));
|
||||
}
|
||||
|
||||
UniValue result(UniValue::VOBJ);
|
||||
for (const auto& logCatActive : LogInstance().LogCategoriesList()) {
|
||||
|
|
|
@ -60,18 +60,15 @@ BOOST_AUTO_TEST_CASE(test_query_parameters_new_behavior)
|
|||
std::string uri {};
|
||||
// This is an invalid URI because it contains a % that is not followed by two hex digits
|
||||
uri = "/rest/endpoint/someresource.json?p1=v1&p2=v2%";
|
||||
// Old behavior: URI with invalid characters (%) raises a runtime error regardless of which query parameter is queried
|
||||
BOOST_CHECK_EXCEPTION(http_libevent::GetQueryParameterFromUri(uri.c_str(), "p1"), std::runtime_error, HasReason("URI parsing failed, it likely contained RFC 3986 invalid characters"));
|
||||
// Old libevent behavior: URI with invalid characters (%) raised a runtime error regardless of which query parameter is queried
|
||||
// New behavior: Tolerate as much as we can even
|
||||
BOOST_CHECK_EQUAL(http_bitcoin::GetQueryParameterFromUri(uri.c_str(), "p1").value(), "v1");
|
||||
BOOST_CHECK_EQUAL(http_bitcoin::GetQueryParameterFromUri(uri.c_str(), "p2").value(), "v2%");
|
||||
|
||||
// This is a valid URI because the %XX encoding is correct: `?p1=v1&p2=100%`
|
||||
uri = "/rest/endpoint/someresource.json%3Fp1%3Dv1%26p2%3D100%25";
|
||||
// Old behavior: libevent does not decode the URI before parsing, so it does not detect or return the query
|
||||
// (libevent will parse the entire argument string as the uri path)
|
||||
BOOST_CHECK(!http_libevent::GetQueryParameterFromUri(uri.c_str(), "p1").has_value());
|
||||
BOOST_CHECK(!http_libevent::GetQueryParameterFromUri(uri.c_str(), "p2").has_value());
|
||||
// Old behavior: libevent did not decode the URI before parsing, so it did not detect or return the query
|
||||
// (libevent would parse the entire argument string as the uri path)
|
||||
// New behavior: Decode before parsing the URI so reserved characters like ? & = are interpreted correctly
|
||||
BOOST_CHECK_EQUAL(http_bitcoin::GetQueryParameterFromUri(uri.c_str(), "p1").value(), "v1");
|
||||
BOOST_CHECK_EQUAL(http_bitcoin::GetQueryParameterFromUri(uri.c_str(), "p2").value(), "100%");
|
||||
|
@ -110,11 +107,6 @@ void test_query_parameters(func GetQueryParameterFromUri) {
|
|||
BOOST_CHECK_EQUAL(GetQueryParameterFromUri(uri.c_str(), "p2").value(), "100%");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_query_parameters_libevent)
|
||||
{
|
||||
test_query_parameters(http_libevent::GetQueryParameterFromUri);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_query_parameters_bitcoin)
|
||||
{
|
||||
test_query_parameters(http_bitcoin::GetQueryParameterFromUri);
|
||||
|
|
Loading…
Add table
Reference in a new issue