From 62ce10bba91e556c8d315aa3d356ec26af2862dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 11 Nov 2024 20:01:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Listener.cpp | 14 +++++--------- src/Listener.h | 10 +++++----- src/Session.cpp | 34 +++++++++++++++++----------------- src/Session.h | 16 ++++++++-------- src/helper.cpp | 6 +++--- src/helpers.h | 14 +++++++------- src/main.cpp | 11 ++--------- 7 files changed, 47 insertions(+), 58 deletions(-) diff --git a/src/Listener.cpp b/src/Listener.cpp index 1ac1092..e7dd2ad 100644 --- a/src/Listener.cpp +++ b/src/Listener.cpp @@ -1,12 +1,8 @@ -// -// Created by Антон on 11.11.2024. -// - #include "Listener.h" namespace uad { -listener::listener(boost::asio::io_context& ioc, +Listener::Listener(boost::asio::io_context& ioc, boost::asio::ip::tcp::endpoint endpoint, const std::shared_ptr& doc_root) : ioc_(ioc), acceptor_(net::make_strand(ioc)), doc_root_(doc_root) @@ -43,21 +39,21 @@ listener::listener(boost::asio::io_context& ioc, } } -void listener::run() +void Listener::Run() { do_accept(); } -void listener::do_accept() +void Listener::DoAccept() { acceptor_.async_accept( net::make_strand(ioc_), beast::bind_front_handler( - &listener::on_accept, + &Listener::on_accept, shared_from_this())); } -void listener::on_accept(beast::error_code ec, tcp::socket socket) +void Listener::OnAccept(beast::error_code ec, tcp::socket socket) { if (ec) { diff --git a/src/Listener.h b/src/Listener.h index ed300e9..9c795f6 100644 --- a/src/Listener.h +++ b/src/Listener.h @@ -23,23 +23,23 @@ namespace http = beast::http; namespace net = boost::asio; using tcp = boost::asio::ip::tcp; -class listener : public std::enable_shared_from_this +class Listener : public std::enable_shared_from_this { net::io_context& ioc_; tcp::acceptor acceptor_; std::shared_ptr doc_root_; public: - listener( + Listener( net::io_context& ioc, tcp::endpoint endpoint, std::shared_ptr const& doc_root); - void run(); + void Run(); private: - void do_accept(); + void DoAccept(); - void on_accept(beast::error_code ec, tcp::socket socket); + void OnAccept(beast::error_code ec, tcp::socket socket); }; } diff --git a/src/Session.cpp b/src/Session.cpp index 9c6f5b9..448202d 100644 --- a/src/Session.cpp +++ b/src/Session.cpp @@ -2,20 +2,20 @@ namespace uad { -session::session( +Session::Session( tcp::socket&& socket, std::shared_ptr const& doc_root) : stream_(std::move(socket)), doc_root_(doc_root) {} -void session::run() +void Session::Run() { net::dispatch(stream_.get_executor(), beast::bind_front_handler( - &session::do_read, + &Session::DoRead, shared_from_this())); } -void session::do_read() +void Session::DoRead() { req_ = {}; @@ -23,27 +23,27 @@ void session::do_read() http::async_read(stream_, buffer_, req_, beast::bind_front_handler( - &session::on_read, + &Session::OnRead, shared_from_this())); } -void session::on_read( +void Session::OnRead( beast::error_code ec, std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); if (ec == http::error::end_of_stream) - return do_close(); + return DoClose(); if (ec) - return fail(ec, "read"); + return Fail(ec, "read"); - send_response( - handle_request(*doc_root_, std::move(req_))); + SendResponse( + HandleRequest(*doc_root_, std::move(req_))); } -void session::send_response(http::message_generator&& msg) +void Session::SendResponse(http::message_generator&& msg) { bool keep_alive = msg.keep_alive(); @@ -51,10 +51,10 @@ void session::send_response(http::message_generator&& msg) stream_, std::move(msg), beast::bind_front_handler( - &session::on_write, shared_from_this(), keep_alive)); + &Session::OnWrite, shared_from_this(), keep_alive)); } -void session::on_write( +void Session::OnWrite( bool keep_alive, beast::error_code ec, std::size_t bytes_transferred) @@ -62,17 +62,17 @@ void session::on_write( boost::ignore_unused(bytes_transferred); if (ec) - return uad::fail(ec, "write"); + return uad::Fail(ec, "write"); if (!keep_alive) { - return do_close(); + return DoClose(); } - do_read(); + DoRead(); } -void session::do_close() +void Session::DoClose() { beast::error_code ec; stream_.socket().shutdown(tcp::socket::shutdown_send, ec); diff --git a/src/Session.h b/src/Session.h index b786078..29c2ae5 100644 --- a/src/Session.h +++ b/src/Session.h @@ -17,7 +17,7 @@ namespace http = beast::http; using tcp = boost::asio::ip::tcp; namespace net = boost::asio; -class session : public std::enable_shared_from_this +class Session : public std::enable_shared_from_this { beast::tcp_stream stream_; beast::flat_buffer buffer_; @@ -25,25 +25,25 @@ class session : public std::enable_shared_from_this http::request req_; public: - session( + Session( tcp::socket&& socket, std::shared_ptr const& doc_root); - void run(); + void Run(); - void do_read(); + void DoRead(); - void on_read( + void OnRead( beast::error_code ec, std::size_t bytes_transferred); - void send_response(http::message_generator&& msg); + void SendResponse(http::message_generator&& msg); - void on_write( + void OnWrite( bool keep_alive, beast::error_code ec, std::size_t bytes_transferred); - void do_close(); + void DoClose(); }; } diff --git a/src/helper.cpp b/src/helper.cpp index 1fa98ab..e2ff51c 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -2,7 +2,7 @@ namespace uad { -beast::string_view mime_type(beast::string_view path) +beast::string_view MimeType(beast::string_view path) { using beast::iequals; auto const ext = [&path] @@ -36,7 +36,7 @@ beast::string_view mime_type(beast::string_view path) return "application/text"; } -std::string path_cat( +std::string PathCat( beast::string_view base, beast::string_view path) { @@ -60,7 +60,7 @@ std::string path_cat( return result; } -void fail(beast::error_code ec, char const* what) +void Fail(beast::error_code ec, char const* what) { std::cerr << what << ": " << ec.message() << "\n"; } diff --git a/src/helpers.h b/src/helpers.h index c09bc6c..ae20537 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -22,14 +22,14 @@ namespace http = beast::http; namespace net = boost::asio; using tcp = boost::asio::ip::tcp; -beast::string_view mime_type(beast::string_view path); +beast::string_view MimeType(beast::string_view path); -std::string path_cat( +std::string PathCat( beast::string_view base, beast::string_view path); template -http::message_generator handle_request( +http::message_generator HandleRequest( beast::string_view doc_root, http::request>&& req) { @@ -78,7 +78,7 @@ http::message_generator handle_request( req.target().find("..") != beast::string_view::npos) return bad_request("Illegal request-target"); - std::string path = path_cat(doc_root, req.target()); + std::string path = PathCat(doc_root, req.target()); if (req.target().back() == '/') path.append("index.html"); @@ -98,7 +98,7 @@ http::message_generator handle_request( { http::response res {http::status::ok, req.version()}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); - res.set(http::field::content_type, mime_type(path)); + res.set(http::field::content_type, MimeType(path)); res.content_length(size); res.keep_alive(req.keep_alive()); return res; @@ -109,11 +109,11 @@ http::message_generator handle_request( std::make_tuple(std::move(body)), std::make_tuple(http::status::ok, req.version())}; res.set(http::field::server, BOOST_BEAST_VERSION_STRING); - res.set(http::field::content_type, mime_type(path)); + res.set(http::field::content_type, MimeType(path)); res.content_length(size); res.keep_alive(req.keep_alive()); return res; } -void fail(beast::error_code ec, char const* what); +void Fail(beast::error_code ec, char const* what); } diff --git a/src/main.cpp b/src/main.cpp index ecb5a08..bbe305c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,22 +1,15 @@ #include "sdk.h" #include -#include -#include -#include #include -#include #include #include -#include #include #include #include #include #include -#include "helpers.h" -#include "Session.h" #include "Listener.h" namespace beast = boost::beast; @@ -42,10 +35,10 @@ int main(int argc, char* argv[]) net::io_context ioc {threads}; - std::make_shared( + std::make_shared( ioc, tcp::endpoint {address, port}, - doc_root)->run(); + doc_root)->Run(); std::vector v; v.reserve(threads - 1);