summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authordeneb <[email protected]>2024-05-18 20:55:36 +0200
committerGitHub <[email protected]>2024-05-18 12:55:36 -0600
commitf98f449f05c251da2b25fe96ffb73319b4a7af76 (patch)
treeedf5b73b310cd24f8a4b102a140c02ef7b45c710
parente66040a6f0b384d9cebd38a78f746f08f4cb22c1 (diff)
downloadcaddy-f98f449f05c251da2b25fe96ffb73319b4a7af76.tar.gz
caddy-f98f449f05c251da2b25fe96ffb73319b4a7af76.zip
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`
-rw-r--r--modules/caddyhttp/fileserver/browse.html2
-rw-r--r--modules/caddyhttp/templates/templates.go12
-rw-r--r--modules/caddyhttp/templates/tplcontext.go2
3 files changed, 15 insertions, 1 deletions
diff --git a/modules/caddyhttp/fileserver/browse.html b/modules/caddyhttp/fileserver/browse.html
index fdc4b9c3c..7b0df1e5f 100644
--- a/modules/caddyhttp/fileserver/browse.html
+++ b/modules/caddyhttp/fileserver/browse.html
@@ -21,7 +21,7 @@
</svg>
{{- else if .HasExt ".jpg" ".jpeg" ".png" ".gif" ".webp" ".tiff" ".bmp" ".heif" ".heic" ".svg"}}
{{- if eq .Tpl.Layout "grid"}}
- <img loading="lazy" src="{{.Name | replace "#" "%23" | replace "?" "%3f" | html}}">
+ <img loading="lazy" src="{{.Name | pathEscape}}">
{{- else}}
<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">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
diff --git a/modules/caddyhttp/templates/templates.go b/modules/caddyhttp/templates/templates.go
index 539666076..0eba4870f 100644
--- a/modules/caddyhttp/templates/templates.go
+++ b/modules/caddyhttp/templates/templates.go
@@ -300,6 +300,18 @@ func init() {
// find the documentation on time layouts [in Go's docs](https://pkg.go.dev/time#pkg-constants).
// The default time layout is `RFC1123Z`, i.e. `Mon, 02 Jan 2006 15:04:05 -0700`.
//
+// ##### `pathEscape`
+//
+// Passes a string through `url.PathEscape`, replacing characters that have
+// special meaning in URL path parameters (`?`, `&`, `%`).
+//
+// Useful e.g. to include filenames containing these characters in URL path
+// parameters, or use them as an `img` element's `src` attribute.
+//
+// ```
+// {{pathEscape "50%_valid_filename?.jpg"}}
+// ```
+//
// ```
// {{humanize "size" "2048000"}}
// {{placeholder "http.response.header.Content-Length" | humanize "size"}}
diff --git a/modules/caddyhttp/templates/tplcontext.go b/modules/caddyhttp/templates/tplcontext.go
index 4c7c86e13..2324586be 100644
--- a/modules/caddyhttp/templates/tplcontext.go
+++ b/modules/caddyhttp/templates/tplcontext.go
@@ -21,6 +21,7 @@ import (
"io/fs"
"net"
"net/http"
+ "net/url"
"os"
"path"
"reflect"
@@ -91,6 +92,7 @@ func (c *TemplateContext) NewTemplate(tplName string) *template.Template {
"httpError": c.funcHTTPError,
"humanize": c.funcHumanize,
"maybe": c.funcMaybe,
+ "pathEscape": url.PathEscape,
})
return c.tpl
}