Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
HttpSession.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, 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 "util/Taggable.hpp"
24#include "web/AdminVerificationStrategy.hpp"
25#include "web/PlainWsSession.hpp"
26#include "web/ProxyIpResolver.hpp"
27#include "web/dosguard/DOSGuardInterface.hpp"
28#include "web/impl/HttpBase.hpp"
29#include "web/interface/Concepts.hpp"
30#include "web/interface/ConnectionBase.hpp"
31
32#include <boost/asio/ip/tcp.hpp>
33#include <boost/beast/core/error.hpp>
34#include <boost/beast/core/flat_buffer.hpp>
35#include <boost/beast/core/tcp_stream.hpp>
36
37#include <cstdint>
38#include <functional>
39#include <memory>
40#include <string>
41#include <utility>
42
43namespace web {
44
45using tcp = boost::asio::ip::tcp;
46
55template <SomeServerHandler HandlerType>
56class HttpSession : public impl::HttpBase<HttpSession, HandlerType>,
57 public std::enable_shared_from_this<HttpSession<HandlerType>> {
58 boost::beast::tcp_stream stream_;
59 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
60 std::uint32_t maxWsSendingQueueSize_;
61
62public:
77 explicit HttpSession(
78 tcp::socket&& socket,
79 std::string const& ip,
80 std::shared_ptr<AdminVerificationStrategy> const& adminVerification,
81 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
82 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
83 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
84 std::shared_ptr<HandlerType> const& handler,
85 std::reference_wrapper<data::LedgerCacheInterface const> cache,
86 boost::beast::flat_buffer buffer,
87 std::uint32_t maxWsSendingQueueSize
88 )
89 : impl::HttpBase<HttpSession, HandlerType>(
90 ip,
91 tagFactory,
92 adminVerification,
93 std::move(proxyIpResolver),
94 dosGuard,
95 handler,
96 cache,
97 std::move(buffer)
98 )
99 , stream_(std::move(socket))
100 , tagFactory_(tagFactory)
101 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
102 {
103 }
104
105 ~HttpSession() override = default;
106
108 boost::beast::tcp_stream&
110 {
111 return stream_;
112 }
113
115 void
117 {
118 boost::asio::dispatch(
119 stream_.get_executor(),
120 boost::beast::bind_front_handler(
121 &impl::HttpBase<HttpSession, HandlerType>::doRead, this->shared_from_this()
122 )
123 );
124 }
125
127 void
129 {
130 boost::beast::error_code ec;
131 stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
132 }
133
135 void
137 {
138 std::make_shared<WsUpgrader<HandlerType>>(
139 std::move(stream_),
140 this->clientIp_,
141 tagFactory_,
142 this->dosGuard_,
143 this->handler_,
144 std::move(this->buffer_),
145 std::move(this->req_),
147 maxWsSendingQueueSize_
148 )
149 ->run();
150 }
151};
152} // namespace web
Represents a HTTP connection established by a client.
Definition HttpSession.hpp:57
void doClose()
Closes the underlying socket.
Definition HttpSession.hpp:128
boost::beast::tcp_stream & stream()
Definition HttpSession.hpp:109
void run()
Starts reading from the stream.
Definition HttpSession.hpp:116
void upgrade()
Upgrade to WebSocket connection.
Definition HttpSession.hpp:136
HttpSession(tcp::socket &&socket, std::string const &ip, std::shared_ptr< AdminVerificationStrategy > const &adminVerification, std::shared_ptr< ProxyIpResolver > proxyIpResolver, std::reference_wrapper< util::TagDecoratorFactory const > tagFactory, std::reference_wrapper< dosguard::DOSGuardInterface > dosGuard, std::shared_ptr< HandlerType > const &handler, std::reference_wrapper< data::LedgerCacheInterface const > cache, boost::beast::flat_buffer buffer, std::uint32_t maxWsSendingQueueSize)
Create a new session.
Definition HttpSession.hpp:77
This is the implementation class for http sessions.
Definition HttpBase.hpp:100
This namespace implements the web server and related components.
Definition Types.hpp:43
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:118