Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor handle_not_found for readability #806

Open
wants to merge 1 commit into
base: Devt
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 126 additions & 121 deletions Grbl_Esp32/src/WebUI/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ namespace WebUI {
//Root of Webserver/////////////////////////////////////////////////////

void Web_Server::handle_root() {
String path = "/index.html";
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
String path = "/index.html";
String pathWithGz = path + ".gz";
//if have a index.html or gzip version this is default root page
if ((SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) && !_webserver->hasArg("forcefallback") &&
_webserver->arg("forcefallback") != "yes") {
Expand All @@ -253,7 +252,7 @@ namespace WebUI {
}

File file = SPIFFS.open(path, FILE_READ);
_webserver->streamFile(file, contentType);
_webserver->streamFile(file, getContentType(path));
file.close();
return;
}
Expand All @@ -263,137 +262,138 @@ namespace WebUI {
_webserver->send_P(200, "text/html", PAGE_NOFILES, PAGE_NOFILES_SIZE);
}

//Handle not registred path on SPIFFS neither SD ///////////////////////
void Web_Server::handle_not_found() {
if (is_authenticated() == AuthenticationLevel::LEVEL_GUEST) {
_webserver->sendContent_P("HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n");
//_webserver->client().stop();
return;
// Returns true if this handler claimed the path
bool Web_Server::handleSD(String path) {
if ((path.substring(0, 4) != "/SD/")) {
return false;
}
# ifndef ENABLE_SD_CARD
_webserver->send(500, "text/plain", "SD is not supported.");
return true;
# else
path = path.substring(3);

bool page_not_found = false;
String path = _webserver->urlDecode(_webserver->uri());
String contentType = getContentType(path);
String pathWithGz = path + ".gz";
String pathWithGz = path + ".gz";
if (SDState::Idle != get_sd_state(true)) {
String content = "cannot open: ";
content += path + ", SD is not available.";

# ifdef ENABLE_SD_CARD
if ((path.substring(0, 4) == "/SD/")) {
//remove /SD
path = path.substring(3);
if (SDState::Idle != get_sd_state(true)) {
String content = "cannot open: ";
content += path + ", SD is not available.";

_webserver->send(500, "text/plain", content);
_webserver->send(500, "text/plain", content);
return true;
}
if (SD.exists(pathWithGz) || SD.exists(path)) {
set_sd_state(SDState::BusyUploading);
if (SD.exists(pathWithGz)) {
path = pathWithGz;
}
if (SD.exists(pathWithGz) || SD.exists(path)) {
set_sd_state(SDState::BusyUploading);
if (SD.exists(pathWithGz)) {
path = pathWithGz;
}
File datafile = SD.open(path);
if (datafile) {
File datafile = SD.open(path);
if (datafile) {
vTaskDelay(1 / portTICK_RATE_MS);
size_t totalFileSize = datafile.size();
size_t i = 0;
bool done = false;
_webserver->setContentLength(totalFileSize);
_webserver->send(200, getContentType(path), "");
uint8_t buf[1024];
while (!done) {
vTaskDelay(1 / portTICK_RATE_MS);
size_t totalFileSize = datafile.size();
size_t i = 0;
bool done = false;
_webserver->setContentLength(totalFileSize);
_webserver->send(200, contentType, "");
uint8_t buf[1024];
while (!done) {
vTaskDelay(1 / portTICK_RATE_MS);
int v = datafile.read(buf, 1024);
if ((v == -1) || (v == 0)) {
done = true;
} else {
_webserver->client().write(buf, 1024);
i += v;
}

if (i >= totalFileSize) {
done = true;
}
int v = datafile.read(buf, 1024);
if ((v == -1) || (v == 0)) {
done = true;
} else {
_webserver->client().write(buf, 1024);
i += v;
}
datafile.close();
if (i != totalFileSize) {
//error: TBD

if (i >= totalFileSize) {
done = true;
}
set_sd_state(SDState::Idle);
return;
}
datafile.close();
if (i != totalFileSize) {
//error: TBD
}
set_sd_state(SDState::Idle);
return true;
}
String content = "cannot find ";
content += path;
_webserver->send(404, "text/plain", content);
return;
} else
set_sd_state(SDState::Idle);
}
String content = "cannot find ";
content += path;
_webserver->send(404, "text/plain", content);
return true;
# endif
}

void Web_Server::sendEmbeddedPage(String page) {
String IPAddress = (WiFi.getMode() == WIFI_STA) ? WiFi.localIP().toString() : WiFi.softAPIP().toString();
//Web address = ip + port
if (_port != 80) {
IPAddress += ":" + String(_port);
}
page.replace("$WEB_ADDRESS$", IPAddress);
page.replace("$QUERY$", _webserver->uri());
_webserver->send(200, "text/html", page);
}

// Returns true if this handler claimed the path
bool Web_Server::handleCaptivePortal() {
# ifndef ENABLE_CAPTIVE_PORTAL
return false;
# else
if (WiFi.getMode() == WIFI_AP) {
sendEmbeddedPage(PAGE_CAPTIVE);
//_webserver->sendContent_P(NOT_AUTH_NF);
//_webserver->client().stop();
return true;
}
return false;
# endif
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
}

// Returns true if this handler claimed the path
bool Web_Server::handleLocalFS(String path) {
String pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
if (SPIFFS.exists(pathWithGz)) {
path = pathWithGz;
}
File file = SPIFFS.open(path, FILE_READ);
_webserver->streamFile(file, contentType);
_webserver->streamFile(file, getContentType(path));
file.close();
return true;
}
return false;
}

// Handle not registered path
void Web_Server::handle_not_found() {
if (is_authenticated() == AuthenticationLevel::LEVEL_GUEST) {
_webserver->sendContent_P("HTTP/1.1 301 OK\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n");
//_webserver->client().stop();
return;
} else {
page_not_found = true;
}

if (page_not_found) {
# ifdef ENABLE_CAPTIVE_PORTAL
if (WiFi.getMode() == WIFI_AP) {
String contentType = PAGE_CAPTIVE;
String stmp = WiFi.softAPIP().toString();
//Web address = ip + port
String KEY_IP = "$WEB_ADDRESS$";
String KEY_QUERY = "$QUERY$";
if (_port != 80) {
stmp += ":";
stmp += String(_port);
}
contentType.replace(KEY_IP, stmp);
contentType.replace(KEY_IP, stmp);
contentType.replace(KEY_QUERY, _webserver->uri());
_webserver->send(200, "text/html", contentType);
//_webserver->sendContent_P(NOT_AUTH_NF);
//_webserver->client().stop();
return;
}
# endif
path = "/404.htm";
contentType = getContentType(path);
pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
if (SPIFFS.exists(pathWithGz)) {
path = pathWithGz;
}
File file = SPIFFS.open(path, FILE_READ);
_webserver->streamFile(file, contentType);
file.close();
String path = _webserver->urlDecode(_webserver->uri());

} else {
//if not template use default page
contentType = PAGE_404;
String stmp;
if (WiFi.getMode() == WIFI_STA) {
stmp = WiFi.localIP().toString();
} else {
stmp = WiFi.softAPIP().toString();
}
//Web address = ip + port
String KEY_IP = "$WEB_ADDRESS$";
String KEY_QUERY = "$QUERY$";
if (_port != 80) {
stmp += ":";
stmp += String(_port);
}
contentType.replace(KEY_IP, stmp);
contentType.replace(KEY_QUERY, _webserver->uri());
_webserver->send(200, "text/html", contentType);
}
if (handleSD(path)) {
return;
}

if (handleLocalFS(path)) {
return;
}

if (handleCaptivePortal()) {
return;
}

if (handleLocalFS("/404.htm")) {
return;
}

// if none of the above handlers succeeded, use the default page
sendEmbeddedPage(PAGE_404);
}

# ifdef ENABLE_SSDP
Expand Down Expand Up @@ -484,16 +484,17 @@ namespace WebUI {
} else {
espresponse->flush();
}
if(espresponse) delete(espresponse);
if (espresponse)
delete (espresponse);
} else { //execute GCODE
if (auth_level == AuthenticationLevel::LEVEL_GUEST) {
_webserver->send(401, "text/plain", "Authentication failed!\n");
return;
}
//Instead of send several commands one by one by web / send full set and split here
String scmd;
bool hasError =false;
uint8_t sindex = 0;
String scmd;
bool hasError = false;
uint8_t sindex = 0;
// TODO Settings - this is very inefficient. get_Splited_Value() is O(n^2)
// when it could easily be O(n). Also, it would be just as easy to push
// the entire string into Serial2Socket and pull off lines from there.
Expand All @@ -516,7 +517,7 @@ namespace WebUI {
hasError = true;
}
}
_webserver->send(200, "text/plain", hasError?"Error":"");
_webserver->send(200, "text/plain", hasError ? "Error" : "");
}
}

Expand Down Expand Up @@ -1224,9 +1225,9 @@ namespace WebUI {
String path = "/";
String sstatus = "Ok";
if ((_upload_status == UploadStatusType::FAILED) || (_upload_status == UploadStatusType::FAILED)) {
sstatus = "Upload failed";
sstatus = "Upload failed";
}
_upload_status = UploadStatusType::NONE;
_upload_status = UploadStatusType::NONE;
bool list_files = true;
uint64_t totalspace = 0;
uint64_t usedspace = 0;
Expand Down Expand Up @@ -1633,6 +1634,10 @@ namespace WebUI {
return "text/html";
} else if (file_name.endsWith(".html")) {
return "text/html";
} else if (file_name.endsWith(".htm.gz")) {
return "text/html";
} else if (file_name.endsWith(".html.gz")) {
return "text/html";
} else if (file_name.endsWith(".css")) {
return "text/css";
} else if (file_name.endsWith(".js")) {
Expand Down
4 changes: 4 additions & 0 deletions Grbl_Esp32/src/WebUI/WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ namespace WebUI {
static void SDFile_direct_upload();
static bool deleteRecursive(String path);
#endif
static bool handleSD(String path);
static bool handleLocalFS(String path);
static bool handleCaptivePortal();
static void sendEmbeddedPage(String page);
};

extern Web_Server web_server;
Expand Down