From 1dd9ead08dde6b9519e5feea501c80dff9ea38d5 Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Sun, 5 Nov 2023 12:23:10 +0800 Subject: [PATCH] Fix GET / HEAD when resource is a directory, Method is not allowed so return 405 Update documentation for GET/HEAD --- docs/WebDavService.md | 6 +- .../http/handlers/webdav/esp3d_webdav_get.cpp | 64 ++++++++++--------- .../handlers/webdav/esp3d_webdav_head.cpp | 4 ++ 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/docs/WebDavService.md b/docs/WebDavService.md index 180feb92..80a93196 100644 --- a/docs/WebDavService.md +++ b/docs/WebDavService.md @@ -34,7 +34,8 @@ The content of the reponse is: - empty if the resource is a directory The response code is: -- 200 if the resource is a file or a directory and the request was successful +- 200 if the resource is a file and the request was successful +- 405 if the resource is a directory - 404 if the resource does not exist - 500 if any error during the file streaming - 503 if any error accessing the local file system (e.g. access denied) @@ -51,8 +52,9 @@ The necessary headers in response are: Unlike GET method, the HEAD method does not return the content of the resource. The response code is: -- 200 if the resource is a file or a directory and the request was successful +- 200 if the resource is a file and the request was successful - 404 if the resource does not exist +- 405 if the resource is a directory - 500 if any error during the file streaming - 503 if any error accessing the local file system (e.g. access denied) diff --git a/main/modules/http/handlers/webdav/esp3d_webdav_get.cpp b/main/modules/http/handlers/webdav/esp3d_webdav_get.cpp index e3a331e8..f5869d43 100644 --- a/main/modules/http/handlers/webdav/esp3d_webdav_get.cpp +++ b/main/modules/http/handlers/webdav/esp3d_webdav_get.cpp @@ -74,40 +74,46 @@ esp_err_t ESP3DHttpService::webdav_get_handler(httpd_req_t *req) { // Add Content-Length header httpd_resp_set_hdr(req, "Content-Length", std::to_string(file_size).c_str()); - } - // open file - FILE *fd = globalFs.open(uri.c_str(), "r"); - if (fd) { - size_t chunksize; - size_t total_send = 0; - // send file - do { - // Read data block from the file - chunksize = fread(_chunk, 1, CHUNK_BUFFER_SIZE, fd); - total_send += chunksize; - if (chunksize > 0) { - // Send the HTTP data block - if (httpd_resp_send_chunk(req, _chunk, chunksize) != ESP_OK) { - esp3d_log_e("File sending failed!"); - chunksize = 0; - response_code = 500; - response_msg = "Failed to send file"; + + // open file + FILE *fd = globalFs.open(uri.c_str(), "r"); + if (fd) { + size_t chunksize; + size_t total_send = 0; + // send file + do { + // Read data block from the file + chunksize = fread(_chunk, 1, CHUNK_BUFFER_SIZE, fd); + total_send += chunksize; + if (chunksize > 0) { + // Send the HTTP data block + if (httpd_resp_send_chunk(req, _chunk, chunksize) != ESP_OK) { + esp3d_log_e("File sending failed!"); + chunksize = 0; + response_code = 500; + response_msg = "Failed to send file"; + } } + } while (chunksize != 0); + // Close the file + fclose(fd); + httpd_resp_send_chunk(req, NULL, 0); + // Check if all the file has been sent + if (total_send != file_size) { + esp3d_log_e("File sending failed: size do not match!"); + response_code = 500; + response_msg = "File sending failed: size do not match!"; } - } while (chunksize != 0); - // Close the file - fclose(fd); - httpd_resp_send_chunk(req, NULL, 0); - // Check if all the file has been sent - if (total_send != file_size) { - esp3d_log_e("File sending failed: size do not match!"); + } else { + esp3d_log_e("Failed to open file"); response_code = 500; - response_msg = "File sending failed: size do not match!"; + response_msg = "Failed to open file"; } } else { - esp3d_log_e("Failed to open file"); - response_code = 500; - response_msg = "Failed to open file"; + // is directory + esp3d_log_e("This is not a file"); + response_code = 405; + response_msg = "This is not a file"; } } // release access diff --git a/main/modules/http/handlers/webdav/esp3d_webdav_head.cpp b/main/modules/http/handlers/webdav/esp3d_webdav_head.cpp index ff37b278..da4ae1b4 100644 --- a/main/modules/http/handlers/webdav/esp3d_webdav_head.cpp +++ b/main/modules/http/handlers/webdav/esp3d_webdav_head.cpp @@ -73,6 +73,10 @@ esp_err_t ESP3DHttpService::webdav_head_handler(httpd_req_t *req) { // Add Content-Length header httpd_resp_set_hdr(req, "Content-Length", std::to_string(file_size).c_str()); + } else { + response_code = 405; + response_msg = "It is not a file"; + esp3d_log_e("It is not a file"); } } // release access