//------------------------------------------------------------------------------ /* This file is part of clio: https://github.com/XRPLF/clio Copyright (c) 2024, the clio developers. Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #pragma once #include "util/Taggable.hpp" #include "util/WithTimeout.hpp" #include "util/build/Build.hpp" #include "web/ng/Connection.hpp" #include "web/ng/Error.hpp" #include "web/ng/Request.hpp" #include "web/ng/Response.hpp" #include "web/ng/impl/Concepts.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace web::ng::impl { class WsConnectionBase : public Connection { public: using Connection::Connection; virtual std::optional sendBuffer( boost::asio::const_buffer buffer, boost::asio::yield_context yield, std::chrono::steady_clock::duration timeout = Connection::DEFAULT_TIMEOUT ) = 0; }; template class WsConnection : public WsConnectionBase { boost::beast::websocket::stream stream_; boost::beast::http::request initialRequest_; public: WsConnection( boost::asio::ip::tcp::socket socket, std::string ip, boost::beast::flat_buffer buffer, boost::beast::http::request initialRequest, util::TagDecoratorFactory const& tagDecoratorFactory ) requires IsTcpStream : WsConnectionBase(std::move(ip), std::move(buffer), tagDecoratorFactory) , stream_(std::move(socket)) , initialRequest_(std::move(initialRequest)) { } WsConnection( boost::asio::ip::tcp::socket socket, std::string ip, boost::beast::flat_buffer buffer, boost::asio::ssl::context& sslContext, boost::beast::http::request initialRequest, util::TagDecoratorFactory const& tagDecoratorFactory ) requires IsSslTcpStream : WsConnectionBase(std::move(ip), std::move(buffer), tagDecoratorFactory) , stream_(std::move(socket), sslContext) , initialRequest_(std::move(initialRequest)) { // Disable the timeout. The websocket::stream uses its own timeout settings. boost::beast::get_lowest_layer(stream_).expires_never(); stream_.set_option(boost::beast::websocket::stream_base::timeout::suggested(boost::beast::role_type::server)); stream_.set_option( boost::beast::websocket::stream_base::decorator([](boost::beast::websocket::response_type& res) { res.set(boost::beast::http::field::server, util::build::getClioFullVersionString()); }) ); } std::optional performHandshake(boost::asio::yield_context yield) { Error error; stream_.async_accept(initialRequest_, yield[error]); if (error) return error; return std::nullopt; } bool wasUpgraded() const override { return true; } std::optional sendBuffer( boost::asio::const_buffer buffer, boost::asio::yield_context yield, std::chrono::steady_clock::duration timeout = Connection::DEFAULT_TIMEOUT ) override { auto error = util::withTimeout([this, buffer](auto&& yield) { stream_.async_write(buffer, yield); }, yield, timeout); if (error) return error; return std::nullopt; } std::optional send( Response response, boost::asio::yield_context yield, std::chrono::steady_clock::duration timeout = DEFAULT_TIMEOUT ) override { return sendBuffer(response.asWsResponse(), yield, timeout); } std::expected receive(boost::asio::yield_context yield, std::chrono::steady_clock::duration timeout = DEFAULT_TIMEOUT) override { auto error = util::withTimeout([this](auto&& yield) { stream_.async_read(buffer_, yield); }, yield, timeout); if (error) return std::unexpected{error}; auto request = boost::beast::buffers_to_string(buffer_.data()); buffer_.consume(buffer_.size()); return Request{std::move(request), initialRequest_}; } void close(boost::asio::yield_context yield, std::chrono::steady_clock::duration timeout = DEFAULT_TIMEOUT) override { boost::beast::websocket::stream_base::timeout wsTimeout{}; stream_.get_option(wsTimeout); wsTimeout.handshake_timeout = timeout; stream_.set_option(wsTimeout); stream_.async_close(boost::beast::websocket::close_code::normal, yield); } }; using PlainWsConnection = WsConnection; using SslWsConnection = WsConnection>; std::expected, Error> make_PlainWsConnection( boost::asio::ip::tcp::socket socket, std::string ip, boost::beast::flat_buffer buffer, boost::beast::http::request request, util::TagDecoratorFactory const& tagDecoratorFactory, boost::asio::yield_context yield ); std::expected, Error> make_SslWsConnection( boost::asio::ip::tcp::socket socket, std::string ip, boost::beast::flat_buffer buffer, boost::beast::http::request request, boost::asio::ssl::context& sslContext, util::TagDecoratorFactory const& tagDecoratorFactory, boost::asio::yield_context yield ); } // namespace web::ng::impl