Bitcoin
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2018 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 <stdint.h>
10 #include <functional>
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 
37 bool UpdateHTTPServerLogging(bool enable);
38 
40 typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
45 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
47 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
48 
52 struct event_base* EventBase();
53 
58 {
59 private:
60  struct evhttp_request* req;
61  bool replySent;
62 
63 public:
64  explicit HTTPRequest(struct evhttp_request* req);
65  ~HTTPRequest();
66 
69  GET,
73  };
74 
77  std::string GetURI() const;
78 
81  CService GetPeer() const;
82 
86 
91  std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
92 
99  std::string ReadBody();
100 
106  void WriteHeader(const std::string& hdr, const std::string& value);
107 
116  void WriteReply(int nStatus, const std::string& strReply = "");
117 };
118 
122 {
123 public:
124  virtual void operator()() = 0;
125  virtual ~HTTPClosure() {}
126 };
127 
131 {
132 public:
137  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
138  ~HTTPEvent();
139 
143  void trigger(struct timeval* tv);
144 
146  std::function<void()> handler;
147 private:
148  struct event* ev;
149 };
150 
151 #endif // BITCOIN_HTTPSERVER_H
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:627
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:14
bool InitHTTPServer()
Definition: httpserver.cpp:358
virtual ~HTTPClosure()
Definition: httpserver.h:125
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:13
bool replySent
Definition: httpserver.h:61
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Definition: httpserver.cpp:496
const char * prefix
Definition: rest.cpp:626
std::string ReadBody()
Definition: httpserver.cpp:538
Definition: httpserver.h:69
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Definition: httpserver.cpp:646
void trigger(struct timeval *tv)
Definition: httpserver.cpp:506
void InterruptHTTPServer()
Definition: httpserver.cpp:438
std::function< void()> handler
Definition: httpserver.h:146
Definition: httpserver.h:130
void StartHTTPServer()
Definition: httpserver.cpp:426
Definition: httpserver.h:72
Definition: httpserver.h:68
~HTTPEvent()
Definition: httpserver.cpp:502
struct event_base * EventBase()
Definition: httpserver.cpp:482
CService GetPeer() const
Definition: httpserver.cpp:600
void WriteReply(int nStatus, const std::string &strReply="")
Definition: httpserver.cpp:570
Definition: httpserver.h:70
Definition: httpserver.h:121
struct event * ev
Definition: httpserver.h:148
RequestMethod
Definition: httpserver.h:67
std::string GetURI() const
Definition: httpserver.cpp:614
virtual void operator()()=0
HTTPRequest(struct evhttp_request *req)
Definition: httpserver.cpp:513
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Definition: httpserver.cpp:640
bool UpdateHTTPServerLogging(bool enable)
Definition: httpserver.cpp:409
Definition: httpserver.h:71
RequestMethod GetRequestMethod() const
Definition: httpserver.cpp:619
struct evhttp_request * req
Definition: httpserver.h:60
Definition: netaddress.h:140
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Definition: httpserver.h:40
void WriteHeader(const std::string &hdr, const std::string &value)
Definition: httpserver.cpp:558
std::pair< bool, std::string > GetHeader(const std::string &hdr) const
Definition: httpserver.cpp:527
void StopHTTPServer()
Definition: httpserver.cpp:449
bool deleteWhenTriggered
Definition: httpserver.h:145
~HTTPRequest()
Definition: httpserver.cpp:517
Definition: httpserver.h:57
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:12