1
0
mirror of https://github.com/golang/go synced 2024-11-17 23:04:56 -07:00

net/url: consider ForceQuery in ResolveReference

Previously, when resolving references of form
  (https://golang.org/?hello).ResolveReference(?)
we only used URL.RawQuery to determine whether or not a query part is
defined. Go 1.7 introduced URL.ForceQuery as a flag for the situation
where a query part is provided but empty. But we did not use it in
ResolveReference. This leads to the erroneous output
  https://golang.org/?hello
when the correct output should be
  https://golang.org/?
This commit rectifies that error.

Fixes #46033

Change-Id: I05bc0b48bf2bbf13b4ddc0dd10599ea613dc2188
Reviewed-on: https://go-review.googlesource.com/c/go/+/317930
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Damien Neil <dneil@google.com>
This commit is contained in:
Timothy Gu 2021-05-07 01:03:59 -07:00 committed by Damien Neil
parent 02e5913406
commit 81fea0b4fd
2 changed files with 4 additions and 1 deletions

View File

@ -1083,7 +1083,7 @@ func (u *URL) ResolveReference(ref *URL) *URL {
url.Path = "" url.Path = ""
return &url return &url
} }
if ref.Path == "" && ref.RawQuery == "" { if ref.Path == "" && !ref.ForceQuery && ref.RawQuery == "" {
url.RawQuery = u.RawQuery url.RawQuery = u.RawQuery
if ref.Fragment == "" { if ref.Fragment == "" {
url.Fragment = u.Fragment url.Fragment = u.Fragment

View File

@ -1244,6 +1244,9 @@ var resolveReferenceTests = []struct {
{"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"}, {"https://a/b/c/d;p?q", "//g/d/e/f?y#s", "https://g/d/e/f?y#s"},
{"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"}, {"https://a/b/c/d;p#s", "?y", "https://a/b/c/d;p?y"},
{"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"}, {"https://a/b/c/d;p?q#s", "?y", "https://a/b/c/d;p?y"},
// Empty path and query but with ForceQuery (issue 46033).
{"https://a/b/c/d;p?q#s", "?", "https://a/b/c/d;p?"},
} }
func TestResolveReference(t *testing.T) { func TestResolveReference(t *testing.T) {