LCOV - code coverage report
Current view: top level - src - httpserver.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 7 423 1.7 %
Date: 2023-11-04 20:49:09 Functions: 0 62 0.0 %
Branches: 0 723 0.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2015-2022 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #if defined(HAVE_CONFIG_H)
       6                 :            : #include <config/bitcoin-config.h>
       7                 :            : #endif
       8                 :            : 
       9                 :            : #include <httpserver.h>
      10                 :            : 
      11                 :            : #include <chainparamsbase.h>
      12                 :            : #include <common/args.h>
      13                 :            : #include <compat/compat.h>
      14                 :            : #include <logging.h>
      15                 :            : #include <netbase.h>
      16                 :            : #include <node/interface_ui.h>
      17                 :            : #include <rpc/protocol.h> // For HTTP status codes
      18                 :            : #include <shutdown.h>
      19                 :            : #include <sync.h>
      20                 :            : #include <util/strencodings.h>
      21                 :            : #include <util/threadnames.h>
      22                 :            : #include <util/translation.h>
      23                 :            : 
      24                 :            : #include <condition_variable>
      25                 :            : #include <cstdio>
      26                 :            : #include <cstdlib>
      27                 :            : #include <deque>
      28                 :            : #include <memory>
      29                 :            : #include <optional>
      30                 :            : #include <string>
      31                 :            : #include <unordered_set>
      32                 :            : 
      33                 :            : #include <sys/types.h>
      34                 :            : #include <sys/stat.h>
      35                 :            : 
      36                 :            : #include <event2/buffer.h>
      37                 :            : #include <event2/bufferevent.h>
      38                 :            : #include <event2/http.h>
      39                 :            : #include <event2/http_struct.h>
      40                 :            : #include <event2/keyvalq_struct.h>
      41                 :            : #include <event2/thread.h>
      42                 :            : #include <event2/util.h>
      43                 :            : 
      44                 :            : #include <support/events.h>
      45                 :            : 
      46                 :            : /** Maximum size of http request (request line + headers) */
      47                 :            : static const size_t MAX_HEADERS_SIZE = 8192;
      48                 :            : 
      49                 :            : /** HTTP request work item */
      50                 :          0 : class HTTPWorkItem final : public HTTPClosure
      51                 :            : {
      52                 :            : public:
      53                 :          0 :     HTTPWorkItem(std::unique_ptr<HTTPRequest> _req, const std::string &_path, const HTTPRequestHandler& _func):
      54 [ #  # ][ #  # ]:          0 :         req(std::move(_req)), path(_path), func(_func)
      55                 :          0 :     {
      56                 :          0 :     }
      57                 :          0 :     void operator()() override
      58                 :            :     {
      59                 :          0 :         func(req.get(), path);
      60                 :          0 :     }
      61                 :            : 
      62                 :            :     std::unique_ptr<HTTPRequest> req;
      63                 :            : 
      64                 :            : private:
      65                 :            :     std::string path;
      66                 :            :     HTTPRequestHandler func;
      67                 :            : };
      68                 :            : 
      69                 :            : /** Simple work queue for distributing work over multiple threads.
      70                 :            :  * Work items are simply callable objects.
      71                 :            :  */
      72                 :            : template <typename WorkItem>
      73                 :            : class WorkQueue
      74                 :            : {
      75                 :            : private:
      76                 :            :     Mutex cs;
      77                 :            :     std::condition_variable cond GUARDED_BY(cs);
      78                 :            :     std::deque<std::unique_ptr<WorkItem>> queue GUARDED_BY(cs);
      79                 :          0 :     bool running GUARDED_BY(cs){true};
      80                 :            :     const size_t maxDepth;
      81                 :            : 
      82                 :            : public:
      83         [ #  # ]:          0 :     explicit WorkQueue(size_t _maxDepth) : maxDepth(_maxDepth)
      84                 :            :     {
      85                 :          0 :     }
      86                 :            :     /** Precondition: worker threads have all stopped (they have been joined).
      87                 :            :      */
      88                 :          0 :     ~WorkQueue() = default;
      89                 :            :     /** Enqueue a work item */
      90                 :          0 :     bool Enqueue(WorkItem* item) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      91                 :            :     {
      92                 :          0 :         LOCK(cs);
      93 [ #  # ][ #  # ]:          0 :         if (!running || queue.size() >= maxDepth) {
      94                 :          0 :             return false;
      95                 :            :         }
      96         [ #  # ]:          0 :         queue.emplace_back(std::unique_ptr<WorkItem>(item));
      97                 :          0 :         cond.notify_one();
      98                 :          0 :         return true;
      99                 :          0 :     }
     100                 :            :     /** Thread function */
     101                 :          0 :     void Run() EXCLUSIVE_LOCKS_REQUIRED(!cs)
     102                 :            :     {
     103                 :          0 :         while (true) {
     104                 :          0 :             std::unique_ptr<WorkItem> i;
     105                 :            :             {
     106         [ #  # ]:          0 :                 WAIT_LOCK(cs, lock);
     107 [ #  # ][ #  # ]:          0 :                 while (running && queue.empty())
     108         [ #  # ]:          0 :                     cond.wait(lock);
     109 [ #  # ][ #  # ]:          0 :                 if (!running && queue.empty())
     110                 :          0 :                     break;
     111                 :          0 :                 i = std::move(queue.front());
     112                 :          0 :                 queue.pop_front();
     113         [ #  # ]:          0 :             }
     114         [ #  # ]:          0 :             (*i)();
     115      [ #  #  # ]:          0 :         }
     116                 :          0 :     }
     117                 :            :     /** Interrupt and exit loops */
     118                 :          0 :     void Interrupt() EXCLUSIVE_LOCKS_REQUIRED(!cs)
     119                 :            :     {
     120                 :          0 :         LOCK(cs);
     121                 :          0 :         running = false;
     122                 :          0 :         cond.notify_all();
     123                 :          0 :     }
     124                 :            : };
     125                 :            : 
     126                 :          0 : struct HTTPPathHandler
     127                 :            : {
     128                 :          0 :     HTTPPathHandler(std::string _prefix, bool _exactMatch, HTTPRequestHandler _handler):
     129         [ #  # ]:          0 :         prefix(_prefix), exactMatch(_exactMatch), handler(_handler)
     130                 :            :     {
     131                 :          0 :     }
     132                 :            :     std::string prefix;
     133                 :            :     bool exactMatch;
     134                 :            :     HTTPRequestHandler handler;
     135                 :            : };
     136                 :            : 
     137                 :            : /** HTTP module state */
     138                 :            : 
     139                 :            : //! libevent event loop
     140                 :            : static struct event_base* eventBase = nullptr;
     141                 :            : //! HTTP server
     142                 :            : static struct evhttp* eventHTTP = nullptr;
     143                 :            : //! List of subnets to allow RPC connections from
     144                 :          2 : static std::vector<CSubNet> rpc_allow_subnets;
     145                 :            : //! Work queue for handling longer requests off the event loop thread
     146                 :            : static std::unique_ptr<WorkQueue<HTTPClosure>> g_work_queue{nullptr};
     147                 :            : //! Handlers for (sub)paths
     148                 :            : static GlobalMutex g_httppathhandlers_mutex;
     149                 :          2 : static std::vector<HTTPPathHandler> pathHandlers GUARDED_BY(g_httppathhandlers_mutex);
     150                 :            : //! Bound listening sockets
     151                 :          2 : static std::vector<evhttp_bound_socket *> boundSockets;
     152                 :            : //! Track active requests
     153                 :            : static GlobalMutex g_requests_mutex;
     154                 :          2 : static std::condition_variable g_requests_cv;
     155                 :          2 : static std::unordered_set<evhttp_request*> g_requests GUARDED_BY(g_requests_mutex);
     156                 :            : 
     157                 :            : /** Check if a network address is allowed to access the HTTP server */
     158                 :          0 : static bool ClientAllowed(const CNetAddr& netaddr)
     159                 :            : {
     160         [ #  # ]:          0 :     if (!netaddr.IsValid())
     161                 :          0 :         return false;
     162         [ #  # ]:          0 :     for(const CSubNet& subnet : rpc_allow_subnets)
     163         [ #  # ]:          0 :         if (subnet.Match(netaddr))
     164                 :          0 :             return true;
     165                 :          0 :     return false;
     166                 :          0 : }
     167                 :            : 
     168                 :            : /** Initialize ACL list for HTTP server */
     169                 :          0 : static bool InitHTTPAllowList()
     170                 :            : {
     171                 :          0 :     rpc_allow_subnets.clear();
     172 [ #  # ][ #  # ]:          0 :     rpc_allow_subnets.push_back(CSubNet{LookupHost("127.0.0.1", false).value(), 8});  // always allow IPv4 local subnet
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     173 [ #  # ][ #  # ]:          0 :     rpc_allow_subnets.push_back(CSubNet{LookupHost("::1", false).value()});  // always allow IPv6 localhost
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     174 [ #  # ][ #  # ]:          0 :     for (const std::string& strAllow : gArgs.GetArgs("-rpcallowip")) {
                 [ #  # ]
              [ #  #  # ]
     175         [ #  # ]:          0 :         CSubNet subnet;
     176         [ #  # ]:          0 :         LookupSubNet(strAllow, subnet);
     177 [ #  # ][ #  # ]:          0 :         if (!subnet.IsValid()) {
     178         [ #  # ]:          0 :             uiInterface.ThreadSafeMessageBox(
     179 [ #  # ][ #  # ]:          0 :                 strprintf(Untranslated("Invalid -rpcallowip subnet specification: %s. Valid are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24)."), strAllow),
                 [ #  # ]
     180         [ #  # ]:          0 :                 "", CClientUIInterface::MSG_ERROR);
     181                 :          0 :             return false;
     182                 :            :         }
     183         [ #  # ]:          0 :         rpc_allow_subnets.push_back(subnet);
     184         [ #  # ]:          0 :     }
     185                 :          0 :     std::string strAllowed;
     186         [ #  # ]:          0 :     for (const CSubNet& subnet : rpc_allow_subnets)
     187 [ #  # ][ #  # ]:          0 :         strAllowed += subnet.ToString() + " ";
                 [ #  # ]
     188 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed);
         [ #  # ][ #  # ]
                 [ #  # ]
     189                 :          0 :     return true;
     190                 :          0 : }
     191                 :            : 
     192                 :            : /** HTTP request method as string - use for logging only */
     193                 :          0 : std::string RequestMethodString(HTTPRequest::RequestMethod m)
     194                 :            : {
     195   [ #  #  #  #  :          0 :     switch (m) {
                   #  # ]
     196                 :            :     case HTTPRequest::GET:
     197         [ #  # ]:          0 :         return "GET";
     198                 :            :     case HTTPRequest::POST:
     199         [ #  # ]:          0 :         return "POST";
     200                 :            :     case HTTPRequest::HEAD:
     201         [ #  # ]:          0 :         return "HEAD";
     202                 :            :     case HTTPRequest::PUT:
     203         [ #  # ]:          0 :         return "PUT";
     204                 :            :     case HTTPRequest::UNKNOWN:
     205         [ #  # ]:          0 :         return "unknown";
     206                 :            :     } // no default case, so the compiler can warn about missing cases
     207                 :          0 :     assert(false);
     208                 :          0 : }
     209                 :            : 
     210                 :            : /** HTTP request callback */
     211                 :          0 : static void http_request_cb(struct evhttp_request* req, void* arg)
     212                 :            : {
     213                 :            :     // Track requests and notify when a request is completed.
     214                 :            :     {
     215         [ #  # ]:          0 :         WITH_LOCK(g_requests_mutex, g_requests.insert(req));
     216                 :          0 :         g_requests_cv.notify_all();
     217                 :          0 :         evhttp_request_set_on_complete_cb(req, [](struct evhttp_request* req, void*) {
     218         [ #  # ]:          0 :             auto n{WITH_LOCK(g_requests_mutex, return g_requests.erase(req))};
     219         [ #  # ]:          0 :             assert(n == 1);
     220                 :          0 :             g_requests_cv.notify_all();
     221                 :          0 :         }, nullptr);
     222                 :            :     }
     223                 :            : 
     224                 :            :     // Disable reading to work around a libevent bug, fixed in 2.1.9
     225                 :            :     // See https://github.com/libevent/libevent/commit/5ff8eb26371c4dc56f384b2de35bea2d87814779
     226                 :            :     // and https://github.com/bitcoin/bitcoin/pull/11593.
     227 [ #  # ][ #  # ]:          0 :     if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02010900) {
     228                 :          0 :         evhttp_connection* conn = evhttp_request_get_connection(req);
     229         [ #  # ]:          0 :         if (conn) {
     230                 :          0 :             bufferevent* bev = evhttp_connection_get_bufferevent(conn);
     231         [ #  # ]:          0 :             if (bev) {
     232                 :          0 :                 bufferevent_disable(bev, EV_READ);
     233                 :          0 :             }
     234                 :          0 :         }
     235                 :          0 :     }
     236         [ #  # ]:          0 :     std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
     237                 :            : 
     238                 :            :     // Early address-based allow check
     239 [ #  # ][ #  # ]:          0 :     if (!ClientAllowed(hreq->GetPeer())) {
                 [ #  # ]
     240 [ #  # ][ #  # ]:          0 :         LogPrint(BCLog::HTTP, "HTTP request from %s rejected: Client network is not allowed RPC access\n",
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     241                 :            :                  hreq->GetPeer().ToStringAddrPort());
     242 [ #  # ][ #  # ]:          0 :         hreq->WriteReply(HTTP_FORBIDDEN);
     243                 :          0 :         return;
     244                 :            :     }
     245                 :            : 
     246                 :            :     // Early reject unknown HTTP methods
     247 [ #  # ][ #  # ]:          0 :     if (hreq->GetRequestMethod() == HTTPRequest::UNKNOWN) {
     248 [ #  # ][ #  # ]:          0 :         LogPrint(BCLog::HTTP, "HTTP request from %s rejected: Unknown HTTP request method\n",
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     249                 :            :                  hreq->GetPeer().ToStringAddrPort());
     250 [ #  # ][ #  # ]:          0 :         hreq->WriteReply(HTTP_BAD_METHOD);
     251                 :          0 :         return;
     252                 :            :     }
     253                 :            : 
     254 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Received a %s request for %s from %s\n",
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     255                 :            :              RequestMethodString(hreq->GetRequestMethod()), SanitizeString(hreq->GetURI(), SAFE_CHARS_URI).substr(0, 100), hreq->GetPeer().ToStringAddrPort());
     256                 :            : 
     257                 :            :     // Find registered handler for prefix
     258         [ #  # ]:          0 :     std::string strURI = hreq->GetURI();
     259                 :          0 :     std::string path;
     260 [ #  # ][ #  # ]:          0 :     LOCK(g_httppathhandlers_mutex);
     261                 :          0 :     std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
     262                 :          0 :     std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
     263         [ #  # ]:          0 :     for (; i != iend; ++i) {
     264                 :          0 :         bool match = false;
     265         [ #  # ]:          0 :         if (i->exactMatch)
     266                 :          0 :             match = (strURI == i->prefix);
     267                 :            :         else
     268         [ #  # ]:          0 :             match = (strURI.substr(0, i->prefix.size()) == i->prefix);
     269         [ #  # ]:          0 :         if (match) {
     270         [ #  # ]:          0 :             path = strURI.substr(i->prefix.size());
     271                 :          0 :             break;
     272                 :            :         }
     273                 :          0 :     }
     274                 :            : 
     275                 :            :     // Dispatch to worker thread
     276         [ #  # ]:          0 :     if (i != iend) {
     277 [ #  # ][ #  # ]:          0 :         std::unique_ptr<HTTPWorkItem> item(new HTTPWorkItem(std::move(hreq), path, i->handler));
                 [ #  # ]
     278         [ #  # ]:          0 :         assert(g_work_queue);
     279 [ #  # ][ #  # ]:          0 :         if (g_work_queue->Enqueue(item.get())) {
     280                 :          0 :             item.release(); /* if true, queue took ownership */
     281                 :          0 :         } else {
     282 [ #  # ][ #  # ]:          0 :             LogPrintf("WARNING: request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting\n");
                 [ #  # ]
     283 [ #  # ][ #  # ]:          0 :             item->req->WriteReply(HTTP_SERVICE_UNAVAILABLE, "Work queue depth exceeded");
     284                 :            :         }
     285                 :          0 :     } else {
     286 [ #  # ][ #  # ]:          0 :         hreq->WriteReply(HTTP_NOT_FOUND);
     287                 :            :     }
     288         [ #  # ]:          0 : }
     289                 :            : 
     290                 :            : /** Callback to reject HTTP requests after shutdown. */
     291                 :          0 : static void http_reject_request_cb(struct evhttp_request* req, void*)
     292                 :            : {
     293 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Rejecting request while shutting down\n");
         [ #  # ][ #  # ]
     294                 :          0 :     evhttp_send_error(req, HTTP_SERVUNAVAIL, nullptr);
     295                 :          0 : }
     296                 :            : 
     297                 :            : /** Event dispatcher thread */
     298                 :          0 : static void ThreadHTTP(struct event_base* base)
     299                 :            : {
     300 [ #  # ][ #  # ]:          0 :     util::ThreadRename("http");
     301 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Entering http event loop\n");
         [ #  # ][ #  # ]
     302                 :          0 :     event_base_dispatch(base);
     303                 :            :     // Event loop will be interrupted by InterruptHTTPServer()
     304 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Exited http event loop\n");
         [ #  # ][ #  # ]
     305                 :          0 : }
     306                 :            : 
     307                 :            : /** Bind HTTP server to specified addresses */
     308                 :          0 : static bool HTTPBindAddresses(struct evhttp* http)
     309                 :            : {
     310 [ #  # ][ #  # ]:          0 :     uint16_t http_port{static_cast<uint16_t>(gArgs.GetIntArg("-rpcport", BaseParams().RPCPort()))};
         [ #  # ][ #  # ]
     311                 :          0 :     std::vector<std::pair<std::string, uint16_t>> endpoints;
     312                 :            : 
     313                 :            :     // Determine what addresses to bind to
     314 [ #  # ][ #  # ]:          0 :     if (!(gArgs.IsArgSet("-rpcallowip") && gArgs.IsArgSet("-rpcbind"))) { // Default to loopback if not allowing external IPs
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     315 [ #  # ][ #  # ]:          0 :         endpoints.push_back(std::make_pair("::1", http_port));
                 [ #  # ]
     316 [ #  # ][ #  # ]:          0 :         endpoints.push_back(std::make_pair("127.0.0.1", http_port));
                 [ #  # ]
     317 [ #  # ][ #  # ]:          0 :         if (gArgs.IsArgSet("-rpcallowip")) {
                 [ #  # ]
     318 [ #  # ][ #  # ]:          0 :             LogPrintf("WARNING: option -rpcallowip was specified without -rpcbind; this doesn't usually make sense\n");
                 [ #  # ]
     319                 :          0 :         }
     320 [ #  # ][ #  # ]:          0 :         if (gArgs.IsArgSet("-rpcbind")) {
                 [ #  # ]
     321 [ #  # ][ #  # ]:          0 :             LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n");
                 [ #  # ]
     322                 :          0 :         }
     323 [ #  # ][ #  # ]:          0 :     } else if (gArgs.IsArgSet("-rpcbind")) { // Specific bind address
                 [ #  # ]
     324 [ #  # ][ #  # ]:          0 :         for (const std::string& strRPCBind : gArgs.GetArgs("-rpcbind")) {
                 [ #  # ]
     325                 :          0 :             uint16_t port{http_port};
     326                 :          0 :             std::string host;
     327         [ #  # ]:          0 :             SplitHostPort(strRPCBind, port, host);
     328 [ #  # ][ #  # ]:          0 :             endpoints.push_back(std::make_pair(host, port));
     329                 :          0 :         }
     330                 :          0 :     }
     331                 :            : 
     332                 :            :     // Bind addresses
     333         [ #  # ]:          0 :     for (std::vector<std::pair<std::string, uint16_t> >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) {
     334 [ #  # ][ #  # ]:          0 :         LogPrintf("Binding RPC on address %s port %i\n", i->first, i->second);
                 [ #  # ]
     335 [ #  # ][ #  # ]:          0 :         evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? nullptr : i->first.c_str(), i->second);
     336         [ #  # ]:          0 :         if (bind_handle) {
     337 [ #  # ][ #  # ]:          0 :             const std::optional<CNetAddr> addr{LookupHost(i->first, false)};
     338 [ #  # ][ #  # ]:          0 :             if (i->first.empty() || (addr.has_value() && addr->IsBindAny())) {
         [ #  # ][ #  # ]
     339 [ #  # ][ #  # ]:          0 :                 LogPrintf("WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet\n");
                 [ #  # ]
     340                 :          0 :             }
     341         [ #  # ]:          0 :             boundSockets.push_back(bind_handle);
     342                 :          0 :         } else {
     343 [ #  # ][ #  # ]:          0 :             LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second);
                 [ #  # ]
     344                 :            :         }
     345                 :          0 :     }
     346                 :          0 :     return !boundSockets.empty();
     347                 :          0 : }
     348                 :            : 
     349                 :            : /** Simple wrapper to set thread name and run work queue */
     350                 :          0 : static void HTTPWorkQueueRun(WorkQueue<HTTPClosure>* queue, int worker_num)
     351                 :            : {
     352         [ #  # ]:          0 :     util::ThreadRename(strprintf("httpworker.%i", worker_num));
     353                 :          0 :     queue->Run();
     354                 :          0 : }
     355                 :            : 
     356                 :            : /** libevent event log callback */
     357                 :          0 : static void libevent_log_cb(int severity, const char *msg)
     358                 :            : {
     359                 :            :     BCLog::Level level;
     360   [ #  #  #  # ]:          0 :     switch (severity) {
     361                 :            :     case EVENT_LOG_DEBUG:
     362                 :          0 :         level = BCLog::Level::Debug;
     363                 :          0 :         break;
     364                 :            :     case EVENT_LOG_MSG:
     365                 :          0 :         level = BCLog::Level::Info;
     366                 :          0 :         break;
     367                 :            :     case EVENT_LOG_WARN:
     368                 :          0 :         level = BCLog::Level::Warning;
     369                 :          0 :         break;
     370                 :            :     default: // EVENT_LOG_ERR and others are mapped to error
     371                 :          0 :         level = BCLog::Level::Error;
     372                 :          0 :         break;
     373                 :            :     }
     374 [ #  # ][ #  # ]:          0 :     LogPrintLevel(BCLog::LIBEVENT, level, "%s\n", msg);
         [ #  # ][ #  # ]
     375                 :          0 : }
     376                 :            : 
     377                 :          0 : bool InitHTTPServer()
     378                 :            : {
     379         [ #  # ]:          0 :     if (!InitHTTPAllowList())
     380                 :          0 :         return false;
     381                 :            : 
     382                 :            :     // Redirect libevent's logging to our own log
     383                 :          0 :     event_set_log_callback(&libevent_log_cb);
     384                 :            :     // Update libevent's log handling.
     385                 :          0 :     UpdateHTTPServerLogging(LogInstance().WillLogCategory(BCLog::LIBEVENT));
     386                 :            : 
     387                 :            : #ifdef WIN32
     388                 :            :     evthread_use_windows_threads();
     389                 :            : #else
     390                 :          0 :     evthread_use_pthreads();
     391                 :            : #endif
     392                 :            : 
     393                 :          0 :     raii_event_base base_ctr = obtain_event_base();
     394                 :            : 
     395                 :            :     /* Create a new evhttp object to handle requests. */
     396         [ #  # ]:          0 :     raii_evhttp http_ctr = obtain_evhttp(base_ctr.get());
     397                 :          0 :     struct evhttp* http = http_ctr.get();
     398         [ #  # ]:          0 :     if (!http) {
     399 [ #  # ][ #  # ]:          0 :         LogPrintf("couldn't create evhttp. Exiting.\n");
                 [ #  # ]
     400                 :          0 :         return false;
     401                 :            :     }
     402                 :            : 
     403 [ #  # ][ #  # ]:          0 :     evhttp_set_timeout(http, gArgs.GetIntArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT));
                 [ #  # ]
     404         [ #  # ]:          0 :     evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE);
     405         [ #  # ]:          0 :     evhttp_set_max_body_size(http, MAX_SIZE);
     406         [ #  # ]:          0 :     evhttp_set_gencb(http, http_request_cb, nullptr);
     407                 :            : 
     408 [ #  # ][ #  # ]:          0 :     if (!HTTPBindAddresses(http)) {
     409 [ #  # ][ #  # ]:          0 :         LogPrintf("Unable to bind any endpoint for RPC server\n");
                 [ #  # ]
     410                 :          0 :         return false;
     411                 :            :     }
     412                 :            : 
     413 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Initialized HTTP server\n");
         [ #  # ][ #  # ]
                 [ #  # ]
     414 [ #  # ][ #  # ]:          0 :     int workQueueDepth = std::max((long)gArgs.GetIntArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
                 [ #  # ]
     415 [ #  # ][ #  # ]:          0 :     LogPrintfCategory(BCLog::HTTP, "creating work queue of depth %d\n", workQueueDepth);
                 [ #  # ]
     416                 :            : 
     417         [ #  # ]:          0 :     g_work_queue = std::make_unique<WorkQueue<HTTPClosure>>(workQueueDepth);
     418                 :            :     // transfer ownership to eventBase/HTTP via .release()
     419                 :          0 :     eventBase = base_ctr.release();
     420                 :          0 :     eventHTTP = http_ctr.release();
     421                 :          0 :     return true;
     422                 :          0 : }
     423                 :            : 
     424                 :          0 : void UpdateHTTPServerLogging(bool enable) {
     425         [ #  # ]:          0 :     if (enable) {
     426                 :          0 :         event_enable_debug_logging(EVENT_DBG_ALL);
     427                 :          0 :     } else {
     428                 :          0 :         event_enable_debug_logging(EVENT_DBG_NONE);
     429                 :            :     }
     430                 :          0 : }
     431                 :            : 
     432                 :          2 : static std::thread g_thread_http;
     433                 :          2 : static std::vector<std::thread> g_thread_http_workers;
     434                 :            : 
     435                 :          0 : void StartHTTPServer()
     436                 :            : {
     437 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Starting HTTP server\n");
         [ #  # ][ #  # ]
     438 [ #  # ][ #  # ]:          0 :     int rpcThreads = std::max((long)gArgs.GetIntArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
                 [ #  # ]
     439 [ #  # ][ #  # ]:          0 :     LogPrintfCategory(BCLog::HTTP, "starting %d worker threads\n", rpcThreads);
                 [ #  # ]
     440                 :          0 :     g_thread_http = std::thread(ThreadHTTP, eventBase);
     441                 :            : 
     442         [ #  # ]:          0 :     for (int i = 0; i < rpcThreads; i++) {
     443                 :          0 :         g_thread_http_workers.emplace_back(HTTPWorkQueueRun, g_work_queue.get(), i);
     444                 :          0 :     }
     445                 :          0 : }
     446                 :            : 
     447                 :          0 : void InterruptHTTPServer()
     448                 :            : {
     449 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Interrupting HTTP server\n");
         [ #  # ][ #  # ]
     450         [ #  # ]:          0 :     if (eventHTTP) {
     451                 :            :         // Reject requests on current connections
     452                 :          0 :         evhttp_set_gencb(eventHTTP, http_reject_request_cb, nullptr);
     453                 :          0 :     }
     454         [ #  # ]:          0 :     if (g_work_queue) {
     455                 :          0 :         g_work_queue->Interrupt();
     456                 :          0 :     }
     457                 :          0 : }
     458                 :            : 
     459                 :          0 : void StopHTTPServer()
     460                 :            : {
     461 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Stopping HTTP server\n");
         [ #  # ][ #  # ]
     462         [ #  # ]:          0 :     if (g_work_queue) {
     463 [ #  # ][ #  # ]:          0 :         LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
         [ #  # ][ #  # ]
     464         [ #  # ]:          0 :         for (auto& thread : g_thread_http_workers) {
     465                 :          0 :             thread.join();
     466                 :            :         }
     467                 :          0 :         g_thread_http_workers.clear();
     468                 :          0 :     }
     469                 :            :     // Unlisten sockets, these are what make the event loop running, which means
     470                 :            :     // that after this and all connections are closed the event loop will quit.
     471         [ #  # ]:          0 :     for (evhttp_bound_socket *socket : boundSockets) {
     472                 :          0 :         evhttp_del_accept_socket(eventHTTP, socket);
     473                 :            :     }
     474                 :          0 :     boundSockets.clear();
     475                 :            :     {
     476                 :          0 :         WAIT_LOCK(g_requests_mutex, lock);
     477         [ #  # ]:          0 :         if (!g_requests.empty()) {
     478 [ #  # ][ #  # ]:          0 :             LogPrint(BCLog::HTTP, "Waiting for %d requests to stop HTTP server\n", g_requests.size());
         [ #  # ][ #  # ]
                 [ #  # ]
     479                 :          0 :         }
     480         [ #  # ]:          0 :         g_requests_cv.wait(lock, []() EXCLUSIVE_LOCKS_REQUIRED(g_requests_mutex) {
     481                 :          0 :             return g_requests.empty();
     482                 :            :         });
     483                 :          0 :     }
     484         [ #  # ]:          0 :     if (eventHTTP) {
     485                 :            :         // Schedule a callback to call evhttp_free in the event base thread, so
     486                 :            :         // that evhttp_free does not need to be called again after the handling
     487                 :            :         // of unfinished request connections that follows.
     488                 :          0 :         event_base_once(eventBase, -1, EV_TIMEOUT, [](evutil_socket_t, short, void*) {
     489                 :          0 :             evhttp_free(eventHTTP);
     490                 :          0 :             eventHTTP = nullptr;
     491                 :          0 :         }, nullptr, nullptr);
     492                 :          0 :     }
     493         [ #  # ]:          0 :     if (eventBase) {
     494 [ #  # ][ #  # ]:          0 :         LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
         [ #  # ][ #  # ]
     495         [ #  # ]:          0 :         if (g_thread_http.joinable()) g_thread_http.join();
     496                 :          0 :         event_base_free(eventBase);
     497                 :          0 :         eventBase = nullptr;
     498                 :          0 :     }
     499                 :          0 :     g_work_queue.reset();
     500 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Stopped HTTP server\n");
         [ #  # ][ #  # ]
     501                 :          0 : }
     502                 :            : 
     503                 :          0 : struct event_base* EventBase()
     504                 :            : {
     505                 :          0 :     return eventBase;
     506                 :            : }
     507                 :            : 
     508                 :          0 : static void httpevent_callback_fn(evutil_socket_t, short, void* data)
     509                 :            : {
     510                 :            :     // Static handler: simply call inner handler
     511                 :          0 :     HTTPEvent *self = static_cast<HTTPEvent*>(data);
     512                 :          0 :     self->handler();
     513         [ #  # ]:          0 :     if (self->deleteWhenTriggered)
     514         [ #  # ]:          0 :         delete self;
     515                 :          0 : }
     516                 :            : 
     517                 :          0 : HTTPEvent::HTTPEvent(struct event_base* base, bool _deleteWhenTriggered, const std::function<void()>& _handler):
     518                 :          0 :     deleteWhenTriggered(_deleteWhenTriggered), handler(_handler)
     519                 :            : {
     520         [ #  # ]:          0 :     ev = event_new(base, -1, 0, httpevent_callback_fn, this);
     521         [ #  # ]:          0 :     assert(ev);
     522                 :          0 : }
     523                 :          0 : HTTPEvent::~HTTPEvent()
     524                 :            : {
     525         [ #  # ]:          0 :     event_free(ev);
     526                 :          0 : }
     527                 :          0 : void HTTPEvent::trigger(struct timeval* tv)
     528                 :            : {
     529         [ #  # ]:          0 :     if (tv == nullptr)
     530                 :          0 :         event_active(ev, 0, 0); // immediately trigger event in main thread
     531                 :            :     else
     532                 :          0 :         evtimer_add(ev, tv); // trigger after timeval passed
     533                 :          0 : }
     534                 :          0 : HTTPRequest::HTTPRequest(struct evhttp_request* _req, bool _replySent) : req(_req), replySent(_replySent)
     535                 :            : {
     536                 :          0 : }
     537                 :            : 
     538                 :          0 : HTTPRequest::~HTTPRequest()
     539                 :            : {
     540         [ #  # ]:          0 :     if (!replySent) {
     541                 :            :         // Keep track of whether reply was sent to avoid request leaks
     542 [ #  # ][ #  # ]:          0 :         LogPrintf("%s: Unhandled request\n", __func__);
                 [ #  # ]
     543 [ #  # ][ #  # ]:          0 :         WriteReply(HTTP_INTERNAL_SERVER_ERROR, "Unhandled request");
     544                 :          0 :     }
     545                 :            :     // evhttpd cleans up the request, as long as a reply was sent.
     546                 :          0 : }
     547                 :            : 
     548                 :          0 : std::pair<bool, std::string> HTTPRequest::GetHeader(const std::string& hdr) const
     549                 :            : {
     550                 :          0 :     const struct evkeyvalq* headers = evhttp_request_get_input_headers(req);
     551         [ #  # ]:          0 :     assert(headers);
     552                 :          0 :     const char* val = evhttp_find_header(headers, hdr.c_str());
     553         [ #  # ]:          0 :     if (val)
     554                 :          0 :         return std::make_pair(true, val);
     555                 :            :     else
     556                 :          0 :         return std::make_pair(false, "");
     557                 :          0 : }
     558                 :            : 
     559                 :          0 : std::string HTTPRequest::ReadBody()
     560                 :            : {
     561                 :          0 :     struct evbuffer* buf = evhttp_request_get_input_buffer(req);
     562         [ #  # ]:          0 :     if (!buf)
     563         [ #  # ]:          0 :         return "";
     564                 :          0 :     size_t size = evbuffer_get_length(buf);
     565                 :            :     /** Trivial implementation: if this is ever a performance bottleneck,
     566                 :            :      * internal copying can be avoided in multi-segment buffers by using
     567                 :            :      * evbuffer_peek and an awkward loop. Though in that case, it'd be even
     568                 :            :      * better to not copy into an intermediate string but use a stream
     569                 :            :      * abstraction to consume the evbuffer on the fly in the parsing algorithm.
     570                 :            :      */
     571                 :          0 :     const char* data = (const char*)evbuffer_pullup(buf, size);
     572         [ #  # ]:          0 :     if (!data) // returns nullptr in case of empty buffer
     573         [ #  # ]:          0 :         return "";
     574         [ #  # ]:          0 :     std::string rv(data, size);
     575         [ #  # ]:          0 :     evbuffer_drain(buf, size);
     576                 :          0 :     return rv;
     577         [ #  # ]:          0 : }
     578                 :            : 
     579                 :          0 : void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
     580                 :            : {
     581                 :          0 :     struct evkeyvalq* headers = evhttp_request_get_output_headers(req);
     582         [ #  # ]:          0 :     assert(headers);
     583                 :          0 :     evhttp_add_header(headers, hdr.c_str(), value.c_str());
     584                 :          0 : }
     585                 :            : 
     586                 :            : /** Closure sent to main thread to request a reply to be sent to
     587                 :            :  * a HTTP request.
     588                 :            :  * Replies must be sent in the main loop in the main http thread,
     589                 :            :  * this cannot be done from worker threads.
     590                 :            :  */
     591                 :          0 : void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
     592                 :            : {
     593 [ #  # ][ #  # ]:          0 :     assert(!replySent && req);
     594         [ #  # ]:          0 :     if (ShutdownRequested()) {
     595 [ #  # ][ #  # ]:          0 :         WriteHeader("Connection", "close");
                 [ #  # ]
     596                 :          0 :     }
     597                 :            :     // Send event to main http thread to send reply message
     598                 :          0 :     struct evbuffer* evb = evhttp_request_get_output_buffer(req);
     599         [ #  # ]:          0 :     assert(evb);
     600                 :          0 :     evbuffer_add(evb, strReply.data(), strReply.size());
     601                 :          0 :     auto req_copy = req;
     602 [ #  # ][ #  # ]:          0 :     HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus]{
     603                 :          0 :         evhttp_send_reply(req_copy, nStatus, nullptr, nullptr);
     604                 :            :         // Re-enable reading from the socket. This is the second part of the libevent
     605                 :            :         // workaround above.
     606 [ #  # ][ #  # ]:          0 :         if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02010900) {
     607                 :          0 :             evhttp_connection* conn = evhttp_request_get_connection(req_copy);
     608         [ #  # ]:          0 :             if (conn) {
     609                 :          0 :                 bufferevent* bev = evhttp_connection_get_bufferevent(conn);
     610         [ #  # ]:          0 :                 if (bev) {
     611                 :          0 :                     bufferevent_enable(bev, EV_READ | EV_WRITE);
     612                 :          0 :                 }
     613                 :          0 :             }
     614                 :          0 :         }
     615                 :          0 :     });
     616                 :          0 :     ev->trigger(nullptr);
     617                 :          0 :     replySent = true;
     618                 :          0 :     req = nullptr; // transferred back to main thread
     619                 :          0 : }
     620                 :            : 
     621                 :          0 : CService HTTPRequest::GetPeer() const
     622                 :            : {
     623                 :          0 :     evhttp_connection* con = evhttp_request_get_connection(req);
     624                 :          0 :     CService peer;
     625         [ #  # ]:          0 :     if (con) {
     626                 :            :         // evhttp retains ownership over returned address string
     627                 :          0 :         const char* address = "";
     628                 :          0 :         uint16_t port = 0;
     629                 :            : 
     630                 :            : #ifdef HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
     631                 :            :         evhttp_connection_get_peer(con, &address, &port);
     632                 :            : #else
     633         [ #  # ]:          0 :         evhttp_connection_get_peer(con, (char**)&address, &port);
     634                 :            : #endif // HAVE_EVHTTP_CONNECTION_GET_PEER_CONST_CHAR
     635                 :            : 
     636 [ #  # ][ #  # ]:          0 :         peer = LookupNumeric(address, port);
                 [ #  # ]
     637                 :          0 :     }
     638                 :          0 :     return peer;
     639         [ #  # ]:          0 : }
     640                 :            : 
     641                 :          0 : std::string HTTPRequest::GetURI() const
     642                 :            : {
     643         [ #  # ]:          0 :     return evhttp_request_get_uri(req);
     644                 :          0 : }
     645                 :            : 
     646                 :          0 : HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod() const
     647                 :            : {
     648   [ #  #  #  #  :          0 :     switch (evhttp_request_get_command(req)) {
                      # ]
     649                 :            :     case EVHTTP_REQ_GET:
     650                 :          0 :         return GET;
     651                 :            :     case EVHTTP_REQ_POST:
     652                 :          0 :         return POST;
     653                 :            :     case EVHTTP_REQ_HEAD:
     654                 :          0 :         return HEAD;
     655                 :            :     case EVHTTP_REQ_PUT:
     656                 :          0 :         return PUT;
     657                 :            :     default:
     658                 :          0 :         return UNKNOWN;
     659                 :            :     }
     660                 :          0 : }
     661                 :            : 
     662                 :          0 : std::optional<std::string> HTTPRequest::GetQueryParameter(const std::string& key) const
     663                 :            : {
     664                 :          0 :     const char* uri{evhttp_request_get_uri(req)};
     665                 :            : 
     666                 :          0 :     return GetQueryParameterFromUri(uri, key);
     667                 :            : }
     668                 :            : 
     669                 :          0 : std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key)
     670                 :            : {
     671                 :          0 :     evhttp_uri* uri_parsed{evhttp_uri_parse(uri)};
     672         [ #  # ]:          0 :     if (!uri_parsed) {
     673         [ #  # ]:          0 :         throw std::runtime_error("URI parsing failed, it likely contained RFC 3986 invalid characters");
     674                 :            :     }
     675                 :          0 :     const char* query{evhttp_uri_get_query(uri_parsed)};
     676                 :          0 :     std::optional<std::string> result;
     677                 :            : 
     678         [ #  # ]:          0 :     if (query) {
     679                 :            :         // Parse the query string into a key-value queue and iterate over it
     680                 :            :         struct evkeyvalq params_q;
     681         [ #  # ]:          0 :         evhttp_parse_query_str(query, &params_q);
     682                 :            : 
     683         [ #  # ]:          0 :         for (struct evkeyval* param{params_q.tqh_first}; param != nullptr; param = param->next.tqe_next) {
     684 [ #  # ][ #  # ]:          0 :             if (param->key == key) {
     685         [ #  # ]:          0 :                 result = param->value;
     686                 :          0 :                 break;
     687                 :            :             }
     688                 :          0 :         }
     689         [ #  # ]:          0 :         evhttp_clear_headers(&params_q);
     690                 :          0 :     }
     691         [ #  # ]:          0 :     evhttp_uri_free(uri_parsed);
     692                 :            : 
     693                 :          0 :     return result;
     694         [ #  # ]:          0 : }
     695                 :            : 
     696                 :          0 : void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
     697                 :            : {
     698 [ #  # ][ #  # ]:          0 :     LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
         [ #  # ][ #  # ]
     699                 :          0 :     LOCK(g_httppathhandlers_mutex);
     700 [ #  # ][ #  # ]:          0 :     pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler));
         [ #  # ][ #  # ]
     701                 :          0 : }
     702                 :            : 
     703                 :          0 : void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
     704                 :            : {
     705                 :          0 :     LOCK(g_httppathhandlers_mutex);
     706                 :          0 :     std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
     707                 :          0 :     std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
     708         [ #  # ]:          0 :     for (; i != iend; ++i)
     709 [ #  # ][ #  # ]:          0 :         if (i->prefix == prefix && i->exactMatch == exactMatch)
     710                 :          0 :             break;
     711         [ #  # ]:          0 :     if (i != iend)
     712                 :            :     {
     713 [ #  # ][ #  # ]:          0 :         LogPrint(BCLog::HTTP, "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
         [ #  # ][ #  # ]
                 [ #  # ]
     714         [ #  # ]:          0 :         pathHandlers.erase(i);
     715                 :          0 :     }
     716                 :          0 : }

Generated by: LCOV version 1.14