diff --git a/src/httpserver.h b/src/httpserver.h --- a/src/httpserver.h +++ b/src/httpserver.h @@ -1,4 +1,5 @@ // Copyright (c) 2015-2016 The Bitcoin Core developers +// Copyright (c) 2018 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -20,17 +21,22 @@ class CService; class HTTPRequest; -/** Initialize HTTP server. +/** + * Initialize HTTP server. * Call this before RegisterHTTPHandler or EventBase(). */ bool InitHTTPServer(Config &config); -/** Start HTTP server. + +/** + * Start HTTP server. * This is separate from InitHTTPServer to give users race-condition-free time * to register their handlers between InitHTTPServer and StartHTTPServer. */ bool StartHTTPServer(); + /** Interrupt HTTP server threads */ void InterruptHTTPServer(); + /** Stop HTTP server */ void StopHTTPServer(); @@ -38,21 +44,26 @@ typedef std::function HTTPRequestHandler; -/** Register handler for prefix. + +/** + * Register handler for prefix. * If multiple handlers match a prefix, the first-registered one will * be invoked. */ void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler); + /** Unregister handler for prefix */ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch); -/** Return evhttp event base. This can be used by submodules to +/** + * Return evhttp event base. This can be used by submodules to * queue timers or custom events. */ struct event_base *EventBase(); -/** In-flight HTTP request. +/** + * In-flight HTTP request. * Thin C++ wrapper around evhttp_request. */ class HTTPRequest { @@ -66,16 +77,13 @@ enum RequestMethod { UNKNOWN, GET, POST, HEAD, PUT, OPTIONS }; - /** Get requested URI. - */ + /** Get requested URI */ std::string GetURI(); - /** Get CService (address:ip) for the origin of the http request. - */ + /** Get CService (address:ip) for the origin of the http request */ CService GetPeer(); - /** Get request method. - */ + /** Get request method */ RequestMethod GetRequestMethod(); /** @@ -112,20 +120,21 @@ void WriteReply(int nStatus, const std::string &strReply = ""); }; -/** Event handler closure. - */ +/** Event handler closure */ class HTTPClosure { public: virtual void operator()() = 0; virtual ~HTTPClosure() {} }; -/** Event class. This can be used either as an cross-thread trigger or as a +/** + * Event class. This can be used either as an cross-thread trigger or as a * timer. */ class HTTPEvent { public: - /** Create a new event. + /** + * 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. @@ -134,9 +143,9 @@ const std::function &handler); ~HTTPEvent(); - /** Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger - * it after - * the given time has elapsed. + /** + * Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger + * it after the given time has elapsed. */ void trigger(struct timeval *tv); diff --git a/src/httpserver.cpp b/src/httpserver.cpp --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -1,4 +1,5 @@ // Copyright (c) 2015-2016 The Bitcoin Core developers +// Copyright (c) 2018 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -95,10 +96,9 @@ public: WorkQueue(size_t _maxDepth) : running(true), maxDepth(_maxDepth), numThreads(0) {} - /** Precondition: worker threads have all stopped - * (call WaitExit) - */ + /** Precondition: worker threads have all stopped (call WaitExit) */ ~WorkQueue() {} + /** Enqueue a work item */ bool Enqueue(WorkItem *item) { std::unique_lock lock(cs); @@ -109,6 +109,7 @@ cond.notify_one(); return true; } + /** Thread function */ void Run() { ThreadCounter count(*this); @@ -125,12 +126,14 @@ (*i)(); } } + /** Interrupt and exit loops */ void Interrupt() { std::unique_lock lock(cs); running = false; cond.notify_all(); } + /** Wait for worker threads to exit */ void WaitExit() { std::unique_lock lock(cs); @@ -403,15 +406,14 @@ evthread_use_pthreads(); #endif - // XXX RAII + // XXX RAII: Create a new event_base for Libevent use base = event_base_new(); if (!base) { LogPrintf("Couldn't create an event_base: exiting\n"); return false; } - /* Create a new evhttp object to handle requests. */ - // XXX RAII + // XXX RAII: Create a new evhttp object to handle requests http = evhttp_new(base); if (!http) { LogPrintf("couldn't create evhttp. Exiting.\n"); @@ -573,13 +575,15 @@ 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, + /** + * 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); + // returns nullptr in case of empty buffer. if (!data) { return ""; @@ -596,7 +600,8 @@ 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. +/** + * 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. */