23#include <idfxx/http/types>
33#include <system_error>
127#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
233#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
342#ifdef CONFIG_HTTPD_WS_SUPPORT
365 std::span<const uint8_t> data;
368#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
460 std::string _status_str;
461 std::string _content_type;
462 std::vector<std::pair<std::string, std::string>> _resp_headers;
463 mutable bool _query_fetched =
false;
464 mutable std::optional<std::string> _query_cache;
521 size_t stack_size = 4096;
522 std::optional<core_id> core_affinity = std::nullopt;
533 size_t max_req_hdr_len = 0;
534 size_t max_uri_len = 0;
537 std::chrono::seconds recv_wait_timeout{5};
538 std::chrono::seconds send_wait_timeout{5};
541 bool lru_purge_enable =
false;
544 bool enable_so_linger =
false;
545 std::chrono::seconds linger_timeout{0};
548 bool keep_alive_enable =
false;
549 std::chrono::seconds keep_alive_idle{0};
550 std::chrono::seconds keep_alive_interval{0};
551 int keep_alive_count = 0;
554 bool wildcard_uri_match =
false;
558 std::move_only_function<result<void>(
int)
const> on_session_open = {};
560 std::move_only_function<
void(
int)
const> on_session_close = {};
563#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
600#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
783#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
808#ifdef CONFIG_HTTPD_WS_SUPPORT
817#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
853#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
892 std::unique_ptr<server_ctx>
ctx,
893 std::move_only_function<
result<void>(
int)
const> on_session_open,
894 std::move_only_function<
void(
int)
const> on_session_close,
901 template<
typename Config>
914 handler_type handler;
915#ifdef CONFIG_HTTPD_WS_SUPPORT
931 std::unique_ptr<server_ctx>
_ctx;
// end of idfxx_http_server
Non-owning view of an HTTP request.
int socket_fd() const
Returns the socket file descriptor for this request's connection.
result< size_t > try_recv(std::span< char > buf)
Receives raw request body data into a character buffer.
enum method method() const
Returns the HTTP method of this request.
request & operator=(request &&)=delete
void set_content_type(std::string_view content_type)
Sets the Content-Type response header.
request(request &&)=delete
void set_status(int code)
Sets the HTTP response status code.
void send(std::string_view body)
Sends a complete response with string body.
result< void > try_end_chunked()
Finalizes a chunked response.
void send_chunk(std::string_view chunk)
Sends a chunk of a chunked response.
result< void > try_send_chunk(std::string_view chunk)
Sends a chunk of a chunked response.
void send(std::span< const uint8_t > body)
Sends a complete response with binary body.
request & operator=(const request &)=delete
size_t content_length() const
Returns the content length of the request body.
size_t recv(std::span< char > buf)
Receives raw request body data into a character buffer.
request(const request &)=delete
void set_header(std::string_view field, std::string_view value)
Adds a response header.
void end_chunked()
Finalizes a chunked response.
result< std::string > try_recv_body()
Receives the entire request body as a string.
std::string recv_body()
Receives the entire request body as a string.
result< void > try_send(std::span< const uint8_t > body)
Sends a complete response with binary body.
result< void > try_send_error(int code, std::string_view message={})
Sends an HTTP error response.
void set_status(std::string_view status)
Sets the HTTP response status line.
std::optional< std::string > header(std::string_view field) const
Retrieves a request header value by name.
void send_error(int code, std::string_view message={})
Sends an HTTP error response.
size_t recv(std::span< uint8_t > buf)
Receives raw request body data into a buffer.
httpd_req_t * idf_handle() const noexcept
Returns the underlying ESP-IDF request handle.
std::string_view uri() const
Returns the URI of this request.
std::optional< std::string > query_param(std::string_view key) const
Retrieves a single query parameter value by key.
std::optional< std::string > query_string() const
Returns the full query string from the request URI.
result< size_t > try_recv(std::span< uint8_t > buf)
Receives raw request body data into a buffer.
result< void > try_send(std::string_view body)
Sends a complete response with string body.
Error category for HTTP server errors.
const char * name() const noexcept override final
Returns the name of the error category.
std::string message(int ec) const override final
Returns a human-readable message for the given error code.
HTTP server with URI handler registration and RAII lifecycle.
httpd_handle_t idf_handle() const noexcept
Returns the underlying ESP-IDF HTTP server handle.
void on_delete(std::string uri, handler_type handler)
Registers a DELETE handler.
void on_put(std::string uri, handler_type handler)
Registers a PUT handler.
result< void > try_on_any(std::string uri, handler_type handler)
Registers a URI handler that matches any HTTP method.
static result< server > make(config cfg)
Creates and starts an HTTP server.
std::move_only_function< result< void >(request &) const > handler_type
Handler function type for URI handlers.
result< void > try_close_session(int sockfd)
Closes a client session by socket file descriptor.
result< void > try_on_head(std::string uri, handler_type handler)
Registers a HEAD handler.
result< void > try_on_get(std::string uri, handler_type handler)
Registers a GET handler.
void on_head(std::string uri, handler_type handler)
Registers a HEAD handler.
server(const server &)=delete
virtual ~server()
Stops the server and releases all resources.
server(server &&) noexcept
Move constructor.
void on_any(std::string uri, handler_type handler)
Registers a URI handler that matches any HTTP method.
errc
Error codes for HTTP server operations.
@ resp_send
Error sending response.
@ resp_header
Response header field too large.
@ handler_exists
URI handler already registered for this method and URI.
@ invalid_request
Invalid request pointer.
@ result_truncated
Result string was truncated.
@ handlers_full
All URI handler slots are full.
@ alloc_mem
Memory allocation failed.
result< void > try_on(enum method m, std::string uri, handler_type handler)
Registers a URI handler for a specific HTTP method.
void on_patch(std::string uri, handler_type handler)
Registers a PATCH handler.
void on_get(std::string uri, handler_type handler)
Registers a GET handler.
void unregister_handler(enum method m, std::string_view uri)
Unregisters a URI handler.
result< void > try_on_delete(std::string uri, handler_type handler)
Registers a DELETE handler.
result< void > try_on_put(std::string uri, handler_type handler)
Registers a PUT handler.
void close_session(int sockfd)
Closes a client session by socket file descriptor.
server & operator=(const server &)=delete
result< void > try_on_patch(std::string uri, handler_type handler)
Registers a PATCH handler.
void on(enum method m, std::string uri, handler_type handler)
Registers a URI handler for a specific HTTP method.
void on_post(std::string uri, handler_type handler)
Registers a POST handler.
result< void > try_unregister_handler(enum method m, std::string_view uri)
Unregisters a URI handler.
result< void > try_on_post(std::string uri, handler_type handler)
Registers a POST handler.
server(config cfg)
Creates and starts an HTTP server.
Type-safe wrapper for FreeRTOS task priority values.
Task lifecycle management.
struct httpd_req httpd_req_t
method
HTTP request methods.
@ delete_
DELETE (trailing underscore — C++ keyword)
const server::error_category & http_server_category() noexcept
Returns a reference to the HTTP server error category singleton.
std::error_code make_error_code(client::errc e) noexcept
Creates an error code from an idfxx::http::client::errc value.
std::unexpected< std::error_code > http_server_error(esp_err_t e)
Creates an unexpected error from an ESP-IDF error code, mapping to HTTP server error codes where poss...
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
HTTP server configuration.