summaryrefslogtreecommitdiffstats
path: root/net/cookies/cookie_util.cc
diff options
context:
space:
mode:
authorerikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-15 09:29:58 +0000
committererikwright@chromium.org <erikwright@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-15 09:29:58 +0000
commit63ee33bde2ec8471a70f0f0ec6a1962dd07fc8ab (patch)
tree7c38150e4b53fa60235d4d3845100d805ba85d35 /net/cookies/cookie_util.cc
parentca2fd075cf4eaeb2ee650affff606bbb3120772c (diff)
downloadchromium_src-63ee33bde2ec8471a70f0f0ec6a1962dd07fc8ab.zip
chromium_src-63ee33bde2ec8471a70f0f0ec6a1962dd07fc8ab.tar.gz
chromium_src-63ee33bde2ec8471a70f0f0ec6a1962dd07fc8ab.tar.bz2
Move the cookie store implementation into its own directory.
In the initial step, forwarding headers are left in net/base/cookie_*h . After all clients are updated these will be removed and erikwright will be removed from net/base/OWNERS BUG=70818 TEST=compilation Review URL: http://codereview.chromium.org/9703011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126871 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/cookies/cookie_util.cc')
-rw-r--r--net/cookies/cookie_util.cc79
1 files changed, 79 insertions, 0 deletions
diff --git a/net/cookies/cookie_util.cc b/net/cookies/cookie_util.cc
new file mode 100644
index 0000000..6e1833f
--- /dev/null
+++ b/net/cookies/cookie_util.cc
@@ -0,0 +1,79 @@
+// Copyright (c) 2012 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/cookies/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
+