mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-05 18:44:58 +03:00
Bug fixes and improvements for browse middleware
This commit is contained in:
parent
00e43197fd
commit
9672850d11
1 changed files with 33 additions and 0 deletions
|
@ -55,6 +55,13 @@ type FileInfo struct {
|
||||||
Mode os.FileMode
|
Mode os.FileMode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var IndexPages = []string{
|
||||||
|
"index.html",
|
||||||
|
"index.htm",
|
||||||
|
"default.html",
|
||||||
|
"default.htm",
|
||||||
|
}
|
||||||
|
|
||||||
// New creates a new instance of browse middleware.
|
// New creates a new instance of browse middleware.
|
||||||
func New(c middleware.Controller) (middleware.Middleware, error) {
|
func New(c middleware.Controller) (middleware.Middleware, error) {
|
||||||
configs, err := parse(c)
|
configs, err := parse(c)
|
||||||
|
@ -89,11 +96,20 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See if there's a browse configuration to match the path
|
||||||
for _, bc := range b.Configs {
|
for _, bc := range b.Configs {
|
||||||
if !middleware.Path(r.URL.Path).Matches(bc.PathScope) {
|
if !middleware.Path(r.URL.Path).Matches(bc.PathScope) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Browsing navigation gets messed up if browsing a directory
|
||||||
|
// that doesn't end in "/" (which it should, anyway)
|
||||||
|
if r.URL.Path[len(r.URL.Path)-1] != '/' {
|
||||||
|
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
panic(err) // TODO
|
||||||
|
@ -109,8 +125,21 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// Assemble listing of directory contents
|
// Assemble listing of directory contents
|
||||||
var fileinfos []FileInfo
|
var fileinfos []FileInfo
|
||||||
|
var abort bool // we bail early if we find an index file
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
name := f.Name()
|
name := f.Name()
|
||||||
|
|
||||||
|
// Directory is not browseable if it contains index file
|
||||||
|
for _, indexName := range IndexPages {
|
||||||
|
if name == indexName {
|
||||||
|
abort = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if abort {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
name += "/"
|
name += "/"
|
||||||
}
|
}
|
||||||
|
@ -125,6 +154,10 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
Mode: f.Mode(),
|
Mode: f.Mode(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if abort {
|
||||||
|
// this dir has an index file, so not browsable
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Determine if user can browse up another folder
|
// Determine if user can browse up another folder
|
||||||
var canGoUp bool
|
var canGoUp bool
|
||||||
|
|
Loading…
Reference in a new issue