Skip to content

net/url: add JoinPath, URL.JoinPath #50383

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

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions api/next.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ pkg encoding/binary, type AppendByteOrder interface, AppendUint16([]uint8, uint1
pkg encoding/binary, type AppendByteOrder interface, AppendUint32([]uint8, uint32) []uint8
pkg encoding/binary, type AppendByteOrder interface, AppendUint64([]uint8, uint64) []uint8
pkg encoding/binary, type AppendByteOrder interface, String() string
pkg net/url, func JoinPath(string, ...string) (string, error)
pkg net/url, method (*URL) JoinPath(...string) *URL
23 changes: 23 additions & 0 deletions src/net/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package url
import (
"errors"
"fmt"
"path"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1176,6 +1177,17 @@ func (u *URL) UnmarshalBinary(text []byte) error {
return nil
}

// JoinPath returns a new URL with the provided path elements joined to
// any existing path and the resulting path cleaned of any ./ or ../ elements.
func (u *URL) JoinPath(elem ...string) *URL {
url := *u
if len(elem) > 0 {
elem = append([]string{u.Path}, elem...)
url.setPath(path.Join(elem...))
}
return &url
}

// validUserinfo reports whether s is a valid userinfo string per RFC 3986
// Section 3.2.1:
// userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
Expand Down Expand Up @@ -1216,3 +1228,14 @@ func stringContainsCTLByte(s string) bool {
}
return false
}

// JoinPath returns a URL string with the provided path elements joined to
// the existing path of base and the resulting path cleaned of any ./ or ../ elements.
func JoinPath(base string, elem ...string) (result string, err error) {
url, err := Parse(base)
if err != nil {
return
}
result = url.JoinPath(elem...).String()
return
}
56 changes: 56 additions & 0 deletions src/net/url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2062,3 +2062,59 @@ func BenchmarkPathUnescape(b *testing.B) {
})
}
}

func TestJoinPath(t *testing.T) {
tests := []struct {
base string
elem []string
out string
}{
{
base: "https://go.googlesource.com",
elem: []string{"go"},
out: "https://go.googlesource.com/go",
},
{
base: "https://go.googlesource.com/a/b/c",
elem: []string{"../../../go"},
out: "https://go.googlesource.com/go",
},
{
base: "https://go.googlesource.com/",
elem: []string{"./go"},
out: "https://go.googlesource.com/go",
},
{
base: "https://go.googlesource.com//",
elem: []string{"/go"},
out: "https://go.googlesource.com/go",
},
{
base: "https://go.googlesource.com//",
elem: []string{"/go", "a", "b", "c"},
out: "https://go.googlesource.com/go/a/b/c",
},
{
base: "http://[fe80::1%en0]:8080/",
elem: []string{"/go"},
},
}
for _, tt := range tests {
wantErr := "nil"
if tt.out == "" {
wantErr = "non-nil error"
}
if out, err := JoinPath(tt.base, tt.elem...); out != tt.out || (err == nil) != (tt.out != "") {
t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
}
var out string
u, err := Parse(tt.base)
if err == nil {
u = u.JoinPath(tt.elem...)
out = u.String()
}
if out != tt.out || (err == nil) != (tt.out != "") {
t.Errorf("Parse(%q).JoinPath(%q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
}
}
}