| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 29 matching lines...) Expand all Loading... |
| 40 * the provisions above, a recipient may use your version of this file under | 40 * the provisions above, a recipient may use your version of this file under |
| 41 * the terms of any one of the MPL, the GPL or the LGPL. | 41 * the terms of any one of the MPL, the GPL or the LGPL. |
| 42 * | 42 * |
| 43 * ***** END LICENSE BLOCK ***** */ | 43 * ***** END LICENSE BLOCK ***** */ |
| 44 | 44 |
| 45 #include "net/cookies/canonical_cookie.h" | 45 #include "net/cookies/canonical_cookie.h" |
| 46 | 46 |
| 47 #include "base/format_macros.h" | 47 #include "base/format_macros.h" |
| 48 #include "base/logging.h" | 48 #include "base/logging.h" |
| 49 #include "base/metrics/histogram_macros.h" | 49 #include "base/metrics/histogram_macros.h" |
| 50 #include "base/strings/string_util.h" | |
| 51 #include "base/strings/stringprintf.h" | 50 #include "base/strings/stringprintf.h" |
| 52 #include "net/cookies/cookie_util.h" | 51 #include "net/cookies/cookie_util.h" |
| 53 #include "net/cookies/parsed_cookie.h" | 52 #include "net/cookies/parsed_cookie.h" |
| 54 #include "url/gurl.h" | 53 #include "url/gurl.h" |
| 55 #include "url/url_canon.h" | 54 #include "url/url_canon.h" |
| 56 | 55 |
| 57 using base::Time; | 56 using base::Time; |
| 58 using base::TimeDelta; | 57 using base::TimeDelta; |
| 59 | 58 |
| 60 namespace net { | 59 namespace net { |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 // would also make no sense for our prefix match. The code that | 342 // would also make no sense for our prefix match. The code that |
| 344 // creates a CanonicalCookie should make sure the path is never zero length, | 343 // creates a CanonicalCookie should make sure the path is never zero length, |
| 345 // but we double check anyway. | 344 // but we double check anyway. |
| 346 if (path_.empty()) | 345 if (path_.empty()) |
| 347 return false; | 346 return false; |
| 348 | 347 |
| 349 // The Mozilla code broke this into three cases, based on if the cookie path | 348 // The Mozilla code broke this into three cases, based on if the cookie path |
| 350 // was longer, the same length, or shorter than the length of the url path. | 349 // was longer, the same length, or shorter than the length of the url path. |
| 351 // I think the approach below is simpler. | 350 // I think the approach below is simpler. |
| 352 | 351 |
| 353 // Make sure the cookie path is a prefix of the url path. If the url path is | 352 // Make sure the cookie path is a prefix of the url path. If the |
| 354 // shorter than the cookie path, then the cookie path can't be a prefix. | 353 // url path is shorter than the cookie path, then the cookie path |
| 355 if (!base::StartsWith(url_path, path_, base::CompareCase::SENSITIVE)) | 354 // can't be a prefix. |
| 355 if (url_path.find(path_) != 0) |
| 356 return false; | 356 return false; |
| 357 | 357 |
| 358 // |url_path| is >= |path_|, and |path_| is a prefix of |url_path|. If they | 358 // Now we know that url_path is >= cookie_path, and that cookie_path |
| 359 // are the are the same length then they are identical, otherwise need an | 359 // is a prefix of url_path. If they are the are the same length then |
| 360 // additional check: | 360 // they are identical, otherwise we need an additional check: |
| 361 | 361 |
| 362 // In order to avoid in correctly matching a cookie path of /blah | 362 // In order to avoid in correctly matching a cookie path of /blah |
| 363 // with a request path of '/blahblah/', we need to make sure that either | 363 // with a request path of '/blahblah/', we need to make sure that either |
| 364 // the cookie path ends in a trailing '/', or that we prefix up to a '/' | 364 // the cookie path ends in a trailing '/', or that we prefix up to a '/' |
| 365 // in the url path. Since we know that the url path length is greater | 365 // in the url path. Since we know that the url path length is greater |
| 366 // than the cookie path length, it's safe to index one byte past. | 366 // than the cookie path length, it's safe to index one byte past. |
| 367 if (path_.length() != url_path.length() && path_.back() != '/' && | 367 if (path_.length() != url_path.length() && path_.back() != '/' && |
| 368 url_path[path_.length()] != '/') { | 368 url_path[path_.length()] != '/') |
| 369 return false; | 369 return false; |
| 370 } | |
| 371 | 370 |
| 372 return true; | 371 return true; |
| 373 } | 372 } |
| 374 | 373 |
| 375 bool CanonicalCookie::IsDomainMatch(const std::string& host) const { | 374 bool CanonicalCookie::IsDomainMatch(const std::string& host) const { |
| 376 // Can domain match in two ways; as a domain cookie (where the cookie | 375 // Can domain match in two ways; as a domain cookie (where the cookie |
| 377 // domain begins with ".") or as a host cookie (where it doesn't). | 376 // domain begins with ".") or as a host cookie (where it doesn't). |
| 378 | 377 |
| 379 // Some consumers of the CookieMonster expect to set cookies on | 378 // Some consumers of the CookieMonster expect to set cookies on |
| 380 // URLs like http://.strange.url. To retrieve cookies in this instance, | 379 // URLs like http://.strange.url. To retrieve cookies in this instance, |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE) | 506 if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE) |
| 508 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic(); | 507 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic(); |
| 509 if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) { | 508 if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) { |
| 510 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() && | 509 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() && |
| 511 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/"; | 510 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/"; |
| 512 } | 511 } |
| 513 return true; | 512 return true; |
| 514 } | 513 } |
| 515 | 514 |
| 516 } // namespace net | 515 } // namespace net |
| OLD | NEW |