2009-04-14 21:31:31 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// HTTP file system request handler
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"utf8"
|
2009-04-14 21:31:31 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO this should be in a mime package somewhere
|
2009-11-04 23:45:59 -07:00
|
|
|
var contentByExt = map[string]string{
|
|
|
|
".css": "text/css",
|
|
|
|
".gif": "image/gif",
|
|
|
|
".html": "text/html; charset=utf-8",
|
|
|
|
".jpg": "image/jpeg",
|
|
|
|
".js": "application/x-javascript",
|
|
|
|
".pdf": "application/pdf",
|
|
|
|
".png": "image/png",
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Heuristic: b is text if it is valid UTF-8 and doesn't
|
|
|
|
// contain any unprintable ASCII or Unicode characters.
|
|
|
|
func isText(b []byte) bool {
|
|
|
|
for len(b) > 0 && utf8.FullRune(b) {
|
2009-12-15 16:35:38 -07:00
|
|
|
rune, size := utf8.DecodeRune(b)
|
2009-04-14 21:31:31 -06:00
|
|
|
if size == 1 && rune == utf8.RuneError {
|
|
|
|
// decoding error
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
if 0x80 <= rune && rune <= 0x9F {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
if rune < ' ' {
|
|
|
|
switch rune {
|
|
|
|
case '\n', '\r', '\t':
|
|
|
|
// okay
|
|
|
|
default:
|
|
|
|
// binary garbage
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
b = b[size:]
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return true
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func dirList(c *Conn, f *os.File) {
|
2009-12-15 16:35:38 -07:00
|
|
|
fmt.Fprintf(c, "<pre>\n")
|
2009-04-14 21:31:31 -06:00
|
|
|
for {
|
2009-12-15 16:35:38 -07:00
|
|
|
dirs, err := f.Readdir(100)
|
2009-04-14 21:31:31 -06:00
|
|
|
if err != nil || len(dirs) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, d := range dirs {
|
2009-12-15 16:35:38 -07:00
|
|
|
name := d.Name
|
2009-04-14 21:31:31 -06:00
|
|
|
if d.IsDirectory() {
|
2009-11-09 13:07:39 -07:00
|
|
|
name += "/"
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
// TODO htmlescape
|
2009-12-15 16:35:38 -07:00
|
|
|
fmt.Fprintf(c, "<a href=\"%s\">%s</a>\n", name, name)
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
fmt.Fprintf(c, "</pre>\n")
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func serveFileInternal(c *Conn, r *Request, name string, redirect bool) {
|
2009-12-15 16:35:38 -07:00
|
|
|
const indexPage = "/index.html"
|
2009-04-14 21:31:31 -06:00
|
|
|
|
2009-11-24 12:47:53 -07:00
|
|
|
// redirect .../index.html to .../
|
|
|
|
if strings.HasSuffix(r.URL.Path, indexPage) {
|
2009-12-15 16:35:38 -07:00
|
|
|
Redirect(c, r.URL.Path[0:len(r.URL.Path)-len(indexPage)+1], StatusMovedPermanently)
|
|
|
|
return
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
f, err := os.Open(name, os.O_RDONLY, 0)
|
2009-04-14 21:31:31 -06:00
|
|
|
if err != nil {
|
|
|
|
// TODO expose actual error?
|
2009-12-15 16:35:38 -07:00
|
|
|
NotFound(c, r)
|
|
|
|
return
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
defer f.Close()
|
2009-04-14 21:31:31 -06:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
d, err1 := f.Stat()
|
2009-04-14 21:31:31 -06:00
|
|
|
if err1 != nil {
|
|
|
|
// TODO expose actual error?
|
2009-12-15 16:35:38 -07:00
|
|
|
NotFound(c, r)
|
|
|
|
return
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if redirect {
|
|
|
|
// redirect to canonical path: / at end of directory url
|
2009-11-08 22:46:20 -07:00
|
|
|
// r.URL.Path always begins with /
|
2009-12-15 16:35:38 -07:00
|
|
|
url := r.URL.Path
|
2009-04-14 21:31:31 -06:00
|
|
|
if d.IsDirectory() {
|
|
|
|
if url[len(url)-1] != '/' {
|
2009-12-15 16:35:38 -07:00
|
|
|
Redirect(c, url+"/", StatusMovedPermanently)
|
|
|
|
return
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if url[len(url)-1] == '/' {
|
2009-12-15 16:35:38 -07:00
|
|
|
Redirect(c, url[0:len(url)-1], StatusMovedPermanently)
|
|
|
|
return
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use contents of index.html for directory, if present
|
|
|
|
if d.IsDirectory() {
|
2009-12-15 16:35:38 -07:00
|
|
|
index := name + indexPage
|
|
|
|
ff, err := os.Open(index, os.O_RDONLY, 0)
|
2009-04-14 21:31:31 -06:00
|
|
|
if err == nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
defer ff.Close()
|
|
|
|
dd, err := ff.Stat()
|
2009-04-14 21:31:31 -06:00
|
|
|
if err == nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
name = index
|
|
|
|
d = dd
|
|
|
|
f = ff
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.IsDirectory() {
|
2009-12-15 16:35:38 -07:00
|
|
|
dirList(c, f)
|
|
|
|
return
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// serve file
|
|
|
|
// use extension to find content type.
|
2009-12-15 16:35:38 -07:00
|
|
|
ext := path.Ext(name)
|
2009-04-14 21:31:31 -06:00
|
|
|
if ctype, ok := contentByExt[ext]; ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
c.SetHeader("Content-Type", ctype)
|
2009-04-14 21:31:31 -06:00
|
|
|
} else {
|
|
|
|
// read first chunk to decide between utf-8 text and binary
|
2009-12-15 16:35:38 -07:00
|
|
|
var buf [1024]byte
|
|
|
|
n, _ := io.ReadFull(f, &buf)
|
|
|
|
b := buf[0:n]
|
2009-04-14 21:31:31 -06:00
|
|
|
if isText(b) {
|
2009-11-09 13:07:39 -07:00
|
|
|
c.SetHeader("Content-Type", "text-plain; charset=utf-8")
|
2009-04-14 21:31:31 -06:00
|
|
|
} else {
|
2009-12-15 16:35:38 -07:00
|
|
|
c.SetHeader("Content-Type", "application/octet-stream") // generic binary
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
c.Write(b)
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
io.Copy(c, f)
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeFile replies to the request with the contents of the named file or directory.
|
|
|
|
func ServeFile(c *Conn, r *Request, name string) {
|
2009-11-09 13:07:39 -07:00
|
|
|
serveFileInternal(c, r, name, false)
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type fileHandler struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
root string
|
|
|
|
prefix string
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// FileServer returns a handler that serves HTTP requests
|
|
|
|
// with the contents of the file system rooted at root.
|
|
|
|
// It strips prefix from the incoming requests before
|
|
|
|
// looking up the file name in the file system.
|
2009-12-15 16:35:38 -07:00
|
|
|
func FileServer(root, prefix string) Handler { return &fileHandler{root, prefix} }
|
2009-04-14 21:31:31 -06:00
|
|
|
|
|
|
|
func (f *fileHandler) ServeHTTP(c *Conn, r *Request) {
|
2009-12-15 16:35:38 -07:00
|
|
|
path := r.URL.Path
|
2009-04-14 21:31:31 -06:00
|
|
|
if !strings.HasPrefix(path, f.prefix) {
|
2009-12-15 16:35:38 -07:00
|
|
|
NotFound(c, r)
|
|
|
|
return
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
path = path[len(f.prefix):]
|
|
|
|
serveFileInternal(c, r, f.root+"/"+path, true)
|
2009-04-14 21:31:31 -06:00
|
|
|
}
|