github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/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  	"os"
    17  	"path"
    18  	"path/filepath"
    19  	"strconv"
    20  	"strings"
    21  	"time"
    22  )
    23  
    24  // A Dir implements http.FileSystem using the native file
    25  // system restricted to a specific directory tree.
    26  //
    27  // An empty Dir is treated as ".".
    28  type Dir string
    29  
    30  func (d Dir) Open(name string) (File, error) {
    31  	if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 ||
    32  		strings.Contains(name, "\x00") {
    33  		return nil, errors.New("http: invalid character in file path")
    34  	}
    35  	dir := string(d)
    36  	if dir == "" {
    37  		dir = "."
    38  	}
    39  	f, err := os.Open(filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name))))
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return f, nil
    44  }
    45  
    46  // A FileSystem implements access to a collection of named files.
    47  // The elements in a file path are separated by slash ('/', U+002F)
    48  // characters, regardless of host operating system convention.
    49  type FileSystem interface {
    50  	Open(name string) (File, error)
    51  }
    52  
    53  // A File is returned by a FileSystem's Open method and can be
    54  // served by the FileServer implementation.
    55  type File interface {
    56  	Close() error
    57  	Stat() (os.FileInfo, error)
    58  	Readdir(count int) ([]os.FileInfo, error)
    59  	Read([]byte) (int, error)
    60  	Seek(offset int64, whence int) (int64, error)
    61  }
    62  
    63  func dirList(w ResponseWriter, f File) {
    64  	w.Header().Set("Content-Type", "text/html; charset=utf-8")
    65  	fmt.Fprintf(w, "<pre>\n")
    66  	for {
    67  		dirs, err := f.Readdir(100)
    68  		if err != nil || len(dirs) == 0 {
    69  			break
    70  		}
    71  		for _, d := range dirs {
    72  			name := d.Name()
    73  			if d.IsDir() {
    74  				name += "/"
    75  			}
    76  			// TODO htmlescape
    77  			fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", name, name)
    78  		}
    79  	}
    80  	fmt.Fprintf(w, "</pre>\n")
    81  }
    82  
    83  // ServeContent replies to the request using the content in the
    84  // provided ReadSeeker.  The main benefit of ServeContent over io.Copy
    85  // is that it handles Range requests properly, sets the MIME type, and
    86  // handles If-Modified-Since requests.
    87  //
    88  // If the response's Content-Type header is not set, ServeContent
    89  // first tries to deduce the type from name's file extension and,
    90  // if that fails, falls back to reading the first block of the content
    91  // and passing it to DetectContentType.
    92  // The name is otherwise unused; in particular it can be empty and is
    93  // never sent in the response.
    94  //
    95  // If modtime is not the zero time, ServeContent includes it in a
    96  // Last-Modified header in the response.  If the request includes an
    97  // If-Modified-Since header, ServeContent uses modtime to decide
    98  // whether the content needs to be sent at all.
    99  //
   100  // The content's Seek method must work: ServeContent uses
   101  // a seek to the end of the content to determine its size.
   102  //
   103  // If the caller has set w's ETag header, ServeContent uses it to
   104  // handle requests using If-Range and If-None-Match.
   105  //
   106  // Note that *os.File implements the io.ReadSeeker interface.
   107  func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker) {
   108  	sizeFunc := func() (int64, error) {
   109  		size, err := content.Seek(0, os.SEEK_END)
   110  		if err != nil {
   111  			return 0, errSeeker
   112  		}
   113  		_, err = content.Seek(0, os.SEEK_SET)
   114  		if err != nil {
   115  			return 0, errSeeker
   116  		}
   117  		return size, nil
   118  	}
   119  	serveContent(w, req, name, modtime, sizeFunc, content)
   120  }
   121  
   122  // errSeeker is returned by ServeContent's sizeFunc when the content
   123  // doesn't seek properly. The underlying Seeker's error text isn't
   124  // included in the sizeFunc reply so it's not sent over HTTP to end
   125  // users.
   126  var errSeeker = errors.New("seeker can't seek")
   127  
   128  // if name is empty, filename is unknown. (used for mime type, before sniffing)
   129  // if modtime.IsZero(), modtime is unknown.
   130  // content must be seeked to the beginning of the file.
   131  // The sizeFunc is called at most once. Its error, if any, is sent in the HTTP response.
   132  func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, sizeFunc func() (int64, error), content io.ReadSeeker) {
   133  	if checkLastModified(w, r, modtime) {
   134  		return
   135  	}
   136  	rangeReq, done := checkETag(w, r)
   137  	if done {
   138  		return
   139  	}
   140  
   141  	code := StatusOK
   142  
   143  	// If Content-Type isn't set, use the file's extension to find it, but
   144  	// if the Content-Type is unset explicitly, do not sniff the type.
   145  	ctypes, haveType := w.Header()["Content-Type"]
   146  	var ctype string
   147  	if !haveType {
   148  		ctype = mime.TypeByExtension(filepath.Ext(name))
   149  		if ctype == "" {
   150  			// read a chunk to decide between utf-8 text and binary
   151  			var buf [sniffLen]byte
   152  			n, _ := io.ReadFull(content, buf[:])
   153  			ctype = DetectContentType(buf[:n])
   154  			_, err := content.Seek(0, os.SEEK_SET) // rewind to output whole file
   155  			if err != nil {
   156  				Error(w, "seeker can't seek", StatusInternalServerError)
   157  				return
   158  			}
   159  		}
   160  		w.Header().Set("Content-Type", ctype)
   161  	} else if len(ctypes) > 0 {
   162  		ctype = ctypes[0]
   163  	}
   164  
   165  	size, err := sizeFunc()
   166  	if err != nil {
   167  		Error(w, err.Error(), StatusInternalServerError)
   168  		return
   169  	}
   170  
   171  	// handle Content-Range header.
   172  	sendSize := size
   173  	var sendContent io.Reader = content
   174  	if size >= 0 {
   175  		ranges, err := parseRange(rangeReq, size)
   176  		if err != nil {
   177  			Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
   178  			return
   179  		}
   180  		if sumRangesSize(ranges) > size {
   181  			// The total number of bytes in all the ranges
   182  			// is larger than the size of the file by
   183  			// itself, so this is probably an attack, or a
   184  			// dumb client.  Ignore the range request.
   185  			ranges = nil
   186  		}
   187  		switch {
   188  		case len(ranges) == 1:
   189  			// RFC 2616, Section 14.16:
   190  			// "When an HTTP message includes the content of a single
   191  			// range (for example, a response to a request for a
   192  			// single range, or to a request for a set of ranges
   193  			// that overlap without any holes), this content is
   194  			// transmitted with a Content-Range header, and a
   195  			// Content-Length header showing the number of bytes
   196  			// actually transferred.
   197  			// ...
   198  			// A response to a request for a single range MUST NOT
   199  			// be sent using the multipart/byteranges media type."
   200  			ra := ranges[0]
   201  			if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil {
   202  				Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
   203  				return
   204  			}
   205  			sendSize = ra.length
   206  			code = StatusPartialContent
   207  			w.Header().Set("Content-Range", ra.contentRange(size))
   208  		case len(ranges) > 1:
   209  			for _, ra := range ranges {
   210  				if ra.start > size {
   211  					Error(w, err.Error(), StatusRequestedRangeNotSatisfiable)
   212  					return
   213  				}
   214  			}
   215  			sendSize = rangesMIMESize(ranges, ctype, size)
   216  			code = StatusPartialContent
   217  
   218  			pr, pw := io.Pipe()
   219  			mw := multipart.NewWriter(pw)
   220  			w.Header().Set("Content-Type", "multipart/byteranges; boundary="+mw.Boundary())
   221  			sendContent = pr
   222  			defer pr.Close() // cause writing goroutine to fail and exit if CopyN doesn't finish.
   223  			go func() {
   224  				for _, ra := range ranges {
   225  					part, err := mw.CreatePart(ra.mimeHeader(ctype, size))
   226  					if err != nil {
   227  						pw.CloseWithError(err)
   228  						return
   229  					}
   230  					if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil {
   231  						pw.CloseWithError(err)
   232  						return
   233  					}
   234  					if _, err := io.CopyN(part, content, ra.length); err != nil {
   235  						pw.CloseWithError(err)
   236  						return
   237  					}
   238  				}
   239  				mw.Close()
   240  				pw.Close()
   241  			}()
   242  		}
   243  
   244  		w.Header().Set("Accept-Ranges", "bytes")
   245  		if w.Header().Get("Content-Encoding") == "" {
   246  			w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
   247  		}
   248  	}
   249  
   250  	w.WriteHeader(code)
   251  
   252  	if r.Method != "HEAD" {
   253  		io.CopyN(w, sendContent, sendSize)
   254  	}
   255  }
   256  
   257  // modtime is the modification time of the resource to be served, or IsZero().
   258  // return value is whether this request is now complete.
   259  func checkLastModified(w ResponseWriter, r *Request, modtime time.Time) bool {
   260  	if modtime.IsZero() {
   261  		return false
   262  	}
   263  
   264  	// The Date-Modified header truncates sub-second precision, so
   265  	// use mtime < t+1s instead of mtime <= t to check for unmodified.
   266  	if t, err := time.Parse(TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
   267  		h := w.Header()
   268  		delete(h, "Content-Type")
   269  		delete(h, "Content-Length")
   270  		w.WriteHeader(StatusNotModified)
   271  		return true
   272  	}
   273  	w.Header().Set("Last-Modified", modtime.UTC().Format(TimeFormat))
   274  	return false
   275  }
   276  
   277  // checkETag implements If-None-Match and If-Range checks.
   278  // The ETag must have been previously set in the ResponseWriter's headers.
   279  //
   280  // The return value is the effective request "Range" header to use and
   281  // whether this request is now considered done.
   282  func checkETag(w ResponseWriter, r *Request) (rangeReq string, done bool) {
   283  	etag := w.Header().get("Etag")
   284  	rangeReq = r.Header.get("Range")
   285  
   286  	// Invalidate the range request if the entity doesn't match the one
   287  	// the client was expecting.
   288  	// "If-Range: version" means "ignore the Range: header unless version matches the
   289  	// current file."
   290  	// We only support ETag versions.
   291  	// The caller must have set the ETag on the response already.
   292  	if ir := r.Header.get("If-Range"); ir != "" && ir != etag {
   293  		// TODO(bradfitz): handle If-Range requests with Last-Modified
   294  		// times instead of ETags? I'd rather not, at least for
   295  		// now. That seems like a bug/compromise in the RFC 2616, and
   296  		// I've never heard of anybody caring about that (yet).
   297  		rangeReq = ""
   298  	}
   299  
   300  	if inm := r.Header.get("If-None-Match"); inm != "" {
   301  		// Must know ETag.
   302  		if etag == "" {
   303  			return rangeReq, false
   304  		}
   305  
   306  		// TODO(bradfitz): non-GET/HEAD requests require more work:
   307  		// sending a different status code on matches, and
   308  		// also can't use weak cache validators (those with a "W/
   309  		// prefix).  But most users of ServeContent will be using
   310  		// it on GET or HEAD, so only support those for now.
   311  		if r.Method != "GET" && r.Method != "HEAD" {
   312  			return rangeReq, false
   313  		}
   314  
   315  		// TODO(bradfitz): deal with comma-separated or multiple-valued
   316  		// list of If-None-match values.  For now just handle the common
   317  		// case of a single item.
   318  		if inm == etag || inm == "*" {
   319  			h := w.Header()
   320  			delete(h, "Content-Type")
   321  			delete(h, "Content-Length")
   322  			w.WriteHeader(StatusNotModified)
   323  			return "", true
   324  		}
   325  	}
   326  	return rangeReq, false
   327  }
   328  
   329  // name is '/'-separated, not filepath.Separator.
   330  func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
   331  	const indexPage = "/index.html"
   332  
   333  	// redirect .../index.html to .../
   334  	// can't use Redirect() because that would make the path absolute,
   335  	// which would be a problem running under StripPrefix
   336  	if strings.HasSuffix(r.URL.Path, indexPage) {
   337  		localRedirect(w, r, "./")
   338  		return
   339  	}
   340  
   341  	f, err := fs.Open(name)
   342  	if err != nil {
   343  		// TODO expose actual error?
   344  		NotFound(w, r)
   345  		return
   346  	}
   347  	defer f.Close()
   348  
   349  	d, err1 := f.Stat()
   350  	if err1 != nil {
   351  		// TODO expose actual error?
   352  		NotFound(w, r)
   353  		return
   354  	}
   355  
   356  	if redirect {
   357  		// redirect to canonical path: / at end of directory url
   358  		// r.URL.Path always begins with /
   359  		url := r.URL.Path
   360  		if d.IsDir() {
   361  			if url[len(url)-1] != '/' {
   362  				localRedirect(w, r, path.Base(url)+"/")
   363  				return
   364  			}
   365  		} else {
   366  			if url[len(url)-1] == '/' {
   367  				localRedirect(w, r, "../"+path.Base(url))
   368  				return
   369  			}
   370  		}
   371  	}
   372  
   373  	// use contents of index.html for directory, if present
   374  	if d.IsDir() {
   375  		index := name + indexPage
   376  		ff, err := fs.Open(index)
   377  		if err == nil {
   378  			defer ff.Close()
   379  			dd, err := ff.Stat()
   380  			if err == nil {
   381  				name = index
   382  				d = dd
   383  				f = ff
   384  			}
   385  		}
   386  	}
   387  
   388  	// Still a directory? (we didn't find an index.html file)
   389  	if d.IsDir() {
   390  		if checkLastModified(w, r, d.ModTime()) {
   391  			return
   392  		}
   393  		dirList(w, f)
   394  		return
   395  	}
   396  
   397  	// serverContent will check modification time
   398  	sizeFunc := func() (int64, error) { return d.Size(), nil }
   399  	serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
   400  }
   401  
   402  // localRedirect gives a Moved Permanently response.
   403  // It does not convert relative paths to absolute paths like Redirect does.
   404  func localRedirect(w ResponseWriter, r *Request, newPath string) {
   405  	if q := r.URL.RawQuery; q != "" {
   406  		newPath += "?" + q
   407  	}
   408  	w.Header().Set("Location", newPath)
   409  	w.WriteHeader(StatusMovedPermanently)
   410  }
   411  
   412  // ServeFile replies to the request with the contents of the named file or directory.
   413  func ServeFile(w ResponseWriter, r *Request, name string) {
   414  	dir, file := filepath.Split(name)
   415  	serveFile(w, r, Dir(dir), file, false)
   416  }
   417  
   418  type fileHandler struct {
   419  	root FileSystem
   420  }
   421  
   422  // FileServer returns a handler that serves HTTP requests
   423  // with the contents of the file system rooted at root.
   424  //
   425  // To use the operating system's file system implementation,
   426  // use http.Dir:
   427  //
   428  //     http.Handle("/", http.FileServer(http.Dir("/tmp")))
   429  func FileServer(root FileSystem) Handler {
   430  	return &fileHandler{root}
   431  }
   432  
   433  func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
   434  	upath := r.URL.Path
   435  	if !strings.HasPrefix(upath, "/") {
   436  		upath = "/" + upath
   437  		r.URL.Path = upath
   438  	}
   439  	serveFile(w, r, f.root, path.Clean(upath), true)
   440  }
   441  
   442  // httpRange specifies the byte range to be sent to the client.
   443  type httpRange struct {
   444  	start, length int64
   445  }
   446  
   447  func (r httpRange) contentRange(size int64) string {
   448  	return fmt.Sprintf("bytes %d-%d/%d", r.start, r.start+r.length-1, size)
   449  }
   450  
   451  func (r httpRange) mimeHeader(contentType string, size int64) textproto.MIMEHeader {
   452  	return textproto.MIMEHeader{
   453  		"Content-Range": {r.contentRange(size)},
   454  		"Content-Type":  {contentType},
   455  	}
   456  }
   457  
   458  // parseRange parses a Range header string as per RFC 2616.
   459  func parseRange(s string, size int64) ([]httpRange, error) {
   460  	if s == "" {
   461  		return nil, nil // header not present
   462  	}
   463  	const b = "bytes="
   464  	if !strings.HasPrefix(s, b) {
   465  		return nil, errors.New("invalid range")
   466  	}
   467  	var ranges []httpRange
   468  	for _, ra := range strings.Split(s[len(b):], ",") {
   469  		ra = strings.TrimSpace(ra)
   470  		if ra == "" {
   471  			continue
   472  		}
   473  		i := strings.Index(ra, "-")
   474  		if i < 0 {
   475  			return nil, errors.New("invalid range")
   476  		}
   477  		start, end := strings.TrimSpace(ra[:i]), strings.TrimSpace(ra[i+1:])
   478  		var r httpRange
   479  		if start == "" {
   480  			// If no start is specified, end specifies the
   481  			// range start relative to the end of the file.
   482  			i, err := strconv.ParseInt(end, 10, 64)
   483  			if err != nil {
   484  				return nil, errors.New("invalid range")
   485  			}
   486  			if i > size {
   487  				i = size
   488  			}
   489  			r.start = size - i
   490  			r.length = size - r.start
   491  		} else {
   492  			i, err := strconv.ParseInt(start, 10, 64)
   493  			if err != nil || i > size || i < 0 {
   494  				return nil, errors.New("invalid range")
   495  			}
   496  			r.start = i
   497  			if end == "" {
   498  				// If no end is specified, range extends to end of the file.
   499  				r.length = size - r.start
   500  			} else {
   501  				i, err := strconv.ParseInt(end, 10, 64)
   502  				if err != nil || r.start > i {
   503  					return nil, errors.New("invalid range")
   504  				}
   505  				if i >= size {
   506  					i = size - 1
   507  				}
   508  				r.length = i - r.start + 1
   509  			}
   510  		}
   511  		ranges = append(ranges, r)
   512  	}
   513  	return ranges, nil
   514  }
   515  
   516  // countingWriter counts how many bytes have been written to it.
   517  type countingWriter int64
   518  
   519  func (w *countingWriter) Write(p []byte) (n int, err error) {
   520  	*w += countingWriter(len(p))
   521  	return len(p), nil
   522  }
   523  
   524  // rangesMIMESize returns the nunber of bytes it takes to encode the
   525  // provided ranges as a multipart response.
   526  func rangesMIMESize(ranges []httpRange, contentType string, contentSize int64) (encSize int64) {
   527  	var w countingWriter
   528  	mw := multipart.NewWriter(&w)
   529  	for _, ra := range ranges {
   530  		mw.CreatePart(ra.mimeHeader(contentType, contentSize))
   531  		encSize += ra.length
   532  	}
   533  	mw.Close()
   534  	encSize += int64(w)
   535  	return
   536  }
   537  
   538  func sumRangesSize(ranges []httpRange) (size int64) {
   539  	for _, ra := range ranges {
   540  		size += ra.length
   541  	}
   542  	return
   543  }