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
17static const int DEFAULT_HTTP_THREADS=4;
18static const int DEFAULT_HTTP_WORKQUEUE=16;
19static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
20
21struct evhttp_request;
22struct event_base;
23class CService;
24class HTTPRequest;
25
29bool InitHTTPServer(const util::SignalInterrupt& interrupt);
34void StartHTTPServer();
38void StopHTTPServer();
39
41void UpdateHTTPServerLogging(bool enable);
42
44typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
49void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
51void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
52
56struct event_base* EventBase();
57
62{
63private:
64 struct evhttp_request* req;
67
68public:
69 explicit HTTPRequest(struct evhttp_request* req, const util::SignalInterrupt& interrupt, bool replySent = false);
71
77 PUT
78 };
79
82 std::string GetURI() const;
83
86 CService GetPeer() const;
87
91
101 std::optional<std::string> GetQueryParameter(const std::string& key) const;
102
107 std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
108
115 std::string ReadBody();
116
122 void WriteHeader(const std::string& hdr, const std::string& value);
123
132 void WriteReply(int nStatus, std::string_view reply = "")
133 {
134 WriteReply(nStatus, std::as_bytes(std::span{reply}));
135 }
136 void WriteReply(int nStatus, std::span<const std::byte> reply);
137};
138
151std::optional<std::string> GetQueryParameterFromUri(const char* uri, const std::string& key);
152
156{
157public:
158 virtual void operator()() = 0;
159 virtual ~HTTPClosure() = default;
160};
161
165{
166public:
171 HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
172 ~HTTPEvent();
173
177 void trigger(struct timeval* tv);
178
180 std::function<void()> handler;
181private:
182 struct event* ev;
183};
184
185#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:156
virtual ~HTTPClosure()=default
virtual void operator()()=0
Event class.
Definition: httpserver.h:165
struct event * ev
Definition: httpserver.h:182
bool deleteWhenTriggered
Definition: httpserver.h:179
std::function< void()> handler
Definition: httpserver.h:180
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:62
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:66
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:132
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:64
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:703
const util::SignalInterrupt & m_interrupt
Definition: httpserver.h:65
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:19
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
Definition: httpserver.h:18
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
Definition: httpserver.h:17
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:44
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