feat: support HTTP_ANY method in esp_http_server

Closes https://github.com/espressif/esp-idf/issues/12794
pull/13090/head
Harshit Malpani 2024-01-09 11:27:15 +05:30
rodzic 7be04869d2
commit 1d81af367d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 441A8ACC7853D493
3 zmienionych plików z 28 dodań i 3 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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

Wyświetl plik

@ -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