|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
HTTP/2 client with TLS support. More...
#include <http2_client.h>


Public Member Functions | |
| http2_client (std::string_view client_id) | |
| Construct HTTP/2 client. | |
| ~http2_client () | |
| Destructor - closes connection gracefully. | |
| http2_client (const http2_client &)=delete | |
| http2_client & | operator= (const http2_client &)=delete |
| http2_client (http2_client &&)=delete | |
| http2_client & | operator= (http2_client &&)=delete |
| auto | connect (const std::string &host, unsigned short port=443) -> VoidResult |
| Connect to HTTP/2 server. | |
| auto | disconnect () -> VoidResult |
| Disconnect from server. | |
| auto | is_connected () const -> bool |
| Check if connected. | |
| auto | get (const std::string &path, const std::vector< http_header > &headers={}) -> Result< http2_response > |
| Perform HTTP/2 GET request. | |
| auto | post (const std::string &path, const std::string &body, const std::vector< http_header > &headers={}) -> Result< http2_response > |
| Perform HTTP/2 POST request. | |
| auto | post (const std::string &path, const std::vector< uint8_t > &body, const std::vector< http_header > &headers={}) -> Result< http2_response > |
| Perform HTTP/2 POST request with binary body. | |
| auto | put (const std::string &path, const std::string &body, const std::vector< http_header > &headers={}) -> Result< http2_response > |
| Perform HTTP/2 PUT request. | |
| auto | del (const std::string &path, const std::vector< http_header > &headers={}) -> Result< http2_response > |
| Perform HTTP/2 DELETE request. | |
| auto | set_timeout (std::chrono::milliseconds timeout) -> void |
| Set request timeout. | |
| auto | get_timeout () const -> std::chrono::milliseconds |
| Get current timeout. | |
| auto | start_stream (const std::string &path, const std::vector< http_header > &headers, std::function< void(std::vector< uint8_t >)> on_data, std::function< void(std::vector< http_header >)> on_headers, std::function< void(int)> on_complete) -> Result< uint32_t > |
| Start a streaming POST request. | |
| auto | write_stream (uint32_t stream_id, const std::vector< uint8_t > &data, bool end_stream=false) -> VoidResult |
| Write data to an open stream. | |
| auto | close_stream_writer (uint32_t stream_id) -> VoidResult |
| Close the write side of a stream. | |
| auto | cancel_stream (uint32_t stream_id) -> VoidResult |
| Cancel a stream. | |
| auto | get_settings () const -> http2_settings |
| Get local settings. | |
| auto | set_settings (const http2_settings &settings) -> void |
| Update local settings. | |
Private Member Functions | |
| auto | send_connection_preface () -> VoidResult |
| auto | send_settings () -> VoidResult |
| auto | handle_settings_frame (const settings_frame &frame) -> VoidResult |
| auto | send_settings_ack () -> VoidResult |
| auto | send_frame (const frame &f) -> VoidResult |
| auto | read_frame () -> Result< std::unique_ptr< frame > > |
| auto | process_frame (std::unique_ptr< frame > f) -> VoidResult |
| auto | allocate_stream_id () -> uint32_t |
| auto | get_stream (uint32_t stream_id) -> http2_stream * |
| auto | create_stream () -> http2_stream & |
| auto | close_stream (uint32_t stream_id) -> void |
| auto | send_request (const std::string &method, const std::string &path, const std::vector< http_header > &headers, const std::vector< uint8_t > &body) -> Result< http2_response > |
| auto | build_headers (const std::string &method, const std::string &path, const std::vector< http_header > &additional) -> std::vector< http_header > |
| auto | handle_headers_frame (const headers_frame &f) -> VoidResult |
| auto | handle_data_frame (const data_frame &f) -> VoidResult |
| auto | handle_rst_stream_frame (const rst_stream_frame &f) -> VoidResult |
| auto | handle_goaway_frame (const goaway_frame &f) -> VoidResult |
| auto | handle_window_update_frame (const window_update_frame &f) -> VoidResult |
| auto | handle_ping_frame (const ping_frame &f) -> VoidResult |
| auto | run_io () -> void |
| auto | stop_io () -> void |
Private Attributes | |
| std::string | client_id_ |
| Client identifier. | |
| std::string | host_ |
| Connected host. | |
| unsigned short | port_ = 443 |
| Connected port. | |
| std::unique_ptr< asio::io_context > | io_context_ |
| std::unique_ptr< asio::ssl::context > | ssl_context_ |
| std::unique_ptr< asio::ssl::stream< asio::ip::tcp::socket > > | socket_ |
| std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > | work_guard_ |
| std::future< void > | io_future_ |
| std::atomic< bool > | is_connected_ {false} |
| std::atomic< bool > | is_running_ {false} |
| std::atomic< bool > | goaway_received_ {false} |
| std::mutex | streams_mutex_ |
| std::map< uint32_t, http2_stream > | streams_ |
| std::atomic< uint32_t > | next_stream_id_ {1} |
| Client streams are odd. | |
| int32_t | connection_window_size_ = 65535 |
| http2_settings | local_settings_ |
| http2_settings | remote_settings_ |
| hpack_encoder | encoder_ |
| hpack_decoder | decoder_ |
| std::chrono::milliseconds | timeout_ {30000} |
| std::vector< uint8_t > | read_buffer_ |
Static Private Attributes | |
| static constexpr std::string_view | CONNECTION_PREFACE |
| static constexpr size_t | FRAME_HEADER_SIZE = 9 |
| static constexpr size_t | DEFAULT_WINDOW_SIZE = 65535 |
HTTP/2 client with TLS support.
Definition at line 154 of file http2_client.h.
|
explicit |
Construct HTTP/2 client.
| client_id | Unique client identifier for logging |
Definition at line 44 of file http2_client.cpp.
| kcenon::network::protocols::http2::http2_client::~http2_client | ( | ) |
Destructor - closes connection gracefully.
Definition at line 51 of file http2_client.cpp.
References disconnect(), and is_connected_.

