Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 01:30 UTC
fileserver: browse: do not encode the paths in breadcrumbs and page title (#4410)
Mohammed Al Sahaf committed
Nov 23, 2021 at 11:13 UTC
1e10f6f725189a371a923a329084f1c3f608ae38
2 files changed
+19
-3
modules/caddyhttp/fileserver/browsetplcontext.go
+6
-3
@@ -77,9 +77,9 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root
77
Mode: f.Mode(),
78
})
79
}
80
-
80
+ name, _ := url.PathUnescape(urlPath)
81
return browseTemplateContext{
82
- Name: path.Base(urlPath),
82
+ Name: path.Base(name),
83
Path: urlPath,
84
CanGoUp: canGoUp,
85
Items: fileInfos,
@@ -133,13 +133,16 @@ func (l browseTemplateContext) Breadcrumbs() []crumb {
133
if lpath[len(lpath)-1] == '/' {
134
lpath = lpath[:len(lpath)-1]
135
}
136
-
136
parts := strings.Split(lpath, "/")
137
result := make([]crumb, len(parts))
138
for i, p := range parts {
139
if i == 0 && p == "" {
140
p = "/"
141
}
142
+ // the directory name could include an encoded slash in its path,
143
+ // so the item name should be unescaped in the loop rather than unescaping the
144
+ // entire path outside the loop.
145
+ p, _ = url.PathUnescape(p)
146
lnk := strings.Repeat("../", len(parts)-i-1)
147
result[i] = crumb{Link: lnk, Text: p}
148
}
modules/caddyhttp/fileserver/browsetplcontext_test.go
+13
@@ -36,6 +36,19 @@ func TestBreadcrumbs(t *testing.T) {
36
{Link: "../", Text: "quux"},
37
{Link: "", Text: "corge"},
38
}},
39
+ {"/مجلد/", []crumb{
40
+ {Link: "../", Text: "/"},
41
+ {Link: "", Text: "مجلد"},
42
+ }},
43
+ {"/مجلد-1/مجلد-2", []crumb{
44
+ {Link: "../../", Text: "/"},
45
+ {Link: "../", Text: "مجلد-1"},
46
+ {Link: "", Text: "مجلد-2"},
47
+ }},
48
+ {"/مجلد%2F1", []crumb{
49
+ {Link: "../", Text: "/"},
50
+ {Link: "", Text: "مجلد/1"},
51
+ }},
52
}
53
54
for _, d := range testdata {