Эх сурвалжийг харах

Fixed personal homepage bug

Toby Chui 3 жил өмнө
parent
commit
1a97e5ba28

+ 2 - 3
mod/filesystem/filesystem.go

@@ -229,16 +229,15 @@ func (fsh *FileSystemHandler) GetUniquePathHash(vpath string, username string) (
 	fshAbs := fsh.FileSystemAbstraction
 	rpath := ""
 	if strings.Contains(vpath, ":/") {
-		rpath, err := fshAbs.VirtualPathToRealPath(vpath, username)
+		r, err := fshAbs.VirtualPathToRealPath(vpath, username)
 		if err != nil {
 			return "", err
 		}
-		rpath = filepath.ToSlash(rpath)
+		rpath = filepath.ToSlash(r)
 	} else {
 		//Passed in realpath as vpath.
 		rpath = vpath
 	}
-
 	hash := md5.Sum([]byte(fsh.UUID + "_" + rpath))
 	return hex.EncodeToString(hash[:]), nil
 }

+ 36 - 8
mod/www/www.go

@@ -1,6 +1,7 @@
 package www
 
 import (
+	"io"
 	"io/ioutil"
 	"net/http"
 	"net/url"
@@ -66,7 +67,7 @@ func (h *Handler) RouteRequest(w http.ResponseWriter, r *http.Request) {
 	parsedRequestURL := strings.Split(filepath.ToSlash(filepath.Clean(decodedValue)[1:]), "/")
 	//Malparsed URL. Ignore request
 	if len(parsedRequestURL) < 2 {
-		http.NotFound(w, r)
+		serveNotFoundTemplate(w, r)
 		return
 	}
 
@@ -75,15 +76,14 @@ func (h *Handler) RouteRequest(w http.ResponseWriter, r *http.Request) {
 
 	userinfo, err := h.Options.UserHandler.GetUserInfoFromUsername(username)
 	if err != nil {
-		http.NotFound(w, r)
+		serveNotFoundTemplate(w, r)
 		return
 	}
 
 	//Check if this user enabled homepage
 	enabled := h.CheckUserHomePageEnabled(userinfo.Username)
 	if !enabled {
-		w.WriteHeader(http.StatusNotFound)
-		w.Write([]byte("404 - Page not found"))
+		serveNotFoundTemplate(w, r)
 		return
 	}
 
@@ -115,9 +115,8 @@ func (h *Handler) RouteRequest(w http.ResponseWriter, r *http.Request) {
 	targetFilePath := filepath.ToSlash(filepath.Join(webrootRealpath, rewrittenPath))
 
 	//Check if the file exists
-	if !fileExists(targetFilePath) {
-		w.WriteHeader(http.StatusNotFound)
-		w.Write([]byte("404 - Page not found"))
+	if !fsh.FileSystemAbstraction.FileExists(targetFilePath) {
+		serveNotFoundTemplate(w, r)
 		return
 	}
 
@@ -126,10 +125,39 @@ func (h *Handler) RouteRequest(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Content-Type", "application/javascript")
 	}
 
+	if filepath.Ext(targetFilePath) == "" {
+		//Reading a folder. Check if index.htm or index.html exists.
+		if fsh.FileSystemAbstraction.FileExists(filepath.Join(targetFilePath, "index.html")) {
+			targetFilePath = filepath.ToSlash(filepath.Join(targetFilePath, "index.html"))
+		} else if fsh.FileSystemAbstraction.FileExists(filepath.Join(targetFilePath, "index.htm")) {
+			targetFilePath = filepath.ToSlash(filepath.Join(targetFilePath, "index.htm"))
+		} else {
+			//Not allow listing folder
+			http.ServeFile(w, r, "system/errors/forbidden.html")
+			return
+		}
+
+	}
+
 	//Record the client IP for analysis, to be added in the future if needed
 
 	//Serve the file
-	http.ServeFile(w, r, targetFilePath)
+	if fileExists(targetFilePath) {
+		http.ServeFile(w, r, targetFilePath)
+	} else {
+		f, err := fsh.FileSystemAbstraction.ReadStream(targetFilePath)
+		if err != nil {
+			w.Write([]byte(err.Error()))
+			return
+		}
+		io.Copy(w, f)
+		f.Close()
+	}
+
+}
+
+func serveNotFoundTemplate(w http.ResponseWriter, r *http.Request) {
+	http.ServeFile(w, r, "system/errors/notfound.html")
 }
 
 func handleWebrootError(w http.ResponseWriter) {

+ 51 - 0
system/errors/forbidden.html

@@ -0,0 +1,51 @@
+<html>
+    <head>
+        <meta charset="UTF-8">
+        <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
+        <link rel="stylesheet" href="/script/semantic/semantic.min.css">
+        <script type="text/javascript" src="/script/jquery.min.js"></script>
+        <script type="text/javascript" src="/script/semantic/semantic.min.js"></script>
+        <title>Forbidden</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%;
+            }   
+
+            small{
+                word-break: break-word;
+            }
+        </style>
+    </head>
+    <body>
+        <div id="msg">
+            <h1 style="font-size: 6em; margin-bottom: 0px;">403</h1>
+            <div>
+                <h3 class="">Forbidden</h3>
+                <div class="ui divider"></div>
+                <p>We cannot show you the content of the URL you requested. That is all we know.</p>
+                <div class="ui divider"></div>
+                <div style="text-align: left;">
+                    <small>Request time: <span id="reqtime"></span></small><br>
+                </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>

+ 51 - 0
system/errors/notfound.html

@@ -0,0 +1,51 @@
+<html>
+    <head>
+        <meta charset="UTF-8">
+        <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
+        <link rel="stylesheet" href="/script/semantic/semantic.min.css">
+        <script type="text/javascript" src="/script/jquery.min.js"></script>
+        <script type="text/javascript" src="/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%;
+            }   
+
+            small{
+                word-break: break-word;
+            }
+        </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 or resource you are trying to access does not exist.</p>
+                <div class="ui divider"></div>
+                <div style="text-align: left;">
+                    <small>Request time: <span id="reqtime"></span></small><br>
+                </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>

+ 52 - 0
system/errors/unauthorized.html

@@ -0,0 +1,52 @@
+<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>Unauthorized</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%;
+            }   
+
+            small{
+                word-break: break-word;
+            }
+        </style>
+    </head>
+    <body>
+        <div id="msg">
+            <h1 style="font-size: 6em; margin-bottom: 0px;">401</h1>
+            <div>
+                <h3 class="">Unauthorized</h3>
+                <div class="ui divider"></div>
+                <p>You do not have permission to view this directory or page. <br><a href="{{root_escape}}">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>