Commit b7c8afab authored by Matthew Holt's avatar Matthew Holt

Respond with 404 if requesting server's config file

parent 6ca475de
...@@ -44,6 +44,9 @@ type Config struct { ...@@ -44,6 +44,9 @@ type Config struct {
// MaxCPU is the maximum number of cores for the whole process to use // MaxCPU is the maximum number of cores for the whole process to use
MaxCPU int MaxCPU int
// The path to the configuration file from which this was loaded
ConfigFile string
} }
// Address returns the host:port of c as a string. // Address returns the host:port of c as a string.
...@@ -75,7 +78,16 @@ func Load(filename string) ([]Config, error) { ...@@ -75,7 +78,16 @@ func Load(filename string) ([]Config, error) {
return nil, err return nil, err
} }
return p.parse() cfgs, err := p.parse()
if err != nil {
return []Config{}, err
}
for i := 0; i < len(cfgs); i++ {
cfgs[i].ConfigFile = filename
}
return cfgs, nil
} }
// IsNotFound returns whether or not the error is // IsNotFound returns whether or not the error is
......
...@@ -10,20 +10,22 @@ import ( ...@@ -10,20 +10,22 @@ import (
"github.com/mholt/caddy/middleware/browse" "github.com/mholt/caddy/middleware/browse"
) )
// This FileServer is adapted from the one in net/http // This FileServer is adapted from the one in net/http by
// by the Go authors. Some modifications have been made. // the Go authors. Significant modifications have been made.
//
// //
// License: // License:
// //
// Copyright 2009 The Go Authors. All rights reserved. // Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
func FileServer(root http.FileSystem) middleware.Handler { func FileServer(root http.FileSystem, hide []string) middleware.Handler {
return &fileHandler{root} return &fileHandler{root: root, hide: hide}
} }
type fileHandler struct { type fileHandler struct {
root http.FileSystem root http.FileSystem
hide []string // list of files to treat as "Not Found"
} }
func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
...@@ -32,12 +34,12 @@ func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er ...@@ -32,12 +34,12 @@ func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
upath = "/" + upath upath = "/" + upath
r.URL.Path = upath r.URL.Path = upath
} }
return serveFile(w, r, f.root, path.Clean(upath)) return f.serveFile(w, r, path.Clean(upath))
} }
// name is '/'-separated, not filepath.Separator. // name is '/'-separated, not filepath.Separator.
func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string) (int, error) { func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name string) (int, error) {
f, err := fs.Open(name) f, err := fh.root.Open(name)
if err != nil { if err != nil {
if os.IsPermission(err) { if os.IsPermission(err) {
return http.StatusForbidden, err return http.StatusForbidden, err
...@@ -58,7 +60,7 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name ...@@ -58,7 +60,7 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
if d.IsDir() { if d.IsDir() {
for _, indexPage := range browse.IndexPages { for _, indexPage := range browse.IndexPages {
index := strings.TrimSuffix(name, "/") + "/" + indexPage index := strings.TrimSuffix(name, "/") + "/" + indexPage
ff, err := fs.Open(index) ff, err := fh.root.Open(index)
if err == nil { if err == nil {
defer ff.Close() defer ff.Close()
dd, err := ff.Stat() dd, err := ff.Stat()
...@@ -78,6 +80,14 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name ...@@ -78,6 +80,14 @@ func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
return http.StatusNotFound, nil return http.StatusNotFound, nil
} }
// 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 {
if d.Name() == hiddenPath {
return http.StatusNotFound, nil
}
}
// Note: Errors generated by ServeContent are written immediately // Note: Errors generated by ServeContent are written immediately
// to the response. This usually only happens if seeking fails (rare). // to the response. This usually only happens if seeking fails (rare).
http.ServeContent(w, r, d.Name(), d.ModTime(), f) http.ServeContent(w, r, d.Name(), d.ModTime(), f)
......
...@@ -134,7 +134,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -134,7 +134,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// on its config. This method should be called last before // on its config. This method should be called last before
// ListenAndServe begins. // ListenAndServe begins.
func (s *Server) buildStack() error { func (s *Server) buildStack() error {
s.fileServer = FileServer(http.Dir(s.config.Root)) s.fileServer = FileServer(http.Dir(s.config.Root), []string{s.config.ConfigFile})
// TODO: We only compile middleware for the "/" scope. // TODO: We only compile middleware for the "/" scope.
// Partial support for multiple location contexts already // Partial support for multiple location contexts already
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment