From 01e73f0a41756a7a63fe35c93cbc60f843b140a7 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Tue, 6 Dec 2022 06:53:05 +0100 Subject: [PATCH] Fix cookie usage, use correct http response codes, add 404 page (#1495) * replaced some HTTP response code with better matching codes * add custom 404 page, add log entry for debugging * fix cookie * replace non-necessary whitespace * . Co-authored-by: CaCO3 --- .../ClassControllCamera.cpp | 2 +- .../jomjol_fileserver_ota/server_file.cpp | 78 ++++++++++--------- .../jomjol_fileserver_ota/server_help.cpp | 8 +- .../jomjol_fileserver_ota/server_ota.cpp | 2 +- .../jomjol_flowcontroll/ClassFlowControll.cpp | 8 +- .../jomjol_flowcontroll/ClassFlowImage.cpp | 2 +- code/components/jomjol_helper/Helper.cpp | 21 +++-- code/components/jomjol_helper/Helper.h | 4 +- .../jomjol_image_proc/CImageBasis.cpp | 2 +- .../jomjol_logfile/ClassLogFile.cpp | 16 ++-- code/main/server_main.cpp | 4 +- sd-card/html/index.html | 2 +- 12 files changed, 82 insertions(+), 67 deletions(-) diff --git a/code/components/jomjol_controlcamera/ClassControllCamera.cpp b/code/components/jomjol_controlcamera/ClassControllCamera.cpp index 850fa17aa..3b047c2d6 100644 --- a/code/components/jomjol_controlcamera/ClassControllCamera.cpp +++ b/code/components/jomjol_controlcamera/ClassControllCamera.cpp @@ -425,7 +425,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay) nm = FormatFileName(nm); #ifdef DEBUG_DETAIL_ON - ESP_LOGD(TAG, "Save Camera to : %s", nm.c_str()); + ESP_LOGD(TAG, "Save Camera to: %s", nm.c_str()); #endif ftype = toUpper(getFileType(nm)); diff --git a/code/components/jomjol_fileserver_ota/server_file.cpp b/code/components/jomjol_fileserver_ota/server_file.cpp index 2aaa8dfd7..6058245e0 100644 --- a/code/components/jomjol_fileserver_ota/server_file.cpp +++ b/code/components/jomjol_fileserver_ota/server_file.cpp @@ -219,9 +219,9 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const ESP_LOGD(TAG, "entrypath: <%s>", entrypath); if (!dir) { - ESP_LOGE(TAG, "Failed to stat dir : %s", dirpath); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to stat dir: %s", dirpath); /* Respond with 404 Not Found */ - httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Directory does not exist"); + httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404()); return ESP_FAIL; } @@ -278,11 +278,11 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const strlcpy(entrypath + dirpath_len, entry->d_name, sizeof(entrypath) - dirpath_len); ESP_LOGD(TAG, "Entrypath: %s", entrypath); if (stat(entrypath, &entry_stat) == -1) { - ESP_LOGE(TAG, "Failed to stat %s : %s", entrytype, entry->d_name); + ESP_LOGE(TAG, "Failed to stat %s: %s", entrytype, entry->d_name); continue; } sprintf(entrysize, "%ld", entry_stat.st_size); - ESP_LOGI(TAG, "Found %s : %s (%s bytes)", entrytype, entry->d_name, entrysize); + ESP_LOGI(TAG, "Found %s: %s (%s bytes)", entrytype, entry->d_name, entrysize); /* Send chunk of HTML file containing table entries with file name and size */ httpd_resp_sendstr_chunk(req, "user_ctx)->base_path, req->uri + sizeof("/upload") - 1, sizeof(filepath)); if (!filename) { - /* Respond with 500 Internal Server Error */ - httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long"); + /* Respond with 413 Error */ + httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long"); return ESP_FAIL; } /* Filename cannot have a trailing '/' */ if (filename[strlen(filename) - 1] == '/') { - ESP_LOGE(TAG, "Invalid filename : %s", filename); - httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Invalid filename"); + ESP_LOGE(TAG, "Invalid filename: %s", filename); + /* Respond with 400 Bad Request */ + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid filename"); return ESP_FAIL; } if (stat(filepath, &file_stat) == 0) { - ESP_LOGE(TAG, "File already exists : %s", filepath); + ESP_LOGE(TAG, "File already exists: %s", filepath); /* Respond with 400 Bad Request */ httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "File already exists"); return ESP_FAIL; @@ -649,7 +650,7 @@ static esp_err_t upload_post_handler(httpd_req_t *req) /* File cannot be larger than a limit */ if (req->content_len > MAX_FILE_SIZE) { - ESP_LOGE(TAG, "File too large : %d bytes", req->content_len); + ESP_LOGE(TAG, "File too large: %d bytes", req->content_len); /* Respond with 400 Bad Request */ httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "File size must be less than " @@ -661,13 +662,13 @@ static esp_err_t upload_post_handler(httpd_req_t *req) fd = OpenFileAndWait(filepath, "w"); if (!fd) { - ESP_LOGE(TAG, "Failed to create file : %s", filepath); + ESP_LOGE(TAG, "Failed to create file: %s", filepath); /* Respond with 500 Internal Server Error */ httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to create file"); return ESP_FAIL; } - ESP_LOGI(TAG, "Receiving file : %s...", filename); + ESP_LOGI(TAG, "Receiving file: %s...", filename); /* Retrieve the pointer to scratch buffer for temporary storage */ char *buf = ((struct file_server_data *)req->user_ctx)->scratch; @@ -679,7 +680,7 @@ static esp_err_t upload_post_handler(httpd_req_t *req) while (remaining > 0) { - ESP_LOGI(TAG, "Remaining size : %d", remaining); + ESP_LOGI(TAG, "Remaining size: %d", remaining); /* Receive the file part by part into a buffer */ if ((received = httpd_req_recv(req, buf, MIN(remaining, SCRATCH_BUFSIZE))) <= 0) { if (received == HTTPD_SOCK_ERR_TIMEOUT) { @@ -790,8 +791,8 @@ static esp_err_t delete_post_handler(httpd_req_t *req) const char *filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path, req->uri + sizeof("/delete") - 1, sizeof(filepath)); if (!filename) { - /* Respond with 500 Internal Server Error */ - httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long"); + /* Respond with 414 Error */ + httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long"); return ESP_FAIL; } zw = std::string(filename); @@ -824,19 +825,20 @@ static esp_err_t delete_post_handler(httpd_req_t *req) /* Filename cannot have a trailing '/' */ if (filename[strlen(filename) - 1] == '/') { - ESP_LOGE(TAG, "Invalid filename : %s", filename); - httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Invalid filename"); + ESP_LOGE(TAG, "Invalid filename: %s", filename); + /* Respond with 400 Bad Request */ + httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid filename"); return ESP_FAIL; } if (stat(filepath, &file_stat) == -1) { - ESP_LOGE(TAG, "File does not exist : %s", filename); + ESP_LOGE(TAG, "File does not exist: %s", filename); /* Respond with 400 Bad Request */ httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "File does not exist"); return ESP_FAIL; } - ESP_LOGI(TAG, "Deleting file : %s", filename); + ESP_LOGI(TAG, "Deleting file: %s", filename); /* Delete file */ unlink(filepath); @@ -878,7 +880,7 @@ void delete_all_in_directory(std::string _directory) std::string filename; if (!dir) { - ESP_LOGE(TAG, "Failed to stat dir : %s", _directory.c_str()); + ESP_LOGE(TAG, "Failed to stat dir: %s", _directory.c_str()); return; } @@ -887,7 +889,7 @@ void delete_all_in_directory(std::string _directory) if (!(entry->d_type == DT_DIR)){ if (strcmp("wlan.ini", entry->d_name) != 0){ // auf wlan.ini soll nicht zugegriffen werden !!! filename = _directory + "/" + std::string(entry->d_name); - ESP_LOGI(TAG, "Deleting file : %s", filename.c_str()); + ESP_LOGI(TAG, "Deleting file: %s", filename.c_str()); /* Delete file */ unlink(filename.c_str()); } diff --git a/code/components/jomjol_fileserver_ota/server_help.cpp b/code/components/jomjol_fileserver_ota/server_help.cpp index 5bdc105d2..92a08c27e 100644 --- a/code/components/jomjol_fileserver_ota/server_help.cpp +++ b/code/components/jomjol_fileserver_ota/server_help.cpp @@ -36,13 +36,13 @@ esp_err_t send_file(httpd_req_t *req, std::string filename) { FILE *fd = OpenFileAndWait(filename.c_str(), "r"); if (!fd) { - ESP_LOGE(TAG, "Failed to read existing file : %s", filename.c_str()); - /* Respond with 500 Internal Server Error */ - httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file"); + ESP_LOGE(TAG, "Failed to read existing file: %s", filename.c_str()); + /* Respond with 404 Error */ + httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404()); return ESP_FAIL; } - ESP_LOGD(TAG, "Sending file : %s ...", filename.c_str()); + ESP_LOGD(TAG, "Sending file: %s ...", filename.c_str()); // httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); set_content_type_from_file(req, filename.c_str()); diff --git a/code/components/jomjol_fileserver_ota/server_ota.cpp b/code/components/jomjol_fileserver_ota/server_ota.cpp index f04f6fc30..8d3d36524 100644 --- a/code/components/jomjol_fileserver_ota/server_ota.cpp +++ b/code/components/jomjol_fileserver_ota/server_ota.cpp @@ -530,7 +530,7 @@ esp_err_t handler_ota_update(httpd_req_t *req) int _result = stat(fn.c_str(), &file_stat); ESP_LOGD(TAG, "Ergebnis %d\n", _result); if (_result == 0) { - ESP_LOGD(TAG, "Deleting file : %s", fn.c_str()); + ESP_LOGD(TAG, "Deleting file: %s", fn.c_str()); /* Delete file */ unlink(fn.c_str()); } diff --git a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp index 4e963a090..714a72a05 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp @@ -569,10 +569,10 @@ bool ClassFlowControll::ReadParameter(FILE* pfile, string& aktparamgraph) int ClassFlowControll::CleanTempFolder() { const char* folderPath = "/sdcard/img_tmp"; - ESP_LOGD(TAG, "Clean up temporary folder to avoid damage of sdcard sectors : %s", folderPath); + ESP_LOGD(TAG, "Clean up temporary folder to avoid damage of sdcard sectors: %s", folderPath); DIR *dir = opendir(folderPath); if (!dir) { - ESP_LOGE(TAG, "Failed to stat dir : %s", folderPath); + ESP_LOGE(TAG, "Failed to stat dir: %s", folderPath); return -1; } @@ -584,7 +584,7 @@ int ClassFlowControll::CleanTempFolder() { if (unlink(path.c_str()) == 0) { deleted ++; } else { - ESP_LOGE(TAG, "can't delete file : %s", path.c_str()); + ESP_LOGE(TAG, "can't delete file: %s", path.c_str()); } } else if (entry->d_type == DT_DIR) { deleted += removeFolder(path.c_str(), TAG); @@ -679,7 +679,7 @@ esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req) if (_send) { - ESP_LOGD(TAG, "Sending file : %s ...", _fn.c_str()); + ESP_LOGD(TAG, "Sending file: %s ...", _fn.c_str()); set_content_type_from_file(req, _fn.c_str()); result = _send->SendJPGtoHTTP(req); ESP_LOGD(TAG, "File sending complete"); diff --git a/code/components/jomjol_flowcontroll/ClassFlowImage.cpp b/code/components/jomjol_flowcontroll/ClassFlowImage.cpp index 0410d6941..2343dd062 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowImage.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowImage.cpp @@ -113,7 +113,7 @@ void ClassFlowImage::RemoveOldLogs() DIR *dir = opendir(LogImageLocation.c_str()); if (!dir) { - ESP_LOGE(TAG, "Failed to stat dir : %s", LogImageLocation.c_str()); + ESP_LOGE(TAG, "Failed to stat dir: %s", LogImageLocation.c_str()); return; } diff --git a/code/components/jomjol_helper/Helper.cpp b/code/components/jomjol_helper/Helper.cpp index 00561c46a..f5af01250 100644 --- a/code/components/jomjol_helper/Helper.cpp +++ b/code/components/jomjol_helper/Helper.cpp @@ -304,7 +304,7 @@ size_t findDelimiterPos(string input, string delimiter) bool RenameFile(string from, string to) { -// ESP_LOGI(logTag, "Deleting file : %s", fn.c_str()); +// ESP_LOGI(logTag, "Deleting file: %s", fn.c_str()); /* Delete file */ FILE* fpSourceFile = OpenFileAndWait(from.c_str(), "rb"); if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch! @@ -321,7 +321,7 @@ bool RenameFile(string from, string to) bool DeleteFile(string fn) { -// ESP_LOGI(logTag, "Deleting file : %s", fn.c_str()); +// ESP_LOGI(logTag, "Deleting file: %s", fn.c_str()); /* Delete file */ FILE* fpSourceFile = OpenFileAndWait(fn.c_str(), "rb"); if (!fpSourceFile) // Sourcefile existiert nicht sonst gibt es einen Fehler beim Kopierversuch! @@ -512,7 +512,7 @@ int removeFolder(const char* folderPath, const char* logTag) { DIR *dir = opendir(folderPath); if (!dir) { - ESP_LOGE(logTag, "Failed to stat dir : %s", folderPath); + ESP_LOGE(logTag, "Failed to stat dir: %s", folderPath); return -1; } @@ -525,7 +525,7 @@ int removeFolder(const char* folderPath, const char* logTag) { if (unlink(path.c_str()) == 0) { deleted ++; } else { - ESP_LOGE(logTag, "can't delete file : %s", path.c_str()); + ESP_LOGE(logTag, "can't delete file: %s", path.c_str()); } } else if (entry->d_type == DT_DIR) { deleted += removeFolder(path.c_str(), logTag); @@ -534,7 +534,7 @@ int removeFolder(const char* folderPath, const char* logTag) { closedir(dir); if (rmdir(folderPath) != 0) { - ESP_LOGE(logTag, "can't delete folder : %s", folderPath); + ESP_LOGE(logTag, "can't delete folder: %s", folderPath); } ESP_LOGD(logTag, "%d files in folder %s deleted.", deleted, folderPath); @@ -791,3 +791,14 @@ string getResetReason(void) { } return reasonText; } + +const char* get404(void) { + return +"
\n\n\n\n"
+"        _\n"
+"    .__(.)< ( oh oh! This page does not exist! )\n"
+"    \\___)\n"
+"\n\n"
+"                You could try your luck here!
\n" +""; // Make sure we load the overview page +} diff --git a/code/components/jomjol_helper/Helper.h b/code/components/jomjol_helper/Helper.h index 1aa39904e..fd8ddf3dc 100644 --- a/code/components/jomjol_helper/Helper.h +++ b/code/components/jomjol_helper/Helper.h @@ -63,4 +63,6 @@ string getSDCardCapacity(); string getSDCardSectorSize(); string getMac(void); -string getResetReason(void); \ No newline at end of file +string getResetReason(void); + +const char* get404(void); diff --git a/code/components/jomjol_image_proc/CImageBasis.cpp b/code/components/jomjol_image_proc/CImageBasis.cpp index 7a1e19f3e..aed53aa39 100644 --- a/code/components/jomjol_image_proc/CImageBasis.cpp +++ b/code/components/jomjol_image_proc/CImageBasis.cpp @@ -453,7 +453,7 @@ CImageBasis::CImageBasis(std::string _image) RGBImageRelease(); zwld = esp_get_free_heap_size(); - ESP_LOGD(TAG, "freeheapsize after : %ld", zwld); + ESP_LOGD(TAG, "freeheapsize after: %ld", zwld); std::string zw = "Image Load failed:" + _image; if (rgb_image == NULL) diff --git a/code/components/jomjol_logfile/ClassLogFile.cpp b/code/components/jomjol_logfile/ClassLogFile.cpp index 6f9079cd7..e4b6b53f9 100644 --- a/code/components/jomjol_logfile/ClassLogFile.cpp +++ b/code/components/jomjol_logfile/ClassLogFile.cpp @@ -315,7 +315,7 @@ void ClassLogFile::RemoveOldLogFile() DIR *dir = opendir(logroot.c_str()); if (!dir) { - ESP_LOGE(TAG, "Failed to stat dir : %s", logroot.c_str()); + ESP_LOGE(TAG, "Failed to stat dir: %s", logroot.c_str()); return; } @@ -324,14 +324,14 @@ void ClassLogFile::RemoveOldLogFile() int notDeleted = 0; while ((entry = readdir(dir)) != NULL) { if (entry->d_type == DT_REG) { - //ESP_LOGD(TAG, "compare log file : %s to %s", entry->d_name, cmpfilename); + //ESP_LOGD(TAG, "compare log file: %s to %s", entry->d_name, cmpfilename); if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) { - //ESP_LOGD(TAG, "delete log file : %s", entry->d_name); + //ESP_LOGD(TAG, "delete log file: %s", entry->d_name); std::string filepath = logroot + "/" + entry->d_name; if (unlink(filepath.c_str()) == 0) { deleted ++; } else { - ESP_LOGE(TAG, "can't delete file : %s", entry->d_name); + ESP_LOGE(TAG, "can't delete file: %s", entry->d_name); notDeleted ++; } } else { @@ -366,7 +366,7 @@ void ClassLogFile::RemoveOldDataLog() DIR *dir = opendir(dataroot.c_str()); if (!dir) { - ESP_LOGE(TAG, "Failed to stat dir : %s", dataroot.c_str()); + ESP_LOGE(TAG, "Failed to stat dir: %s", dataroot.c_str()); return; } @@ -375,14 +375,14 @@ void ClassLogFile::RemoveOldDataLog() int notDeleted = 0; while ((entry = readdir(dir)) != NULL) { if (entry->d_type == DT_REG) { - //ESP_LOGD(TAG, "Compare data file : %s to %s", entry->d_name, cmpfilename); + //ESP_LOGD(TAG, "Compare data file: %s to %s", entry->d_name, cmpfilename); if ((strlen(entry->d_name) == strlen(cmpfilename)) && (strcmp(entry->d_name, cmpfilename) < 0)) { - //ESP_LOGD(TAG, "delete data file : %s", entry->d_name); + //ESP_LOGD(TAG, "delete data file: %s", entry->d_name); std::string filepath = dataroot + "/" + entry->d_name; if (unlink(filepath.c_str()) == 0) { deleted ++; } else { - ESP_LOGE(TAG, "can't delete file : %s", entry->d_name); + ESP_LOGE(TAG, "can't delete file: %s", entry->d_name); notDeleted ++; } } else { diff --git a/code/main/server_main.cpp b/code/main/server_main.cpp index 8532b9635..3663cca33 100644 --- a/code/main/server_main.cpp +++ b/code/main/server_main.cpp @@ -241,8 +241,8 @@ esp_err_t hello_main_handler(httpd_req_t *req) if (!filename) { ESP_LOGE(TAG, "Filename is too long"); - /* Respond with 500 Internal Server Error */ - httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long"); + /* Respond with 414 Error */ + httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long"); return ESP_FAIL; } diff --git a/sd-card/html/index.html b/sd-card/html/index.html index ca4e05f75..c69fe49cf 100644 --- a/sd-card/html/index.html +++ b/sd-card/html/index.html @@ -15,7 +15,7 @@