mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-27 05:06:44 +00:00
Better http status codes
This commit is contained in:
parent
d284434f91
commit
00a43953d2
@ -894,7 +894,7 @@ void webHandleFirmwareUpload()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
|
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
|
||||||
bool handleFileRead(String path)
|
int handleFileRead(String path)
|
||||||
{
|
{
|
||||||
// if(!httpIsAuthenticated(F("fileread"))) return false;
|
// if(!httpIsAuthenticated(F("fileread"))) return false;
|
||||||
if(!httpIsAuthenticated()) return false;
|
if(!httpIsAuthenticated()) return false;
|
||||||
@ -909,10 +909,9 @@ bool handleFileRead(String path)
|
|||||||
File file = HASP_FS.open(path, "r");
|
File file = HASP_FS.open(path, "r");
|
||||||
|
|
||||||
String configFile((char*)0);
|
String configFile((char*)0);
|
||||||
configFile.reserve(32);
|
|
||||||
configFile = String(FPSTR(FP_HASP_CONFIG_FILE));
|
configFile = String(FPSTR(FP_HASP_CONFIG_FILE));
|
||||||
|
|
||||||
String contentType;
|
String contentType((char*)0);
|
||||||
if(webServer.hasArg(F("download")))
|
if(webServer.hasArg(F("download")))
|
||||||
contentType = F("application/octet-stream");
|
contentType = F("application/octet-stream");
|
||||||
else
|
else
|
||||||
@ -924,16 +923,17 @@ bool handleFileRead(String path)
|
|||||||
DeserializationError error = configParseFile(configFile, settings);
|
DeserializationError error = configParseFile(configFile, settings);
|
||||||
|
|
||||||
if(!error) {
|
if(!error) {
|
||||||
LOG_TRACE(TAG_CONF, F(D_FILE_LOADING), configFile.c_str());
|
// LOG_TRACE(TAG_CONF, F(D_FILE_LOADING), configFile.c_str());
|
||||||
// Output settings in log with masked passwords
|
configMaskPasswords(settings); // Output settings in log with masked passwords
|
||||||
configMaskPasswords(settings);
|
|
||||||
char buffer[800];
|
char buffer[800];
|
||||||
size_t len = serializeJson(settings, buffer, sizeof(buffer));
|
size_t len = serializeJson(settings, buffer, sizeof(buffer));
|
||||||
LOG_VERBOSE(TAG_CONF, buffer);
|
// LOG_VERBOSE(TAG_CONF, buffer);
|
||||||
|
|
||||||
webServer.setContentLength(len);
|
webServer.setContentLength(len);
|
||||||
webServer.send(200, contentType, "");
|
webServer.send(200, contentType, "");
|
||||||
webServer.sendContent(buffer, len);
|
webServer.sendContent((const char*)buffer, len);
|
||||||
} else {
|
} else {
|
||||||
|
return 500; // Internal Server error
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -942,7 +942,7 @@ bool handleFileRead(String path)
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return 200; // OK
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
@ -950,11 +950,11 @@ bool handleFileRead(String path)
|
|||||||
size_t size = EDIT_HTM_GZ_END - EDIT_HTM_GZ_START;
|
size_t size = EDIT_HTM_GZ_END - EDIT_HTM_GZ_START;
|
||||||
webServer.sendHeader(F("Content-Encoding"), F("gzip"));
|
webServer.sendHeader(F("Content-Encoding"), F("gzip"));
|
||||||
webServer.send_P(200, PSTR("text/html"), (const char*)EDIT_HTM_GZ_START, size);
|
webServer.send_P(200, PSTR("text/html"), (const char*)EDIT_HTM_GZ_START, size);
|
||||||
return true;
|
return 200; // OK
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return false;
|
return 404; // Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleFileUpload()
|
void handleFileUpload()
|
||||||
@ -1862,24 +1862,28 @@ void webHandleHaspConfig()
|
|||||||
void httpHandleNotFound()
|
void httpHandleNotFound()
|
||||||
{ // webServer 404
|
{ // webServer 404
|
||||||
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
|
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
|
||||||
if(handleFileRead(webServer.uri())) {
|
int statuscode = handleFileRead(webServer.uri());
|
||||||
LOG_TRACE(TAG_HTTP, F("Sending %d %s to client connected from: %s"), 200, webServer.uri().c_str(),
|
if(statuscode == 200) return;
|
||||||
webServer.client().remoteIP().toString().c_str());
|
#else
|
||||||
return;
|
int statuscode = 404;
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
|
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
|
||||||
LOG_TRACE(TAG_HTTP, F("Sending %d %s to client connected from: %s"), 404, webServer.uri().c_str(),
|
LOG_TRACE(TAG_HTTP, F("Sending %d %s to client connected from: %s"), statuscode, webServer.uri().c_str(),
|
||||||
webServer.client().remoteIP().toString().c_str());
|
webServer.client().remoteIP().toString().c_str());
|
||||||
#else
|
#else
|
||||||
// LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"), String(webServer.client().remoteIP()).c_str());
|
// LOG_TRACE(TAG_HTTP,F("Sending 404 to client connected from: %s"), String(webServer.client().remoteIP()).c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
String httpMessage((char*)0);
|
String httpMessage((char*)0);
|
||||||
httpMessage.reserve(HTTP_PAGE_SIZE);
|
httpMessage.reserve(HTTP_PAGE_SIZE);
|
||||||
|
|
||||||
httpMessage += F("File Not Found\n\nURI: ");
|
if(statuscode == 500)
|
||||||
|
httpMessage += F("Internal Server Error");
|
||||||
|
else
|
||||||
|
httpMessage += F("File Not Found");
|
||||||
|
|
||||||
|
httpMessage += F("\n\nURI: ");
|
||||||
httpMessage += webServer.uri();
|
httpMessage += webServer.uri();
|
||||||
httpMessage += F("\nMethod: ");
|
httpMessage += F("\nMethod: ");
|
||||||
httpMessage += (webServer.method() == HTTP_GET) ? F("GET") : F("POST");
|
httpMessage += (webServer.method() == HTTP_GET) ? F("GET") : F("POST");
|
||||||
@ -1889,7 +1893,7 @@ void httpHandleNotFound()
|
|||||||
for(int i = 0; i < webServer.args(); i++) {
|
for(int i = 0; i < webServer.args(); i++) {
|
||||||
httpMessage += " " + webServer.argName(i) + ": " + webServer.arg(i) + "\n";
|
httpMessage += " " + webServer.argName(i) + ": " + webServer.arg(i) + "\n";
|
||||||
}
|
}
|
||||||
webServer.send(404, PSTR("text/plain"), httpMessage.c_str());
|
webServer.send(statuscode, PSTR("text/plain"), httpMessage.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
x
Reference in New Issue
Block a user