-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
static.go
62 lines (55 loc) · 1.28 KB
/
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
package goyave
import (
"io/fs"
"net/http"
"path/filepath"
"strings"
"github.com/samber/lo"
"goyave.dev/goyave/v5/util/fsutil"
)
func staticHandler(fs fs.StatFS, download bool) Handler {
return func(response *Response, r *Request) {
file := r.RouteParams["resource"]
if !checkStaticPath(file) {
response.Status(http.StatusNotFound)
return
}
path := cleanStaticPath(fs, file)
if download {
response.Download(fs, path, path[lo.Clamp(strings.LastIndex(file, "/"), 0, len(path)):])
return
}
response.File(fs, path)
}
}
func cleanStaticPath(fs fs.StatFS, file string) string {
file = strings.TrimPrefix(file, "/")
path := file
if path == "" {
return "index.html"
}
if fsutil.IsDirectory(fs, strings.TrimSuffix(path, "/")) {
if !strings.HasSuffix(path, "/") {
path += "/"
}
path += "index.html"
}
return filepath.Clean(path)
}
func checkStaticPath(path string) bool {
if !strings.HasPrefix(path, "/") && len(path) > 0 { // Force leading slash if the path is not empty
return false
}
if strings.Contains(path, "\\") || strings.Contains(path, "//") {
return false
}
for _, ent := range strings.FieldsFunc(path, isSlash) {
if ent == "." || ent == ".." || ent == "" {
return false
}
}
return true
}
func isSlash(r rune) bool {
return r == '/'
}