|
idfxx 1.0.0
Modern C++23 components for ESP-IDF
|
HTTP client with blocking and streaming request support. More...
Classes | |
| struct | config |
| HTTP client configuration. More... | |
| class | error_category |
| Error category for HTTP client errors. More... | |
Public Types | |
| enum class | errc : esp_err_t { max_redirect = 0x7001 , connect = 0x7002 , write_data = 0x7003 , fetch_header = 0x7004 , invalid_transport = 0x7005 , connecting = 0x7006 , eagain = 0x7007 , connection_closed = 0x7008 } |
| Error codes for HTTP client operations. More... | |
Public Member Functions | |
| client (config cfg) | |
| Creates an HTTP client from configuration. | |
| ~client () | |
| Destroys the client and releases all resources. | |
| client (const client &)=delete | |
| client & | operator= (const client &)=delete |
| client (client &&) noexcept | |
| Move constructor. | |
| client & | operator= (client &&) noexcept |
| Move assignment. | |
| void | set_url (std::string_view url) |
| Sets the request URL. | |
| result< void > | try_set_url (std::string_view url) |
| Sets the request URL. | |
| void | set_method (enum method m) |
| Sets the HTTP method. | |
| void | set_header (std::string_view key, std::string_view value) |
| Sets a request header. | |
| void | delete_header (std::string_view key) |
| Deletes a request header. | |
| void | set_post_field (std::string_view data) |
| Sets the POST request body. | |
| void | set_username (std::string_view username) |
| Sets the username for authentication. | |
| void | set_password (std::string_view password) |
| Sets the password for authentication. | |
| void | set_auth_type (enum auth_type type) |
| Sets the authentication type. | |
| template<typename Rep , typename Period > | |
| void | set_timeout (const std::chrono::duration< Rep, Period > &timeout) |
| Sets the request timeout. | |
| void | perform () |
| Performs a blocking HTTP request. | |
| result< void > | try_perform () |
| Performs a blocking HTTP request. | |
| void | open (size_t write_len=0) |
| Opens a connection for streaming. | |
| void | open (int write_len) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| size_t | write (std::span< const uint8_t > data) |
| Writes data to the open connection. | |
| size_t | write (std::string_view data) |
| Writes string data to the open connection. | |
| int64_t | fetch_headers () |
| Reads and processes response headers. | |
| size_t | read (std::span< uint8_t > buf) |
| Reads response body data. | |
| size_t | read (std::span< char > buf) |
| Reads response body data into a character buffer. | |
| void | flush_response () |
| Discards any remaining response data. | |
| void | close () |
| Closes the connection. | |
| result< void > | try_open (size_t write_len=0) |
| Opens a connection for streaming. | |
| result< void > | try_open (int write_len) |
| This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
| result< size_t > | try_write (std::span< const uint8_t > data) |
| Writes data to the open connection. | |
| result< size_t > | try_write (std::string_view data) |
| Writes string data to the open connection. | |
| result< int64_t > | try_fetch_headers () |
| Reads and processes response headers. | |
| result< size_t > | try_read (std::span< uint8_t > buf) |
| Reads response body data. | |
| result< size_t > | try_read (std::span< char > buf) |
| Reads response body data into a character buffer. | |
| result< void > | try_flush_response () |
| Discards any remaining response data. | |
| result< void > | try_close () |
| Closes the connection. | |
| int | status_code () const |
| Returns the HTTP response status code. | |
| int64_t | content_length () const |
| Returns the response content length. | |
| bool | is_chunked_response () const |
| Checks whether the response uses chunked transfer encoding. | |
| std::optional< std::string > | get_header (std::string_view key) const |
| Retrieves a response header value. | |
| std::string | get_url () const |
| Retrieves the current effective URL. | |
| void | set_redirection () |
| Applies the current redirect URL. | |
| result< void > | try_set_redirection () |
| Applies the current redirect URL. | |
| void | reset_redirect_counter () |
| Resets the redirect counter to zero. | |
| esp_http_client_handle_t | idf_handle () const noexcept |
| Returns the underlying ESP-IDF HTTP client handle. | |
Static Public Member Functions | |
| static result< client > | make (config cfg) |
| Creates an HTTP client from configuration. | |
HTTP client with blocking and streaming request support.
Manages an HTTP session with support for TLS, authentication, custom headers, redirects, and event callbacks. Provides both a simple blocking perform() API and a streaming open()/write()/read()/close() API.
Not thread-safe — callers must manage their own synchronization.
Definition at line 77 of file client.hpp.
|
strong |
Error codes for HTTP client operations.
Definition at line 82 of file client.hpp.
|
explicit |
Creates an HTTP client from configuration.
| cfg | Client configuration. |
| std::system_error | on failure. |
| idfxx::http::client::~client | ( | ) |
Destroys the client and releases all resources.
|
noexcept |
Move constructor.
Transfers handle ownership.
|
inline |
Closes the connection.
| std::system_error | on failure. |
Definition at line 415 of file client.hpp.
References try_close(), and idfxx::unwrap().
| int64_t idfxx::http::client::content_length | ( | ) | const |
Returns the response content length.
| void idfxx::http::client::delete_header | ( | std::string_view | key | ) |
Deletes a request header.
| key | Header name to remove. |
|
inline |
Reads and processes response headers.
Must be called after open() (and any write() calls) before reading response body data.
| std::system_error | on failure. |
Definition at line 379 of file client.hpp.
References try_fetch_headers(), and idfxx::unwrap().
|
inline |
Discards any remaining response data.
| std::system_error | on failure. |
Definition at line 407 of file client.hpp.
References try_flush_response(), and idfxx::unwrap().
| std::optional< std::string > idfxx::http::client::get_header | ( | std::string_view | key | ) | const |
Retrieves a response header value.
| key | Header name to look up. |
| std::string idfxx::http::client::get_url | ( | ) | const |
Retrieves the current effective URL.
May differ from the originally configured URL after redirects.
|
inlinenoexcept |
Returns the underlying ESP-IDF HTTP client handle.
Definition at line 571 of file client.hpp.
| bool idfxx::http::client::is_chunked_response | ( | ) | const |
Checks whether the response uses chunked transfer encoding.
Creates an HTTP client from configuration.
| cfg | Client configuration. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 347 of file client.hpp.
References open().
Referenced by open().
Opens a connection for streaming.
Opens the HTTP connection and sends request headers. If write_len is greater than zero, the connection is opened for writing that many bytes before reading the response.
| write_len | Number of bytes to write (0 for read-only requests). |
| std::system_error | if write_len exceeds INT_MAX or the connection fails. |
Definition at line 344 of file client.hpp.
References try_open(), and idfxx::unwrap().
Move assignment.
Transfers handle ownership.
|
inline |
Performs a blocking HTTP request.
Sends the request and reads the complete response. Use event callbacks or the streaming API to process response data.
| std::system_error | on failure. |
Definition at line 315 of file client.hpp.
References try_perform(), and idfxx::unwrap().
Reads response body data into a character buffer.
| buf | Buffer to read into. |
| std::system_error | on failure. |
Definition at line 399 of file client.hpp.
References try_read(), and idfxx::unwrap().
Reads response body data.
| buf | Buffer to read into. |
| std::system_error | on failure. |
Definition at line 389 of file client.hpp.
References try_read(), and idfxx::unwrap().
| void idfxx::http::client::reset_redirect_counter | ( | ) |
Resets the redirect counter to zero.
Allows additional redirects up to max_redirection_count.
Sets the authentication type.
| type | The authentication type. |
| void idfxx::http::client::set_header | ( | std::string_view | key, |
| std::string_view | value | ||
| ) |
Sets a request header.
Throws std::bad_alloc (or aborts) on allocation failure.
| key | Header name. |
| value | Header value. |
Sets the HTTP method.
| m | The HTTP method. |
| void idfxx::http::client::set_password | ( | std::string_view | password | ) |
Sets the password for authentication.
| password | The password. |
| void idfxx::http::client::set_post_field | ( | std::string_view | data | ) |
Sets the POST request body.
The client takes ownership of a copy of the data, keeping it alive for the duration of the request. Throws std::bad_alloc (or aborts) on allocation failure.
| data | POST body content. |
|
inline |
Applies the current redirect URL.
Sets the client URL to the redirect target from the Location header. Typically called from a redirect event callback when automatic redirect is disabled.
| std::system_error | on failure. |
Definition at line 546 of file client.hpp.
References try_set_redirection(), and idfxx::unwrap().
|
inline |
Sets the request timeout.
| Rep | Duration representation type. |
| Period | Duration period type. |
| timeout | The timeout duration. |
Definition at line 291 of file client.hpp.
References idfxx::timeout.
|
inline |
Sets the request URL.
| url | The URL string. |
| std::system_error | on failure. |
Definition at line 215 of file client.hpp.
References try_set_url(), and idfxx::unwrap().
| void idfxx::http::client::set_username | ( | std::string_view | username | ) |
Sets the username for authentication.
| username | The username. |
| int idfxx::http::client::status_code | ( | ) | const |
Returns the HTTP response status code.
Reads and processes response headers.
Must be called after open() (and any write() calls) before reading response body data.
Referenced by fetch_headers().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 432 of file client.hpp.
References try_open().
Referenced by try_open().
Opens a connection for streaming.
Opens the HTTP connection and sends request headers. If write_len is greater than zero, the connection is opened for writing that many bytes before reading the response.
| write_len | Number of bytes to write (0 for read-only requests). |
| idfxx::errc::invalid_arg | if write_len exceeds INT_MAX. |
Referenced by open().
Performs a blocking HTTP request.
Sends the request and reads the complete response. Use event callbacks or the streaming API to process response data.
Referenced by perform().
Reads response body data into a character buffer.
| buf | Buffer to read into. |
Applies the current redirect URL.
Sets the client URL to the redirect target from the Location header. Typically called from a redirect event callback when automatic redirect is disabled.
Referenced by set_redirection().
Sets the request URL.
| url | The URL string. |
Referenced by set_url().
Writes string data to the open connection.
| data | String data to write. |
Writes data to the open connection.
| data | Binary data to write. |
| std::system_error | on failure. |
Definition at line 357 of file client.hpp.
References try_write(), and idfxx::unwrap().
|
inline |
Writes string data to the open connection.
| data | String data to write. |
| std::system_error | on failure. |
Definition at line 367 of file client.hpp.
References try_write(), and idfxx::unwrap().