PIVX Core  5.6.99
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
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 
25 bool InitHTTPServer();
30 bool StartHTTPServer();
32 void InterruptHTTPServer();
34 void StopHTTPServer();
35 
38 bool UpdateHTTPServerLogging(bool enable);
39 
41 typedef std::function<void(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
46 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
48 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
49 
53 struct event_base* EventBase();
54 
59 {
60 private:
61  struct evhttp_request* req;
62  bool replySent;
63 
64 public:
65  explicit HTTPRequest(struct evhttp_request* req);
66  ~HTTPRequest();
67 
70  GET,
73  PUT
74  };
75 
78  std::string GetURI();
79 
82  CService GetPeer();
83 
87 
92  std::pair<bool, std::string> GetHeader(const std::string& hdr);
93 
100  std::string ReadBody();
101 
107  void WriteHeader(const std::string& hdr, const std::string& value);
108 
117  void WriteReply(int nStatus, const std::string& strReply = "");
118 };
119 
123 {
124 public:
125  virtual void operator()() = 0;
126  virtual ~HTTPClosure() {}
127 };
128 
132 {
133 public:
138  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void(void)>& handler);
139  ~HTTPEvent();
140 
144  void trigger(struct timeval* tv);
145 
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
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:484
Event handler closure.
Definition: httpserver.h:123
virtual void operator()()=0
virtual ~HTTPClosure()
Definition: httpserver.h:126
Event class.
Definition: httpserver.h:132
struct event * ev
Definition: httpserver.h:149
bool deleteWhenTriggered
Definition: httpserver.h:146
std::function< void(void)> handler
Definition: httpserver.h:147
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void(void)> &handler)
Create a new event.
Definition: httpserver.cpp:515
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:525
In-flight HTTP request.
Definition: httpserver.h:59
bool replySent
Definition: httpserver.h:62
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:589
std::string GetURI()
Get requested URI.
Definition: httpserver.cpp:633
CService GetPeer()
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:619
std::pair< bool, std::string > GetHeader(const std::string &hdr)
Get the request header specified by hdr, or an empty string.
Definition: httpserver.cpp:546
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:577
struct evhttp_request * req
Definition: httpserver.h:61
HTTPRequest(struct evhttp_request *req)
Definition: httpserver.cpp:532
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:557
RequestMethod GetRequestMethod()
Get request method.
Definition: httpserver.cpp:638
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:444
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:665
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:659
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:501
std::string urlDecode(const std::string &urlEncoded)
Definition: httpserver.cpp:679
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:356
std::function< void(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:41
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:414
bool StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:431
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:455
const char * prefix
Definition: rest.cpp:564
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:565