2015-03-25 11:55:51 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2015-03-29 06:49:42 +08:00
|
|
|
"os"
|
2015-03-25 11:55:51 +08:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2015-03-29 06:49:42 +08:00
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-03-25 11:55:51 +08:00
|
|
|
"github.com/mholt/caddy/middleware/browse"
|
|
|
|
)
|
|
|
|
|
2015-04-13 07:44:02 +08:00
|
|
|
// This FileServer is adapted from the one in net/http by
|
|
|
|
// the Go authors. Significant modifications have been made.
|
|
|
|
//
|
2015-03-25 11:55:51 +08:00
|
|
|
//
|
|
|
|
// License:
|
|
|
|
//
|
|
|
|
// 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.
|
2015-04-13 07:44:02 +08:00
|
|
|
func FileServer(root http.FileSystem, hide []string) middleware.Handler {
|
|
|
|
return &fileHandler{root: root, hide: hide}
|
2015-03-25 11:55:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type fileHandler struct {
|
|
|
|
root http.FileSystem
|
2015-04-13 07:44:02 +08:00
|
|
|
hide []string // list of files to treat as "Not Found"
|
2015-03-25 11:55:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-29 06:49:42 +08:00
|
|
|
func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-03-25 11:55:51 +08:00
|
|
|
upath := r.URL.Path
|
|
|
|
if !strings.HasPrefix(upath, "/") {
|
|
|
|
upath = "/" + upath
|
|
|
|
r.URL.Path = upath
|
|
|
|
}
|
2015-04-13 07:44:02 +08:00
|
|
|
return f.serveFile(w, r, path.Clean(upath))
|
2015-03-25 11:55:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// name is '/'-separated, not filepath.Separator.
|
2015-04-13 07:44:02 +08:00
|
|
|
func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name string) (int, error) {
|
|
|
|
f, err := fh.root.Open(name)
|
2015-03-25 11:55:51 +08:00
|
|
|
if err != nil {
|
2015-03-29 06:49:42 +08:00
|
|
|
if os.IsPermission(err) {
|
|
|
|
return http.StatusForbidden, err
|
|
|
|
}
|
|
|
|
return http.StatusNotFound, nil
|
2015-03-25 11:55:51 +08:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
d, err1 := f.Stat()
|
|
|
|
if err1 != nil {
|
2015-03-29 06:49:42 +08:00
|
|
|
if os.IsPermission(err) {
|
|
|
|
return http.StatusForbidden, err
|
|
|
|
}
|
|
|
|
return http.StatusNotFound, nil
|
2015-03-25 11:55:51 +08:00
|
|
|
}
|
|
|
|
|
2015-04-19 03:24:54 +08:00
|
|
|
// redirect to canonical path
|
|
|
|
url := r.URL.Path
|
|
|
|
if d.IsDir() {
|
|
|
|
// Ensure / at end of directory url
|
|
|
|
if url[len(url)-1] != '/' {
|
|
|
|
redirect(w, r, path.Base(url)+"/")
|
|
|
|
return http.StatusMovedPermanently, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Ensure no / at end of file url
|
|
|
|
if url[len(url)-1] == '/' {
|
|
|
|
redirect(w, r, "../"+path.Base(url))
|
|
|
|
return http.StatusMovedPermanently, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 11:55:51 +08:00
|
|
|
// use contents of an index file, if present, for directory
|
|
|
|
if d.IsDir() {
|
|
|
|
for _, indexPage := range browse.IndexPages {
|
|
|
|
index := strings.TrimSuffix(name, "/") + "/" + indexPage
|
2015-04-13 07:44:02 +08:00
|
|
|
ff, err := fh.root.Open(index)
|
2015-03-25 11:55:51 +08:00
|
|
|
if err == nil {
|
|
|
|
defer ff.Close()
|
|
|
|
dd, err := ff.Stat()
|
|
|
|
if err == nil {
|
|
|
|
name = index
|
|
|
|
d = dd
|
|
|
|
f = ff
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still a directory? (we didn't find an index file)
|
2015-03-29 06:49:42 +08:00
|
|
|
// Return 404 to hide the fact that the folder exists
|
2015-03-25 11:55:51 +08:00
|
|
|
if d.IsDir() {
|
2015-03-29 06:49:42 +08:00
|
|
|
return http.StatusNotFound, nil
|
2015-03-25 11:55:51 +08:00
|
|
|
}
|
|
|
|
|
2015-04-13 07:44:02 +08:00
|
|
|
// If the file is supposed to be hidden, return a 404
|
|
|
|
// (TODO: If the slice gets large, a set may be faster)
|
|
|
|
for _, hiddenPath := range fh.hide {
|
2015-04-28 12:27:34 +08:00
|
|
|
// Case-insensitive file systems may have loaded "CaddyFile" when
|
|
|
|
// we think we got "Caddyfile", which poses a security risk if we
|
|
|
|
// aren't careful here: case-insensitive comparison is required!
|
|
|
|
// TODO: This matches file NAME only, regardless of path. In other
|
|
|
|
// words, trying to serve another file with the same name as the
|
|
|
|
// active config file will result in a 404 when it shouldn't.
|
|
|
|
if strings.EqualFold(d.Name(), path.Base(hiddenPath)) {
|
2015-04-13 07:44:02 +08:00
|
|
|
return http.StatusNotFound, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 06:49:42 +08:00
|
|
|
// Note: Errors generated by ServeContent are written immediately
|
|
|
|
// to the response. This usually only happens if seeking fails (rare).
|
2015-03-25 11:55:51 +08:00
|
|
|
http.ServeContent(w, r, d.Name(), d.ModTime(), f)
|
2015-03-29 06:49:42 +08:00
|
|
|
|
|
|
|
return http.StatusOK, nil
|
2015-03-25 11:55:51 +08:00
|
|
|
}
|
2015-04-19 03:24:54 +08:00
|
|
|
|
|
|
|
// redirect is taken from http.localRedirect of the std lib. It
|
|
|
|
// sends an HTTP redirect to the client but will preserve the
|
|
|
|
// query string for the new path.
|
|
|
|
func redirect(w http.ResponseWriter, r *http.Request, newPath string) {
|
|
|
|
if q := r.URL.RawQuery; q != "" {
|
|
|
|
newPath += "?" + q
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, newPath, http.StatusMovedPermanently)
|
|
|
|
}
|