Line data Source code
1 : // Copyright (c) 2015-2021 The Bitcoin Core developers 2 : // Copyright (c) 2021 The PIVX Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef PIVX_HTTPSERVER_H 7 : #define PIVX_HTTPSERVER_H 8 : 9 : #include <string> 10 : #include <stdint.h> 11 : #include <functional> 12 : 13 : static const int DEFAULT_HTTP_THREADS=4; 14 : static const int DEFAULT_HTTP_WORKQUEUE=16; 15 : static const int DEFAULT_HTTP_SERVER_TIMEOUT=30; 16 : 17 : struct evhttp_request; 18 : struct event_base; 19 : class CService; 20 : class HTTPRequest; 21 : 22 : /** Initialize HTTP server. 23 : * Call this before RegisterHTTPHandler or EventBase(). 24 : */ 25 : bool InitHTTPServer(); 26 : /** Start HTTP server. 27 : * This is separate from InitHTTPServer to give users race-condition-free time 28 : * to register their handlers between InitHTTPServer and StartHTTPServer. 29 : */ 30 : bool StartHTTPServer(); 31 : /** Interrupt HTTP server threads */ 32 : void InterruptHTTPServer(); 33 : /** Stop HTTP server */ 34 : void StopHTTPServer(); 35 : 36 : /** Change logging level for libevent. Removes BCLog::LIBEVENT from log categories if 37 : * libevent doesn't support debug logging.*/ 38 : bool UpdateHTTPServerLogging(bool enable); 39 : 40 : /** Handler for requests to a certain HTTP path */ 41 : typedef std::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 44 : * be invoked. 45 : */ 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); 49 : 50 : /** Return evhttp event base. This can be used by submodules to 51 : * queue timers or custom events. 52 : */ 53 : struct event_base* EventBase(); 54 : 55 : /** In-flight HTTP request. 56 : * Thin C++ wrapper around evhttp_request. 57 : */ 58 : class HTTPRequest 59 : { 60 : private: 61 : struct evhttp_request* req; 62 : bool replySent; 63 : 64 : public: 65 : explicit HTTPRequest(struct evhttp_request* req); 66 : ~HTTPRequest(); 67 : 68 : enum RequestMethod { 69 : UNKNOWN, 70 : GET, 71 : POST, 72 : HEAD, 73 : PUT 74 : }; 75 : 76 : /** Get requested URI. 77 : */ 78 : std::string GetURI(); 79 : 80 : /** Get CService (address:ip) for the origin of the http request. 81 : */ 82 : CService GetPeer(); 83 : 84 : /** Get request method. 85 : */ 86 : RequestMethod GetRequestMethod(); 87 : 88 : /** 89 : * Get the request header specified by hdr, or an empty string. 90 : * Return an pair (isPresent,std::string). 91 : */ 92 : std::pair<bool, std::string> GetHeader(const std::string& hdr); 93 : 94 : /** 95 : * Read request body. 96 : * 97 : * @note As this consumes the underlying buffer, call this only once. 98 : * Repeated calls will return an empty string. 99 : */ 100 : std::string ReadBody(); 101 : 102 : /** 103 : * Write output header. 104 : * 105 : * @note call this before calling WriteErrorReply or Reply. 106 : */ 107 : void WriteHeader(const std::string& hdr, const std::string& value); 108 : 109 : /** 110 : * Write HTTP reply. 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. 113 : * 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. 116 : */ 117 : void WriteReply(int nStatus, const std::string& strReply = ""); 118 : }; 119 : 120 : /** Event handler closure. 121 : */ 122 298193 : class HTTPClosure 123 : { 124 : public: 125 : virtual void operator()() = 0; 126 : virtual ~HTTPClosure() {} 127 : }; 128 : 129 : /** Event class. This can be used either as an cross-thread trigger or as a timer. 130 : */ 131 : class HTTPEvent 132 : { 133 : public: 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. 137 : */ 138 : HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void(void)>& handler); 139 : ~HTTPEvent(); 140 : 141 : /** Trigger the event. If tv is 0, trigger it immediately. Otherwise trigger it after 142 : * the given time has elapsed. 143 : */ 144 : void trigger(struct timeval* tv); 145 : 146 : bool deleteWhenTriggered; 147 : std::function<void(void)> handler; 148 : private: 149 : struct event* ev; 150 : }; 151 : 152 : std::string urlDecode(const std::string &urlEncoded); 153 : 154 : #endif // PIVX_HTTPSERVER_H