-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_static.go
92 lines (79 loc) · 2.03 KB
/
handler_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cooking
import (
"net/http"
"os"
"path"
"regexp"
"strings"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
var (
validFilenameCharacters = regexp.MustCompile(`^[a-zA-Z0-9.\-_]+$`)
)
func init() {
PublicHandlers["/static/{filename}"] = handleStatic
PublicHandlers["/favicon.ico"] = handleStatic
}
func sanitizeFilename(filename string) (string, bool) {
filename = strings.Replace(filename, "\n", "", -1)
filename = strings.Replace(filename, "\r", "", -1)
return filename, validFilenameCharacters.MatchString(filename)
}
func handleStatic(server *Server) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
var filename string
var ok bool
if strings.HasSuffix(request.URL.Path, "favicon.ico") {
filename = "favicon.ico"
} else {
filename, ok = mux.Vars(request)["filename"]
if !ok {
writer.WriteHeader(http.StatusNotFound)
return
}
}
filename, ok = sanitizeFilename(filename)
if !ok {
logrus.Debugf("filename %q does not match allowed pattern", filename)
writer.WriteHeader(http.StatusNotFound)
return
}
filename = path.Base(filename)
if filename == "." || filename == "/" {
writer.WriteHeader(http.StatusNotFound)
return
}
filepath := path.Join(server.Config.StaticDir, filename)
content, err := os.ReadFile(filepath)
if err != nil {
logrus.Errorf("cannnot open file %s: %s", filename, err)
writer.WriteHeader(http.StatusNotFound)
return
}
ext := path.Ext(filename)
var contentType string
switch ext {
case ".css":
contentType = "text/css"
case ".js":
contentType = "application/javascript"
case ".jpg":
contentType = "image/jpeg"
case ".gif":
contentType = "image/gif"
case ".svg":
contentType = "image/svg+xml"
case ".png":
contentType = "image/png"
case ".ico":
contentType = "image/x-icon"
default:
logrus.Errorf("%s is not an allowed type", ext)
writer.WriteHeader(http.StatusNotFound)
return
}
writer.Header().Add("Content-Type", contentType)
writer.Write(content)
}
}