Commit 586afc6e authored by stkhapugin@chromium.org's avatar stkhapugin@chromium.org Committed by Commit Bot

Add new url formatter flag to omit everything after the host.

Adds a new flag, kFormatUrlOmitPath, that formats URL by omitting
everything after the hostname, like path and query.

Bug: none
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I3f98fd52a6e5fbb404fb87d72cdb4bbebe2d15b5
Reviewed-on: https://chromium-review.googlesource.com/971481
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550574}
parent 1aed207b
......@@ -408,6 +408,7 @@ const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2;
const FormatUrlType kFormatUrlOmitHTTPS = 1 << 3;
const FormatUrlType kFormatUrlExperimentalElideAfterHost = 1 << 4;
const FormatUrlType kFormatUrlOmitTrivialSubdomains = 1 << 5;
const FormatUrlType kFormatUrlTrimAfterHost = 1 << 6;
const FormatUrlType kFormatUrlOmitDefaults =
kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP |
......@@ -484,7 +485,8 @@ base::string16 FormatUrlWithAdjustments(
new_parsed->scheme = parsed.scheme;
// Username & password.
if ((format_types & kFormatUrlOmitUsernamePassword) != 0) {
if (((format_types & kFormatUrlOmitUsernamePassword) != 0) ||
((format_types & kFormatUrlTrimAfterHost) != 0)) {
// Remove the username and password fields. We don't want to display those
// to the user since they can be used for attacks,
// e.g. "http://google.com:search@evil.ru/"
......@@ -543,37 +545,44 @@ base::string16 FormatUrlWithAdjustments(
}
// Path & query. Both get the same general unescape & convert treatment.
if ((format_types & kFormatUrlOmitTrailingSlashOnBareHostname) &&
CanStripTrailingSlash(url)) {
if ((format_types & kFormatUrlTrimAfterHost) ||
((format_types & kFormatUrlExperimentalElideAfterHost) &&
url.IsStandard() && !url.SchemeIsFile() && !url.SchemeIsFileSystem())) {
// Only elide when the eliding is required and when the host is followed by
// more than just one forward slash.
bool should_elide = (format_types & kFormatUrlExperimentalElideAfterHost) &&
((parsed.path.len > 1) || parsed.query.is_valid() ||
parsed.ref.is_valid());
// Either remove the path completely, or, if eliding, keep the first slash.
const size_t new_path_len = should_elide ? 1 : 0;
size_t trimmed_length = parsed.path.len - new_path_len;
// Remove query and the '?' delimeter.
if (parsed.query.is_valid())
trimmed_length += parsed.query.len + 1;
// Remove ref and the '#" delimiter.
if (parsed.ref.is_valid())
trimmed_length += parsed.ref.len + 1;
if (should_elide) {
// Replace everything after the host with a forward slash and ellipsis.
url_string.push_back('/');
constexpr base::char16 kEllipsisUTF16[] = {0x2026, 0};
url_string.append(kEllipsisUTF16);
}
adjustments->push_back(base::OffsetAdjuster::Adjustment(
parsed.path.begin + new_path_len, trimmed_length, new_path_len));
} else if ((format_types & kFormatUrlOmitTrailingSlashOnBareHostname) &&
CanStripTrailingSlash(url)) {
// Omit the path, which is a single trailing slash. There's no query or ref.
if (parsed.path.len > 0) {
adjustments->push_back(base::OffsetAdjuster::Adjustment(
parsed.path.begin, parsed.path.len, 0));
}
} else if ((format_types & kFormatUrlExperimentalElideAfterHost) &&
url.IsStandard() && !url.SchemeIsFile() &&
!url.SchemeIsFileSystem()) {
// Replace everything after the host with a forward slash and ellipsis.
url_string.push_back('/');
constexpr base::char16 kEllipsisUTF16[] = {0x2026, 0};
url_string.append(kEllipsisUTF16);
// Compute the length of everything we're replacing. For the path, we are
// removing everything but the first slash.
size_t old_length = parsed.path.len - 1;
// We're also removing any query, plus the delimiting '?'.
if (parsed.query.is_valid())
old_length += parsed.query.len + 1;
// We're also removing any ref, plus the delimiting '#'.
if (parsed.ref.is_valid())
old_length += parsed.ref.len + 1;
// We're replacing all of these with a single character (an ellipsis). The
// adjustment begins after the forward slash at the beginning of the path.
adjustments->push_back(
base::OffsetAdjuster::Adjustment(parsed.path.begin + 1, old_length, 1));
} else {
// Append the formatted path, query, and ref.
AppendFormattedComponent(spec, parsed.path,
......
......@@ -62,6 +62,10 @@ extern const FormatUrlType kFormatUrlExperimentalElideAfterHost;
// kFormatUrlOmitDefaults.
extern const FormatUrlType kFormatUrlOmitTrivialSubdomains;
// Omits everything after the host: the path, query, ref, username and password
// are all omitted.
extern const FormatUrlType kFormatUrlTrimAfterHost;
// Convenience for omitting all unecessary types. Does not include HTTPS scheme
// removal, or experimental flags.
extern const FormatUrlType kFormatUrlOmitDefaults;
......
......@@ -1177,6 +1177,46 @@ TEST(UrlFormatterTest, FormatUrl) {
{"omit trivial subdomains but leave registry and domain alone - co.uk",
"http://m.co.uk/", kFormatUrlOmitTrivialSubdomains,
net::UnescapeRule::NORMAL, L"http://m.co.uk/", 7},
// -------- trim after host --------
{"omit the trailing slash when ommitting the path", "http://google.com/",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit the simple file path when ommitting the path",
"http://google.com/foo",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit the file and folder path when ommitting the path",
"http://google.com/ab/cd",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit everything after host with query only",
"http://google.com/?foo=bar",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit everything after host with ref only", "http://google.com/#foobar",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit everything after host with path and query only",
"http://google.com/foo?a=b",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit everything after host with path and ref only",
"http://google.com/foo#c",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit everything after host with query and ref only",
"http://google.com/?a=b#c",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit everything after host with path, query and ref",
"http://google.com/foo?a=b#c",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
{"omit everything after host with repeated delimiters (sanity check)",
"http://google.com////???####",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, L"google.com", 0},
};
for (size_t i = 0; i < arraysize(tests); ++i) {
......@@ -1553,6 +1593,28 @@ TEST(UrlFormatterTest, FormatUrlWithOffsets) {
kFormatUrlOmitDefaults | kFormatUrlExperimentalElideAfterHost,
net::UnescapeRule::NORMAL, elide_after_host_offsets);
const size_t trim_after_host_offsets[] = {
0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3, 4,
5, 6, 7, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 9};
CheckAdjustedOffsets("http://foo.com/abcdefg",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, trim_after_host_offsets);
CheckAdjustedOffsets("http://foo.com/abc/def",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, trim_after_host_offsets);
CheckAdjustedOffsets("http://foo.com/abc?a=b",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, trim_after_host_offsets);
CheckAdjustedOffsets("http://foo.com/abc#def",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, trim_after_host_offsets);
CheckAdjustedOffsets("http://foo.com/a?a=b#f",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, trim_after_host_offsets);
CheckAdjustedOffsets("http://foo.com//??###",
kFormatUrlOmitDefaults | kFormatUrlTrimAfterHost,
net::UnescapeRule::NORMAL, trim_after_host_offsets);
const size_t omit_https_offsets[] = {
0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
......
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