Skip to content

Commit

Permalink
Fix cookie usage, use correct http response codes, add 404 page (#1495)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
caco3 and CaCO3 authored Dec 6, 2022
1 parent fa406d8 commit 4f07c88
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
78 changes: 40 additions & 38 deletions code/components/jomjol_fileserver_ota/server_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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, "<tr><td><a href=\"");
Expand Down Expand Up @@ -361,15 +361,15 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)

fd = OpenFileAndWait(currentfilename.c_str(), "r");
if (!fd) {
ESP_LOGE(TAG, "Failed to read existing file : %s", filepath);
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file");
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read existing file: %s", filepath);
/* Respond with 404 Error */
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL;
}

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

// ESP_LOGI(TAG, "Sending file : %s (%ld bytes)...", &filename, file_stat.st_size);
// ESP_LOGI(TAG, "Sending file: %s (%ld bytes)...", &filename, file_stat.st_size);
set_content_type_from_file(req, filename);

if (!send_full_file) { // Send only last part of file
Expand Down Expand Up @@ -446,15 +446,15 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)

fd = OpenFileAndWait(currentfilename.c_str(), "r");
if (!fd) {
ESP_LOGE(TAG, "Failed to read existing file : %s", filepath);
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file");
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read existing file: %s", filepath);
/* Respond with 404 Error */
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL;
}

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

// ESP_LOGI(TAG, "Sending file : %s (%ld bytes)...", &filename, file_stat.st_size);
// ESP_LOGI(TAG, "Sending file: %s (%ld bytes)...", &filename, file_stat.st_size);
set_content_type_from_file(req, filename);

if (!send_full_file) { // Send only last part of file
Expand Down Expand Up @@ -534,8 +534,8 @@ static esp_err_t download_get_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;
}

Expand Down Expand Up @@ -566,23 +566,23 @@ static esp_err_t download_get_handler(httpd_req_t *req)

/* If file not present on SPIFFS check if URI
* 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 */
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;
}

fd = OpenFileAndWait(filepath, "r");
if (!fd) {
ESP_LOGE(TAG, "Failed to read existing file : %s", filepath);
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to read existing file");
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read existing file: %s!", filepath);
/* Respond with 404 Error */
httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, get404());
return ESP_FAIL;
}

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

ESP_LOGD(TAG, "Sending file : %s (%ld bytes)...", filename, file_stat.st_size);
ESP_LOGD(TAG, "Sending file: %s (%ld bytes)...", filename, file_stat.st_size);
set_content_type_from_file(req, filename);

/* Retrieve the pointer to scratch buffer for temporary storage */
Expand All @@ -599,7 +599,7 @@ static esp_err_t download_get_handler(httpd_req_t *req)
/* Abort sending file */
httpd_resp_sendstr_chunk(req, NULL);
/* 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;
}

Expand Down Expand Up @@ -628,28 +628,29 @@ 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,
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;
}

/* 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 "
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand All @@ -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());
}
Expand Down
8 changes: 4 additions & 4 deletions code/components/jomjol_fileserver_ota/server_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
8 changes: 4 additions & 4 deletions code/components/jomjol_flowcontroll/ClassFlowControll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_flowcontroll/ClassFlowImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
21 changes: 16 additions & 5 deletions code/components/jomjol_helper/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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!
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -791,3 +791,14 @@ string getResetReason(void) {
}
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
}
4 changes: 3 additions & 1 deletion code/components/jomjol_helper/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ string getSDCardCapacity();
string getSDCardSectorSize();

string getMac(void);
string getResetReason(void);
string getResetReason(void);

const char* get404(void);
2 changes: 1 addition & 1 deletion code/components/jomjol_image_proc/CImageBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 4f07c88

Please sign in to comment.