]> Cypherpunks.ru repositories - gostls13.git/blob - src/net/http/fs.go
net/http: send Content-Range if no byte range overlaps
[gostls13.git] / src / net / http / fs.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // HTTP file system request handler
6
7 package http
8
9 import (
10         "errors"
11         "fmt"
12         "io"
13         "mime"
14         "mime/multipart"
15         "net/textproto"
16         "net/url"
17         "os"
18         "path"
19         "path/filepath"
20         "sort"
21         "strconv"
22         "strings"
23         "time"
24 )
25
26 // A Dir implements FileSystem using the native file system restricted to a
27 // specific directory tree.
28 //
29 // While the FileSystem.Open method takes '/'-separated paths, a Dir's string
30 // value is a filename on the native file system, not a URL, so it is separated
31 // by filepath.Separator, which isn't necessarily '/'.
32 //
33 // An empty Dir is treated as ".".
34 type Dir string
35
36 func (d Dir) Open(name string) (File, error) {
37         if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) ||
38                 strings.Contains(name, "\x00") {
39                 return nil, errors.New("http: invalid character in file path")
40         }
41         dir := string(d)
42         if dir == "" {
43                 dir = "."
44         }
45         f, err := os.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))
46         if err != nil {
47                 return nil, err
48         }
49         return f, nil
50 }
51
52 // A FileSystem implements access to a collection of named files.
53 // The elements in a file path are separated by slash ('/', U+002F)
54 // characters, regardless of host operating system convention.
55 type FileSystem interface {
56         Open(name string) (File, error)
57 }
58
59 // A File is returned by a FileSystem's Open method and can be
60 // served by the FileServer implementation.
61 //
62 // The methods should behave the same as those on an *os.File.
63 type File interface {
64         io.Closer
65         io.Reader
66         io.Seeker
67         Readdir(count int) ([]os.FileInfo, error)
68         Stat() (os.FileInfo, error)
69 }
70
71 func dirList(w ResponseWriter, f File) {
72         dirs, err := f.Readdir(-1)
73         if err != nil {
74                 // TODO: log err.Error() to the Server.ErrorLog, once it's possible
75                 // for a handler to get at its Server via the ResponseWriter. See
76                 // Issue 12438.
77                 Error(w, "Error reading directory", StatusInternalServerError)
78                 return
79         }
80         sort.Sort(byName(dirs))
81
82         w.Header().Set("Content-Type", "text/html; charset=utf-8")
83         fmt.Fprintf(w, "<pre>\n")
84         for _, d := range dirs {
85                 name := d.Name()
86                 if d.IsDir() {
87                         name += "/"
88                 }
89                 // name may contain '?' or '#', which must be escaped to remain
90                 // part of the URL path, and not indicate the start of a query
91                 // string or fragment.
92                 url := url.URL{Path: name}
93                 fmt.Fprintf(w, "<a href=\"./%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
94         }
95         fmt.Fprintf(w, "</pre>\n")
96 }
97
98 // ServeContent replies to the request using the content in the
99 // provided ReadSeeker. The main benefit of ServeContent over io.Copy
100 // is that it handles Range requests properly, sets the MIME type, and
101 // handles If-Modified-Since requests.
102 //
103 // If the response's Content-Type header is not set, ServeContent
104 // first tries to deduce the type from name's file extension and,
105 // if that fails, falls back to reading the first block of the content
106 // and passing it to DetectContentType.
107 // The name is otherwise unused; in particular it can be empty and is
108 // never sent in the response.
109 //
110 // If modtime is not the zero time or Unix epoch, ServeContent
111 // includes it in a Last-Modified header in the response. If the
112 // request includes an If-Modified-Since header, ServeContent uses
113 // modtime to decide whether the content needs to be sent at all.
114 //
115 // The content's Seek method must work: ServeContent uses
116 // a seek to the end of the content to determine its size.
117 //
118 // If the caller has set w's ETag header, ServeContent uses it to
119 // handle requests using If-Range and If-None-Match.
120 //
121 // Note that *os.File implements the io.ReadSeeker interface.
122 func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker) {
123         sizeFunc := func() (int64, error) {
124                 size, err := content.Seek(0, io.SeekEnd)
125                 if err != nil {
126                         return 0, errSeeker
127                 }
128                 _, err = content.Seek(0, io.SeekStart)
129                 if err != nil {
130                         return 0, errSeeker
131                 }
132                 return size, nil
133         }
134         serveContent(w, req, name, modtime, sizeFunc, content)
135 }
136
137 // errSeeker is returned by ServeContent's sizeFunc when the content
138 // doesn't seek properly. The underlying Seeker's error text isn't
139 // included in the sizeFunc reply so it's not sent over HTTP to end
140 // users.
141 var errSeeker = errors.New("seeker can't seek")
142
143 // errNoOverlap is returned by serveContent's parseRange if first-byte-pos of
144 // all of the byte-range-spec values is greater than the content size.
145 var errNoOverlap = errors.New("invalid range: failed to overlap")
146
147 // if name is empty, filename is unknown. (used for mime type, before sniffing)
148 // if modtime.IsZero(), modtime is unknown.
149 // content must be seeked to the beginning of the file.
150 // The sizeFunc is called at most once. Its error, if any, is sent in the HTTP response.
151 func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, sizeFunc func() (int64, error), content io.ReadSeeker) {
152         if checkLastModified(w, r, modtime) {
153                 return
154         }
155         rangeReq, done := checkETag(w, r, modtime)
156         if done {
157                 return
158         }
159
160         code := StatusOK
161
162         // If Content-Type isn't set, use the file's extension to find it, but
163         // if the Content-Type is unset explicitly, do not sniff the type.
164         ctypes, haveType := w.Header()["Content-Type"]
165         var ctype string
166         if !haveType {
167                 ctype = mime.TypeByExtension(filepath.Ext(name))
168                 if ctype == "" {
169                         // read a chunk to decide between utf-8 text and binary
170                         var buf [sniffLen]byte
171                         n, _ := io.ReadFull(content, buf[:])
172                         ctype = DetectContentType(buf[:n])
173                         _, err := content.Seek(0, io.SeekStart) // rewind to output whole file
174                         if err != nil {
175                                 Error(w, "seeker can't seek", StatusInternalServerError)
176                                 return
177                         }
178                 }
179                 w.Header().Set("Content-Type", ctype)
180         } else if len(ctypes) > 0 {
181                 ctype = ctypes[0]
182         }
183
184         size, err := sizeFunc()
185         if err != nil {
186                 Error(w, err.Error(), StatusInternalServerError)
187                 return
188         }
189
190         // handle Content-Range header.
191         sendSize := size
192         var sendContent io.Reader = content
193         if size >= 0 {
194                 ranges, err := parseRange(rangeReq, size)
195                 if err != nil {
196                         if err == errNoOverlap {
197                                 w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", size))
198                         }
199                         Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
200                         return
201                 }
202                 if sumRangesSize(ranges) > size {
203                         // The total number of bytes in all the ranges
204                         // is larger than the size of the file by
205                         // itself, so this is probably an attack, or a
206                         // dumb client. Ignore the range request.
207                         ranges = nil
208                 }
209                 switch {
210                 case len(ranges) == 1:
211                         // RFC 2616, Section 14.16:
212                         // "When an HTTP message includes the content of a single
213                         // range (for example, a response to a request for a
214                         // single range, or to a request for a set of ranges
215                         // that overlap without any holes), this content is
216                         // transmitted with a Content-Range header, and a
217                         // Content-Length header showing the number of bytes
218                         // actually transferred.
219                         // ...
220                         // A response to a request for a single range MUST NOT
221                         // be sent using the multipart/byteranges media type."
222                         ra := ranges[0]
223                         if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
224                                 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
225                                 return
226                         }
227                         sendSize = ra.length
228                         code = StatusPartialContent
229                         w.Header().Set("Content-Range", ra.contentRange(size))
230                 case len(ranges) > 1:
231                         sendSize = rangesMIMESize(ranges, ctype, size)
232                         code = StatusPartialContent
233
234                         pr, pw := io.Pipe()
235                         mw := multipart.NewWriter(pw)
236                         w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
237                         sendContent = pr
238                         defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
239                         go func() {
240                                 for _, ra := range ranges {
241                                         part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
242                                         if err != nil {
243                                                 pw.CloseWithError(err)
244                                                 return
245                                         }
246                                         if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
247                                                 pw.CloseWithError(err)
248                                                 return
249                                         }
250                                         if _, err := io.CopyN(part, content, ra.length); err != nil {
251                                                 pw.CloseWithError(err)
252                                                 return
253                                         }
254                                 }
255                                 mw.Close()
256                                 pw.Close()
257                         }()
258                 }
259
260                 w.Header().Set("Accept-Ranges", "bytes")
261                 if w.Header().Get("Content-Encoding") == "" {
262                         w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
263                 }
264         }
265
266         w.WriteHeader(code)
267
268         if r.Method != "HEAD" {
269                 io.CopyN(w, sendContent, sendSize)
270         }
271 }
272
273 var unixEpochTime = time.Unix(0, 0)
274
275 // modtime is the modification time of the resource to be served, or IsZero().
276 // return value is whether this request is now complete.
277 func checkLastModified(w ResponseWriter, r *Request, modtime time.Time) bool {
278         if modtime.IsZero() || modtime.Equal(unixEpochTime) {
279                 // If the file doesn't have a modtime (IsZero), or the modtime
280                 // is obviously garbage (Unix time == 0), then ignore modtimes
281                 // and don't process the If-Modified-Since header.
282                 return false
283         }
284
285         // The Date-Modified header truncates sub-second precision, so
286         // use mtime < t+1s instead of mtime <= t to check for unmodified.
287         if t, err := time.Parse(TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
288                 h := w.Header()
289                 delete(h, "Content-Type")
290                 delete(h, "Content-Length")
291                 w.WriteHeader(StatusNotModified)
292                 return true
293         }
294         w.Header().Set("Last-Modified", modtime.UTC().Format(TimeFormat))
295         return false
296 }
297
298 // checkETag implements If-None-Match and If-Range checks.
299 //
300 // The ETag or modtime must have been previously set in the
301 // ResponseWriter's headers. The modtime is only compared at second
302 // granularity and may be the zero value to mean unknown.
303 //
304 // The return value is the effective request "Range" header to use and
305 // whether this request is now considered done.
306 func checkETag(w ResponseWriter, r *Request, modtime time.Time) (rangeReq string, done bool) {
307         etag := w.Header().get("Etag")
308         rangeReq = r.Header.get("Range")
309
310         // Invalidate the range request if the entity doesn't match the one
311         // the client was expecting.
312         // "If-Range: version" means "ignore the Range: header unless version matches the
313         // current file."
314         // We only support ETag versions.
315         // The caller must have set the ETag on the response already.
316         if ir := r.Header.get("If-Range"); ir != "" && ir != etag {
317                 // The If-Range value is typically the ETag value, but it may also be
318                 // the modtime date. See golang.org/issue/8367.
319                 timeMatches := false
320                 if !modtime.IsZero() {
321                         if t, err := ParseTime(ir); err == nil && t.Unix() == modtime.Unix() {
322                                 timeMatches = true
323                         }
324                 }
325                 if !timeMatches {
326                         rangeReq = ""
327                 }
328         }
329
330         if inm := r.Header.get("If-None-Match"); inm != "" {
331                 // Must know ETag.
332                 if etag == "" {
333                         return rangeReq, false
334                 }
335
336                 // TODO(bradfitz): non-GET/HEAD requests require more work:
337                 // sending a different status code on matches, and
338                 // also can't use weak cache validators (those with a "W/
339                 // prefix).  But most users of ServeContent will be using
340                 // it on GET or HEAD, so only support those for now.
341                 if r.Method != "GET" && r.Method != "HEAD" {
342                         return rangeReq, false
343                 }
344
345                 // TODO(bradfitz): deal with comma-separated or multiple-valued
346                 // list of If-None-match values. For now just handle the common
347                 // case of a single item.
348                 if inm == etag || inm == "*" {
349                         h := w.Header()
350                         delete(h, "Content-Type")
351                         delete(h, "Content-Length")
352                         w.WriteHeader(StatusNotModified)
353                         return "", true
354                 }
355         }
356         return rangeReq, false
357 }
358
359 // name is '/'-separated, not filepath.Separator.
360 func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
361         const indexPage = "/index.html"
362
363         // redirect .../index.html to .../
364         // can't use Redirect() because that would make the path absolute,
365         // which would be a problem running under StripPrefix
366         if strings.HasSuffix(r.URL.Path, indexPage) {
367                 localRedirect(w, r, "./")
368                 return
369         }
370
371         f, err := fs.Open(name)
372         if err != nil {
373                 msg, code := toHTTPError(err)
374                 Error(w, msg, code)
375                 return
376         }
377         defer f.Close()
378
379         d, err := f.Stat()
380         if err != nil {
381                 msg, code := toHTTPError(err)
382                 Error(w, msg, code)
383                 return
384         }
385
386         if redirect {
387                 // redirect to canonical path: / at end of directory url
388                 // r.URL.Path always begins with /
389                 url := r.URL.Path
390                 if d.IsDir() {
391                         if url[len(url)-1] != '/' {
392                                 localRedirect(w, r, path.Base(url)+"/")
393                                 return
394                         }
395                 } else {
396                         if url[len(url)-1] == '/' {
397                                 localRedirect(w, r, "../"+path.Base(url))
398                                 return
399                         }
400                 }
401         }
402
403         // redirect if the directory name doesn't end in a slash
404         if d.IsDir() {
405                 url := r.URL.Path
406                 if url[len(url)-1] != '/' {
407                         localRedirect(w, r, path.Base(url)+"/")
408                         return
409                 }
410         }
411
412         // use contents of index.html for directory, if present
413         if d.IsDir() {
414                 index := strings.TrimSuffix(name, "/") + indexPage
415                 ff, err := fs.Open(index)
416                 if err == nil {
417                         defer ff.Close()
418                         dd, err := ff.Stat()
419                         if err == nil {
420                                 name = index
421                                 d = dd
422                                 f = ff
423                         }
424                 }
425         }
426
427         // Still a directory? (we didn't find an index.html file)
428         if d.IsDir() {
429                 if checkLastModified(w, r, d.ModTime()) {
430                         return
431                 }
432                 dirList(w, f)
433                 return
434         }
435
436         // serveContent will check modification time
437         sizeFunc := func() (int64, error) { return d.Size(), nil }
438         serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
439 }
440
441 // toHTTPError returns a non-specific HTTP error message and status code
442 // for a given non-nil error value. It's important that toHTTPError does not
443 // actually return err.Error(), since msg and httpStatus are returned to users,
444 // and historically Go's ServeContent always returned just "404 Not Found" for
445 // all errors. We don't want to start leaking information in error messages.
446 func toHTTPError(err error) (msg string, httpStatus int) {
447         if os.IsNotExist(err) {
448                 return "404 page not found", StatusNotFound
449         }
450         if os.IsPermission(err) {
451                 return "403 Forbidden", StatusForbidden
452         }
453         // Default:
454         return "500 Internal Server Error", StatusInternalServerError
455 }
456
457 // localRedirect gives a Moved Permanently response.
458 // It does not convert relative paths to absolute paths like Redirect does.
459 func localRedirect(w ResponseWriter, r *Request, newPath string) {
460         if q := r.URL.RawQuery; q != "" {
461                 newPath += "?" + q
462         }
463         w.Header().Set("Location", newPath)
464         w.WriteHeader(StatusMovedPermanently)
465 }
466
467 // ServeFile replies to the request with the contents of the named
468 // file or directory.
469 //
470 // If the provided file or directory name is a relative path, it is
471 // interpreted relative to the current directory and may ascend to parent
472 // directories. If the provided name is constructed from user input, it
473 // should be sanitized before calling ServeFile. As a precaution, ServeFile
474 // will reject requests where r.URL.Path contains a ".." path element.
475 //
476 // As a special case, ServeFile redirects any request where r.URL.Path
477 // ends in "/index.html" to the same path, without the final
478 // "index.html". To avoid such redirects either modify the path or
479 // use ServeContent.
480 func ServeFile(w ResponseWriter, r *Request, name string) {
481         if containsDotDot(r.URL.Path) {
482                 // Too many programs use r.URL.Path to construct the argument to
483                 // serveFile. Reject the request under the assumption that happened
484                 // here and ".." may not be wanted.
485                 // Note that name might not contain "..", for example if code (still
486                 // incorrectly) used filepath.Join(myDir, r.URL.Path).
487                 Error(w, "invalid URL path", StatusBadRequest)
488                 return
489         }
490         dir, file := filepath.Split(name)
491         serveFile(w, r, Dir(dir), file, false)
492 }
493
494 func containsDotDot(v string) bool {
495         if !strings.Contains(v, "..") {
496                 return false
497         }
498         for _, ent := range strings.FieldsFunc(v, isSlashRune) {
499                 if ent == ".." {
500                         return true
501                 }
502         }
503         return false
504 }
505
506 func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
507
508 type fileHandler struct {
509         root FileSystem
510 }
511
512 // FileServer returns a handler that serves HTTP requests
513 // with the contents of the file system rooted at root.
514 //
515 // To use the operating system's file system implementation,
516 // use http.Dir:
517 //
518 //     http.Handle("/", http.FileServer(http.Dir("/tmp")))
519 //
520 // As a special case, the returned file server redirects any request
521 // ending in "/index.html" to the same path, without the final
522 // "index.html".
523 func FileServer(root FileSystem) Handler {
524         return &fileHandler{root}
525 }
526
527 func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
528         upath := r.URL.Path
529         if !strings.HasPrefix(upath, "/") {
530                 upath = "/" + upath
531                 r.URL.Path = upath
532         }
533         serveFile(w, r, f.root, path.Clean(upath), true)
534 }
535
536 // httpRange specifies the byte range to be sent to the client.
537 type httpRange struct {
538         start, length int64
539 }
540
541 func (r httpRange) contentRange(size int64) string {
542         return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
543 }
544
545 func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
546         return textproto.MIMEHeader{
547                 "Content-Range": {r.contentRange(size)},
548                 "Content-Type":  {contentType},
549         }
550 }
551
552 // parseRange parses a Range header string as per RFC 2616.
553 // errNoOverlap is returned if none of the ranges overlap.
554 func parseRange(s string, size int64) ([]httpRange, error) {
555         if s == "" {
556                 return nil, nil // header not present
557         }
558         const b = "bytes="
559         if !strings.HasPrefix(s, b) {
560                 return nil, errors.New("invalid range")
561         }
562         var ranges []httpRange
563         noOverlap := false
564         for _, ra := range strings.Split(s[len(b):], ",") {
565                 ra = strings.TrimSpace(ra)
566                 if ra == "" {
567                         continue
568                 }
569                 i := strings.Index(ra, "-")
570                 if i < 0 {
571                         return nil, errors.New("invalid range")
572                 }
573                 start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
574                 var r httpRange
575                 if start == "" {
576                         // If no start is specified, end specifies the
577                         // range start relative to the end of the file.
578                         i, err := strconv.ParseInt(end, 10, 64)
579                         if err != nil {
580                                 return nil, errors.New("invalid range")
581                         }
582                         if i > size {
583                                 i = size
584                         }
585                         r.start = size - i
586                         r.length = size - r.start
587                 } else {
588                         i, err := strconv.ParseInt(start, 10, 64)
589                         if err != nil || i < 0 {
590                                 return nil, errors.New("invalid range")
591                         }
592                         if i >= size {
593                                 // If the range begins after the size of the content,
594                                 // then it does not overlap.
595                                 noOverlap = true
596                                 continue
597                         }
598                         r.start = i
599                         if end == "" {
600                                 // If no end is specified, range extends to end of the file.
601                                 r.length = size - r.start
602                         } else {
603                                 i, err := strconv.ParseInt(end, 10, 64)
604                                 if err != nil || r.start > i {
605                                         return nil, errors.New("invalid range")
606                                 }
607                                 if i >= size {
608                                         i = size - 1
609                                 }
610                                 r.length = i - r.start + 1
611                         }
612                 }
613                 ranges = append(ranges, r)
614         }
615         if noOverlap && len(ranges) == 0 {
616                 // The specified ranges did not overlap with the content.
617                 return nil, errNoOverlap
618         }
619         return ranges, nil
620 }
621
622 // countingWriter counts how many bytes have been written to it.
623 type countingWriter int64
624
625 func (w *countingWriter) Write(p []byte) (n int, err error) {
626         *w += countingWriter(len(p))
627         return len(p), nil
628 }
629
630 // rangesMIMESize returns the number of bytes it takes to encode the
631 // provided ranges as a multipart response.
632 func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
633         var w countingWriter
634         mw := multipart.NewWriter(&w)
635         for _, ra := range ranges {
636                 mw.CreatePart(ra.mimeHeader(contentType, contentSize))
637                 encSize += ra.length
638         }
639         mw.Close()
640         encSize += int64(w)
641         return
642 }
643
644 func sumRangesSize(ranges []httpRange) (size int64) {
645         for _, ra := range ranges {
646                 size += ra.length
647         }
648         return
649 }
650
651 type byName []os.FileInfo
652
653 func (s byName) Len() int           { return len(s) }
654 func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
655 func (s byName) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }