Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
WebHandlers.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/LedgerCacheInterface.hpp"
23#include "rpc/Errors.hpp"
24#include "util/log/Logger.hpp"
25#include "web/AdminVerificationStrategy.hpp"
26#include "web/SubscriptionContextInterface.hpp"
27#include "web/dosguard/DOSGuardInterface.hpp"
28#include "web/ng/Connection.hpp"
29#include "web/ng/Request.hpp"
30#include "web/ng/Response.hpp"
31
32#include <boost/asio/spawn.hpp>
33#include <boost/beast/http/status.hpp>
34#include <boost/json/array.hpp>
35#include <boost/json/parse.hpp>
36
37#include <exception>
38#include <functional>
39#include <memory>
40#include <string>
41#include <utility>
42
43namespace app {
44
49 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
50
51public:
58
65 std::expected<void, web::ng::Response>
66 operator()(web::ng::Connection const& connection);
67};
68
74 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
75
76public:
83
90 void
91 operator()(std::string const& oldIp, std::string const& newIp);
92};
93
98 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
99
100public:
107
113 void
114 operator()(web::ng::Connection const& connection);
115};
116
121 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
122
123public:
129 MetricsHandler(std::shared_ptr<web::AdminVerificationStrategy> adminVerifier);
130
140 web::ng::Request const& request,
141 web::ng::ConnectionMetadata& connectionMetadata,
143 boost::asio::yield_context
144 );
145};
146
151public:
160 web::ng::Request const& request,
163 boost::asio::yield_context
164 );
165};
166
171 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
172
173public:
179 CacheStateHandler(data::LedgerCacheInterface const& cache) : cache_{cache}
180 {
181 }
182
191 web::ng::Request const& request,
194 boost::asio::yield_context
195 );
196};
197
203template <typename RpcHandlerType>
205 util::Logger webServerLog_{"WebServer"};
206 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
207 std::reference_wrapper<RpcHandlerType> rpcHandler_;
208
209public:
216 RequestHandler(std::shared_ptr<web::AdminVerificationStrategy> adminVerifier, RpcHandlerType& rpcHandler)
217 : adminVerifier_(std::move(adminVerifier)), rpcHandler_(rpcHandler)
218 {
219 }
220
232 web::ng::Request const& request,
233 web::ng::ConnectionMetadata& connectionMetadata,
234 web::SubscriptionContextPtr subscriptionContext,
235 boost::asio::yield_context yield
236 )
237 {
238 LOG(webServerLog_.info()) << connectionMetadata.tag()
239 << "Received request from ip = " << connectionMetadata.ip()
240 << " - posting to WorkQueue";
241
242 connectionMetadata.setIsAdmin([this, &request, &connectionMetadata]() {
243 return adminVerifier_->isAdmin(request.httpHeaders(), connectionMetadata.ip());
244 });
245
246 try {
247 return rpcHandler_(request, connectionMetadata, std::move(subscriptionContext), yield);
248 } catch (std::exception const&) {
249 return web::ng::Response{
250 boost::beast::http::status::internal_server_error,
251 rpc::makeError(rpc::RippledError::rpcINTERNAL),
252 request
253 };
254 }
255 }
256};
257
258} // namespace app
web::ng::Response operator()(web::ng::Request const &request, web::ng::ConnectionMetadata &, web::SubscriptionContextPtr, boost::asio::yield_context)
The call of the function object.
Definition WebHandlers.cpp:118
CacheStateHandler(data::LedgerCacheInterface const &cache)
Construct a new CacheStateHandler object.
Definition WebHandlers.hpp:179
DisconnectHook(web::dosguard::DOSGuardInterface &dosguard)
Construct a new DisconnectHook object.
Definition WebHandlers.cpp:69
void operator()(web::ng::Connection const &connection)
The call of the function object.
Definition WebHandlers.cpp:74
A function object that handles the health check endpoint.
Definition WebHandlers.hpp:150
web::ng::Response operator()(web::ng::Request const &request, web::ng::ConnectionMetadata &, web::SubscriptionContextPtr, boost::asio::yield_context)
The call of the function object.
Definition WebHandlers.cpp:105
IpChangeHook(web::dosguard::DOSGuardInterface &dosguard)
Construct a new IpChangeHook object.
Definition WebHandlers.cpp:58
void operator()(std::string const &oldIp, std::string const &newIp)
The call of the function object.
Definition WebHandlers.cpp:63
MetricsHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier)
Construct a new MetricsHandler object.
Definition WebHandlers.cpp:79
web::ng::Response operator()(web::ng::Request const &request, web::ng::ConnectionMetadata &connectionMetadata, web::SubscriptionContextPtr, boost::asio::yield_context)
The call of the function object.
Definition WebHandlers.cpp:85
OnConnectCheck(web::dosguard::DOSGuardInterface &dosguard)
Construct a new OnConnectCheck object.
Definition WebHandlers.cpp:41
std::expected< void, web::ng::Response > operator()(web::ng::Connection const &connection)
Check if the connection is allowed to proceed.
Definition WebHandlers.cpp:46
RequestHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier, RpcHandlerType &rpcHandler)
Construct a new RequestHandler object.
Definition WebHandlers.hpp:216
web::ng::Response operator()(web::ng::Request const &request, web::ng::ConnectionMetadata &connectionMetadata, web::SubscriptionContextPtr subscriptionContext, boost::asio::yield_context yield)
The call of the function object.
Definition WebHandlers.hpp:231
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:38
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:94
BaseTagDecorator const & tag() const
Getter for tag decorator.
Definition Taggable.hpp:280
The interface of a denial of service guard.
Definition DOSGuardInterface.hpp:46
An interface for a connection metadata class.
Definition Connection.hpp:44
std::string const & ip() const
Get the ip of the client.
Definition Connection.cpp:37
void setIsAdmin(T &&setter)
Set the isAdmin field.
Definition Connection.hpp:102
A class representing a connection to a client.
Definition Connection.hpp:112
Represents an HTTP or WebSocket request.
Definition Request.hpp:37
HttpHeaders const & httpHeaders() const
Get the headers of the request.
Definition Request.cpp:110
Represents an HTTP or Websocket response.
Definition Response.hpp:40
boost::json::object makeError(RippledError err, std::optional< std::string_view > customError, std::optional< std::string_view > customMessage)
Generate JSON from a rpc::RippledError.
Definition Errors.cpp:157
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:83