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
25 // A Dir implements FileSystem using the native file system restricted to a
26 // specific directory tree.
28 // While the FileSystem.Open method takes '/'-separated paths, a Dir's string
29 // value is a filename on the native file system, not a URL, so it is separated
30 // by filepath.Separator, which isn't necessarily '/'.
32 // An empty Dir is treated as ".".
35 func (d Dir) Open(name string) (File, error) {
36 if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 ||
37 strings.Contains(name, "\x00") {
38 return nil, errors.New("http: invalid character in file path")
44 f, err := os.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))
51 // A FileSystem implements access to a collection of named files.
52 // The elements in a file path are separated by slash ('/', U+002F)
53 // characters, regardless of host operating system convention.
54 type FileSystem interface {
55 Open(name string) (File, error)
58 // A File is returned by a FileSystem's Open method and can be
59 // served by the FileServer implementation.
61 // The methods should behave the same as those on an *os.File.
65 Readdir(count int) ([]os.FileInfo, error)
66 Seek(offset int64, whence int) (int64, error)
67 Stat() (os.FileInfo, error)
70 func dirList(w ResponseWriter, f File) {
71 w.Header().Set("Content-Type", "text/html; charset=utf-8")
72 fmt.Fprintf(w, "<pre>\n")
74 dirs, err := f.Readdir(100)
75 if err != nil || len(dirs) == 0 {
78 for _, d := range dirs {
83 // name may contain '?' or '#', which must be escaped to remain
84 // part of the URL path, and not indicate the start of a query
85 // string or fragment.
86 url := url.URL{Path: name}
87 fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
90 fmt.Fprintf(w, "</pre>\n")
93 // ServeContent replies to the request using the content in the
94 // provided ReadSeeker. The main benefit of ServeContent over io.Copy
95 // is that it handles Range requests properly, sets the MIME type, and
96 // handles If-Modified-Since requests.
98 // If the response's Content-Type header is not set, ServeContent
99 // first tries to deduce the type from name's file extension and,
100 // if that fails, falls back to reading the first block of the content
101 // and passing it to DetectContentType.
102 // The name is otherwise unused; in particular it can be empty and is
103 // never sent in the response.
105 // If modtime is not the zero time or Unix epoch, ServeContent
106 // includes it in a Last-Modified header in the response. If the
107 // request includes an If-Modified-Since header, ServeContent uses
108 // modtime to decide whether the content needs to be sent at all.
110 // The content's Seek method must work: ServeContent uses
111 // a seek to the end of the content to determine its size.
113 // If the caller has set w's ETag header, ServeContent uses it to
114 // handle requests using If-Range and If-None-Match.
116 // Note that *os.File implements the io.ReadSeeker interface.
117 func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker) {
118 sizeFunc := func() (int64, error) {
119 size, err := content.Seek(0, os.SEEK_END)
123 _, err = content.Seek(0, os.SEEK_SET)
129 serveContent(w, req, name, modtime, sizeFunc, content)
132 // errSeeker is returned by ServeContent's sizeFunc when the content
133 // doesn't seek properly. The underlying Seeker's error text isn't
134 // included in the sizeFunc reply so it's not sent over HTTP to end
136 var errSeeker = errors.New("seeker can't seek")
138 // if name is empty, filename is unknown. (used for mime type, before sniffing)
139 // if modtime.IsZero(), modtime is unknown.
140 // content must be seeked to the beginning of the file.
141 // The sizeFunc is called at most once. Its error, if any, is sent in the HTTP response.
142 func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, sizeFunc func() (int64, error), content io.ReadSeeker) {
143 if checkLastModified(w, r, modtime) {
146 rangeReq, done := checkETag(w, r, modtime)
153 // If Content-Type isn't set, use the file's extension to find it, but
154 // if the Content-Type is unset explicitly, do not sniff the type.
155 ctypes, haveType := w.Header()["Content-Type"]
158 ctype = mime.TypeByExtension(filepath.Ext(name))
160 // read a chunk to decide between utf-8 text and binary
161 var buf [sniffLen]byte
162 n, _ := io.ReadFull(content, buf[:])
163 ctype = DetectContentType(buf[:n])
164 _, err := content.Seek(0, os.SEEK_SET) // rewind to output whole file
166 Error(w, "seeker can't seek", StatusInternalServerError)
170 w.Header().Set("Content-Type", ctype)
171 } else if len(ctypes) > 0 {
175 size, err := sizeFunc()
177 Error(w, err.Error(), StatusInternalServerError)
181 // handle Content-Range header.
183 var sendContent io.Reader = content
185 ranges, err := parseRange(rangeReq, size)
187 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
190 if sumRangesSize(ranges) > size {
191 // The total number of bytes in all the ranges
192 // is larger than the size of the file by
193 // itself, so this is probably an attack, or a
194 // dumb client. Ignore the range request.
198 case len(ranges) == 1:
199 // RFC 2616, Section 14.16:
200 // "When an HTTP message includes the content of a single
201 // range (for example, a response to a request for a
202 // single range, or to a request for a set of ranges
203 // that overlap without any holes), this content is
204 // transmitted with a Content-Range header, and a
205 // Content-Length header showing the number of bytes
206 // actually transferred.
208 // A response to a request for a single range MUST NOT
209 // be sent using the multipart/byteranges media type."
211 if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil {
212 Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
216 code = StatusPartialContent
217 w.Header().Set("Content-Range", ra.contentRange(size))
218 case len(ranges) > 1:
219 sendSize = rangesMIMESize(ranges, ctype, size)
220 code = StatusPartialContent
223 mw := multipart.NewWriter(pw)
224 w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
226 defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
228 for _, ra := range ranges {
229 part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
231 pw.CloseWithError(err)
234 if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil {
235 pw.CloseWithError(err)
238 if _, err := io.CopyN(part, content, ra.length); err != nil {
239 pw.CloseWithError(err)
248 w.Header().Set("Accept-Ranges", "bytes")
249 if w.Header().Get("Content-Encoding") == "" {
250 w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
256 if r.Method != "HEAD" {
257 io.CopyN(w, sendContent, sendSize)
261 var unixEpochTime = time.Unix(0, 0)
263 // modtime is the modification time of the resource to be served, or IsZero().
264 // return value is whether this request is now complete.
265 func checkLastModified(w ResponseWriter, r *Request, modtime time.Time) bool {
266 if modtime.IsZero() || modtime.Equal(unixEpochTime) {
267 // If the file doesn't have a modtime (IsZero), or the modtime
268 // is obviously garbage (Unix time == 0), then ignore modtimes
269 // and don't process the If-Modified-Since header.
273 // The Date-Modified header truncates sub-second precision, so
274 // use mtime < t+1s instead of mtime <= t to check for unmodified.
275 if t, err := time.Parse(TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
277 delete(h, "Content-Type")
278 delete(h, "Content-Length")
279 w.WriteHeader(StatusNotModified)
282 w.Header().Set("Last-Modified", modtime.UTC().Format(TimeFormat))
286 // checkETag implements If-None-Match and If-Range checks.
288 // The ETag or modtime must have been previously set in the
289 // ResponseWriter's headers. The modtime is only compared at second
290 // granularity and may be the zero value to mean unknown.
292 // The return value is the effective request "Range" header to use and
293 // whether this request is now considered done.
294 func checkETag(w ResponseWriter, r *Request, modtime time.Time) (rangeReq string, done bool) {
295 etag := w.Header().get("Etag")
296 rangeReq = r.Header.get("Range")
298 // Invalidate the range request if the entity doesn't match the one
299 // the client was expecting.
300 // "If-Range: version" means "ignore the Range: header unless version matches the
302 // We only support ETag versions.
303 // The caller must have set the ETag on the response already.
304 if ir := r.Header.get("If-Range"); ir != "" && ir != etag {
305 // The If-Range value is typically the ETag value, but it may also be
306 // the modtime date. See golang.org/issue/8367.
308 if !modtime.IsZero() {
309 if t, err := ParseTime(ir); err == nil && t.Unix() == modtime.Unix() {
318 if inm := r.Header.get("If-None-Match"); inm != "" {
321 return rangeReq, false
324 // TODO(bradfitz): non-GET/HEAD requests require more work:
325 // sending a different status code on matches, and
326 // also can't use weak cache validators (those with a "W/
327 // prefix). But most users of ServeContent will be using
328 // it on GET or HEAD, so only support those for now.
329 if r.Method != "GET" && r.Method != "HEAD" {
330 return rangeReq, false
333 // TODO(bradfitz): deal with comma-separated or multiple-valued
334 // list of If-None-match values. For now just handle the common
335 // case of a single item.
336 if inm == etag || inm == "*" {
338 delete(h, "Content-Type")
339 delete(h, "Content-Length")
340 w.WriteHeader(StatusNotModified)
344 return rangeReq, false
347 // name is '/'-separated, not filepath.Separator.
348 func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
349 const indexPage = "/index.html"
351 // redirect .../index.html to .../
352 // can't use Redirect() because that would make the path absolute,
353 // which would be a problem running under StripPrefix
354 if strings.HasSuffix(r.URL.Path, indexPage) {
355 localRedirect(w, r, "./")
359 f, err := fs.Open(name)
361 msg, code := toHTTPError(err)
369 msg, code := toHTTPError(err)
375 // redirect to canonical path: / at end of directory url
376 // r.URL.Path always begins with /
379 if url[len(url)-1] != '/' {
380 localRedirect(w, r, path.Base(url)+"/")
384 if url[len(url)-1] == '/' {
385 localRedirect(w, r, "../"+path.Base(url))
391 // use contents of index.html for directory, if present
393 index := strings.TrimSuffix(name, "/") + indexPage
394 ff, err := fs.Open(index)
406 // Still a directory? (we didn't find an index.html file)
408 if checkLastModified(w, r, d.ModTime()) {
415 // serveContent will check modification time
416 sizeFunc := func() (int64, error) { return d.Size(), nil }
417 serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
420 // toHTTPError returns a non-specific HTTP error message and status code
421 // for a given non-nil error value. It's important that toHTTPError does not
422 // actually return err.Error(), since msg and httpStatus are returned to users,
423 // and historically Go's ServeContent always returned just "404 Not Found" for
424 // all errors. We don't want to start leaking information in error messages.
425 func toHTTPError(err error) (msg string, httpStatus int) {
426 if os.IsNotExist(err) {
427 return "404 page not found", StatusNotFound
429 if os.IsPermission(err) {
430 return "403 Forbidden", StatusForbidden
433 return "500 Internal Server Error", StatusInternalServerError
436 // localRedirect gives a Moved Permanently response.
437 // It does not convert relative paths to absolute paths like Redirect does.
438 func localRedirect(w ResponseWriter, r *Request, newPath string) {
439 if q := r.URL.RawQuery; q != "" {
442 w.Header().Set("Location", newPath)
443 w.WriteHeader(StatusMovedPermanently)
446 // ServeFile replies to the request with the contents of the named file or directory.
447 func ServeFile(w ResponseWriter, r *Request, name string) {
448 dir, file := filepath.Split(name)
449 serveFile(w, r, Dir(dir), file, false)
452 type fileHandler struct {
456 // FileServer returns a handler that serves HTTP requests
457 // with the contents of the file system rooted at root.
459 // To use the operating system's file system implementation,
462 // http.Handle("/", http.FileServer(http.Dir("/tmp")))
463 func FileServer(root FileSystem) Handler {
464 return &fileHandler{root}
467 func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
469 if !strings.HasPrefix(upath, "/") {
473 serveFile(w, r, f.root, path.Clean(upath), true)
476 // httpRange specifies the byte range to be sent to the client.
477 type httpRange struct {
481 func (r httpRange) contentRange(size int64) string {
482 return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
485 func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
486 return textproto.MIMEHeader{
487 "Content-Range": {r.contentRange(size)},
488 "Content-Type": {contentType},
492 // parseRange parses a Range header string as per RFC 2616.
493 func parseRange(s string, size int64) ([]httpRange, error) {
495 return nil, nil // header not present
498 if !strings.HasPrefix(s, b) {
499 return nil, errors.New("invalid range")
501 var ranges []httpRange
502 for _, ra := range strings.Split(s[len(b):], ",") {
503 ra = strings.TrimSpace(ra)
507 i := strings.Index(ra, "-")
509 return nil, errors.New("invalid range")
511 start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
514 // If no start is specified, end specifies the
515 // range start relative to the end of the file.
516 i, err := strconv.ParseInt(end, 10, 64)
518 return nil, errors.New("invalid range")
524 r.length = size - r.start
526 i, err := strconv.ParseInt(start, 10, 64)
527 if err != nil || i >= size || i < 0 {
528 return nil, errors.New("invalid range")
532 // If no end is specified, range extends to end of the file.
533 r.length = size - r.start
535 i, err := strconv.ParseInt(end, 10, 64)
536 if err != nil || r.start > i {
537 return nil, errors.New("invalid range")
542 r.length = i - r.start + 1
545 ranges = append(ranges, r)
550 // countingWriter counts how many bytes have been written to it.
551 type countingWriter int64
553 func (w *countingWriter) Write(p []byte) (n int, err error) {
554 *w += countingWriter(len(p))
558 // rangesMIMESize returns the number of bytes it takes to encode the
559 // provided ranges as a multipart response.
560 func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
562 mw := multipart.NewWriter(&w)
563 for _, ra := range ranges {
564 mw.CreatePart(ra.mimeHeader(contentType, contentSize))
572 func sumRangesSize(ranges []httpRange) (size int64) {
573 for _, ra := range ranges {