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
22 // A Dir implements http.FileSystem using the native file
23 // system restricted to a specific directory tree.
26 func (d Dir) Open(name string) (File, os.Error) {
27 if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 {
28 return nil, os.NewError("http: invalid character in file path")
30 f, err := os.Open(filepath.Join(string(d), filepath.FromSlash(path.Clean("/"+name))))
37 // A FileSystem implements access to a collection of named files.
38 // The elements in a file path are separated by slash ('/', U+002F)
39 // characters, regardless of host operating system convention.
40 type FileSystem interface {
41 Open(name string) (File, os.Error)
44 // A File is returned by a FileSystem's Open method and can be
45 // served by the FileServer implementation.
48 Stat() (*os.FileInfo, os.Error)
49 Readdir(count int) ([]os.FileInfo, os.Error)
50 Read([]byte) (int, os.Error)
51 Seek(offset int64, whence int) (int64, os.Error)
54 // Heuristic: b is text if it is valid UTF-8 and doesn't
55 // contain any unprintable ASCII or Unicode characters.
56 func isText(b []byte) bool {
57 for len(b) > 0 && utf8.FullRune(b) {
58 rune, size := utf8.DecodeRune(b)
59 if size == 1 && rune == utf8.RuneError {
63 if 0x7F <= rune && rune <= 0x9F {
68 case '\n', '\r', '\t':
80 func dirList(w ResponseWriter, f File) {
81 w.Header().Set("Content-Type", "text/html; charset=utf-8")
82 fmt.Fprintf(w, "<pre>\n")
84 dirs, err := f.Readdir(100)
85 if err != nil || len(dirs) == 0 {
88 for _, d := range dirs {
94 fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", name, name)
97 fmt.Fprintf(w, "</pre>\n")
100 // name is '/'-separated, not filepath.Separator.
101 func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {
102 const indexPage = "/index.html"
104 // redirect .../index.html to .../
105 // can't use Redirect() because that would make the path absolute,
106 // which would be a problem running under StripPrefix
107 if strings.HasSuffix(r.URL.Path, indexPage) {
108 localRedirect(w, r, "./")
112 f, err := fs.Open(name)
114 // TODO expose actual error?
122 // TODO expose actual error?
128 // redirect to canonical path: / at end of directory url
129 // r.URL.Path always begins with /
132 if url[len(url)-1] != '/' {
133 localRedirect(w, r, path.Base(url)+"/")
137 if url[len(url)-1] == '/' {
138 localRedirect(w, r, "../"+path.Base(url))
144 if t, _ := time.Parse(TimeFormat, r.Header.Get("If-Modified-Since")); t != nil && d.Mtime_ns/1e9 <= t.Seconds() {
145 w.WriteHeader(StatusNotModified)
148 w.Header().Set("Last-Modified", time.SecondsToUTC(d.Mtime_ns/1e9).Format(TimeFormat))
150 // use contents of index.html for directory, if present
152 index := name + indexPage
153 ff, err := fs.Open(index)
174 // If Content-Type isn't set, use the file's extension to find it.
175 if w.Header().Get("Content-Type") == "" {
176 ctype := mime.TypeByExtension(filepath.Ext(name))
178 // read a chunk to decide between utf-8 text and binary
180 n, _ := io.ReadFull(f, buf[:])
183 ctype = "text/plain; charset=utf-8"
186 ctype = "application/octet-stream"
188 f.Seek(0, os.SEEK_SET) // rewind to output whole file
190 w.Header().Set("Content-Type", ctype)
193 // handle Content-Range header.
194 // TODO(adg): handle multiple ranges
195 ranges, err := parseRange(r.Header.Get("Range"), size)
196 if err == nil && len(ranges) > 1 {
197 err = os.NewError("multiple ranges not supported")
200 Error(w, err.String(), StatusRequestedRangeNotSatisfiable)
203 if len(ranges) == 1 {
205 if _, err := f.Seek(ra.start, os.SEEK_SET); err != nil {
206 Error(w, err.String(), StatusRequestedRangeNotSatisfiable)
210 code = StatusPartialContent
211 w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", ra.start, ra.start+ra.length-1, d.Size))
214 w.Header().Set("Accept-Ranges", "bytes")
215 if w.Header().Get("Content-Encoding") == "" {
216 w.Header().Set("Content-Length", strconv.Itoa64(size))
221 if r.Method != "HEAD" {
226 // localRedirect gives a Moved Permanently response.
227 // It does not convert relative paths to absolute paths like Redirect does.
228 func localRedirect(w ResponseWriter, r *Request, newPath string) {
229 if q := r.URL.RawQuery; q != "" {
232 w.Header().Set("Location", newPath)
233 w.WriteHeader(StatusMovedPermanently)
236 // ServeFile replies to the request with the contents of the named file or directory.
237 func ServeFile(w ResponseWriter, r *Request, name string) {
238 dir, file := filepath.Split(name)
239 serveFile(w, r, Dir(dir), file, false)
242 type fileHandler struct {
246 // FileServer returns a handler that serves HTTP requests
247 // with the contents of the file system rooted at root.
249 // To use the operating system's file system implementation,
252 // http.Handle("/", http.FileServer(http.Dir("/tmp")))
253 func FileServer(root FileSystem) Handler {
254 return &fileHandler{root}
257 func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
259 if !strings.HasPrefix(upath, "/") {
263 serveFile(w, r, f.root, path.Clean(upath), true)
266 // httpRange specifies the byte range to be sent to the client.
267 type httpRange struct {
271 // parseRange parses a Range header string as per RFC 2616.
272 func parseRange(s string, size int64) ([]httpRange, os.Error) {
274 return nil, nil // header not present
277 if !strings.HasPrefix(s, b) {
278 return nil, os.NewError("invalid range")
280 var ranges []httpRange
281 for _, ra := range strings.Split(s[len(b):], ",") {
282 i := strings.Index(ra, "-")
284 return nil, os.NewError("invalid range")
286 start, end := ra[:i], ra[i+1:]
289 // If no start is specified, end specifies the
290 // range start relative to the end of the file.
291 i, err := strconv.Atoi64(end)
293 return nil, os.NewError("invalid range")
299 r.length = size - r.start
301 i, err := strconv.Atoi64(start)
302 if err != nil || i > size || i < 0 {
303 return nil, os.NewError("invalid range")
307 // If no end is specified, range extends to end of the file.
308 r.length = size - r.start
310 i, err := strconv.Atoi64(end)
311 if err != nil || r.start > i {
312 return nil, os.NewError("invalid range")
317 r.length = i - r.start + 1
320 ranges = append(ranges, r)