|
delete |
|
delete |
|
private |
Definition at line 685 of file http2_client.cpp.
|
private |
Definition at line 846 of file http2_client.cpp.
References kcenon::network::protocols::http2::headers.
| auto kcenon::network::protocols::http2::http2_client::cancel_stream | ( | uint32_t | stream_id | ) | -> VoidResult |
Cancel a stream.
| stream_id | Stream identifier |
Definition at line 430 of file http2_client.cpp.
References kcenon::network::protocols::http2::cancel, kcenon::network::protocols::http2::closed, kcenon::network::error_codes::network_system::connection_closed, kcenon::network::error_void(), kcenon::network::error_codes::common_errors::not_found, and kcenon::network::ok().

|
private |
Definition at line 713 of file http2_client.cpp.
References kcenon::network::protocols::http2::closed.
| auto kcenon::network::protocols::http2::http2_client::close_stream_writer | ( | uint32_t | stream_id | ) | -> VoidResult |
Close the write side of a stream.
| stream_id | Stream identifier |
Definition at line 395 of file http2_client.cpp.
References kcenon::network::protocols::http2::closed, kcenon::network::error_codes::network_system::connection_closed, kcenon::network::error_void(), kcenon::network::protocols::http2::half_closed_local, kcenon::network::error_codes::common_errors::not_found, and kcenon::network::ok().

| auto kcenon::network::protocols::http2::http2_client::connect | ( | const std::string & | host, |
| unsigned short | port = 443 ) -> VoidResult |
Connect to HTTP/2 server.
| host | Server hostname |
| port | Server port (default 443) |
Establishes TLS connection with ALPN "h2", sends connection preface, and exchanges SETTINGS frames.
Definition at line 66 of file http2_client.cpp.
References kcenon::network::error_codes::common_errors::already_exists, kcenon::network::error_codes::network_system::connection_failed, kcenon::network::tracing::trace_context::create_span(), kcenon::network::error_void(), kcenon::network::error_codes::common_errors::invalid_argument, kcenon::network::tracing::is_tracing_enabled(), kcenon::network::ok(), and kcenon::network::tracing::ok.

