Raven Core  3.0.0
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core 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 RAVEN_HTTPSERVER_H
7 #define RAVEN_HTTPSERVER_H
8 
9 #include <string>
10 #include <stdint.h>
11 #include <functional>
12 
13 static const int DEFAULT_HTTP_THREADS=4;
14 static const int DEFAULT_HTTP_WORKQUEUE=16;
15 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
16 
17 struct evhttp_request;
18 struct event_base;
19 class CService;
20 class HTTPRequest;
21 
25 bool InitHTTPServer();
30 bool StartHTTPServer();
32 void InterruptHTTPServer();
34 void StopHTTPServer();
35 
38 bool UpdateHTTPServerLogging(bool enable);
39 
41 typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
46 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
48 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
49 
53 struct event_base* EventBase();
54 
59 {
60 private:
61  struct evhttp_request* req;
62  bool replySent;
63 
64 public:
65  explicit HTTPRequest(struct evhttp_request* req);
66  ~HTTPRequest();
67 
70  GET,
74  };
75 
78  std::string GetURI();
79 
82  CService GetPeer();
83 
87 
92  std::pair<bool, std::string> GetHeader(const std::string& hdr);
93 
100  std::string ReadBody();
101 
107  void WriteHeader(const std::string& hdr, const std::string& value);
108 
117  void WriteReply(int nStatus, const std::string& strReply = "");
118 };
119 
123 {
124 public:
125  virtual void operator()() = 0;
126  virtual ~HTTPClosure() {}
127 };
128 
132 {
133 public:
138  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void(void)>& handler);
139  ~HTTPEvent();
140 
144  void trigger(struct timeval* tv);
145 
147  std::function<void(void)> handler;
148 private:
149  struct event* ev;
150 };
151 
152 std::string urlDecode(const std::string &urlEncoded);
153 
154 #endif // RAVEN_HTTPSERVER_H
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:569
HTTPRequest(struct evhttp_request *req)
Definition: httpserver.cpp:541
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:368
std::pair< bool, std::string > GetHeader(const std::string &hdr)
Get the request header specified by hdr, or an empty string.
Definition: httpserver.cpp:555
Event class.
Definition: httpserver.h:131
const char * prefix
Definition: rest.cpp:568
std::string GetURI()
Get requested URI.
Definition: httpserver.cpp:626
struct evhttp_request * req
Definition: httpserver.h:61
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:658
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:459
RequestMethod GetRequestMethod()
Get request method.
Definition: httpserver.cpp:631
Event handler closure.
Definition: httpserver.h:122
struct event * ev
Definition: httpserver.h:149
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:598
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:510
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:141
std::function< void(void)> handler
Definition: httpserver.h:147
CService GetPeer()
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:612
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:652
virtual ~HTTPClosure()
Definition: httpserver.h:126
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:586
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:426
bool deleteWhenTriggered
Definition: httpserver.h:146
std::string urlDecode(const std::string &urlEncoded)
Definition: httpserver.cpp:672
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:41
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:566
bool StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:443
In-flight HTTP request.
Definition: httpserver.h:58
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:474
bool replySent
Definition: httpserver.h:62