diff options
Diffstat (limited to 'http/static.go')
-rw-r--r-- | http/static.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/http/static.go b/http/static.go new file mode 100644 index 00000000..3bd69160 --- /dev/null +++ b/http/static.go @@ -0,0 +1,57 @@ +package http + +import ( + "github.com/sirupsen/logrus" + "net/http" + "os" + "path" + "strings" +) + +// staticServer serves static files +type staticServer struct { + cfg *FsConfig + root http.Dir +} + +// serve attempts to serve static file and returns true in case of success, will return false in case if file not +// found, not allowed or on read error. +func (svr *staticServer) serve(w http.ResponseWriter, r *http.Request) bool { + fPath := r.URL.Path + if !strings.HasPrefix(fPath, "/") { + fPath = "/" + fPath + } + fPath = path.Clean(fPath) + + if svr.cfg.Forbids(fPath) { + logrus.Warningf("attempt to access forbidden file %s", fPath) // todo: better logs + return false + } + + f, err := svr.root.Open(fPath) + if err != nil { + if !os.IsNotExist(err) { + logrus.Error(err) //todo: rr or access error + } + + return false + } + defer f.Close() + + d, err := f.Stat() + if err != nil { + logrus.Error(err) //todo: rr or access error + + // todo: do i need it, bypass log? + + return false + } + + if d.IsDir() { + // do not serve directories + return false + } + + http.ServeContent(w, r, d.Name(), d.ModTime(), f) + return true +} |