OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Portions of this code based on Mozilla: | 5 // Portions of this code based on Mozilla: |
6 // (netwerk/cookie/src/nsCookieService.cpp) | 6 // (netwerk/cookie/src/nsCookieService.cpp) |
7 /* ***** BEGIN LICENSE BLOCK ***** | 7 /* ***** BEGIN LICENSE BLOCK ***** |
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
9 * | 9 * |
10 * The contents of this file are subject to the Mozilla Public License Version | 10 * The contents of this file are subject to the Mozilla Public License Version |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 #include "base/logging.h" | 54 #include "base/logging.h" |
55 #include "base/memory/scoped_ptr.h" | 55 #include "base/memory/scoped_ptr.h" |
56 #include "base/message_loop.h" | 56 #include "base/message_loop.h" |
57 #include "base/message_loop_proxy.h" | 57 #include "base/message_loop_proxy.h" |
58 #include "base/metrics/histogram.h" | 58 #include "base/metrics/histogram.h" |
59 #include "base/string_tokenizer.h" | 59 #include "base/string_tokenizer.h" |
60 #include "base/string_util.h" | 60 #include "base/string_util.h" |
61 #include "base/stringprintf.h" | 61 #include "base/stringprintf.h" |
62 #include "googleurl/src/gurl.h" | 62 #include "googleurl/src/gurl.h" |
63 #include "googleurl/src/url_canon.h" | 63 #include "googleurl/src/url_canon.h" |
64 #include "net/base/cookie_utils.h" | 64 #include "net/base/net_util.h" |
65 #include "net/base/registry_controlled_domain.h" | 65 #include "net/base/registry_controlled_domain.h" |
66 | 66 |
67 using base::Time; | 67 using base::Time; |
68 using base::TimeDelta; | 68 using base::TimeDelta; |
69 using base::TimeTicks; | 69 using base::TimeTicks; |
70 | 70 |
71 // In steady state, most cookie requests can be satisfied by the in memory | 71 // In steady state, most cookie requests can be satisfied by the in memory |
72 // cookie monster store. However, if a request comes in during the initial | 72 // cookie monster store. However, if a request comes in during the initial |
73 // cookie load, it must be delayed until that load completes. That is done by | 73 // cookie load, it must be delayed until that load completes. That is done by |
74 // queueing it on CookieMonster::queue_ and running it when notification of | 74 // queueing it on CookieMonster::queue_ and running it when notification of |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 return diff < 0; | 181 return diff < 0; |
182 | 182 |
183 return path.compare(cs.path) < 0; | 183 return path.compare(cs.path) < 0; |
184 } | 184 } |
185 | 185 |
186 std::string name; | 186 std::string name; |
187 std::string domain; | 187 std::string domain; |
188 std::string path; | 188 std::string path; |
189 }; | 189 }; |
190 | 190 |
| 191 // Returns the effective TLD+1 for a given host. This only makes sense for http |
| 192 // and https schemes. For other schemes, the host will be returned unchanged |
| 193 // (minus any leading period). |
| 194 std::string GetEffectiveDomain(const std::string& scheme, |
| 195 const std::string& host) { |
| 196 if (scheme == "http" || scheme == "https") |
| 197 return RegistryControlledDomainService::GetDomainAndRegistry(host); |
| 198 |
| 199 if (!CookieMonster::DomainIsHostOnly(host)) |
| 200 return host.substr(1); |
| 201 return host; |
| 202 } |
| 203 |
| 204 // Determine the actual cookie domain based on the domain string passed |
| 205 // (if any) and the URL from which the cookie came. |
| 206 // On success returns true, and sets cookie_domain to either a |
| 207 // -host cookie domain (ex: "google.com") |
| 208 // -domain cookie domain (ex: ".google.com") |
| 209 bool GetCookieDomainWithString(const GURL& url, |
| 210 const std::string& domain_string, |
| 211 std::string* result) { |
| 212 const std::string url_host(url.host()); |
| 213 |
| 214 // If no domain was specified in the domain string, default to a host cookie. |
| 215 // We match IE/Firefox in allowing a domain=IPADDR if it matches the url |
| 216 // ip address hostname exactly. It should be treated as a host cookie. |
| 217 if (domain_string.empty() || |
| 218 (url.HostIsIPAddress() && url_host == domain_string)) { |
| 219 *result = url_host; |
| 220 DCHECK(CookieMonster::DomainIsHostOnly(*result)); |
| 221 return true; |
| 222 } |
| 223 |
| 224 // Get the normalized domain specified in cookie line. |
| 225 url_canon::CanonHostInfo ignored; |
| 226 std::string cookie_domain(CanonicalizeHost(domain_string, &ignored)); |
| 227 if (cookie_domain.empty()) |
| 228 return false; |
| 229 if (cookie_domain[0] != '.') |
| 230 cookie_domain = "." + cookie_domain; |
| 231 |
| 232 // Ensure |url| and |cookie_domain| have the same domain+registry. |
| 233 const std::string url_scheme(url.scheme()); |
| 234 const std::string url_domain_and_registry( |
| 235 GetEffectiveDomain(url_scheme, url_host)); |
| 236 if (url_domain_and_registry.empty()) |
| 237 return false; // IP addresses/intranet hosts can't set domain cookies. |
| 238 const std::string cookie_domain_and_registry( |
| 239 GetEffectiveDomain(url_scheme, cookie_domain)); |
| 240 if (url_domain_and_registry != cookie_domain_and_registry) |
| 241 return false; // Can't set a cookie on a different domain + registry. |
| 242 |
| 243 // Ensure |url_host| is |cookie_domain| or one of its subdomains. Given that |
| 244 // we know the domain+registry are the same from the above checks, this is |
| 245 // basically a simple string suffix check. |
| 246 if ((url_host.length() < cookie_domain.length()) ? |
| 247 (cookie_domain != ("." + url_host)) : |
| 248 url_host.compare(url_host.length() - cookie_domain.length(), |
| 249 cookie_domain.length(), cookie_domain)) |
| 250 return false; |
| 251 |
| 252 *result = cookie_domain; |
| 253 return true; |
| 254 } |
| 255 |
191 // Determine the cookie domain to use for setting the specified cookie. | 256 // Determine the cookie domain to use for setting the specified cookie. |
192 bool GetCookieDomain(const GURL& url, | 257 bool GetCookieDomain(const GURL& url, |
193 const CookieMonster::ParsedCookie& pc, | 258 const CookieMonster::ParsedCookie& pc, |
194 std::string* result) { | 259 std::string* result) { |
195 std::string domain_string; | 260 std::string domain_string; |
196 if (pc.HasDomain()) | 261 if (pc.HasDomain()) |
197 domain_string = pc.Domain(); | 262 domain_string = pc.Domain(); |
198 return cookie_utils::GetCookieDomainWithString(url, domain_string, result); | 263 return GetCookieDomainWithString(url, domain_string, result); |
199 } | 264 } |
200 | 265 |
201 std::string CanonPathWithString(const GURL& url, | 266 std::string CanonPathWithString(const GURL& url, |
202 const std::string& path_string) { | 267 const std::string& path_string) { |
203 // The RFC says the path should be a prefix of the current URL path. | 268 // The RFC says the path should be a prefix of the current URL path. |
204 // However, Mozilla allows you to set any path for compatibility with | 269 // However, Mozilla allows you to set any path for compatibility with |
205 // broken websites. We unfortunately will mimic this behavior. We try | 270 // broken websites. We unfortunately will mimic this behavior. We try |
206 // to be generous and accept cookies with an invalid path attribute, and | 271 // to be generous and accept cookies with an invalid path attribute, and |
207 // default the path to something reasonable. | 272 // default the path to something reasonable. |
208 | 273 |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 return Time::FromUTCExploded(exploded); | 567 return Time::FromUTCExploded(exploded); |
503 } | 568 } |
504 | 569 |
505 // One of our values was out of expected range. For well-formed input, | 570 // One of our values was out of expected range. For well-formed input, |
506 // the following check would be reasonable: | 571 // the following check would be reasonable: |
507 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; | 572 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; |
508 | 573 |
509 return Time(); | 574 return Time(); |
510 } | 575 } |
511 | 576 |
| 577 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { |
| 578 return (domain_string.empty() || domain_string[0] != '.'); |
| 579 } |
| 580 |
512 // Task classes for queueing the coming request. | 581 // Task classes for queueing the coming request. |
513 | 582 |
514 class CookieMonster::CookieMonsterTask | 583 class CookieMonster::CookieMonsterTask |
515 : public base::RefCountedThreadSafe<CookieMonsterTask> { | 584 : public base::RefCountedThreadSafe<CookieMonsterTask> { |
516 public: | 585 public: |
517 // Runs the task and invokes the client callback on the thread that | 586 // Runs the task and invokes the client callback on the thread that |
518 // originally constructed the task. | 587 // originally constructed the task. |
519 virtual void Run() = 0; | 588 virtual void Run() = 0; |
520 | 589 |
521 protected: | 590 protected: |
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1052 void CookieMonster::DoCookieTaskForURL( | 1121 void CookieMonster::DoCookieTaskForURL( |
1053 const scoped_refptr<CookieMonsterTask>& task_item, | 1122 const scoped_refptr<CookieMonsterTask>& task_item, |
1054 const GURL& url) { | 1123 const GURL& url) { |
1055 { | 1124 { |
1056 base::AutoLock autolock(lock_); | 1125 base::AutoLock autolock(lock_); |
1057 InitIfNecessary(); | 1126 InitIfNecessary(); |
1058 // If cookies for the requested domain key (eTLD+1) have been loaded from DB | 1127 // If cookies for the requested domain key (eTLD+1) have been loaded from DB |
1059 // then run the task, otherwise load from DB. | 1128 // then run the task, otherwise load from DB. |
1060 if (!loaded_) { | 1129 if (!loaded_) { |
1061 // Checks if the domain key has been loaded. | 1130 // Checks if the domain key has been loaded. |
1062 std::string key(cookie_utils::GetEffectiveDomain(url.scheme(), | 1131 std::string key(GetEffectiveDomain(url.scheme(), url.host())); |
1063 url.host())); | |
1064 if (keys_loaded_.find(key) == keys_loaded_.end()) { | 1132 if (keys_loaded_.find(key) == keys_loaded_.end()) { |
1065 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > | 1133 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > |
1066 ::iterator it = tasks_queued_.find(key); | 1134 ::iterator it = tasks_queued_.find(key); |
1067 if (it == tasks_queued_.end()) { | 1135 if (it == tasks_queued_.end()) { |
1068 store_->LoadCookiesForKey(key, | 1136 store_->LoadCookiesForKey(key, |
1069 base::Bind(&CookieMonster::OnKeyLoaded, this, key)); | 1137 base::Bind(&CookieMonster::OnKeyLoaded, this, key)); |
1070 it = tasks_queued_.insert(std::make_pair(key, | 1138 it = tasks_queued_.insert(std::make_pair(key, |
1071 std::deque<scoped_refptr<CookieMonsterTask> >())).first; | 1139 std::deque<scoped_refptr<CookieMonsterTask> >())).first; |
1072 } | 1140 } |
1073 it->second.push_back(task_item); | 1141 it->second.push_back(task_item); |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1659 } else { | 1727 } else { |
1660 // Need to probe for all domains that might have relevant | 1728 // Need to probe for all domains that might have relevant |
1661 // cookies for us. | 1729 // cookies for us. |
1662 | 1730 |
1663 // Query for the full host, For example: 'a.c.blah.com'. | 1731 // Query for the full host, For example: 'a.c.blah.com'. |
1664 std::string key(GetKey(url.host())); | 1732 std::string key(GetKey(url.host())); |
1665 FindCookiesForKey(key, url, options, current_time, update_access_time, | 1733 FindCookiesForKey(key, url, options, current_time, update_access_time, |
1666 cookies); | 1734 cookies); |
1667 | 1735 |
1668 // See if we can search for domain cookies, i.e. if the host has a TLD + 1. | 1736 // See if we can search for domain cookies, i.e. if the host has a TLD + 1. |
1669 const std::string domain(cookie_utils::GetEffectiveDomain(url.scheme(), | 1737 const std::string domain(GetEffectiveDomain(url.scheme(), key)); |
1670 key)); | |
1671 if (domain.empty()) | 1738 if (domain.empty()) |
1672 return; | 1739 return; |
1673 DCHECK_LE(domain.length(), key.length()); | 1740 DCHECK_LE(domain.length(), key.length()); |
1674 DCHECK_EQ(0, key.compare(key.length() - domain.length(), domain.length(), | 1741 DCHECK_EQ(0, key.compare(key.length() - domain.length(), domain.length(), |
1675 domain)); | 1742 domain)); |
1676 | 1743 |
1677 // Walk through the string and query at the dot points (GURL should have | 1744 // Walk through the string and query at the dot points (GURL should have |
1678 // canonicalized the dots, so this should be safe). Stop once we reach the | 1745 // canonicalized the dots, so this should be safe). Stop once we reach the |
1679 // domain + registry; we can't write cookies past this point, and with some | 1746 // domain + registry; we can't write cookies past this point, and with some |
1680 // registrars other domains can, in which case we don't want to read their | 1747 // registrars other domains can, in which case we don't want to read their |
(...skipping 888 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2569 else | 2636 else |
2570 SetSessionCookieExpiryTime(); | 2637 SetSessionCookieExpiryTime(); |
2571 | 2638 |
2572 // Do the best we can with the domain. | 2639 // Do the best we can with the domain. |
2573 std::string cookie_domain; | 2640 std::string cookie_domain; |
2574 std::string domain_string; | 2641 std::string domain_string; |
2575 if (pc.HasDomain()) { | 2642 if (pc.HasDomain()) { |
2576 domain_string = pc.Domain(); | 2643 domain_string = pc.Domain(); |
2577 } | 2644 } |
2578 bool result | 2645 bool result |
2579 = cookie_utils::GetCookieDomainWithString(url, domain_string, | 2646 = GetCookieDomainWithString(url, domain_string, |
2580 &cookie_domain); | 2647 &cookie_domain); |
2581 // Caller is responsible for passing in good arguments. | 2648 // Caller is responsible for passing in good arguments. |
2582 DCHECK(result); | 2649 DCHECK(result); |
2583 domain_ = cookie_domain; | 2650 domain_ = cookie_domain; |
2584 } | 2651 } |
2585 | 2652 |
2586 CookieMonster::CanonicalCookie::~CanonicalCookie() { | 2653 CookieMonster::CanonicalCookie::~CanonicalCookie() { |
2587 } | 2654 } |
2588 | 2655 |
2589 std::string CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | 2656 std::string CookieMonster::CanonicalCookie::GetCookieSourceFromURL( |
2590 const GURL& url) { | 2657 const GURL& url) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2653 if (parsed_name != name) | 2720 if (parsed_name != name) |
2654 return NULL; | 2721 return NULL; |
2655 std::string parsed_value = ParsedCookie::ParseValueString(value); | 2722 std::string parsed_value = ParsedCookie::ParseValueString(value); |
2656 if (parsed_value != value) | 2723 if (parsed_value != value) |
2657 return NULL; | 2724 return NULL; |
2658 | 2725 |
2659 std::string parsed_domain = ParsedCookie::ParseValueString(domain); | 2726 std::string parsed_domain = ParsedCookie::ParseValueString(domain); |
2660 if (parsed_domain != domain) | 2727 if (parsed_domain != domain) |
2661 return NULL; | 2728 return NULL; |
2662 std::string cookie_domain; | 2729 std::string cookie_domain; |
2663 if (!cookie_utils::GetCookieDomainWithString(url, parsed_domain, | 2730 if (!GetCookieDomainWithString(url, parsed_domain, &cookie_domain)) |
2664 &cookie_domain)) { | |
2665 return NULL; | 2731 return NULL; |
2666 } | |
2667 | 2732 |
2668 std::string parsed_path = ParsedCookie::ParseValueString(path); | 2733 std::string parsed_path = ParsedCookie::ParseValueString(path); |
2669 if (parsed_path != path) | 2734 if (parsed_path != path) |
2670 return NULL; | 2735 return NULL; |
2671 | 2736 |
2672 std::string cookie_path = CanonPathWithString(url, parsed_path); | 2737 std::string cookie_path = CanonPathWithString(url, parsed_path); |
2673 // Expect that the path was either not specified (empty), or is valid. | 2738 // Expect that the path was either not specified (empty), or is valid. |
2674 if (!parsed_path.empty() && cookie_path != parsed_path) | 2739 if (!parsed_path.empty() && cookie_path != parsed_path) |
2675 return NULL; | 2740 return NULL; |
2676 // Canonicalize path again to make sure it escapes characters as needed. | 2741 // Canonicalize path again to make sure it escapes characters as needed. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2762 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2827 std::string CookieMonster::CanonicalCookie::DebugString() const { |
2763 return base::StringPrintf( | 2828 return base::StringPrintf( |
2764 "name: %s value: %s domain: %s path: %s creation: %" | 2829 "name: %s value: %s domain: %s path: %s creation: %" |
2765 PRId64, | 2830 PRId64, |
2766 name_.c_str(), value_.c_str(), | 2831 name_.c_str(), value_.c_str(), |
2767 domain_.c_str(), path_.c_str(), | 2832 domain_.c_str(), path_.c_str(), |
2768 static_cast<int64>(creation_date_.ToTimeT())); | 2833 static_cast<int64>(creation_date_.ToTimeT())); |
2769 } | 2834 } |
2770 | 2835 |
2771 } // namespace | 2836 } // namespace |
OLD | NEW |