From c1f4678cdbb498640ca373ce723276ccc52def5b Mon Sep 17 00:00:00 2001 From: Bogdan Pilyugin Date: Thu, 17 Nov 2022 14:07:48 +0200 Subject: [PATCH] implemented spiffs with http web access --- src/FileServer.c | 20 +++++++++----------- src/HTTPServer.c | 19 ++++++------------- upload_script.html | 4 ++-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/FileServer.c b/src/FileServer.c index fc4465f..74daa88 100644 --- a/src/FileServer.c +++ b/src/FileServer.c @@ -103,7 +103,7 @@ static const char* get_path_from_uri(char *dest, const char *base_path, /* Construct full path (base + path) */ strcpy(dest, base_path); - strlcpy(dest + base_pathlen, uri, pathlen + 1); + strlcpy(dest + base_pathlen, uri + (sizeof("/files")-1), pathlen + 1 - (sizeof("/files")-1)); /* Return pointer to path, skipping the base */ return dest + base_pathlen; @@ -150,8 +150,8 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath) /* Send file-list table definition and column labels */ httpd_resp_sendstr_chunk( req, - "" - "" + "
" + "" "" ""); @@ -184,8 +184,8 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath) httpd_resp_sendstr_chunk(req, "\n"); @@ -228,7 +228,7 @@ esp_err_t download_get_handler(httpd_req_t *req) /* If name has trailing '/', respond with directory contents */ if (filename[strlen(filename) - 1] == '/') { - return http_resp_dir_html(req, "/data/"); + return http_resp_dir_html(req, filepath); } if (stat(filepath, &file_stat) == -1) @@ -241,8 +241,6 @@ esp_err_t download_get_handler(httpd_req_t *req) return index_html_get_handler(req); } - - ESP_LOGE(TAG, "Failed to stat file : %s", filepath); /* Respond with 404 Not Found */ httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist"); @@ -310,7 +308,7 @@ esp_err_t upload_post_handler(httpd_req_t *req) /* Skip leading "/upload" from URI to get filename */ /* Note sizeof() counts NULL termination hence the -1 */ const char *filename = get_path_from_uri(filepath, ((struct file_server_data*) req->user_ctx)->base_path2, - req->uri + sizeof("/files/upload") - 1, + req->uri + sizeof("/upload") - 1, sizeof(filepath)); ESP_LOGW(TAG, "FILE_POST_URI %s", req->uri); ESP_LOGW(TAG, "FILE_POST_FILEPATH %s", filepath); @@ -420,7 +418,7 @@ 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", "/"); + httpd_resp_set_hdr(req, "Location", "/files/"); #ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER httpd_resp_set_hdr(req, "Connection", "close"); #endif @@ -468,7 +466,7 @@ 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", "/"); + httpd_resp_set_hdr(req, "Location", "/files/"); #ifdef CONFIG_EXAMPLE_HTTPD_CONN_CLOSE_HEADER httpd_resp_set_hdr(req, "Connection", "close"); #endif diff --git a/src/HTTPServer.c b/src/HTTPServer.c index 85d558f..7ac9ad5 100644 --- a/src/HTTPServer.c +++ b/src/HTTPServer.c @@ -173,10 +173,11 @@ static esp_err_t POSTHandler(httpd_req_t *req) ESP_LOGI(TAG, "POST request handle"); #endif - if (true) - { + if (memmem(req->uri, strlen(req->uri), "/files/upload/", sizeof("/files/upload/")-1)) return upload_post_handler(req); - } + + if (memmem(req->uri, strlen(req->uri), "/files/delete/", sizeof("/files/delete/")-1)) + return delete_post_handler(req); char *buf = ((struct file_server_data*) req->user_ctx)->scratch; int received; @@ -261,12 +262,9 @@ static esp_err_t GETHandler(httpd_req_t *req) ESP_LOGI(TAG, "GET request handle"); #endif - ESP_LOGW(TAG, "URI %s", req->uri); - //if (!strcmp(req->uri, "/files/")) - if(memmem(req->uri, strlen(req->uri), "/files/", 7)) - { + //Route to file server GET handler + if (memmem(req->uri, strlen(req->uri), "/files/", sizeof("/files/") - 1)) return download_get_handler(req); - } char filepath[FILE_PATH_MAX]; espfs_file_t *file; @@ -275,9 +273,6 @@ static esp_err_t GETHandler(httpd_req_t *req) uint32_t bufSize; //size of ram buffer for chunk of data, read from file uint32_t readBytes; //number of bytes, read from file. used for information only - - - const char *filename = get_path_from_uri(filepath, ((struct file_server_data*) req->user_ctx)->base_path, req->uri, @@ -291,8 +286,6 @@ static esp_err_t GETHandler(httpd_req_t *req) return ESP_FAIL; } - - /* Redirect request to /index.html */ if (filename[strlen(filename) - 1] == '/') { diff --git a/upload_script.html b/upload_script.html index 608f1ee..c3bc094 100644 --- a/upload_script.html +++ b/upload_script.html @@ -1,7 +1,7 @@
NameTypeSize (Bytes)Delete
"); httpd_resp_sendstr_chunk(req, entrysize); httpd_resp_sendstr_chunk(req, ""); - httpd_resp_sendstr_chunk(req, "
uri); + httpd_resp_sendstr_chunk(req, "uri); httpd_resp_sendstr_chunk(req, entry->d_name); httpd_resp_sendstr_chunk(req, "\">
"); httpd_resp_sendstr_chunk(req, "
- +
-

ESP32 File Server

+

ESP32 File Server