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 <string>
40#include <utility>
41
42namespace app {
43
48 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
49
50public:
57
64 std::expected<void, web::ng::Response>
65 operator()(web::ng::Connection const& connection);
66};
67
73 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
74
75public:
82
89 void
90 operator()(std::string const& oldIp, std::string const& newIp);
91};
92
97 std::reference_wrapper<web::dosguard::DOSGuardInterface> dosguard_;
98
99public:
106
112 void
113 operator()(web::ng::Connection const& connection);
114};
115
120 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
121
122public:
128 MetricsHandler(std::shared_ptr<web::AdminVerificationStrategy> adminVerifier);
129
139 web::ng::Request const& request,
140 web::ng::ConnectionMetadata& connectionMetadata,
142 boost::asio::yield_context
143 );
144};
145
150public:
159 web::ng::Request const& request,
162 boost::asio::yield_context
163 );
164};
165
171template <typename RpcHandlerType>
173 util::Logger webServerLog_{"WebServer"};
174 std::shared_ptr<web::AdminVerificationStrategy> adminVerifier_;
175 std::reference_wrapper<RpcHandlerType> rpcHandler_;
176
177public:
184 RequestHandler(std::shared_ptr<web::AdminVerificationStrategy> adminVerifier, RpcHandlerType& rpcHandler)
185 : adminVerifier_(std::move(adminVerifier)), rpcHandler_(rpcHandler)
186 {
187 }
188
200 web::ng::Request const& request,
201 web::ng::ConnectionMetadata& connectionMetadata,
202 web::SubscriptionContextPtr subscriptionContext,
203 boost::asio::yield_context yield
204 )
205 {
206 LOG(webServerLog_.info()) << connectionMetadata.tag()
207 << "Received request from ip = " << connectionMetadata.ip()
208 << " - posting to WorkQueue";
209
210 connectionMetadata.setIsAdmin([this, &request, &connectionMetadata]() {
211 return adminVerifier_->isAdmin(request.httpHeaders(), connectionMetadata.ip());
212 });
213
214 try {
215 return rpcHandler_(request, connectionMetadata, std::move(subscriptionContext), yield);
216 } catch (std::exception const&) {
217 return web::ng::Response{
218 boost::beast::http::status::internal_server_error,
219 rpc::makeError(rpc::RippledError::rpcINTERNAL),
220 request
221 };
222 }
223 }
224};
225
226} // namespace app
A function object to be called when a connection is disconnected.
Definition WebHandlers.hpp:96
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:149
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
A function object that is called when the IP of a connection changes (usually if proxy detected)....
Definition WebHandlers.hpp:72
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
A function object that handles the metrics endpoint.
Definition WebHandlers.hpp:119
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
A function object that checks if the connection is allowed to proceed.
Definition WebHandlers.hpp:47
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
A function object that handles the websocket endpoint.
Definition WebHandlers.hpp:172
RequestHandler(std::shared_ptr< web::AdminVerificationStrategy > adminVerifier, RpcHandlerType &rpcHandler)
Construct a new RequestHandler object.
Definition WebHandlers.hpp:184
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:199
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:87
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:404
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:120
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:83