Bitcoin Core  28.1.0
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2022 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 <functional>
9 #include <optional>
10 #include <span>
11 #include <string>
12 
13 namespace util {
14 class SignalInterrupt;
15 } // namespace util
16 
17 static const int DEFAULT_HTTP_THREADS=4;
18 static const int DEFAULT_HTTP_WORKQUEUE=16;
19 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
20 
21 struct evhttp_request;
22 struct event_base;
23 class CService;
24 class HTTPRequest;
25 
29 bool InitHTTPServer(const util::SignalInterrupt& interrupt);
34 void StartHTTPServer();
36 void InterruptHTTPServer();
38 void StopHTTPServer();
39 
41 void UpdateHTTPServerLogging(bool enable);
42 
44 typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
49 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
51 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
52 
56 struct event_base* EventBase();
57 
62 {
63 private:
64  struct evhttp_request* req;
66  bool replySent;
67 
68 public:
69  explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false);
70  ~HTTPRequest();
71 
74  GET,
78  };
79 
82  std::string GetURI() const;
83 
86  CService GetPeer() const;
87 
91 
101  std::optional<std::string> GetQueryParameter(const std::string& key) const;
102 
107  std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
108 
115  std::string ReadBody();
116 
122  void WriteHeader(const std::string& hdr, const std::string& value);
123 
132  void WriteReply(int nStatus, std::string_view reply = "")
133  {
134  WriteReply(nStatus, std::as_bytes(std::span{reply}));
135  }
136  void WriteReply(int nStatus, std::span<const std::byte> reply);
137 };
138 
151 std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key);
152 
156 {
157 public:
158  virtual void operator()() = 0;
159  virtual ~HTTPClosure() = default;
160 };
161 
165 {
166 public:
171  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
172  ~HTTPEvent();
173 
177  void trigger(struct timeval* tv);
178 
180  std::function<void()> handler;
181 private:
182  struct event* ev;
183 };
184 
185 #endif // BITCOIN_HTTPSERVER_H
HTTPRequest(struct evhttp_request *req, const util::SignalInterrupt &interrupt, bool replySent=false)
Definition: httpserver.cpp:580
void WriteReply(int nStatus, std::string_view reply="")
Write HTTP reply.
Definition: httpserver.h:132
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:19
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:18
void UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:474
Event class.
Definition: httpserver.h:164
const char * prefix
Definition: rest.cpp:1007
std::optional< std::string > GetQueryParameter(const std::string &key) const
Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not...
Definition: httpserver.cpp:709
struct evhttp_request * req
Definition: httpserver.h:64
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:750
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:496
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1008
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:485
Event handler closure.
Definition: httpserver.h:155
struct event * ev
Definition: httpserver.h:182
std::optional< std::string > GetQueryParameterFromUri(const char *uri, const std::string &key)
Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not...
Definition: httpserver.cpp:716
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:549
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:563
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:693
bool InitHTTPServer(const util::SignalInterrupt &interrupt)
Initialize HTTP server.
Definition: httpserver.cpp:427
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
virtual void operator()()=0
const util::SignalInterrupt & m_interrupt
Definition: httpserver.h:65
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
virtual ~HTTPClosure()=default
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:743
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:626
bool deleteWhenTriggered
Definition: httpserver.h:179
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:573
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:595
std::function< void()> handler
Definition: httpserver.h:180
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:44
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:668
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:606
In-flight HTTP request.
Definition: httpserver.h:61
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:508
bool replySent
Definition: httpserver.h:66
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:17
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:688