| 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 28 matching lines...) Expand all Loading... |
| 39 * and other provisions required by the GPL or the LGPL. If you do not delete | 39 * and other provisions required by the GPL or the LGPL. If you do not delete |
| 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/memory/ptr_util.h" |
| 49 #include "base/metrics/histogram_macros.h" | 50 #include "base/metrics/histogram_macros.h" |
| 50 #include "base/strings/string_util.h" | 51 #include "base/strings/string_util.h" |
| 51 #include "base/strings/stringprintf.h" | 52 #include "base/strings/stringprintf.h" |
| 52 #include "net/cookies/cookie_util.h" | 53 #include "net/cookies/cookie_util.h" |
| 53 #include "net/cookies/parsed_cookie.h" | 54 #include "net/cookies/parsed_cookie.h" |
| 54 #include "url/gurl.h" | 55 #include "url/gurl.h" |
| 55 #include "url/url_canon.h" | 56 #include "url/url_canon.h" |
| 56 | 57 |
| 57 using base::Time; | 58 using base::Time; |
| 58 using base::TimeDelta; | 59 using base::TimeDelta; |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 base::Time parsed_expiry = cookie_util::ParseCookieTime(pc.Expires()); | 186 base::Time parsed_expiry = cookie_util::ParseCookieTime(pc.Expires()); |
| 186 if (!parsed_expiry.is_null()) | 187 if (!parsed_expiry.is_null()) |
| 187 return parsed_expiry + (current - server_time); | 188 return parsed_expiry + (current - server_time); |
| 188 } | 189 } |
| 189 | 190 |
| 190 // Invalid or no expiration, persistent cookie. | 191 // Invalid or no expiration, persistent cookie. |
| 191 return Time(); | 192 return Time(); |
| 192 } | 193 } |
| 193 | 194 |
| 194 // static | 195 // static |
| 195 scoped_ptr<CanonicalCookie> CanonicalCookie::Create( | 196 std::unique_ptr<CanonicalCookie> CanonicalCookie::Create( |
| 196 const GURL& url, | 197 const GURL& url, |
| 197 const std::string& cookie_line, | 198 const std::string& cookie_line, |
| 198 const base::Time& creation_time, | 199 const base::Time& creation_time, |
| 199 const CookieOptions& options) { | 200 const CookieOptions& options) { |
| 200 ParsedCookie parsed_cookie(cookie_line); | 201 ParsedCookie parsed_cookie(cookie_line); |
| 201 | 202 |
| 202 if (!parsed_cookie.IsValid()) { | 203 if (!parsed_cookie.IsValid()) { |
| 203 VLOG(kVlogSetCookies) << "WARNING: Couldn't parse cookie"; | 204 VLOG(kVlogSetCookies) << "WARNING: Couldn't parse cookie"; |
| 204 return nullptr; | 205 return nullptr; |
| 205 } | 206 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 CookiePrefix prefix = CanonicalCookie::GetCookiePrefix(parsed_cookie.Name()); | 239 CookiePrefix prefix = CanonicalCookie::GetCookiePrefix(parsed_cookie.Name()); |
| 239 bool is_cookie_valid = | 240 bool is_cookie_valid = |
| 240 CanonicalCookie::IsCookiePrefixValid(prefix, url, parsed_cookie); | 241 CanonicalCookie::IsCookiePrefixValid(prefix, url, parsed_cookie); |
| 241 CanonicalCookie::RecordCookiePrefixMetrics(prefix, is_cookie_valid); | 242 CanonicalCookie::RecordCookiePrefixMetrics(prefix, is_cookie_valid); |
| 242 if (!is_cookie_valid) { | 243 if (!is_cookie_valid) { |
| 243 VLOG(kVlogSetCookies) | 244 VLOG(kVlogSetCookies) |
| 244 << "Create() failed because the cookie violated prefix rules."; | 245 << "Create() failed because the cookie violated prefix rules."; |
| 245 return nullptr; | 246 return nullptr; |
| 246 } | 247 } |
| 247 | 248 |
| 248 return make_scoped_ptr(new CanonicalCookie( | 249 return base::WrapUnique(new CanonicalCookie( |
| 249 url, parsed_cookie.Name(), parsed_cookie.Value(), cookie_domain, | 250 url, parsed_cookie.Name(), parsed_cookie.Value(), cookie_domain, |
| 250 cookie_path, creation_time, cookie_expires, creation_time, | 251 cookie_path, creation_time, cookie_expires, creation_time, |
| 251 parsed_cookie.IsSecure(), parsed_cookie.IsHttpOnly(), | 252 parsed_cookie.IsSecure(), parsed_cookie.IsHttpOnly(), |
| 252 parsed_cookie.SameSite(), parsed_cookie.Priority())); | 253 parsed_cookie.SameSite(), parsed_cookie.Priority())); |
| 253 } | 254 } |
| 254 | 255 |
| 255 // static | 256 // static |
| 256 scoped_ptr<CanonicalCookie> CanonicalCookie::Create( | 257 std::unique_ptr<CanonicalCookie> CanonicalCookie::Create( |
| 257 const GURL& url, | 258 const GURL& url, |
| 258 const std::string& name, | 259 const std::string& name, |
| 259 const std::string& value, | 260 const std::string& value, |
| 260 const std::string& domain, | 261 const std::string& domain, |
| 261 const std::string& path, | 262 const std::string& path, |
| 262 const base::Time& creation, | 263 const base::Time& creation, |
| 263 const base::Time& expiration, | 264 const base::Time& expiration, |
| 264 bool secure, | 265 bool secure, |
| 265 bool http_only, | 266 bool http_only, |
| 266 CookieSameSite same_site, | 267 CookieSameSite same_site, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 297 return nullptr; | 298 return nullptr; |
| 298 // Canonicalize path again to make sure it escapes characters as needed. | 299 // Canonicalize path again to make sure it escapes characters as needed. |
| 299 url::Component path_component(0, cookie_path.length()); | 300 url::Component path_component(0, cookie_path.length()); |
| 300 url::RawCanonOutputT<char> canon_path; | 301 url::RawCanonOutputT<char> canon_path; |
| 301 url::Component canon_path_component; | 302 url::Component canon_path_component; |
| 302 url::CanonicalizePath(cookie_path.data(), path_component, &canon_path, | 303 url::CanonicalizePath(cookie_path.data(), path_component, &canon_path, |
| 303 &canon_path_component); | 304 &canon_path_component); |
| 304 cookie_path = std::string(canon_path.data() + canon_path_component.begin, | 305 cookie_path = std::string(canon_path.data() + canon_path_component.begin, |
| 305 canon_path_component.len); | 306 canon_path_component.len); |
| 306 | 307 |
| 307 return make_scoped_ptr(new CanonicalCookie( | 308 return base::WrapUnique(new CanonicalCookie( |
| 308 url, parsed_name, parsed_value, cookie_domain, cookie_path, creation, | 309 url, parsed_name, parsed_value, cookie_domain, cookie_path, creation, |
| 309 expiration, creation, secure, http_only, same_site, priority)); | 310 expiration, creation, secure, http_only, same_site, priority)); |
| 310 } | 311 } |
| 311 | 312 |
| 312 bool CanonicalCookie::IsOnPath(const std::string& url_path) const { | 313 bool CanonicalCookie::IsOnPath(const std::string& url_path) const { |
| 313 | 314 |
| 314 // A zero length would be unsafe for our trailing '/' checks, and | 315 // A zero length would be unsafe for our trailing '/' checks, and |
| 315 // would also make no sense for our prefix match. The code that | 316 // would also make no sense for our prefix match. The code that |
| 316 // creates a CanonicalCookie should make sure the path is never zero length, | 317 // creates a CanonicalCookie should make sure the path is never zero length, |
| 317 // but we double check anyway. | 318 // but we double check anyway. |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE) | 494 if (prefix == CanonicalCookie::COOKIE_PREFIX_SECURE) |
| 494 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic(); | 495 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic(); |
| 495 if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) { | 496 if (prefix == CanonicalCookie::COOKIE_PREFIX_HOST) { |
| 496 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() && | 497 return parsed_cookie.IsSecure() && url.SchemeIsCryptographic() && |
| 497 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/"; | 498 !parsed_cookie.HasDomain() && parsed_cookie.Path() == "/"; |
| 498 } | 499 } |
| 499 return true; | 500 return true; |
| 500 } | 501 } |
| 501 | 502 |
| 502 } // namespace net | 503 } // namespace net |
| OLD | NEW |