Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support host port split of template functions #1244

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/07.Reference/7.02.Filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<namespace>.<field>`
Expand Down
11 changes: 11 additions & 0 deletions pkg/filters/builder/extrafuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package builder
import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -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 {
Expand Down
Loading