Bitcoin ABC  0.24.7
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2016 The Bitcoin Core developers
2 // Copyright (c) 2018-2019 The Bitcoin developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_HTTPSERVER_H
7 #define BITCOIN_HTTPSERVER_H
8 
9 #include <functional>
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 
19 class Config;
20 class CService;
21 class HTTPRequest;
22 
27 bool InitHTTPServer(Config &config);
28 
34 void StartHTTPServer();
35 
37 void InterruptHTTPServer();
38 
40 void StopHTTPServer();
41 
46 bool UpdateHTTPServerLogging(bool enable);
47 
49 typedef std::function<bool(Config &config, HTTPRequest *req,
50  const std::string &)>
52 
58 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch,
60 
62 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
63 
68 struct event_base *EventBase();
69 
74 class HTTPRequest {
75 private:
76  struct evhttp_request *req;
77  bool replySent;
78 
79 public:
80  explicit HTTPRequest(struct evhttp_request *req, bool replySent = false);
81  ~HTTPRequest();
82 
84 
86  std::string GetURI() const;
87 
89  CService GetPeer() const;
90 
93 
98  std::pair<bool, std::string> GetHeader(const std::string &hdr) const;
99 
106  std::string ReadBody();
107 
113  void WriteHeader(const std::string &hdr, const std::string &value);
114 
125  void WriteReply(int nStatus, const std::string &strReply = "");
126 };
127 
129 class HTTPClosure {
130 public:
131  virtual void operator()() = 0;
132  virtual ~HTTPClosure() {}
133 };
134 
139 class HTTPEvent {
140 public:
147  HTTPEvent(struct event_base *base, bool deleteWhenTriggered,
148  const std::function<void()> &handler);
149  ~HTTPEvent();
150 
155  void trigger(struct timeval *tv);
156 
158  std::function<void()> handler;
159 
160 private:
161  struct event *ev;
162 };
163 
164 #endif // BITCOIN_HTTPSERVER_H
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:514
HTTPRequest::HEAD
@ HEAD
Definition: httpserver.h:83
HTTPRequest::GetRequestMethod
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:661
InterruptHTTPServer
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:478
HTTPEvent::~HTTPEvent
~HTTPEvent()
Definition: httpserver.cpp:542
UpdateHTTPServerLogging
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:449
DEFAULT_HTTP_WORKQUEUE
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:13
EventBase
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:523
HTTPRequest::~HTTPRequest
~HTTPRequest()
Definition: httpserver.cpp:556
HTTPRequest::GetPeer
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:644
StartHTTPServer
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:466
HTTPClosure::~HTTPClosure
virtual ~HTTPClosure()
Definition: httpserver.h:132
HTTPRequest::WriteHeader
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:601
HTTPRequest::PUT
@ PUT
Definition: httpserver.h:83
HTTPRequest::GetURI
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:657
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:566
HTTPRequest::OPTIONS
@ OPTIONS
Definition: httpserver.h:83
HTTPEvent::ev
struct event * ev
Definition: httpserver.h:161
HTTPEvent::HTTPEvent
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:536
RegisterHTTPHandler
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:678
prefix
const char * prefix
Definition: rest.cpp:772
HTTPRequest::replySent
bool replySent
Definition: httpserver.h:77
Config
Definition: config.h:17
HTTPRequest::POST
@ POST
Definition: httpserver.h:83
HTTPRequest
In-flight HTTP request.
Definition: httpserver.h:74
HTTPRequest::ReadBody
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:577
HTTPRequest::UNKNOWN
@ UNKNOWN
Definition: httpserver.h:83
HTTPRequest::HTTPRequest
HTTPRequest(struct evhttp_request *req, bool replySent=false)
Definition: httpserver.cpp:554
HTTPEvent::deleteWhenTriggered
bool deleteWhenTriggered
Definition: httpserver.h:157
handler
bool(* handler)(Config &config, const util::Ref &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:773
HTTPRequest::RequestMethod
RequestMethod
Definition: httpserver.h:83
HTTPRequest::req
struct evhttp_request * req
Definition: httpserver.h:76
HTTPEvent::trigger
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:545
HTTPEvent
Event class.
Definition: httpserver.h:139
HTTPClosure
Event handler closure.
Definition: httpserver.h:129
StopHTTPServer
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:489
HTTPRequestHandler
std::function< bool(Config &config, HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:51
HTTPRequest::GET
@ GET
Definition: httpserver.h:83
HTTPClosure::operator()
virtual void operator()()=0
InitHTTPServer
bool InitHTTPServer(Config &config)
Initialize HTTP server.
Definition: httpserver.cpp:388
HTTPRequest::WriteReply
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:613
DEFAULT_HTTP_THREADS
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:12
UnregisterHTTPHandler
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:685
DEFAULT_HTTP_SERVER_TIMEOUT
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:14
HTTPEvent::handler
std::function< void()> handler
Definition: httpserver.h:158