PRCYCoin  2.0.0.7rc1
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1 // Copyright (c) 2015 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 
37 bool UpdateHTTPServerLogging(bool enable);
38 
40 typedef std::function<void(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  HTTPRequest(struct evhttp_request* req);
65  ~HTTPRequest();
66 
69  GET,
73  };
74 
77  std::string GetURI();
78 
81  CService GetPeer();
82 
86 
91  std::pair<bool, std::string> GetHeader(const std::string& hdr);
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(void)>& handler);
138  ~HTTPEvent();
139 
143  void trigger(struct timeval* tv);
144 
146  std::function<void(void)> handler;
147 private:
148  struct event* ev;
149 };
150 
151 #endif // BITCOIN_HTTPSERVER_H
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:133
HTTPRequest::HEAD
@ HEAD
Definition: httpserver.h:71
HTTPRequest::GetPeer
CService GetPeer()
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:655
InterruptHTTPServer
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:492
HTTPEvent::~HTTPEvent
~HTTPEvent()
Definition: httpserver.cpp:560
UpdateHTTPServerLogging
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:459
EventBase
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:540
HTTPRequest::~HTTPRequest
~HTTPRequest()
Definition: httpserver.cpp:575
InitHTTPServer
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:393
HTTPClosure::~HTTPClosure
virtual ~HTTPClosure()
Definition: httpserver.h:125
HTTPRequest::WriteHeader
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:616
HTTPRequest::PUT
@ PUT
Definition: httpserver.h:72
HTTPEvent::ev
struct event * ev
Definition: httpserver.h:148
RegisterHTTPHandler
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:695
prefix
const char * prefix
Definition: rest.cpp:588
HTTPRequest::replySent
bool replySent
Definition: httpserver.h:61
HTTPRequest::POST
@ POST
Definition: httpserver.h:70
HTTPRequest
In-flight HTTP request.
Definition: httpserver.h:57
HTTPRequest::ReadBody
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:596
HTTPRequest::UNKNOWN
@ UNKNOWN
Definition: httpserver.h:68
HTTPRequest::GetHeader
std::pair< bool, std::string > GetHeader(const std::string &hdr)
Get the request header specified by hdr, or an empty string.
Definition: httpserver.cpp:585
HTTPEvent::deleteWhenTriggered
bool deleteWhenTriggered
Definition: httpserver.h:145
HTTPRequest::RequestMethod
RequestMethod
Definition: httpserver.h:67
HTTPRequest::req
struct evhttp_request * req
Definition: httpserver.h:60
HTTPEvent::HTTPEvent
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void(void)> &handler)
Create a new event.
Definition: httpserver.cpp:554
HTTPEvent::trigger
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:564
HTTPEvent
Event class.
Definition: httpserver.h:130
HTTPRequest::GetURI
std::string GetURI()
Get requested URI.
Definition: httpserver.cpp:669
HTTPRequest::HTTPRequest
HTTPRequest(struct evhttp_request *req)
Definition: httpserver.cpp:571
StartHTTPServer
bool StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:476
HTTPClosure
Event handler closure.
Definition: httpserver.h:121
StopHTTPServer
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:505
HTTPEvent::handler
std::function< void(void)> handler
Definition: httpserver.h:146
HTTPRequest::GetRequestMethod
RequestMethod GetRequestMethod()
Get request method.
Definition: httpserver.cpp:674
HTTPRequest::GET
@ GET
Definition: httpserver.h:69
handler
bool(* handler)(HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:589
HTTPRequestHandler
std::function< void(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:40
HTTPClosure::operator()
virtual void operator()()=0
HTTPRequest::WriteReply
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:628
UnregisterHTTPHandler
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:701