Bitcoin Core 22.99.0
P2P Digital Currency
httpserver.h
Go to the documentation of this file.
1// Copyright (c) 2015-2020 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 <string>
9#include <functional>
10
11static const int DEFAULT_HTTP_THREADS=4;
12static const int DEFAULT_HTTP_WORKQUEUE=16;
13static const int DEFAULT_HTTP_SERVER_TIMEOUT=30;
14
15struct evhttp_request;
16struct event_base;
17class CService;
18class HTTPRequest;
19
23bool InitHTTPServer();
28void StartHTTPServer();
32void StopHTTPServer();
33
36bool UpdateHTTPServerLogging(bool enable);
37
39typedef std::function<bool(HTTPRequest* req, const std::string &)> HTTPRequestHandler;
44void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler);
46void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch);
47
51struct event_base* EventBase();
52
57{
58private:
59 struct evhttp_request* req;
61
62public:
63 explicit HTTPRequest(struct evhttp_request* req, bool replySent = false);
65
71 PUT
72 };
73
76 std::string GetURI() const;
77
80 CService GetPeer() const;
81
85
90 std::pair<bool, std::string> GetHeader(const std::string& hdr) const;
91
98 std::string ReadBody();
99
105 void WriteHeader(const std::string& hdr, const std::string& value);
106
115 void WriteReply(int nStatus, const std::string& strReply = "");
116};
117
121{
122public:
123 virtual void operator()() = 0;
124 virtual ~HTTPClosure() {}
125};
126
130{
131public:
136 HTTPEvent(struct event_base* base, bool deleteWhenTriggered, const std::function<void()>& handler);
137 ~HTTPEvent();
138
142 void trigger(struct timeval* tv);
143
145 std::function<void()> handler;
146private:
147 struct event* ev;
148};
149
150#endif // BITCOIN_HTTPSERVER_H
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
Event handler closure.
Definition: httpserver.h:121
virtual void operator()()=0
virtual ~HTTPClosure()
Definition: httpserver.h:124
Event class.
Definition: httpserver.h:130
struct event * ev
Definition: httpserver.h:147
bool deleteWhenTriggered
Definition: httpserver.h:144
std::function< void()> handler
Definition: httpserver.h:145
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:488
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:498
In-flight HTTP request.
Definition: httpserver.h:57
bool replySent
Definition: httpserver.h:60
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:519
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:606
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:562
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:550
struct evhttp_request * req
Definition: httpserver.h:59
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:611
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:530
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:592
HTTPRequest(struct evhttp_request *req, bool replySent=false)
Definition: httpserver.cpp:505
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:430
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:13
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:638
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:632
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:12
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:418
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:474
bool InitHTTPServer()
Initialize HTTP server.
Definition: httpserver.cpp:350
bool UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:401
static const int DEFAULT_HTTP_THREADS
Definition: httpserver.h:11
std::function< bool(HTTPRequest *req, const std::string &)> HTTPRequestHandler
Handler for requests to a certain HTTP path.
Definition: httpserver.h:39
void StopHTTPServer()
Stop HTTP server.
Definition: httpserver.cpp:442
const char * prefix
Definition: rest.cpp:714
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:715