mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-08 11:58:49 +03:00
add ignoreIndexes option to browse
This commit is contained in:
parent
840bc505f6
commit
30b19190dc
1 changed files with 11 additions and 8 deletions
|
@ -23,9 +23,10 @@ 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 middleware.Handler
|
Next middleware.Handler
|
||||||
Root string
|
Root string
|
||||||
Configs []Config
|
Configs []Config
|
||||||
|
IgnoreIndexes bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config is a configuration for browsing in a particular path.
|
// Config is a configuration for browsing in a particular path.
|
||||||
|
@ -142,16 +143,18 @@ var IndexPages = []string{
|
||||||
"default.txt",
|
"default.txt",
|
||||||
}
|
}
|
||||||
|
|
||||||
func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string) (Listing, error) {
|
func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string, ignoreIndexes bool) (Listing, error) {
|
||||||
var fileinfos []FileInfo
|
var fileinfos []FileInfo
|
||||||
var urlPath = r.URL.Path
|
var urlPath = r.URL.Path
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
name := f.Name()
|
name := f.Name()
|
||||||
|
|
||||||
// Directory is not browsable if it contains index file
|
// Directory is not browsable if it contains index file
|
||||||
for _, indexName := range IndexPages {
|
if !ignoreIndexes {
|
||||||
if name == indexName {
|
for _, indexName := range IndexPages {
|
||||||
return Listing{}, errors.New("Directory contains index file, not browsable!")
|
if name == indexName {
|
||||||
|
return Listing{}, errors.New("Directory contains index file, not browsable!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,7 +237,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Assemble listing of directory contents
|
// Assemble listing of directory contents
|
||||||
listing, err := directoryListing(files, r, canGoUp, b.Root)
|
listing, err := directoryListing(files, r, canGoUp, b.Root, b.IgnoreIndexes)
|
||||||
if err != nil { // directory isn't browsable
|
if err != nil { // directory isn't browsable
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue