Bitcoin Core 28.99.0
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1// Copyright (c) 2015-2022 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 <functional>
9#include <optional>
10#include <span>
11#include <string>
12
13namespace util {
14class SignalInterrupt;
15} // namespace util
16
20static const int DEFAULT_HTTP_THREADS=16;
21
26static const int DEFAULT_HTTP_WORKQUEUE=64;
27
28static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
29
30struct evhttp_request;
31struct event_base;
32class CService;
33class HTTPRequest;
34
38bool InitHTTPServer(const util::SignalInterrupt& interrupt);
43void StartHTTPServer();
47void StopHTTPServer();
48
50void UpdateHTTPServerLogging(bool enable);
51
53typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
58void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
60void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
61
65struct event_base* EventBase();
66
71{
72private:
73 struct evhttp_request* req;
76
77public:
78 explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false);
80
86 PUT
87 };
88
91 std::string GetURI() const;
92
95 CService GetPeer() const;
96
100
110 std::optional<std::string> GetQueryParameter(const std::string& key) const;
111
116 std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
117
124 std::string ReadBody();
125
131 void WriteHeader(const std::string& hdr, const std::string& value);
132
141 void WriteReply(int nStatus, std::string_view reply = "")
142 {
143 WriteReply(nStatus, std::as_bytes(std::span{reply}));
144 }
145 void WriteReply(int nStatus, std::span<const std::byte> reply);
146};
147
160std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key);
161
165{
166public:
167 virtual void operator()() = 0;
168 virtual ~HTTPClosure() = default;
169};
170
174{
175public:
180 HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
181 ~HTTPEvent();
182
186 void trigger(struct timeval* tv);
187
189 std::function<void()> handler;
190private:
191 struct event* ev;
192};
193
194#endif // BITCOIN_HTTPSERVER_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
Event handler closure.
Definition: httpserver.h:165
virtual ~HTTPClosure()=default
virtual void operator()()=0
Event class.
Definition: httpserver.h:174
struct event * ev
Definition: httpserver.h:191
bool deleteWhenTriggered
Definition: httpserver.h:188
std::function< void()> handler
Definition: httpserver.h:189
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:573
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:583
In-flight HTTP request.
Definition: httpserver.h:71
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:719
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:605
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:698
void WriteReply(int nStatus, std::string_view reply="")
Write HTTP reply.
Definition: httpserver.h:141
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:636
HTTPRequest(struct evhttp_request *req, const util::SignalInterrupt &interrupt, bool replySent=false)
Definition: httpserver.cpp:590
struct evhttp_request * req
Definition: httpserver.h:73
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:703
const util::SignalInterrupt & m_interrupt
Definition: httpserver.h:74
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:616
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:678
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:506
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:28
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:760
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:753
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:726
static const int DEFAULT_HTTP_WORKQUEUE
The default value for -rpcworkqueue.
Definition: httpserver.h:26
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:495
void UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:484
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:559
static const int DEFAULT_HTTP_THREADS
The default value for -rpcthreads.
Definition: httpserver.h:20
bool InitHTTPServer(const util::SignalInterrupt &interrupt)
Initialize HTTP server.
Definition: httpserver.cpp:437
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:53
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:518
const char * prefix
Definition: rest.cpp:1009
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1010