Fix static directory listing recursion

This commit is contained in:
andreimarcu 2015-09-30 15:54:30 -04:00
parent d55cf33a5d
commit ba73f4adf3
2 changed files with 25 additions and 3 deletions

View File

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

View File

@ -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<name>[a-z0-9-\.]+)$`)
selifRe := regexp.MustCompile(`^/selif/(?P<name>[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)