Bitcoin ABC 0.30.8
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 <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
43void UpdateHTTPServerLogging(bool enable);
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:
73 struct evhttp_request *req;
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
Definition: config.h:19
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
HTTPEvent(struct event_base *base, bool deleteWhenTriggered, const std::function< void()> &handler)
Create a new event.
Definition: httpserver.cpp:630
void trigger(struct timeval *tv)
Trigger the event.
Definition: httpserver.cpp:639
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.
Definition: httpserver.cpp:660
std::string GetURI() const
Get requested URI.
Definition: httpserver.cpp:751
void WriteReply(int nStatus, const std::string &strReply="")
Write HTTP reply.
Definition: httpserver.cpp:707
void WriteHeader(const std::string &hdr, const std::string &value)
Write output header.
Definition: httpserver.cpp:695
struct evhttp_request * req
Definition: httpserver.h:73
RequestMethod GetRequestMethod() const
Get request method.
Definition: httpserver.cpp:755
std::string ReadBody()
Read request body.
Definition: httpserver.cpp:671
CService GetPeer() const
Get CService (address:ip) for the origin of the http request.
Definition: httpserver.cpp:738
HTTPRequest(struct evhttp_request *req, bool replySent=false)
Definition: httpserver.cpp:648
void InterruptHTTPServer()
Interrupt HTTP server threads.
Definition: httpserver.cpp:557
static const int DEFAULT_HTTP_SERVER_TIMEOUT
Definition: httpserver.h:14
void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
Unregister handler for prefix.
Definition: httpserver.cpp:779
void RegisterHTTPHandler(const std::string &prefix, bool exactMatch, const HTTPRequestHandler &handler)
Register handler for prefix.
Definition: httpserver.cpp:772
static const int DEFAULT_HTTP_WORKQUEUE
Definition: httpserver.h:13
void StartHTTPServer()
Start HTTP server.
Definition: httpserver.cpp:545
void UpdateHTTPServerLogging(bool enable)
Change logging level for libevent.
Definition: httpserver.cpp:534
struct event_base * EventBase()
Return evhttp event base.
Definition: httpserver.cpp:617
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.
Definition: httpserver.cpp:568
bool InitHTTPServer(Config &config)
Initialize HTTP server.
Definition: httpserver.cpp:477
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