Skip to content

Commit

Permalink
fileserver: browse: do not encode the paths in breadcrumbs and page t…
Browse files Browse the repository at this point in the history
…itle (#4410)
  • Loading branch information
mohammed90 authored Nov 23, 2021
1 parent c8b5a81 commit 1e10f6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 6 additions & 3 deletions modules/caddyhttp/fileserver/browsetplcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
Mode: f.Mode(),
})
}

name, _ := url.PathUnescape(urlPath)
return browseTemplateContext{
Name: path.Base(urlPath),
Name: path.Base(name),
Path: urlPath,
CanGoUp: canGoUp,
Items: fileInfos,
Expand Down Expand Up @@ -133,13 +133,16 @@ func (l browseTemplateContext) Breadcrumbs() []crumb {
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 = "/"
}
// the directory name could include an encoded slash in its path,
// so the item name should be unescaped in the loop rather than unescaping the
// entire path outside the loop.
p, _ = url.PathUnescape(p)
lnk := strings.Repeat("../", len(parts)-i-1)
result[i] = crumb{Link: lnk, Text: p}
}
Expand Down
13 changes: 13 additions & 0 deletions modules/caddyhttp/fileserver/browsetplcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ func TestBreadcrumbs(t *testing.T) {
{Link: "../", Text: "quux"},
{Link: "", Text: "corge"},
}},
{"/مجلد/", []crumb{
{Link: "../", Text: "/"},
{Link: "", Text: "مجلد"},
}},
{"/مجلد-1/مجلد-2", []crumb{
{Link: "../../", Text: "/"},
{Link: "../", Text: "مجلد-1"},
{Link: "", Text: "مجلد-2"},
}},
{"/مجلد%2F1", []crumb{
{Link: "../", Text: "/"},
{Link: "", Text: "مجلد/1"},
}},
}

for _, d := range testdata {
Expand Down

0 comments on commit 1e10f6f

Please sign in to comment.