Bitcoin Core  0.19.99
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2019 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.
4 
5 #ifndef BITCOIN_HTTPSERVER_H
6 #define BITCOIN_HTTPSERVER_H
7 
8 #include <string>
9 #include <functional>
10 
11 static const int DEFAULT_HTTP_THREADS=4;
12 static const int DEFAULT_HTTP_WORKQUEUE=16;
13 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
14 
15 struct evhttp_request;
16 struct event_base;
17 class CService;
18 class HTTPRequest;
19 
23 bool InitHTTPServer();
28 void StartHTTPServer();
30 void InterruptHTTPServer();
32 void StopHTTPServer();
33 
36 bool UpdateHTTPServerLogging(bool enable);
37 
39 typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
44 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
46 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
47 
51 struct event_base* EventBase();
52 
57 {
58 private:
59  struct evhttp_request* req;
60  bool replySent;
61 
62 public:
63  explicit HTTPRequest(struct evhttp_request* req, bool replySent = false);
64  ~HTTPRequest();
65 
68  GET,
72  };
73 
76  std::string GetURI() const;
77 
80  CService GetPeer() const;
81 
85 
90  std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
91 
98  std::string ReadBody();
99 
105  void WriteHeader(const std::string& hdr, const std::string& value);
106 
115  void WriteReply(int nStatus, const std::string& strReply = "");
116 };
117 
121 {
122 public:
123  virtual void operator()() = 0;
124  virtual ~HTTPClosure() {}
125 };
126 
130 {
131 public:
136  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
137  ~HTTPEvent();
138 
142  void trigger(struct timeval* tv);
143 
145  std::function<void()> handler;
146 private:
147  struct event* ev;
148 };
149 
150 #endif // BITCOIN_HTTPSERVER_H
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:651
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:13
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:358
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:12
Event class.
Definition: httpserver.h:129
const char * prefix
Definition: rest.cpp:650
struct evhttp_request * req
Definition: httpserver.h:59
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:646
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:438
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:426
Event handler closure.
Definition: httpserver.h:120
struct event * ev
Definition: httpserver.h:147
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:570
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:482
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:619
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:150
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:640
virtual ~HTTPClosure()
Definition: httpserver.h:124
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:558
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:409
bool deleteWhenTriggered
Definition: httpserver.h:144
std::pair< bool, std::string > GetHeader(const std::string &hdr) const
Get the request header specified by hdr, or an empty string.
Definition: httpserver.cpp:527
std::function< void()> handler
Definition: httpserver.h:145
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:39
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:600
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:538
In-flight HTTP request.
Definition: httpserver.h:56
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:449
bool replySent
Definition: httpserver.h:60
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:11
HTTPRequest(struct evhttp_request *req, bool replySent=false)
Definition: httpserver.cpp:513
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:614