mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-13 22:51:08 -05:00
* Fix for issue #1287 - hide hidden files * Reuse IsHidden * Fix failing tests
This commit is contained in:
parent
466269fd10
commit
c555e95366
5 changed files with 48 additions and 24 deletions
|
@ -20,6 +20,12 @@ import (
|
||||||
"github.com/mholt/caddy/caddyhttp/staticfiles"
|
"github.com/mholt/caddy/caddyhttp/staticfiles"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sortByName = "name"
|
||||||
|
sortBySize = "size"
|
||||||
|
sortByTime = "time"
|
||||||
|
)
|
||||||
|
|
||||||
// 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 {
|
||||||
|
@ -31,7 +37,7 @@ type Browse struct {
|
||||||
// Config is a configuration for browsing in a particular path.
|
// Config is a configuration for browsing in a particular path.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PathScope string
|
PathScope string
|
||||||
Root http.FileSystem
|
Fs staticfiles.FileServer
|
||||||
Variables interface{}
|
Variables interface{}
|
||||||
Template *template.Template
|
Template *template.Template
|
||||||
}
|
}
|
||||||
|
@ -161,11 +167,11 @@ func (l Listing) applySort() {
|
||||||
// Check '.Order' to know how to sort
|
// Check '.Order' to know how to sort
|
||||||
if l.Order == "desc" {
|
if l.Order == "desc" {
|
||||||
switch l.Sort {
|
switch l.Sort {
|
||||||
case "name":
|
case sortByName:
|
||||||
sort.Sort(sort.Reverse(byName(l)))
|
sort.Sort(sort.Reverse(byName(l)))
|
||||||
case "size":
|
case sortBySize:
|
||||||
sort.Sort(sort.Reverse(bySize(l)))
|
sort.Sort(sort.Reverse(bySize(l)))
|
||||||
case "time":
|
case sortByTime:
|
||||||
sort.Sort(sort.Reverse(byTime(l)))
|
sort.Sort(sort.Reverse(byTime(l)))
|
||||||
default:
|
default:
|
||||||
// If not one of the above, do nothing
|
// If not one of the above, do nothing
|
||||||
|
@ -173,11 +179,11 @@ func (l Listing) applySort() {
|
||||||
}
|
}
|
||||||
} else { // If we had more Orderings we could add them here
|
} else { // If we had more Orderings we could add them here
|
||||||
switch l.Sort {
|
switch l.Sort {
|
||||||
case "name":
|
case sortByName:
|
||||||
sort.Sort(byName(l))
|
sort.Sort(byName(l))
|
||||||
case "size":
|
case sortBySize:
|
||||||
sort.Sort(bySize(l))
|
sort.Sort(bySize(l))
|
||||||
case "time":
|
case sortByTime:
|
||||||
sort.Sort(byTime(l))
|
sort.Sort(byTime(l))
|
||||||
default:
|
default:
|
||||||
// If not one of the above, do nothing
|
// If not one of the above, do nothing
|
||||||
|
@ -186,7 +192,7 @@ func (l Listing) applySort() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string) (Listing, bool) {
|
func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, config *Config) (Listing, bool) {
|
||||||
var (
|
var (
|
||||||
fileinfos []FileInfo
|
fileinfos []FileInfo
|
||||||
dirCount, fileCount int
|
dirCount, fileCount int
|
||||||
|
@ -212,6 +218,10 @@ func directoryListing(files []os.FileInfo, canGoUp bool, urlPath string) (Listin
|
||||||
|
|
||||||
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
url := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name
|
||||||
|
|
||||||
|
if config.Fs.IsHidden(f) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
fileinfos = append(fileinfos, FileInfo{
|
fileinfos = append(fileinfos, FileInfo{
|
||||||
IsDir: f.IsDir(),
|
IsDir: f.IsDir(),
|
||||||
Name: f.Name(),
|
Name: f.Name(),
|
||||||
|
@ -247,7 +257,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
inScope:
|
inScope:
|
||||||
|
|
||||||
// Browse works on existing directories; delegate everything else
|
// Browse works on existing directories; delegate everything else
|
||||||
requestedFilepath, err := bc.Root.Open(r.URL.Path)
|
requestedFilepath, err := bc.Fs.Root.Open(r.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
switch {
|
||||||
case os.IsPermission(err):
|
case os.IsPermission(err):
|
||||||
|
@ -295,7 +305,7 @@ inScope:
|
||||||
return b.ServeListing(w, r, requestedFilepath, bc)
|
return b.ServeListing(w, r, requestedFilepath, bc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b Browse) loadDirectoryContents(requestedFilepath http.File, urlPath string) (*Listing, bool, error) {
|
func (b Browse) loadDirectoryContents(requestedFilepath http.File, urlPath string, config *Config) (*Listing, bool, error) {
|
||||||
files, err := requestedFilepath.Readdir(-1)
|
files, err := requestedFilepath.Readdir(-1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
|
@ -312,7 +322,7 @@ func (b Browse) loadDirectoryContents(requestedFilepath http.File, urlPath strin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assemble listing of directory contents
|
// Assemble listing of directory contents
|
||||||
listing, hasIndex := directoryListing(files, canGoUp, urlPath)
|
listing, hasIndex := directoryListing(files, canGoUp, urlPath, config)
|
||||||
|
|
||||||
return &listing, hasIndex, nil
|
return &listing, hasIndex, nil
|
||||||
}
|
}
|
||||||
|
@ -327,11 +337,11 @@ func (b Browse) handleSortOrder(w http.ResponseWriter, r *http.Request, scope st
|
||||||
// If the query 'sort' or 'order' is empty, use defaults or any values previously saved in Cookies
|
// If the query 'sort' or 'order' is empty, use defaults or any values previously saved in Cookies
|
||||||
switch sort {
|
switch sort {
|
||||||
case "":
|
case "":
|
||||||
sort = "name"
|
sort = sortByName
|
||||||
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
|
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
|
||||||
sort = sortCookie.Value
|
sort = sortCookie.Value
|
||||||
}
|
}
|
||||||
case "name", "size", "type":
|
case sortByName, sortBySize, sortByTime:
|
||||||
http.SetCookie(w, &http.Cookie{Name: "sort", Value: sort, Path: scope, Secure: r.TLS != nil})
|
http.SetCookie(w, &http.Cookie{Name: "sort", Value: sort, Path: scope, Secure: r.TLS != nil})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,7 +367,7 @@ func (b Browse) handleSortOrder(w http.ResponseWriter, r *http.Request, scope st
|
||||||
|
|
||||||
// ServeListing returns a formatted view of 'requestedFilepath' contents'.
|
// ServeListing returns a formatted view of 'requestedFilepath' contents'.
|
||||||
func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFilepath http.File, bc *Config) (int, error) {
|
func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFilepath http.File, bc *Config) (int, error) {
|
||||||
listing, containsIndex, err := b.loadDirectoryContents(requestedFilepath, r.URL.Path)
|
listing, containsIndex, err := b.loadDirectoryContents(requestedFilepath, r.URL.Path, bc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
switch {
|
||||||
case os.IsPermission(err):
|
case os.IsPermission(err):
|
||||||
|
@ -372,7 +382,7 @@ func (b Browse) ServeListing(w http.ResponseWriter, r *http.Request, requestedFi
|
||||||
return b.Next.ServeHTTP(w, r)
|
return b.Next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
listing.Context = httpserver.Context{
|
listing.Context = httpserver.Context{
|
||||||
Root: bc.Root,
|
Root: bc.Fs.Root,
|
||||||
Req: r,
|
Req: r,
|
||||||
URL: r.URL,
|
URL: r.URL,
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||||
|
"github.com/mholt/caddy/caddyhttp/staticfiles"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSort(t *testing.T) {
|
func TestSort(t *testing.T) {
|
||||||
|
@ -106,7 +107,9 @@ func TestBrowseHTTPMethods(t *testing.T) {
|
||||||
Configs: []Config{
|
Configs: []Config{
|
||||||
{
|
{
|
||||||
PathScope: "/photos",
|
PathScope: "/photos",
|
||||||
|
Fs: staticfiles.FileServer{
|
||||||
Root: http.Dir("./testdata"),
|
Root: http.Dir("./testdata"),
|
||||||
|
},
|
||||||
Template: tmpl,
|
Template: tmpl,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -145,7 +148,10 @@ func TestBrowseTemplate(t *testing.T) {
|
||||||
Configs: []Config{
|
Configs: []Config{
|
||||||
{
|
{
|
||||||
PathScope: "/photos",
|
PathScope: "/photos",
|
||||||
|
Fs: staticfiles.FileServer{
|
||||||
Root: http.Dir("./testdata"),
|
Root: http.Dir("./testdata"),
|
||||||
|
Hide: []string{"photos/hidden.html"},
|
||||||
|
},
|
||||||
Template: tmpl,
|
Template: tmpl,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -199,9 +205,11 @@ func TestBrowseJson(t *testing.T) {
|
||||||
Configs: []Config{
|
Configs: []Config{
|
||||||
{
|
{
|
||||||
PathScope: "/photos/",
|
PathScope: "/photos/",
|
||||||
|
Fs: staticfiles.FileServer{
|
||||||
Root: http.Dir("./testdata"),
|
Root: http.Dir("./testdata"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
//Getting the listing from the ./testdata/photos, the listing returned will be used to validate test results
|
//Getting the listing from the ./testdata/photos, the listing returned will be used to validate test results
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/mholt/caddy"
|
"github.com/mholt/caddy"
|
||||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||||
|
"github.com/mholt/caddy/caddyhttp/staticfiles"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -61,7 +62,11 @@ func browseParse(c *caddy.Controller) ([]Config, error) {
|
||||||
} else {
|
} else {
|
||||||
bc.PathScope = "/"
|
bc.PathScope = "/"
|
||||||
}
|
}
|
||||||
bc.Root = http.Dir(cfg.Root)
|
|
||||||
|
bc.Fs = staticfiles.FileServer{
|
||||||
|
Root: http.Dir(cfg.Root),
|
||||||
|
Hide: httpserver.GetConfig(c).HiddenFiles,
|
||||||
|
}
|
||||||
|
|
||||||
// Second argument would be the template file to use
|
// Second argument would be the template file to use
|
||||||
var tplText string
|
var tplText string
|
||||||
|
|
1
caddyhttp/browse/testdata/photos/hidden.html
vendored
Normal file
1
caddyhttp/browse/testdata/photos/hidden.html
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Should be hidden
|
|
@ -117,7 +117,7 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request, name stri
|
||||||
return http.StatusNotFound, nil
|
return http.StatusNotFound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if fs.isHidden(d) {
|
if fs.IsHidden(d) {
|
||||||
return http.StatusNotFound, nil
|
return http.StatusNotFound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,8 +132,8 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request, name stri
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isHidden checks if file with FileInfo d is on hide list.
|
// IsHidden checks if file with FileInfo d is on hide list.
|
||||||
func (fs FileServer) isHidden(d os.FileInfo) bool {
|
func (fs FileServer) IsHidden(d os.FileInfo) bool {
|
||||||
// If the file is supposed to be hidden, return a 404
|
// If the file is supposed to be hidden, return a 404
|
||||||
for _, hiddenPath := range fs.Hide {
|
for _, hiddenPath := range fs.Hide {
|
||||||
// Check if the served file is exactly the hidden file.
|
// Check if the served file is exactly the hidden file.
|
||||||
|
|
Loading…
Reference in a new issue