Gerrit Dou has uploaded this change for review.
net/url: add JoinPath, URL.JoinPath
Builds on CL 332209.
Fixes #47005
Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 41a5d34b1b74e8ee8bef1a92e47d52aecf9c7e53
GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
M api/except.txt
3 files changed, 110 insertions(+), 0 deletions(-)
diff --git a/api/except.txt b/api/except.txt
index b9972c1..37c76a3 100644
--- a/api/except.txt
+++ b/api/except.txt
@@ -505,3 +505,5 @@
pkg unicode, const Version = "7.0.0"
pkg unicode, const Version = "8.0.0"
pkg unicode, const Version = "9.0.0"
+pkg net/url, func JoinPath(string, ...string) (string, error)
+pkg net/url, method (*URL) JoinPath(...string) *URL
\ No newline at end of file
diff --git a/src/net/url/url.go b/src/net/url/url.go
index f31aa08..286e6aa 100644
--- a/src/net/url/url.go
+++ b/src/net/url/url.go
@@ -13,6 +13,7 @@
import (
"errors"
"fmt"
+ "path"
"sort"
"strconv"
"strings"
@@ -1176,6 +1177,17 @@
return nil
}
+// JoinPath returns a new URL with the provided path elements
+// joined to any existing path and 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 / ":" )
@@ -1216,3 +1228,13 @@
}
return false
}
+
+// JoinPath concatenates the path elements to the base URL and cleans any ./ or ../ elements from the final URL string.
+func JoinPath(base string, elem ...string) (result string, err error) {
+ url, err := Parse(base)
+ if err != nil {
+ return
+ }
+ result = url.JoinPath(elem...).String()
+ return
+}
diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go
index 664757b..268ba56 100644
--- a/src/net/url/url_test.go
+++ b/src/net/url/url_test.go
@@ -2062,3 +2062,74 @@
})
}
}
+
+func TestJoinPath(t *testing.T) {
+ type args struct {
+ baseUrl string
+ elem []string
+ }
+ tests := []struct {
+ name string
+ args args
+ wantResult string
+ wantErr bool
+ }{
+ {
+ name: "test normal url",
+ args: args{
+ baseUrl: "https://go.googlesource.com",
+ elem: []string{"go"},
+ },
+ wantResult: "https://go.googlesource.com/go",
+ wantErr: false,
+ },
+ {
+ name: "test .. parent url",
+ args: args{
+ baseUrl: "https://go.googlesource.com/a/b/c",
+ elem: []string{"../../../go"},
+ },
+ wantResult: "https://go.googlesource.com/go",
+ wantErr: false,
+ },
+ {
+ name: "test . cul path",
+ args: args{
+ baseUrl: "https://go.googlesource.com/",
+ elem: []string{"./go"},
+ },
+ wantResult: "https://go.googlesource.com/go",
+ wantErr: false,
+ },
+ {
+ name: "test multiple Separator",
+ args: args{
+ baseUrl: "https://go.googlesource.com//",
+ elem: []string{"/go"},
+ },
+ wantResult: "https://go.googlesource.com/go",
+ wantErr: false,
+ },
+ {
+ name: "test more elems",
+ args: args{
+ baseUrl: "https://go.googlesource.com//",
+ elem: []string{"/go", "a", "b", "c"},
+ },
+ wantResult: "https://go.googlesource.com/go/a/b/c",
+ wantErr: false,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotResult, err := JoinPath(tt.args.baseUrl, tt.args.elem...)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("JoinPath() error = %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ if gotResult != tt.wantResult {
+ t.Errorf("JoinPath() = %v, want %v", gotResult, tt.wantResult)
+ }
+ })
+ }
+}
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Gerrit Dou uploaded patch set #2 to this change.
net/url: add JoinPath, URL.JoinPath
Builds on CL 332209.
Fixes #47005
Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 5a67f0efd32770d6ba8d98aa79dd1a8210fc6994
GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
2 files changed, 108 insertions(+), 0 deletions(-)
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Damien Neil.
1 comment:
Patchset:
The try bot error is
Error running API checker: exit status 1
+pkg net/url, func JoinPath(string, ...string) (string, error)
+pkg net/url, method (*URL) JoinPath(...string) *URL
exit status 1
2022/02/22 16:37:33 Failed: exit status 1
Do the public API definitions need to be updated when adding a new method to url.URL? How does that work?
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Carl Johnson, Damien Neil.
Gerrit Bot uploaded patch set #4 to this change.
net/url: add JoinPath, URL.JoinPath
Builds on CL 332209.
Fixes #47005
Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 565ccd029cbb9806a9589b600a8b6e276b750d55
GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
2 files changed, 99 insertions(+), 0 deletions(-)
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Carl Johnson, Damien Neil.
Gerrit Bot uploaded patch set #5 to this change.
net/url: add JoinPath, URL.JoinPath
Builds on CL 332209.
Fixes #47005
Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 1910d6613e6763de1961017740d107cfc09f205d
GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
2 files changed, 106 insertions(+), 0 deletions(-)
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Damien Neil, Russ Cox.
3 comments:
File src/net/url/url.go:
Patch Set #3, Line 1232: // JoinPath concatenates the path elements to the base URL and cleans any ./ or ../ elements from the final URL string.
The wording here seems unnecessarily different from the wording in URL.JoinPath. […]
Done
File src/net/url/url_test.go:
Patch Set #3, Line 2067: type args struct {
Please simplify: […]
Done
Patch Set #3, Line 2124: t.Run(tt.name, func(t *testing.T) {
Please drop t.Run - overkill for such a simple table. […]
Done
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Damien Neil.
Gerrit Bot uploaded patch set #6 to this change.
net/url: add JoinPath, URL.JoinPath
Builds on CL 332209.
Fixes #47005
Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: c06c3ce8379546f6135a6385a24c19422a382a19
GitHub-Pull-Request: golang/go#50383
---
M src/net/url/url.go
M src/net/url/url_test.go
2 files changed, 94 insertions(+), 0 deletions(-)
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Damien Neil, Russ Cox.
6 comments:
File src/net/url/url_test.go:
Patch Set #5, Line 2077: err: false,
Please delete this line.
Done
Patch Set #5, Line 2083: err: false,
Please delete this line.
Done
Patch Set #5, Line 2089: err: false,
Please delete this line.
Done
Patch Set #5, Line 2095: err: false,
Please delete this line.
Done
Patch Set #5, Line 2101: err: false,
Please delete this line.
Done
Patch Set #5, Line 2106: out: "",
Please delete this line.
Done
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Patchset:
API check failure again:
##### API check
Error running API checker: exit status 1
+pkg net/url, func JoinPath(string, ...string) (string, error)
+pkg net/url, method (*URL) JoinPath(...string) *URL
exit status 1
2022/03/04 03:17:17 Failed: exit status 1
go tool dist: FAILED
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Damien Neil.
Gerrit Bot uploaded patch set #7 to this change.
net/url: add JoinPath, URL.JoinPath
Builds on CL 332209.
Fixes #47005
Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 51b735066eef74f5e67c3e8899c58f44c0383c61
GitHub-Pull-Request: golang/go#50383
---
M api/next.txt
M src/net/url/url.go
M src/net/url/url_test.go
3 files changed, 96 insertions(+), 0 deletions(-)
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Carl Johnson, Damien Neil, Russ Cox.
1 comment:
Patchset:
RELNOTES=yes
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Carl Johnson, Damien Neil, Russ Cox.
Patch set 8:Trust +1
Attention is currently required from: Carl Johnson, Russ Cox.
Patch set 8:Code-Review +2
Attention is currently required from: Carl Johnson, Russ Cox.
Patch set 8:Run-TryBot +1
Gopher Robot submitted this change.
net/url: add JoinPath, URL.JoinPath
Builds on CL 332209.
Fixes #47005
Change-Id: I82708dede05d79a196ca63f5a4e7cb5ac9a041ea
GitHub-Last-Rev: 51b735066eef74f5e67c3e8899c58f44c0383c61
GitHub-Pull-Request: golang/go#50383
Reviewed-on: https://go-review.googlesource.com/c/go/+/374654
Reviewed-by: Russ Cox <r...@golang.org>
Auto-Submit: Russ Cox <r...@golang.org>
Trust: Ian Lance Taylor <ia...@golang.org>
Reviewed-by: Damien Neil <dn...@google.com>
Run-TryBot: Ian Lance Taylor <ia...@golang.org>
TryBot-Result: Gopher Robot <go...@golang.org>
---
M api/next.txt
M src/net/url/url.go
M src/net/url/url_test.go
3 files changed, 103 insertions(+), 0 deletions(-)
diff --git a/api/next.txt b/api/next.txt
index 23fd98a..148cbff 100644
--- a/api/next.txt
+++ b/api/next.txt
@@ -3,3 +3,5 @@
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
diff --git a/src/net/url/url.go b/src/net/url/url.go
index f31aa08..1571bf7 100644
--- a/src/net/url/url.go
+++ b/src/net/url/url.go
@@ -13,6 +13,7 @@
import (
"errors"
"fmt"
+ "path"
"sort"
"strconv"
"strings"
@@ -1176,6 +1177,17 @@
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 / ":" )
@@ -1216,3 +1228,14 @@
}
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
+}
diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go
index 664757b..84dba45 100644
--- a/src/net/url/url_test.go
+++ b/src/net/url/url_test.go
@@ -2062,3 +2062,59 @@
})
}
}
+
+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)
+ }
+ }
+}
To view, visit change 374654. To unsubscribe, or for help writing mail filters, visit settings.