From 9ef8cafecaa8d3c599e2cbc01b018b9d5e101414 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Mon, 23 Dec 2019 12:01:16 +0530 Subject: [PATCH] file_server: fix issue with sending last chunk Closes: https://github.com/espressif/esp-idf/issues/4528 Closes IDFGH-2414 --- .../file_serving/main/file_server.c | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) 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 11fb9f3548..178172fed3 100644 --- a/examples/protocols/http_server/file_serving/main/file_server.c +++ b/examples/protocols/http_server/file_serving/main/file_server.c @@ -256,15 +256,17 @@ static esp_err_t download_get_handler(httpd_req_t *req) /* Read file in chunks into the scratch buffer */ chunksize = fread(chunk, 1, SCRATCH_BUFSIZE, fd); - /* Send the buffer contents as HTTP response chunk */ - if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) { - fclose(fd); - ESP_LOGE(TAG, "File sending failed!"); - /* Abort sending file */ - httpd_resp_sendstr_chunk(req, NULL); - /* Respond with 500 Internal Server Error */ - httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file"); - return ESP_FAIL; + if (chunksize > 0) { + /* Send the buffer contents as HTTP response chunk */ + if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) { + fclose(fd); + ESP_LOGE(TAG, "File sending failed!"); + /* Abort sending file */ + httpd_resp_sendstr_chunk(req, NULL); + /* Respond with 500 Internal Server Error */ + httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file"); + return ESP_FAIL; + } } /* Keep looping till the whole file is sent */