|
@@ -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) {
|