forked from coffeehc/httpx
/
static.go
398 lines (371 loc) · 9.95 KB
/
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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// static
package web
import (
"bytes"
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
)
const sniffLen = 512
type StaticService struct {
root http.FileSystem
urlPrefix string
}
func NewStaticFilter(root http.FileSystem, urlPrefix string) ActionFilter {
service := &StaticService{root, urlPrefix}
return service.StaticFilter
}
func (this *StaticService) StaticFilter(request *http.Request, reply *Reply, chain FilterChain) {
upath := strings.TrimPrefix(request.URL.Path, this.urlPrefix)
if len(upath) < len(request.URL.Path) {
request.URL.Path = upath
if !strings.HasPrefix(upath, "/") {
upath = "/" + upath
request.URL.Path = upath
}
serveFile(reply, request, this.root, path.Clean(upath), true)
} else {
chain(request, reply)
}
}
func localRedirect(reply *Reply, r *http.Request, newPath string) {
if q := r.URL.RawQuery; q != "" {
newPath += "?" + q
}
reply.SetHeader("Location", newPath)
reply.SetCode(http.StatusMovedPermanently)
}
func serveFile(reply *Reply, r *http.Request, fs http.FileSystem, name string, redirect bool) {
const indexPage = "/index.html"
if strings.HasSuffix(r.URL.Path, indexPage) {
localRedirect(reply, r, "./")
return
}
f, err := fs.Open(name)
if err != nil {
reply.SetCode(http.StatusNotFound)
return
}
d, err1 := f.Stat()
if err1 != nil {
reply.SetCode(http.StatusNotFound)
return
}
if redirect {
url := r.URL.Path
if d.IsDir() {
if url[len(url)-1] != '/' {
localRedirect(reply, r, path.Base(url)+"/")
return
}
} else {
if url[len(url)-1] == '/' {
localRedirect(reply, r, "../"+path.Base(url))
return
}
}
}
// use contents of index.html for directory, if present
if d.IsDir() {
index := strings.TrimSuffix(name, "/") + indexPage
ff, err := fs.Open(index)
if err == nil {
dd, err := ff.Stat()
if err == nil {
name = index
d = dd
f = ff
}
}
}
// Still a directory? (we didn't find an index.html file)
if d.IsDir() {
if checkLastModified(reply, r, d.ModTime()) {
return
}
dirList(reply, f)
return
}
// serveContent will check modification time
sizeFunc := func() (int64, error) { return d.Size(), nil }
serveContent(reply, r, d.Name(), d.ModTime(), sizeFunc, f)
}
func serveContent(reply *Reply, r *http.Request, name string, modtime time.Time, sizeFunc func() (int64, error), content io.ReadSeeker) {
if checkLastModified(reply, r, modtime) {
return
}
rangeReq, done := checkETag(reply, r, modtime)
if done {
return
}
code := http.StatusOK
ctype := mime.TypeByExtension(filepath.Ext(name))
if ctype == "" {
// read a chunk to decide between utf-8 text and binary
var buf [sniffLen]byte
n, _ := io.ReadFull(content, buf[:])
ctype = http.DetectContentType(buf[:n])
_, err := content.Seek(0, os.SEEK_SET) // rewind to output whole file
if err != nil {
reply.SetCode(http.StatusInternalServerError)
reply.With("seeker can't seek")
return
}
}
reply.SetContentType(ctype)
size, err := sizeFunc()
if err != nil {
reply.SetCode(http.StatusInternalServerError)
reply.With(err.Error())
return
}
// handle Content-Range header.
sendSize := size
var sendContent io.Reader = content
if size >= 0 {
ranges, err := parseRange(rangeReq, size)
if err != nil {
reply.SetCode(http.StatusRequestedRangeNotSatisfiable).With(err.Error())
}
if sumRangesSize(ranges) > size {
ranges = nil
}
switch {
case len(ranges) == 1:
ra := ranges[0]
if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil {
reply.SetCode(http.StatusRequestedRangeNotSatisfiable).With(err.Error())
return
}
sendSize = ra.length
code = http.StatusPartialContent
reply.SetHeader("Content-Range", ra.contentRange(size))
case len(ranges) > 1:
sendSize = rangesMIMESize(ranges, ctype, size)
code = http.StatusPartialContent
pr, pw := io.Pipe()
mw := multipart.NewWriter(pw)
reply.SetHeader("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
sendContent = pr
// cause writing goroutine to fail and exit if CopyN doesn't finish.
go func() {
for _, ra := range ranges {
part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
if err != nil {
pw.CloseWithError(err)
return
}
if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil {
pw.CloseWithError(err)
return
}
if _, err := io.CopyN(part, content, ra.length); err != nil {
pw.CloseWithError(err)
return
}
}
mw.Close()
pw.Close()
}()
}
reply.SetHeader("Accept-Ranges", "bytes")
if reply.GetHeader("Content-Encoding") == "" {
reply.SetHeader("Content-Length", strconv.FormatInt(sendSize, 10))
}
}
reply.SetCode(code)
if r.Method != "HEAD" {
reply.With(io.LimitReader(sendContent, sendSize))
}
}
func checkLastModified(reply *Reply, r *http.Request, modtime time.Time) bool {
if modtime.IsZero() {
return false
}
// The Date-Modified header truncates sub-second precision, so
// use mtime < t+1s instead of mtime <= t to check for unmodified.
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(time.Second)) {
reply.DelHeader("Content-Type")
reply.DelHeader("Content-Length")
reply.SetCode(http.StatusNotModified)
return true
}
reply.SetHeader("Last-Modified", modtime.UTC().Format(http.TimeFormat))
return false
}
func dirList(reply *Reply, f http.File) {
reply.SetHeader("Content-Type", "text/html; charset=utf-8")
buf := bytes.NewBuffer(nil)
buf.WriteString("<pre>\n")
for {
dirs, err := f.Readdir(100)
if err != nil || len(dirs) == 0 {
break
}
for _, d := range dirs {
name := d.Name()
if d.IsDir() {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
// part of the URL path, and not indicate the start of a query
// string or fragment.
url := url.URL{Path: name}
fmt.Fprintf(buf, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
}
}
fmt.Fprintf(buf, "</pre>\n")
reply.With(buf)
}
func checkETag(reply *Reply, r *http.Request, modtime time.Time) (rangeReq string, done bool) {
etag := reply.GetHeader("Etag")
rangeReq = r.Header.Get("Range")
if ir := r.Header.Get("If-Range"); ir != "" && ir != etag {
// The If-Range value is typically the ETag value, but it may also be
// the modtime date. See golang.org/issue/8367.
timeMatches := false
if !modtime.IsZero() {
if t, err := http.ParseTime(ir); err == nil && t.Unix() == modtime.Unix() {
timeMatches = true
}
}
if !timeMatches {
rangeReq = ""
}
}
if inm := r.Header.Get("If-None-Match"); inm != "" {
// Must know ETag.
if etag == "" {
return rangeReq, false
}
// TODO(bradfitz): non-GET/HEAD requests require more work:
// sending a different status code on matches, and
// also can't use weak cache validators (those with a "W/
// prefix). But most users of ServeContent will be using
// it on GET or HEAD, so only support those for now.
if r.Method != "GET" && r.Method != "HEAD" {
return rangeReq, false
}
// TODO(bradfitz): deal with comma-separated or multiple-valued
// list of If-None-match values. For now just handle the common
// case of a single item.
if inm == etag || inm == "*" {
// h := w.Header()
reply.DelHeader("Content-Type")
reply.DelHeader("Content-Length")
reply.SetCode(http.StatusNotModified)
return "", true
}
}
return rangeReq, false
}
func parseRange(s string, size int64) ([]httpRange, error) {
if s == "" {
return nil, nil // header not present
}
const b = "bytes="
if !strings.HasPrefix(s, b) {
return nil, errors.New("invalid range")
}
var ranges []httpRange
for _, ra := range strings.Split(s[len(b):], ",") {
ra = strings.TrimSpace(ra)
if ra == "" {
continue
}
i := strings.Index(ra, "-")
if i < 0 {
return nil, errors.New("invalid range")
}
start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
var r httpRange
if start == "" {
// If no start is specified, end specifies the
// range start relative to the end of the file.
i, err := strconv.ParseInt(end, 10, 64)
if err != nil {
return nil, errors.New("invalid range")
}
if i > size {
i = size
}
r.start = size - i
r.length = size - r.start
} else {
i, err := strconv.ParseInt(start, 10, 64)
if err != nil || i > size || i < 0 {
return nil, errors.New("invalid range")
}
r.start = i
if end == "" {
// If no end is specified, range extends to end of the file.
r.length = size - r.start
} else {
i, err := strconv.ParseInt(end, 10, 64)
if err != nil || r.start > i {
return nil, errors.New("invalid range")
}
if i >= size {
i = size - 1
}
r.length = i - r.start + 1
}
}
ranges = append(ranges, r)
}
return ranges, nil
}
func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
var w countingWriter
mw := multipart.NewWriter(&w)
for _, ra := range ranges {
mw.CreatePart(ra.mimeHeader(contentType, contentSize))
encSize += ra.length
}
mw.Close()
encSize += int64(w)
return
}
func sumRangesSize(ranges []httpRange) (size int64) {
for _, ra := range ranges {
size += ra.length
}
return
}
var htmlReplacer = strings.NewReplacer(
"&", "&",
"<", "<",
">", ">",
// """ is shorter than """.
`"`, """,
// "'" is shorter than "'" and apos was not in HTML until HTML5.
"'", "'",
)
type httpRange struct {
start, length int64
}
func (r httpRange) contentRange(size int64) string {
return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
}
func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
return textproto.MIMEHeader{
"Content-Range": {r.contentRange(size)},
"Content-Type": {contentType},
}
}
type countingWriter int64
func (w *countingWriter) Write(p []byte) (n int, err error) {
*w += countingWriter(len(p))
return len(p), nil
}