Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
WebHandlers.hpp
1#pragma once
2
3#include "data/LedgerCacheInterface.hpp"
4#include "rpc/Errors.hpp"
5#include "rpc/WorkQueue.hpp"
6#include "util/log/Logger.hpp"
7#include "web/AdminVerificationStrategy.hpp"
8#include "web/SubscriptionContextInterface.hpp"
9#include "web/dosguard/DOSGuardInterface.hpp"
10#include "web/ng/Connection.hpp"
11#include "web/ng/Request.hpp"
12#include "web/ng/Response.hpp"
13
14#include <boost/asio/spawn.hpp>
15#include <boost/beast/http/status.hpp>
16#include <boost/json/array.hpp>
17#include <boost/json/parse.hpp>
18
19#include <exception>
20#include <functional>
21#include <memory>
22#include <string>
23#include <utility>
24
25namespace app {
26
31 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
32
33public:
40
47 std::expected<void, web::ng::Response>
48 operator()(web::ng::Connection const& connection);
49};
50
56 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
57
58public:
65
72 void
73 operator()(std::string const& oldIp, std::string const& newIp);
74};
75
80 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
81
82public:
89
95 void
96 operator()(web::ng::Connection const& connection);
97};
98
103 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
104 std::reference_wrapper<rpc::WorkQueue> workQueue_;
105
106public:
115 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier,
116 rpc::WorkQueue& workQueue
117 );
118
129 web::ng::Request const& request,
130 web::ng::ConnectionMetadata& connectionMetadata,
132 boost::asio::yield_context yield
133 );
134};
135
140public:
149 web::ng::Request const& request,
152 boost::asio::yield_context
153 );
154};
155
160 std::reference_wrapper<data::LedgerCacheInterface const> cache_;
161
162public:
168 CacheStateHandler(data::LedgerCacheInterface const& cache) : cache_{cache}
169 {
170 }
171
180 web::ng::Request const& request,
183 boost::asio::yield_context
184 );
185};
186
192template <typename RpcHandlerType>
194 util::Logger webServerLog_{"WebServer"};
195 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
196 std::reference_wrapper<RpcHandlerType> rpcHandler_;
197
198public:
207 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier,
208 RpcHandlerType& rpcHandler
209 )
210 : adminVerifier_(std::move(adminVerifier)), rpcHandler_(rpcHandler)
211 {
212 }
213
225 web::ng::Request const& request,
226 web::ng::ConnectionMetadata& connectionMetadata,
227 web::SubscriptionContextPtr subscriptionContext,
228 boost::asio::yield_context yield
229 )
230 {
231 LOG(webServerLog_.info()) << connectionMetadata.tag()
232 << "Received request from ip = " << connectionMetadata.ip()
233 << " - posting to WorkQueue";
234
235 connectionMetadata.setIsAdmin([this, &request, &connectionMetadata]() {
236 return adminVerifier_->isAdmin(request.httpHeaders(), connectionMetadata.ip());
237 });
238
239 try {
240 return rpcHandler_(request, connectionMetadata, std::move(subscriptionContext), yield);
241 } catch (std::exception const&) {
242 return web::ng::Response{
243 boost::beast::http::status::internal_server_error,
244 rpc::makeError(rpc::RippledError::rpcINTERNAL),
245 request
246 };
247 }
248 }
249};
250
251} // 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:134
CacheStateHandler(data::LedgerCacheInterface const &cache)
Construct a new CacheStateHandler object.
Definition WebHandlers.hpp:168
DisconnectHook(web::dosguard::DOSGuardInterface &dosguard)
Construct a new DisconnectHook object.
Definition WebHandlers.cpp:54
void operator()(web::ng::Connection const &connection)
The call of the function object.
Definition WebHandlers.cpp:59
A function object that handles the health check endpoint.
Definition WebHandlers.hpp:139
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:121
IpChangeHook(web::dosguard::DOSGuardInterface &dosguard)
Construct a new IpChangeHook object.
Definition WebHandlers.cpp:43
void operator()(std::string const &oldIp, std::string const &newIp)
The call of the function object.
Definition WebHandlers.cpp:48
MetricsHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier, rpc::WorkQueue &workQueue)
Construct a new MetricsHandler object.
Definition WebHandlers.cpp:64
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:73
OnConnectCheck(web::dosguard::DOSGuardInterface &dosguard)
Construct a new OnConnectCheck object.
Definition WebHandlers.cpp:26
std::expected< void, web::ng::Response > operator()(web::ng::Connection const &connection)
Check if the connection is allowed to proceed.
Definition WebHandlers.cpp:31
RequestHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier, RpcHandlerType &rpcHandler)
Construct a new RequestHandler object.
Definition WebHandlers.hpp:206
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:224
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:21
An asynchronous, thread-safe queue for RPC requests.
Definition WorkQueue.hpp:43
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:77
BaseTagDecorator const & tag() const
Getter for tag decorator.
Definition Taggable.hpp:264
The interface of a denial of service guard.
Definition DOSGuardInterface.hpp:27
An interface for a connection metadata class.
Definition Connection.hpp:25
std::string const & ip() const
Get the ip of the client.
Definition Connection.cpp:21
void setIsAdmin(T &&setter)
Set the isAdmin field.
Definition Connection.hpp:83
A class representing a connection to a client.
Definition Connection.hpp:93
Represents an HTTP or WebSocket request.
Definition Request.hpp:18
HttpHeaders const & httpHeaders() const
Get the headers of the request.
Definition Request.cpp:93
Represents an HTTP or Websocket response.
Definition Response.hpp:21
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:160
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:64