Fabcoin Core  0.16.2
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2017 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 FABCOIN_HTTPSERVER_H
6 #define FABCOIN_HTTPSERVER_H
7 
8 #include <string>
9 #include <stdint.h>
10 #include <functional>
11 #include <mutex>
12 #include <condition_variable>
13 
14 static const int DEFAULT_HTTP_THREADS=4;
15 static const int DEFAULT_HTTP_WORKQUEUE=16;
16 static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
17 
18 struct evhttp_request;
19 struct event_base;
20 class CService;
21 class HTTPRequest;
22 
26 bool InitHTTPServer();
31 bool StartHTTPServer();
33 void InterruptHTTPServer();
35 void StopHTTPServer();
36 
39 bool UpdateHTTPServerLogging(bool enable);
40 
42 typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
47 void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
49 void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
50 
54 struct event_base* EventBase();
55 
60 {
61 private:
62  struct evhttp_request* req;
63  bool replySent;
65  bool connClosed;
66 
67  std::mutex cs;
68  std::condition_variable closeCv;
69 
71  void waitClientClose();
72 
73 public:
74  HTTPRequest(struct evhttp_request* req);
75  ~HTTPRequest();
76 
79  GET,
83  };
84 
85  void setConnClosed();
86  bool isConnClosed();
87  bool isChunkMode();
88 
91  std::string GetURI();
92 
95  CService GetPeer();
96 
100 
105  std::pair<bool, std::string> GetHeader(const std::string& hdr);
106 
113  std::string ReadBody();
114 
120  void WriteHeader(const std::string& hdr, const std::string& value);
121 
130  void WriteReply(int nStatus, const std::string& strReply = "");
131 
135  void Chunk(const std::string& chunk);
136 
140  void ChunkEnd();
141 
145  bool ReplySent();
146 };
147 
151 {
152 public:
153  virtual void operator()() = 0;
154  virtual ~HTTPClosure() {}
155 };
156 
160 {
161 public:
166  HTTPEvent(struct event_base* base, bool deleteWhenTriggered, struct evbuffer *_databuf, const std::function<void(void)>& handler);
167  ~HTTPEvent();
168 
172  void trigger(struct timeval* tv);
173 
175  std::function<void(void)> handler;
176 private:
177  struct evbuffer *databuf;
178  struct event* ev;
179 };
180 
181 std::string urlDecode(const std::string &urlEncoded);
182 
183 #endif // FABCOIN_HTTPSERVER_H
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:624
#define function(a, b, c, d, k, s)
bool ReplySent()
Is reply sent?
Definition: httpserver.cpp:660
HTTPRequest(struct evhttp_request *req)
Definition: httpserver.cpp:553
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:379
std::pair< bool, std::string > GetHeader(const std::string &hdr)
Get the request header specified by hdr, or an empty string.
Definition: httpserver.cpp:629
Event class.
Definition: httpserver.h:159
const char * prefix
Definition: rest.cpp:623
std::string GetURI()
Get requested URI.
Definition: httpserver.cpp:760
void Chunk(const std::string &chunk)
Start chunk transfer.
Definition: httpserver.cpp:689
struct evhttp_request * req
Definition: httpserver.h:62
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:792
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:470
RequestMethod GetRequestMethod()
Get request method.
Definition: httpserver.cpp:765
bool isConnClosed()
Definition: httpserver.cpp:620
void ChunkEnd()
End chunk transfer.
Definition: httpserver.cpp:671
Event handler closure.
Definition: httpserver.h:150
struct event * ev
Definition: httpserver.h:178
std::mutex cs
Definition: httpserver.h:67
bool connClosed
Definition: httpserver.h:65
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:719
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:519
void startDetectClientClose()
Definition: httpserver.cpp:587
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:140
struct evbuffer * databuf
Definition: httpserver.h:177
std::function< void(void)> handler
Definition: httpserver.h:175
bool isChunkMode()
Definition: httpserver.cpp:625
CService GetPeer()
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:746
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:786
virtual ~HTTPClosure()
Definition: httpserver.h:154
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:664
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:437
bool deleteWhenTriggered
Definition: httpserver.h:174
std::string urlDecode(const std::string &urlEncoded)
Definition: httpserver.cpp:806
bool startedChunkTransfer
Definition: httpserver.h:64
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:42
void waitClientClose()
Definition: httpserver.cpp:569
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:640
bool StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:454
In-flight HTTP request.
Definition: httpserver.h:59
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:485
void setConnClosed()
Definition: httpserver.cpp:614
std::condition_variable closeCv
Definition: httpserver.h:68
bool replySent
Definition: httpserver.h:63