From 6cccaebf64c62d314fb0fe70b36d24542541bf5c Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Sun, 1 Dec 2019 09:44:21 +0100 Subject: [PATCH] Fix parsing relative URLs starting with // (#702) * Fix parsing relative URLs starting with // * Improve test --- uri.go | 11 ++++++++--- uri_test.go | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/uri.go b/uri.go index 21ab3ae..80e5bf3 100644 --- a/uri.go +++ b/uri.go @@ -263,9 +263,14 @@ func (u *URI) Parse(host, uri []byte) { func (u *URI) parse(host, uri []byte, isTLS bool) { u.Reset() - scheme, host, uri := splitHostURI(host, uri) - u.scheme = append(u.scheme, scheme...) - lowercaseBytes(u.scheme) + if len(host) == 0 || bytes.Contains(uri, strColonSlashSlash) { + scheme, newHost, newURI := splitHostURI(host, uri) + u.scheme = append(u.scheme, scheme...) + lowercaseBytes(u.scheme) + host = newHost + uri = newURI + } + if isTLS { u.scheme = append(u.scheme[:0], strHTTPS...) } diff --git a/uri_test.go b/uri_test.go index c90b4e1..06d7526 100644 --- a/uri_test.go +++ b/uri_test.go @@ -345,6 +345,12 @@ func TestURIParse(t *testing.T) { // http:// in query params testURIParse(t, &u, "aaa.com", "/foo?bar=http://google.com", "http://aaa.com/foo?bar=http://google.com", "aaa.com", "/foo", "/foo", "bar=http://google.com", "") + + testURIParse(t, &u, "aaa.com", "//relative", + "http://aaa.com/relative", "aaa.com", "/relative", "//relative", "", "") + + testURIParse(t, &u, "", "//aaa.com//absolute", + "http://aaa.com/absolute", "aaa.com", "/absolute", "//absolute", "", "") } func testURIParse(t *testing.T, u *URI, host, uri,