Bitcoin Core  27.99.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 <string>
11 
12 namespace util {
13 class SignalInterrupt;
14 } // namespace util
15 
16 static const int DEFAULT_HTTP_THREADS=4;
17 static const int DEFAULT_HTTP_WORKQUEUE=16;
18 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
19 
20 struct evhttp_request;
21 struct event_base;
22 class CService;
23 class HTTPRequest;
24 
28 bool InitHTTPServer(const util::SignalInterrupt& interrupt);
33 void StartHTTPServer();
35 void InterruptHTTPServer();
37 void StopHTTPServer();
38 
40 void UpdateHTTPServerLogging(bool enable);
41 
43 typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
48 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
50 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
51 
55 struct event_base* EventBase();
56 
61 {
62 private:
63  struct evhttp_request* req;
65  bool replySent;
66 
67 public:
68  explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false);
69  ~HTTPRequest();
70 
73  GET,
76  PUT
77  };
78 
81  std::string GetURI() const;
82 
85  CService GetPeer() const;
86 
90 
100  std::optional<std::string> GetQueryParameter(const std::string& key) const;
101 
106  std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
107 
114  std::string ReadBody();
115 
121  void WriteHeader(const std::string& hdr, const std::string& value);
122 
131  void WriteReply(int nStatus, const std::string& strReply = "");
132 };
133 
146 std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key);
147 
151 {
152 public:
153  virtual void operator()() = 0;
154  virtual ~HTTPClosure() {}
155 };
156 
160 {
161 public:
166  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
167  ~HTTPEvent();
168 
172  void trigger(struct timeval* tv);
173 
175  std::function<void()> handler;
176 private:
177  struct event* ev;
178 };
179 
180 #endif // BITCOIN_HTTPSERVER_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
Event handler closure.
Definition: httpserver.h:151
virtual void operator()()=0
virtual ~HTTPClosure()
Definition: httpserver.h:154
Event class.
Definition: httpserver.h:160
struct event * ev
Definition: httpserver.h:177
bool deleteWhenTriggered
Definition: httpserver.h:174
std::function< void()> handler
Definition: httpserver.h:175
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:564
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:574
In-flight HTTP request.
Definition: httpserver.h:61
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:710
bool replySent
Definition: httpserver.h:65
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:596
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:689
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:639
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:627
HTTPRequest(struct evhttp_request *req, const util::SignalInterrupt &interrupt, bool replySent=false)
Definition: httpserver.cpp:581
struct evhttp_request * req
Definition: httpserver.h:63
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:694
const util::SignalInterrupt & m_interrupt
Definition: httpserver.h:64
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:607
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:669
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:497
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:18
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:717
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:751
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:744
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:550
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:17
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:486
void UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:475
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:16
bool InitHTTPServer(const util::SignalInterrupt &interrupt)
Initialize HTTP server.
Definition: httpserver.cpp:428
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:43
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:509
const char * prefix
Definition: rest.cpp:1007
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1008