Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
HttpBase.hpp
1#pragma once
2
3#include "data/LedgerCacheInterface.hpp"
4#include "rpc/Errors.hpp"
5#include "util/Assert.hpp"
6#include "util/Taggable.hpp"
7#include "util/build/Build.hpp"
8#include "util/log/Logger.hpp"
9#include "util/prometheus/Http.hpp"
10#include "web/AdminVerificationStrategy.hpp"
11#include "web/LoadWarning.hpp"
12#include "web/ProxyIpResolver.hpp"
13#include "web/SubscriptionContextInterface.hpp"
14#include "web/dosguard/DOSGuardInterface.hpp"
15#include "web/interface/Concepts.hpp"
16#include "web/interface/ConnectionBase.hpp"
17
18#include <boost/asio/error.hpp>
19#include <boost/asio/ip/tcp.hpp>
20#include <boost/asio/ssl/error.hpp>
21#include <boost/beast/core.hpp>
22#include <boost/beast/core/error.hpp>
23#include <boost/beast/core/flat_buffer.hpp>
24#include <boost/beast/http.hpp>
25#include <boost/beast/http/error.hpp>
26#include <boost/beast/http/field.hpp>
27#include <boost/beast/http/message.hpp>
28#include <boost/beast/http/status.hpp>
29#include <boost/beast/http/string_body.hpp>
30#include <boost/beast/http/verb.hpp>
31#include <boost/beast/ssl.hpp>
32#include <boost/core/ignore_unused.hpp>
33#include <boost/json.hpp>
34#include <boost/json/array.hpp>
35#include <boost/json/parse.hpp>
36#include <boost/json/serialize.hpp>
37#include <xrpl/protocol/ErrorCodes.h>
38
39#include <chrono>
40#include <cstddef>
41#include <exception>
42#include <functional>
43#include <memory>
44#include <string>
45#include <utility>
46
47namespace web::impl {
48
49static constexpr auto kHealthCheckHtml = R"html(
50 <!DOCTYPE html>
51 <html>
52 <head><title>Test page for Clio</title></head>
53 <body><h1>Clio Test</h1><p>This page shows Clio http(s) connectivity is working.</p></body>
54 </html>
55)html";
56
57static constexpr auto kCacheCheckLoadedHtml = R"html(
58 <!DOCTYPE html>
59 <html>
60 <head><title>Cache state</title></head>
61 <body><h1>Cache state</h1><p>Cache is fully loaded</p></body>
62 </html>
63)html";
64
65static constexpr auto kCacheCheckNotLoadedHtml = R"html(
66 <!DOCTYPE html>
67 <html>
68 <head><title>Cache state</title></head>
69 <body><h1>Cache state</h1><p>Cache is not yet loaded</p></body>
70 </html>
71)html";
72
73using boost::asio::ip::tcp;
74
81template <template <typename> typename Derived, SomeServerHandler HandlerType>
82class HttpBase : public ConnectionBase {
83 Derived<HandlerType>&
84 derived()
85 {
86 return static_cast<Derived<HandlerType>&>(*this);
87 }
88
89 // TODO: this should be rewritten using http::message_generator instead
90 struct SendLambda {
91 HttpBase& self;
92
93 explicit SendLambda(HttpBase& self) : self(self)
94 {
95 }
96
97 template <bool IsRequest, typename Body, typename Fields>
98 void
99 operator()(http::message<IsRequest, Body, Fields>&& msg) const
100 {
101 if (self.dead())
102 return;
103
104 // The lifetime of the message has to extend for the duration of the async operation so
105 // we use a shared_ptr to manage it.
106 auto const sp =
107 std::make_shared<http::message<IsRequest, Body, Fields>>(std::move(msg));
108
109 // Store a type-erased version of the shared pointer in the class to keep it alive.
110 self.res_ = sp;
111
112 // Write the response
113 http::async_write(
114 self.derived().stream(),
115 *sp,
116 boost::beast::bind_front_handler(
117 &HttpBase::onWrite, self.derived().shared_from_this(), sp->need_eof()
118 )
119 );
120 }
121 };
122
123 std::shared_ptr<void> res_;
124 SendLambda sender_;
125 std::shared_ptr<AdminVerificationStrategy> adminVerification_;
126 std::shared_ptr<ProxyIpResolver> proxyIpResolver_;
127 bool isProxyConnection_ = false;
128
129protected:
130 boost::beast::flat_buffer buffer_;
131 http::request<http::string_body> req_;
132 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard_;
133 std::shared_ptr<HandlerType> const handler_;
134 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
135 util::Logger log_{"WebServer"};
136 util::Logger perfLog_{"Performance"};
137
138 void
139 httpFail(boost::beast::error_code ec, char const* what)
140 {
141 // ssl::error::stream_truncated, also known as an SSL "short read",
142 // indicates the peer closed the connection without performing the
143 // required closing handshake (for example, Google does this to
144 // improve performance). Generally this can be a security issue,
145 // but if your communication protocol is self-terminated (as
146 // it is with both HTTP and WebSocket) then you may simply
147 // ignore the lack of close_notify.
148 //
149 // https://github.com/boostorg/beast/issues/38
150 //
151 // https://security.stackexchange.com/questions/91435/how-to-handle-a-malicious-ssl-tls-shutdown
152 //
153 // When a short read would cut off the end of an HTTP message,
154 // Beast returns the error boost::beast::http::error::partial_message.
155 // Therefore, if we see a short read here, it has occurred
156 // after the message has been completed, so it is safe to ignore it.
157
158 if (ec == boost::asio::ssl::error::stream_truncated)
159 return;
160
161 if (!ec_ && ec != boost::asio::error::operation_aborted) {
162 ec_ = ec;
163 LOG(perfLog_.info()) << tag() << ": " << what << ": " << ec.message();
164 boost::beast::get_lowest_layer(derived().stream()).socket().close(ec);
165 }
166 }
167
168public:
169 HttpBase(
170 std::string const& ip,
171 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
172 std::shared_ptr<AdminVerificationStrategy> adminVerification,
173 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
174 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
175 std::shared_ptr<HandlerType> handler,
176 std::reference_wrapper<data::LedgerCacheInterface const> cache,
177 boost::beast::flat_buffer buffer
178 )
179 : ConnectionBase(tagFactory, ip)
180 , sender_(*this)
181 , adminVerification_(std::move(adminVerification))
182 , proxyIpResolver_(std::move(proxyIpResolver))
183 , buffer_(std::move(buffer))
184 , dosGuard_(dosGuard)
185 , handler_(std::move(handler))
186 , cache_(cache)
187 {
188 LOG(perfLog_.debug()) << tag() << "http session created";
189 dosGuard_.get().increment(ip);
190 }
191
192 ~HttpBase() override
193 {
194 LOG(perfLog_.debug()) << tag() << "http session closed";
195 if (not upgraded)
196 dosGuard_.get().decrement(clientIp_);
197 }
198
199 void
200 doRead()
201 {
202 if (dead())
203 return;
204
205 // Make the request empty before reading, otherwise the operation behavior is undefined.
206 req_ = {};
207
208 // Set the timeout.
209 boost::beast::get_lowest_layer(derived().stream()).expires_after(std::chrono::seconds(30));
210
211 http::async_read(
212 derived().stream(),
213 buffer_,
214 req_,
215 boost::beast::bind_front_handler(&HttpBase::onRead, derived().shared_from_this())
216 );
217 }
218
219 void
220 onRead(boost::beast::error_code ec, [[maybe_unused]] std::size_t bytesTransferred)
221 {
222 if (ec == http::error::end_of_stream)
223 return derived().doClose();
224
225 if (ec)
226 return httpFail(ec, "read");
227
228 auto const updateClientIp = [&](std::string newIp) {
229 if (newIp == clientIp_)
230 return;
231 LOG(log_.info()) << tag()
232 << "Detected a forwarded request from proxy. Resolved client ip: "
233 << newIp;
234 dosGuard_.get().decrement(clientIp_);
235 clientIp_ = std::move(newIp);
236 dosGuard_.get().increment(clientIp_);
237 };
238
239 if (isProxyConnection_) {
240 if (auto resolvedIp = ProxyIpResolver::extractClientIp(req_); resolvedIp.has_value())
241 updateClientIp(std::move(*resolvedIp));
242 } else if (
243 auto resolvedIp = proxyIpResolver_->resolveClientIp(clientIp_, req_);
244 resolvedIp.has_value()
245 ) {
246 updateClientIp(std::move(*resolvedIp));
247 isProxyConnection_ = true;
248 }
249
250 if (req_.method() == http::verb::get and req_.target() == "/health")
251 return sender_(httpResponse(http::status::ok, "text/html", kHealthCheckHtml));
252
253 if (req_.method() == http::verb::get and req_.target() == "/cache_state") {
254 if (cache_.get().isFull()) {
255 return sender_(httpResponse(http::status::ok, "text/html", kCacheCheckLoadedHtml));
256 }
257
258 return sender_(httpResponse(
259 http::status::service_unavailable, "text/html", kCacheCheckNotLoadedHtml
260 ));
261 }
262
263 // Update isAdmin property of the connection
264 ConnectionBase::isAdmin_ = adminVerification_->isAdmin(req_, clientIp_);
265
266 if (boost::beast::websocket::is_upgrade(req_)) {
267 if (dosGuard_.get().isOk(clientIp_)) {
268 // Disable the timeout. The websocket::stream uses its own timeout settings.
269 boost::beast::get_lowest_layer(derived().stream()).expires_never();
270
271 upgraded = true;
272 return derived().upgrade();
273 }
274
275 return sender_(
276 httpResponse(http::status::too_many_requests, "text/html", "Too many requests")
277 );
278 }
279
280 if (auto response = util::prometheus::handlePrometheusRequest(req_, isAdmin());
281 response.has_value())
282 return sender_(std::move(response.value()));
283
284 if (req_.method() != http::verb::post) {
285 return sender_(
286 httpResponse(http::status::bad_request, "text/html", "Expected a POST request")
287 );
288 }
289
290 LOG(log_.info()) << tag() << "Received request from ip = " << clientIp_;
291
292 try {
293 (*handler_)(req_.body(), derived().shared_from_this());
294 } catch (std::exception const&) {
295 return sender_(httpResponse(
296 http::status::internal_server_error,
297 "application/json",
298 boost::json::serialize(rpc::makeError(rpc::XrpldError::RpcInternal))
299 ));
300 }
301 }
302
303 void
304 sendSlowDown(std::string const&) override
305 {
306 sender_(httpResponse(
307 http::status::service_unavailable,
308 "text/plain",
309 boost::json::serialize(rpc::makeError(rpc::XrpldError::RpcSlowDown))
310 ));
311 }
312
320 void
321 send(std::string&& msg, http::status status = http::status::ok) override
322 {
323 if (!dosGuard_.get().add(clientIp_, msg.size())) {
324 if (auto const warned = withLoadWarning(msg); warned.has_value()) {
325 // Reserialize when we need to include this warning
326 msg = boost::json::serialize(*warned);
327 }
328 }
329 sender_(httpResponse(status, "application/json", std::move(msg)));
330 }
331
334 {
335 ASSERT(false, "SubscriptionContext can't be created for a HTTP connection");
336 std::unreachable();
337 }
338
339 void
340 onWrite(bool close, boost::beast::error_code ec, std::size_t bytesTransferred)
341 {
342 boost::ignore_unused(bytesTransferred);
343
344 if (ec)
345 return httpFail(ec, "write");
346
347 // This means we should close the connection, usually because
348 // the response indicated the "Connection: close" semantic.
349 if (close)
350 return derived().doClose();
351
352 res_ = nullptr;
353 doRead();
354 }
355
356private:
357 [[nodiscard]] http::response<http::string_body>
358 httpResponse(http::status status, std::string contentType, std::string message) const
359 {
360 http::response<http::string_body> res{status, req_.version()};
361 res.set(http::field::server, "clio-server-" + util::build::getClioVersionString());
362 res.set(http::field::content_type, contentType);
363 res.keep_alive(req_.keep_alive());
364 res.body() = std::move(message);
365 res.prepare_payload();
366 return res;
367 };
368};
369
370} // namespace web::impl
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:78
A factory for TagDecorator instantiation.
Definition Taggable.hpp:165
BaseTagDecorator const & tag() const
Getter for tag decorator.
Definition Taggable.hpp:264
static std::optional< std::string > extractClientIp(HttpHeaders const &headers)
Extracts the client IP from the Forwarded HTTP header.
Definition ProxyIpResolver.cpp:65
void send(std::string &&msg, http::status status=http::status::ok) override
Send the response to the client.
Definition HttpBase.hpp:321
void sendSlowDown(std::string const &) override
Send a "slow down" error response to the client.
Definition HttpBase.hpp:304
SubscriptionContextPtr makeSubscriptionContext(util::TagDecoratorFactory const &) override
Get the subscription context for this connection.
Definition HttpBase.hpp:333
boost::json::object makeError(XrpldError err, std::optional< std::string_view > customError, std::optional< std::string_view > customMessage)
Generate JSON from a rpc::XrpldError.
Definition Errors.cpp:185
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:64
std::optional< boost::json::object > withLoadWarning(std::string_view message)
Parse a serialized response body and attach the DOSGuard "load" warning to it.
Definition LoadWarning.hpp:33
ConnectionBase(util::TagDecoratorFactory const &tagFactory, std::string ip)
Create a new connection base.
Definition ConnectionBase.hpp:40
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:99
bool dead()
Indicates whether the connection had an error and is considered dead.
Definition ConnectionBase.hpp:88