Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
httpserver.h
Go to the documentation of this file.
1// Copyright (c) 2015-2016 The Bitcoin Core developers
2// Copyright (c) 2018-2019 The Bitcoin 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 BITCOIN_HTTPSERVER_H
7#define BITCOIN_HTTPSERVER_H
8
9#include <functional>
10#include <string>
11
12static const int DEFAULT_HTTP_THREADS = 4;
13static const int DEFAULT_HTTP_WORKQUEUE = 16;
14static const int DEFAULT_HTTP_SERVER_TIMEOUT = 30;
15
16struct evhttp_request;
17struct event_base;
18
19class Config;
20class CService;
21class HTTPRequest;
22
27bool InitHTTPServer(Config &config);
28
34void StartHTTPServer();
35
38
40void StopHTTPServer();
41
47
49typedef std::function<bool(Config &config, HTTPRequest *req,
50 const std::string &)>
52
58void RegisterHTTPHandler(const std::string &prefix, bool exactMatch,
60
62void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
63
68struct event_base *EventBase();
69
75private:
78
79public:
80 explicit HTTPRequest(struct evhttp_request *req, bool replySent = false);
82
84
86 std::string GetURI() const;
87
89 CService GetPeer() const;
90
93
98 std::pair<bool, std::string> GetHeader(const std::string &hdr) const;
99
106 std::string ReadBody();
107
113 void WriteHeader(const std::string &hdr, const std::string &value);
114
125 void WriteReply(int nStatus, const std::string &strReply = "");
126};
127
130public:
131 virtual void operator()() = 0;
132 virtual ~HTTPClosure() {}
133};
134
140public:
147 HTTPEvent(struct event_base *base, bool deleteWhenTriggered,
148 const std::function<void()> &handler);
149 ~HTTPEvent();
150
155 void trigger(struct timeval *tv);
156
158 std::function<void()> handler;
159
160private:
161 struct event *ev;
162};
163
164#endif // BITCOIN_HTTPSERVER_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:545
Event handler closure.
Definition httpserver.h:129
virtual void operator()()=0
virtual ~HTTPClosure()
Definition httpserver.h:132
Event class.
Definition httpserver.h:139
struct event * ev
Definition httpserver.h:161
bool deleteWhenTriggered
Definition httpserver.h:157
std::function< void()> handler
Definition httpserver.h:158
void trigger(struct timeval *tv)
Trigger the event.
In-flight HTTP request.
Definition httpserver.h:74
bool replySent
Definition httpserver.h:77
std::pair< bool, std::string > GetHeader(const std::string &hdr) const
Get the request header specified by hdr, or an empty string.
std::string GetURI() const
Get requested URI.
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
struct evhttp_request * req
Definition httpserver.h:76
RequestMethod GetRequestMethod() const
Get request method.
std::string ReadBody()
Read request body.
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
void InterruptHTTPServer()
Interrupt HTTP server threads.
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition httpserver.h:14
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
static const int DEFAULT_HTTP_WORKQUEUE
Definition httpserver.h:13
void StartHTTPServer()
Start HTTP server.
struct event_base * EventBase()
Return evhttp event base.
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
static const int DEFAULT_HTTP_THREADS
Definition httpserver.h:12
std::function< bool(Config &config, HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition httpserver.h:51
void StopHTTPServer()
Stop HTTP server.
bool InitHTTPServer(Config &config)
Initialize HTTP server.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * prefix
Definition rest.cpp:817
bool(* handler)(Config &config, const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition rest.cpp:818