Browse Source

Fix static directory listing recursion

pull/23/head v0.4.1
andreimarcu 9 years ago
parent
commit
ba73f4adf3
  1. 18
      fileserve.go
  2. 10
      server.go

18
fileserve.go

@ -29,6 +29,24 @@ func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filePath) 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 { func fileExistsAndNotExpired(filename string) bool {
filePath := path.Join(Config.filesDir, filename) filePath := path.Join(Config.filesDir, filename)

10
server.go

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
"time"
"github.com/GeertJohan/go.rice" "github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
@ -27,6 +28,8 @@ var Config struct {
var Templates = make(map[string]*pongo2.Template) var Templates = make(map[string]*pongo2.Template)
var TemplateSet *pongo2.TemplateSet var TemplateSet *pongo2.TemplateSet
var staticBox *rice.Box
var timeStarted time.Time
func setup() { func setup() {
if Config.noLogs { if Config.noLogs {
@ -65,6 +68,9 @@ func setup() {
os.Exit(1) os.Exit(1)
} }
staticBox = rice.MustFindBox("static")
timeStarted = time.Now()
// Routing setup // Routing setup
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`) nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
selifRe := regexp.MustCompile(`^/selif/(?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.Put("/upload/:name", uploadPutHandler)
goji.Delete("/:name", deleteHandler) 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(nameRe, fileDisplayHandler)
goji.Get(selifRe, fileServeHandler) goji.Get(selifRe, fileServeHandler)
goji.Get(selifIndexRe, unauthorizedHandler) goji.Get(selifIndexRe, unauthorizedHandler)

Loading…
Cancel
Save