-
Notifications
You must be signed in to change notification settings - Fork 251
/
fs.go
1035 lines (951 loc) · 28.9 KB
/
fs.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 AndeyaLee. All Rights Reserved.
//
// 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.
// HTTP file system with cache request handler
package faygo
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/andeya/faygo/acceptencoder"
"github.com/andeya/faygo/freecache"
"github.com/andeya/faygo/markdown"
)
const indexPage = "/index.html"
// FileServerManager is file cache system manager
type FileServerManager struct {
files map[string]CacheFile
cache *freecache.Cache
fileExpireSeconds int
maxSizeOfSingle int64
enableCache bool
enableCompress bool
errorFunc ErrorFunc
filesLock sync.RWMutex
}
// The cache size will be set to 512KB at minimum.
// If the size is set relatively large, you should call
// `debug.SetGCPercent()`, set it to a much smaller value
// to limit the memory consumption and GC pause time.
// expireSeconds <= 0 means no expire.
func newFileServerManager(cacheSize int64, fileExpireSeconds int, enableCache bool, enableCompress bool) *FileServerManager {
manager := &FileServerManager{
enableCache: enableCache,
enableCompress: enableCompress,
}
if enableCache {
manager.fileExpireSeconds = fileExpireSeconds
manager.cache = freecache.NewCache(int(cacheSize))
manager.files = map[string]CacheFile{}
manager.maxSizeOfSingle = cacheSize / 1024
if manager.maxSizeOfSingle < 512 {
manager.maxSizeOfSingle = 512
}
}
return manager
}
// Open gets or stores the file with compression and caching options.
// If the name is larger than 65535 or body is larger than 1/1024 of the cache size,
// the entry will not be written to the cache.
func (c *FileServerManager) Open(name string, encoding string, nocache bool) (http.File, error) {
var f http.File
var err error
var compressible = encoding != "" && c.enableCompress
var cacheable = !nocache && c.enableCache
if cacheable {
f, err = c.Get(name)
if err == nil {
return f, nil
}
}
f, err = os.Open(name)
if err != nil {
return nil, err
}
fileInfo, err := f.Stat()
if err != nil || fileInfo.IsDir() {
return f, err
}
var content []byte
if compressible {
content, encoding, err = fileCompress2(f, encoding)
f.Close()
if err != nil {
return nil, err
}
if !cacheable || int64(len(content)) > c.maxSizeOfSingle {
return &CacheFile{
fileInfo: fileInfo,
encoding: encoding,
Reader: bytes.NewReader(content),
}, nil
}
} else {
if !cacheable || fileInfo.Size() > c.maxSizeOfSingle {
return f, nil
}
content, err = ioutil.ReadAll(f)
f.Close()
if err != nil {
return nil, err
}
}
return c.Set(name, content, fileInfo, encoding)
}
// OpenFS gets or stores the cache file.
// If the name is larger than 65535 or body is larger than 1/1024 of the cache size,
// the entry will not be written to the cache.
func (c *FileServerManager) OpenFS(ctx *Context, name string, fs FileSystem) (http.File, error) {
var f http.File
var err error
var compressible = !fs.Nocompress() && c.enableCompress
var cacheable = !fs.Nocache() && c.enableCache
if cacheable {
f, err = c.Get(name)
if err == nil {
if encoding := f.(*CacheFile).encoding; encoding != "" {
ctx.W.Header().Set("Content-Encoding", encoding)
}
return f, nil
}
}
f, err = fs.Open(name)
if err != nil {
return nil, err
}
fileInfo, err := f.Stat()
if err != nil || fileInfo.IsDir() {
return f, err
}
var content []byte
var encoding string
if compressible {
content, encoding, err = fileCompress(f, ctx)
f.Close()
if err != nil {
return nil, err
}
if !cacheable || int64(len(content)) > c.maxSizeOfSingle {
return &CacheFile{
fileInfo: fileInfo,
encoding: encoding,
Reader: bytes.NewReader(content),
}, nil
}
} else {
if !cacheable || fileInfo.Size() > c.maxSizeOfSingle {
return f, nil
}
content, err = ioutil.ReadAll(f)
f.Close()
if err != nil {
return nil, err
}
}
return c.Set(name, content, fileInfo, encoding)
}
// Get gets file from cache.
func (c *FileServerManager) Get(name string) (http.File, error) {
b, err := c.cache.Get([]byte(name))
if err != nil {
c.filesLock.Lock()
delete(c.files, name)
c.filesLock.Unlock()
return nil, err
}
c.filesLock.RLock()
f := c.files[name]
c.filesLock.RUnlock()
f.Reader = bytes.NewReader(b)
return &f, nil
}
// Set sets file to cache.
func (c *FileServerManager) Set(name string, body []byte, fileInfo os.FileInfo, encoding string) (http.File, error) {
err := c.cache.Set([]byte(name), body, c.fileExpireSeconds)
if err != nil {
return nil, err
}
f := CacheFile{
fileInfo: fileInfo,
encoding: encoding,
}
c.filesLock.Lock()
c.files[name] = f
c.filesLock.Unlock()
f.Reader = bytes.NewReader(body)
return &f, nil
}
type (
// FileSystem is a file system with compression and caching options
FileSystem interface {
http.FileSystem
Nocompress() bool // not allowed compress
Nocache() bool // not allowed cache
}
fileSystem struct {
http.FileSystem
nocompress bool
nocache bool
}
)
func (fs *fileSystem) Nocompress() bool {
return fs.nocompress
}
func (fs *fileSystem) Nocache() bool {
return fs.nocache
}
// FS creates a file system with compression and caching options
func FS(fs http.FileSystem, nocompressAndNocache ...bool) FileSystem {
var nocompress, nocache bool
var count = len(nocompressAndNocache)
if count == 1 {
nocompress = nocompressAndNocache[0]
} else if count >= 2 {
nocompress = nocompressAndNocache[0]
nocache = nocompressAndNocache[1]
}
return &fileSystem{
FileSystem: fs,
nocompress: nocompress,
nocache: nocache,
}
}
// DirFS creates a file system with compression and caching options, similar to http.Dir
func DirFS(root string, nocompressAndNocache ...bool) FileSystem {
return FS(http.Dir(root), nocompressAndNocache...)
}
// RenderFS creates a file system with auto-rendering.
// param `suffix` is used to specify the extension to be rendered, `*` for all extensions.
func RenderFS(root string, suffix string, tplVar Map) FileSystem {
mime.AddExtensionType(path.Ext(suffix), "text/html")
return FS(&renderFS{
dir: root,
suffix: suffix,
tplVar: tplVar,
render: GetRender(),
}, false, true)
}
type renderFS struct {
dir string
suffix string
tplVar Map
render *Render
}
func (fs *renderFS) Open(name string) (http.File, error) {
if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) ||
strings.Contains(name, "\x00") {
return nil, errors.New("RenderFS: invalid character in file path")
}
dir := fs.dir
if dir == "" {
dir = "."
}
fname := filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name)))
if fs.suffix != "*" && !strings.HasSuffix(fname, fs.suffix) {
f, err := global.fsManager.Open(fname, "", false)
if err != nil {
// Error("RenderFS:", fname, err)
return nil, err
}
return f, nil
}
b, fileInfo, err := fs.render.renderForFS(fname, fs.tplVar)
if err != nil {
if strings.Contains(err.Error(), "not find") {
return nil, os.ErrNotExist
}
// Error("RenderFS:", fname, err)
return NewFile(b, fileInfo), err
}
return NewFile(b, fileInfo), nil
}
// MarkdownFS creates a markdown file system.
func MarkdownFS(root string, nocompressAndNocache ...bool) FileSystem {
return FS(&markdownFS{
dir: root,
}, nocompressAndNocache...)
}
type markdownFS struct {
dir string
}
func (fs *markdownFS) Open(name string) (http.File, error) {
if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) ||
strings.Contains(name, "\x00") {
return nil, errors.New("MarkdownFS: invalid character in file path")
}
dir := fs.dir
if dir == "" {
dir = "."
}
fname := filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name)))
f, err := global.fsManager.Open(fname, "", false)
if err != nil {
// Error("MarkdownFS:", fname, err)
return nil, err
}
if !strings.HasSuffix(fname, ".md") {
return f, nil
}
fileInfo, err := f.Stat()
if err != nil {
f.Close()
// Error("MarkdownFS:", fname, err)
return nil, err
}
b, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
// Error("MarkdownFS:", fname, err)
return nil, err
}
b, err = markdown.GithubMarkdown(b, false)
if err != nil {
// Error("MarkdownFS:", fname, err)
return nil, err
}
return NewFile(b, fileInfo), nil
}
// CacheFile implements os.File
type CacheFile struct {
fileInfo os.FileInfo
encoding string
*bytes.Reader
}
var _ http.File = new(CacheFile)
// NewFile creates a cacheFile
func NewFile(b []byte, fileInfo os.FileInfo) *CacheFile {
return &CacheFile{
Reader: bytes.NewReader(b),
fileInfo: fileInfo,
}
}
// Stat returns file info
func (c *CacheFile) Stat() (os.FileInfo, error) {
if c.fileInfo == nil {
c.fileInfo = &FileInfo{
size: int64(c.Len()),
modTime: time.Now(),
}
}
return c.fileInfo, nil
}
// Close closes file
func (c *CacheFile) Close() error {
c.Reader = nil
return nil
}
// Readdir gets path info
func (c *CacheFile) Readdir(count int) ([]os.FileInfo, error) {
return []os.FileInfo{}, errors.New("Readdir " + c.fileInfo.Name() + ": The system cannot find the path specified.")
}
// FileInfo implements os.FileInfo
type FileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
isDir bool
sys interface{}
}
// Name returns base name of the file
func (info *FileInfo) Name() string {
return info.name
}
// Size returns the size in bytes for regular files; system-dependent for others
func (info *FileInfo) Size() int64 {
return info.size
}
// Mode returns file mode bits
func (info *FileInfo) Mode() os.FileMode {
return info.mode
}
// ModTime returns modification time
func (info *FileInfo) ModTime() time.Time {
return info.modTime
}
// IsDir is the abbreviation for Mode().IsDir()
func (info *FileInfo) IsDir() bool {
return info.isDir
}
// Sys returns underlying data source (can return nil)
func (info *FileInfo) Sys() interface{} {
return info.sys
}
func (c *FileServerManager) dirList(ctx *Context, f http.File) {
dirs, err := f.Readdir(-1)
if err != nil {
// TODO: log err.Error() to the Server.ErrorLog, once it's possible
// for a handler to get at its Server via the *Context. See
// Issue 12438.
global.errorFunc(ctx, "Error reading directory", http.StatusInternalServerError)
return
}
sort.Sort(byName(dirs))
ctx.W.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(ctx.W, "<pre>\n")
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(ctx.W, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
}
fmt.Fprintf(ctx.W, "</pre>\n")
}
var htmlReplacer = strings.NewReplacer(
"&", "&",
"<", "<",
">", ">",
// """ is shorter than """.
`"`, """,
// "'" is shorter than "'" and apos was not in HTML until HTML5.
"'", "'",
)
// ServeContent replies to the request using the content in the
// provided ReadSeeker. The main benefit of ServeContent over io.Copy
// is that it handles Range requests properly, sets the MIME type, and
// handles If-Modified-Since requests.
//
// If the response's Content-Type header is not set, ServeContent
// first tries to deduce the type from name's file extension and,
// if that fails, falls back to reading the first block of the content
// and passing it to DetectContentType.
// The name is otherwise unused; in particular it can be empty and is
// never sent in the response.
//
// If modtime is not the zero time or Unix epoch, ServeContent
// includes it in a Last-Modified header in the response. If the
// request includes an If-Modified-Since header, ServeContent uses
// modtime to decide whether the content needs to be sent at all.
//
// The content's Seek method must work: ServeContent uses
// a seek to the end of the content to determine its size.
//
// If the caller has set ctx's ETag header, ServeContent uses it to
// handle requests using If-Range and If-None-Match.
//
// Note that *os.File implements the io.ReadSeeker interface.
func (c *FileServerManager) ServeContent(ctx *Context, name string, modtime time.Time, content io.ReadSeeker) {
sizeFunc := func() (int64, error) {
size, err := content.Seek(0, io.SeekEnd)
if err != nil {
return 0, errSeeker
}
_, err = content.Seek(0, io.SeekStart)
if err != nil {
return 0, errSeeker
}
return size, nil
}
c.serveContent(ctx, name, modtime, sizeFunc, content)
}
// errSeeker is returned by ServeContent's sizeFunc when the content
// doesn't seek properly. The underlying Seeker's error text isn't
// included in the sizeFunc reply so it's not sent over HTTP to end
// users.
var errSeeker = errors.New("seeker can't seek")
// The algorithm uses at most sniffLen bytes to make its decision.
const sniffLen = 512
// if name is empty, filename is unknown. (used for mime type, before sniffing)
// if modtime.IsZero(), modtime is unknown.
// content must be seeked to the beginning of the file.
// The sizeFunc is called at most once. Its error, if any, is sent in the HTTP response.
func (c *FileServerManager) serveContent(ctx *Context, name string, modtime time.Time, sizeFunc func() (int64, error), content io.ReadSeeker) {
if checkLastModified(ctx, modtime) {
return
}
rangeReq, done := checkETag(ctx, modtime)
if done {
return
}
code := http.StatusOK
// If Content-Type isn't set, use the file's extension to find it, but
// if the Content-Type is unset explicitly, do not sniff the type.
ctypes, haveType := ctx.W.Header()["Content-Type"]
var ctype string
if !haveType {
ctype = mime.TypeByExtension(filepath.Ext(name))
// Warning("ctypes:", haveType, name, filepath.Ext(name), ctype)
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, io.SeekStart) // rewind to output whole file
if err != nil {
global.errorFunc(ctx, "seeker can't seek", http.StatusInternalServerError)
return
}
}
ctx.W.Header().Set("Content-Type", ctype)
} else if len(ctypes) > 0 {
ctype = ctypes[0]
}
size, err := sizeFunc()
if err != nil {
global.errorFunc(ctx, err.Error(), http.StatusInternalServerError)
return
}
// handle Content-Range header.
sendSize := size
var sendContent io.Reader = content
if size >= 0 {
ranges, err := parseRange(rangeReq, size)
if err != nil {
global.errorFunc(ctx, err.Error(), http.StatusRequestedRangeNotSatisfiable)
return
}
if sumRangesSize(ranges) > size {
// The total number of bytes in all the ranges
// is larger than the size of the file by
// itself, so this is probably an attack, or a
// dumb client. Ignore the range request.
ranges = nil
}
switch {
case len(ranges) == 1:
// RFC 2616, Section 14.16:
// "When an HTTP message includes the content of a single
// range (for example, a response to a request for a
// single range, or to a request for a set of ranges
// that overlap without any holes), this content is
// transmitted with a Content-Range header, and a
// Content-Length header showing the number of bytes
// actually transferred.
// ...
// A response to a request for a single range MUST NOT
// be sent using the multipart/byteranges media type."
ra := ranges[0]
if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
global.errorFunc(ctx, err.Error(), http.StatusRequestedRangeNotSatisfiable)
return
}
sendSize = ra.length
code = http.StatusPartialContent
ctx.W.Header().Set("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)
ctx.W.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
sendContent = pr
defer pr.Close() // 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, io.SeekStart); err != nil {
pw.CloseWithError(err)
return
}
if _, err := io.CopyN(part, content, ra.length); err != nil {
pw.CloseWithError(err)
return
}
}
mw.Close()
pw.Close()
}()
}
ctx.W.Header().Set("Accept-Ranges", "bytes")
if ctx.W.Header().Get("Content-Encoding") == "" {
ctx.W.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
}
}
ctx.W.WriteHeader(code)
if ctx.R.Method != "HEAD" {
io.CopyN(ctx.W, sendContent, sendSize)
}
}
var unixEpochTime = time.Unix(0, 0)
// modtime is the modification time of the resource to be served, or IsZero().
// return value is whether this request is now complete.
func checkLastModified(ctx *Context, modtime time.Time) bool {
if modtime.IsZero() || modtime.Equal(unixEpochTime) {
// If the file doesn't have a modtime (IsZero), or the modtime
// is obviously garbage (Unix time == 0), then ignore modtimes
// and don't process the If-Modified-Since header.
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, ctx.R.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
h := ctx.W.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
ctx.W.WriteHeader(http.StatusNotModified)
return true
}
ctx.W.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
return false
}
// checkETag implements If-None-Match and If-Range checks.
//
// The ETag or modtime must have been previously set in the
// *Context's headers. The modtime is only compared at second
// granularity and may be the zero value to mean unknown.
//
// The return value is the effective request "Range" header to use and
// whether this request is now considered done.
func checkETag(ctx *Context, modtime time.Time) (rangeReq string, done bool) {
etag := ctx.W.Header().Get("Etag")
rangeReq = ctx.R.Header.Get("Range")
// Invalidate the range request if the entity doesn't match the one
// the client was expecting.
// "If-Range: version" means "ignore the Range: header unless version matches the
// current file."
// We only support ETag versions.
// The caller must have set the ETag on the response already.
if ir := ctx.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 := ctx.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 ctx.R.Method != "GET" && ctx.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 := ctx.W.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
ctx.W.WriteHeader(http.StatusNotModified)
return "", true
}
}
return rangeReq, false
}
// name is '/'-separated, not filepath.Separator.
func (c *FileServerManager) serveFile(ctx *Context, fs FileSystem, name string, redirect bool) {
// redirect .../index.html to .../
// can't use Redirect() because that would make the path absolute,
// which would be a problem running under StripPrefix
//
// if strings.HasSuffix(ctx.R.URL.Path, indexPage) {
// localRedirect(ctx, "./")
// return
// }
f, err := c.OpenFS(ctx, name, fs)
if err != nil {
msg, code := toHTTPError(err)
global.errorFunc(ctx, msg, code)
return
}
defer f.Close()
d, err := f.Stat()
if err != nil {
msg, code := toHTTPError(err)
global.errorFunc(ctx, msg, code)
return
}
// if redirect {
// // redirect to canonical path: / at end of directory url
// // ctx.R.URL.Path always begins with /
// url := ctx.R.URL.Path
// if d.IsDir() {
// if url[len(url)-1] != '/' {
// localRedirect(ctx, path.Base(url)+"/")
// return
// }
// } else {
// if url[len(url)-1] == '/' {
// localRedirect(ctx, "../"+path.Base(url))
// return
// }
// }
// }
// redirect if the directory name doesn't end in a slash
if d.IsDir() {
url := ctx.R.URL.Path
if url[len(url)-1] != '/' {
localRedirect(ctx, path.Base(url)+"/")
return
}
}
// use contents of index.html for directory, if present
if d.IsDir() {
index := strings.TrimSuffix(name, "/") + indexPage
ff, err := c.OpenFS(ctx, index, fs)
if err == nil {
defer ff.Close()
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(ctx, d.ModTime()) {
return
}
global.errorFunc(ctx, http.StatusText(http.StatusNotFound), http.StatusNotFound)
// c.dirList(ctx, f)
return
}
// serveContent will check modification time
sizeFunc := func() (int64, error) { return d.Size(), nil }
c.serveContent(ctx, d.Name(), d.ModTime(), sizeFunc, f)
}
func fileCompress(file http.File, ctx *Context) ([]byte, string, error) {
var buf = &bytes.Buffer{}
var encoding string
if b, n, _ := acceptencoder.WriteFile(acceptencoder.ParseEncoding(ctx.R), buf, file); b {
ctx.W.Header().Set("Content-Encoding", n)
encoding = n
}
return buf.Bytes(), encoding, nil
}
func fileCompress2(f http.File, encoding string) ([]byte, string, error) {
var buf = &bytes.Buffer{}
if b, n, _ := acceptencoder.WriteFile(encoding, buf, f); b {
encoding = n
}
f.Close()
return buf.Bytes(), encoding, nil
}
// toHTTPError returns a non-specific HTTP error message and status code
// for a given non-nil error value. It's important that toHTTPError does not
// actually return err.Error(), since msg and httpStatus are returned to users,
// and historically Go's ServeContent always returned just "404 Not Found" for
// all errors. We don't want to start leaking information in error messages.
func toHTTPError(err error) (msg string, httpStatus int) {
if os.IsNotExist(err) {
return "404 page not found", http.StatusNotFound
}
if os.IsPermission(err) {
return "403 Forbidden", http.StatusForbidden
}
// Default:
return err.Error(), http.StatusInternalServerError
// return "500 Internal Server Error", http.StatusInternalServerError
}
// localRedirect gives a Moved Permanently response.
// It does not convert relative paths to absolute paths like Redirect does.
func localRedirect(ctx *Context, newPath string) {
if q := ctx.R.URL.RawQuery; q != "" {
newPath += "?" + q
}
ctx.W.Header().Set("Location", newPath)
ctx.W.WriteHeader(http.StatusMovedPermanently)
}
// ServeFile replies to the request with the contents of the named
// file or directory.
//
// If the provided file or directory name is a relative path, it is
// interpreted relative to the current directory and may ascend to parent
// directories. If the provided name is constructed from user input, it
// should be sanitized before calling ServeFile. As a precaution, ServeFile
// will reject requests where r.URL.Path contains a ".." path element.
//
// As a special case, ServeFile redirects any request where r.URL.Path
// ends in "/index.html" to the same path, without the final
// "index.html". To avoid such redirects either modify the path or
// use ServeContent.
func (c *FileServerManager) ServeFile(ctx *Context, name string, nocompressAndNocache ...bool) {
if containsDotDot(ctx.R.URL.Path) {
// Too many programs use ctx.R.URL.Path to construct the argument to
// serveFile. Reject the request under the assumption that happened
// here and ".." may not be wanted.
// Note that name might not contain "..", for example if code (still
// incorrectly) used filepath.Join(myDir, ctx.R.URL.Path).
global.errorFunc(ctx, "invalid URL path", http.StatusBadRequest)
return
}
dir, file := filepath.Split(name)
c.serveFile(ctx, DirFS(dir, nocompressAndNocache...), file, false)
}
func containsDotDot(v string) bool {
if !strings.Contains(v, "..") {
return false
}
for _, ent := range strings.FieldsFunc(v, isSlashRune) {
if ent == ".." {
return true
}
}
return false
}
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
type fileHandler struct {
root FileSystem
fileServerManager *FileServerManager
}
// FileServer returns a handler that serves HTTP requests
// with the contents of the file system rooted at fs.
//
// To use the operating system's file system implementation,
// use http.Dir:
//
// http.Handle("/", http.FileServer(http.Dir("/tmp")))
//
// As a special case, the returned file server redirects any request
// ending in "/index.html" to the same path, without the final
// "index.html".
func (c *FileServerManager) FileServer(fs FileSystem) Handler {
return &fileHandler{fs, c}
}
func (f *fileHandler) Serve(ctx *Context) error {
r := ctx.R
upath := r.URL.Path
if !strings.HasPrefix(upath, "/") {
upath = "/" + upath
r.URL.Path = upath
}
f.fileServerManager.serveFile(ctx, f.root, path.Clean(upath), true)
return nil
}
// httpRange specifies the byte range to be sent to the client.
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},
}
}
// parseRange parses a Range header string as per RFC 2616.
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
}