|
private |
Definition at line 702 of file http2_client.cpp.
References kcenon::network::protocols::http2::idle.
| auto kcenon::network::protocols::http2::http2_client::del | ( | const std::string & | path, |
| const std::vector< http_header > & | headers = {} ) -> Result<http2_response> |
Perform HTTP/2 DELETE request.
| path | Request path |
| headers | Additional headers (optional) |
Definition at line 277 of file http2_client.cpp.
References kcenon::network::protocols::http2::headers.
| auto kcenon::network::protocols::http2::http2_client::disconnect | ( | ) | -> VoidResult |
Disconnect from server.
Sends GOAWAY frame and closes connection gracefully.
Definition at line 210 of file http2_client.cpp.
References kcenon::network::protocols::http2::goaway, kcenon::network::protocols::http2::no_error, and kcenon::network::ok().
Referenced by ~http2_client().


| auto kcenon::network::protocols::http2::http2_client::get | ( | const std::string & | path, |
| const std::vector< http_header > & | headers = {} ) -> Result<http2_response> |
Perform HTTP/2 GET request.
| path | Request path (e.g., "/api/users") |
| headers | Additional headers (optional) |
Definition at line 244 of file http2_client.cpp.
References kcenon::network::protocols::http2::headers.
| auto kcenon::network::protocols::http2::http2_client::get_settings | ( | ) | const -> http2_settings |
Get local settings.
Definition at line 294 of file http2_client.cpp.
References local_settings_.
|
private |
Definition at line 691 of file http2_client.cpp.
| auto kcenon::network::protocols::http2::http2_client::get_timeout | ( | ) | const -> std::chrono::milliseconds |
Get current timeout.
Definition at line 289 of file http2_client.cpp.
References timeout_.
|
private |
Definition at line 960 of file http2_client.cpp.
References kcenon::network::protocols::http2::http2_response::body, kcenon::network::protocols::http2::closed, kcenon::network::protocols::http2::data, kcenon::network::error_void(), kcenon::network::protocols::http2::http2_response::headers, kcenon::network::error_codes::common_errors::not_found, kcenon::network::ok(), and kcenon::network::protocols::http2::http2_response::status_code.

|
private |
Definition at line 1069 of file http2_client.cpp.
References kcenon::network::protocols::http2::closed, kcenon::network::ok(), and kcenon::network::protocols::http2::http2_response::status_code.

|
private |
Definition at line 875 of file http2_client.cpp.
References kcenon::network::protocols::http2::http2_response::body, kcenon::network::protocols::http2::closed, kcenon::network::error_void(), kcenon::network::get_error_details(), kcenon::network::protocols::http2::http2_response::headers, kcenon::network::error_codes::common_errors::not_found, kcenon::network::ok(), and kcenon::network::protocols::http2::http2_response::status_code.

|
private |
Definition at line 1114 of file http2_client.cpp.
References kcenon::network::ok().

|
private |
Definition at line 1051 of file http2_client.cpp.
References kcenon::network::protocols::http2::closed, kcenon::network::ok(), and kcenon::network::protocols::http2::http2_response::status_code.

|
private |
Definition at line 503 of file http2_client.cpp.
References kcenon::network::protocols::http2::enable_push, kcenon::network::protocols::http2::header_table_size, kcenon::network::protocols::http2::initial_window_size, kcenon::network::protocols::http2::max_concurrent_streams, kcenon::network::protocols::http2::max_frame_size, kcenon::network::protocols::http2::max_header_list_size, and kcenon::network::ok().

|
private |
Definition at line 1092 of file http2_client.cpp.
References kcenon::network::ok().

