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>
47 using Request = boost::beast::http::request<boost::beast::http::string_body>;
48 using Response = boost::beast::http::response<boost::beast::http::string_body>;
59 explicit HttpServer(boost::asio::io_context& io_context);
69 boost::system::result<void>
start(std::string_view bind_address, uint16_t port);
96 void get(
const std::string_view pattern,
Handler handler) {
97 router_.insert(boost::beast::http::verb::get, pattern, std::move(handler));
106 router_.insert(boost::beast::http::verb::post, pattern, std::move(handler));
115 router_.insert(boost::beast::http::verb::options, pattern, std::move(handler));
124 router_.insert(boost::beast::http::verb::patch, pattern, std::move(handler));
129 std::shared_ptr<Listener> listener_;
132 std::vector<std::shared_ptr<ClientSession>> client_sessions_;
134 boost::asio::io_context& io_context_;
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);
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.