-
Notifications
You must be signed in to change notification settings - Fork 56
/
util.go
94 lines (78 loc) · 2.12 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package serverHandler
import (
"mjpclab.dev/ghfs/src/shimgo"
"mjpclab.dev/ghfs/src/util"
"net/http"
"os"
"path"
"regexp"
"strings"
)
func wildcardToRegexp(wildcards []string) (*regexp.Regexp, error) {
if len(wildcards) == 0 {
return nil, nil
}
normalizedWildcards := make([]string, 0, len(wildcards))
for _, wildcard := range wildcards {
if len(wildcard) == 0 {
continue
}
normalizedWildcards = append(normalizedWildcards, util.WildcardToStrRegexp(wildcard))
}
if len(normalizedWildcards) == 0 {
return nil, nil
}
exp := strings.Join(normalizedWildcards, "|")
return regexp.Compile(exp)
}
func getRedirectCode(r *http.Request) int {
if r.Method == shimgo.Net_Http_MethodPost {
return http.StatusTemporaryRedirect
} else {
return http.StatusMovedPermanently
}
}
func NeedResponseBody(method string) bool {
return method != shimgo.Net_Http_MethodHead &&
method != shimgo.Net_Http_MethodOptions &&
method != shimgo.Net_Http_MethodConnect &&
method != shimgo.Net_Http_MethodTrace
}
func lacksHeader(header http.Header, key string) bool {
return len(header.Get(key)) == 0
}
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
}
func createVirtualFileInfo(name string, refItem os.FileInfo) os.FileInfo {
if refItem != nil {
return createRenamedFileInfo(name, refItem)
} else {
return createPlaceholderFileInfo(name, true)
}
}
func isVirtual(info os.FileInfo) bool {
switch info.(type) {
case placeholderFileInfo, renamedFileInfo:
return true
}
return false
}
func containsItem(infos []os.FileInfo, name string) bool {
for i := range infos {
if util.IsPathEqual(infos[i].Name(), name) {
return true
}
}
return false
}
func shouldServeAsContent(file *os.File, item os.FileInfo) bool {
return file != nil && item != nil && !item.IsDir()
}