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
21 // Heuristic: b is text if it is valid UTF-8 and doesn't
22 // contain any unprintable ASCII or Unicode characters.
23 func isText(b []byte) bool {
24 for len(b) > 0 && utf8.FullRune(b) {
25 rune, size := utf8.DecodeRune(b)
26 if size == 1 && rune == utf8.RuneError {
30 if 0x7F <= rune && rune <= 0x9F {
35 case '\n', '\r', '\t':
47 func dirList(w ResponseWriter, f *os.File) {
48 fmt.Fprintf(w, "<pre>\n")
50 dirs, err := f.Readdir(100)
51 if err != nil || len(dirs) == 0 {
54 for _, d := range dirs {
60 fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", name, name)
63 fmt.Fprintf(w, "</pre>\n")
66 func serveFile(w ResponseWriter, r *Request, name string, redirect bool) {
67 const indexPage = "/index.html"
69 // redirect .../index.html to .../
70 if strings.HasSuffix(r.URL.Path, indexPage) {
71 Redirect(w, r, r.URL.Path[0:len(r.URL.Path)-len(indexPage)+1], StatusMovedPermanently)
75 f, err := os.Open(name)
77 // TODO expose actual error?
85 // TODO expose actual error?
91 // redirect to canonical path: / at end of directory url
92 // r.URL.Path always begins with /
95 if url[len(url)-1] != '/' {
96 Redirect(w, r, url+"/", StatusMovedPermanently)
100 if url[len(url)-1] == '/' {
101 Redirect(w, r, url[0:len(url)-1], StatusMovedPermanently)
107 if t, _ := time.Parse(TimeFormat, r.Header.Get("If-Modified-Since")); t != nil && d.Mtime_ns/1e9 <= t.Seconds() {
108 w.WriteHeader(StatusNotModified)
111 w.Header().Set("Last-Modified", time.SecondsToUTC(d.Mtime_ns/1e9).Format(TimeFormat))
113 // use contents of index.html for directory, if present
115 index := name + filepath.FromSlash(indexPage)
116 ff, err := os.Open(index)
137 // If Content-Type isn't set, use the file's extension to find it.
138 if w.Header().Get("Content-Type") == "" {
139 ctype := mime.TypeByExtension(filepath.Ext(name))
141 // read a chunk to decide between utf-8 text and binary
143 n, _ := io.ReadFull(f, buf[:])
146 ctype = "text/plain; charset=utf-8"
149 ctype = "application/octet-stream"
151 f.Seek(0, os.SEEK_SET) // rewind to output whole file
153 w.Header().Set("Content-Type", ctype)
156 // handle Content-Range header.
157 // TODO(adg): handle multiple ranges
158 ranges, err := parseRange(r.Header.Get("Range"), size)
159 if err == nil && len(ranges) > 1 {
160 err = os.ErrorString("multiple ranges not supported")
163 Error(w, err.String(), StatusRequestedRangeNotSatisfiable)
166 if len(ranges) == 1 {
168 if _, err := f.Seek(ra.start, os.SEEK_SET); err != nil {
169 Error(w, err.String(), StatusRequestedRangeNotSatisfiable)
173 code = StatusPartialContent
174 w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", ra.start, ra.start+ra.length-1, d.Size))
177 w.Header().Set("Accept-Ranges", "bytes")
178 w.Header().Set("Content-Length", strconv.Itoa64(size))
182 if r.Method != "HEAD" {
187 // ServeFile replies to the request with the contents of the named file or directory.
188 func ServeFile(w ResponseWriter, r *Request, name string) {
189 serveFile(w, r, name, false)
192 type fileHandler struct {
197 // FileServer returns a handler that serves HTTP requests
198 // with the contents of the file system rooted at root.
199 // It strips prefix from the incoming requests before
200 // looking up the file name in the file system.
201 func FileServer(root, prefix string) Handler { return &fileHandler{root, prefix} }
203 func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
205 if !strings.HasPrefix(path, f.prefix) {
209 path = path[len(f.prefix):]
210 serveFile(w, r, filepath.Join(f.root, filepath.FromSlash(path)), true)
213 // httpRange specifies the byte range to be sent to the client.
214 type httpRange struct {
218 // parseRange parses a Range header string as per RFC 2616.
219 func parseRange(s string, size int64) ([]httpRange, os.Error) {
221 return nil, nil // header not present
224 if !strings.HasPrefix(s, b) {
225 return nil, os.NewError("invalid range")
227 var ranges []httpRange
228 for _, ra := range strings.Split(s[len(b):], ",", -1) {
229 i := strings.Index(ra, "-")
231 return nil, os.NewError("invalid range")
233 start, end := ra[:i], ra[i+1:]
236 // If no start is specified, end specifies the
237 // range start relative to the end of the file.
238 i, err := strconv.Atoi64(end)
240 return nil, os.NewError("invalid range")
246 r.length = size - r.start
248 i, err := strconv.Atoi64(start)
249 if err != nil || i > size || i < 0 {
250 return nil, os.NewError("invalid range")
254 // If no end is specified, range extends to end of the file.
255 r.length = size - r.start
257 i, err := strconv.Atoi64(end)
258 if err != nil || r.start > i {
259 return nil, os.NewError("invalid range")
264 r.length = i - r.start + 1
267 ranges = append(ranges, r)