From b05ec15bd379b59a14f31f2bc8ef2738ebcb3f61 Mon Sep 17 00:00:00 2001 From: CJ Cobb Date: Thu, 17 Jun 2021 19:26:53 +0000 Subject: [PATCH] upgrade http to websocket. didn't test ssl --- server/PlainWsSession.h | 37 +++++++++++++++---------------------- server/listener.h | 9 ++++----- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/server/PlainWsSession.h b/server/PlainWsSession.h index 79079edae..60bf7cd04 100644 --- a/server/PlainWsSession.h +++ b/server/PlainWsSession.h @@ -241,6 +241,7 @@ class WsUpgrader : public std::enable_shared_from_this std::shared_ptr subscriptions_; std::shared_ptr balancer_; DOSGuard& dosGuard_; + http::request req_; public: WsUpgrader( @@ -257,6 +258,7 @@ public: , dosGuard_(dosGuard) , buffer_(std::move(b)) { + std::cout << "1" << std::endl; } WsUpgrader( boost::beast::tcp_stream&& stream, @@ -264,14 +266,17 @@ public: std::shared_ptr subscriptions, std::shared_ptr balancer, DOSGuard& dosGuard, - boost::beast::flat_buffer&& b) + boost::beast::flat_buffer&& b, + http::request req) : http_(std::move(stream)) , backend_(backend) , subscriptions_(subscriptions) , balancer_(balancer) , dosGuard_(dosGuard) , buffer_(std::move(b)) + , req_(std::move(req)) { + std::cout << "2" << std::endl; } void @@ -304,36 +309,24 @@ private: boost::beast::get_lowest_layer(http_).expires_after( std::chrono::seconds(30)); - // Read a request using the parser-oriented interface - http::async_read( - http_, - buffer_, - *parser_, - boost::beast::bind_front_handler( - &WsUpgrader::on_upgrade, shared_from_this())); + on_upgrade(); } void - on_upgrade(boost::beast::error_code ec, std::size_t bytes_transferred) + on_upgrade() { - std::cout << "upgraded WS" << std::endl; - boost::ignore_unused(bytes_transferred); - - // This means they closed the connection - if (ec == http::error::end_of_stream) - return; - - if (ec) - return wsFail(ec, "upgrade"); - // See if it is a WebSocket Upgrade - if (!websocket::is_upgrade(parser_->get())) - return wsFail(ec, "is_upgrade"); + if (!websocket::is_upgrade(req_)) + { + std::cout << "is not upgrade" << std::endl; + return; + } // Disable the timeout. // The websocket::stream uses its own timeout settings. boost::beast::get_lowest_layer(http_).expires_never(); + std::cout << "making session" << std::endl; std::make_shared( http_.release_socket(), backend_, @@ -341,7 +334,7 @@ private: balancer_, dosGuard_, std::move(buffer_)) - ->run(parser_->release()); + ->run(std::move(req_)); } }; diff --git a/server/listener.h b/server/listener.h index b893ee60d..875e4e6c8 100644 --- a/server/listener.h +++ b/server/listener.h @@ -115,11 +115,10 @@ public: } }; -template void make_websocket_session( boost::beast::tcp_stream stream, - http::request> req, + http::request req, boost::beast::flat_buffer buffer, std::shared_ptr backend, std::shared_ptr subscriptions, @@ -132,15 +131,15 @@ make_websocket_session( subscriptions, balancer, dosGuard, - std::move(buffer)) + std::move(buffer), + std::move(req)) ->run(); } -template void make_websocket_session( boost::beast::ssl_stream stream, - http::request> req, + http::request req, boost::beast::flat_buffer buffer, std::shared_ptr backend, std::shared_ptr subscriptions,