Commit ade5dbf0 authored by Dylan Cutler's avatar Dylan Cutler Committed by Commit Bot

Add functions to construct a cookie URL from its domain and path alone.

I included overloads for setting the source scheme with either a boolean
or the scheme string itself.

I refacted other similar utility functions in cookie_util.cc to reuse
this new function as well.

Bug: 1102874
Change-Id: I879ea6534f65ab54d4374fb768a751115e68471b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2285244Reviewed-by: default avatarLily Chen <chlily@chromium.org>
Commit-Queue: Dylan Cutler <dylancutler@google.com>
Cr-Commit-Position: refs/heads/master@{#786049}
parent 511028b8
......@@ -14,6 +14,7 @@
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/stl_util.h"
#include "base/strings/strcat.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
......@@ -331,23 +332,33 @@ base::Time ParseCookieExpirationTime(const std::string& time_string) {
return base::Time();
}
GURL CookieOriginToURL(const std::string& domain, bool is_https) {
if (domain.empty())
GURL CookieDomainAndPathToURL(const std::string& domain,
const std::string& path,
const std::string& source_scheme) {
// Note: domain_no_dot could be empty for e.g. file cookies.
std::string domain_no_dot = CookieDomainAsHost(domain);
if (domain_no_dot.empty() || source_scheme.empty())
return GURL();
return GURL(base::StrCat(
{source_scheme, url::kStandardSchemeSeparator, domain_no_dot, path}));
}
const std::string scheme = is_https ? url::kHttpsScheme : url::kHttpScheme;
return GURL(scheme + url::kStandardSchemeSeparator +
CookieDomainAsHost(domain) + "/");
GURL CookieDomainAndPathToURL(const std::string& domain,
const std::string& path,
bool is_https) {
return CookieDomainAndPathToURL(
domain, path,
std::string(is_https ? url::kHttpsScheme : url::kHttpScheme));
}
GURL CookieOriginToURL(const std::string& domain, bool is_https) {
return CookieDomainAndPathToURL(domain, "/", is_https);
}
GURL SimulatedCookieSource(const CanonicalCookie& cookie,
const std::string& source_scheme) {
// Note: cookie.DomainWithoutDot() could be empty for e.g. file cookies.
if (cookie.DomainWithoutDot().empty() || source_scheme.empty())
return GURL();
return GURL(source_scheme + url::kStandardSchemeSeparator +
cookie.DomainWithoutDot() + cookie.Path());
return CookieDomainAndPathToURL(cookie.Domain(), cookie.Path(),
source_scheme);
}
bool IsDomainMatch(const std::string& domain, const std::string& host) {
......
......@@ -81,6 +81,19 @@ NET_EXPORT std::string CookieDomainAsHost(const std::string& cookie_domain);
// Time::Max(), respectively.
NET_EXPORT base::Time ParseCookieExpirationTime(const std::string& time_string);
// Get a cookie's URL from it's domain, path, and source scheme.
// The first field can be the combined domain-and-host-only-flag (e.g. the
// string returned by CanonicalCookie::Domain()) as opposed to the domain
// attribute per RFC6265bis. The GURL is constructed after stripping off any
// leading dot.
// Note: the GURL returned by this method is not guaranteed to be valid.
NET_EXPORT GURL CookieDomainAndPathToURL(const std::string& domain,
const std::string& path,
const std::string& source_scheme);
NET_EXPORT GURL CookieDomainAndPathToURL(const std::string& domain,
const std::string& path,
bool is_https);
// Convenience for converting a cookie origin (domain and https pair) to a URL.
NET_EXPORT GURL CookieOriginToURL(const std::string& domain, bool is_https);
......
......@@ -225,6 +225,36 @@ TEST(CookieUtilTest, TestRequestCookieParsing) {
}
}
TEST(CookieUtilTest, CookieDomainAndPathToURL) {
struct {
std::string domain;
std::string path;
bool is_https;
std::string expected_url;
} kTests[]{
{"a.com", "/", true, "https://a.com/"},
{"a.com", "/", false, "http://a.com/"},
{".a.com", "/", true, "https://a.com/"},
{".a.com", "/", false, "http://a.com/"},
{"b.a.com", "/", true, "https://b.a.com/"},
{"b.a.com", "/", false, "http://b.a.com/"},
{"a.com", "/example/path", true, "https://a.com/example/path"},
{".a.com", "/example/path", false, "http://a.com/example/path"},
{"b.a.com", "/example/path", true, "https://b.a.com/example/path"},
{".b.a.com", "/example/path", false, "http://b.a.com/example/path"},
};
for (auto& test : kTests) {
GURL url1 = cookie_util::CookieDomainAndPathToURL(test.domain, test.path,
test.is_https);
GURL url2 = cookie_util::CookieDomainAndPathToURL(
test.domain, test.path, std::string(test.is_https ? "https" : "http"));
// Test both overloads for equality.
EXPECT_EQ(url1, url2);
EXPECT_EQ(url1, GURL(test.expected_url));
}
}
TEST(CookieUtilTest, SimulatedCookieSource) {
GURL secure_url("https://b.a.com");
GURL insecure_url("http://b.a.com");
......
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