1 // Copyright (c) 2015 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.
5 #ifndef BITCOIN_HTTPSERVER_H
6 #define BITCOIN_HTTPSERVER_H
10 #include <boost/thread.hpp>
11 #include <boost/scoped_ptr.hpp>
12 #include <boost/function.hpp>
14 static const int DEFAULT_HTTP_THREADS=4;
15 static const int DEFAULT_HTTP_WORKQUEUE=16;
16 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
18 struct evhttp_request;
23 /** Initialize HTTP server.
24 * Call this before RegisterHTTPHandler or EventBase().
26 bool InitHTTPServer();
27 /** Start HTTP server.
28 * This is separate from InitHTTPServer to give users race-condition-free time
29 * to register their handlers between InitHTTPServer and StartHTTPServer.
31 bool StartHTTPServer();
32 /** Interrupt HTTP server threads */
33 void InterruptHTTPServer();
34 /** Stop HTTP server */
35 void StopHTTPServer();
37 /** Handler for requests to a certain HTTP path */
38 typedef boost::function<void(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
39 /** Register handler for prefix.
40 * If multiple handlers match a prefix, the first-registered one will
43 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
44 /** Unregister handler for prefix */
45 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
47 /** Return evhttp event base. This can be used by submodules to
48 * queue timers or custom events.
50 struct event_base* EventBase();
52 /** In-flight HTTP request.
53 * Thin C++ wrapper around evhttp_request.
58 struct evhttp_request* req;
65 HTTPRequest(struct evhttp_request* req);
66 virtual ~HTTPRequest();
76 /** Get requested URI.
80 /** Get CService (address:ip) for the origin of the http request.
82 virtual CService GetPeer();
84 /** Get request method.
86 virtual RequestMethod GetRequestMethod();
89 * Get the request header specified by hdr, or an empty string.
90 * Return an pair (isPresent,string).
92 virtual std::pair<bool, std::string> GetHeader(const std::string& hdr);
97 * @note As this consumes the underlying buffer, call this only once.
98 * Repeated calls will return an empty string.
100 std::string ReadBody();
103 * Write output header.
105 * @note call this before calling WriteErrorReply or Reply.
107 virtual void WriteHeader(const std::string& hdr, const std::string& value);
111 * nStatus is the HTTP status code to send.
112 * strReply is the body of the reply. Keep it empty to send a standard message.
114 * @note Can be called only once. As this will give the request back to the
115 * main thread, do not call any other HTTPRequest methods after calling this.
117 virtual void WriteReply(int nStatus, const std::string& strReply = "");
120 /** Event handler closure.
125 virtual void operator()() = 0;
126 virtual ~HTTPClosure() {}
129 /** Event class. This can be used either as an cross-thread trigger or as a timer.
134 /** Create a new event.
135 * deleteWhenTriggered deletes this event object after the event is triggered (and the handler called)
136 * handler is the handler to call when the event is triggered.
138 HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const boost::function<void(void)>& handler);
141 /** Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger it after
142 * the given time has elapsed.
144 void trigger(struct timeval* tv);
146 bool deleteWhenTriggered;
147 boost::function<void(void)> handler;
152 #endif // BITCOIN_HTTPSERVER_H