#ifndef HTTPSESSION_H #define HTTPSESSION_H #include "router.hpp" #include #include #include #include #include #include #include #include /** Represents an established HTTP connection */ class HttpSession : public std::enable_shared_from_this { void doRead(); void onWrite(boost::beast::error_code ec, std::size_t, bool close); public: using Request = boost::beast::http::request; using RequestHandler = std::function; HttpSession(boost::asio::ip::tcp::socket &&socket, const std::shared_ptr> &router); template void reply(Response &&response) { using ResponseType = typename std::decay_t; auto sp = std::make_shared(std::forward(response)); boost::beast::http::async_write(m_stream, *sp, [self = shared_from_this(), sp](boost::beast::error_code ec, std::size_t bytes) { self->onWrite(ec, bytes, sp->need_eof()); }); } void errorReply(const Request &request, boost::beast::http::status status, boost::beast::string_view message); boost::beast::tcp_stream::executor_type executor(); boost::asio::ip::tcp::socket releaseSocket(); void run(); protected: void onRead(const boost::beast::error_code &error, std::size_t); private: boost::beast::tcp_stream m_stream; std::weak_ptr> m_router; boost::beast::flat_buffer m_buffer{std::numeric_limits::max()}; std::optional> m_parser; }; #endif // HTTPSESSION_H