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