Dogecoin Core  1.14.2
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2016 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 bool StartHTTPServer();
31 void InterruptHTTPServer();
33 void StopHTTPServer();
34 
36 typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
41 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
43 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
44 
48 struct event_base* EventBase();
49 
54 {
55 private:
56  struct evhttp_request* req;
57  bool replySent;
58 
59 public:
60  HTTPRequest(struct evhttp_request* req);
61  ~HTTPRequest();
62 
65  GET,
68  PUT
69  };
70 
73  std::string GetURI();
74 
77  CService GetPeer();
78 
82 
87  std::pair<bool, std::string> GetHeader(const std::string& hdr);
88 
95  std::string ReadBody();
96 
102  void WriteHeader(const std::string& hdr, const std::string& value);
103 
112  void WriteReply(int nStatus, const std::string& strReply = "");
113 };
114 
118 {
119 public:
120  virtual void operator()() = 0;
121  virtual ~HTTPClosure() {}
122 };
123 
127 {
128 public:
133  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void(void)>& handler);
134  ~HTTPEvent();
135 
139  void trigger(struct timeval* tv);
140 
142  std::function<void(void)> handler;
143 private:
144  struct event* ev;
145 };
146 
147 #endif // BITCOIN_HTTPSERVER_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:134
Event handler closure.
Definition: httpserver.h:118
virtual void operator()()=0
virtual ~HTTPClosure()
Definition: httpserver.h:121
Event class.
Definition: httpserver.h:127
struct event * ev
Definition: httpserver.h:144
bool deleteWhenTriggered
Definition: httpserver.h:141
std::function< void(void)> handler
Definition: httpserver.h:142
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void(void)> &handler)
Create a new event.
Definition: httpserver.cpp:526
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:536
In-flight HTTP request.
Definition: httpserver.h:54
bool replySent
Definition: httpserver.h:57
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:600
std::string GetURI()
Get requested URI.
Definition: httpserver.cpp:628
CService GetPeer()
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:614
std::pair< bool, std::string > GetHeader(const std::string &hdr)
Get the request header specified by hdr, or an empty string.
Definition: httpserver.cpp:557
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:588
struct evhttp_request * req
Definition: httpserver.h:56
HTTPRequest(struct evhttp_request *req)
Definition: httpserver.cpp:543
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:568
RequestMethod GetRequestMethod()
Get request method.
Definition: httpserver.cpp:633
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:464
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:660
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:654
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:512
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:378
bool StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:448
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:36
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:479
const char * prefix
Definition: rest.cpp:605
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:606
struct event_base * base
Definition: torcontrol.cpp:679