|
| 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 | + "fmt"; |
| 11 | + "http"; |
| 12 | + "io"; |
| 13 | + "os"; |
| 14 | + "path"; |
| 15 | + "strings"; |
| 16 | + "utf8"; |
| 17 | +) |
| 18 | + |
| 19 | +// TODO this should be in a mime package somewhere |
| 20 | +var contentByExt = map[string] string { |
| 21 | + ".css": "text/css", |
| 22 | + ".gif": "image/gif", |
| 23 | + ".html": "text/html; charset=utf-8", |
| 24 | + ".jpg": "image/jpeg", |
| 25 | + ".js": "application/x-javascript", |
| 26 | + ".png": "image/png", |
| 27 | +} |
| 28 | + |
| 29 | +// Heuristic: b is text if it is valid UTF-8 and doesn't |
| 30 | +// contain any unprintable ASCII or Unicode characters. |
| 31 | +func isText(b []byte) bool { |
| 32 | + for len(b) > 0 && utf8.FullRune(b) { |
| 33 | + rune, size := utf8.DecodeRune(b); |
| 34 | + if size == 1 && rune == utf8.RuneError { |
| 35 | + // decoding error |
| 36 | + return false; |
| 37 | + } |
| 38 | + if 0x80 <= rune && rune <= 0x9F { |
| 39 | + return false; |
| 40 | + } |
| 41 | + if rune < ' ' { |
| 42 | + switch rune { |
| 43 | + case '\n', '\r', '\t': |
| 44 | + // okay |
| 45 | + default: |
| 46 | + // binary garbage |
| 47 | + return false; |
| 48 | + } |
| 49 | + } |
| 50 | + b = b[size:len(b)]; |
| 51 | + } |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +func dirList(c *Conn, f *os.File) { |
| 56 | + fmt.Fprintf(c, "<pre>\n"); |
| 57 | + for { |
| 58 | + dirs, err := f.Readdir(100); |
| 59 | + if err != nil || len(dirs) == 0 { |
| 60 | + break |
| 61 | + } |
| 62 | + for i, d := range dirs { |
| 63 | + name := d.Name; |
| 64 | + if d.IsDirectory() { |
| 65 | + name += "/" |
| 66 | + } |
| 67 | + // TODO htmlescape |
| 68 | + fmt.Fprintf(c, "<a href=\"%s\">%s</a>\n", name, name); |
| 69 | + } |
| 70 | + } |
| 71 | + fmt.Fprintf(c, "</pre>\n"); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +func serveFileInternal(c *Conn, r *Request, name string, redirect bool) { |
| 76 | + const indexPage = "/index.html"; |
| 77 | + |
| 78 | + // redirect to strip off any index.html |
| 79 | + n := len(name) - len(indexPage); |
| 80 | + if n >= 0 && name[n:len(name)] == indexPage { |
| 81 | + http.Redirect(c, name[0:n+1]); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + f, err := os.Open(name, os.O_RDONLY, 0); |
| 86 | + if err != nil { |
| 87 | + // TODO expose actual error? |
| 88 | + NotFound(c, r); |
| 89 | + return; |
| 90 | + } |
| 91 | + defer f.Close(); |
| 92 | + |
| 93 | + d, err1 := f.Stat(); |
| 94 | + if err1 != nil { |
| 95 | + // TODO expose actual error? |
| 96 | + NotFound(c, r); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + if redirect { |
| 101 | + // redirect to canonical path: / at end of directory url |
| 102 | + // r.Url.Path always begins with / |
| 103 | + url := r.Url.Path; |
| 104 | + if d.IsDirectory() { |
| 105 | + if url[len(url)-1] != '/' { |
| 106 | + http.Redirect(c, url + "/"); |
| 107 | + return; |
| 108 | + } |
| 109 | + } else { |
| 110 | + if url[len(url)-1] == '/' { |
| 111 | + http.Redirect(c, url[0:len(url)-1]); |
| 112 | + return; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // use contents of index.html for directory, if present |
| 118 | + if d.IsDirectory() { |
| 119 | + index := name + indexPage; |
| 120 | + ff, err := os.Open(index, os.O_RDONLY, 0); |
| 121 | + if err == nil { |
| 122 | + defer ff.Close(); |
| 123 | + dd, err := ff.Stat(); |
| 124 | + if err == nil { |
| 125 | + name = index; |
| 126 | + d = dd; |
| 127 | + f = ff; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if d.IsDirectory() { |
| 133 | + dirList(c, f); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + // serve file |
| 138 | + // use extension to find content type. |
| 139 | + ext := path.Ext(name); |
| 140 | + if ctype, ok := contentByExt[ext]; ok { |
| 141 | + c.SetHeader("Content-Type", ctype); |
| 142 | + } else { |
| 143 | + // read first chunk to decide between utf-8 text and binary |
| 144 | + var buf [1024]byte; |
| 145 | + n, err := io.Readn(f, buf); |
| 146 | + b := buf[0:n]; |
| 147 | + if isText(b) { |
| 148 | + c.SetHeader("Content-Type", "text-plain; charset=utf-8"); |
| 149 | + } else { |
| 150 | + c.SetHeader("Content-Type", "application/octet-stream"); // generic binary |
| 151 | + } |
| 152 | + c.Write(b); |
| 153 | + } |
| 154 | + io.Copy(f, c); |
| 155 | +} |
| 156 | + |
| 157 | +// ServeFile replies to the request with the contents of the named file or directory. |
| 158 | +func ServeFile(c *Conn, r *Request, name string) { |
| 159 | + serveFileInternal(c, r, name, false); |
| 160 | +} |
| 161 | + |
| 162 | +type fileHandler struct { |
| 163 | + root string; |
| 164 | + prefix string; |
| 165 | +} |
| 166 | + |
| 167 | +// FileServer returns a handler that serves HTTP requests |
| 168 | +// with the contents of the file system rooted at root. |
| 169 | +// It strips prefix from the incoming requests before |
| 170 | +// looking up the file name in the file system. |
| 171 | +func FileServer(root, prefix string) Handler { |
| 172 | + return &fileHandler{root, prefix}; |
| 173 | +} |
| 174 | + |
| 175 | +func (f *fileHandler) ServeHTTP(c *Conn, r *Request) { |
| 176 | + path := r.Url.Path; |
| 177 | + if !strings.HasPrefix(path, f.prefix) { |
| 178 | + NotFound(c, r); |
| 179 | + return; |
| 180 | + } |
| 181 | + path = path[len(f.prefix):len(path)]; |
| 182 | + serveFileInternal(c, r, f.root + "/" + path, true); |
| 183 | +} |
| 184 | + |
0 commit comments