Skip to content
Open
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
7 changes: 7 additions & 0 deletions docs/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ userinfo: 'admin:secret'

For more info, check https://golang.org/pkg/net/url/#URL

When the path has a non-default encoding, such as an encoded slash (`%2F`),
the dict also includes `rawPath`. This preserves that encoding when passed to
`urlJoin`; `path` still contains the decoded path.

## urlJoin
Joins map (produced by `urlParse`) to produce URL string

An optional `rawPath` supplies the encoded form of `path`. If it no longer
matches `path` (for example, after editing `path`), the path is escaped normally.

```
urlJoin (dict "fragment" "fragment" "host" "host:80" "path" "/path" "query" "query" "scheme" "http")
```
Expand Down
4 changes: 4 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func urlParse(v string) map[string]interface{} {
dict["host"] = parsedURL.Host
dict["hostname"] = parsedURL.Hostname()
dict["path"] = parsedURL.Path
if parsedURL.RawPath != "" {
dict["rawPath"] = parsedURL.RawPath
}
dict["query"] = parsedURL.RawQuery
dict["opaque"] = parsedURL.Opaque
dict["fragment"] = parsedURL.Fragment
Expand All @@ -47,6 +50,7 @@ func urlJoin(d map[string]interface{}) string {
Scheme: dictGetOrEmpty(d, "scheme"),
Host: dictGetOrEmpty(d, "host"),
Path: dictGetOrEmpty(d, "path"),
RawPath: dictGetOrEmpty(d, "rawPath"),
RawQuery: dictGetOrEmpty(d, "query"),
Opaque: dictGetOrEmpty(d, "opaque"),
Fragment: dictGetOrEmpty(d, "fragment"),
Expand Down
21 changes: 21 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,24 @@ func TestUrlJoin(t *testing.T) {
}

}

func TestUrlParseJoinEscapedPath(t *testing.T) {
for _, input := range []string{
"https://example.com/a%2Fb",
"https://example.com/a%2fb",
"https://example.com/%252F/a%2Fb?key=a%2Fb#fragment",
"/a%2Fb",
} {
t.Run(input, func(t *testing.T) {
parts := urlParse(input)
assert.Equal(t, input, urlJoin(parts))
assert.NoError(t, runtv(`{{ urlParse . | urlJoin }}`, input, input))
})
}
}

func TestUrlJoinUpdatedEscapedPath(t *testing.T) {
parts := urlParse("https://example.com/a%2Fb")
parts["path"] = "/new path"
assert.Equal(t, "https://example.com/new%20path", urlJoin(parts))
}