ORE Studio 0.0.4
Loading...
Searching...
No Matches
http_session.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_HTTP_NET_HTTP_SESSION_HPP
21#define ORES_HTTP_NET_HTTP_SESSION_HPP
22
23#include <memory>
24#include <string>
25#include <chrono>
26#include <functional>
27#include <boost/asio/ip/tcp.hpp>
28#include <boost/asio/awaitable.hpp>
29#include <boost/beast/core.hpp>
30#include <boost/beast/http.hpp>
31#include "ores.http.api/net/router.hpp"
32#include "ores.http.api/net/http_server_options.hpp"
33#include "ores.security/jwt/jwt_authenticator.hpp"
34#include "ores.logging/make_logger.hpp"
35
36namespace ores::http::net {
37
49using session_bytes_callback = std::function<void(
50 const std::string& session_id,
51 std::chrono::system_clock::time_point start_time,
52 std::size_t bytes_sent,
53 std::size_t bytes_received)>;
54
58class http_session final : public std::enable_shared_from_this<http_session> {
59public:
60 explicit http_session(boost::asio::ip::tcp::socket socket,
61 std::shared_ptr<router> router,
62 std::shared_ptr<ores::security::jwt::jwt_authenticator> authenticator,
63 const http_server_options& options,
64 session_bytes_callback bytes_callback = nullptr);
65
69 boost::asio::awaitable<void> run();
70
71private:
72 inline static std::string_view logger_name = "ores.http.net.http_session";
73
74 static auto& lg() {
75 using namespace ores::logging;
76 static auto instance = make_logger(logger_name);
77 return instance;
78 }
79
80 boost::asio::awaitable<void> handle_request(
81 boost::beast::http::request<boost::beast::http::string_body> req);
82
83 domain::http_request convert_request(
84 const boost::beast::http::request<boost::beast::http::string_body>& req);
85
86 boost::beast::http::response<boost::beast::http::string_body> convert_response(
87 const domain::http_response& resp, unsigned version, bool keep_alive);
88
89 domain::http_method convert_method(boost::beast::http::verb verb);
90
91 void parse_query_params(const std::string& target, domain::http_request& req);
92
93 boost::beast::tcp_stream stream_;
94 boost::beast::flat_buffer buffer_;
95 std::shared_ptr<router> router_;
96 std::shared_ptr<ores::security::jwt::jwt_authenticator> authenticator_;
97 http_server_options options_;
98 std::string remote_address_;
99 session_bytes_callback bytes_callback_;
100};
101
102}
103
104#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Represents an incoming HTTP request.
Definition http_request.hpp:35
Represents an HTTP response to be sent to the client.
Definition http_response.hpp:32
Configuration options for the HTTP server.
Definition http_server_options.hpp:33
Handles a single HTTP session/connection.
Definition http_session.hpp:58
boost::asio::awaitable< void > run()
Starts processing the session.
Definition http_session.cpp:53
HTTP request router that matches requests to handlers.
Definition router.hpp:163