-
Notifications
You must be signed in to change notification settings - Fork 5
/
pipe_static.go
67 lines (59 loc) · 1.22 KB
/
pipe_static.go
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
58
59
60
61
62
63
64
65
66
67
package xingyun
import (
"net/http"
"path"
"strings"
)
func (s *Server) GetStaticPipeHandler() PipeHandler {
return PipeHandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
s.logger.Tracef("enter static handler")
defer s.logger.Tracef("exit static handler")
cfg := s.Config
if r.Method != "GET" && r.Method != "HEAD" {
next(rw, r)
return
}
file := r.URL.Path
if cfg.StaticPrefix != "" {
if !strings.HasPrefix(file, cfg.StaticPrefix) {
next(rw, r)
return
}
file = file[len(cfg.StaticPrefix):]
if file != "" && file[0] != '/' {
next(rw, r)
return
}
}
f, err := s.StaticDir.Open(file)
if err != nil {
next(rw, r)
return
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
next(rw, r)
return
}
if fi.IsDir() {
if !strings.HasSuffix(r.URL.Path, "/") {
http.Redirect(rw, r, r.URL.Path+"/", http.StatusFound)
return
}
file = path.Join(file, cfg.StaticIndexFile)
f, err = s.StaticDir.Open(file)
if err != nil {
next(rw, r)
return
}
defer f.Close()
fi, err = f.Stat()
if err != nil || fi.IsDir() {
next(rw, r)
return
}
}
http.ServeContent(rw, r, file, fi.ModTime(), f)
})
}