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.Sort(byName(dirs))
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-Modified-Since requests.
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.
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.
115 // The content's Seek method must work: ServeContent uses
116 // a seek to the end of the content to determine its size.
118 // If the caller has set w's ETag header, ServeContent uses it to
119 // handle requests using If-Range and If-None-Match.
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)
128 _, err = content.Seek(0, io.SeekStart)
134 serveContent(w, req, name, modtime, sizeFunc, content)
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
141 var errSeeker = errors.New("seeker can't seek")
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")
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) {
155 rangeReq, done := checkETag(w, r, modtime)
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"]
167 ctype = mime.TypeByExtension(filepath.Ext(name))
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
175 Error(w, "seeker can't seek", StatusInternalServerError)
179 w.Header().Set("Content-Type", ctype)
180 } else if len(ctypes) > 0 {
184 size, err := sizeFunc()
186 Error(w, err.Error(), StatusInternalServerError)
190 // handle Content-Range header.
192 var sendContent io.Reader = content
194 ranges, err := parseRange(rangeReq, size)
196 if err == errNoOverlap {
197 w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", size))
199 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
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.
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.
220 // A response to a request for a single range MUST NOT
221 // be sent using the multipart/byteranges media type."
223 if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
224 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
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
235 mw := multipart.NewWriter(pw)
236 w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
238 defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
240 for _, ra := range ranges {
241 part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
243 pw.CloseWithError(err)
246 if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
247 pw.CloseWithError(err)
250 if _, err := io.CopyN(part, content, ra.length); err != nil {
251 pw.CloseWithError(err)
260 w.Header().Set("Accept-Ranges", "bytes")
261 if w.Header().Get("Content-Encoding") == "" {
262 w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
268 if r.Method != "HEAD" {
269 io.CopyN(w, sendContent, sendSize)
273 var unixEpochTime = time.Unix(0, 0)
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.
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)) {
289 delete(h, "Content-Type")
290 delete(h, "Content-Length")
291 w.WriteHeader(StatusNotModified)
294 w.Header().Set("Last-Modified", modtime.UTC().Format(TimeFormat))
298 // checkETag implements If-None-Match and If-Range checks.
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.
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")
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
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.
320 if !modtime.IsZero() {
321 if t, err := ParseTime(ir); err == nil && t.Unix() == modtime.Unix() {
330 if inm := r.Header.get("If-None-Match"); inm != "" {
333 return rangeReq, false
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
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 == "*" {
350 delete(h, "Content-Type")
351 delete(h, "Content-Length")
352 w.WriteHeader(StatusNotModified)
356 return rangeReq, false
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"
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, "./")
371 f, err := fs.Open(name)
373 msg, code := toHTTPError(err)
381 msg, code := toHTTPError(err)
387 // redirect to canonical path: / at end of directory url
388 // r.URL.Path always begins with /
391 if url[len(url)-1] != '/' {
392 localRedirect(w, r, path.Base(url)+"/")
396 if url[len(url)-1] == '/' {
397 localRedirect(w, r, "../"+path.Base(url))
403 // redirect if the directory name doesn't end in a slash
406 if url[len(url)-1] != '/' {
407 localRedirect(w, r, path.Base(url)+"/")
412 // use contents of index.html for directory, if present
414 index := strings.TrimSuffix(name, "/") + indexPage
415 ff, err := fs.Open(index)
427 // Still a directory? (we didn't find an index.html file)
429 if checkLastModified(w, r, d.ModTime()) {
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)
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
450 if os.IsPermission(err) {
451 return "403 Forbidden", StatusForbidden
454 return "500 Internal Server Error", StatusInternalServerError
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 != "" {
463 w.Header().Set("Location", newPath)
464 w.WriteHeader(StatusMovedPermanently)
467 // ServeFile replies to the request with the contents of the named
468 // file or directory.
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.
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
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)
490 dir, file := filepath.Split(name)
491 serveFile(w, r, Dir(dir), file, false)
494 func containsDotDot(v string) bool {
495 if !strings.Contains(v, "..") {
498 for _, ent := range strings.FieldsFunc(v, isSlashRune) {
506 func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
508 type fileHandler struct {
512 // FileServer returns a handler that serves HTTP requests
513 // with the contents of the file system rooted at root.
515 // To use the operating system's file system implementation,
518 // http.Handle("/", http.FileServer(http.Dir("/tmp")))
520 // As a special case, the returned file server redirects any request
521 // ending in "/index.html" to the same path, without the final
523 func FileServer(root FileSystem) Handler {
524 return &fileHandler{root}
527 func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
529 if !strings.HasPrefix(upath, "/") {
533 serveFile(w, r, f.root, path.Clean(upath), true)
536 // httpRange specifies the byte range to be sent to the client.
537 type httpRange struct {
541 func (r httpRange) contentRange(size int64) string {
542 return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
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},
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) {
556 return nil, nil // header not present
559 if !strings.HasPrefix(s, b) {
560 return nil, errors.New("invalid range")
562 var ranges []httpRange
564 for _, ra := range strings.Split(s[len(b):], ",") {
565 ra = strings.TrimSpace(ra)
569 i := strings.Index(ra, "-")
571 return nil, errors.New("invalid range")
573 start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
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)
580 return nil, errors.New("invalid range")
586 r.length = size - r.start
588 i, err := strconv.ParseInt(start, 10, 64)
589 if err != nil || i < 0 {
590 return nil, errors.New("invalid range")
593 // If the range begins after the size of the content,
594 // then it does not overlap.
600 // If no end is specified, range extends to end of the file.
601 r.length = size - r.start
603 i, err := strconv.ParseInt(end, 10, 64)
604 if err != nil || r.start > i {
605 return nil, errors.New("invalid range")
610 r.length = i - r.start + 1
613 ranges = append(ranges, r)
615 if noOverlap && len(ranges) == 0 {
616 // The specified ranges did not overlap with the content.
617 return nil, errNoOverlap
622 // countingWriter counts how many bytes have been written to it.
623 type countingWriter int64
625 func (w *countingWriter) Write(p []byte) (n int, err error) {
626 *w += countingWriter(len(p))
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) {
634 mw := multipart.NewWriter(&w)
635 for _, ra := range ranges {
636 mw.CreatePart(ra.mimeHeader(contentType, contentSize))
644 func sumRangesSize(ranges []httpRange) (size int64) {
645 for _, ra := range ranges {
651 type byName []os.FileInfo
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] }