diff options
Diffstat (limited to 'modules/caddyhttp/fileserver')
-rw-r--r-- | modules/caddyhttp/fileserver/browse.go | 73 | ||||
-rw-r--r-- | modules/caddyhttp/fileserver/browse_test.go | 30 | ||||
-rw-r--r-- | modules/caddyhttp/fileserver/browsetplcontext.go (renamed from modules/caddyhttp/fileserver/browselisting.go) | 21 | ||||
-rw-r--r-- | modules/caddyhttp/fileserver/browsetplcontext_test.go (renamed from modules/caddyhttp/fileserver/browselisting_test.go) | 16 | ||||
-rw-r--r-- | modules/caddyhttp/fileserver/staticfiles.go | 18 |
5 files changed, 112 insertions, 46 deletions
diff --git a/modules/caddyhttp/fileserver/browse.go b/modules/caddyhttp/fileserver/browse.go index 2712077..fc8bddb 100644 --- a/modules/caddyhttp/fileserver/browse.go +++ b/modules/caddyhttp/fileserver/browse.go @@ -17,14 +17,16 @@ package fileserver import ( "bytes" "encoding/json" - "html/template" + "fmt" "net/http" "os" "path" "strings" + "text/template" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2/modules/caddyhttp" + "github.com/caddyserver/caddy/v2/modules/caddyhttp/templates" "go.uber.org/zap" ) @@ -82,7 +84,26 @@ func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter, } w.Header().Set("Content-Type", "application/json; charset=utf-8") } else { - if buf, err = fsrv.browseWriteHTML(listing); err != nil { + var fs http.FileSystem + if fsrv.Root != "" { + fs = http.Dir(repl.ReplaceAll(fsrv.Root, ".")) + } + + var tplCtx = &templateContext{ + TemplateContext: templates.TemplateContext{ + Root: fs, + Req: r, + RespHeader: templates.WrappedHeader{Header: w.Header()}, + }, + browseTemplateContext: listing, + } + + err = fsrv.makeBrowseTemplate(tplCtx) + if err != nil { + return fmt.Errorf("parsing browse template: %v", err) + } + + if buf, err = fsrv.browseWriteHTML(tplCtx); err != nil { return caddyhttp.Error(http.StatusInternalServerError, err) } w.Header().Set("Content-Type", "text/html; charset=utf-8") @@ -93,10 +114,10 @@ func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter, return nil } -func (fsrv *FileServer) loadDirectoryContents(dir *os.File, root, urlPath string, repl *caddy.Replacer) (browseListing, error) { +func (fsrv *FileServer) loadDirectoryContents(dir *os.File, root, urlPath string, repl *caddy.Replacer) (browseTemplateContext, error) { files, err := dir.Readdir(-1) if err != nil { - return browseListing{}, err + return browseTemplateContext{}, err } // user can presumably browse "up" to parent folder if path is longer than "/" @@ -107,7 +128,7 @@ func (fsrv *FileServer) loadDirectoryContents(dir *os.File, root, urlPath string // browseApplyQueryParams applies query parameters to the listing. // It mutates the listing and may set cookies. -func (fsrv *FileServer) browseApplyQueryParams(w http.ResponseWriter, r *http.Request, listing *browseListing) { +func (fsrv *FileServer) browseApplyQueryParams(w http.ResponseWriter, r *http.Request, listing *browseTemplateContext) { sortParam := r.URL.Query().Get("sort") orderParam := r.URL.Query().Get("order") limitParam := r.URL.Query().Get("limit") @@ -139,17 +160,41 @@ func (fsrv *FileServer) browseApplyQueryParams(w http.ResponseWriter, r *http.Re listing.applySortAndLimit(sortParam, orderParam, limitParam, offsetParam) } -func (fsrv *FileServer) browseWriteJSON(listing browseListing) (*bytes.Buffer, error) { +// makeBrowseTemplate creates the template to be used for directory listings. +func (fsrv *FileServer) makeBrowseTemplate(tplCtx *templateContext) error { + var tpl *template.Template + var err error + + if fsrv.Browse.TemplateFile != "" { + tpl = tplCtx.NewTemplate(path.Base(fsrv.Browse.TemplateFile)) + tpl, err = tpl.ParseFiles(fsrv.Browse.TemplateFile) + if err != nil { + return fmt.Errorf("parsing browse template file: %v", err) + } + } else { + tpl = tplCtx.NewTemplate("default_listing") + tpl, err = tpl.Parse(defaultBrowseTemplate) + if err != nil { + return fmt.Errorf("parsing default browse template: %v", err) + } + } + + fsrv.Browse.template = tpl + + return nil +} + +func (fsrv *FileServer) browseWriteJSON(listing browseTemplateContext) (*bytes.Buffer, error) { buf := bufPool.Get().(*bytes.Buffer) + defer bufPool.Put(buf) err := json.NewEncoder(buf).Encode(listing.Items) - bufPool.Put(buf) return buf, err } -func (fsrv *FileServer) browseWriteHTML(listing browseListing) (*bytes.Buffer, error) { +func (fsrv *FileServer) browseWriteHTML(tplCtx *templateContext) (*bytes.Buffer, error) { buf := bufPool.Get().(*bytes.Buffer) - err := fsrv.Browse.template.Execute(buf, listing) - bufPool.Put(buf) + defer bufPool.Put(buf) + err := fsrv.Browse.template.Execute(buf, tplCtx) return buf, err } @@ -171,3 +216,11 @@ func isSymlinkTargetDir(f os.FileInfo, root, urlPath string) bool { } return targetInfo.IsDir() } + +// templateContext powers the context used when evaluating the browse template. +// It combines browse-specific features with the standard templates handler +// features. +type templateContext struct { + templates.TemplateContext + browseTemplateContext +} diff --git a/modules/caddyhttp/fileserver/browse_test.go b/modules/caddyhttp/fileserver/browse_test.go index 5d5874f..30862fa 100644 --- a/modules/caddyhttp/fileserver/browse_test.go +++ b/modules/caddyhttp/fileserver/browse_test.go @@ -1,16 +1,27 @@ +// Copyright 2015 Matthew Holt and The Caddy Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package fileserver import ( - "html/template" "testing" - - "github.com/caddyserver/caddy/v2" + "text/template" ) func BenchmarkBrowseWriteJSON(b *testing.B) { fsrv := new(FileServer) - fsrv.Provision(caddy.Context{}) - listing := browseListing{ + listing := browseTemplateContext{ Name: "test", Path: "test", CanGoUp: false, @@ -30,12 +41,11 @@ func BenchmarkBrowseWriteJSON(b *testing.B) { func BenchmarkBrowseWriteHTML(b *testing.B) { fsrv := new(FileServer) - fsrv.Provision(caddy.Context{}) fsrv.Browse = &Browse{ TemplateFile: "", template: template.New("test"), } - listing := browseListing{ + listing := browseTemplateContext{ Name: "test", Path: "test", CanGoUp: false, @@ -46,9 +56,13 @@ func BenchmarkBrowseWriteHTML(b *testing.B) { Order: "", Limit: 42, } + tplCtx := &templateContext{ + browseTemplateContext: listing, + } + fsrv.makeBrowseTemplate(tplCtx) b.ResetTimer() for n := 0; n < b.N; n++ { - fsrv.browseWriteHTML(listing) + fsrv.browseWriteHTML(tplCtx) } } diff --git a/modules/caddyhttp/fileserver/browselisting.go b/modules/caddyhttp/fileserver/browsetplcontext.go index 2b8c66c..489e8c0 100644 --- a/modules/caddyhttp/fileserver/browselisting.go +++ b/modules/caddyhttp/fileserver/browsetplcontext.go @@ -27,7 +27,7 @@ import ( "github.com/dustin/go-humanize" ) -func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseListing { +func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseTemplateContext { filesToHide := fsrv.transformHidePaths(repl) var dirCount, fileCount int @@ -62,7 +62,7 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root }) } - return browseListing{ + return browseTemplateContext{ Name: path.Base(urlPath), Path: urlPath, CanGoUp: canGoUp, @@ -72,7 +72,8 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root } } -type browseListing struct { +// browseTemplateContext provides the template context for directory listings. +type browseTemplateContext struct { // The name of the directory (the last element of the path). Name string `json:"name"` @@ -106,7 +107,7 @@ type browseListing struct { // Breadcrumbs returns l.Path where every element maps // the link to the text to display. -func (l browseListing) Breadcrumbs() []crumb { +func (l browseTemplateContext) Breadcrumbs() []crumb { if len(l.Path) == 0 { return []crumb{} } @@ -130,7 +131,7 @@ func (l browseListing) Breadcrumbs() []crumb { return result } -func (l *browseListing) applySortAndLimit(sortParam, orderParam, limitParam string, offsetParam string) { +func (l *browseTemplateContext) applySortAndLimit(sortParam, orderParam, limitParam string, offsetParam string) { l.Sort = sortParam l.Order = orderParam @@ -207,10 +208,12 @@ func (fi fileInfo) HumanModTime(format string) string { return fi.ModTime.Format(format) } -type byName browseListing -type byNameDirFirst browseListing -type bySize browseListing -type byTime browseListing +type ( + byName browseTemplateContext + byNameDirFirst browseTemplateContext + bySize browseTemplateContext + byTime browseTemplateContext +) func (l byName) Len() int { return len(l.Items) } func (l byName) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } diff --git a/modules/caddyhttp/fileserver/browselisting_test.go b/modules/caddyhttp/fileserver/browsetplcontext_test.go index 6d58b7e..01e6c8f 100644 --- a/modules/caddyhttp/fileserver/browselisting_test.go +++ b/modules/caddyhttp/fileserver/browsetplcontext_test.go @@ -1,3 +1,17 @@ +// Copyright 2015 Matthew Holt and The Caddy Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package fileserver import ( @@ -25,7 +39,7 @@ func TestBreadcrumbs(t *testing.T) { } for _, d := range testdata { - l := browseListing{Path: d.path} + l := browseTemplateContext{Path: d.path} actual := l.Breadcrumbs() if len(actual) != len(d.expected) { t.Errorf("wrong size output, got %d elements but expected %d", len(actual), len(d.expected)) diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index dd44817..f2320aa 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -17,7 +17,6 @@ package fileserver import ( "bytes" "fmt" - "html/template" weakrand "math/rand" "mime" "net/http" @@ -118,23 +117,6 @@ func (fsrv *FileServer) Provision(ctx caddy.Context) error { fsrv.IndexNames = defaultIndexNames } - if fsrv.Browse != nil { - var tpl *template.Template - var err error - if fsrv.Browse.TemplateFile != "" { - tpl, err = template.ParseFiles(fsrv.Browse.TemplateFile) - if err != nil { - return fmt.Errorf("parsing browse template file: %v", err) - } - } else { - tpl, err = template.New("default_listing").Parse(defaultBrowseTemplate) - if err != nil { - return fmt.Errorf("parsing default browse template: %v", err) - } - } - fsrv.Browse.template = tpl - } - // for hide paths that are static (i.e. no placeholders), we can transform them into // absolute paths before the server starts for very slight performance improvement for i, h := range fsrv.Hide { |