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 "util/Taggable.hpp"
23#include "web/AdminVerificationStrategy.hpp"
24#include "web/PlainWsSession.hpp"
25#include "web/ProxyIpResolver.hpp"
26#include "web/dosguard/DOSGuardInterface.hpp"
27#include "web/impl/HttpBase.hpp"
28#include "web/interface/Concepts.hpp"
29#include "web/interface/ConnectionBase.hpp"
30
31#include <boost/asio/ip/tcp.hpp>
32#include <boost/beast/core/error.hpp>
33#include <boost/beast/core/flat_buffer.hpp>
34#include <boost/beast/core/tcp_stream.hpp>
35
36#include <cstdint>
37#include <functional>
38#include <memory>
39#include <string>
40#include <utility>
41
42namespace web {
43
44using tcp = boost::asio::ip::tcp;
45
54template <SomeServerHandler HandlerType>
55class HttpSession : public impl::HttpBase<HttpSession, HandlerType>,
56 public std::enable_shared_from_this<HttpSession<HandlerType>> {
57 boost::beast::tcp_stream stream_;
58 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
59 std::uint32_t maxWsSendingQueueSize_;
60
61public:
75 explicit HttpSession(
76 tcp::socket&& socket,
77 std::string const& ip,
78 std::shared_ptr<AdminVerificationStrategy> const& adminVerification,
79 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
80 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
81 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
82 std::shared_ptr<HandlerType> const& handler,
83 boost::beast::flat_buffer buffer,
84 std::uint32_t maxWsSendingQueueSize
85 )
86 : impl::HttpBase<HttpSession, HandlerType>(
87 ip,
88 tagFactory,
89 adminVerification,
90 std::move(proxyIpResolver),
91 dosGuard,
92 handler,
93 std::move(buffer)
94 )
95 , stream_(std::move(socket))
96 , tagFactory_(tagFactory)
97 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
98 {
99 }
100
101 ~HttpSession() override = default;
102
104 boost::beast::tcp_stream&
106 {
107 return stream_;
108 }
109
111 void
113 {
114 boost::asio::dispatch(
115 stream_.get_executor(),
116 boost::beast::bind_front_handler(
117 &impl::HttpBase<HttpSession, HandlerType>::doRead, this->shared_from_this()
118 )
119 );
120 }
121
123 void
125 {
126 boost::beast::error_code ec;
127 stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
128 }
129
131 void
133 {
134 std::make_shared<WsUpgrader<HandlerType>>(
135 std::move(stream_),
136 this->clientIp_,
137 tagFactory_,
138 this->dosGuard_,
139 this->handler_,
140 std::move(this->buffer_),
141 std::move(this->req_),
143 maxWsSendingQueueSize_
144 )
145 ->run();
146 }
147};
148} // namespace web
Represents a HTTP connection established by a client.
Definition HttpSession.hpp:56
void doClose()
Closes the underlying socket.
Definition HttpSession.hpp:124
boost::beast::tcp_stream & stream()
Definition HttpSession.hpp:105
void run()
Starts reading from the stream.
Definition HttpSession.hpp:112
void upgrade()
Upgrade to WebSocket connection.
Definition HttpSession.hpp:132
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, boost::beast::flat_buffer buffer, std::uint32_t maxWsSendingQueueSize)
Create a new session.
Definition HttpSession.hpp:75
This is the implementation class for http sessions.
Definition HttpBase.hpp:83
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