From 81fea0b4fd3b134d4c1d121abad171e358037ce3 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Fri, 7 May 2021 01:03:59 -0700 Subject: [PATCH] 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 TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor Trust: Damien Neil --- src/net/url/url.go | 2 +- src/net/url/url_test.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/net/url/url.go b/src/net/url/url.go index d571ab2fc4..f31aa08b59 100644 --- a/src/net/url/url.go +++ b/src/net/url/url.go @@ -1083,7 +1083,7 @@ func (u *URL) ResolveReference(ref *URL) *URL { url.Path = "" return &url } - if ref.Path == "" && ref.RawQuery == "" { + if ref.Path == "" && !ref.ForceQuery && ref.RawQuery == "" { url.RawQuery = u.RawQuery if ref.Fragment == "" { url.Fragment = u.Fragment diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go index 6807d58f1a..7c807d7a38 100644 --- a/src/net/url/url_test.go +++ b/src/net/url/url_test.go @@ -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#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) {