From ba73f4adf3f931549da08acb7365ce8ed04a2bd6 Mon Sep 17 00:00:00 2001 From: andreimarcu Date: Wed, 30 Sep 2015 15:54:30 -0400 Subject: [PATCH] Fix static directory listing recursion --- fileserve.go | 18 ++++++++++++++++++ server.go | 10 +++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/fileserve.go b/fileserve.go index 4e9e1da..45a0e63 100644 --- a/fileserve.go +++ b/fileserve.go @@ -29,6 +29,24 @@ func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filePath) } +func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + if path[len(path)-1:] == "/" { + notFoundHandler(c, w, r) + return + } else { + filePath := strings.TrimPrefix(path, "/static/") + file, err := staticBox.Open(filePath) + if err != nil { + oopsHandler(c, w, r) + return + } + + http.ServeContent(w, r, filePath, timeStarted, file) + return + } +} + func fileExistsAndNotExpired(filename string) bool { filePath := path.Join(Config.filesDir, filename) diff --git a/server.go b/server.go index 98fdd53..963aa3a 100644 --- a/server.go +++ b/server.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "regexp" + "time" "github.com/GeertJohan/go.rice" "github.com/flosch/pongo2" @@ -27,6 +28,8 @@ var Config struct { var Templates = make(map[string]*pongo2.Template) var TemplateSet *pongo2.TemplateSet +var staticBox *rice.Box +var timeStarted time.Time func setup() { if Config.noLogs { @@ -65,6 +68,9 @@ func setup() { os.Exit(1) } + staticBox = rice.MustFindBox("static") + timeStarted = time.Now() + // Routing setup nameRe := regexp.MustCompile(`^/(?P[a-z0-9-\.]+)$`) selifRe := regexp.MustCompile(`^/selif/(?P[a-z0-9-\.]+)$`) @@ -81,9 +87,7 @@ func setup() { goji.Put("/upload/:name", uploadPutHandler) goji.Delete("/:name", deleteHandler) - staticBox := rice.MustFindBox("static") - goji.Get("/static/*", http.StripPrefix("/static/", - http.FileServer(staticBox.HTTPBox()))) + goji.Get("/static/*", staticHandler) goji.Get(nameRe, fileDisplayHandler) goji.Get(selifRe, fileServeHandler) goji.Get(selifIndexRe, unauthorizedHandler)