Skip to content

Commit

Permalink
Merge pull request #12407 from tomponline/tp-url-encoded
Browse files Browse the repository at this point in the history
shared/api/url: Fix double path encoding issue
  • Loading branch information
tomponline authored Oct 18, 2023
2 parents b0099f3 + 144a6c2 commit ca197ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
14 changes: 10 additions & 4 deletions shared/api/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ func (u *URL) Host(host string) *URL {
// Path sets the path of the URL from one or more path parts.
// It appends each of the pathParts (escaped using url.PathEscape) prefixed with "/" to the URL path.
func (u *URL) Path(pathParts ...string) *URL {
var b strings.Builder
var path, rawPath strings.Builder

for _, pathPart := range pathParts {
b.WriteString("/") // Build an absolute URL.
b.WriteString(url.PathEscape(pathPart))
// Generate unencoded path.
path.WriteString("/") // Build an absolute URL.
path.WriteString(pathPart)

// Generate encoded path hint (this will be used by u.URL.EncodedPath() to decide its methodology).
rawPath.WriteString("/") // Build an absolute URL.
rawPath.WriteString(url.PathEscape(pathPart))
}

u.URL.Path = b.String()
u.URL.Path = path.String()
u.URL.RawPath = rawPath.String()

return u
}
Expand Down
14 changes: 7 additions & 7 deletions shared/api/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func ExampleURL() {
fmt.Println(u.Host("example.com"))
fmt.Println(u.Scheme("https"))

// Output: /1.0/networks/name-with-%252F-in-it
// /1.0/networks/name-with-%252F-in-it
// /1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it
// /1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it
// /1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// //example.com/1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// https://example.com/1.0/networks/name-with-%252F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// Output: /1.0/networks/name-with-%2F-in-it
// /1.0/networks/name-with-%2F-in-it
// /1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it
// /1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it
// /1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// //example.com/1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
// https://example.com/1.0/networks/name-with-%2F-in-it?project=project-with-%25-in-it&target=member-with-%25-in-it
}

0 comments on commit ca197ee

Please sign in to comment.