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 "rpc/WorkQueue.hpp"
25#include "util/log/Logger.hpp"
26#include "web/AdminVerificationStrategy.hpp"
27#include "web/SubscriptionContextInterface.hpp"
28#include "web/dosguard/DOSGuardInterface.hpp"
29#include "web/ng/Connection.hpp"
30#include "web/ng/Request.hpp"
31#include "web/ng/Response.hpp"
32
33#include <boost/asio/spawn.hpp>
34#include <boost/beast/http/status.hpp>
35#include <boost/json/array.hpp>
36#include <boost/json/parse.hpp>
37
38#include <exception>
39#include <functional>
40#include <memory>
41#include <string>
42#include <utility>
43
44namespace app {
45
50 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
51
52public:
59
66 std::expected<void, web::ng::Response>
67 operator()(web::ng::Connection const& connection);
68};
69
75 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
76
77public:
84
91 void
92 operator()(std::string const& oldIp, std::string const& newIp);
93};
94
99 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
100
101public:
108
114 void
115 operator()(web::ng::Connection const& connection);
116};
117
122 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
123 std::reference_wrapper<rpc::WorkQueue> workQueue_;
124
125public:
132 MetricsHandler(std::shared_ptr<web::AdminVerificationStrategy> adminVerifier, rpc::WorkQueue& workQueue);
133
144 web::ng::Request const& request,
145 web::ng::ConnectionMetadata& connectionMetadata,
147 boost::asio::yield_context yield
148 );
149};
150
155public:
164 web::ng::Request const& request,
167 boost::asio::yield_context
168 );
169};
170
175 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
176
177public:
183 CacheStateHandler(data::LedgerCacheInterface const& cache) : cache_{cache}
184 {
185 }
186
195 web::ng::Request const& request,
198 boost::asio::yield_context
199 );
200};
201
207template <typename RpcHandlerType>
209 util::Logger webServerLog_{"WebServer"};
210 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
211 std::reference_wrapper<RpcHandlerType> rpcHandler_;
212
213public:
220 RequestHandler(std::shared_ptr<web::AdminVerificationStrategy> adminVerifier, RpcHandlerType& rpcHandler)
221 : adminVerifier_(std::move(adminVerifier)), rpcHandler_(rpcHandler)
222 {
223 }
224
236 web::ng::Request const& request,
237 web::ng::ConnectionMetadata& connectionMetadata,
238 web::SubscriptionContextPtr subscriptionContext,
239 boost::asio::yield_context yield
240 )
241 {
242 LOG(webServerLog_.info()) << connectionMetadata.tag()
243 << "Received request from ip = " << connectionMetadata.ip()
244 << " - posting to WorkQueue";
245
246 connectionMetadata.setIsAdmin([this, &request, &connectionMetadata]() {
247 return adminVerifier_->isAdmin(request.httpHeaders(), connectionMetadata.ip());
248 });
249
250 try {
251 return rpcHandler_(request, connectionMetadata, std::move(subscriptionContext), yield);
252 } catch (std::exception const&) {
253 return web::ng::Response{
254 boost::beast::http::status::internal_server_error,
255 rpc::makeError(rpc::RippledError::rpcINTERNAL),
256 request
257 };
258 }
259 }
260};
261
262} // 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:148
CacheStateHandler(data::LedgerCacheInterface const &cache)
Construct a new CacheStateHandler object.
Definition WebHandlers.hpp:183
DisconnectHook(web::dosguard::DOSGuardInterface &dosguard)
Construct a new DisconnectHook object.
Definition WebHandlers.cpp:73
void operator()(web::ng::Connection const &connection)
The call of the function object.
Definition WebHandlers.cpp:78
A function object that handles the health check endpoint.
Definition WebHandlers.hpp:154
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:135
IpChangeHook(web::dosguard::DOSGuardInterface &dosguard)
Construct a new IpChangeHook object.
Definition WebHandlers.cpp:62
void operator()(std::string const &oldIp, std::string const &newIp)
The call of the function object.
Definition WebHandlers.cpp:67
MetricsHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier, rpc::WorkQueue &workQueue)
Construct a new MetricsHandler object.
Definition WebHandlers.cpp:83
web::ng::Response operator()(web::ng::Request const &request, web::ng::ConnectionMetadata &connectionMetadata, web::SubscriptionContextPtr, boost::asio::yield_context yield)
The call of the function object.
Definition WebHandlers.cpp:89
OnConnectCheck(web::dosguard::DOSGuardInterface &dosguard)
Construct a new OnConnectCheck object.
Definition WebHandlers.cpp:45
std::expected< void, web::ng::Response > operator()(web::ng::Connection const &connection)
Check if the connection is allowed to proceed.
Definition WebHandlers.cpp:50
RequestHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier, RpcHandlerType &rpcHandler)
Construct a new RequestHandler object.
Definition WebHandlers.hpp:220
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:235
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:40
An asynchronous, thread-safe queue for RPC requests.
Definition WorkQueue.hpp:63
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:95
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