diff options
author | droger@google.com <droger@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-17 09:22:03 +0000 |
---|---|---|
committer | droger@google.com <droger@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-17 09:22:03 +0000 |
commit | 2fb376add13b6f658741e804bc226ac87b68698b (patch) | |
tree | 229b69a8ddc07ad0218677b6d3e438347a9633a4 /net/base | |
parent | 0654cc8ef8cd3fa4af7980a899c6ec1e8791d6f2 (diff) | |
download | chromium_src-2fb376add13b6f658741e804bc226ac87b68698b.zip chromium_src-2fb376add13b6f658741e804bc226ac87b68698b.tar.gz chromium_src-2fb376add13b6f658741e804bc226ac87b68698b.tar.bz2 |
Creation of cookie_utils.
BUG=NONE
TEST=NONE
Follow up on:
Review URL: http://codereview.chromium.org/8465022
Then reverted by:
http://codereview.chromium.org/8571008/
Review URL: http://codereview.chromium.org/8468010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110476 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/cookie_monster.cc | 87 | ||||
-rw-r--r-- | net/base/cookie_monster.h | 4 | ||||
-rw-r--r-- | net/base/cookie_monster_unittest.cc | 3 | ||||
-rw-r--r-- | net/base/cookie_util.cc | 79 | ||||
-rw-r--r-- | net/base/cookie_util.h | 40 |
5 files changed, 132 insertions, 81 deletions
diff --git a/net/base/cookie_monster.cc b/net/base/cookie_monster.cc index c2ac435..7274cec 100644 --- a/net/base/cookie_monster.cc +++ b/net/base/cookie_monster.cc @@ -61,7 +61,7 @@ #include "base/stringprintf.h" #include "googleurl/src/gurl.h" #include "googleurl/src/url_canon.h" -#include "net/base/net_util.h" +#include "net/base/cookie_util.h" #include "net/base/registry_controlled_domain.h" using base::Time; @@ -188,71 +188,6 @@ struct CookieSignature { std::string path; }; -// Returns the effective TLD+1 for a given host. This only makes sense for http -// and https schemes. For other schemes, the host will be returned unchanged -// (minus any leading period). -std::string GetEffectiveDomain(const std::string& scheme, - const std::string& host) { - if (scheme == "http" || scheme == "https") - return RegistryControlledDomainService::GetDomainAndRegistry(host); - - if (!CookieMonster::DomainIsHostOnly(host)) - return host.substr(1); - return host; -} - -// Determine the actual cookie domain based on the domain string passed -// (if any) and the URL from which the cookie came. -// On success returns true, and sets cookie_domain to either a -// -host cookie domain (ex: "google.com") -// -domain cookie domain (ex: ".google.com") -bool GetCookieDomainWithString(const GURL& url, - const std::string& domain_string, - std::string* result) { - const std::string url_host(url.host()); - - // If no domain was specified in the domain string, default to a host cookie. - // We match IE/Firefox in allowing a domain=IPADDR if it matches the url - // ip address hostname exactly. It should be treated as a host cookie. - if (domain_string.empty() || - (url.HostIsIPAddress() && url_host == domain_string)) { - *result = url_host; - DCHECK(CookieMonster::DomainIsHostOnly(*result)); - return true; - } - - // Get the normalized domain specified in cookie line. - url_canon::CanonHostInfo ignored; - std::string cookie_domain(CanonicalizeHost(domain_string, &ignored)); - if (cookie_domain.empty()) - return false; - if (cookie_domain[0] != '.') - cookie_domain = "." + cookie_domain; - - // Ensure |url| and |cookie_domain| have the same domain+registry. - const std::string url_scheme(url.scheme()); - const std::string url_domain_and_registry( - GetEffectiveDomain(url_scheme, url_host)); - if (url_domain_and_registry.empty()) - return false; // IP addresses/intranet hosts can't set domain cookies. - const std::string cookie_domain_and_registry( - GetEffectiveDomain(url_scheme, cookie_domain)); - if (url_domain_and_registry != cookie_domain_and_registry) - return false; // Can't set a cookie on a different domain + registry. - - // Ensure |url_host| is |cookie_domain| or one of its subdomains. Given that - // we know the domain+registry are the same from the above checks, this is - // basically a simple string suffix check. - if ((url_host.length() < cookie_domain.length()) ? - (cookie_domain != ("." + url_host)) : - url_host.compare(url_host.length() - cookie_domain.length(), - cookie_domain.length(), cookie_domain)) - return false; - - *result = cookie_domain; - return true; -} - // Determine the cookie domain to use for setting the specified cookie. bool GetCookieDomain(const GURL& url, const CookieMonster::ParsedCookie& pc, @@ -260,7 +195,7 @@ bool GetCookieDomain(const GURL& url, std::string domain_string; if (pc.HasDomain()) domain_string = pc.Domain(); - return GetCookieDomainWithString(url, domain_string, result); + return cookie_util::GetCookieDomainWithString(url, domain_string, result); } std::string CanonPathWithString(const GURL& url, @@ -574,10 +509,6 @@ Time CookieMonster::ParseCookieTime(const std::string& time_string) { return Time(); } -bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { - return (domain_string.empty() || domain_string[0] != '.'); -} - // Task classes for queueing the coming request. class CookieMonster::CookieMonsterTask @@ -1128,7 +1059,8 @@ void CookieMonster::DoCookieTaskForURL( // then run the task, otherwise load from DB. if (!loaded_) { // Checks if the domain key has been loaded. - std::string key(GetEffectiveDomain(url.scheme(), url.host())); + std::string key(cookie_util::GetEffectiveDomain(url.scheme(), + url.host())); if (keys_loaded_.find(key) == keys_loaded_.end()) { std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > ::iterator it = tasks_queued_.find(key); @@ -1734,7 +1666,8 @@ void CookieMonster::FindCookiesForHostAndDomain( cookies); // See if we can search for domain cookies, i.e. if the host has a TLD + 1. - const std::string domain(GetEffectiveDomain(url.scheme(), key)); + const std::string domain(cookie_util::GetEffectiveDomain(url.scheme(), + key)); if (domain.empty()) return; DCHECK_LE(domain.length(), key.length()); @@ -2643,8 +2576,8 @@ CookieMonster::CanonicalCookie::CanonicalCookie(const GURL& url, domain_string = pc.Domain(); } bool result - = GetCookieDomainWithString(url, domain_string, - &cookie_domain); + = cookie_util::GetCookieDomainWithString(url, domain_string, + &cookie_domain); // Caller is responsible for passing in good arguments. DCHECK(result); domain_ = cookie_domain; @@ -2727,8 +2660,10 @@ CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( if (parsed_domain != domain) return NULL; std::string cookie_domain; - if (!GetCookieDomainWithString(url, parsed_domain, &cookie_domain)) + if (!cookie_util::GetCookieDomainWithString(url, parsed_domain, + &cookie_domain)) { return NULL; + } std::string parsed_path = ParsedCookie::ParseValueString(path); if (parsed_path != path) diff --git a/net/base/cookie_monster.h b/net/base/cookie_monster.h index d8874dc..6d59092 100644 --- a/net/base/cookie_monster.h +++ b/net/base/cookie_monster.h @@ -146,10 +146,6 @@ class NET_EXPORT CookieMonster : public CookieStore { // Parses the string with the cookie time (very forgivingly). static base::Time ParseCookieTime(const std::string& time_string); - // Returns true if a domain string represents a host-only cookie, - // i.e. it doesn't begin with a leading '.' character. - static bool DomainIsHostOnly(const std::string& domain_string); - // Helper function that adds all cookies from |list| into this instance. bool InitializeFrom(const CookieList& list); diff --git a/net/base/cookie_monster_unittest.cc b/net/base/cookie_monster_unittest.cc index d95b448..5e2db5b 100644 --- a/net/base/cookie_monster_unittest.cc +++ b/net/base/cookie_monster_unittest.cc @@ -21,6 +21,7 @@ #include "googleurl/src/gurl.h" #include "net/base/cookie_monster.h" #include "net/base/cookie_monster_store_test.h" // For CookieStore mock +#include "net/base/cookie_util.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -1852,7 +1853,7 @@ TEST_F(CookieMonsterTest, TestDomainIsHostOnly) { for (size_t i = 0; i < arraysize(tests); ++i) { EXPECT_EQ(tests[i].is_host_only, - CookieMonster::DomainIsHostOnly(tests[i].str)); + cookie_util::DomainIsHostOnly(tests[i].str)); } } diff --git a/net/base/cookie_util.cc b/net/base/cookie_util.cc new file mode 100644 index 0000000..8d8a0c3 --- /dev/null +++ b/net/base/cookie_util.cc @@ -0,0 +1,79 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/base/cookie_util.h" + +#include "base/logging.h" +#include "googleurl/src/gurl.h" +#include "net/base/net_util.h" +#include "net/base/registry_controlled_domain.h" + +namespace net { +namespace cookie_util { + +bool DomainIsHostOnly(const std::string& domain_string) { + return (domain_string.empty() || domain_string[0] != '.'); +} + +std::string GetEffectiveDomain(const std::string& scheme, + const std::string& host) { + if (scheme == "http" || scheme == "https") + return RegistryControlledDomainService::GetDomainAndRegistry(host); + + if (!DomainIsHostOnly(host)) + return host.substr(1); + return host; +} + +bool GetCookieDomainWithString(const GURL& url, + const std::string& domain_string, + std::string* result) { + const std::string url_host(url.host()); + + // If no domain was specified in the domain string, default to a host cookie. + // We match IE/Firefox in allowing a domain=IPADDR if it matches the url + // ip address hostname exactly. It should be treated as a host cookie. + if (domain_string.empty() || + (url.HostIsIPAddress() && url_host == domain_string)) { + *result = url_host; + DCHECK(DomainIsHostOnly(*result)); + return true; + } + + // Get the normalized domain specified in cookie line. + url_canon::CanonHostInfo ignored; + std::string cookie_domain(CanonicalizeHost(domain_string, &ignored)); + if (cookie_domain.empty()) + return false; + if (cookie_domain[0] != '.') + cookie_domain = "." + cookie_domain; + + // Ensure |url| and |cookie_domain| have the same domain+registry. + const std::string url_scheme(url.scheme()); + const std::string url_domain_and_registry( + GetEffectiveDomain(url_scheme, url_host)); + if (url_domain_and_registry.empty()) + return false; // IP addresses/intranet hosts can't set domain cookies. + const std::string cookie_domain_and_registry( + GetEffectiveDomain(url_scheme, cookie_domain)); + if (url_domain_and_registry != cookie_domain_and_registry) + return false; // Can't set a cookie on a different domain + registry. + + // Ensure |url_host| is |cookie_domain| or one of its subdomains. Given that + // we know the domain+registry are the same from the above checks, this is + // basically a simple string suffix check. + const bool is_suffix = (url_host.length() < cookie_domain.length()) ? + (cookie_domain != ("." + url_host)) : + (url_host.compare(url_host.length() - cookie_domain.length(), + cookie_domain.length(), cookie_domain) != 0); + if (is_suffix) + return false; + + *result = cookie_domain; + return true; +} + +} // namespace cookie_utils +} // namespace net + diff --git a/net/base/cookie_util.h b/net/base/cookie_util.h new file mode 100644 index 0000000..bda52f0 --- /dev/null +++ b/net/base/cookie_util.h @@ -0,0 +1,40 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_BASE_COOKIE_UTIL_H_ +#define NET_BASE_COOKIE_UTIL_H_ +#pragma once + +#include <string> + +#include "net/base/net_export.h" + +class GURL; + +namespace net { +namespace cookie_util { + +// Returns the effective TLD+1 for a given host. This only makes sense for http +// and https schemes. For other schemes, the host will be returned unchanged +// (minus any leading period). +NET_EXPORT std::string GetEffectiveDomain(const std::string& scheme, + const std::string& host); + +// Determine the actual cookie domain based on the domain string passed +// (if any) and the URL from which the cookie came. +// On success returns true, and sets cookie_domain to either a +// -host cookie domain (ex: "google.com") +// -domain cookie domain (ex: ".google.com") +NET_EXPORT bool GetCookieDomainWithString(const GURL& url, + const std::string& domain_string, + std::string* result); + +// Returns true if a domain string represents a host-only cookie, +// i.e. it doesn't begin with a leading '.' character. +NET_EXPORT bool DomainIsHostOnly(const std::string& domain_string); + +} // namspace cookie_util +} // namespace net + +#endif // NET_BASE_COOKIE_UTIL_H_ |