Skip to content

Commit 1dd9ead

Browse files
committed
Fix GET / HEAD when resource is a directory, Method is not allowed so return 405
Update documentation for GET/HEAD
1 parent 4ecf0dc commit 1dd9ead

File tree

3 files changed

+43
-31
lines changed

3 files changed

+43
-31
lines changed

docs/WebDavService.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ The content of the reponse is:
3434
- empty if the resource is a directory
3535

3636
The response code is:
37-
- 200 if the resource is a file or a directory and the request was successful
37+
- 200 if the resource is a file and the request was successful
38+
- 405 if the resource is a directory
3839
- 404 if the resource does not exist
3940
- 500 if any error during the file streaming
4041
- 503 if any error accessing the local file system (e.g. access denied)
@@ -51,8 +52,9 @@ The necessary headers in response are:
5152
Unlike GET method, the HEAD method does not return the content of the resource.
5253

5354
The response code is:
54-
- 200 if the resource is a file or a directory and the request was successful
55+
- 200 if the resource is a file and the request was successful
5556
- 404 if the resource does not exist
57+
- 405 if the resource is a directory
5658
- 500 if any error during the file streaming
5759
- 503 if any error accessing the local file system (e.g. access denied)
5860

main/modules/http/handlers/webdav/esp3d_webdav_get.cpp

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,40 +74,46 @@ esp_err_t ESP3DHttpService::webdav_get_handler(httpd_req_t *req) {
7474
// Add Content-Length header
7575
httpd_resp_set_hdr(req, "Content-Length",
7676
std::to_string(file_size).c_str());
77-
}
78-
// open file
79-
FILE *fd = globalFs.open(uri.c_str(), "r");
80-
if (fd) {
81-
size_t chunksize;
82-
size_t total_send = 0;
83-
// send file
84-
do {
85-
// Read data block from the file
86-
chunksize = fread(_chunk, 1, CHUNK_BUFFER_SIZE, fd);
87-
total_send += chunksize;
88-
if (chunksize > 0) {
89-
// Send the HTTP data block
90-
if (httpd_resp_send_chunk(req, _chunk, chunksize) != ESP_OK) {
91-
esp3d_log_e("File sending failed!");
92-
chunksize = 0;
93-
response_code = 500;
94-
response_msg = "Failed to send file";
77+
78+
// open file
79+
FILE *fd = globalFs.open(uri.c_str(), "r");
80+
if (fd) {
81+
size_t chunksize;
82+
size_t total_send = 0;
83+
// send file
84+
do {
85+
// Read data block from the file
86+
chunksize = fread(_chunk, 1, CHUNK_BUFFER_SIZE, fd);
87+
total_send += chunksize;
88+
if (chunksize > 0) {
89+
// Send the HTTP data block
90+
if (httpd_resp_send_chunk(req, _chunk, chunksize) != ESP_OK) {
91+
esp3d_log_e("File sending failed!");
92+
chunksize = 0;
93+
response_code = 500;
94+
response_msg = "Failed to send file";
95+
}
9596
}
97+
} while (chunksize != 0);
98+
// Close the file
99+
fclose(fd);
100+
httpd_resp_send_chunk(req, NULL, 0);
101+
// Check if all the file has been sent
102+
if (total_send != file_size) {
103+
esp3d_log_e("File sending failed: size do not match!");
104+
response_code = 500;
105+
response_msg = "File sending failed: size do not match!";
96106
}
97-
} while (chunksize != 0);
98-
// Close the file
99-
fclose(fd);
100-
httpd_resp_send_chunk(req, NULL, 0);
101-
// Check if all the file has been sent
102-
if (total_send != file_size) {
103-
esp3d_log_e("File sending failed: size do not match!");
107+
} else {
108+
esp3d_log_e("Failed to open file");
104109
response_code = 500;
105-
response_msg = "File sending failed: size do not match!";
110+
response_msg = "Failed to open file";
106111
}
107112
} else {
108-
esp3d_log_e("Failed to open file");
109-
response_code = 500;
110-
response_msg = "Failed to open file";
113+
// is directory
114+
esp3d_log_e("This is not a file");
115+
response_code = 405;
116+
response_msg = "This is not a file";
111117
}
112118
}
113119
// release access

main/modules/http/handlers/webdav/esp3d_webdav_head.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ esp_err_t ESP3DHttpService::webdav_head_handler(httpd_req_t *req) {
7373
// Add Content-Length header
7474
httpd_resp_set_hdr(req, "Content-Length",
7575
std::to_string(file_size).c_str());
76+
} else {
77+
response_code = 405;
78+
response_msg = "It is not a file";
79+
esp3d_log_e("It is not a file");
7680
}
7781
}
7882
// release access

0 commit comments

Comments
 (0)