Bitcoin ABC 0.34.2
P2P Digital Currency
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 <optional>
11#include <string>
12
13static const int DEFAULT_HTTP_THREADS = 4;
14static const int DEFAULT_HTTP_WORKQUEUE = 16;
15static const int DEFAULT_HTTP_SERVER_TIMEOUT = 30;
16
17struct evhttp_request;
18struct event_base;
19
20class Config;
21class CService;
22class HTTPRequest;
23
28bool InitHTTPServer(Config &config);
29
35void StartHTTPServer();
36
39
41void StopHTTPServer();
42
44void UpdateHTTPServerLogging(bool enable);
45
47typedef std::function<bool(Config &config, HTTPRequest *req,
48 const std::string &)>
50
56void RegisterHTTPHandler(const std::string &prefix, bool exactMatch,
58
60void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
61
66struct event_base *EventBase();
67
73private:
74 struct evhttp_request *req;
76
77public:
78 explicit HTTPRequest(struct evhttp_request *req, bool replySent = false);
80
82
84 std::string GetURI() const;
85
87 CService GetPeer() const;
88
91
104 std::optional<std::string> GetQueryParameter(const std::string &key) const;
105
110 std::pair<bool, std::string> GetHeader(const std::string &hdr) const;
111
118 std::string ReadBody();
119
125 void WriteHeader(const std::string &hdr, const std::string &value);
126
137 void WriteReply(int nStatus, const std::string &strReply = "");
138};
139
154std::optional<std::string> GetQueryParameterFromUri(const char *uri,
155 const std::string &key);
156
159public:
160 virtual void operator()() = 0;
161 virtual ~HTTPClosure() = default;
162};
163
169public:
176 HTTPEvent(struct event_base *base, bool deleteWhenTriggered,
177 const std::function<void()> &handler);
178 ~HTTPEvent();
179
184 void trigger(struct timeval *tv);
185
187 std::function<void()> handler;
188
189private:
190 struct event *ev;
191};
192
193#endif // BITCOIN_HTTPSERVER_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:573
Definition: config.h:19
Event handler closure.
Definition: httpserver.h:158
virtual ~HTTPClosure()=default
virtual void operator()()=0
Event class.
Definition: httpserver.h:168
struct event * ev
Definition: httpserver.h:190
bool deleteWhenTriggered
Definition: httpserver.h:186
std::function< void()> handler
Definition: httpserver.h:187
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:620
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:629
In-flight HTTP request.
Definition: httpserver.h:72
std::optional< std::string > GetQueryParameter(const std::string &key) const
Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not...
Definition: httpserver.cpp:763
bool replySent
Definition: httpserver.h:75
std::pair< bool, std::string > GetHeader(const std::string &hdr) const
Get the request header specified by hdr, or an empty string.
Definition: httpserver.cpp:650
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:741
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:697
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:685
struct evhttp_request * req
Definition: httpserver.h:74
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:745
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:661
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:728
HTTPRequest(struct evhttp_request *req, bool replySent=false)
Definition: httpserver.cpp:638
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:548
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:15
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:804
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:797
std::optional< std::string > GetQueryParameterFromUri(const char *uri, const std::string &key)
Get the query parameter value from request uri for a specified key, or std::nullopt if the key is not...
Definition: httpserver.cpp:769
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:14
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:536
void UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:525
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:607
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:13
std::function< bool(Config &config, HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:49
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:559
bool InitHTTPServer(Config &config)
Initialize HTTP server.
Definition: httpserver.cpp:469
const char * prefix
Definition: rest.cpp:1078
bool(* handler)(Config &config, const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1079