15#include <asio/ssl.hpp>
33 class http2_server_connection;
92 class http2_server :
public std::enable_shared_from_this<http2_server>
102 const std::string& error_message)>;
154 [[nodiscard]]
auto is_running() const ->
bool;
206 [[nodiscard]] auto
server_id() const -> std::string_view;
232 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
work_guard_;
247 std::map<uint64_t, std::shared_ptr<http2_server_connection>>
connections_;
262 "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
285 asio::ip::tcp::socket socket,
299 std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket>> socket,
331 [[nodiscard]]
auto is_alive() const ->
bool;
379 std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket>>
tls_socket_;
DATA frame (RFC 7540 Section 6.1)
Base class for HTTP/2 frames.
GOAWAY frame (RFC 7540 Section 6.8)
HPACK header decoder (RFC 7541)
HPACK header encoder (RFC 7541)
Represents a single HTTP/2 connection on the server side.
http2_server_connection(uint64_t connection_id, asio::ip::tcp::socket socket, const http2_settings &settings, http2_server::request_handler_t request_handler, http2_server::error_handler_t error_handler)
Construct server connection with plain socket.
http2_server::error_handler_t error_handler_
auto handle_data_frame(const data_frame &f) -> VoidResult
auto close_stream(uint32_t stream_id) -> void
auto start_reading() -> void
auto get_or_create_stream(uint32_t stream_id) -> http2_stream *
auto read_frame_payload(uint32_t length) -> void
std::unique_ptr< asio::ip::tcp::socket > plain_socket_
~http2_server_connection()
Destructor.
std::atomic< bool > preface_received_
auto read_connection_preface() -> void
std::vector< uint8_t > read_buffer_
auto start() -> VoidResult
Start connection handling.
auto stream_count() const -> size_t
Get number of active streams.
auto handle_goaway_frame(const goaway_frame &f) -> VoidResult
bool use_tls_
TLS mode flag.
auto stop() -> VoidResult
Stop connection.
auto connection_id() const -> uint64_t
Get connection identifier.
auto send_settings_ack() -> VoidResult
auto handle_window_update_frame(const window_update_frame &f) -> VoidResult
auto process_frame(std::unique_ptr< frame > f) -> VoidResult
http2_server_connection(http2_server_connection &&)=delete
std::array< uint8_t, 9 > frame_header_buffer_
auto handle_settings_frame(const settings_frame &frame) -> VoidResult
int32_t connection_window_size_
auto handle_rst_stream_frame(const rst_stream_frame &f) -> VoidResult
std::unique_ptr< asio::ssl::stream< asio::ip::tcp::socket > > tls_socket_
auto send_frame(const frame &f) -> VoidResult
auto send_settings() -> VoidResult
http2_server_connection & operator=(http2_server_connection &&)=delete
auto handle_headers_frame(const headers_frame &f) -> VoidResult
http2_settings local_settings_
http2_server::request_handler_t request_handler_
auto read_frame_header() -> void
auto is_alive() const -> bool
Check if connection is alive.
http2_server_connection & operator=(const http2_server_connection &)=delete
http2_server_connection(const http2_server_connection &)=delete
std::atomic< bool > goaway_sent_
std::atomic< bool > is_alive_
auto dispatch_request(uint32_t stream_id) -> void
uint64_t connection_id_
Connection identifier.
std::map< uint32_t, http2_stream > streams_
http2_settings remote_settings_
auto handle_ping_frame(const ping_frame &f) -> VoidResult
std::mutex streams_mutex_
Server-side HTTP/2 stream for sending responses.
HTTP/2 server with TLS support.
auto active_streams() const -> size_t
Get total number of active streams across all connections.
auto remove_connection(uint64_t connection_id) -> void
http2_server(std::string_view server_id)
Construct HTTP/2 server.
auto set_error_handler(error_handler_t handler) -> void
Set error handler.
std::promise< void > stop_promise_
std::atomic< bool > use_tls_
http2_server & operator=(http2_server &&)=delete
static constexpr size_t FRAME_HEADER_SIZE
std::unique_ptr< asio::io_context > io_context_
http2_server(http2_server &&)=delete
auto start_cleanup_timer() -> void
http2_server(const http2_server &)=delete
std::string server_id_
Server identifier.
std::function< void( const std::string &error_message)> error_handler_t
Error handler function type.
auto server_id() const -> std::string_view
Get server identifier.
std::shared_ptr< hpack_encoder > encoder_
auto set_settings(const http2_settings &settings) -> void
Set server settings.
auto do_accept_tls() -> void
auto handle_accept(std::error_code ec, asio::ip::tcp::socket socket) -> void
std::shared_ptr< hpack_decoder > decoder_
request_handler_t request_handler_
auto cleanup_dead_connections() -> void
auto get_settings() const -> http2_settings
Get current settings.
~http2_server()
Destructor - stops server gracefully.
auto stop() -> VoidResult
Stop the server.
http2_server & operator=(const http2_server &)=delete
auto active_connections() const -> size_t
Get number of active connections.
auto is_running() const -> bool
Check if server is running.
auto add_connection(std::shared_ptr< http2_server_connection > conn) -> void
error_handler_t error_handler_
static constexpr std::string_view CONNECTION_PREFACE
std::mutex connections_mutex_
auto handle_accept_tls(std::error_code ec, asio::ip::tcp::socket socket) -> void
std::atomic< bool > is_running_
std::map< uint64_t, std::shared_ptr< http2_server_connection > > connections_
std::future< void > io_future_
auto set_request_handler(request_handler_t handler) -> void
Set request handler.
auto start(unsigned short port) -> VoidResult
Start HTTP/2 server without TLS (h2c - HTTP/2 cleartext)
auto start_tls(unsigned short port, const tls_config &config) -> VoidResult
Start HTTP/2 server with TLS.
std::function< void( http2_server_stream &stream, const http2_request &request)> request_handler_t
Request handler function type.
std::atomic< uint64_t > next_connection_id_
std::unique_ptr< asio::steady_timer > cleanup_timer_
auto wait() -> void
Wait for server to stop.
std::unique_ptr< asio::ssl::context > ssl_context_
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
std::unique_ptr< asio::ip::tcp::acceptor > acceptor_
std::future< void > stop_future_
PING frame (RFC 7540 Section 6.7)
RST_STREAM frame (RFC 7540 Section 6.4)
SETTINGS frame (RFC 7540 Section 6.5)
WINDOW_UPDATE frame (RFC 7540 Section 6.9)
error_code
HTTP/2 error codes (RFC 7540 Section 7)
@ settings
SETTINGS frame.
Result< std::monostate > VoidResult
Network-specific error and result type definitions.
HTTP/2 request data structure.
HTTP/2 connection settings.
HTTP/2 stream state and data.
TLS configuration for HTTP/2 server.
bool verify_client
Require client certificate.
std::string ca_file
Path to CA certificate file (optional)
std::string cert_file
Path to certificate file (PEM)
std::string key_file
Path to private key file (PEM)