From 6f91d408ab65e262d2597eb9979f6f5f762d1d03 Mon Sep 17 00:00:00 2001 From: Shubham Kulkarni Date: Fri, 8 Jan 2021 11:48:52 +0530 Subject: [PATCH] file_serving example: Demonstrate use of connection close header from request headers Closes: https://github.com/espressif/esp-idf/issues/5775 --- .../http_server/file_serving/main/Kconfig.projbuild | 6 ++++++ .../http_server/file_serving/main/file_server.c | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/examples/protocols/http_server/file_serving/main/Kconfig.projbuild b/examples/protocols/http_server/file_serving/main/Kconfig.projbuild index 537cf2ee38..e0832bdc99 100644 --- a/examples/protocols/http_server/file_serving/main/Kconfig.projbuild +++ b/examples/protocols/http_server/file_serving/main/Kconfig.projbuild @@ -21,4 +21,10 @@ menu "Http_File_Serving Example menu" If this config item is set, SDMMC is used to mount the SDcard. Otherwise, will use SPI host to access and mount the SDcard. + config EXAMPLE_HTTPD_CONN_CLOSE_HEADER + bool "Send connection close header from request handlers" + default y + help + If this config item is set, Connection: close header will be set in handlers. + This closes HTTP connection and frees the server socket instantly. endmenu diff --git a/examples/protocols/http_server/file_serving/main/file_server.c b/examples/protocols/http_server/file_serving/main/file_server.c index 178172fed3..878fdab4d0 100644 --- a/examples/protocols/http_server/file_serving/main/file_server.c +++ b/examples/protocols/http_server/file_serving/main/file_server.c @@ -277,6 +277,9 @@ static esp_err_t download_get_handler(httpd_req_t *req) ESP_LOGI(TAG, "File sending complete"); /* Respond with an empty chunk to signal HTTP response completion */ +#ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER + httpd_resp_set_hdr(req, "Connection", "close"); +#endif httpd_resp_send_chunk(req, NULL, 0); return ESP_OK; } @@ -388,6 +391,9 @@ static esp_err_t upload_post_handler(httpd_req_t *req) /* Redirect onto root to see the updated file list */ httpd_resp_set_status(req, "303 See Other"); httpd_resp_set_hdr(req, "Location", "/"); +#ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER + httpd_resp_set_hdr(req, "Connection", "close"); +#endif httpd_resp_sendstr(req, "File uploaded successfully"); return ESP_OK; } @@ -429,6 +435,9 @@ static esp_err_t delete_post_handler(httpd_req_t *req) /* Redirect onto root to see the updated file list */ httpd_resp_set_status(req, "303 See Other"); httpd_resp_set_hdr(req, "Location", "/"); +#ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER + httpd_resp_set_hdr(req, "Connection", "close"); +#endif httpd_resp_sendstr(req, "File deleted successfully"); return ESP_OK; }