-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic.go
More file actions
178 lines (135 loc) · 3.38 KB
/
static.go
File metadata and controls
178 lines (135 loc) · 3.38 KB
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
176
177
178
package mod
import (
"mime"
"net/http"
"path"
"strings"
)
/**
GzipPre 发送静态预压缩文件, root 路径由调用者传递.
依据:
Request.Method 为 GET/HEAD
Request.Header 含 "Accept-Encoding" 支持 "gzip".
URL.Path 有扩展名.
exts 非空, 扩展名在 exts 内.
对应的 ".gz" 预压缩文件存在.
参数:
exts 指定文件扩展名, 如果为空, 表示尝试所有文件.
返回:
文件被发送返回 true, 否则返回 false
注意:
确保相应文件类型已注册 mime Content-Type.
*/
func GzipPreInExts(exts string) Dir {
return Dir(func(root http.Dir,
rw http.ResponseWriter, req *http.Request) bool {
if req.Method != "GET" && req.Method != "HEAD" {
return false
}
// Accept-Encoding:gzip,deflate,sdch
if strings.Index(
req.Header.Get("Accept-Encoding"), "gzip") == -1 {
return false
}
ext := path.Ext(req.URL.Path)
if ext == "" || len(exts) != 0 && ExtIndex(exts, ext) == -1 {
return false
}
name := req.URL.Path
basename := name[strings.LastIndex(name, "/")+1:]
if ext == ".gz" {
oext := path.Ext(name[:len(name)-3])
if oext != "" {
ext = oext
basename = basename[:len(basename)-3]
}
} else {
name += ".gz"
}
f, e := root.Open(name)
if e != nil {
return false
}
defer f.Close()
fi, e := f.Stat()
if e != nil || fi.IsDir() {
return false
}
ctype := mime.TypeByExtension(ext)
if ctype != "" {
rw.Header().Set("Content-Type", ctype)
} else if ext == ".gz" {
rw.Header().Set("Content-Type", "application/gzip")
}
rw.Header().Set("Content-Encoding", "gzip")
http.ServeContent(rw, req, basename, fi.ModTime(), f)
return true
})
}
/**
GzipPre 调用 GzipPreInExts 发送静态预压缩文件.
参数:
root 指定文档根路径
exts 指定文件扩展名, 如果为空, 表示尝试所有文件.
返回:
文件被发送返回 true, 否则返回 false
*/
func GzipPre(root http.Dir, exts string) Finish {
finish := GzipPreInExts(exts)
return Finish(func(
rw http.ResponseWriter, req *http.Request) bool {
return finish.ServeHTTP(root, rw, req)
})
}
/**
StaticInExts 发送静态文件, root 路径由调用者传递.
依据:
Request.Method 为 GET/HEAD
URL.Path 有扩展名.
exts 非空, 扩展名在 exts 内.
文件存在.
参数:
exts 指定文件扩展名, 如果为空, 表示尝试所有文件.
返回:
文件被发送返回 true, 否则返回 false
*/
func StaticInExts(exts string) Dir {
return Dir(func(root http.Dir,
rw http.ResponseWriter, req *http.Request) bool {
if req.Method != "GET" && req.Method != "HEAD" {
return false
}
ext := path.Ext(req.URL.Path)
if ext == "" || len(exts) != 0 && ExtIndex(exts, ext) == -1 {
return false
}
name := req.URL.Path
f, e := root.Open(name)
if e != nil {
return false
}
defer f.Close()
fi, e := f.Stat()
if e != nil || fi.IsDir() {
return false
}
http.ServeContent(rw, req,
name[strings.LastIndex(name, "/")+1:], fi.ModTime(), f)
return true
})
}
/**
Static 调用 StaticInExts 发送静态文件.
参数:
root 指定文档根路径
exts 指定文件扩展名, 如果为空, 表示尝试所有文件.
返回:
文件被发送返回 true, 否则返回 false
*/
func Static(root http.Dir, exts string) Finish {
finish := StaticInExts(exts)
return Finish(func(
rw http.ResponseWriter, req *http.Request) bool {
return finish.ServeHTTP(root, rw, req)
})
}