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; }