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 <caco@ruinelli.ch>
This commit is contained in:
CaCO3
2022-12-06 06:53:05 +01:00
committed by GitHub
parent fa406d8989
commit 4f07c88769
12 changed files with 82 additions and 67 deletions

View File

@@ -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); ESP_LOGD(TAG, "entrypath: <%s>", entrypath);
if (!dir) { 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 */ /* 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; return ESP_FAIL;
} }
@@ -361,9 +361,9 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)
fd = OpenFileAndWait(currentfilename.c_str(), "r"); fd = OpenFileAndWait(currentfilename.c_str(), "r");
if (!fd) { if (!fd) {
ESP_LOGE(TAG, "Failed to read existing file : %s", filepath); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read existing file: %s", filepath);
/* Respond with 500 Internal Server Error */ /* Respond with 404 Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file"); httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL; return ESP_FAIL;
} }
@@ -446,9 +446,9 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)
fd = OpenFileAndWait(currentfilename.c_str(), "r"); fd = OpenFileAndWait(currentfilename.c_str(), "r");
if (!fd) { if (!fd) {
ESP_LOGE(TAG, "Failed to read existing file : %s", filepath); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read existing file: %s", filepath);
/* Respond with 500 Internal Server Error */ /* Respond with 404 Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file"); httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL; return ESP_FAIL;
} }
@@ -534,8 +534,8 @@ static esp_err_t download_get_handler(httpd_req_t *req)
if (!filename) { if (!filename) {
ESP_LOGE(TAG, "Filename is too long"); ESP_LOGE(TAG, "Filename is too long");
/* Respond with 500 Internal Server Error */ /* Respond with 414 Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long"); httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long");
return ESP_FAIL; return ESP_FAIL;
} }
@@ -566,17 +566,17 @@ static esp_err_t download_get_handler(httpd_req_t *req)
/* If file not present on SPIFFS check if URI /* If file not present on SPIFFS check if URI
* corresponds to one of the hardcoded paths */ * corresponds to one of the hardcoded paths */
ESP_LOGE(TAG, "Failed to stat file : %s", filepath); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to stat file: %s!", filepath);
/* Respond with 404 Not Found */ /* Respond with 404 Not Found */
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "File does not exist"); httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL; return ESP_FAIL;
} }
fd = OpenFileAndWait(filepath, "r"); fd = OpenFileAndWait(filepath, "r");
if (!fd) { if (!fd) {
ESP_LOGE(TAG, "Failed to read existing file : %s", filepath); LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read existing file: %s!", filepath);
/* Respond with 500 Internal Server Error */ /* Respond with 404 Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file"); httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL; return ESP_FAIL;
} }
@@ -599,7 +599,7 @@ static esp_err_t download_get_handler(httpd_req_t *req)
/* Abort sending file */ /* Abort sending file */
httpd_resp_sendstr_chunk(req, NULL); httpd_resp_sendstr_chunk(req, NULL);
/* Respond with 500 Internal Server Error */ /* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file"); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file!");
return ESP_FAIL; return ESP_FAIL;
} }
@@ -628,15 +628,16 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
const char *filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path, const char *filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path,
req->uri + sizeof("/upload") - 1, sizeof(filepath)); req->uri + sizeof("/upload") - 1, sizeof(filepath));
if (!filename) { if (!filename) {
/* Respond with 500 Internal Server Error */ /* Respond with 413 Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long"); httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long");
return ESP_FAIL; return ESP_FAIL;
} }
/* Filename cannot have a trailing '/' */ /* Filename cannot have a trailing '/' */
if (filename[strlen(filename) - 1] == '/') { if (filename[strlen(filename) - 1] == '/') {
ESP_LOGE(TAG, "Invalid filename: %s", filename); ESP_LOGE(TAG, "Invalid filename: %s", filename);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Invalid filename"); /* Respond with 400 Bad Request */
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid filename");
return ESP_FAIL; return ESP_FAIL;
} }
@@ -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, const char *filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path,
req->uri + sizeof("/delete") - 1, sizeof(filepath)); req->uri + sizeof("/delete") - 1, sizeof(filepath));
if (!filename) { if (!filename) {
/* Respond with 500 Internal Server Error */ /* Respond with 414 Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long"); httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long");
return ESP_FAIL; return ESP_FAIL;
} }
zw = std::string(filename); zw = std::string(filename);
@@ -825,7 +826,8 @@ static esp_err_t delete_post_handler(httpd_req_t *req)
/* Filename cannot have a trailing '/' */ /* Filename cannot have a trailing '/' */
if (filename[strlen(filename) - 1] == '/') { if (filename[strlen(filename) - 1] == '/') {
ESP_LOGE(TAG, "Invalid filename: %s", filename); ESP_LOGE(TAG, "Invalid filename: %s", filename);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Invalid filename"); /* Respond with 400 Bad Request */
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid filename");
return ESP_FAIL; return ESP_FAIL;
} }

View File

@@ -37,8 +37,8 @@ esp_err_t send_file(httpd_req_t *req, std::string filename)
FILE *fd = OpenFileAndWait(filename.c_str(), "r"); FILE *fd = OpenFileAndWait(filename.c_str(), "r");
if (!fd) { if (!fd) {
ESP_LOGE(TAG, "Failed to read existing file: %s", filename.c_str()); ESP_LOGE(TAG, "Failed to read existing file: %s", filename.c_str());
/* Respond with 500 Internal Server Error */ /* Respond with 404 Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file"); httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL; return ESP_FAIL;
} }

View File

@@ -791,3 +791,14 @@ string getResetReason(void) {
} }
return reasonText; return reasonText;
} }
const char* get404(void) {
return
"<pre>\n\n\n\n"
" _\n"
" .__(.)< ( oh oh! This page does not exist! )\n"
" \\___)\n"
"\n\n"
" You could try your <a href=index.html target=_parent>luck</a> here!</pre>\n"
"<script>document.cookie = \"page=overview.html\"</script>"; // Make sure we load the overview page
}

View File

@@ -64,3 +64,5 @@ string getSDCardSectorSize();
string getMac(void); string getMac(void);
string getResetReason(void); string getResetReason(void);
const char* get404(void);

View File

@@ -241,8 +241,8 @@ esp_err_t hello_main_handler(httpd_req_t *req)
if (!filename) { if (!filename) {
ESP_LOGE(TAG, "Filename is too long"); ESP_LOGE(TAG, "Filename is too long");
/* Respond with 500 Internal Server Error */ /* Respond with 414 Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Filename too long"); httpd_resp_send_err(req, HTTPD_414_URI_TOO_LONG, "Filename too long");
return ESP_FAIL; return ESP_FAIL;
} }

View File

@@ -15,7 +15,7 @@
<script> <script>
async function loadPage(page) { async function loadPage(page) {
console.log("loadPage(" + page + ")"); console.log("loadPage(" + page + ")");
document.cookie = page; document.cookie = "page="+page;
document.getElementById('maincontent').src = page; document.getElementById('maincontent').src = page;
[].forEach.call(document.querySelectorAll('.submenu'), function (el) { [].forEach.call(document.querySelectorAll('.submenu'), function (el) {