-
Notifications
You must be signed in to change notification settings - Fork 56
/
util.go
80 lines (67 loc) · 1.75 KB
/
util.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
package serverHandler
import (
"../acceptHeaders"
"compress/flate"
"compress/gzip"
"io"
"net/http"
"os"
"path"
"strings"
)
func needResponseBody(method string) bool {
return method != http.MethodHead &&
method != http.MethodOptions &&
method != http.MethodConnect &&
method != http.MethodTrace
}
func containsItem(infos []os.FileInfo, name string) bool {
for i := range infos {
if infos[i].Name() == name {
return true
}
}
return false
}
func getCleanFilePath(requestPath string) (filePath string, ok bool) {
filePath = path.Clean(requestPath)
ok = filePath == path.Base(filePath)
return
}
func getCleanDirFilePath(requestPath string) (filePath string, ok bool) {
filePath = path.Clean(strings.Replace(requestPath, "\\", "/", -1))
ok = filePath[0] != '/' && filePath != "." && filePath != ".." && !strings.HasPrefix(filePath, "../")
return
}
const contentEncGzip = "gzip"
const contentEncDeflate = "deflate"
var encodings = []string{contentEncGzip, contentEncDeflate}
func getCompressWriter(w http.ResponseWriter, r *http.Request) (wr io.WriteCloser, encoding string, ok bool) {
accepts := acceptHeaders.ParseAccepts(r.Header.Get("Accept-Encoding"))
_, encoding, ok = accepts.GetPreferredValue(encodings)
if !ok {
return nil, "", false
}
var err error
switch encoding {
case contentEncGzip:
wr, err = gzip.NewWriterLevel(w, flate.BestSpeed)
case contentEncDeflate:
wr, err = flate.NewWriter(w, flate.BestSpeed)
default:
return nil, "", false
}
if err != nil {
return nil, "", false
}
return wr, encoding, true
}
func isVirtual(info os.FileInfo) bool {
if _, isRenamedInfo := info.(*renamedFileInfo); isRenamedInfo {
return true
}
if _, isFakeInfo := info.(*fakeFileInfo); isFakeInfo {
return true
}
return false
}