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.
5 // HTTP file system request handler
26 // A Dir implements FileSystem using the native file system restricted to a
27 // specific directory tree.
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 '/'.
33 // An empty Dir is treated as ".".
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")
45 f, err := os.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))
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)
59 // A File is returned by a FileSystem's Open method and can be
60 // served by the FileServer implementation.
62 // The methods should behave the same as those on an *os.File.
67 Readdir(count int) ([]os.FileInfo, error)
68 Stat() (os.FileInfo, error)
71 func dirList(w ResponseWriter, f File) {
72 dirs, err := f.Readdir(-1)
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
77 Error(w, "Error reading directory", StatusInternalServerError)
80 sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
82 w.Header().Set("Content-Type", "text/html; charset=utf-8")
83 fmt.Fprintf(w, "<pre>\n")
84 for _, d := range dirs {
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))
95 fmt.Fprintf(w, "</pre>\n")
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-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since,
102 // and If-Range requests.
104 // If the response's Content-Type header is not set, ServeContent
105 // first tries to deduce the type from name's file extension and,
106 // if that fails, falls back to reading the first block of the content
107 // and passing it to DetectContentType.
108 // The name is otherwise unused; in particular it can be empty and is
109 // never sent in the response.
111 // If modtime is not the zero time or Unix epoch, ServeContent
112 // includes it in a Last-Modified header in the response. If the
113 // request includes an If-Modified-Since header, ServeContent uses
114 // modtime to decide whether the content needs to be sent at all.
116 // The content's Seek method must work: ServeContent uses
117 // a seek to the end of the content to determine its size.
119 // If the caller has set w's ETag header formatted per RFC 7232, section 2.3,
120 // ServeContent uses it to handle requests using If-Match, If-None-Match, or If-Range.
122 // Note that *os.File implements the io.ReadSeeker interface.
123 func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker) {
124 sizeFunc := func() (int64, error) {
125 size, err := content.Seek(0, io.SeekEnd)
129 _, err = content.Seek(0, io.SeekStart)
135 serveContent(w, req, name, modtime, sizeFunc, content)
138 // errSeeker is returned by ServeContent's sizeFunc when the content
139 // doesn't seek properly. The underlying Seeker's error text isn't
140 // included in the sizeFunc reply so it's not sent over HTTP to end
142 var errSeeker = errors.New("seeker can't seek")
144 // errNoOverlap is returned by serveContent's parseRange if first-byte-pos of
145 // all of the byte-range-spec values is greater than the content size.
146 var errNoOverlap = errors.New("invalid range: failed to overlap")
148 // if name is empty, filename is unknown. (used for mime type, before sniffing)
149 // if modtime.IsZero(), modtime is unknown.
150 // content must be seeked to the beginning of the file.
151 // The sizeFunc is called at most once. Its error, if any, is sent in the HTTP response.
152 func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, sizeFunc func() (int64, error), content io.ReadSeeker) {
153 setLastModified(w, modtime)
154 done, rangeReq := checkPreconditions(w, r, modtime)
161 // If Content-Type isn't set, use the file's extension to find it, but
162 // if the Content-Type is unset explicitly, do not sniff the type.
163 ctypes, haveType := w.Header()["Content-Type"]
166 ctype = mime.TypeByExtension(filepath.Ext(name))
168 // read a chunk to decide between utf-8 text and binary
169 var buf [sniffLen]byte
170 n, _ := io.ReadFull(content, buf[:])
171 ctype = DetectContentType(buf[:n])
172 _, err := content.Seek(0, io.SeekStart) // rewind to output whole file
174 Error(w, "seeker can't seek", StatusInternalServerError)
178 w.Header().Set("Content-Type", ctype)
179 } else if len(ctypes) > 0 {
183 size, err := sizeFunc()
185 Error(w, err.Error(), StatusInternalServerError)
189 // handle Content-Range header.
191 var sendContent io.Reader = content
193 ranges, err := parseRange(rangeReq, size)
195 if err == errNoOverlap {
196 w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", size))
198 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
201 if sumRangesSize(ranges) > size {
202 // The total number of bytes in all the ranges
203 // is larger than the size of the file by
204 // itself, so this is probably an attack, or a
205 // dumb client. Ignore the range request.
209 case len(ranges) == 1:
210 // RFC 2616, Section 14.16:
211 // "When an HTTP message includes the content of a single
212 // range (for example, a response to a request for a
213 // single range, or to a request for a set of ranges
214 // that overlap without any holes), this content is
215 // transmitted with a Content-Range header, and a
216 // Content-Length header showing the number of bytes
217 // actually transferred.
219 // A response to a request for a single range MUST NOT
220 // be sent using the multipart/byteranges media type."
222 if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
223 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
227 code = StatusPartialContent
228 w.Header().Set("Content-Range", ra.contentRange(size))
229 case len(ranges) > 1:
230 sendSize = rangesMIMESize(ranges, ctype, size)
231 code = StatusPartialContent
234 mw := multipart.NewWriter(pw)
235 w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
237 defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
239 for _, ra := range ranges {
240 part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
242 pw.CloseWithError(err)
245 if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
246 pw.CloseWithError(err)
249 if _, err := io.CopyN(part, content, ra.length); err != nil {
250 pw.CloseWithError(err)
259 w.Header().Set("Accept-Ranges", "bytes")
260 if w.Header().Get("Content-Encoding") == "" {
261 w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
267 if r.Method != "HEAD" {
268 io.CopyN(w, sendContent, sendSize)
272 // scanETag determines if a syntactically valid ETag is present at s. If so,
273 // the ETag and remaining text after consuming ETag is returned. Otherwise,
274 // it returns "", "".
275 func scanETag(s string) (etag string, remain string) {
276 s = textproto.TrimString(s)
278 if strings.HasPrefix(s, "W/") {
281 if len(s[start:]) < 2 || s[start] != '"' {
284 // ETag is either W/"text" or "text".
286 for i := start + 1; i < len(s); i++ {
289 // Character values allowed in ETags.
290 case c == 0x21 || c >= 0x23 && c <= 0x7E || c >= 0x80:
292 return string(s[:i+1]), s[i+1:]
300 // etagStrongMatch reports whether a and b match using strong ETag comparison.
301 // Assumes a and b are valid ETags.
302 func etagStrongMatch(a, b string) bool {
303 return a == b && a != "" && a[0] == '"'
306 // etagWeakMatch reports whether a and b match using weak ETag comparison.
307 // Assumes a and b are valid ETags.
308 func etagWeakMatch(a, b string) bool {
309 return strings.TrimPrefix(a, "W/") == strings.TrimPrefix(b, "W/")
312 // condResult is the result of an HTTP request precondition check.
313 // See https://tools.ietf.org/html/rfc7232 section 3.
317 condNone condResult = iota
322 func checkIfMatch(w ResponseWriter, r *Request) condResult {
323 im := r.Header.Get("If-Match")
328 im = textproto.TrimString(im)
339 etag, remain := scanETag(im)
343 if etagStrongMatch(etag, w.Header().get("Etag")) {
352 func checkIfUnmodifiedSince(w ResponseWriter, r *Request, modtime time.Time) condResult {
353 ius := r.Header.Get("If-Unmodified-Since")
354 if ius == "" || isZeroTime(modtime) {
357 if t, err := ParseTime(ius); err == nil {
358 // The Date-Modified header truncates sub-second precision, so
359 // use mtime < t+1s instead of mtime <= t to check for unmodified.
360 if modtime.Before(t.Add(1 * time.Second)) {
368 func checkIfNoneMatch(w ResponseWriter, r *Request) condResult {
369 inm := r.Header.get("If-None-Match")
375 buf = textproto.TrimString(buf)
385 etag, remain := scanETag(buf)
389 if etagWeakMatch(etag, w.Header().get("Etag")) {
397 func checkIfModifiedSince(w ResponseWriter, r *Request, modtime time.Time) condResult {
398 if r.Method != "GET" && r.Method != "HEAD" {
401 ims := r.Header.Get("If-Modified-Since")
402 if ims == "" || isZeroTime(modtime) {
405 t, err := ParseTime(ims)
409 // The Date-Modified header truncates sub-second precision, so
410 // use mtime < t+1s instead of mtime <= t to check for unmodified.
411 if modtime.Before(t.Add(1 * time.Second)) {
417 func checkIfRange(w ResponseWriter, r *Request, modtime time.Time) condResult {
418 if r.Method != "GET" {
421 ir := r.Header.get("If-Range")
425 etag, _ := scanETag(ir)
427 if etagStrongMatch(etag, w.Header().Get("Etag")) {
433 // The If-Range value is typically the ETag value, but it may also be
434 // the modtime date. See golang.org/issue/8367.
435 if modtime.IsZero() {
438 t, err := ParseTime(ir)
442 if t.Unix() == modtime.Unix() {
448 var unixEpochTime = time.Unix(0, 0)
450 // isZeroTime reports whether t is obviously unspecified (either zero or Unix()=0).
451 func isZeroTime(t time.Time) bool {
452 return t.IsZero() || t.Equal(unixEpochTime)
455 func setLastModified(w ResponseWriter, modtime time.Time) {
456 if !isZeroTime(modtime) {
457 w.Header().Set("Last-Modified", modtime.UTC().Format(TimeFormat))
461 func writeNotModified(w ResponseWriter) {
462 // RFC 7232 section 4.1:
463 // a sender SHOULD NOT generate representation metadata other than the
464 // above listed fields unless said metadata exists for the purpose of
465 // guiding cache updates (e.g., Last-Modified might be useful if the
466 // response does not have an ETag field).
468 delete(h, "Content-Type")
469 delete(h, "Content-Length")
470 if h.Get("Etag") != "" {
471 delete(h, "Last-Modified")
473 w.WriteHeader(StatusNotModified)
476 // checkPreconditions evaluates request preconditions and reports whether a precondition
477 // resulted in sending StatusNotModified or StatusPreconditionFailed.
478 func checkPreconditions(w ResponseWriter, r *Request, modtime time.Time) (done bool, rangeHeader string) {
479 // This function carefully follows RFC 7232 section 6.
480 ch := checkIfMatch(w, r)
482 ch = checkIfUnmodifiedSince(w, r, modtime)
485 w.WriteHeader(StatusPreconditionFailed)
488 switch checkIfNoneMatch(w, r) {
490 if r.Method == "GET" || r.Method == "HEAD" {
494 w.WriteHeader(StatusPreconditionFailed)
498 if checkIfModifiedSince(w, r, modtime) == condFalse {
504 rangeHeader = r.Header.get("Range")
505 if rangeHeader != "" {
506 if checkIfRange(w, r, modtime) == condFalse {
510 return false, rangeHeader
513 // name is '/'-separated, not filepath.Separator.
514 func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
515 const indexPage = "/index.html"
517 // redirect .../index.html to .../
518 // can't use Redirect() because that would make the path absolute,
519 // which would be a problem running under StripPrefix
520 if strings.HasSuffix(r.URL.Path, indexPage) {
521 localRedirect(w, r, "./")
525 f, err := fs.Open(name)
527 msg, code := toHTTPError(err)
535 msg, code := toHTTPError(err)
541 // redirect to canonical path: / at end of directory url
542 // r.URL.Path always begins with /
545 if url[len(url)-1] != '/' {
546 localRedirect(w, r, path.Base(url)+"/")
550 if url[len(url)-1] == '/' {
551 localRedirect(w, r, "../"+path.Base(url))
557 // redirect if the directory name doesn't end in a slash
560 if url[len(url)-1] != '/' {
561 localRedirect(w, r, path.Base(url)+"/")
566 // use contents of index.html for directory, if present
568 index := strings.TrimSuffix(name, "/") + indexPage
569 ff, err := fs.Open(index)
581 // Still a directory? (we didn't find an index.html file)
583 if checkIfModifiedSince(w, r, d.ModTime()) == condFalse {
587 w.Header().Set("Last-Modified", d.ModTime().UTC().Format(TimeFormat))
592 // serveContent will check modification time
593 sizeFunc := func() (int64, error) { return d.Size(), nil }
594 serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
597 // toHTTPError returns a non-specific HTTP error message and status code
598 // for a given non-nil error value. It's important that toHTTPError does not
599 // actually return err.Error(), since msg and httpStatus are returned to users,
600 // and historically Go's ServeContent always returned just "404 Not Found" for
601 // all errors. We don't want to start leaking information in error messages.
602 func toHTTPError(err error) (msg string, httpStatus int) {
603 if os.IsNotExist(err) {
604 return "404 page not found", StatusNotFound
606 if os.IsPermission(err) {
607 return "403 Forbidden", StatusForbidden
610 return "500 Internal Server Error", StatusInternalServerError
613 // localRedirect gives a Moved Permanently response.
614 // It does not convert relative paths to absolute paths like Redirect does.
615 func localRedirect(w ResponseWriter, r *Request, newPath string) {
616 if q := r.URL.RawQuery; q != "" {
619 w.Header().Set("Location", newPath)
620 w.WriteHeader(StatusMovedPermanently)
623 // ServeFile replies to the request with the contents of the named
624 // file or directory.
626 // If the provided file or directory name is a relative path, it is
627 // interpreted relative to the current directory and may ascend to parent
628 // directories. If the provided name is constructed from user input, it
629 // should be sanitized before calling ServeFile. As a precaution, ServeFile
630 // will reject requests where r.URL.Path contains a ".." path element.
632 // As a special case, ServeFile redirects any request where r.URL.Path
633 // ends in "/index.html" to the same path, without the final
634 // "index.html". To avoid such redirects either modify the path or
636 func ServeFile(w ResponseWriter, r *Request, name string) {
637 if containsDotDot(r.URL.Path) {
638 // Too many programs use r.URL.Path to construct the argument to
639 // serveFile. Reject the request under the assumption that happened
640 // here and ".." may not be wanted.
641 // Note that name might not contain "..", for example if code (still
642 // incorrectly) used filepath.Join(myDir, r.URL.Path).
643 Error(w, "invalid URL path", StatusBadRequest)
646 dir, file := filepath.Split(name)
647 serveFile(w, r, Dir(dir), file, false)
650 func containsDotDot(v string) bool {
651 if !strings.Contains(v, "..") {
654 for _, ent := range strings.FieldsFunc(v, isSlashRune) {
662 func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
664 type fileHandler struct {
668 // FileServer returns a handler that serves HTTP requests
669 // with the contents of the file system rooted at root.
671 // To use the operating system's file system implementation,
674 // http.Handle("/", http.FileServer(http.Dir("/tmp")))
676 // As a special case, the returned file server redirects any request
677 // ending in "/index.html" to the same path, without the final
679 func FileServer(root FileSystem) Handler {
680 return &fileHandler{root}
683 func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
685 if !strings.HasPrefix(upath, "/") {
689 serveFile(w, r, f.root, path.Clean(upath), true)
692 // httpRange specifies the byte range to be sent to the client.
693 type httpRange struct {
697 func (r httpRange) contentRange(size int64) string {
698 return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
701 func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
702 return textproto.MIMEHeader{
703 "Content-Range": {r.contentRange(size)},
704 "Content-Type": {contentType},
708 // parseRange parses a Range header string as per RFC 2616.
709 // errNoOverlap is returned if none of the ranges overlap.
710 func parseRange(s string, size int64) ([]httpRange, error) {
712 return nil, nil // header not present
715 if !strings.HasPrefix(s, b) {
716 return nil, errors.New("invalid range")
718 var ranges []httpRange
720 for _, ra := range strings.Split(s[len(b):], ",") {
721 ra = strings.TrimSpace(ra)
725 i := strings.Index(ra, "-")
727 return nil, errors.New("invalid range")
729 start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
732 // If no start is specified, end specifies the
733 // range start relative to the end of the file.
734 i, err := strconv.ParseInt(end, 10, 64)
736 return nil, errors.New("invalid range")
742 r.length = size - r.start
744 i, err := strconv.ParseInt(start, 10, 64)
745 if err != nil || i < 0 {
746 return nil, errors.New("invalid range")
749 // If the range begins after the size of the content,
750 // then it does not overlap.
756 // If no end is specified, range extends to end of the file.
757 r.length = size - r.start
759 i, err := strconv.ParseInt(end, 10, 64)
760 if err != nil || r.start > i {
761 return nil, errors.New("invalid range")
766 r.length = i - r.start + 1
769 ranges = append(ranges, r)
771 if noOverlap && len(ranges) == 0 {
772 // The specified ranges did not overlap with the content.
773 return nil, errNoOverlap
778 // countingWriter counts how many bytes have been written to it.
779 type countingWriter int64
781 func (w *countingWriter) Write(p []byte) (n int, err error) {
782 *w += countingWriter(len(p))
786 // rangesMIMESize returns the number of bytes it takes to encode the
787 // provided ranges as a multipart response.
788 func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
790 mw := multipart.NewWriter(&w)
791 for _, ra := range ranges {
792 mw.CreatePart(ra.mimeHeader(contentType, contentSize))
800 func sumRangesSize(ranges []httpRange) (size int64) {
801 for _, ra := range ranges {