Bitcoin Core  29.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 
20 static const int DEFAULT_HTTP_THREADS=16;
21 
26 static const int DEFAULT_HTTP_WORKQUEUE=64;
27 
28 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
29 
30 struct evhttp_request;
31 struct event_base;
32 class CService;
33 class HTTPRequest;
34 
38 bool InitHTTPServer(const util::SignalInterrupt& interrupt);
43 void StartHTTPServer();
45 void InterruptHTTPServer();
47 void StopHTTPServer();
48 
50 void UpdateHTTPServerLogging(bool enable);
51 
53 typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
58 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
60 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
61 
65 struct event_base* EventBase();
66 
71 {
72 private:
73  struct evhttp_request* req;
75  bool replySent;
76 
77 public:
78  explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false);
79  ~HTTPRequest();
80 
83  GET,
87  };
88 
91  std::string GetURI() const;
92 
95  CService GetPeer() const;
96 
100 
110  std::optional<std::string> GetQueryParameter(const std::string& key) const;
111 
116  std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
117 
124  std::string ReadBody();
125 
131  void WriteHeader(const std::string& hdr, const std::string& value);
132 
141  void WriteReply(int nStatus, std::string_view reply = "")
142  {
143  WriteReply(nStatus, std::as_bytes(std::span{reply}));
144  }
145  void WriteReply(int nStatus, std::span<const std::byte> reply);
146 };
147 
160 std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key);
161 
165 {
166 public:
167  virtual void operator()() = 0;
168  virtual ~HTTPClosure() = default;
169 };
170 
174 {
175 public:
180  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
181  ~HTTPEvent();
182 
186  void trigger(struct timeval* tv);
187 
189  std::function<void()> handler;
190 private:
191  struct event* ev;
192 };
193 
194 #endif // BITCOIN_HTTPSERVER_H
HTTPRequest(struct evhttp_request *req, const util::SignalInterrupt &interrupt, bool replySent=false)
Definition: httpserver.cpp:594
void WriteReply(int nStatus, std::string_view reply="")
Write HTTP reply.
Definition: httpserver.h:141
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:28
static const int DEFAULT_HTTP_WORKQUEUE
The default value for -rpcworkqueue.
Definition: httpserver.h:26
void UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:488
Event class.
Definition: httpserver.h:173
const char * prefix
Definition: rest.cpp:1009
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:723
struct evhttp_request * req
Definition: httpserver.h:73
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:764
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:510
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1010
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:499
Event handler closure.
Definition: httpserver.h:164
struct event * ev
Definition: httpserver.h:191
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:730
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:563
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:577
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:707
bool InitHTTPServer(const util::SignalInterrupt &interrupt)
Initialize HTTP server.
Definition: httpserver.cpp:441
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:74
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:757
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:640
bool deleteWhenTriggered
Definition: httpserver.h:188
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:587
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:609
std::function< void()> handler
Definition: httpserver.h:189
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:53
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:682
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:620
In-flight HTTP request.
Definition: httpserver.h:70
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:522
bool replySent
Definition: httpserver.h:75
static const int DEFAULT_HTTP_THREADS
The default value for -rpcthreads.
Definition: httpserver.h:20
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:702