blob: 3bd69160a7201d82c12b969c2c9d988da48eee51 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
}
|