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 // if name is empty, filename is unknown. (used for mime type, before sniffing)
144 // if modtime.IsZero(), modtime is unknown.
145 // content must be seeked to the beginning of the file.
146 // The sizeFunc is called at most once. Its error, if any, is sent in the HTTP response.
147 func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, sizeFunc func() (int64, error), content io.ReadSeeker) {
148 if checkLastModified(w, r, modtime) {
151 rangeReq, done := checkETag(w, r, modtime)
158 // If Content-Type isn't set, use the file's extension to find it, but
159 // if the Content-Type is unset explicitly, do not sniff the type.
160 ctypes, haveType := w.Header()["Content-Type"]
163 ctype = mime.TypeByExtension(filepath.Ext(name))
165 // read a chunk to decide between utf-8 text and binary
166 var buf [sniffLen]byte
167 n, _ := io.ReadFull(content, buf[:])
168 ctype = DetectContentType(buf[:n])
169 _, err := content.Seek(0, io.SeekStart) // rewind to output whole file
171 Error(w, "seeker can't seek", StatusInternalServerError)
175 w.Header().Set("Content-Type", ctype)
176 } else if len(ctypes) > 0 {
180 size, err := sizeFunc()
182 Error(w, err.Error(), StatusInternalServerError)
186 // handle Content-Range header.
188 var sendContent io.Reader = content
190 ranges, err := parseRange(rangeReq, size)
192 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
195 if sumRangesSize(ranges) > size {
196 // The total number of bytes in all the ranges
197 // is larger than the size of the file by
198 // itself, so this is probably an attack, or a
199 // dumb client. Ignore the range request.
203 case len(ranges) == 1:
204 // RFC 2616, Section 14.16:
205 // "When an HTTP message includes the content of a single
206 // range (for example, a response to a request for a
207 // single range, or to a request for a set of ranges
208 // that overlap without any holes), this content is
209 // transmitted with a Content-Range header, and a
210 // Content-Length header showing the number of bytes
211 // actually transferred.
213 // A response to a request for a single range MUST NOT
214 // be sent using the multipart/byteranges media type."
216 if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
217 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
221 code = StatusPartialContent
222 w.Header().Set("Content-Range", ra.contentRange(size))
223 case len(ranges) > 1:
224 sendSize = rangesMIMESize(ranges, ctype, size)
225 code = StatusPartialContent
228 mw := multipart.NewWriter(pw)
229 w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
231 defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
233 for _, ra := range ranges {
234 part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
236 pw.CloseWithError(err)
239 if _, err := content.Seek(ra.start, io.SeekStart); err != nil {
240 pw.CloseWithError(err)
243 if _, err := io.CopyN(part, content, ra.length); err != nil {
244 pw.CloseWithError(err)
253 w.Header().Set("Accept-Ranges", "bytes")
254 if w.Header().Get("Content-Encoding") == "" {
255 w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
261 if r.Method != "HEAD" {
262 io.CopyN(w, sendContent, sendSize)
266 var unixEpochTime = time.Unix(0, 0)
268 // modtime is the modification time of the resource to be served, or IsZero().
269 // return value is whether this request is now complete.
270 func checkLastModified(w ResponseWriter, r *Request, modtime time.Time) bool {
271 if modtime.IsZero() || modtime.Equal(unixEpochTime) {
272 // If the file doesn't have a modtime (IsZero), or the modtime
273 // is obviously garbage (Unix time == 0), then ignore modtimes
274 // and don't process the If-Modified-Since header.
278 // The Date-Modified header truncates sub-second precision, so
279 // use mtime < t+1s instead of mtime <= t to check for unmodified.
280 if t, err := time.Parse(TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
282 delete(h, "Content-Type")
283 delete(h, "Content-Length")
284 w.WriteHeader(StatusNotModified)
287 w.Header().Set("Last-Modified", modtime.UTC().Format(TimeFormat))
291 // checkETag implements If-None-Match and If-Range checks.
293 // The ETag or modtime must have been previously set in the
294 // ResponseWriter's headers. The modtime is only compared at second
295 // granularity and may be the zero value to mean unknown.
297 // The return value is the effective request "Range" header to use and
298 // whether this request is now considered done.
299 func checkETag(w ResponseWriter, r *Request, modtime time.Time) (rangeReq string, done bool) {
300 etag := w.Header().get("Etag")
301 rangeReq = r.Header.get("Range")
303 // Invalidate the range request if the entity doesn't match the one
304 // the client was expecting.
305 // "If-Range: version" means "ignore the Range: header unless version matches the
307 // We only support ETag versions.
308 // The caller must have set the ETag on the response already.
309 if ir := r.Header.get("If-Range"); ir != "" && ir != etag {
310 // The If-Range value is typically the ETag value, but it may also be
311 // the modtime date. See golang.org/issue/8367.
313 if !modtime.IsZero() {
314 if t, err := ParseTime(ir); err == nil && t.Unix() == modtime.Unix() {
323 if inm := r.Header.get("If-None-Match"); inm != "" {
326 return rangeReq, false
329 // TODO(bradfitz): non-GET/HEAD requests require more work:
330 // sending a different status code on matches, and
331 // also can't use weak cache validators (those with a "W/
332 // prefix). But most users of ServeContent will be using
333 // it on GET or HEAD, so only support those for now.
334 if r.Method != "GET" && r.Method != "HEAD" {
335 return rangeReq, false
338 // TODO(bradfitz): deal with comma-separated or multiple-valued
339 // list of If-None-match values. For now just handle the common
340 // case of a single item.
341 if inm == etag || inm == "*" {
343 delete(h, "Content-Type")
344 delete(h, "Content-Length")
345 w.WriteHeader(StatusNotModified)
349 return rangeReq, false
352 // name is '/'-separated, not filepath.Separator.
353 func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
354 const indexPage = "/index.html"
356 // redirect .../index.html to .../
357 // can't use Redirect() because that would make the path absolute,
358 // which would be a problem running under StripPrefix
359 if strings.HasSuffix(r.URL.Path, indexPage) {
360 localRedirect(w, r, "./")
364 f, err := fs.Open(name)
366 msg, code := toHTTPError(err)
374 msg, code := toHTTPError(err)
380 // redirect to canonical path: / at end of directory url
381 // r.URL.Path always begins with /
384 if url[len(url)-1] != '/' {
385 localRedirect(w, r, path.Base(url)+"/")
389 if url[len(url)-1] == '/' {
390 localRedirect(w, r, "../"+path.Base(url))
396 // redirect if the directory name doesn't end in a slash
399 if url[len(url)-1] != '/' {
400 localRedirect(w, r, path.Base(url)+"/")
405 // use contents of index.html for directory, if present
407 index := strings.TrimSuffix(name, "/") + indexPage
408 ff, err := fs.Open(index)
420 // Still a directory? (we didn't find an index.html file)
422 if checkLastModified(w, r, d.ModTime()) {
429 // serveContent will check modification time
430 sizeFunc := func() (int64, error) { return d.Size(), nil }
431 serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
434 // toHTTPError returns a non-specific HTTP error message and status code
435 // for a given non-nil error value. It's important that toHTTPError does not
436 // actually return err.Error(), since msg and httpStatus are returned to users,
437 // and historically Go's ServeContent always returned just "404 Not Found" for
438 // all errors. We don't want to start leaking information in error messages.
439 func toHTTPError(err error) (msg string, httpStatus int) {
440 if os.IsNotExist(err) {
441 return "404 page not found", StatusNotFound
443 if os.IsPermission(err) {
444 return "403 Forbidden", StatusForbidden
447 return "500 Internal Server Error", StatusInternalServerError
450 // localRedirect gives a Moved Permanently response.
451 // It does not convert relative paths to absolute paths like Redirect does.
452 func localRedirect(w ResponseWriter, r *Request, newPath string) {
453 if q := r.URL.RawQuery; q != "" {
456 w.Header().Set("Location", newPath)
457 w.WriteHeader(StatusMovedPermanently)
460 // ServeFile replies to the request with the contents of the named
461 // file or directory.
463 // If the provided file or directory name is a relative path, it is
464 // interpreted relative to the current directory and may ascend to parent
465 // directories. If the provided name is constructed from user input, it
466 // should be sanitized before calling ServeFile. As a precaution, ServeFile
467 // will reject requests where r.URL.Path contains a ".." path element.
469 // As a special case, ServeFile redirects any request where r.URL.Path
470 // ends in "/index.html" to the same path, without the final
471 // "index.html". To avoid such redirects either modify the path or
473 func ServeFile(w ResponseWriter, r *Request, name string) {
474 if containsDotDot(r.URL.Path) {
475 // Too many programs use r.URL.Path to construct the argument to
476 // serveFile. Reject the request under the assumption that happened
477 // here and ".." may not be wanted.
478 // Note that name might not contain "..", for example if code (still
479 // incorrectly) used filepath.Join(myDir, r.URL.Path).
480 Error(w, "invalid URL path", StatusBadRequest)
483 dir, file := filepath.Split(name)
484 serveFile(w, r, Dir(dir), file, false)
487 func containsDotDot(v string) bool {
488 if !strings.Contains(v, "..") {
491 for _, ent := range strings.FieldsFunc(v, isSlashRune) {
499 func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
501 type fileHandler struct {
505 // FileServer returns a handler that serves HTTP requests
506 // with the contents of the file system rooted at root.
508 // To use the operating system's file system implementation,
511 // http.Handle("/", http.FileServer(http.Dir("/tmp")))
513 // As a special case, the returned file server redirects any request
514 // ending in "/index.html" to the same path, without the final
516 func FileServer(root FileSystem) Handler {
517 return &fileHandler{root}
520 func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
522 if !strings.HasPrefix(upath, "/") {
526 serveFile(w, r, f.root, path.Clean(upath), true)
529 // httpRange specifies the byte range to be sent to the client.
530 type httpRange struct {
534 func (r httpRange) contentRange(size int64) string {
535 return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
538 func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
539 return textproto.MIMEHeader{
540 "Content-Range": {r.contentRange(size)},
541 "Content-Type": {contentType},
545 // parseRange parses a Range header string as per RFC 2616.
546 func parseRange(s string, size int64) ([]httpRange, error) {
548 return nil, nil // header not present
551 if !strings.HasPrefix(s, b) {
552 return nil, errors.New("invalid range")
554 var ranges []httpRange
555 for _, ra := range strings.Split(s[len(b):], ",") {
556 ra = strings.TrimSpace(ra)
560 i := strings.Index(ra, "-")
562 return nil, errors.New("invalid range")
564 start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
567 // If no start is specified, end specifies the
568 // range start relative to the end of the file.
569 i, err := strconv.ParseInt(end, 10, 64)
571 return nil, errors.New("invalid range")
577 r.length = size - r.start
579 i, err := strconv.ParseInt(start, 10, 64)
580 if err != nil || i >= size || i < 0 {
581 return nil, errors.New("invalid range")
585 // If no end is specified, range extends to end of the file.
586 r.length = size - r.start
588 i, err := strconv.ParseInt(end, 10, 64)
589 if err != nil || r.start > i {
590 return nil, errors.New("invalid range")
595 r.length = i - r.start + 1
598 ranges = append(ranges, r)
603 // countingWriter counts how many bytes have been written to it.
604 type countingWriter int64
606 func (w *countingWriter) Write(p []byte) (n int, err error) {
607 *w += countingWriter(len(p))
611 // rangesMIMESize returns the number of bytes it takes to encode the
612 // provided ranges as a multipart response.
613 func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
615 mw := multipart.NewWriter(&w)
616 for _, ra := range ranges {
617 mw.CreatePart(ra.mimeHeader(contentType, contentSize))
625 func sumRangesSize(ranges []httpRange) (size int64) {
626 for _, ra := range ranges {
632 type byName []os.FileInfo
634 func (s byName) Len() int { return len(s) }
635 func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() }
636 func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }