@marcel91200 / caddy Mirror · read-only / commits / f98f449f
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 12:00 UTC

templates: Add `pathEscape` template function and use it in file browser (#6278)

* use url.PathEscape in file-server browse template - add `pathEscape` to c.tpl.Funcs, using `url.PathEscape` - use `pathEscape` in browse.html in place of `replace` * document `pathEscape` * Remove unnecessary pipe of img src to `html`

deneb committed May 18, 2024 at 20:55 UTC f98f449f05c251da2b25fe96ffb73319b4a7af76
3 files changed +15 -1
modules/caddyhttp/fileserver/browse.html
+1 -1
@@ -21,7 +21,7 @@
21 </svg>
22 {{- else if .HasExt ".jpg" ".jpeg" ".png" ".gif" ".webp" ".tiff" ".bmp" ".heif" ".heic" ".svg"}}
23 {{- if eq .Tpl.Layout "grid"}}
24 - <img loading="lazy" src="{{.Name | replace "#" "%23" | replace "?" "%3f" | html}}">
24 + <img loading="lazy" src="{{.Name | pathEscape}}">
25 {{- else}}
26 <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-photo" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
27 <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
modules/caddyhttp/templates/templates.go
+12
@@ -300,6 +300,18 @@ func init() {
300 // find the documentation on time layouts [in Go's docs](https://pkg.go.dev/time#pkg-constants).
301 // The default time layout is `RFC1123Z`, i.e. `Mon, 02 Jan 2006 15:04:05 -0700`.
302 //
303 +// ##### `pathEscape`
304 +//
305 +// Passes a string through `url.PathEscape`, replacing characters that have
306 +// special meaning in URL path parameters (`?`, `&`, `%`).
307 +//
308 +// Useful e.g. to include filenames containing these characters in URL path
309 +// parameters, or use them as an `img` element's `src` attribute.
310 +//
311 +// ```
312 +// {{pathEscape "50%_valid_filename?.jpg"}}
313 +// ```
314 +//
315 // ```
316 // {{humanize "size" "2048000"}}
317 // {{placeholder "http.response.header.Content-Length" | humanize "size"}}
modules/caddyhttp/templates/tplcontext.go
+2
@@ -21,6 +21,7 @@ import (
21 "io/fs"
22 "net"
23 "net/http"
24 + "net/url"
25 "os"
26 "path"
27 "reflect"
@@ -91,6 +92,7 @@ func (c *TemplateContext) NewTemplate(tplName string) *template.Template {
92 "httpError": c.funcHTTPError,
93 "humanize": c.funcHumanize,
94 "maybe": c.funcMaybe,
95 + "pathEscape": url.PathEscape,
96 })
97 return c.tpl
98 }