Commit 60f0c536 authored by Charles Harrison's avatar Charles Harrison Committed by Commit Bot

[url] For empty hosts, just trust GURL to make the invalid/valid decision

The SchemeHostPort always considers tuples with SCHEME_WITHOUT_PORT to
have empty hosts without being invalid (to cater to cases like file
schemes which can have hosts or not).

This led to a mismatch in SchemeHostPost::GetURL where we always
considered URLs with empty hosts to be valid GURLs, but this is not an
assumption that holds true. This CL just punts the valid decision in this
case to GURL for re-parsing, since it should be a rare occurance.

Bug: 820194
Change-Id: I3f4441d8c71a9b9500c71dbe98f0769f42ad13b5
Reviewed-on: https://chromium-review.googlesource.com/956424Reviewed-by: default avatarMike West <mkwst@chromium.org>
Commit-Queue: Charlie Harrison <csharrison@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542527}
parent 6d1fd84a
......@@ -175,6 +175,12 @@ GURL SchemeHostPort::GetURL() const {
if (IsInvalid())
return GURL(std::move(serialized), parsed, false);
// SchemeHostPort does not have enough information to determine if an empty
// host is valid or not for the given scheme. Force re-parsing.
DCHECK(!scheme_.empty());
if (host_.empty())
return GURL(serialized);
// If the serialized string is passed to GURL for parsing, it will append an
// empty path "/". Add that here. Note: per RFC 6454 we cannot do this for
// normal Origin serialization.
......
......@@ -9,6 +9,7 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
#include "url/url_util.h"
namespace {
......@@ -252,4 +253,21 @@ TEST(SchemeHostPortTest, Comparison) {
}
}
// Some schemes have optional authority. Make sure that GURL conversion from
// SchemeHostPort is not opinionated in that regard. For more info, See
// crbug.com/820194, where we considered all SchemeHostPorts with
// SCHEME_WITHOUT_PORT as valid with empty hosts, even though some are not (e.g.
// chrome URLs).
TEST(SchemeHostPortTest, EmptyHostGurlConversion) {
url::AddStandardScheme("chrome", url::SCHEME_WITHOUT_PORT);
GURL chrome_url("chrome:");
EXPECT_FALSE(chrome_url.is_valid());
url::SchemeHostPort chrome_tuple("chrome", "", 0);
EXPECT_FALSE(chrome_tuple.GetURL().is_valid());
ExpectParsedUrlsEqual(GURL(chrome_tuple.Serialize()), chrome_tuple.GetURL());
ExpectParsedUrlsEqual(chrome_url, chrome_tuple.GetURL());
}
} // namespace url
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment