Commit 843f6e83 authored by Matthew Holt's avatar Matthew Holt

Refactored browse middleware to return errors

parent 058ff948
...@@ -20,7 +20,7 @@ import ( ...@@ -20,7 +20,7 @@ import (
// Browse is an http.Handler that can show a file listing when // Browse is an http.Handler that can show a file listing when
// directories in the given paths are specified. // directories in the given paths are specified.
type Browse struct { type Browse struct {
Next http.HandlerFunc Next middleware.HandlerFunc
Root string Root string
Configs []BrowseConfig Configs []BrowseConfig
} }
...@@ -83,26 +83,23 @@ func New(c middleware.Controller) (middleware.Middleware, error) { ...@@ -83,26 +83,23 @@ func New(c middleware.Controller) (middleware.Middleware, error) {
Configs: configs, Configs: configs,
} }
return func(next http.HandlerFunc) http.HandlerFunc { return func(next middleware.HandlerFunc) middleware.HandlerFunc {
browse.Next = next browse.Next = next
return browse.ServeHTTP return browse.ServeHTTP
}, nil }, nil
} }
// ServeHTTP implements the http.Handler interface. // ServeHTTP implements the middleware.Handler interface.
func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
filename := b.Root + r.URL.Path filename := b.Root + r.URL.Path
info, err := os.Stat(filename) info, err := os.Stat(filename)
if err != nil { if err != nil {
// TODO: 404 Not Found return b.Next(w, r)
b.Next(w, r)
return
} }
if !info.IsDir() { if !info.IsDir() {
b.Next(w, r) return b.Next(w, r)
return
} }
// See if there's a browse configuration to match the path // See if there's a browse configuration to match the path
...@@ -115,21 +112,21 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -115,21 +112,21 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// that doesn't end in "/" (which it should, anyway) // that doesn't end in "/" (which it should, anyway)
if r.URL.Path[len(r.URL.Path)-1] != '/' { if r.URL.Path[len(r.URL.Path)-1] != '/' {
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect) http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
return return 0, nil
} }
// Load directory contents // Load directory contents
file, err := os.Open(b.Root + r.URL.Path) file, err := os.Open(b.Root + r.URL.Path)
if err != nil { if err != nil {
panic(err) // TODO return http.StatusForbidden, err
} }
defer file.Close() defer file.Close()
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
files, err := file.Readdir(-1) files, err := file.Readdir(-1)
if err != nil || len(files) == 0 { if err != nil {
// TODO - second condition may not be necessary? See docs... return http.StatusForbidden, err
} }
// Assemble listing of directory contents // Assemble listing of directory contents
...@@ -185,16 +182,17 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -185,16 +182,17 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Items: fileinfos, Items: fileinfos,
} }
// TODO: Don't write to w until we know there wasn't an error
err = bc.Template.Execute(w, listing) err = bc.Template.Execute(w, listing)
if err != nil { if err != nil {
panic(err) // TODO return http.StatusInternalServerError, err
} }
return return http.StatusOK, nil
} }
// Didn't qualify; pass-thru // Didn't qualify; pass-thru
b.Next(w, r) return b.Next(w, r)
} }
// parse returns a list of browsing configurations // parse returns a list of browsing configurations
......
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