From 1d81af367d9d44f3bddf8ee3c0f9c53315fe976c Mon Sep 17 00:00:00 2001 From: Harshit Malpani Date: Tue, 9 Jan 2024 11:27:15 +0530 Subject: [PATCH] feat: support HTTP_ANY method in esp_http_server Closes https://github.com/espressif/esp-idf/issues/12794 --- .../esp_http_server/include/esp_http_server.h | 6 +++-- components/esp_http_server/src/httpd_uri.c | 2 +- .../protocols/http_server/simple/main/main.c | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/components/esp_http_server/include/esp_http_server.h b/components/esp_http_server/include/esp_http_server.h index 6b0e789bd4..b6975fac00 100644 --- a/components/esp_http_server/include/esp_http_server.h +++ b/components/esp_http_server/include/esp_http_server.h @@ -112,6 +112,8 @@ typedef void* httpd_handle_t; */ typedef enum http_method httpd_method_t; +#define HTTP_ANY INT_MAX + /** * @brief Prototype for freeing context data (if any) * @param[in] ctx object to free @@ -367,7 +369,7 @@ esp_err_t httpd_stop(httpd_handle_t handle); */ typedef struct httpd_req { httpd_handle_t handle; /*!< Handle to server instance */ - int method; /*!< The type of HTTP request, -1 if unsupported method */ + int method; /*!< The type of HTTP request, -1 if unsupported method, HTTP_ANY for wildcard method to support every method */ const char uri[HTTPD_MAX_URI_LEN + 1]; /*!< The URI of this request (1 byte extra for null termination) */ size_t content_len; /*!< Length of the request body */ void *aux; /*!< Internally used members */ @@ -423,7 +425,7 @@ typedef struct httpd_req { */ typedef struct httpd_uri { const char *uri; /*!< The URI to handle */ - httpd_method_t method; /*!< Method supported by the URI */ + httpd_method_t method; /*!< Method supported by the URI, HTTP_ANY for wildcard method to support all methods*/ /** * Handler to call for supported request method. This must diff --git a/components/esp_http_server/src/httpd_uri.c b/components/esp_http_server/src/httpd_uri.c index 44d173f6cc..5a871902ca 100644 --- a/components/esp_http_server/src/httpd_uri.c +++ b/components/esp_http_server/src/httpd_uri.c @@ -103,7 +103,7 @@ static httpd_uri_t* httpd_find_uri_handler(struct httpd_data *hd, hd->config.uri_match_fn(hd->hd_calls[i]->uri, uri, uri_len) : httpd_uri_match_simple(hd->hd_calls[i]->uri, uri, uri_len)) { /* URIs match. Now check if method is supported */ - if (hd->hd_calls[i]->method == method) { + if (hd->hd_calls[i]->method == method || hd->hd_calls[i]->method == HTTP_ANY) { /* Match found! */ if (err) { /* Unset any error that may diff --git a/examples/protocols/http_server/simple/main/main.c b/examples/protocols/http_server/simple/main/main.c index d23969c591..d3dfffe497 100644 --- a/examples/protocols/http_server/simple/main/main.c +++ b/examples/protocols/http_server/simple/main/main.c @@ -300,6 +300,28 @@ static const httpd_uri_t echo = { .user_ctx = NULL }; +/* An HTTP_ANY handler */ +static esp_err_t any_handler(httpd_req_t *req) +{ + /* Send response with body set as the + * string passed in user context*/ + const char* resp_str = (const char*) req->user_ctx; + httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN); + + // End response + httpd_resp_send_chunk(req, NULL, 0); + return ESP_OK; +} + +static const httpd_uri_t any = { + .uri = "/any", + .method = HTTP_ANY, + .handler = any_handler, + /* Let's pass response string in user + * context to demonstrate it's usage */ + .user_ctx = "Hello World!" +}; + /* This handler allows the custom error handling functionality to be * tested from client side. For that, when a PUT request 0 is sent to * URI /ctrl, the /hello and /echo URIs are unregistered and following @@ -391,6 +413,7 @@ static httpd_handle_t start_webserver(void) httpd_register_uri_handler(server, &hello); httpd_register_uri_handler(server, &echo); httpd_register_uri_handler(server, &ctrl); + httpd_register_uri_handler(server, &any); #if CONFIG_EXAMPLE_BASIC_AUTH httpd_register_basic_auth(server); #endif