diff --git a/docs/07.Reference/7.02.Filters.md b/docs/07.Reference/7.02.Filters.md index ebe6d18458..17627e37f0 100644 --- a/docs/07.Reference/7.02.Filters.md +++ b/docs/07.Reference/7.02.Filters.md @@ -2097,6 +2097,9 @@ package, and extra functions defined by Easegress: a URL query, equivalent to the `urlquery` template function. * **urlQueryUnescape**: performs the inverse transformation of `urlQueryEscape`, decoding a URL-encoded string back to its origin form. +* **host**: host splits a network address of the form "host:port" and return host part by using `net.SplitHostPort`. +* **port**: port splits a network address of the form "host:port" and return port part by using `net.SplitHostPort`. + Easegress injects existing requests/responses of the current context into the template engine at runtime, so we can use `.requests..` diff --git a/pkg/filters/builder/extrafuncs.go b/pkg/filters/builder/extrafuncs.go index b78b4e80a6..e014dfed17 100644 --- a/pkg/filters/builder/extrafuncs.go +++ b/pkg/filters/builder/extrafuncs.go @@ -20,6 +20,7 @@ package builder import ( "encoding/json" "fmt" + "net" "net/http" "net/url" "strconv" @@ -156,6 +157,16 @@ var extraFuncs = template.FuncMap{ return username }, + "host": func(hostPort string) string { + addr, _, _ := net.SplitHostPort(hostPort) + return addr + }, + + "port": func(hostPort string) string { + _, port, _ := net.SplitHostPort(hostPort) + return port + }, + "urlQueryUnescape": func(s string) string { unescapedStr, err := url.QueryUnescape(s) if err != nil {