Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
HttpSession.hpp
1#pragma once
2
3#include "data/LedgerCacheInterface.hpp"
4#include "util/Taggable.hpp"
5#include "web/AdminVerificationStrategy.hpp"
6#include "web/PlainWsSession.hpp"
7#include "web/ProxyIpResolver.hpp"
8#include "web/dosguard/DOSGuardInterface.hpp"
9#include "web/impl/HttpBase.hpp"
10#include "web/interface/Concepts.hpp"
11#include "web/interface/ConnectionBase.hpp"
12
13#include <boost/asio/ip/tcp.hpp>
14#include <boost/beast/core/error.hpp>
15#include <boost/beast/core/flat_buffer.hpp>
16#include <boost/beast/core/tcp_stream.hpp>
17
18#include <cstdint>
19#include <functional>
20#include <memory>
21#include <string>
22#include <utility>
23
24namespace web {
25
26using tcp = boost::asio::ip::tcp;
27
36template <SomeServerHandler HandlerType>
37class HttpSession : public impl::HttpBase<HttpSession, HandlerType>,
38 public std::enable_shared_from_this<HttpSession<HandlerType>> {
39 boost::beast::tcp_stream stream_;
40 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory_;
41 std::uint32_t maxWsSendingQueueSize_;
42
43public:
58 explicit HttpSession(
59 tcp::socket&& socket,
60 std::string const& ip,
61 std::shared_ptr<AdminVerificationStrategy> const& adminVerification,
62 std::shared_ptr<ProxyIpResolver> proxyIpResolver,
63 std::reference_wrapper<util::TagDecoratorFactory const> tagFactory,
64 std::reference_wrapper<dosguard::DOSGuardInterface> dosGuard,
65 std::shared_ptr<HandlerType> const& handler,
66 std::reference_wrapper<data::LedgerCacheInterface const> cache,
67 boost::beast::flat_buffer buffer,
68 std::uint32_t maxWsSendingQueueSize
69 )
70 : impl::HttpBase<HttpSession, HandlerType>(
71 ip,
72 tagFactory,
73 adminVerification,
74 std::move(proxyIpResolver),
75 dosGuard,
76 handler,
77 cache,
78 std::move(buffer)
79 )
80 , stream_(std::move(socket))
81 , tagFactory_(tagFactory)
82 , maxWsSendingQueueSize_(maxWsSendingQueueSize)
83 {
84 }
85
86 ~HttpSession() override = default;
87
89 boost::beast::tcp_stream&
91 {
92 return stream_;
93 }
94
96 void
98 {
99 boost::asio::dispatch(
100 stream_.get_executor(),
101 boost::beast::bind_front_handler(
102 &impl::HttpBase<HttpSession, HandlerType>::doRead, this->shared_from_this()
103 )
104 );
105 }
106
108 void
110 {
111 boost::beast::error_code ec;
112 stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
113 }
114
116 void
118 {
119 std::make_shared<WsUpgrader<HandlerType>>(
120 std::move(stream_),
121 this->clientIp_,
122 tagFactory_,
123 this->dosGuard_,
124 this->handler_,
125 std::move(this->buffer_),
126 std::move(this->req_),
128 maxWsSendingQueueSize_
129 )
130 ->run();
131 }
132};
133} // namespace web
Represents a HTTP connection established by a client.
Definition HttpSession.hpp:38
void doClose()
Closes the underlying socket.
Definition HttpSession.hpp:109
boost::beast::tcp_stream & stream()
Definition HttpSession.hpp:90
void run()
Starts reading from the stream.
Definition HttpSession.hpp:97
void upgrade()
Upgrade to WebSocket connection.
Definition HttpSession.hpp:117
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:58
This is the implementation class for http sessions.
Definition HttpBase.hpp:81
This namespace implements the web server and related components.
Definition Types.hpp:24
bool isAdmin() const
Indicates whether the connection has admin privileges.
Definition ConnectionBase.hpp:99