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 "rpc/Errors.hpp"
23#include "util/log/Logger.hpp"
24#include "web/AdminVerificationStrategy.hpp"
25#include "web/SubscriptionContextInterface.hpp"
26#include "web/dosguard/DOSGuardInterface.hpp"
27#include "web/ng/Connection.hpp"
28#include "web/ng/Request.hpp"
29#include "web/ng/Response.hpp"
30
31#include <boost/asio/spawn.hpp>
32#include <boost/beast/http/status.hpp>
33#include <boost/json/array.hpp>
34#include <boost/json/parse.hpp>
35
36#include <exception>
37#include <functional>
38#include <memory>
39#include <utility>
40
41namespace app {
42
47 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
48
49public:
56
63 std::expected<void, web::ng::Response>
64 operator()(web::ng::Connection const& connection);
65};
66
71 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
72
73public:
80
86 void
87 operator()(web::ng::Connection const& connection);
88};
89
94 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
95
96public:
102 MetricsHandler(std::shared_ptr<web::AdminVerificationStrategy> adminVerifier);
103
113 web::ng::Request const& request,
114 web::ng::ConnectionMetadata& connectionMetadata,
116 boost::asio::yield_context
117 );
118};
119
124public:
133 web::ng::Request const& request,
136 boost::asio::yield_context
137 );
138};
139
145template <typename RpcHandlerType>
147 util::Logger webServerLog_{"WebServer"};
148 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
149 std::reference_wrapper<RpcHandlerType> rpcHandler_;
150
151public:
158 RequestHandler(std::shared_ptr<web::AdminVerificationStrategy> adminVerifier, RpcHandlerType& rpcHandler)
159 : adminVerifier_(std::move(adminVerifier)), rpcHandler_(rpcHandler)
160 {
161 }
162
174 web::ng::Request const& request,
175 web::ng::ConnectionMetadata& connectionMetadata,
176 web::SubscriptionContextPtr subscriptionContext,
177 boost::asio::yield_context yield
178 )
179 {
180 LOG(webServerLog_.info()) << connectionMetadata.tag()
181 << "Received request from ip = " << connectionMetadata.ip()
182 << " - posting to WorkQueue";
183
184 connectionMetadata.setIsAdmin([this, &request, &connectionMetadata]() {
185 return adminVerifier_->isAdmin(request.httpHeaders(), connectionMetadata.ip());
186 });
187
188 try {
189 return rpcHandler_(request, connectionMetadata, std::move(subscriptionContext), yield);
190 } catch (std::exception const&) {
191 return web::ng::Response{
192 boost::beast::http::status::internal_server_error,
193 rpc::makeError(rpc::RippledError::rpcINTERNAL),
194 request
195 };
196 }
197 }
198};
199
200} // namespace app
A function object to be called when a connection is disconnected.
Definition WebHandlers.hpp:70
DisconnectHook(web::dosguard::DOSGuardInterface &dosguard)
Construct a new DisconnectHook object.
Definition WebHandlers.cpp:57
void operator()(web::ng::Connection const &connection)
The call of the function object.
Definition WebHandlers.cpp:62
A function object that handles the health check endpoint.
Definition WebHandlers.hpp:123
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:93
A function object that handles the metrics endpoint.
Definition WebHandlers.hpp:93
MetricsHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier)
Construct a new MetricsHandler object.
Definition WebHandlers.cpp:67
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:73
A function object that checks if the connection is allowed to proceed.
Definition WebHandlers.hpp:46
OnConnectCheck(web::dosguard::DOSGuardInterface &dosguard)
Construct a new OnConnectCheck object.
Definition WebHandlers.cpp:40
std::expected< void, web::ng::Response > operator()(web::ng::Connection const &connection)
Check if the connection is allowed to proceed.
Definition WebHandlers.cpp:45
A function object that handles the websocket endpoint.
Definition WebHandlers.hpp:146
RequestHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier, RpcHandlerType &rpcHandler)
Construct a new RequestHandler object.
Definition WebHandlers.hpp:158
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:173
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:219
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:43
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:90
A class representing a connection to a client.
Definition Connection.hpp:100
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:120
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:86