This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
forked from sipin/xingyun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
context_static.go
175 lines (152 loc) · 3.18 KB
/
context_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package xingyun
import (
"crypto/md5"
"fmt"
"io/ioutil"
"strings"
)
func (ctx *Context) GetStaticUrl(url string) string {
cfg := ctx.Config
if url[0] != '/' {
return url
}
if cfg.StaticHost == "" {
return url
}
if ctx.isExcludeFile(url) {
return url
}
if ctx.isExcludeType(url) {
return url
}
hash := ctx.getStaticFileHash(url)
if hash == "" {
return cfg.StaticHost + url
}
if cfg.StaticHost == "/" {
return url + "?hash=" + hash
}
return cfg.StaticHost + url + "?hash=" + hash
}
func isIE(version, user_agent string) bool {
key := "MSIE " + version
return strings.Contains(user_agent, key)
}
var browserMatchMap = map[string]func(user_agent string) bool{
"ie8": func(user_agent string) bool { return isIE("8.0", user_agent) },
"ie9": func(user_agent string) bool { return isIE("9.0", user_agent) },
}
func browserMatch(t, user_agent string) bool {
if user_agent == "" {
return false
}
if t == "" {
return true
}
handler, ok := browserMatchMap[t]
if !ok {
return false
}
return handler(user_agent)
}
func splitExclude(s string) (t, v string) {
if !strings.Contains(s, ":") {
return "", s
}
ss := strings.Split(s, ":")
return ss[0], ss[1]
}
func (ctx *Context) GetUserAgent() string {
agents := ctx.Request.Header["User-Agent"]
if len(agents) > 0 {
return agents[0]
}
return ""
}
func (ctx *Context) isExcludeType(url string) bool {
types := strings.Split(ctx.Config.StaticHostExcludeType, ",")
for _, t := range types {
if t == "" {
continue
}
t1, v := splitExclude(t)
agent := ctx.GetUserAgent()
if browserMatch(t1, agent) && strings.HasSuffix(url, v) {
return true
}
}
return false
}
func (ctx *Context) isExcludeFile(url string) bool {
files := strings.Split(ctx.Config.StaticHostExcludeFile, ",")
for _, f := range files {
if f == "" {
continue
}
t1, v := splitExclude(f)
if browserMatch(t1, ctx.GetUserAgent()) && url == v {
return true
}
}
return false
}
func (ctx *Context) getStaticFileHash(name string) string {
dir := ctx.Server.StaticDir
if dir == nil {
return ""
}
f, err := dir.Open(name)
if err != nil {
return ""
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return ""
}
if stat.IsDir() {
return ""
}
data, err := ioutil.ReadAll(f)
if err != nil {
return ""
}
return fmt.Sprintf("%x", md5.Sum(data))
}
func (ctx *Context) addDataUnique(dataType string, values ...string) {
var data []string
var ok bool
if ctx.staticData == nil {
ctx.staticData = make(map[string][]string)
}
if data, ok = ctx.staticData[dataType]; !ok {
data = []string{}
}
for _, val := range values {
isNewVal := true
for _, ele := range data {
if ele == val {
isNewVal = false
}
}
if isNewVal {
data = append(data, val)
}
}
ctx.staticData[dataType] = data
}
func (ctx *Context) getData(dataType string) (values []string) {
return ctx.staticData[dataType]
}
func (ctx *Context) AddJS(val ...string) {
ctx.addDataUnique("js", val...)
}
func (ctx *Context) GetJS() (values []string) {
return ctx.getData("js")
}
func (ctx *Context) AddCSS(val ...string) {
ctx.addDataUnique("css", val...)
}
func (ctx *Context) GetCSS() (values []string) {
return ctx.getData("css")
}