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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. for (const std::string& strAllow : gArgs.GetArgs("-rpcallowip")) {
  183. CSubNet subnet;
  184. LookupSubNet(strAllow.c_str(), subnet);
  185. if (!subnet.IsValid()) {
  186. uiInterface.ThreadSafeMessageBox(
  187. 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),
  188. "", CClientUIInterface::MSG_ERROR);
  189. return false;
  190. }
  191. rpc_allow_subnets.push_back(subnet);
  192. }
  193. }
  194. std::string strAllowed;
  195. for (const CSubNet& subnet : rpc_allow_subnets)
  196. strAllowed += subnet.ToString() + " ";
  197. LogPrint(BCLog::HTTP, "Allowing HTTP connections from: %s\n", strAllowed);
  198. return true;
  199. }
  200. /** HTTP request method as string - use for logging only */
  201. static std::string RequestMethodString(HTTPRequest::RequestMethod m)
  202. {
  203. switch (m) {
  204. case HTTPRequest::GET:
  205. return "GET";
  206. break;
  207. case HTTPRequest::POST:
  208. return "POST";
  209. break;
  210. case HTTPRequest::HEAD:
  211. return "HEAD";
  212. break;
  213. case HTTPRequest::PUT:
  214. return "PUT";
  215. break;
  216. default:
  217. return "unknown";
  218. }
  219. }
  220. /** HTTP request callback */
  221. static void http_request_cb(struct evhttp_request* req, void* arg)
  222. {
  223. std::unique_ptr<HTTPRequest> hreq(new HTTPRequest(req));
  224. LogPrint(BCLog::HTTP, "Received a %s request for %s from %s\n",
  225. RequestMethodString(hreq->GetRequestMethod()), hreq->GetURI(), hreq->GetPeer().ToString());
  226. // Early address-based allow check
  227. if (!ClientAllowed(hreq->GetPeer())) {
  228. hreq->WriteReply(HTTP_FORBIDDEN);
  229. return;
  230. }
  231. // Early reject unknown HTTP methods
  232. if (hreq->GetRequestMethod() == HTTPRequest::UNKNOWN) {
  233. hreq->WriteReply(HTTP_BADMETHOD);
  234. return;
  235. }
  236. // Find registered handler for prefix
  237. std::string strURI = hreq->GetURI();
  238. std::string path;
  239. std::vector<HTTPPathHandler>::const_iterator i = pathHandlers.begin();
  240. std::vector<HTTPPathHandler>::const_iterator iend = pathHandlers.end();
  241. for (; i != iend; ++i) {
  242. bool match = false;
  243. if (i->exactMatch)
  244. match = (strURI == i->prefix);
  245. else
  246. match = (strURI.substr(0, i->prefix.size()) == i->prefix);
  247. if (match) {
  248. path = strURI.substr(i->prefix.size());
  249. break;
  250. }
  251. }
  252. // Dispatch to worker thread
  253. if (i != iend) {
  254. std::unique_ptr<HTTPWorkItem> item(new HTTPWorkItem(std::move(hreq), path, i->handler));
  255. assert(workQueue);
  256. if (workQueue->Enqueue(item.get()))
  257. item.release(); /* if true, queue took ownership */
  258. else {
  259. LogPrintf("WARNING: request rejected because http work queue depth exceeded, it can be increased with the -rpcworkqueue= setting\n");
  260. item->req->WriteReply(HTTP_INTERNAL, "Work queue depth exceeded");
  261. }
  262. } else {
  263. hreq->WriteReply(HTTP_NOTFOUND);
  264. }
  265. }
  266. /** Callback to reject HTTP requests after shutdown. */
  267. static void http_reject_request_cb(struct evhttp_request* req, void*)
  268. {
  269. LogPrint(BCLog::HTTP, "Rejecting request while shutting down\n");
  270. evhttp_send_error(req, HTTP_SERVUNAVAIL, NULL);
  271. }
  272. /** Event dispatcher thread */
  273. static bool ThreadHTTP(struct event_base* base, struct evhttp* http)
  274. {
  275. RenameThread("bitcoin-http");
  276. LogPrint(BCLog::HTTP, "Entering http event loop\n");
  277. event_base_dispatch(base);
  278. // Event loop will be interrupted by InterruptHTTPServer()
  279. LogPrint(BCLog::HTTP, "Exited http event loop\n");
  280. return event_base_got_break(base) == 0;
  281. }
  282. /** Bind HTTP server to specified addresses */
  283. static bool HTTPBindAddresses(struct evhttp* http)
  284. {
  285. int defaultPort = GetArg("-rpcport", BaseParams().RPCPort());
  286. std::vector<std::pair<std::string, uint16_t> > endpoints;
  287. // Determine what addresses to bind to
  288. if (!IsArgSet("-rpcallowip")) { // Default to loopback if not allowing external IPs
  289. endpoints.push_back(std::make_pair("::1", defaultPort));
  290. endpoints.push_back(std::make_pair("127.0.0.1", defaultPort));
  291. if (IsArgSet("-rpcbind")) {
  292. LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n");
  293. }
  294. } else if (gArgs.IsArgSet("-rpcbind")) { // Specific bind address
  295. for (const std::string& strRPCBind : gArgs.GetArgs("-rpcbind")) {
  296. int port = defaultPort;
  297. std::string host;
  298. SplitHostPort(strRPCBind, port, host);
  299. endpoints.push_back(std::make_pair(host, port));
  300. }
  301. } else { // No specific bind address specified, bind to any
  302. endpoints.push_back(std::make_pair("::", defaultPort));
  303. endpoints.push_back(std::make_pair("0.0.0.0", defaultPort));
  304. }
  305. // Bind addresses
  306. for (std::vector<std::pair<std::string, uint16_t> >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) {
  307. LogPrint(BCLog::HTTP, "Binding RPC on address %s port %i\n", i->first, i->second);
  308. evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? NULL : i->first.c_str(), i->second);
  309. if (bind_handle) {
  310. boundSockets.push_back(bind_handle);
  311. } else {
  312. LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second);
  313. }
  314. }
  315. return !boundSockets.empty();
  316. }
  317. /** Simple wrapper to set thread name and run work queue */
  318. static void HTTPWorkQueueRun(WorkQueue<HTTPClosure>* queue)
  319. {
  320. RenameThread("bitcoin-httpworker");
  321. queue->Run();
  322. }
  323. /** libevent event log callback */
  324. static void libevent_log_cb(int severity, const char *msg)
  325. {
  326. #ifndef EVENT_LOG_WARN
  327. // EVENT_LOG_WARN was added in 2.0.19; but before then _EVENT_LOG_WARN existed.
  328. # define EVENT_LOG_WARN _EVENT_LOG_WARN
  329. #endif
  330. if (severity >= EVENT_LOG_WARN) // Log warn messages and higher without debug category
  331. LogPrintf("libevent: %s\n", msg);
  332. else
  333. LogPrint(BCLog::LIBEVENT, "libevent: %s\n", msg);
  334. }
  335. bool InitHTTPServer()
  336. {
  337. struct evhttp* http = 0;
  338. struct event_base* base = 0;
  339. if (!InitHTTPAllowList())
  340. return false;
  341. if (GetBoolArg("-rpcssl", false)) {
  342. uiInterface.ThreadSafeMessageBox(
  343. "SSL mode for RPC (-rpcssl) is no longer supported.",
  344. "", CClientUIInterface::MSG_ERROR);
  345. return false;
  346. }
  347. // Redirect libevent's logging to our own log
  348. event_set_log_callback(&libevent_log_cb);
  349. // Update libevent's log handling. Returns false if our version of
  350. // libevent doesn't support debug logging, in which case we should
  351. // clear the BCLog::LIBEVENT flag.
  352. if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) {
  353. logCategories &= ~BCLog::LIBEVENT;
  354. }
  355. #ifdef WIN32
  356. evthread_use_windows_threads();
  357. #else
  358. evthread_use_pthreads();
  359. #endif
  360. base = event_base_new(); // XXX RAII
  361. if (!base) {
  362. LogPrintf("Couldn't create an event_base: exiting\n");
  363. return false;
  364. }
  365. /* Create a new evhttp object to handle requests. */
  366. http = evhttp_new(base); // XXX RAII
  367. if (!http) {
  368. LogPrintf("couldn't create evhttp. Exiting.\n");
  369. event_base_free(base);
  370. return false;
  371. }
  372. evhttp_set_timeout(http, GetArg("-rpcservertimeout", DEFAULT_HTTP_SERVER_TIMEOUT));
  373. evhttp_set_max_headers_size(http, MAX_HEADERS_SIZE);
  374. evhttp_set_max_body_size(http, MAX_SIZE);
  375. evhttp_set_gencb(http, http_request_cb, NULL);
  376. if (!HTTPBindAddresses(http)) {
  377. LogPrintf("Unable to bind any endpoint for RPC server\n");
  378. evhttp_free(http);
  379. event_base_free(base);
  380. return false;
  381. }
  382. LogPrint(BCLog::HTTP, "Initialized HTTP server\n");
  383. int workQueueDepth = std::max((long)GetArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
  384. LogPrintf("HTTP: creating work queue of depth %d\n", workQueueDepth);
  385. workQueue = new WorkQueue<HTTPClosure>(workQueueDepth);
  386. eventBase = base;
  387. eventHTTP = http;
  388. return true;
  389. }
  390. bool UpdateHTTPServerLogging(bool enable) {
  391. #if LIBEVENT_VERSION_NUMBER >= 0x02010100
  392. if (enable) {
  393. event_enable_debug_logging(EVENT_DBG_ALL);
  394. } else {
  395. event_enable_debug_logging(EVENT_DBG_NONE);
  396. }
  397. return true;
  398. #else
  399. // Can't update libevent logging if version < 02010100
  400. return false;
  401. #endif
  402. }
  403. std::thread threadHTTP;
  404. std::future<bool> threadResult;
  405. bool StartHTTPServer()
  406. {
  407. LogPrint(BCLog::HTTP, "Starting HTTP server\n");
  408. int rpcThreads = std::max((long)GetArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
  409. LogPrintf("HTTP: starting %d worker threads\n", rpcThreads);
  410. std::packaged_task<bool(event_base*, evhttp*)> task(ThreadHTTP);
  411. threadResult = task.get_future();
  412. threadHTTP = std::thread(std::move(task), eventBase, eventHTTP);
  413. for (int i = 0; i < rpcThreads; i++) {
  414. std::thread rpc_worker(HTTPWorkQueueRun, workQueue);
  415. rpc_worker.detach();
  416. }
  417. return true;
  418. }
  419. void InterruptHTTPServer()
  420. {
  421. LogPrint(BCLog::HTTP, "Interrupting HTTP server\n");
  422. if (eventHTTP) {
  423. // Unlisten sockets
  424. for (evhttp_bound_socket *socket : boundSockets) {
  425. evhttp_del_accept_socket(eventHTTP, socket);
  426. }
  427. // Reject requests on current connections
  428. evhttp_set_gencb(eventHTTP, http_reject_request_cb, NULL);
  429. }
  430. if (workQueue)
  431. workQueue->Interrupt();
  432. }
  433. void StopHTTPServer()
  434. {
  435. LogPrint(BCLog::HTTP, "Stopping HTTP server\n");
  436. if (workQueue) {
  437. LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
  438. workQueue->WaitExit();
  439. delete workQueue;
  440. workQueue = nullptr;
  441. }
  442. if (eventBase) {
  443. LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
  444. // Give event loop a few seconds to exit (to send back last RPC responses), then break it
  445. // Before this was solved with event_base_loopexit, but that didn't work as expected in
  446. // at least libevent 2.0.21 and always introduced a delay. In libevent
  447. // master that appears to be solved, so in the future that solution
  448. // could be used again (if desirable).
  449. // (see discussion in https://github.com/bitcoin/bitcoin/pull/6990)
  450. if (threadResult.valid() && threadResult.wait_for(std::chrono::milliseconds(2000)) == std::future_status::timeout) {
  451. LogPrintf("HTTP event loop did not exit within allotted time, sending loopbreak\n");
  452. event_base_loopbreak(eventBase);
  453. }
  454. threadHTTP.join();
  455. }
  456. if (eventHTTP) {
  457. evhttp_free(eventHTTP);
  458. eventHTTP = 0;
  459. }
  460. if (eventBase) {
  461. event_base_free(eventBase);
  462. eventBase = 0;
  463. }
  464. LogPrint(BCLog::HTTP, "Stopped HTTP server\n");
  465. }
  466. struct event_base* EventBase()
  467. {
  468. return eventBase;
  469. }
  470. static void httpevent_callback_fn(evutil_socket_t, short, void* data)
  471. {
  472. // Static handler: simply call inner handler
  473. HTTPEvent *self = ((HTTPEvent*)data);
  474. self->handler();
  475. if (self->deleteWhenTriggered)
  476. delete self;
  477. }
  478. HTTPEvent::HTTPEvent(struct event_base* base, bool _deleteWhenTriggered, const std::function<void(void)>& _handler):
  479. deleteWhenTriggered(_deleteWhenTriggered), handler(_handler)
  480. {
  481. ev = event_new(base, -1, 0, httpevent_callback_fn, this);
  482. assert(ev);
  483. }
  484. HTTPEvent::~HTTPEvent()
  485. {
  486. event_free(ev);
  487. }
  488. void HTTPEvent::trigger(struct timeval* tv)
  489. {
  490. if (tv == NULL)
  491. event_active(ev, 0, 0); // immediately trigger event in main thread
  492. else
  493. evtimer_add(ev, tv); // trigger after timeval passed
  494. }
  495. HTTPRequest::HTTPRequest(struct evhttp_request* _req) : req(_req),
  496. replySent(false)
  497. {
  498. }
  499. HTTPRequest::~HTTPRequest()
  500. {
  501. if (!replySent) {
  502. // Keep track of whether reply was sent to avoid request leaks
  503. LogPrintf("%s: Unhandled request\n", __func__);
  504. WriteReply(HTTP_INTERNAL, "Unhandled request");
  505. }
  506. // evhttpd cleans up the request, as long as a reply was sent.
  507. }
  508. std::pair<bool, std::string> HTTPRequest::GetHeader(const std::string& hdr)
  509. {
  510. const struct evkeyvalq* headers = evhttp_request_get_input_headers(req);
  511. assert(headers);
  512. const char* val = evhttp_find_header(headers, hdr.c_str());
  513. if (val)
  514. return std::make_pair(true, val);
  515. else
  516. return std::make_pair(false, "");
  517. }
  518. std::string HTTPRequest::ReadBody()
  519. {
  520. struct evbuffer* buf = evhttp_request_get_input_buffer(req);
  521. if (!buf)
  522. return "";
  523. size_t size = evbuffer_get_length(buf);
  524. /** Trivial implementation: if this is ever a performance bottleneck,
  525. * internal copying can be avoided in multi-segment buffers by using
  526. * evbuffer_peek and an awkward loop. Though in that case, it'd be even
  527. * better to not copy into an intermediate string but use a stream
  528. * abstraction to consume the evbuffer on the fly in the parsing algorithm.
  529. */
  530. const char* data = (const char*)evbuffer_pullup(buf, size);
  531. if (!data) // returns NULL in case of empty buffer
  532. return "";
  533. std::string rv(data, size);
  534. evbuffer_drain(buf, size);
  535. return rv;
  536. }
  537. void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
  538. {
  539. struct evkeyvalq* headers = evhttp_request_get_output_headers(req);
  540. assert(headers);
  541. evhttp_add_header(headers, hdr.c_str(), value.c_str());
  542. }
  543. /** Closure sent to main thread to request a reply to be sent to
  544. * a HTTP request.
  545. * Replies must be sent in the main loop in the main http thread,
  546. * this cannot be done from worker threads.
  547. */
  548. void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
  549. {
  550. assert(!replySent && req);
  551. // Send event to main http thread to send reply message
  552. struct evbuffer* evb = evhttp_request_get_output_buffer(req);
  553. assert(evb);
  554. evbuffer_add(evb, strReply.data(), strReply.size());
  555. HTTPEvent* ev = new HTTPEvent(eventBase, true,
  556. std::bind(evhttp_send_reply, req, nStatus, (const char*)NULL, (struct evbuffer *)NULL));
  557. ev->trigger(0);
  558. replySent = true;
  559. req = 0; // transferred back to main thread
  560. }
  561. CService HTTPRequest::GetPeer()
  562. {
  563. evhttp_connection* con = evhttp_request_get_connection(req);
  564. CService peer;
  565. if (con) {
  566. // evhttp retains ownership over returned address string
  567. const char* address = "";
  568. uint16_t port = 0;
  569. evhttp_connection_get_peer(con, (char**)&address, &port);
  570. peer = LookupNumeric(address, port);
  571. }
  572. return peer;
  573. }
  574. std::string HTTPRequest::GetURI()
  575. {
  576. return evhttp_request_get_uri(req);
  577. }
  578. HTTPRequest::RequestMethod HTTPRequest::GetRequestMethod()
  579. {
  580. switch (evhttp_request_get_command(req)) {
  581. case EVHTTP_REQ_GET:
  582. return GET;
  583. break;
  584. case EVHTTP_REQ_POST:
  585. return POST;
  586. break;
  587. case EVHTTP_REQ_HEAD:
  588. return HEAD;
  589. break;
  590. case EVHTTP_REQ_PUT:
  591. return PUT;
  592. break;
  593. default:
  594. return UNKNOWN;
  595. break;
  596. }
  597. }
  598. void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
  599. {
  600. LogPrint(BCLog::HTTP, "Registering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
  601. pathHandlers.push_back(HTTPPathHandler(prefix, exactMatch, handler));
  602. }
  603. void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
  604. {
  605. std::vector<HTTPPathHandler>::iterator i = pathHandlers.begin();
  606. std::vector<HTTPPathHandler>::iterator iend = pathHandlers.end();
  607. for (; i != iend; ++i)
  608. if (i->prefix == prefix && i->exactMatch == exactMatch)
  609. break;
  610. if (i != iend)
  611. {
  612. LogPrint(BCLog::HTTP, "Unregistering HTTP handler for %s (exactmatch %d)\n", prefix, exactMatch);
  613. pathHandlers.erase(i);
  614. }
  615. }