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 /** Change logging level for libevent. Removes BCLog::LIBEVENT from logCategories if
36 * libevent doesn't support debug logging.*/
37 bool UpdateHTTPServerLogging(bool enable
);
39 /** Handler for requests to a certain HTTP path */
40 typedef std::function
<bool(HTTPRequest
* req
, const std::string
&)> HTTPRequestHandler
;
41 /** Register handler for prefix.
42 * If multiple handlers match a prefix, the first-registered one will
45 void RegisterHTTPHandler(const std::string
&prefix
, bool exactMatch
, const HTTPRequestHandler
&handler
);
46 /** Unregister handler for prefix */
47 void UnregisterHTTPHandler(const std::string
&prefix
, bool exactMatch
);
49 /** Return evhttp event base. This can be used by submodules to
50 * queue timers or custom events.
52 struct event_base
* EventBase();
54 /** In-flight HTTP request.
55 * Thin C++ wrapper around evhttp_request.
60 struct evhttp_request
* req
;
64 HTTPRequest(struct evhttp_request
* req
);
75 /** Get requested URI.
79 /** Get CService (address:ip) for the origin of the http request.
83 /** Get request method.
85 RequestMethod
GetRequestMethod();
88 * Get the request header specified by hdr, or an empty string.
89 * Return an pair (isPresent,string).
91 std::pair
<bool, std::string
> GetHeader(const std::string
& hdr
);
96 * @note As this consumes the underlying buffer, call this only once.
97 * Repeated calls will return an empty string.
99 std::string
ReadBody();
102 * Write output header.
104 * @note call this before calling WriteErrorReply or Reply.
106 void WriteHeader(const std::string
& hdr
, const std::string
& value
);
110 * nStatus is the HTTP status code to send.
111 * strReply is the body of the reply. Keep it empty to send a standard message.
113 * @note Can be called only once. As this will give the request back to the
114 * main thread, do not call any other HTTPRequest methods after calling this.
116 void WriteReply(int nStatus
, const std::string
& strReply
= "");
119 /** Event handler closure.
124 virtual void operator()() = 0;
125 virtual ~HTTPClosure() {}
128 /** Event class. This can be used either as an cross-thread trigger or as a timer.
133 /** Create a new event.
134 * deleteWhenTriggered deletes this event object after the event is triggered (and the handler called)
135 * handler is the handler to call when the event is triggered.
137 HTTPEvent(struct event_base
* base
, bool deleteWhenTriggered
, const std::function
<void(void)>& handler
);
140 /** Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger it after
141 * the given time has elapsed.
143 void trigger(struct timeval
* tv
);
145 bool deleteWhenTriggered
;
146 std::function
<void(void)> handler
;
151 #endif // BITCOIN_HTTPSERVER_H