Bitcoin Core  22.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 <string>
9 #include <functional>
10 
11 static const int DEFAULT_HTTP_THREADS=4;
12 static const int DEFAULT_HTTP_WORKQUEUE=16;
13 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
14 
15 struct evhttp_request;
16 struct event_base;
17 class CService;
18 class HTTPRequest;
19 
23 bool InitHTTPServer();
28 void StartHTTPServer();
30 void InterruptHTTPServer();
32 void StopHTTPServer();
33 
36 bool 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,
72  };
73 
76  std::string GetURI() const;
77 
80  CService GetPeer() const;
81 
85 
90  std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
91 
98  std::string ReadBody();
99 
105  void WriteHeader(const std::string& hdr, const std::string& value);
106 
115  void WriteReply(int nStatus, const std::string& strReply = "");
116 };
117 
121 {
122 public:
123  virtual void operator()() = 0;
124  virtual ~HTTPClosure() {}
125 };
126 
130 {
131 public:
136  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
137  ~HTTPEvent();
138 
142  void trigger(struct timeval* tv);
143 
145  std::function<void()> handler;
146 private:
147  struct event* ev;
148 };
149 
150 #endif // BITCOIN_HTTPSERVER_H
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:539
HTTPRequest::HEAD
@ HEAD
Definition: httpserver.h:70
HTTPRequest::GetRequestMethod
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:608
InterruptHTTPServer
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:427
HTTPEvent::~HTTPEvent
~HTTPEvent()
Definition: httpserver.cpp:491
UpdateHTTPServerLogging
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:398
DEFAULT_HTTP_WORKQUEUE
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:12
EventBase
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:471
HTTPRequest::~HTTPRequest
~HTTPRequest()
Definition: httpserver.cpp:506
InitHTTPServer
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:347
handler
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:713
HTTPRequest::GetPeer
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:589
StartHTTPServer
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:415
HTTPClosure::~HTTPClosure
virtual ~HTTPClosure()
Definition: httpserver.h:124
HTTPRequest::WriteHeader
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:547
HTTPRequest::PUT
@ PUT
Definition: httpserver.h:71
HTTPRequest::GetURI
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:603
HTTPRequest::GetHeader
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:516
HTTPEvent::ev
struct event * ev
Definition: httpserver.h:147
HTTPEvent::HTTPEvent
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:485
RegisterHTTPHandler
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:629
prefix
const char * prefix
Definition: rest.cpp:712
HTTPRequest::replySent
bool replySent
Definition: httpserver.h:60
HTTPRequest::POST
@ POST
Definition: httpserver.h:69
HTTPRequest
In-flight HTTP request.
Definition: httpserver.h:56
HTTPRequest::ReadBody
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:527
HTTPRequest::UNKNOWN
@ UNKNOWN
Definition: httpserver.h:67
HTTPRequest::HTTPRequest
HTTPRequest(struct evhttp_request *req, bool replySent=false)
Definition: httpserver.cpp:502
HTTPEvent::deleteWhenTriggered
bool deleteWhenTriggered
Definition: httpserver.h:144
HTTPRequest::RequestMethod
RequestMethod
Definition: httpserver.h:66
HTTPRequest::req
struct evhttp_request * req
Definition: httpserver.h:59
HTTPEvent::trigger
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:495
HTTPEvent
Event class.
Definition: httpserver.h:129
HTTPRequestHandler
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:39
HTTPClosure
Event handler closure.
Definition: httpserver.h:120
StopHTTPServer
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:439
HTTPRequest::GET
@ GET
Definition: httpserver.h:68
HTTPClosure::operator()
virtual void operator()()=0
HTTPRequest::WriteReply
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:559
DEFAULT_HTTP_THREADS
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:11
UnregisterHTTPHandler
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:635
DEFAULT_HTTP_SERVER_TIMEOUT
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:13
HTTPEvent::handler
std::function< void()> handler
Definition: httpserver.h:145