Skip to content

Commit

Permalink
Fix GET / HEAD when resource is a directory, Method is not allowed so…
Browse files Browse the repository at this point in the history
… return 405

Update documentation for GET/HEAD
  • Loading branch information
luc-github committed Nov 5, 2023
1 parent 4ecf0dc commit 1dd9ead
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
6 changes: 4 additions & 2 deletions docs/WebDavService.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
64 changes: 35 additions & 29 deletions main/modules/http/handlers/webdav/esp3d_webdav_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions main/modules/http/handlers/webdav/esp3d_webdav_head.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1dd9ead

Please sign in to comment.