Browse Source

Updated Not Found page

Toby Chui 3 years ago
parent
commit
5800b22451
4 changed files with 103 additions and 11 deletions
  1. 41 2
      error.go
  2. 13 8
      main.router.go
  3. 1 1
      web/SystemAO/closeTabInsturction.html
  4. 48 0
      web/SystemAO/notfound.html

+ 41 - 2
error.go

@@ -5,10 +5,49 @@ package main
 
 */
 
-import "net/http"
+import (
+	"io/ioutil"
+	"net/http"
+	"strings"
+)
 
 func errorHandleNotFound(w http.ResponseWriter, r *http.Request) {
-	http.NotFound(w, r)
+	notFoundPage := "./web/SystemAO/notfound.html"
+	if fileExists(notFoundPage) {
+
+		notFoundTemplateBytes, err := ioutil.ReadFile(notFoundPage)
+		notFoundTemplate := string(notFoundTemplateBytes)
+		if err != nil {
+			http.NotFound(w, r)
+		} else {
+			//Replace the request URL inside the page
+			notFoundTemplate = strings.ReplaceAll(notFoundTemplate, "{{request_url}}", r.RequestURI)
+			rel := getRootEscapeFromCurrentPath(r.RequestURI)
+			notFoundTemplate = strings.ReplaceAll(notFoundTemplate, "{{root_escape}}", rel)
+			w.WriteHeader(http.StatusNotFound)
+			w.Write([]byte(notFoundTemplate))
+		}
+	} else {
+		http.NotFound(w, r)
+	}
+
+}
+
+//Get escape root path, example /asd/asd => ../../
+func getRootEscapeFromCurrentPath(requestURL string) string {
+	rel := ""
+	if !strings.Contains(requestURL, "/") {
+		return ""
+	}
+	splitter := requestURL
+	if splitter[len(splitter)-1:] != "/" {
+		splitter = splitter + "/"
+	}
+	for i := 0; i < len(strings.Split(splitter, "/"))-2; i++ {
+		rel += "../"
+	}
+
+	return rel
 }
 
 func errorHandleNotLoggedIn(w http.ResponseWriter, r *http.Request) {

+ 13 - 8
main.router.go

@@ -76,25 +76,25 @@ func mrouter(h http.Handler) http.Handler {
 			} else {
 				interfaceModule := userinfo.GetInterfaceModules()
 				if len(interfaceModule) == 1 && interfaceModule[0] == "Desktop" {
-					http.Redirect(w, r, "./desktop.system", 307)
+					http.Redirect(w, r, "./desktop.system", http.StatusTemporaryRedirect)
 				} else if len(interfaceModule) == 1 {
 					//User with default interface module not desktop
 					modileInfo := moduleHandler.GetModuleInfoByID(interfaceModule[0])
 					if modileInfo == nil {
 						//The module is not found or not enabled
-						http.Redirect(w, r, "./SystemAO/boot/interface_disabled.html", 307)
+						http.Redirect(w, r, "./SystemAO/boot/interface_disabled.html", http.StatusTemporaryRedirect)
 						return
 					}
-					http.Redirect(w, r, modileInfo.StartDir, 307)
+					http.Redirect(w, r, modileInfo.StartDir, http.StatusTemporaryRedirect)
 				} else if len(interfaceModule) > 1 {
 					//Redirect to module selector
-					http.Redirect(w, r, "./SystemAO/boot/interface_selector.html", 307)
+					http.Redirect(w, r, "./SystemAO/boot/interface_selector.html", http.StatusTemporaryRedirect)
 				} else if len(interfaceModule) == 0 {
 					//Redirect to error page
-					http.Redirect(w, r, "./SystemAO/boot/no_interfaceing.html", 307)
+					http.Redirect(w, r, "./SystemAO/boot/no_interfaceing.html", http.StatusTemporaryRedirect)
 				} else {
 					//For unknown operations, send it to desktop
-					http.Redirect(w, r, "./desktop.system", 307)
+					http.Redirect(w, r, "./desktop.system", http.StatusTemporaryRedirect)
 				}
 			}
 		} else if ((len(r.URL.Path) >= 5 && r.URL.Path[:5] == "/www/") || r.URL.Path == "/www") && *allow_homepage == true {
@@ -112,7 +112,7 @@ func mrouter(h http.Handler) http.Handler {
 				w.Header().Add("Content-Type", "application/javascript; charset=UTF-8")
 			}
 
-			if *disable_subservices == false {
+			if !*disable_subservices {
 				//Enable subservice access
 				//Check if this path is reverse proxy path. If yes, serve with proxyserver
 				isRP, proxy, rewriteURL, subserviceObject := ssRouter.CheckIfReverseProxyPath(r)
@@ -125,7 +125,7 @@ func mrouter(h http.Handler) http.Handler {
 			}
 
 			//Not subservice routine. Handle file server
-			if *enable_dir_listing == false {
+			if !*enable_dir_listing {
 				if strings.HasSuffix(r.URL.Path, "/") {
 					//User trying to access a directory. Send NOT FOUND.
 					if fileExists("web" + r.URL.Path + "index.html") {
@@ -137,6 +137,11 @@ func mrouter(h http.Handler) http.Handler {
 					}
 				}
 			}
+			if !fileExists("web" + r.URL.Path) {
+				//File not found
+				errorHandleNotFound(w, r)
+				return
+			}
 			h.ServeHTTP(w, r)
 
 		} else {

+ 1 - 1
web/SystemAO/closeTabInsturction.html

@@ -26,7 +26,7 @@
           <script>
 
             $('.ui.basic.modal').modal('show');
-            
+           
             //If this tab is opened in fw mode accidentally, close this tab.
             if (!(!parent.isDesktopMode)){
                 var ao_module_windowID =  $(window.frameElement).parent().parent().attr("windowId");

+ 48 - 0
web/SystemAO/notfound.html

@@ -0,0 +1,48 @@
+<html>
+    <head>
+        <meta charset="UTF-8">
+        <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
+        <link rel="stylesheet" href="{{root_escape}}script/semantic/semantic.min.css">
+        <script type="text/javascript" src="{{root_escape}}script/jquery.min.js"></script>
+        <script type="text/javascript" src="{{root_escape}}script/semantic/semantic.min.js"></script>
+        <title>Not Found</title>
+        <style>
+            #msg{
+                position: absolute;
+                top: calc(50% - 150px);
+                left: calc(50% - 250px);
+                width: 500px;
+                height: 300px;
+                text-align: center;
+            }
+
+            #footer{
+                position: fixed;
+                padding: 2em;
+                padding-left: 5em;
+                padding-right: 5em;
+                bottom: 0px;
+                left: 0px;
+                width: 100%;
+            }   
+        </style>
+    </head>
+    <body>
+        <div id="msg">
+            <h1 style="font-size: 6em; margin-bottom: 0px;">404</h1>
+            <div>
+                <h3 class="">Page Not Found</h3>
+                <div class="ui divider"></div>
+                <p>The page you are looking for do not exists on this server. <br><a href="../">Back</a></p>
+                <div class="ui divider"></div>
+                <div style="text-align: left;">
+                    <small>Request time: <span id="reqtime"></span></small><br>
+                    <small id="reqURLDisplay">Request URI: {{request_url}}</small>
+                </div>
+            </div>
+        </div>
+        <script>
+            $("#reqtime").text(new Date().toLocaleString(undefined, {year: 'numeric', month: '2-digit', day: '2-digit', weekday:"long", hour: '2-digit', hour12: false, minute:'2-digit', second:'2-digit'}));
+        </script>
+    </body>
+</html>