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/net_util.h" | 64 #include "net/base/cookie_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 | |
256 // Determine the cookie domain to use for setting the specified cookie. | 191 // Determine the cookie domain to use for setting the specified cookie. |
257 bool GetCookieDomain(const GURL& url, | 192 bool GetCookieDomain(const GURL& url, |
258 const CookieMonster::ParsedCookie& pc, | 193 const CookieMonster::ParsedCookie& pc, |
259 std::string* result) { | 194 std::string* result) { |
260 std::string domain_string; | 195 std::string domain_string; |
261 if (pc.HasDomain()) | 196 if (pc.HasDomain()) |
262 domain_string = pc.Domain(); | 197 domain_string = pc.Domain(); |
263 return GetCookieDomainWithString(url, domain_string, result); | 198 return cookie_util::GetCookieDomainWithString(url, domain_string, result); |
264 } | 199 } |
265 | 200 |
266 std::string CanonPathWithString(const GURL& url, | 201 std::string CanonPathWithString(const GURL& url, |
267 const std::string& path_string) { | 202 const std::string& path_string) { |
268 // The RFC says the path should be a prefix of the current URL path. | 203 // The RFC says the path should be a prefix of the current URL path. |
269 // However, Mozilla allows you to set any path for compatibility with | 204 // However, Mozilla allows you to set any path for compatibility with |
270 // broken websites. We unfortunately will mimic this behavior. We try | 205 // broken websites. We unfortunately will mimic this behavior. We try |
271 // to be generous and accept cookies with an invalid path attribute, and | 206 // to be generous and accept cookies with an invalid path attribute, and |
272 // default the path to something reasonable. | 207 // default the path to something reasonable. |
273 | 208 |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 return Time::FromUTCExploded(exploded); | 502 return Time::FromUTCExploded(exploded); |
568 } | 503 } |
569 | 504 |
570 // One of our values was out of expected range. For well-formed input, | 505 // One of our values was out of expected range. For well-formed input, |
571 // the following check would be reasonable: | 506 // the following check would be reasonable: |
572 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; | 507 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; |
573 | 508 |
574 return Time(); | 509 return Time(); |
575 } | 510 } |
576 | 511 |
577 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { | |
578 return (domain_string.empty() || domain_string[0] != '.'); | |
579 } | |
580 | |
581 // Task classes for queueing the coming request. | 512 // Task classes for queueing the coming request. |
582 | 513 |
583 class CookieMonster::CookieMonsterTask | 514 class CookieMonster::CookieMonsterTask |
584 : public base::RefCountedThreadSafe<CookieMonsterTask> { | 515 : public base::RefCountedThreadSafe<CookieMonsterTask> { |
585 public: | 516 public: |
586 // Runs the task and invokes the client callback on the thread that | 517 // Runs the task and invokes the client callback on the thread that |
587 // originally constructed the task. | 518 // originally constructed the task. |
588 virtual void Run() = 0; | 519 virtual void Run() = 0; |
589 | 520 |
590 protected: | 521 protected: |
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1121 void CookieMonster::DoCookieTaskForURL( | 1052 void CookieMonster::DoCookieTaskForURL( |
1122 const scoped_refptr<CookieMonsterTask>& task_item, | 1053 const scoped_refptr<CookieMonsterTask>& task_item, |
1123 const GURL& url) { | 1054 const GURL& url) { |
1124 { | 1055 { |
1125 base::AutoLock autolock(lock_); | 1056 base::AutoLock autolock(lock_); |
1126 InitIfNecessary(); | 1057 InitIfNecessary(); |
1127 // If cookies for the requested domain key (eTLD+1) have been loaded from DB | 1058 // If cookies for the requested domain key (eTLD+1) have been loaded from DB |
1128 // then run the task, otherwise load from DB. | 1059 // then run the task, otherwise load from DB. |
1129 if (!loaded_) { | 1060 if (!loaded_) { |
1130 // Checks if the domain key has been loaded. | 1061 // Checks if the domain key has been loaded. |
1131 std::string key(GetEffectiveDomain(url.scheme(), url.host())); | 1062 std::string key(cookie_util::GetEffectiveDomain(url.scheme(), |
| 1063 url.host())); |
1132 if (keys_loaded_.find(key) == keys_loaded_.end()) { | 1064 if (keys_loaded_.find(key) == keys_loaded_.end()) { |
1133 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > | 1065 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > |
1134 ::iterator it = tasks_queued_.find(key); | 1066 ::iterator it = tasks_queued_.find(key); |
1135 if (it == tasks_queued_.end()) { | 1067 if (it == tasks_queued_.end()) { |
1136 store_->LoadCookiesForKey(key, | 1068 store_->LoadCookiesForKey(key, |
1137 base::Bind(&CookieMonster::OnKeyLoaded, this, key)); | 1069 base::Bind(&CookieMonster::OnKeyLoaded, this, key)); |
1138 it = tasks_queued_.insert(std::make_pair(key, | 1070 it = tasks_queued_.insert(std::make_pair(key, |
1139 std::deque<scoped_refptr<CookieMonsterTask> >())).first; | 1071 std::deque<scoped_refptr<CookieMonsterTask> >())).first; |
1140 } | 1072 } |
1141 it->second.push_back(task_item); | 1073 it->second.push_back(task_item); |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1727 } else { | 1659 } else { |
1728 // Need to probe for all domains that might have relevant | 1660 // Need to probe for all domains that might have relevant |
1729 // cookies for us. | 1661 // cookies for us. |
1730 | 1662 |
1731 // Query for the full host, For example: 'a.c.blah.com'. | 1663 // Query for the full host, For example: 'a.c.blah.com'. |
1732 std::string key(GetKey(url.host())); | 1664 std::string key(GetKey(url.host())); |
1733 FindCookiesForKey(key, url, options, current_time, update_access_time, | 1665 FindCookiesForKey(key, url, options, current_time, update_access_time, |
1734 cookies); | 1666 cookies); |
1735 | 1667 |
1736 // See if we can search for domain cookies, i.e. if the host has a TLD + 1. | 1668 // See if we can search for domain cookies, i.e. if the host has a TLD + 1. |
1737 const std::string domain(GetEffectiveDomain(url.scheme(), key)); | 1669 const std::string domain(cookie_util::GetEffectiveDomain(url.scheme(), |
| 1670 key)); |
1738 if (domain.empty()) | 1671 if (domain.empty()) |
1739 return; | 1672 return; |
1740 DCHECK_LE(domain.length(), key.length()); | 1673 DCHECK_LE(domain.length(), key.length()); |
1741 DCHECK_EQ(0, key.compare(key.length() - domain.length(), domain.length(), | 1674 DCHECK_EQ(0, key.compare(key.length() - domain.length(), domain.length(), |
1742 domain)); | 1675 domain)); |
1743 | 1676 |
1744 // Walk through the string and query at the dot points (GURL should have | 1677 // Walk through the string and query at the dot points (GURL should have |
1745 // canonicalized the dots, so this should be safe). Stop once we reach the | 1678 // canonicalized the dots, so this should be safe). Stop once we reach the |
1746 // domain + registry; we can't write cookies past this point, and with some | 1679 // domain + registry; we can't write cookies past this point, and with some |
1747 // registrars other domains can, in which case we don't want to read their | 1680 // 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... |
2636 else | 2569 else |
2637 SetSessionCookieExpiryTime(); | 2570 SetSessionCookieExpiryTime(); |
2638 | 2571 |
2639 // Do the best we can with the domain. | 2572 // Do the best we can with the domain. |
2640 std::string cookie_domain; | 2573 std::string cookie_domain; |
2641 std::string domain_string; | 2574 std::string domain_string; |
2642 if (pc.HasDomain()) { | 2575 if (pc.HasDomain()) { |
2643 domain_string = pc.Domain(); | 2576 domain_string = pc.Domain(); |
2644 } | 2577 } |
2645 bool result | 2578 bool result |
2646 = GetCookieDomainWithString(url, domain_string, | 2579 = cookie_util::GetCookieDomainWithString(url, domain_string, |
2647 &cookie_domain); | 2580 &cookie_domain); |
2648 // Caller is responsible for passing in good arguments. | 2581 // Caller is responsible for passing in good arguments. |
2649 DCHECK(result); | 2582 DCHECK(result); |
2650 domain_ = cookie_domain; | 2583 domain_ = cookie_domain; |
2651 } | 2584 } |
2652 | 2585 |
2653 CookieMonster::CanonicalCookie::~CanonicalCookie() { | 2586 CookieMonster::CanonicalCookie::~CanonicalCookie() { |
2654 } | 2587 } |
2655 | 2588 |
2656 std::string CookieMonster::CanonicalCookie::GetCookieSourceFromURL( | 2589 std::string CookieMonster::CanonicalCookie::GetCookieSourceFromURL( |
2657 const GURL& url) { | 2590 const GURL& url) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2720 if (parsed_name != name) | 2653 if (parsed_name != name) |
2721 return NULL; | 2654 return NULL; |
2722 std::string parsed_value = ParsedCookie::ParseValueString(value); | 2655 std::string parsed_value = ParsedCookie::ParseValueString(value); |
2723 if (parsed_value != value) | 2656 if (parsed_value != value) |
2724 return NULL; | 2657 return NULL; |
2725 | 2658 |
2726 std::string parsed_domain = ParsedCookie::ParseValueString(domain); | 2659 std::string parsed_domain = ParsedCookie::ParseValueString(domain); |
2727 if (parsed_domain != domain) | 2660 if (parsed_domain != domain) |
2728 return NULL; | 2661 return NULL; |
2729 std::string cookie_domain; | 2662 std::string cookie_domain; |
2730 if (!GetCookieDomainWithString(url, parsed_domain, &cookie_domain)) | 2663 if (!cookie_util::GetCookieDomainWithString(url, parsed_domain, |
| 2664 &cookie_domain)) { |
2731 return NULL; | 2665 return NULL; |
| 2666 } |
2732 | 2667 |
2733 std::string parsed_path = ParsedCookie::ParseValueString(path); | 2668 std::string parsed_path = ParsedCookie::ParseValueString(path); |
2734 if (parsed_path != path) | 2669 if (parsed_path != path) |
2735 return NULL; | 2670 return NULL; |
2736 | 2671 |
2737 std::string cookie_path = CanonPathWithString(url, parsed_path); | 2672 std::string cookie_path = CanonPathWithString(url, parsed_path); |
2738 // Expect that the path was either not specified (empty), or is valid. | 2673 // Expect that the path was either not specified (empty), or is valid. |
2739 if (!parsed_path.empty() && cookie_path != parsed_path) | 2674 if (!parsed_path.empty() && cookie_path != parsed_path) |
2740 return NULL; | 2675 return NULL; |
2741 // Canonicalize path again to make sure it escapes characters as needed. | 2676 // Canonicalize path again to make sure it escapes characters as needed. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2827 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2762 std::string CookieMonster::CanonicalCookie::DebugString() const { |
2828 return base::StringPrintf( | 2763 return base::StringPrintf( |
2829 "name: %s value: %s domain: %s path: %s creation: %" | 2764 "name: %s value: %s domain: %s path: %s creation: %" |
2830 PRId64, | 2765 PRId64, |
2831 name_.c_str(), value_.c_str(), | 2766 name_.c_str(), value_.c_str(), |
2832 domain_.c_str(), path_.c_str(), | 2767 domain_.c_str(), path_.c_str(), |
2833 static_cast<int64>(creation_date_.ToTimeT())); | 2768 static_cast<int64>(creation_date_.ToTimeT())); |
2834 } | 2769 } |
2835 | 2770 |
2836 } // namespace | 2771 } // namespace |
OLD | NEW |