Fix travis failing to fetch keys from the sks keyserver pool
[bitcoinplatinum.git] / src / httpserver.h
blobb55b253beac1b5469b186d8e1be8497a74ddfdcc
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
8 #include <string>
9 #include <stdint.h>
10 #include <functional>
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;
17 struct event_base;
18 class CService;
19 class HTTPRequest;
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
39 * be invoked.
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.
53 class HTTPRequest
55 private:
56 struct evhttp_request* req;
57 bool replySent;
59 public:
60 HTTPRequest(struct evhttp_request* req);
61 ~HTTPRequest();
63 enum RequestMethod {
64 UNKNOWN,
65 GET,
66 POST,
67 HEAD,
68 PUT
71 /** Get requested URI.
73 std::string GetURI();
75 /** Get CService (address:ip) for the origin of the http request.
77 CService GetPeer();
79 /** Get request method.
81 RequestMethod GetRequestMethod();
83 /**
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);
89 /**
90 * Read request body.
92 * @note As this consumes the underlying buffer, call this only once.
93 * Repeated calls will return an empty string.
95 std::string ReadBody();
97 /**
98 * Write output header.
100 * @note call this before calling WriteErrorReply or Reply.
102 void WriteHeader(const std::string& hdr, const std::string& value);
105 * Write HTTP reply.
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.
117 class HTTPClosure
119 public:
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.
126 class HTTPEvent
128 public:
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);
134 ~HTTPEvent();
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;
143 private:
144 struct event* ev;
147 #endif // BITCOIN_HTTPSERVER_H