You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

httpserver.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. // Copyright (c) 2015-2016 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. #include "httpserver.h"
  5. #include "chainparamsbase.h"
  6. #include "compat.h"
  7. #include "util.h"
  8. #include "netbase.h"
  9. #include "rpc/protocol.h" // For HTTP status codes
  10. #include "sync.h"
  11. #include "ui_interface.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <signal.h>
  18. #include <future>
  19. #include <event2/event.h>
  20. #include <event2/http.h>
  21. #include <event2/thread.h>
  22. #include <event2/buffer.h>
  23. #include <event2/util.h>
  24. #include <event2/keyvalq_struct.h>
  25. #ifdef EVENT__HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #ifdef _XOPEN_SOURCE_EXTENDED
  28. #include <arpa/inet.h>
  29. #endif
  30. #endif
  31. /** Maximum size of http request (request line + headers) */
  32. static const size_t MAX_HEADERS_SIZE = 8192;
  33. /** HTTP request work item */
  34. class HTTPWorkItem : public HTTPClosure
  35. {
  36. public:
  37. HTTPWorkItem(std::unique_ptr<HTTPRequest> _req, const std::string &_path, const HTTPRequestHandler& _func):
  38. req(std::move(_req)), path(_path), func(_func)
  39. {
  40. }
  41. void operator()()
  42. {
  43. func(req.get(), path);
  44. }
  45. std::unique_ptr<HTTPRequest> req;
  46. private:
  47. std::string path;
  48. HTTPRequestHandler func;
  49. };
  50. /** Simple work queue for distributing work over multiple threads.
  51. * Work items are simply callable objects.
  52. */
  53. template <typename WorkItem>
  54. class WorkQueue
  55. {
  56. private:
  57. /** Mutex protects entire object */
  58. std::mutex cs;
  59. std::condition_variable cond;
  60. std::deque<std::unique_ptr<WorkItem>> queue;
  61. bool running;
  62. size_t maxDepth;
  63. int numThreads;
  64. /** RAII object to keep track of number of running worker threads */
  65. class ThreadCounter
  66. {
  67. public:
  68. WorkQueue &wq;
  69. ThreadCounter(WorkQueue &w): wq(w)
  70. {
  71. std::lock_guard<std::mutex> lock(wq.cs);
  72. wq.numThreads += 1;
  73. }
  74. ~ThreadCounter()
  75. {
  76. std::lock_guard<std::mutex> lock(wq.cs);
  77. wq.numThreads -= 1;
  78. wq.cond.notify_all();
  79. }
  80. };
  81. public:
  82. WorkQueue(size_t _maxDepth) : running(true),
  83. maxDepth(_maxDepth),
  84. numThreads(0)
  85. {
  86. }
  87. /** Precondition: worker threads have all stopped
  88. * (call WaitExit)
  89. */
  90. ~WorkQueue()
  91. {
  92. }
  93. /** Enqueue a work item */
  94. bool Enqueue(WorkItem* item)
  95. {
  96. std::unique_lock<std::mutex> lock(cs);
  97. if (queue.size() >= maxDepth) {
  98. return false;
  99. }
  100. queue.emplace_back(std::unique_ptr<WorkItem>(item));
  101. cond.notify_one();
  102. return true;
  103. }
  104. /** Thread function */
  105. void Run()
  106. {
  107. ThreadCounter count(*this);
  108. while (true) {
  109. std::unique_ptr<WorkItem> i;
  110. {
  111. std::unique_lock<std::mutex> lock(cs);
  112. while (running && queue.empty())
  113. cond.wait(lock);
  114. if (!running)
  115. break;
  116. i = std::move(queue.front());
  117. queue.pop_front();
  118. }
  119. (*i)();
  120. }
  121. }
  122. /** Interrupt and exit loops */
  123. void Interrupt()
  124. {
  125. std::unique_lock<std::mutex> lock(cs);
  126. running = false;
  127. cond.notify_all();
  128. }
  129. /** Wait for worker threads to exit */
  130. void WaitExit()
  131. {
  132. std::unique_lock<std::mutex> lock(cs);
  133. while (numThreads > 0)
  134. cond.wait(lock);
  135. }
  136. };
  137. struct HTTPPathHandler
  138. {
  139. HTTPPathHandler() {}
  140. HTTPPathHandler(std::string _prefix, bool _exactMatch, HTTPRequestHandler _handler):
  141. prefix(_prefix), exactMatch(_exactMatch), handler(_handler)
  142. {
  143. }
  144. std::string prefix;
  145. bool exactMatch;
  146. HTTPRequestHandler handler;
  147. };
  148. /** HTTP module state */
  149. //! libevent event loop
  150. static struct event_base* eventBase = 0;
  151. //! HTTP server
  152. struct evhttp* eventHTTP = 0;
  153. //! List of subnets to allow RPC connections from
  154. static std::vector<CSubNet> rpc_allow_subnets;
  155. //! Work queue for handling longer requests off the event loop thread
  156. static WorkQueue<HTTPClosure>* workQueue = 0;
  157. //! Handlers for (sub)paths
  158. std::vector<HTTPPathHandler> pathHandlers;
  159. //! Bound listening sockets
  160. std::vector<evhttp_bound_socket *> boundSockets;
  161. /** Check if a network address is allowed to access the HTTP server */
  162. static bool ClientAllowed(const CNetAddr& netaddr)
  163. {
  164. if (!netaddr.IsValid())
  165. return false;
  166. for(const CSubNet& subnet : rpc_allow_subnets)
  167. if (subnet.Match(netaddr))
  168. return true;
  169. return false;
  170. }
  171. /** Initialize ACL list for HTTP server */
  172. static bool InitHTTPAllowList()
  173. {
  174. rpc_allow_subnets.clear();
  175. CNetAddr localv4;
  176. CNetAddr localv6;
  177. LookupHost("127.0.0.1", localv4, false);
  178. LookupHost("::1", localv6, false);
  179. rpc_allow_subnets.push_back(CSubNet(localv4, 8)); // always allow IPv4 local subnet
  180. rpc_allow_subnets.push_back(CSubNet(localv6)); // always allow IPv6 localhost
  181. if (gArgs.IsArgSet("-rpcallowip")) {
  182. const std::vector<std::string>& vAllow = gArgs.GetArgs("-rpcallowip");
  183. for (std::string strAllow : vAllow) {
  184. CSubNet subnet;
  185. LookupSubNet(strAllow.c_str(), subnet);
  186. if (!subnet.IsValid()) {
  187. uiInterface.ThreadSafeMessageBox(
  188. strprintf("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),
  189. "", CClientUIInterface::MSG_ERROR);
  190. return false;
  191. }
  192. rpc_allow_subnets.push_back(subnet);
  193. }
  194. }
  195. std::string strAllowed;
  196. for (const CSubNet& subnet : rpc_allow_subnets)
  197. strAllowed += subnet.ToString() + " ";
  198. LogPrint(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed);
  199. return true;
  200. }
  201. /** HTTP request method as string - use for logging only */
  202. static std::string RequestMethodString(HTTPRequest::RequestMethod m)
  203. {
  204. switch (m) {
  205. case HTTPRequest::GET:
  206. return "GET";
  207. break;
  208. case HTTPRequest::POST:
  209. return "POST";
  210. break;
  211. case HTTPRequest::HEAD:
  212. return "HEAD";
  213. break;
  214. case HTTPRequest::PUT:
  215. return "PUT";
  216. break;
  217. default:
  218. return "unknown";
  219. }
  220. }
  221. /** HTTP request callback */
  222. static void http_request_cb(struct evhttp_request* req, void* arg)
  223. {
  224. std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
  225. LogPrint(BCLog::HTTP, "Received a %s request for %s from %s\n",
  226. RequestMethodString(hreq->GetRequestMethod()), hreq->GetURI(), hreq->GetPeer().ToString());
  227. // Early address-based allow check
  228. if (!ClientAllowed(hreq->GetPeer())) {
  229. hreq->WriteReply(HTTP_FORBIDDEN);
  230. return;
  231. }
  232. // Early reject unknown HTTP methods
  233. if (hreq->GetRequestMethod() == HTTPRequest::UNKNOWN) {
  234. hreq->WriteReply(HTTP_BADMETHOD);
  235. return;
  236. }
  237. // Find registered handler for prefix
  238. std::string strURI = hreq->GetURI();
  239. std::string path;
  240. std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
  241. std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
  242. for (; i != iend; ++i) {
  243. bool match = false;
  244. if (i->exactMatch)
  245. match = (strURI == i->prefix);
  246. else
  247. match = (strURI.substr(0, i->prefix.size()) == i->prefix);
  248. if (match) {
  249. path = strURI.substr(i->prefix.size());
  250. break;
  251. }
  252. }
  253. // Dispatch to worker thread
  254. if (i != iend) {
  255. std::unique_ptr<HTTPWorkItem> item(new HTTPWorkItem(std::move(hreq), path, i->handler));
  256. assert(workQueue);
  257. if (workQueue->Enqueue(item.get()))
  258. item.release(); /* if true, queue took ownership */
  259. else {
  260. LogPrintf("WARNING: request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting\n");
  261. item->req->WriteReply(HTTP_INTERNAL, "Work queue depth exceeded");
  262. }
  263. } else {
  264. hreq->WriteReply(HTTP_NOTFOUND);
  265. }
  266. }
  267. /** Callback to reject HTTP requests after shutdown. */
  268. static void http_reject_request_cb(struct evhttp_request* req, void*)
  269. {
  270. LogPrint(BCLog::HTTP, "Rejecting request while shutting down\n");
  271. evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
  272. }
  273. /** Event dispatcher thread */
  274. static bool ThreadHTTP(struct event_base* base, struct evhttp* http)
  275. {
  276. RenameThread("bitcoin-http");
  277. LogPrint(BCLog::HTTP, "Entering http event loop\n");
  278. event_base_dispatch(base);
  279. // Event loop will be interrupted by InterruptHTTPServer()
  280. LogPrint(BCLog::HTTP, "Exited http event loop\n");
  281. return event_base_got_break(base) == 0;
  282. }
  283. /** Bind HTTP server to specified addresses */
  284. static bool HTTPBindAddresses(struct evhttp* http)
  285. {
  286. int defaultPort = GetArg("-rpcport", BaseParams().RPCPort());
  287. std::vector<std::pair<std::string, uint16_t> > endpoints;
  288. // Determine what addresses to bind to
  289. if (!IsArgSet("-rpcallowip")) { // Default to loopback if not allowing external IPs
  290. endpoints.push_back(std::make_pair("::1", defaultPort));
  291. endpoints.push_back(std::make_pair("127.0.0.1", defaultPort));
  292. if (IsArgSet("-rpcbind")) {
  293. LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n");
  294. }
  295. } else if (gArgs.IsArgSet("-rpcbind")) { // Specific bind address
  296. const std::vector<std::string>& vbind = gArgs.GetArgs("-rpcbind");
  297. for (std::vector<std::string>::const_iterator i = vbind.begin(); i != vbind.end(); ++i) {
  298. int port = defaultPort;
  299. std::string host;
  300. SplitHostPort(*i, port, host);
  301. endpoints.push_back(std::make_pair(host, port));
  302. }
  303. } else { // No specific bind address specified, bind to any
  304. endpoints.push_back(std::make_pair("::", defaultPort));
  305. endpoints.push_back(std::make_pair("0.0.0.0", defaultPort));
  306. }
  307. // Bind addresses
  308. for (std::vector<std::pair<std::string, uint16_t> >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) {
  309. LogPrint(BCLog::HTTP, "Binding RPC on address %s port %i\n", i->first, i->second);
  310. evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? NULL : i->first.c_str(), i->second);
  311. if (bind_handle) {
  312. boundSockets.push_back(bind_handle);
  313. } else {
  314. LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second);
  315. }
  316. }
  317. return !boundSockets.empty();
  318. }
  319. /** Simple wrapper to set thread name and run work queue */
  320. static void HTTPWorkQueueRun(WorkQueue<HTTPClosure>* queue)
  321. {
  322. RenameThread("bitcoin-httpworker");
  323. queue->Run();
  324. }
  325. /** libevent event log callback */
  326. static void libevent_log_cb(int severity, const char *msg)
  327. {
  328. #ifndef EVENT_LOG_WARN
  329. // EVENT_LOG_WARN was added in 2.0.19; but before then _EVENT_LOG_WARN existed.
  330. # define EVENT_LOG_WARN _EVENT_LOG_WARN
  331. #endif
  332. if (severity >= EVENT_LOG_WARN) // Log warn messages and higher without debug category
  333. LogPrintf("libevent: %s\n", msg);
  334. else
  335. LogPrint(BCLog::LIBEVENT, "libevent: %s\n", msg);
  336. }
  337. bool InitHTTPServer()
  338. {
  339. struct evhttp* http = 0;
  340. struct event_base* base = 0;
  341. if (!InitHTTPAllowList())
  342. return false;
  343. if (GetBoolArg("-rpcssl", false)) {
  344. uiInterface.ThreadSafeMessageBox(
  345. "SSL mode for RPC (-rpcssl) is no longer supported.",
  346. "", CClientUIInterface::MSG_ERROR);
  347. return false;
  348. }
  349. // Redirect libevent's logging to our own log
  350. event_set_log_callback(&libevent_log_cb);
  351. // Update libevent's log handling. Returns false if our version of
  352. // libevent doesn't support debug logging, in which case we should
  353. // clear the BCLog::LIBEVENT flag.
  354. if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) {
  355. logCategories &= ~BCLog::LIBEVENT;
  356. }
  357. #ifdef WIN32
  358. evthread_use_windows_threads();
  359. #else
  360. evthread_use_pthreads();
  361. #endif
  362. base = event_base_new(); // XXX RAII
  363. if (!base) {
  364. LogPrintf("Couldn't create an event_base: exiting\n");
  365. return false;
  366. }
  367. /* Create a new evhttp object to handle requests. */
  368. http = evhttp_new(base); // XXX RAII
  369. if (!http) {
  370. LogPrintf("couldn't create evhttp. Exiting.\n");
  371. event_base_free(base);
  372. return false;
  373. }
  374. evhttp_set_timeout(http, GetArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT));
  375. evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE);
  376. evhttp_set_max_body_size(http, MAX_SIZE);
  377. evhttp_set_gencb(http, http_request_cb, NULL);
  378. if (!HTTPBindAddresses(http)) {
  379. LogPrintf("Unable to bind any endpoint for RPC server\n");
  380. evhttp_free(http);
  381. event_base_free(base);
  382. return false;
  383. }
  384. LogPrint(BCLog::HTTP, "Initialized HTTP server\n");
  385. int workQueueDepth = std::max((long)GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
  386. LogPrintf("HTTP: creating work queue of depth %d\n", workQueueDepth);
  387. workQueue = new WorkQueue<HTTPClosure>(workQueueDepth);
  388. eventBase = base;
  389. eventHTTP = http;
  390. return true;
  391. }
  392. bool UpdateHTTPServerLogging(bool enable) {
  393. #if LIBEVENT_VERSION_NUMBER >= 0x02010100
  394. if (enable) {
  395. event_enable_debug_logging(EVENT_DBG_ALL);
  396. } else {
  397. event_enable_debug_logging(EVENT_DBG_NONE);
  398. }
  399. return true;
  400. #else
  401. // Can't update libevent logging if version < 02010100
  402. return false;
  403. #endif
  404. }
  405. std::thread threadHTTP;
  406. std::future<bool> threadResult;
  407. bool StartHTTPServer()
  408. {
  409. LogPrint(BCLog::HTTP, "Starting HTTP server\n");
  410. int rpcThreads = std::max((long)GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
  411. LogPrintf("HTTP: starting %d worker threads\n", rpcThreads);
  412. std::packaged_task<bool(event_base*, evhttp*)> task(ThreadHTTP);
  413. threadResult = task.get_future();
  414. threadHTTP = std::thread(std::move(task), eventBase, eventHTTP);
  415. for (int i = 0; i < rpcThreads; i++) {
  416. std::thread rpc_worker(HTTPWorkQueueRun, workQueue);
  417. rpc_worker.detach();
  418. }
  419. return true;
  420. }
  421. void InterruptHTTPServer()
  422. {
  423. LogPrint(BCLog::HTTP, "Interrupting HTTP server\n");
  424. if (eventHTTP) {
  425. // Unlisten sockets
  426. for (evhttp_bound_socket *socket : boundSockets) {
  427. evhttp_del_accept_socket(eventHTTP, socket);
  428. }
  429. // Reject requests on current connections
  430. evhttp_set_gencb(eventHTTP, http_reject_request_cb, NULL);
  431. }
  432. if (workQueue)
  433. workQueue->Interrupt();
  434. }
  435. void StopHTTPServer()
  436. {
  437. LogPrint(BCLog::HTTP, "Stopping HTTP server\n");
  438. if (workQueue) {
  439. LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
  440. workQueue->WaitExit();
  441. delete workQueue;
  442. workQueue = nullptr;
  443. }
  444. if (eventBase) {
  445. LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
  446. // Give event loop a few seconds to exit (to send back last RPC responses), then break it
  447. // Before this was solved with event_base_loopexit, but that didn't work as expected in
  448. // at least libevent 2.0.21 and always introduced a delay. In libevent
  449. // master that appears to be solved, so in the future that solution
  450. // could be used again (if desirable).
  451. // (see discussion in https://github.com/bitcoin/bitcoin/pull/6990)
  452. if (threadResult.valid() && threadResult.wait_for(std::chrono::milliseconds(2000)) == std::future_status::timeout) {
  453. LogPrintf("HTTP event loop did not exit within allotted time, sending loopbreak\n");
  454. event_base_loopbreak(eventBase);
  455. }
  456. threadHTTP.join();
  457. }
  458. if (eventHTTP) {
  459. evhttp_free(eventHTTP);
  460. eventHTTP = 0;
  461. }
  462. if (eventBase) {
  463. event_base_free(eventBase);
  464. eventBase = 0;
  465. }
  466. LogPrint(BCLog::HTTP, "Stopped HTTP server\n");
  467. }
  468. struct event_base* EventBase()
  469. {
  470. return eventBase;
  471. }
  472. static void httpevent_callback_fn(evutil_socket_t, short, void* data)
  473. {
  474. // Static handler: simply call inner handler
  475. HTTPEvent *self = ((HTTPEvent*)data);
  476. self->handler();
  477. if (self->deleteWhenTriggered)
  478. delete self;
  479. }
  480. HTTPEvent::HTTPEvent(struct event_base* base, bool _deleteWhenTriggered, const std::function<void(void)>& _handler):
  481. deleteWhenTriggered(_deleteWhenTriggered), handler(_handler)
  482. {
  483. ev = event_new(base, -1, 0, httpevent_callback_fn, this);
  484. assert(ev);
  485. }
  486. HTTPEvent::~HTTPEvent()
  487. {
  488. event_free(ev);
  489. }
  490. void HTTPEvent::trigger(struct timeval* tv)
  491. {
  492. if (tv == NULL)
  493. event_active(ev, 0, 0); // immediately trigger event in main thread
  494. else
  495. evtimer_add(ev, tv); // trigger after timeval passed
  496. }
  497. HTTPRequest::HTTPRequest(struct evhttp_request* _req) : req(_req),
  498. replySent(false)
  499. {
  500. }
  501. HTTPRequest::~HTTPRequest()
  502. {
  503. if (!replySent) {
  504. // Keep track of whether reply was sent to avoid request leaks
  505. LogPrintf("%s: Unhandled request\n", __func__);
  506. WriteReply(HTTP_INTERNAL, "Unhandled request");
  507. }
  508. // evhttpd cleans up the request, as long as a reply was sent.
  509. }
  510. std::pair<bool, std::string> HTTPRequest::GetHeader(const std::string& hdr)
  511. {
  512. const struct evkeyvalq* headers = evhttp_request_get_input_headers(req);
  513. assert(headers);
  514. const char* val = evhttp_find_header(headers, hdr.c_str());
  515. if (val)
  516. return std::make_pair(true, val);
  517. else
  518. return std::make_pair(false, "");
  519. }
  520. std::string HTTPRequest::ReadBody()
  521. {
  522. struct evbuffer* buf = evhttp_request_get_input_buffer(req);
  523. if (!buf)
  524. return "";
  525. size_t size = evbuffer_get_length(buf);
  526. /** Trivial implementation: if this is ever a performance bottleneck,
  527. * internal copying can be avoided in multi-segment buffers by using
  528. * evbuffer_peek and an awkward loop. Though in that case, it'd be even
  529. * better to not copy into an intermediate string but use a stream
  530. * abstraction to consume the evbuffer on the fly in the parsing algorithm.
  531. */
  532. const char* data = (const char*)evbuffer_pullup(buf, size);
  533. if (!data) // returns NULL in case of empty buffer
  534. return "";
  535. std::string rv(data, size);
  536. evbuffer_drain(buf, size);
  537. return rv;
  538. }
  539. void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
  540. {
  541. struct evkeyvalq* headers = evhttp_request_get_output_headers(req);
  542. assert(headers);
  543. evhttp_add_header(headers, hdr.c_str(), value.c_str());
  544. }
  545. /** Closure sent to main thread to request a reply to be sent to
  546. * a HTTP request.
  547. * Replies must be sent in the main loop in the main http thread,
  548. * this cannot be done from worker threads.
  549. */
  550. void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
  551. {
  552. assert(!replySent && req);
  553. // Send event to main http thread to send reply message
  554. struct evbuffer* evb = evhttp_request_get_output_buffer(req);
  555. assert(evb);
  556. evbuffer_add(evb, strReply.data(), strReply.size());
  557. HTTPEvent* ev = new HTTPEvent(eventBase, true,
  558. std::bind(evhttp_send_reply, req, nStatus, (const char*)NULL, (struct evbuffer *)NULL));
  559. ev->trigger(0);
  560. replySent = true;
  561. req = 0; // transferred back to main thread
  562. }
  563. CService HTTPRequest::GetPeer()
  564. {
  565. evhttp_connection* con = evhttp_request_get_connection(req);
  566. CService peer;
  567. if (con) {
  568. // evhttp retains ownership over returned address string
  569. const char* address = "";
  570. uint16_t port = 0;
  571. evhttp_connection_get_peer(con, (char**)&address, &port);
  572. peer = LookupNumeric(address, port);
  573. }
  574. return peer;
  575. }
  576. std::string HTTPRequest::GetURI()
  577. {
  578. return evhttp_request_get_uri(req);
  579. }
  580. HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod()
  581. {
  582. switch (evhttp_request_get_command(req)) {
  583. case EVHTTP_REQ_GET:
  584. return GET;
  585. break;
  586. case EVHTTP_REQ_POST:
  587. return POST;
  588. break;
  589. case EVHTTP_REQ_HEAD:
  590. return HEAD;
  591. break;
  592. case EVHTTP_REQ_PUT:
  593. return PUT;
  594. break;
  595. default:
  596. return UNKNOWN;
  597. break;
  598. }
  599. }
  600. void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
  601. {
  602. LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
  603. pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler));
  604. }
  605. void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
  606. {
  607. std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
  608. std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
  609. for (; i != iend; ++i)
  610. if (i->prefix == prefix && i->exactMatch == exactMatch)
  611. break;
  612. if (i != iend)
  613. {
  614. LogPrint(BCLog::HTTP, "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
  615. pathHandlers.erase(i);
  616. }
  617. }