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
44
46typedef std::function<bool(Config &config, HTTPRequest *req,
47 const std::string &)>
49
55void RegisterHTTPHandler(const std::string &prefix, bool exactMatch,
57
59void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
60
65struct event_base *EventBase();
66
72private:
75
76public:
77 explicit HTTPRequest(struct evhttp_request *req, bool replySent = false);
79
81
83 std::string GetURI() const;
84
86 CService GetPeer() const;
87
90
95 std::pair<bool, std::string> GetHeader(const std::string &hdr) const;
96
103 std::string ReadBody();
104
110 void WriteHeader(const std::string &hdr, const std::string &value);
111
122 void WriteReply(int nStatus, const std::string &strReply = "");
123};
124
127public:
128 virtual void operator()() = 0;
129 virtual ~HTTPClosure() {}
130};
131
137public:
144 HTTPEvent(struct event_base *base, bool deleteWhenTriggered,
145 const std::function<void()> &handler);
146 ~HTTPEvent();
147
152 void trigger(struct timeval *tv);
153
155 std::function<void()> handler;
156
157private:
158 struct event *ev;
159};
160
161#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:126
virtual void operator()()=0
virtual ~HTTPClosure()
Definition httpserver.h:129
Event class.
Definition httpserver.h:136
struct event * ev
Definition httpserver.h:158
bool deleteWhenTriggered
Definition httpserver.h:154
std::function< void()> handler
Definition httpserver.h:155
void trigger(struct timeval *tv)
Trigger the event.
In-flight HTTP request.
Definition httpserver.h:71
bool replySent
Definition httpserver.h:74
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:73
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.
void UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
struct event_base * EventBase()
Return evhttp event base.
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:48
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