Bitcoin Core  24.99.0
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 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 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 class CService;
19 class HTTPRequest;
20 
24 bool InitHTTPServer();
29 void StartHTTPServer();
31 void InterruptHTTPServer();
33 void StopHTTPServer();
34 
36 void 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,
71  PUT
72  };
73 
76  std::string GetURI() const;
77 
80  CService GetPeer() const;
81 
85 
95  std::optional<std::string> GetQueryParameter(const std::string& key) const;
96 
101  std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
102 
109  std::string ReadBody();
110 
116  void WriteHeader(const std::string& hdr, const std::string& value);
117 
126  void WriteReply(int nStatus, const std::string& strReply = "");
127 };
128 
141 std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key);
142 
146 {
147 public:
148  virtual void operator()() = 0;
149  virtual ~HTTPClosure() {}
150 };
151 
155 {
156 public:
161  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
162  ~HTTPEvent();
163 
167  void trigger(struct timeval* tv);
168 
170  std::function<void()> handler;
171 private:
172  struct event* ev;
173 };
174 
175 #endif // BITCOIN_HTTPSERVER_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:521
Event handler closure.
Definition: httpserver.h:146
virtual void operator()()=0
virtual ~HTTPClosure()
Definition: httpserver.h:149
Event class.
Definition: httpserver.h:155
struct event * ev
Definition: httpserver.h:172
bool deleteWhenTriggered
Definition: httpserver.h:169
std::function< void()> handler
Definition: httpserver.h:170
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:495
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:505
In-flight HTTP request.
Definition: httpserver.h:57
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:645
bool replySent
Definition: httpserver.h:60
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:526
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:619
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:569
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:557
struct evhttp_request * req
Definition: httpserver.h:59
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:624
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:537
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:599
HTTPRequest(struct evhttp_request *req, bool replySent=false)
Definition: httpserver.cpp:512
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:437
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:14
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:652
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:683
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:676
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:481
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:13
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:425
void UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:414
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:367
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:12
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:39
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:449
const char * prefix
Definition: rest.cpp:930
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:931