Bitcoin ABC  0.26.3
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2016 The Bitcoin Core developers
2 // Copyright (c) 2018-2019 The Bitcoin 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 BITCOIN_HTTPSERVER_H
7 #define BITCOIN_HTTPSERVER_H
8 
9 #include <functional>
10 #include <string>
11 
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;
15 
16 struct evhttp_request;
17 struct event_base;
18 
19 class Config;
20 class CService;
21 class HTTPRequest;
22 
27 bool InitHTTPServer(Config &config);
28 
34 void StartHTTPServer();
35 
37 void InterruptHTTPServer();
38 
40 void StopHTTPServer();
41 
46 bool UpdateHTTPServerLogging(bool enable);
47 
49 typedef std::function<bool(Config &config, HTTPRequest *req,
50  const std::string &)>
52 
58 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch,
60 
62 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
63 
68 struct event_base *EventBase();
69 
74 class HTTPRequest {
75 private:
76  struct evhttp_request *req;
77  bool replySent;
78 
79 public:
80  explicit HTTPRequest(struct evhttp_request *req, bool replySent = false);
81  ~HTTPRequest();
82 
84 
86  std::string GetURI() const;
87 
89  CService GetPeer() const;
90 
93 
98  std::pair<bool, std::string> GetHeader(const std::string &hdr) const;
99 
106  std::string ReadBody();
107 
113  void WriteHeader(const std::string &hdr, const std::string &value);
114 
125  void WriteReply(int nStatus, const std::string &strReply = "");
126 };
127 
129 class HTTPClosure {
130 public:
131  virtual void operator()() = 0;
132  virtual ~HTTPClosure() {}
133 };
134 
139 class HTTPEvent {
140 public:
147  HTTPEvent(struct event_base *base, bool deleteWhenTriggered,
148  const std::function<void()> &handler);
149  ~HTTPEvent();
150 
155  void trigger(struct timeval *tv);
156 
158  std::function<void()> handler;
159 
160 private:
161  struct event *ev;
162 };
163 
164 #endif // BITCOIN_HTTPSERVER_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
Definition: config.h:17
Event handler closure.
Definition: httpserver.h:129
virtual void operator()()=0
virtual ~HTTPClosure()
Definition: httpserver.h:132
Event class.
Definition: httpserver.h:139
struct event * ev
Definition: httpserver.h:161
bool deleteWhenTriggered
Definition: httpserver.h:157
std::function< void()> handler
Definition: httpserver.h:158
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:530
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:539
In-flight HTTP request.
Definition: httpserver.h:74
bool replySent
Definition: httpserver.h:77
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:560
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:651
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:607
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:595
struct evhttp_request * req
Definition: httpserver.h:76
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:655
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:571
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:638
HTTPRequest(struct evhttp_request *req, bool replySent=false)
Definition: httpserver.cpp:548
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:472
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:14
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:679
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:672
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:517
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:13
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:460
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:443
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:12
std::function< bool(Config &config, HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:51
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:483
bool InitHTTPServer(Config &config)
Initialize HTTP server.
Definition: httpserver.cpp:382
const char * prefix
Definition: rest.cpp:821
bool(* handler)(Config &config, const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:822