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
13 #include <boost/thread.hpp>
14 #include <boost/scoped_ptr.hpp>
15 #include <boost/function.hpp>
17 static const int DEFAULT_HTTP_THREADS=4;
18 static const int DEFAULT_HTTP_WORKQUEUE=16;
19 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
21 struct evhttp_request;
26 /** Initialize HTTP server.
27 * Call this before RegisterHTTPHandler or EventBase().
29 bool InitHTTPServer();
30 /** Start HTTP server.
31 * This is separate from InitHTTPServer to give users race-condition-free time
32 * to register their handlers between InitHTTPServer and StartHTTPServer.
34 bool StartHTTPServer();
35 /** Interrupt HTTP server threads */
36 void InterruptHTTPServer();
37 /** Stop HTTP server */
38 void StopHTTPServer();
40 /** Handler for requests to a certain HTTP path */
41 typedef boost::function<void(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
42 /** Register handler for prefix.
43 * If multiple handlers match a prefix, the first-registered one will
46 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
47 /** Unregister handler for prefix */
48 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
50 /** Return evhttp event base. This can be used by submodules to
51 * queue timers or custom events.
53 struct event_base* EventBase();
55 /** In-flight HTTP request.
56 * Thin C++ wrapper around evhttp_request.
61 struct evhttp_request* req;
68 HTTPRequest(struct evhttp_request* req);
69 virtual ~HTTPRequest();
79 /** Get requested URI.
83 /** Get CService (address:ip) for the origin of the http request.
85 virtual CService GetPeer();
87 /** Get request method.
89 virtual RequestMethod GetRequestMethod();
92 * Get the request header specified by hdr, or an empty string.
93 * Return an pair (isPresent,string).
95 virtual std::pair<bool, std::string> GetHeader(const std::string& hdr);
100 * @note As this consumes the underlying buffer, call this only once.
101 * Repeated calls will return an empty string.
103 std::string ReadBody();
106 * Write output header.
108 * @note call this before calling WriteErrorReply or Reply.
110 virtual void WriteHeader(const std::string& hdr, const std::string& value);
114 * nStatus is the HTTP status code to send.
115 * strReply is the body of the reply. Keep it empty to send a standard message.
117 * @note Can be called only once. As this will give the request back to the
118 * main thread, do not call any other HTTPRequest methods after calling this.
120 virtual void WriteReply(int nStatus, const std::string& strReply = "");
123 /** Event handler closure.
128 virtual void operator()() = 0;
129 virtual ~HTTPClosure() {}
132 /** Event class. This can be used either as an cross-thread trigger or as a timer.
137 /** Create a new event.
138 * deleteWhenTriggered deletes this event object after the event is triggered (and the handler called)
139 * handler is the handler to call when the event is triggered.
141 HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const boost::function<void(void)>& handler);
144 /** Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger it after
145 * the given time has elapsed.
147 void trigger(struct timeval* tv);
149 bool deleteWhenTriggered;
150 boost::function<void(void)> handler;
155 #endif // BITCOIN_HTTPSERVER_H