From 637fd8f67b7ff6191673ff13e94825a4ec896891 Mon Sep 17 00:00:00 2001 From: Jason Du Date: Fri, 30 Apr 2021 19:17:23 -0700 Subject: fileserver: Share template logic for both `templates` and `file_server browse` (#4093) Co-authored-by: Matthew Holt --- modules/caddyhttp/fileserver/browse.go | 73 +++++- modules/caddyhttp/fileserver/browse_test.go | 30 ++- modules/caddyhttp/fileserver/browselisting.go | 267 -------------------- modules/caddyhttp/fileserver/browselisting_test.go | 40 --- modules/caddyhttp/fileserver/browsetplcontext.go | 270 +++++++++++++++++++++ .../caddyhttp/fileserver/browsetplcontext_test.go | 54 +++++ modules/caddyhttp/fileserver/staticfiles.go | 18 -- modules/caddyhttp/templates/templates.go | 6 +- modules/caddyhttp/templates/tplcontext.go | 98 ++++---- modules/caddyhttp/templates/tplcontext_test.go | 24 +- 10 files changed, 472 insertions(+), 408 deletions(-) delete mode 100644 modules/caddyhttp/fileserver/browselisting.go delete mode 100644 modules/caddyhttp/fileserver/browselisting_test.go create mode 100644 modules/caddyhttp/fileserver/browsetplcontext.go create mode 100644 modules/caddyhttp/fileserver/browsetplcontext_test.go (limited to 'modules') 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/browselisting.go deleted file mode 100644 index 2b8c66c..0000000 --- a/modules/caddyhttp/fileserver/browselisting.go +++ /dev/null @@ -1,267 +0,0 @@ -// 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 ( - "net/url" - "os" - "path" - "sort" - "strconv" - "strings" - "time" - - "github.com/caddyserver/caddy/v2" - "github.com/dustin/go-humanize" -) - -func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseListing { - filesToHide := fsrv.transformHidePaths(repl) - - var dirCount, fileCount int - fileInfos := []fileInfo{} - - for _, f := range files { - name := f.Name() - - if fileHidden(name, filesToHide) { - continue - } - - isDir := f.IsDir() || isSymlinkTargetDir(f, root, urlPath) - - if isDir { - name += "/" - dirCount++ - } else { - fileCount++ - } - - u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name - - fileInfos = append(fileInfos, fileInfo{ - IsDir: isDir, - IsSymlink: isSymlink(f), - Name: f.Name(), - Size: f.Size(), - URL: u.String(), - ModTime: f.ModTime().UTC(), - Mode: f.Mode(), - }) - } - - return browseListing{ - Name: path.Base(urlPath), - Path: urlPath, - CanGoUp: canGoUp, - Items: fileInfos, - NumDirs: dirCount, - NumFiles: fileCount, - } -} - -type browseListing struct { - // The name of the directory (the last element of the path). - Name string `json:"name"` - - // The full path of the request. - Path string `json:"path"` - - // Whether the parent directory is browseable. - CanGoUp bool `json:"can_go_up"` - - // The items (files and folders) in the path. - Items []fileInfo `json:"items,omitempty"` - - // If ≠0 then Items starting from that many elements. - Offset int `json:"offset,omitempty"` - - // If ≠0 then Items have been limited to that many elements. - Limit int `json:"limit,omitempty"` - - // The number of directories in the listing. - NumDirs int `json:"num_dirs"` - - // The number of files (items that aren't directories) in the listing. - NumFiles int `json:"num_files"` - - // Sort column used - Sort string `json:"sort,omitempty"` - - // Sorting order - Order string `json:"order,omitempty"` -} - -// Breadcrumbs returns l.Path where every element maps -// the link to the text to display. -func (l browseListing) Breadcrumbs() []crumb { - if len(l.Path) == 0 { - return []crumb{} - } - - // skip trailing slash - lpath := l.Path - if lpath[len(lpath)-1] == '/' { - lpath = lpath[:len(lpath)-1] - } - - parts := strings.Split(lpath, "/") - result := make([]crumb, len(parts)) - for i, p := range parts { - if i == 0 && p == "" { - p = "/" - } - lnk := strings.Repeat("../", len(parts)-i-1) - result[i] = crumb{Link: lnk, Text: p} - } - - return result -} - -func (l *browseListing) applySortAndLimit(sortParam, orderParam, limitParam string, offsetParam string) { - l.Sort = sortParam - l.Order = orderParam - - if l.Order == "desc" { - switch l.Sort { - case sortByName: - sort.Sort(sort.Reverse(byName(*l))) - case sortByNameDirFirst: - sort.Sort(sort.Reverse(byNameDirFirst(*l))) - case sortBySize: - sort.Sort(sort.Reverse(bySize(*l))) - case sortByTime: - sort.Sort(sort.Reverse(byTime(*l))) - } - } else { - switch l.Sort { - case sortByName: - sort.Sort(byName(*l)) - case sortByNameDirFirst: - sort.Sort(byNameDirFirst(*l)) - case sortBySize: - sort.Sort(bySize(*l)) - case sortByTime: - sort.Sort(byTime(*l)) - } - } - - if offsetParam != "" { - offset, _ := strconv.Atoi(offsetParam) - if offset > 0 && offset <= len(l.Items) { - l.Items = l.Items[offset:] - l.Offset = offset - } - } - - if limitParam != "" { - limit, _ := strconv.Atoi(limitParam) - - if limit > 0 && limit <= len(l.Items) { - l.Items = l.Items[:limit] - l.Limit = limit - } - } -} - -// crumb represents part of a breadcrumb menu, -// pairing a link with the text to display. -type crumb struct { - Link, Text string -} - -// fileInfo contains serializable information -// about a file or directory. -type fileInfo struct { - Name string `json:"name"` - Size int64 `json:"size"` - URL string `json:"url"` - ModTime time.Time `json:"mod_time"` - Mode os.FileMode `json:"mode"` - IsDir bool `json:"is_dir"` - IsSymlink bool `json:"is_symlink"` -} - -// HumanSize returns the size of the file as a -// human-readable string in IEC format (i.e. -// power of 2 or base 1024). -func (fi fileInfo) HumanSize() string { - return humanize.IBytes(uint64(fi.Size)) -} - -// HumanModTime returns the modified time of the file -// as a human-readable string given by format. -func (fi fileInfo) HumanModTime(format string) string { - return fi.ModTime.Format(format) -} - -type byName browseListing -type byNameDirFirst browseListing -type bySize browseListing -type byTime browseListing - -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] } - -func (l byName) Less(i, j int) bool { - return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) -} - -func (l byNameDirFirst) Len() int { return len(l.Items) } -func (l byNameDirFirst) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } - -func (l byNameDirFirst) Less(i, j int) bool { - // sort by name if both are dir or file - if l.Items[i].IsDir == l.Items[j].IsDir { - return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) - } - // sort dir ahead of file - return l.Items[i].IsDir -} - -func (l bySize) Len() int { return len(l.Items) } -func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } - -func (l bySize) Less(i, j int) bool { - const directoryOffset = -1 << 31 // = -math.MinInt32 - - iSize, jSize := l.Items[i].Size, l.Items[j].Size - - // directory sizes depend on the file system; to - // provide a consistent experience, put them up front - // and sort them by name - if l.Items[i].IsDir { - iSize = directoryOffset - } - if l.Items[j].IsDir { - jSize = directoryOffset - } - if l.Items[i].IsDir && l.Items[j].IsDir { - return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) - } - - return iSize < jSize -} - -func (l byTime) Len() int { return len(l.Items) } -func (l byTime) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } -func (l byTime) Less(i, j int) bool { return l.Items[i].ModTime.Before(l.Items[j].ModTime) } - -const ( - sortByName = "name" - sortByNameDirFirst = "name_dir_first" - sortBySize = "size" - sortByTime = "time" -) diff --git a/modules/caddyhttp/fileserver/browselisting_test.go b/modules/caddyhttp/fileserver/browselisting_test.go deleted file mode 100644 index 6d58b7e..0000000 --- a/modules/caddyhttp/fileserver/browselisting_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package fileserver - -import ( - "testing" -) - -func TestBreadcrumbs(t *testing.T) { - testdata := []struct { - path string - expected []crumb - }{ - {"", []crumb{}}, - {"/", []crumb{{Text: "/"}}}, - {"foo/bar/baz", []crumb{ - {Link: "../../", Text: "foo"}, - {Link: "../", Text: "bar"}, - {Link: "", Text: "baz"}, - }}, - {"/qux/quux/corge/", []crumb{ - {Link: "../../../", Text: "/"}, - {Link: "../../", Text: "qux"}, - {Link: "../", Text: "quux"}, - {Link: "", Text: "corge"}, - }}, - } - - for _, d := range testdata { - l := browseListing{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)) - continue - } - for i, c := range actual { - if c != d.expected[i] { - t.Errorf("got %#v but expected %#v at index %d", c, d.expected[i], i) - } - } - } -} diff --git a/modules/caddyhttp/fileserver/browsetplcontext.go b/modules/caddyhttp/fileserver/browsetplcontext.go new file mode 100644 index 0000000..489e8c0 --- /dev/null +++ b/modules/caddyhttp/fileserver/browsetplcontext.go @@ -0,0 +1,270 @@ +// 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 ( + "net/url" + "os" + "path" + "sort" + "strconv" + "strings" + "time" + + "github.com/caddyserver/caddy/v2" + "github.com/dustin/go-humanize" +) + +func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseTemplateContext { + filesToHide := fsrv.transformHidePaths(repl) + + var dirCount, fileCount int + fileInfos := []fileInfo{} + + for _, f := range files { + name := f.Name() + + if fileHidden(name, filesToHide) { + continue + } + + isDir := f.IsDir() || isSymlinkTargetDir(f, root, urlPath) + + if isDir { + name += "/" + dirCount++ + } else { + fileCount++ + } + + u := url.URL{Path: "./" + name} // prepend with "./" to fix paths with ':' in the name + + fileInfos = append(fileInfos, fileInfo{ + IsDir: isDir, + IsSymlink: isSymlink(f), + Name: f.Name(), + Size: f.Size(), + URL: u.String(), + ModTime: f.ModTime().UTC(), + Mode: f.Mode(), + }) + } + + return browseTemplateContext{ + Name: path.Base(urlPath), + Path: urlPath, + CanGoUp: canGoUp, + Items: fileInfos, + NumDirs: dirCount, + NumFiles: fileCount, + } +} + +// 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"` + + // The full path of the request. + Path string `json:"path"` + + // Whether the parent directory is browseable. + CanGoUp bool `json:"can_go_up"` + + // The items (files and folders) in the path. + Items []fileInfo `json:"items,omitempty"` + + // If ≠0 then Items starting from that many elements. + Offset int `json:"offset,omitempty"` + + // If ≠0 then Items have been limited to that many elements. + Limit int `json:"limit,omitempty"` + + // The number of directories in the listing. + NumDirs int `json:"num_dirs"` + + // The number of files (items that aren't directories) in the listing. + NumFiles int `json:"num_files"` + + // Sort column used + Sort string `json:"sort,omitempty"` + + // Sorting order + Order string `json:"order,omitempty"` +} + +// Breadcrumbs returns l.Path where every element maps +// the link to the text to display. +func (l browseTemplateContext) Breadcrumbs() []crumb { + if len(l.Path) == 0 { + return []crumb{} + } + + // skip trailing slash + lpath := l.Path + if lpath[len(lpath)-1] == '/' { + lpath = lpath[:len(lpath)-1] + } + + parts := strings.Split(lpath, "/") + result := make([]crumb, len(parts)) + for i, p := range parts { + if i == 0 && p == "" { + p = "/" + } + lnk := strings.Repeat("../", len(parts)-i-1) + result[i] = crumb{Link: lnk, Text: p} + } + + return result +} + +func (l *browseTemplateContext) applySortAndLimit(sortParam, orderParam, limitParam string, offsetParam string) { + l.Sort = sortParam + l.Order = orderParam + + if l.Order == "desc" { + switch l.Sort { + case sortByName: + sort.Sort(sort.Reverse(byName(*l))) + case sortByNameDirFirst: + sort.Sort(sort.Reverse(byNameDirFirst(*l))) + case sortBySize: + sort.Sort(sort.Reverse(bySize(*l))) + case sortByTime: + sort.Sort(sort.Reverse(byTime(*l))) + } + } else { + switch l.Sort { + case sortByName: + sort.Sort(byName(*l)) + case sortByNameDirFirst: + sort.Sort(byNameDirFirst(*l)) + case sortBySize: + sort.Sort(bySize(*l)) + case sortByTime: + sort.Sort(byTime(*l)) + } + } + + if offsetParam != "" { + offset, _ := strconv.Atoi(offsetParam) + if offset > 0 && offset <= len(l.Items) { + l.Items = l.Items[offset:] + l.Offset = offset + } + } + + if limitParam != "" { + limit, _ := strconv.Atoi(limitParam) + + if limit > 0 && limit <= len(l.Items) { + l.Items = l.Items[:limit] + l.Limit = limit + } + } +} + +// crumb represents part of a breadcrumb menu, +// pairing a link with the text to display. +type crumb struct { + Link, Text string +} + +// fileInfo contains serializable information +// about a file or directory. +type fileInfo struct { + Name string `json:"name"` + Size int64 `json:"size"` + URL string `json:"url"` + ModTime time.Time `json:"mod_time"` + Mode os.FileMode `json:"mode"` + IsDir bool `json:"is_dir"` + IsSymlink bool `json:"is_symlink"` +} + +// HumanSize returns the size of the file as a +// human-readable string in IEC format (i.e. +// power of 2 or base 1024). +func (fi fileInfo) HumanSize() string { + return humanize.IBytes(uint64(fi.Size)) +} + +// HumanModTime returns the modified time of the file +// as a human-readable string given by format. +func (fi fileInfo) HumanModTime(format string) string { + return fi.ModTime.Format(format) +} + +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] } + +func (l byName) Less(i, j int) bool { + return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) +} + +func (l byNameDirFirst) Len() int { return len(l.Items) } +func (l byNameDirFirst) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } + +func (l byNameDirFirst) Less(i, j int) bool { + // sort by name if both are dir or file + if l.Items[i].IsDir == l.Items[j].IsDir { + return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) + } + // sort dir ahead of file + return l.Items[i].IsDir +} + +func (l bySize) Len() int { return len(l.Items) } +func (l bySize) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } + +func (l bySize) Less(i, j int) bool { + const directoryOffset = -1 << 31 // = -math.MinInt32 + + iSize, jSize := l.Items[i].Size, l.Items[j].Size + + // directory sizes depend on the file system; to + // provide a consistent experience, put them up front + // and sort them by name + if l.Items[i].IsDir { + iSize = directoryOffset + } + if l.Items[j].IsDir { + jSize = directoryOffset + } + if l.Items[i].IsDir && l.Items[j].IsDir { + return strings.ToLower(l.Items[i].Name) < strings.ToLower(l.Items[j].Name) + } + + return iSize < jSize +} + +func (l byTime) Len() int { return len(l.Items) } +func (l byTime) Swap(i, j int) { l.Items[i], l.Items[j] = l.Items[j], l.Items[i] } +func (l byTime) Less(i, j int) bool { return l.Items[i].ModTime.Before(l.Items[j].ModTime) } + +const ( + sortByName = "name" + sortByNameDirFirst = "name_dir_first" + sortBySize = "size" + sortByTime = "time" +) diff --git a/modules/caddyhttp/fileserver/browsetplcontext_test.go b/modules/caddyhttp/fileserver/browsetplcontext_test.go new file mode 100644 index 0000000..01e6c8f --- /dev/null +++ b/modules/caddyhttp/fileserver/browsetplcontext_test.go @@ -0,0 +1,54 @@ +// 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 ( + "testing" +) + +func TestBreadcrumbs(t *testing.T) { + testdata := []struct { + path string + expected []crumb + }{ + {"", []crumb{}}, + {"/", []crumb{{Text: "/"}}}, + {"foo/bar/baz", []crumb{ + {Link: "../../", Text: "foo"}, + {Link: "../", Text: "bar"}, + {Link: "", Text: "baz"}, + }}, + {"/qux/quux/corge/", []crumb{ + {Link: "../../../", Text: "/"}, + {Link: "../../", Text: "qux"}, + {Link: "../", Text: "quux"}, + {Link: "", Text: "corge"}, + }}, + } + + for _, d := range testdata { + 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)) + continue + } + for i, c := range actual { + if c != d.expected[i] { + t.Errorf("got %#v but expected %#v at index %d", c, d.expected[i], i) + } + } + } +} 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 { diff --git a/modules/caddyhttp/templates/templates.go b/modules/caddyhttp/templates/templates.go index f9ce1c3..33e5a70 100644 --- a/modules/caddyhttp/templates/templates.go +++ b/modules/caddyhttp/templates/templates.go @@ -37,7 +37,7 @@ func init() { // // [All Sprig functions](https://masterminds.github.io/sprig/) are supported. // -// In addition to the standard functions and Sprig functions, Caddy adds +// In addition to the standard functions and the Sprig library, Caddy adds // extra functions and data that are available to a template: // // ##### `.Args` @@ -303,10 +303,10 @@ func (t *Templates) executeTemplate(rr caddyhttp.ResponseRecorder, r *http.Reque fs = http.Dir(repl.ReplaceAll(t.FileRoot, ".")) } - ctx := &templateContext{ + ctx := &TemplateContext{ Root: fs, Req: r, - RespHeader: tplWrappedHeader{rr.Header()}, + RespHeader: WrappedHeader{rr.Header()}, config: t, } diff --git a/modules/caddyhttp/templates/tplcontext.go b/modules/caddyhttp/templates/tplcontext.go index 1212ee2..1a145a2 100644 --- a/modules/caddyhttp/templates/tplcontext.go +++ b/modules/caddyhttp/templates/tplcontext.go @@ -38,19 +38,49 @@ import ( gmhtml "github.com/yuin/goldmark/renderer/html" ) -// templateContext is the templateContext with which HTTP templates are executed. -type templateContext struct { +// TemplateContext is the TemplateContext with which HTTP templates are executed. +type TemplateContext struct { Root http.FileSystem Req *http.Request Args []interface{} // defined by arguments to funcInclude - RespHeader tplWrappedHeader + RespHeader WrappedHeader config *Templates } +// NewTemplate returns a new template intended to be evaluated with this +// context, as it is initialized with configuration from this context. +func (c TemplateContext) NewTemplate(tplName string) *template.Template { + tpl := template.New(tplName) + + // customize delimiters, if applicable + if c.config != nil && len(c.config.Delimiters) == 2 { + tpl.Delims(c.config.Delimiters[0], c.config.Delimiters[1]) + } + + // add sprig library + tpl.Funcs(sprigFuncMap) + + // add our own library + tpl.Funcs(template.FuncMap{ + "include": c.funcInclude, + "httpInclude": c.funcHTTPInclude, + "stripHTML": c.funcStripHTML, + "markdown": c.funcMarkdown, + "splitFrontMatter": c.funcSplitFrontMatter, + "listFiles": c.funcListFiles, + "env": c.funcEnv, + "placeholder": c.funcPlaceholder, + "fileExists": c.funcFileExists, + "httpError": c.funcHTTPError, + }) + + return tpl +} + // OriginalReq returns the original, unmodified, un-rewritten request as // it originally came in over the wire. -func (c templateContext) OriginalReq() http.Request { +func (c TemplateContext) OriginalReq() http.Request { or, _ := c.Req.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request) return or } @@ -59,7 +89,7 @@ func (c templateContext) OriginalReq() http.Request { // Note that included files are NOT escaped, so you should only include // trusted files. If it is not trusted, be sure to use escaping functions // in your template. -func (c templateContext) funcInclude(filename string, args ...interface{}) (string, error) { +func (c TemplateContext) funcInclude(filename string, args ...interface{}) (string, error) { if c.Root == nil { return "", fmt.Errorf("root file system not specified") } @@ -93,7 +123,7 @@ func (c templateContext) funcInclude(filename string, args ...interface{}) (stri // to the given URI on the same server. Note that included bodies // are NOT escaped, so you should only include trusted resources. // If it is not trusted, be sure to use escaping functions yourself. -func (c templateContext) funcHTTPInclude(uri string) (string, error) { +func (c TemplateContext) funcHTTPInclude(uri string) (string, error) { // prevent virtual request loops by counting how many levels // deep we are; and if we get too deep, return an error recursionCount := 1 @@ -137,26 +167,8 @@ func (c templateContext) funcHTTPInclude(uri string) (string, error) { return buf.String(), nil } -func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buffer) error { - tpl := template.New(tplName) - if len(c.config.Delimiters) == 2 { - tpl.Delims(c.config.Delimiters[0], c.config.Delimiters[1]) - } - - tpl.Funcs(sprigFuncMap) - - tpl.Funcs(template.FuncMap{ - "include": c.funcInclude, - "httpInclude": c.funcHTTPInclude, - "stripHTML": c.funcStripHTML, - "markdown": c.funcMarkdown, - "splitFrontMatter": c.funcSplitFrontMatter, - "listFiles": c.funcListFiles, - "env": c.funcEnv, - "placeholder": c.funcPlaceholder, - "fileExists": c.funcFileExists, - "httpError": c.funcHTTPError, - }) +func (c TemplateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buffer) error { + tpl := c.NewTemplate(tplName) parsedTpl, err := tpl.Parse(buf.String()) if err != nil { @@ -168,18 +180,18 @@ func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buff return parsedTpl.Execute(buf, c) } -func (c templateContext) funcPlaceholder(name string) string { +func (c TemplateContext) funcPlaceholder(name string) string { repl := c.Req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) value, _ := repl.GetString(name) return value } -func (templateContext) funcEnv(varName string) string { +func (TemplateContext) funcEnv(varName string) string { return os.Getenv(varName) } // Cookie gets the value of a cookie with name name. -func (c templateContext) Cookie(name string) string { +func (c TemplateContext) Cookie(name string) string { cookies := c.Req.Cookies() for _, cookie := range cookies { if cookie.Name == name { @@ -190,7 +202,7 @@ func (c templateContext) Cookie(name string) string { } // RemoteIP gets the IP address of the client making the request. -func (c templateContext) RemoteIP() string { +func (c TemplateContext) RemoteIP() string { ip, _, err := net.SplitHostPort(c.Req.RemoteAddr) if err != nil { return c.Req.RemoteAddr @@ -200,7 +212,7 @@ func (c templateContext) RemoteIP() string { // Host returns the hostname portion of the Host header // from the HTTP request. -func (c templateContext) Host() (string, error) { +func (c TemplateContext) Host() (string, error) { host, _, err := net.SplitHostPort(c.Req.Host) if err != nil { if !strings.Contains(c.Req.Host, ":") { @@ -214,7 +226,7 @@ func (c templateContext) Host() (string, error) { // funcStripHTML returns s without HTML tags. It is fairly naive // but works with most valid HTML inputs. -func (templateContext) funcStripHTML(s string) string { +func (TemplateContext) funcStripHTML(s string) string { var buf bytes.Buffer var inTag, inQuotes bool var tagStart int @@ -247,7 +259,7 @@ func (templateContext) funcStripHTML(s string) string { // funcMarkdown renders the markdown body as HTML. The resulting // HTML is NOT escaped so that it can be rendered as HTML. -func (templateContext) funcMarkdown(input interface{}) (string, error) { +func (TemplateContext) funcMarkdown(input interface{}) (string, error) { inputStr := toString(input) md := goldmark.New( @@ -283,7 +295,7 @@ func (templateContext) funcMarkdown(input interface{}) (string, error) { // splitFrontMatter parses front matter out from the beginning of input, // and returns the separated key-value pairs and the body/content. input // must be a "stringy" value. -func (templateContext) funcSplitFrontMatter(input interface{}) (parsedMarkdownDoc, error) { +func (TemplateContext) funcSplitFrontMatter(input interface{}) (parsedMarkdownDoc, error) { meta, body, err := extractFrontMatter(toString(input)) if err != nil { return parsedMarkdownDoc{}, err @@ -293,7 +305,7 @@ func (templateContext) funcSplitFrontMatter(input interface{}) (parsedMarkdownDo // funcListFiles reads and returns a slice of names from the given // directory relative to the root of c. -func (c templateContext) funcListFiles(name string) ([]string, error) { +func (c TemplateContext) funcListFiles(name string) ([]string, error) { if c.Root == nil { return nil, fmt.Errorf("root file system not specified") } @@ -326,7 +338,7 @@ func (c templateContext) funcListFiles(name string) ([]string, error) { } // funcFileExists returns true if filename can be opened successfully. -func (c templateContext) funcFileExists(filename string) (bool, error) { +func (c TemplateContext) funcFileExists(filename string) (bool, error) { if c.Root == nil { return false, fmt.Errorf("root file system not specified") } @@ -339,21 +351,21 @@ func (c templateContext) funcFileExists(filename string) (bool, error) { } // funcHTTPError returns a structured HTTP handler error. EXPERIMENTAL. -// TODO: Requires https://github.com/golang/go/issues/34201 to be fixed. +// TODO: Requires https://github.com/golang/go/issues/34201 to be fixed (Go 1.17). // Example usage might be: `{{if not (fileExists $includeFile)}}{{httpError 404}}{{end}}` -func (c templateContext) funcHTTPError(statusCode int) (bool, error) { +func (c TemplateContext) funcHTTPError(statusCode int) (bool, error) { return false, caddyhttp.Error(statusCode, nil) } -// tplWrappedHeader wraps niladic functions so that they +// WrappedHeader wraps niladic functions so that they // can be used in templates. (Template functions must // return a value.) -type tplWrappedHeader struct{ http.Header } +type WrappedHeader struct{ http.Header } // Add adds a header field value, appending val to // existing values for that field. It returns an // empty string. -func (h tplWrappedHeader) Add(field, val string) string { +func (h WrappedHeader) Add(field, val string) string { h.Header.Add(field, val) return "" } @@ -361,13 +373,13 @@ func (h tplWrappedHeader) Add(field, val string) string { // Set sets a header field value, overwriting any // other values for that field. It returns an // empty string. -func (h tplWrappedHeader) Set(field, val string) string { +func (h WrappedHeader) Set(field, val string) string { h.Header.Set(field, val) return "" } // Del deletes a header field. It returns an empty string. -func (h tplWrappedHeader) Del(field string) string { +func (h WrappedHeader) Del(field string) string { h.Header.Del(field) return "" } diff --git a/modules/caddyhttp/templates/tplcontext_test.go b/modules/caddyhttp/templates/tplcontext_test.go index c0658a1..efee3be 100644 --- a/modules/caddyhttp/templates/tplcontext_test.go +++ b/modules/caddyhttp/templates/tplcontext_test.go @@ -12,20 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Copyright 2015 Light Code Labs, LLC -// -// 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 templates import ( @@ -357,7 +343,7 @@ title = "Welcome" } -func getContextOrFail(t *testing.T) templateContext { +func getContextOrFail(t *testing.T) TemplateContext { context, err := initTestContext() if err != nil { t.Fatalf("failed to prepare test context: %v", err) @@ -365,15 +351,15 @@ func getContextOrFail(t *testing.T) templateContext { return context } -func initTestContext() (templateContext, error) { +func initTestContext() (TemplateContext, error) { body := bytes.NewBufferString("request body") request, err := http.NewRequest("GET", "https://example.com/foo/bar", body) if err != nil { - return templateContext{}, err + return TemplateContext{}, err } - return templateContext{ + return TemplateContext{ Root: http.Dir(os.TempDir()), Req: request, - RespHeader: tplWrappedHeader{make(http.Header)}, + RespHeader: WrappedHeader{make(http.Header)}, }, nil } -- cgit v1.2.3