github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/net/http/fs.go (about)

     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  	"strconv"
    21  	"strings"
    22  	"time"
    23  )
    24  
    25  // A Dir implements FileSystem using the native file system restricted to a
    26  // specific directory tree.
    27  //
    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 '/'.
    31  //
    32  // An empty Dir is treated as ".".
    33  type Dir string
    34  
    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")
    39  	}
    40  	dir := string(d)
    41  	if dir == "" {
    42  		dir = "."
    43  	}
    44  	f, err := os.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return f, nil
    49  }
    50  
    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)
    56  }
    57  
    58  // A File is returned by a FileSystem's Open method and can be
    59  // served by the FileServer implementation.
    60  //
    61  // The methods should behave the same as those on an *os.File.
    62  type File interface {
    63  	io.Closer
    64  	io.Reader
    65  	Readdir(count int) ([]os.FileInfo, error)
    66  	Seek(offset int64, whence int) (int64, error)
    67  	Stat() (os.FileInfo, error)
    68  }
    69  
    70  func dirList(w ResponseWriter, f File) {
    71  	w.Header().Set("Content-Type", "text/html; charset=utf-8")
    72  	fmt.Fprintf(w, "<pre>\n")
    73  	for {
    74  		dirs, err := f.Readdir(100)
    75  		if err != nil || len(dirs) == 0 {
    76  			break
    77  		}
    78  		for _, d := range dirs {
    79  			name := d.Name()
    80  			if d.IsDir() {
    81  				name += "/"
    82  			}
    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))
    88  		}
    89  	}
    90  	fmt.Fprintf(w, "</pre>\n")
    91  }
    92  
    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.
    97  //
    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.
   104  //
   105  // If modtime is not the zero time, ServeContent includes it in a
   106  // Last-Modified header in the response.  If the request includes an
   107  // If-Modified-Since header, ServeContent uses modtime to decide
   108  // whether the content needs to be sent at all.
   109  //
   110  // The content's Seek method must work: ServeContent uses
   111  // a seek to the end of the content to determine its size.
   112  //
   113  // If the caller has set w's ETag header, ServeContent uses it to
   114  // handle requests using If-Range and If-None-Match.
   115  //
   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)
   120  		if err != nil {
   121  			return 0, errSeeker
   122  		}
   123  		_, err = content.Seek(0, os.SEEK_SET)
   124  		if err != nil {
   125  			return 0, errSeeker
   126  		}
   127  		return size, nil
   128  	}
   129  	serveContent(w, req, name, modtime, sizeFunc, content)
   130  }
   131  
   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
   135  // users.
   136  var errSeeker = errors.New("seeker can't seek")
   137  
   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) {
   144  		return
   145  	}
   146  	rangeReq, done := checkETag(w, r, modtime)
   147  	if done {
   148  		return
   149  	}
   150  
   151  	code := StatusOK
   152  
   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"]
   156  	var ctype string
   157  	if !haveType {
   158  		ctype = mime.TypeByExtension(filepath.Ext(name))
   159  		if ctype == "" {
   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
   165  			if err != nil {
   166  				Error(w, "seeker can't seek", StatusInternalServerError)
   167  				return
   168  			}
   169  		}
   170  		w.Header().Set("Content-Type", ctype)
   171  	} else if len(ctypes) > 0 {
   172  		ctype = ctypes[0]
   173  	}
   174  
   175  	size, err := sizeFunc()
   176  	if err != nil {
   177  		Error(w, err.Error(), StatusInternalServerError)
   178  		return
   179  	}
   180  
   181  	// handle Content-Range header.
   182  	sendSize := size
   183  	var sendContent io.Reader = content
   184  	if size >= 0 {
   185  		ranges, err := parseRange(rangeReq, size)
   186  		if err != nil {
   187  			Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
   188  			return
   189  		}
   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.
   195  			ranges = nil
   196  		}
   197  		switch {
   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.
   207  			// ...
   208  			// A response to a request for a single range MUST NOT
   209  			// be sent using the multipart/byteranges media type."
   210  			ra := ranges[0]
   211  			if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil {
   212  				Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
   213  				return
   214  			}
   215  			sendSize = ra.length
   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
   221  
   222  			pr, pw := io.Pipe()
   223  			mw := multipart.NewWriter(pw)
   224  			w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
   225  			sendContent = pr
   226  			defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
   227  			go func() {
   228  				for _, ra := range ranges {
   229  					part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
   230  					if err != nil {
   231  						pw.CloseWithError(err)
   232  						return
   233  					}
   234  					if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil {
   235  						pw.CloseWithError(err)
   236  						return
   237  					}
   238  					if _, err := io.CopyN(part, content, ra.length); err != nil {
   239  						pw.CloseWithError(err)
   240  						return
   241  					}
   242  				}
   243  				mw.Close()
   244  				pw.Close()
   245  			}()
   246  		}
   247  
   248  		w.Header().Set("Accept-Ranges", "bytes")
   249  		if w.Header().Get("Content-Encoding") == "" {
   250  			w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
   251  		}
   252  	}
   253  
   254  	w.WriteHeader(code)
   255  
   256  	if r.Method != "HEAD" {
   257  		io.CopyN(w, sendContent, sendSize)
   258  	}
   259  }
   260  
   261  // modtime is the modification time of the resource to be served, or IsZero().
   262  // return value is whether this request is now complete.
   263  func checkLastModified(w ResponseWriter, r *Request, modtime time.Time) bool {
   264  	if modtime.IsZero() {
   265  		return false
   266  	}
   267  
   268  	// The Date-Modified header truncates sub-second precision, so
   269  	// use mtime < t+1s instead of mtime <= t to check for unmodified.
   270  	if t, err := time.Parse(TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
   271  		h := w.Header()
   272  		delete(h, "Content-Type")
   273  		delete(h, "Content-Length")
   274  		w.WriteHeader(StatusNotModified)
   275  		return true
   276  	}
   277  	w.Header().Set("Last-Modified", modtime.UTC().Format(TimeFormat))
   278  	return false
   279  }
   280  
   281  // checkETag implements If-None-Match and If-Range checks.
   282  //
   283  // The ETag or modtime must have been previously set in the
   284  // ResponseWriter's headers.  The modtime is only compared at second
   285  // granularity and may be the zero value to mean unknown.
   286  //
   287  // The return value is the effective request "Range" header to use and
   288  // whether this request is now considered done.
   289  func checkETag(w ResponseWriter, r *Request, modtime time.Time) (rangeReq string, done bool) {
   290  	etag := w.Header().get("Etag")
   291  	rangeReq = r.Header.get("Range")
   292  
   293  	// Invalidate the range request if the entity doesn't match the one
   294  	// the client was expecting.
   295  	// "If-Range: version" means "ignore the Range: header unless version matches the
   296  	// current file."
   297  	// We only support ETag versions.
   298  	// The caller must have set the ETag on the response already.
   299  	if ir := r.Header.get("If-Range"); ir != "" && ir != etag {
   300  		// The If-Range value is typically the ETag value, but it may also be
   301  		// the modtime date. See golang.org/issue/8367.
   302  		timeMatches := false
   303  		if !modtime.IsZero() {
   304  			if t, err := ParseTime(ir); err == nil && t.Unix() == modtime.Unix() {
   305  				timeMatches = true
   306  			}
   307  		}
   308  		if !timeMatches {
   309  			rangeReq = ""
   310  		}
   311  	}
   312  
   313  	if inm := r.Header.get("If-None-Match"); inm != "" {
   314  		// Must know ETag.
   315  		if etag == "" {
   316  			return rangeReq, false
   317  		}
   318  
   319  		// TODO(bradfitz): non-GET/HEAD requests require more work:
   320  		// sending a different status code on matches, and
   321  		// also can't use weak cache validators (those with a "W/
   322  		// prefix).  But most users of ServeContent will be using
   323  		// it on GET or HEAD, so only support those for now.
   324  		if r.Method != "GET" && r.Method != "HEAD" {
   325  			return rangeReq, false
   326  		}
   327  
   328  		// TODO(bradfitz): deal with comma-separated or multiple-valued
   329  		// list of If-None-match values.  For now just handle the common
   330  		// case of a single item.
   331  		if inm == etag || inm == "*" {
   332  			h := w.Header()
   333  			delete(h, "Content-Type")
   334  			delete(h, "Content-Length")
   335  			w.WriteHeader(StatusNotModified)
   336  			return "", true
   337  		}
   338  	}
   339  	return rangeReq, false
   340  }
   341  
   342  // name is '/'-separated, not filepath.Separator.
   343  func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
   344  	const indexPage = "/index.html"
   345  
   346  	// redirect .../index.html to .../
   347  	// can't use Redirect() because that would make the path absolute,
   348  	// which would be a problem running under StripPrefix
   349  	if strings.HasSuffix(r.URL.Path, indexPage) {
   350  		localRedirect(w, r, "./")
   351  		return
   352  	}
   353  
   354  	f, err := fs.Open(name)
   355  	if err != nil {
   356  		// TODO expose actual error?
   357  		NotFound(w, r)
   358  		return
   359  	}
   360  	defer f.Close()
   361  
   362  	d, err1 := f.Stat()
   363  	if err1 != nil {
   364  		// TODO expose actual error?
   365  		NotFound(w, r)
   366  		return
   367  	}
   368  
   369  	if redirect {
   370  		// redirect to canonical path: / at end of directory url
   371  		// r.URL.Path always begins with /
   372  		url := r.URL.Path
   373  		if d.IsDir() {
   374  			if url[len(url)-1] != '/' {
   375  				localRedirect(w, r, path.Base(url)+"/")
   376  				return
   377  			}
   378  		} else {
   379  			if url[len(url)-1] == '/' {
   380  				localRedirect(w, r, "../"+path.Base(url))
   381  				return
   382  			}
   383  		}
   384  	}
   385  
   386  	// use contents of index.html for directory, if present
   387  	if d.IsDir() {
   388  		index := strings.TrimSuffix(name, "/") + indexPage
   389  		ff, err := fs.Open(index)
   390  		if err == nil {
   391  			defer ff.Close()
   392  			dd, err := ff.Stat()
   393  			if err == nil {
   394  				name = index
   395  				d = dd
   396  				f = ff
   397  			}
   398  		}
   399  	}
   400  
   401  	// Still a directory? (we didn't find an index.html file)
   402  	if d.IsDir() {
   403  		if checkLastModified(w, r, d.ModTime()) {
   404  			return
   405  		}
   406  		dirList(w, f)
   407  		return
   408  	}
   409  
   410  	// serveContent will check modification time
   411  	sizeFunc := func() (int64, error) { return d.Size(), nil }
   412  	serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
   413  }
   414  
   415  // localRedirect gives a Moved Permanently response.
   416  // It does not convert relative paths to absolute paths like Redirect does.
   417  func localRedirect(w ResponseWriter, r *Request, newPath string) {
   418  	if q := r.URL.RawQuery; q != "" {
   419  		newPath += "?" + q
   420  	}
   421  	w.Header().Set("Location", newPath)
   422  	w.WriteHeader(StatusMovedPermanently)
   423  }
   424  
   425  // ServeFile replies to the request with the contents of the named file or directory.
   426  func ServeFile(w ResponseWriter, r *Request, name string) {
   427  	dir, file := filepath.Split(name)
   428  	serveFile(w, r, Dir(dir), file, false)
   429  }
   430  
   431  type fileHandler struct {
   432  	root FileSystem
   433  }
   434  
   435  // FileServer returns a handler that serves HTTP requests
   436  // with the contents of the file system rooted at root.
   437  //
   438  // To use the operating system's file system implementation,
   439  // use http.Dir:
   440  //
   441  //     http.Handle("/", http.FileServer(http.Dir("/tmp")))
   442  func FileServer(root FileSystem) Handler {
   443  	return &fileHandler{root}
   444  }
   445  
   446  func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
   447  	upath := r.URL.Path
   448  	if !strings.HasPrefix(upath, "/") {
   449  		upath = "/" + upath
   450  		r.URL.Path = upath
   451  	}
   452  	serveFile(w, r, f.root, path.Clean(upath), true)
   453  }
   454  
   455  // httpRange specifies the byte range to be sent to the client.
   456  type httpRange struct {
   457  	start, length int64
   458  }
   459  
   460  func (r httpRange) contentRange(size int64) string {
   461  	return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
   462  }
   463  
   464  func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
   465  	return textproto.MIMEHeader{
   466  		"Content-Range": {r.contentRange(size)},
   467  		"Content-Type":  {contentType},
   468  	}
   469  }
   470  
   471  // parseRange parses a Range header string as per RFC 2616.
   472  func parseRange(s string, size int64) ([]httpRange, error) {
   473  	if s == "" {
   474  		return nil, nil // header not present
   475  	}
   476  	const b = "bytes="
   477  	if !strings.HasPrefix(s, b) {
   478  		return nil, errors.New("invalid range")
   479  	}
   480  	var ranges []httpRange
   481  	for _, ra := range strings.Split(s[len(b):], ",") {
   482  		ra = strings.TrimSpace(ra)
   483  		if ra == "" {
   484  			continue
   485  		}
   486  		i := strings.Index(ra, "-")
   487  		if i < 0 {
   488  			return nil, errors.New("invalid range")
   489  		}
   490  		start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
   491  		var r httpRange
   492  		if start == "" {
   493  			// If no start is specified, end specifies the
   494  			// range start relative to the end of the file.
   495  			i, err := strconv.ParseInt(end, 10, 64)
   496  			if err != nil {
   497  				return nil, errors.New("invalid range")
   498  			}
   499  			if i > size {
   500  				i = size
   501  			}
   502  			r.start = size - i
   503  			r.length = size - r.start
   504  		} else {
   505  			i, err := strconv.ParseInt(start, 10, 64)
   506  			if err != nil || i >= size || i < 0 {
   507  				return nil, errors.New("invalid range")
   508  			}
   509  			r.start = i
   510  			if end == "" {
   511  				// If no end is specified, range extends to end of the file.
   512  				r.length = size - r.start
   513  			} else {
   514  				i, err := strconv.ParseInt(end, 10, 64)
   515  				if err != nil || r.start > i {
   516  					return nil, errors.New("invalid range")
   517  				}
   518  				if i >= size {
   519  					i = size - 1
   520  				}
   521  				r.length = i - r.start + 1
   522  			}
   523  		}
   524  		ranges = append(ranges, r)
   525  	}
   526  	return ranges, nil
   527  }
   528  
   529  // countingWriter counts how many bytes have been written to it.
   530  type countingWriter int64
   531  
   532  func (w *countingWriter) Write(p []byte) (n int, err error) {
   533  	*w += countingWriter(len(p))
   534  	return len(p), nil
   535  }
   536  
   537  // rangesMIMESize returns the number of bytes it takes to encode the
   538  // provided ranges as a multipart response.
   539  func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
   540  	var w countingWriter
   541  	mw := multipart.NewWriter(&w)
   542  	for _, ra := range ranges {
   543  		mw.CreatePart(ra.mimeHeader(contentType, contentSize))
   544  		encSize += ra.length
   545  	}
   546  	mw.Close()
   547  	encSize += int64(w)
   548  	return
   549  }
   550  
   551  func sumRangesSize(ranges []httpRange) (size int64) {
   552  	for _, ra := range ranges {
   553  		size += ra.length
   554  	}
   555  	return
   556  }