This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_serve.go
269 lines (246 loc) · 6.47 KB
/
static_serve.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2018 tree xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package staticserve
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/vicanso/elton"
"github.com/vicanso/hes"
)
type (
// StaticFile static file
StaticFile interface {
Exists(string) bool
Get(string) ([]byte, error)
Stat(string) os.FileInfo
NewReader(string) (io.Reader, error)
}
// Config static serve config
Config struct {
// 静态文件目录
Path string
// http cache control max age
MaxAge int
// http cache control s-maxage
SMaxAge int
// http response header
Header map[string]string
// 禁止query string(因为有时静态文件为CDN回源,避免生成各种重复的缓存)
DenyQueryString bool
// 是否禁止文件路径以.开头(因为这些文件有可能包括重要信息)
DenyDot bool
// 是否使用strong etag
EnableStrongETag bool
// 禁止生成ETag
DisableETag bool
// 禁止生成 last-modifed
DisableLastModified bool
// 如果404,是否调用next执行后续的中间件(默认为不执行,返回404错误)
NotFoundNext bool
Skipper elton.Skipper
}
// FS file system
FS struct {
}
)
const (
// ErrCategory static serve error category
ErrCategory = "elton-static-serve"
)
var (
// ErrNotAllowQueryString not all query string
ErrNotAllowQueryString = getStaticServeError("static serve not allow query string", http.StatusBadRequest)
// ErrNotFound static file not found
ErrNotFound = getStaticServeError("static file not found", http.StatusNotFound)
// ErrOutOfPath file out of path
ErrOutOfPath = getStaticServeError("out of path", http.StatusBadRequest)
// ErrNotAllowAccessDot file include dot
ErrNotAllowAccessDot = getStaticServeError("static server not allow with dot", http.StatusBadRequest)
)
// Exists check the file exists
func (fs *FS) Exists(file string) bool {
if _, err := os.Stat(file); os.IsNotExist(err) {
return false
}
return true
}
// Stat get stat of file
func (fs *FS) Stat(file string) os.FileInfo {
info, _ := os.Stat(file)
return info
}
// Get get the file's content
func (fs *FS) Get(file string) (buf []byte, err error) {
buf, err = ioutil.ReadFile(file)
return
}
// NewReader new a reader for file
func (fs *FS) NewReader(file string) (io.Reader, error) {
return os.Open(file)
}
// getStaticServeError 获取static serve的出错
func getStaticServeError(message string, statusCode int) *hes.Error {
return &hes.Error{
StatusCode: statusCode,
Message: message,
Category: ErrCategory,
}
}
// generateETag generate eTag
func generateETag(buf []byte) string {
size := len(buf)
if size == 0 {
return `"0-2jmj7l5rSw0yVb_vlWAYkK_YBwk="`
}
h := sha1.New()
_, err := h.Write(buf)
if err != nil {
return ""
}
hash := base64.URLEncoding.EncodeToString(h.Sum(nil))
return fmt.Sprintf(`"%x-%s"`, size, hash)
}
// NewDefault create a static server milldeware use FS
func NewDefault(config Config) elton.Handler {
return New(&FS{}, config)
}
// New create a static serve middleware
func New(staticFile StaticFile, config Config) elton.Handler {
cacheArr := []string{
"public",
}
if config.MaxAge > 0 {
cacheArr = append(cacheArr, "max-age="+strconv.Itoa(config.MaxAge))
}
if config.SMaxAge > 0 {
cacheArr = append(cacheArr, "s-maxage="+strconv.Itoa(config.SMaxAge))
}
cacheControl := ""
if len(cacheArr) > 1 {
cacheControl = strings.Join(cacheArr, ", ")
}
skipper := config.Skipper
if skipper == nil {
skipper = elton.DefaultSkipper
}
// convert to the different os file path
basePath := filepath.Join(config.Path, "")
return func(c *elton.Context) (err error) {
if skipper(c) {
return c.Next()
}
file := ""
rawParams := c.RawParams
// 从第一个参数获取文件名
if len(rawParams) > 0 {
file = rawParams[0].Value
}
url := c.Request.URL
if file == "" {
file = url.Path
}
// 检查文件(路径)是否包括.
if config.DenyDot {
arr := strings.SplitN(file, string(filepath.Separator), -1)
for _, item := range arr {
if item != "" && item[0] == '.' {
err = ErrNotAllowAccessDot
return
}
}
}
file = filepath.Join(config.Path, file)
// 避免文件名是有 .. 等导致最终文件路径越过配置的路径
if !strings.HasPrefix(file, basePath) {
err = ErrOutOfPath
return
}
// 禁止 querystring
if config.DenyQueryString && url.RawQuery != "" {
err = ErrNotAllowQueryString
return
}
exists := staticFile.Exists(file)
if !exists {
if config.NotFoundNext {
return c.Next()
}
err = ErrNotFound
return
}
c.SetContentTypeByExt(file)
var fileBuf []byte
// strong etag需要读取文件内容计算etag
if !config.DisableETag && config.EnableStrongETag {
buf, e := staticFile.Get(file)
if e != nil {
he, ok := e.(*hes.Error)
if !ok {
he = hes.NewWithErrorStatusCode(e, http.StatusInternalServerError)
he.Category = ErrCategory
}
err = he
return
}
fileBuf = buf
}
if !config.DisableETag {
if config.EnableStrongETag {
eTag := generateETag(fileBuf)
if eTag != "" {
c.SetHeader(elton.HeaderETag, eTag)
}
} else {
fileInfo := staticFile.Stat(file)
if fileInfo != nil {
eTag := fmt.Sprintf(`W/"%x-%x"`, fileInfo.Size(), fileInfo.ModTime().Unix())
c.SetHeader(elton.HeaderETag, eTag)
}
}
}
if !config.DisableLastModified {
fileInfo := staticFile.Stat(file)
if fileInfo != nil {
lmd := fileInfo.ModTime().UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")
c.SetHeader(elton.HeaderLastModified, lmd)
}
}
for k, v := range config.Header {
c.SetHeader(k, v)
}
if cacheControl != "" {
c.SetHeader(elton.HeaderCacheControl, cacheControl)
}
if fileBuf != nil {
c.BodyBuffer = bytes.NewBuffer(fileBuf)
} else {
r, e := staticFile.NewReader(file)
if e != nil {
err = getStaticServeError(e.Error(), http.StatusBadRequest)
return
}
c.Body = r
}
return c.Next()
}
}