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