| auto kcenon::network::protocols::http2::http2_client::is_connected | ( | ) | const -> bool |
Check if connected.
Definition at line 239 of file http2_client.cpp.
References goaway_received_, and is_connected_.
|
delete |
|
delete |
| auto kcenon::network::protocols::http2::http2_client::post | ( | const std::string & | path, |
| const std::string & | body, | ||
| const std::vector< http_header > & | headers = {} ) -> Result<http2_response> |
Perform HTTP/2 POST request.
| path | Request path |
| body | Request body as string |
| headers | Additional headers (optional) |
Definition at line 251 of file http2_client.cpp.
References kcenon::network::protocols::http2::headers.
| auto kcenon::network::protocols::http2::http2_client::post | ( | const std::string & | path, |
| const std::vector< uint8_t > & | body, | ||
| const std::vector< http_header > & | headers = {} ) -> Result<http2_response> |
Perform HTTP/2 POST request with binary body.
| path | Request path |
| body | Request body as bytes |
| headers | Additional headers (optional) |
Definition at line 260 of file http2_client.cpp.
References kcenon::network::protocols::http2::headers.
|
private |
Definition at line 618 of file http2_client.cpp.
References kcenon::network::protocols::http2::data, kcenon::network::error_void(), kcenon::network::protocols::http2::goaway, kcenon::network::protocols::http2::headers, kcenon::network::error_codes::common_errors::invalid_argument, kcenon::network::ok(), kcenon::network::protocols::http2::ping, kcenon::network::protocols::http2::rst_stream, kcenon::network::protocols::http2::settings, and kcenon::network::protocols::http2::window_update.

| auto kcenon::network::protocols::http2::http2_client::put | ( | const std::string & | path, |
| const std::string & | body, | ||
| const std::vector< http_header > & | headers = {} ) -> Result<http2_response> |
Perform HTTP/2 PUT request.
| path | Request path |
| body | Request body |
| headers | Additional headers (optional) |
Definition at line 268 of file http2_client.cpp.
References kcenon::network::protocols::http2::headers.
|
private |
Definition at line 569 of file http2_client.cpp.
References kcenon::network::error_codes::network_system::connection_closed, kcenon::network::error, kcenon::network::get_error_details(), kcenon::network::protocols::http2::frame::parse(), kcenon::network::protocols::http2::frame_header::parse(), and kcenon::network::error_codes::network_system::receive_failed.

|
private |
Definition at line 1125 of file http2_client.cpp.
|
private |
Definition at line 466 of file http2_client.cpp.
References kcenon::network::error_void(), kcenon::network::ok(), and kcenon::network::error_codes::network_system::send_failed.

|
private |
Definition at line 547 of file http2_client.cpp.
References kcenon::network::error_codes::network_system::connection_closed, kcenon::network::protocols::http2::data, kcenon::network::error_void(), kcenon::network::ok(), and kcenon::network::error_codes::network_system::send_failed.

|
private |
Definition at line 723 of file http2_client.cpp.
References kcenon::network::error_codes::network_system::connection_closed, kcenon::network::tracing::trace_context::create_span(), kcenon::network::error, kcenon::network::tracing::error, kcenon::network::get_error_details(), kcenon::network::protocols::http2::half_closed_local, kcenon::network::protocols::http2::headers, kcenon::network::tracing::is_tracing_enabled(), kcenon::network::ok(), kcenon::network::tracing::ok, kcenon::network::protocols::http2::open, kcenon::network::protocols::http2::http2_stream::promise, kcenon::network::protocols::http2::http2_stream::request_body, kcenon::network::protocols::http2::http2_stream::request_headers, kcenon::network::protocols::http2::http2_stream::state, kcenon::network::protocols::http2::http2_stream::stream_id, and kcenon::network::error_codes::common_errors::timeout.

