Skip to content

Commit

Permalink
support host port split of template functions (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
suchen-sci committed Mar 25, 2024
1 parent a88bd7f commit a252e90
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
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

0 comments on commit a252e90

Please sign in to comment.