RAVENNAKIT
Loading...
Searching...
No Matches
http_server.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3 * Project: RAVENNAKIT (RAVENNA / AES67 / ST2110-30 SDK)
4 * Copyright (c) 2024-2025 Sound on Digital
5 *
6 * This file is part of RAVENNAKIT.
7 *
8 * RAVENNAKIT is dual-licensed:
9 * 1) Under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version (the "AGPL License"); and
12 * 2) Under a commercial license from Sound on Digital, for customers who
13 * cannot (or do not wish to) comply with the AGPL License terms.
14 *
15 * If you obtained this file under the AGPL License, you may redistribute it
16 * and/or modify it under the terms of the AGPL License. See the LICENSE
17 * file in the project root for details.
18 *
19 * For commercial licensing, support, and other inquiries, please visit:
20 *
21 * https://ravennakit.com
22 *
23 */
24
25#pragma once
26
27#include "http_router.hpp"
29
30#include <map>
31#include <boost/asio.hpp>
32#include <boost/beast/core/error.hpp>
33#include <boost/beast/http/message_generator.hpp>
34#include <boost/beast/http/string_body_fwd.hpp>
35#include <boost/system/result.hpp>
36#include <boost/url.hpp>
37
38#include <functional>
39
40namespace rav {
41
46 public:
47 using Request = boost::beast::http::request<boost::beast::http::string_body>;
48 using Response = boost::beast::http::response<boost::beast::http::string_body>;
49 using Handler = std::function<void(const Request&, Response&, PathMatcher::Parameters&)>;
50
52 static constexpr auto k_timeout_seconds = 5;
53
59 explicit HttpServer(boost::asio::io_context& io_context);
60
62
69 boost::system::result<void> start(std::string_view bind_address, uint16_t port);
70
74 void stop();
75
79 [[nodiscard]] boost::asio::ip::tcp::endpoint get_local_endpoint() const;
80
84 [[nodiscard]] std::string get_address_string() const;
85
89 [[nodiscard]] size_t get_client_count() const;
90
96 void get(const std::string_view pattern, Handler handler) {
97 router_.insert(boost::beast::http::verb::get, pattern, std::move(handler));
98 }
99
105 void post(const std::string_view pattern, Handler handler) {
106 router_.insert(boost::beast::http::verb::post, pattern, std::move(handler));
107 }
108
114 void options(const std::string_view pattern, Handler handler) {
115 router_.insert(boost::beast::http::verb::options, pattern, std::move(handler));
116 }
117
123 void patch(const std::string_view pattern, Handler handler) {
124 router_.insert(boost::beast::http::verb::patch, pattern, std::move(handler));
125 }
126
127 private:
128 class Listener;
129 std::shared_ptr<Listener> listener_;
130
131 class ClientSession;
132 std::vector<std::shared_ptr<ClientSession>> client_sessions_;
133
134 boost::asio::io_context& io_context_;
135
136 HttpRouter<Handler> router_;
137
138 void on_accept(boost::asio::ip::tcp::socket socket);
139 void on_listener_error(const boost::beast::error_code& ec, std::string_view what) const;
140 void on_client_error(const boost::beast::error_code& ec, std::string_view what) const;
141 boost::beast::http::message_generator on_request(const boost::beast::http::request<boost::beast::http::string_body>&);
142 void remove_client_session(const ClientSession* session);
143};
144
145} // namespace rav
A simple HTTP router that matches HTTP requests to handlers based on the method and path.
std::function< void(const Request &, Response &, PathMatcher::Parameters &)> Handler
static constexpr auto k_timeout_seconds
The time to wait for a request before closing the connection.
boost::beast::http::response< boost::beast::http::string_body > Response
void options(const std::string_view pattern, Handler handler)
Adds a handler for OPTIONS requests to the given pattern.
boost::system::result< void > start(std::string_view bind_address, uint16_t port)
Starts the server on the given host and port.
boost::asio::ip::tcp::endpoint get_local_endpoint() const
void post(const std::string_view pattern, Handler handler)
Adds a handler for POST requests to the given pattern.
size_t get_client_count() const
void stop()
Stops the server and closes all client sessions.
std::string get_address_string() const
HttpServer(boost::asio::io_context &io_context)
Constructs a new HttpServer using the given io_context.
void patch(const std::string_view pattern, Handler handler)
Adds a handler for PATCH requests to the given pattern.
boost::beast::http::request< boost::beast::http::string_body > Request
void get(const std::string_view pattern, Handler handler)
Adds a handler for GET requests to the given pattern.
Contains parameters extracted from a path with convenience functions to convert to integers.