|
idfxx 1.0.0
Modern C++23 components for ESP-IDF
|
Non-owning view of an HTTP request. More...
Public Member Functions | |
| request (const request &)=delete | |
| request & | operator= (const request &)=delete |
| request (request &&)=delete | |
| request & | operator= (request &&)=delete |
| enum method | method () const |
| Returns the HTTP method of this request. | |
| std::string_view | uri () const |
| Returns the URI of this request. | |
| size_t | content_length () const |
| Returns the content length of the request body. | |
| int | socket_fd () const |
| Returns the socket file descriptor for this request's connection. | |
| std::optional< std::string > | header (std::string_view field) const |
| Retrieves a request header value by name. | |
| std::optional< std::string > | query_string () const |
| Returns the full query string from the request URI. | |
| std::optional< std::string > | query_param (std::string_view key) const |
| Retrieves a single query parameter value by key. | |
| size_t | recv (std::span< uint8_t > buf) |
| Receives raw request body data into a buffer. | |
| size_t | recv (std::span< char > buf) |
| Receives raw request body data into a character buffer. | |
| std::string | recv_body () |
| Receives the entire request body as a string. | |
| result< size_t > | try_recv (std::span< uint8_t > buf) |
| Receives raw request body data into a buffer. | |
| result< size_t > | try_recv (std::span< char > buf) |
| Receives raw request body data into a character buffer. | |
| result< std::string > | try_recv_body () |
| Receives the entire request body as a string. | |
| void | set_status (std::string_view status) |
| Sets the HTTP response status line. | |
| void | set_status (int code) |
| Sets the HTTP response status code. | |
| void | set_content_type (std::string_view content_type) |
| Sets the Content-Type response header. | |
| void | set_header (std::string_view field, std::string_view value) |
| Adds a response header. | |
| void | send (std::string_view body) |
| Sends a complete response with string body. | |
| void | send (std::span< const uint8_t > body) |
| Sends a complete response with binary body. | |
| void | send_chunk (std::string_view chunk) |
| Sends a chunk of a chunked response. | |
| void | end_chunked () |
| Finalizes a chunked response. | |
| void | send_error (int code, std::string_view message={}) |
| Sends an HTTP error response. | |
| result< void > | try_send (std::string_view body) |
| Sends a complete response with string body. | |
| result< void > | try_send (std::span< const uint8_t > body) |
| Sends a complete response with binary body. | |
| result< void > | try_send_chunk (std::string_view chunk) |
| Sends a chunk of a chunked response. | |
| result< void > | try_end_chunked () |
| Finalizes a chunked response. | |
| result< void > | try_send_error (int code, std::string_view message={}) |
| Sends an HTTP error response. | |
| httpd_req_t * | idf_handle () const noexcept |
| Returns the underlying ESP-IDF request handle. | |
Friends | |
| class | server |
Non-owning view of an HTTP request.
Constructed on the stack within the C-to-C++ trampoline and passed by reference to handlers. Provides methods for reading request data (headers, query parameters, body) and sending responses.
This class must not outlive the handler invocation. It is non-copyable and non-movable to prevent accidental use-after-callback.
Definition at line 58 of file server.hpp.
|
delete |
| size_t idfxx::http::request::content_length | ( | ) | const |
Returns the content length of the request body.
|
inline |
Finalizes a chunked response.
Sends the terminal empty chunk, signalling end of response.
| std::system_error | on failure. |
Definition at line 280 of file server.hpp.
References try_end_chunked(), and idfxx::unwrap().
| std::optional< std::string > idfxx::http::request::header | ( | std::string_view | field | ) | const |
Retrieves a request header value by name.
| field | Header name to look up. |
|
inlinenoexcept |
Returns the underlying ESP-IDF request handle.
Definition at line 452 of file server.hpp.
Returns the HTTP method of this request.
| std::optional< std::string > idfxx::http::request::query_param | ( | std::string_view | key | ) | const |
Retrieves a single query parameter value by key.
| key | The query parameter name. |
| std::optional< std::string > idfxx::http::request::query_string | ( | ) | const |
Returns the full query string from the request URI.
Receives raw request body data into a character buffer.
| buf | Buffer to receive data into. |
| std::system_error | on failure. |
Definition at line 146 of file server.hpp.
References try_recv(), and idfxx::unwrap().
Receives raw request body data into a buffer.
| buf | Buffer to receive data into. |
| std::system_error | on failure. |
Definition at line 136 of file server.hpp.
References try_recv(), and idfxx::unwrap().
|
inline |
Receives the entire request body as a string.
Reads all remaining body data up to content_length() bytes.
| std::system_error | on failure. |
Definition at line 157 of file server.hpp.
References try_recv_body(), and idfxx::unwrap().
Sends a complete response with binary body.
| body | The response body data. |
| std::system_error | on failure. |
Definition at line 258 of file server.hpp.
References try_send(), and idfxx::unwrap().
|
inline |
Sends a complete response with string body.
| body | The response body string. |
| std::system_error | on failure. |
Definition at line 249 of file server.hpp.
References try_send(), and idfxx::unwrap().
|
inline |
Sends a chunk of a chunked response.
Call repeatedly to stream data to the client. Call end_chunked() to finalize the chunked response.
| chunk | The chunk data to send. |
| std::system_error | on failure. |
Definition at line 270 of file server.hpp.
References try_send_chunk(), and idfxx::unwrap().
Sends an HTTP error response.
| code | The HTTP error status code (e.g. 404, 500). |
| message | Optional error message body. |
| std::system_error | on failure. |
Definition at line 290 of file server.hpp.
| void idfxx::http::request::set_content_type | ( | std::string_view | content_type | ) |
Sets the Content-Type response header.
The string is copied internally and kept alive until the response is sent.
| content_type | The MIME type string (e.g. "application/json"). |
| void idfxx::http::request::set_header | ( | std::string_view | field, |
| std::string_view | value | ||
| ) |
Adds a response header.
Both field and value are copied internally and kept alive until the response is sent.
| field | Header name. |
| value | Header value. |
Sets the HTTP response status code.
Maps common status codes to their standard reason phrases. Falls back to just the numeric code for unrecognized values.
| code | The HTTP status code (e.g. 200, 404). |
| void idfxx::http::request::set_status | ( | std::string_view | status | ) |
Sets the HTTP response status line.
The string must remain in the format "NNN Reason Phrase" (e.g. "200 OK"). The string is copied internally and kept alive until the response is sent.
| status | The status string (e.g. "200 OK"). |
| int idfxx::http::request::socket_fd | ( | ) | const |
Returns the socket file descriptor for this request's connection.
Finalizes a chunked response.
Sends the terminal empty chunk, signalling end of response.
Referenced by end_chunked().
Receives raw request body data into a character buffer.
| buf | Buffer to receive data into. |
| result< std::string > idfxx::http::request::try_recv_body | ( | ) |
Receives the entire request body as a string.
Reads all remaining body data up to content_length() bytes.
Referenced by recv_body().
Sends a complete response with binary body.
| body | The response body data. |
Sends a chunk of a chunked response.
Call repeatedly to stream data to the client. Call try_end_chunked() to finalize the chunked response.
| chunk | The chunk data to send. |
Referenced by send_chunk().
Sends an HTTP error response.
| code | The HTTP error status code (e.g. 404, 500). |
| message | Optional error message body. |
| std::string_view idfxx::http::request::uri | ( | ) | const |
Returns the URI of this request.
Definition at line 455 of file server.hpp.