|
private |
Definition at line 482 of file http2_client.cpp.
References kcenon::network::protocols::http2::enable_push, kcenon::network::protocols::http2::header_table_size, kcenon::network::protocols::http2::initial_window_size, kcenon::network::protocols::http2::max_concurrent_streams, kcenon::network::protocols::http2::max_frame_size, kcenon::network::protocols::http2::max_header_list_size, and kcenon::network::protocols::http2::settings.
|
private |
Definition at line 541 of file http2_client.cpp.
| auto kcenon::network::protocols::http2::http2_client::set_settings | ( | const http2_settings & | settings | ) | -> void |
Update local settings.
| settings | New settings to apply |
Definition at line 299 of file http2_client.cpp.
References kcenon::network::protocols::http2::settings.
| auto kcenon::network::protocols::http2::http2_client::set_timeout | ( | std::chrono::milliseconds | timeout | ) | -> void |
Set request timeout.
| timeout | Timeout duration |
Definition at line 284 of file http2_client.cpp.
| auto kcenon::network::protocols::http2::http2_client::start_stream | ( | const std::string & | path, |
| const std::vector< http_header > & | headers, | ||
| std::function< void(std::vector< uint8_t >)> | on_data, | ||
| std::function< void(std::vector< http_header >)> | on_headers, | ||
| std::function< void(int)> | on_complete ) -> Result<uint32_t> |
Start a streaming POST request.
| path | Request path |
| headers | Additional headers |
| on_data | Callback for each data chunk received |
| on_headers | Callback when headers are received |
| on_complete | Callback when stream completes (with status code) |
Use write_stream() to send data and close_stream_writer() when done.
Definition at line 306 of file http2_client.cpp.
References kcenon::network::error_codes::network_system::connection_closed, kcenon::network::error, kcenon::network::get_error_details(), kcenon::network::protocols::http2::headers, kcenon::network::protocols::http2::http2_stream::is_streaming, kcenon::network::ok(), kcenon::network::protocols::http2::http2_stream::on_complete, kcenon::network::protocols::http2::http2_stream::on_data, kcenon::network::protocols::http2::http2_stream::on_headers, kcenon::network::protocols::http2::open, kcenon::network::protocols::http2::http2_stream::request_headers, kcenon::network::protocols::http2::http2_stream::state, and kcenon::network::protocols::http2::http2_stream::stream_id.

|
private |
Definition at line 1154 of file http2_client.cpp.
| auto kcenon::network::protocols::http2::http2_client::write_stream | ( | uint32_t | stream_id, |
| const std::vector< uint8_t > & | data, | ||
| bool | end_stream = false ) -> VoidResult |
Write data to an open stream.
| stream_id | Stream identifier |
| data | Data to write |
| end_stream | Set to true to signal end of client data |
Definition at line 352 of file http2_client.cpp.
References kcenon::network::protocols::http2::closed, kcenon::network::error_codes::network_system::connection_closed, kcenon::network::protocols::http2::data, kcenon::network::error_void(), kcenon::network::protocols::http2::half_closed_local, kcenon::network::error_codes::common_errors::internal_error, kcenon::network::error_codes::common_errors::not_found, and kcenon::network::ok().

|
private |
Client identifier.
Definition at line 365 of file http2_client.h.
|
staticconstexprprivate |
Definition at line 402 of file http2_client.h.
|
private |
Definition at line 385 of file http2_client.h.
|
private |
Definition at line 393 of file http2_client.h.
|
staticconstexprprivate |
Definition at line 405 of file http2_client.h.
|
private |
Definition at line 392 of file http2_client.h.
|
staticconstexprprivate |
Definition at line 404 of file http2_client.h.
|
private |
|
private |
Connected host.
Definition at line 366 of file http2_client.h.
|
private |
Definition at line 370 of file http2_client.h.
|
private |
Definition at line 374 of file http2_client.h.
|
private |
Definition at line 377 of file http2_client.h.
Referenced by is_connected(), and ~http2_client().
|
private |
Definition at line 378 of file http2_client.h.
|
private |
Definition at line 388 of file http2_client.h.
Referenced by get_settings().
|
private |
|
private |
Connected port.
Definition at line 367 of file http2_client.h.
|
private |
Definition at line 399 of file http2_client.h.
|
private |
Definition at line 389 of file http2_client.h.
|
private |
Definition at line 372 of file http2_client.h.
|
private |
Definition at line 371 of file http2_client.h.
|
private |
Definition at line 383 of file http2_client.h.
|
private |
Definition at line 382 of file http2_client.h.
|
private |
|
private |
Definition at line 373 of file http2_client.h.