| 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 1174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1185 | 1185 |
| 1186 // TODO(abarth): Take these values as parameters. | 1186 // TODO(abarth): Take these values as parameters. |
| 1187 std::string mac_key; | 1187 std::string mac_key; |
| 1188 std::string mac_algorithm; | 1188 std::string mac_algorithm; |
| 1189 | 1189 |
| 1190 scoped_ptr<CanonicalCookie> cc; | 1190 scoped_ptr<CanonicalCookie> cc; |
| 1191 cc.reset(CanonicalCookie::Create( | 1191 cc.reset(CanonicalCookie::Create( |
| 1192 url, name, value, domain, path, | 1192 url, name, value, domain, path, |
| 1193 mac_key, mac_algorithm, | 1193 mac_key, mac_algorithm, |
| 1194 creation_time, expiration_time, | 1194 creation_time, expiration_time, |
| 1195 secure, http_only, !expiration_time.is_null())); | 1195 secure, http_only)); |
| 1196 | 1196 |
| 1197 if (!cc.get()) | 1197 if (!cc.get()) |
| 1198 return false; | 1198 return false; |
| 1199 | 1199 |
| 1200 CookieOptions options; | 1200 CookieOptions options; |
| 1201 options.set_include_httponly(); | 1201 options.set_include_httponly(); |
| 1202 return SetCanonicalCookie(&cc, creation_time, options); | 1202 return SetCanonicalCookie(&cc, creation_time, options); |
| 1203 } | 1203 } |
| 1204 | 1204 |
| 1205 bool CookieMonster::InitializeFrom(const CookieList& list) { | 1205 bool CookieMonster::InitializeFrom(const CookieList& list) { |
| (...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1909 } | 1909 } |
| 1910 | 1910 |
| 1911 std::string cookie_path = CanonPath(url, pc); | 1911 std::string cookie_path = CanonPath(url, pc); |
| 1912 std::string mac_key = pc.HasMACKey() ? pc.MACKey() : std::string(); | 1912 std::string mac_key = pc.HasMACKey() ? pc.MACKey() : std::string(); |
| 1913 std::string mac_algorithm = pc.HasMACAlgorithm() ? | 1913 std::string mac_algorithm = pc.HasMACAlgorithm() ? |
| 1914 pc.MACAlgorithm() : std::string(); | 1914 pc.MACAlgorithm() : std::string(); |
| 1915 | 1915 |
| 1916 scoped_ptr<CanonicalCookie> cc; | 1916 scoped_ptr<CanonicalCookie> cc; |
| 1917 Time cookie_expires = CanonExpiration(pc, creation_time); | 1917 Time cookie_expires = CanonExpiration(pc, creation_time); |
| 1918 | 1918 |
| 1919 bool session_only = options.force_session() || cookie_expires.is_null(); | |
| 1920 cc.reset(new CanonicalCookie(url, pc.Name(), pc.Value(), cookie_domain, | 1919 cc.reset(new CanonicalCookie(url, pc.Name(), pc.Value(), cookie_domain, |
| 1921 cookie_path, mac_key, mac_algorithm, | 1920 cookie_path, mac_key, mac_algorithm, |
| 1922 creation_time, cookie_expires, | 1921 creation_time, cookie_expires, |
| 1923 creation_time, pc.IsSecure(), pc.IsHttpOnly(), | 1922 creation_time, pc.IsSecure(), pc.IsHttpOnly())); |
| 1924 !cookie_expires.is_null(), | |
| 1925 !session_only)); | |
| 1926 | 1923 |
| 1927 if (!cc.get()) { | 1924 if (!cc.get()) { |
| 1928 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie"; | 1925 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie"; |
| 1929 return false; | 1926 return false; |
| 1930 } | 1927 } |
| 1931 return SetCanonicalCookie(&cc, creation_time, options); | 1928 return SetCanonicalCookie(&cc, creation_time, options); |
| 1932 } | 1929 } |
| 1933 | 1930 |
| 1934 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc, | 1931 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc, |
| 1935 const Time& creation_time, | 1932 const Time& creation_time, |
| 1936 const CookieOptions& options) { | 1933 const CookieOptions& options) { |
| 1937 const std::string key(GetKey((*cc)->Domain())); | 1934 const std::string key(GetKey((*cc)->Domain())); |
| 1938 bool already_expired = (*cc)->IsExpired(creation_time); | 1935 bool already_expired = (*cc)->IsExpired(creation_time); |
| 1939 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(), | 1936 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(), |
| 1940 already_expired)) { | 1937 already_expired)) { |
| 1941 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie"; | 1938 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie"; |
| 1942 return false; | 1939 return false; |
| 1943 } | 1940 } |
| 1944 | 1941 |
| 1945 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: " | 1942 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: " |
| 1946 << (*cc)->DebugString(); | 1943 << (*cc)->DebugString(); |
| 1947 | 1944 |
| 1948 // Realize that we might be setting an expired cookie, and the only point | 1945 // Realize that we might be setting an expired cookie, and the only point |
| 1949 // was to delete the cookie which we've already done. | 1946 // was to delete the cookie which we've already done. |
| 1950 if (!already_expired || keep_expired_cookies_) { | 1947 if (!already_expired || keep_expired_cookies_) { |
| 1951 // See InitializeHistograms() for details. | 1948 // See InitializeHistograms() for details. |
| 1952 if ((*cc)->DoesExpire()) { | 1949 if ((*cc)->IsPersistent()) { |
| 1953 histogram_expiration_duration_minutes_->Add( | 1950 histogram_expiration_duration_minutes_->Add( |
| 1954 ((*cc)->ExpiryDate() - creation_time).InMinutes()); | 1951 ((*cc)->ExpiryDate() - creation_time).InMinutes()); |
| 1955 } | 1952 } |
| 1956 | 1953 |
| 1957 InternalInsertCookie(key, cc->release(), true); | 1954 InternalInsertCookie(key, cc->release(), true); |
| 1958 } | 1955 } |
| 1959 | 1956 |
| 1960 // We assume that hopefully setting a cookie will be less common than | 1957 // We assume that hopefully setting a cookie will be less common than |
| 1961 // querying a cookie. Since setting a cookie can put us over our limits, | 1958 // querying a cookie. Since setting a cookie can put us over our limits, |
| 1962 // make sure that we garbage collect... We can also make the assumption that | 1959 // make sure that we garbage collect... We can also make the assumption that |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2602 } else if (pairs_[i].first == kHttpOnlyTokenName) { | 2599 } else if (pairs_[i].first == kHttpOnlyTokenName) { |
| 2603 httponly_index_ = i; | 2600 httponly_index_ = i; |
| 2604 } else { | 2601 } else { |
| 2605 /* some attribute we don't know or don't care about. */ | 2602 /* some attribute we don't know or don't care about. */ |
| 2606 } | 2603 } |
| 2607 } | 2604 } |
| 2608 } | 2605 } |
| 2609 | 2606 |
| 2610 CookieMonster::CanonicalCookie::CanonicalCookie() | 2607 CookieMonster::CanonicalCookie::CanonicalCookie() |
| 2611 : secure_(false), | 2608 : secure_(false), |
| 2612 httponly_(false), | 2609 httponly_(false) { |
| 2613 has_expires_(false), | |
| 2614 is_persistent_(false) { | |
| 2615 SetSessionCookieExpiryTime(); | 2610 SetSessionCookieExpiryTime(); |
| 2616 } | 2611 } |
| 2617 | 2612 |
| 2618 CookieMonster::CanonicalCookie::CanonicalCookie( | 2613 CookieMonster::CanonicalCookie::CanonicalCookie( |
| 2619 const GURL& url, const std::string& name, const std::string& value, | 2614 const GURL& url, const std::string& name, const std::string& value, |
| 2620 const std::string& domain, const std::string& path, | 2615 const std::string& domain, const std::string& path, |
| 2621 const std::string& mac_key, const std::string& mac_algorithm, | 2616 const std::string& mac_key, const std::string& mac_algorithm, |
| 2622 const base::Time& creation, const base::Time& expiration, | 2617 const base::Time& creation, const base::Time& expiration, |
| 2623 const base::Time& last_access, bool secure, bool httponly, bool has_expires, | 2618 const base::Time& last_access, bool secure, bool httponly) |
| 2624 bool is_persistent) | |
| 2625 : source_(GetCookieSourceFromURL(url)), | 2619 : source_(GetCookieSourceFromURL(url)), |
| 2626 name_(name), | 2620 name_(name), |
| 2627 value_(value), | 2621 value_(value), |
| 2628 domain_(domain), | 2622 domain_(domain), |
| 2629 path_(path), | 2623 path_(path), |
| 2630 mac_key_(mac_key), | 2624 mac_key_(mac_key), |
| 2631 mac_algorithm_(mac_algorithm), | 2625 mac_algorithm_(mac_algorithm), |
| 2632 creation_date_(creation), | 2626 creation_date_(creation), |
| 2633 expiry_date_(expiration), | 2627 expiry_date_(expiration), |
| 2634 last_access_date_(last_access), | 2628 last_access_date_(last_access), |
| 2635 secure_(secure), | 2629 secure_(secure), |
| 2636 httponly_(httponly), | 2630 httponly_(httponly) { |
| 2637 has_expires_(has_expires), | 2631 if (expiration.is_null()) |
| 2638 is_persistent_(is_persistent) { | |
| 2639 if (!has_expires_) { | |
| 2640 DCHECK(!is_persistent_); | |
| 2641 SetSessionCookieExpiryTime(); | 2632 SetSessionCookieExpiryTime(); |
| 2642 } | |
| 2643 } | 2633 } |
| 2644 | 2634 |
| 2645 CookieMonster::CanonicalCookie::CanonicalCookie(const GURL& url, | 2635 CookieMonster::CanonicalCookie::CanonicalCookie(const GURL& url, |
| 2646 const ParsedCookie& pc) | 2636 const ParsedCookie& pc) |
| 2647 : source_(GetCookieSourceFromURL(url)), | 2637 : source_(GetCookieSourceFromURL(url)), |
| 2648 name_(pc.Name()), | 2638 name_(pc.Name()), |
| 2649 value_(pc.Value()), | 2639 value_(pc.Value()), |
| 2650 path_(CanonPath(url, pc)), | 2640 path_(CanonPath(url, pc)), |
| 2651 mac_key_(pc.MACKey()), | 2641 mac_key_(pc.MACKey()), |
| 2652 mac_algorithm_(pc.MACAlgorithm()), | 2642 mac_algorithm_(pc.MACAlgorithm()), |
| 2653 creation_date_(Time::Now()), | 2643 creation_date_(Time::Now()), |
| 2654 last_access_date_(Time()), | 2644 last_access_date_(Time()), |
| 2655 secure_(pc.IsSecure()), | 2645 secure_(pc.IsSecure()), |
| 2656 httponly_(pc.IsHttpOnly()), | 2646 httponly_(pc.IsHttpOnly()) { |
| 2657 has_expires_(pc.HasExpires()), | 2647 if (pc.HasExpires()) |
| 2658 is_persistent_(pc.HasExpires()) { | |
| 2659 if (has_expires_) | |
| 2660 expiry_date_ = CanonExpiration(pc, creation_date_); | 2648 expiry_date_ = CanonExpiration(pc, creation_date_); |
| 2661 else | 2649 else |
| 2662 SetSessionCookieExpiryTime(); | 2650 SetSessionCookieExpiryTime(); |
| 2663 | 2651 |
| 2664 // Do the best we can with the domain. | 2652 // Do the best we can with the domain. |
| 2665 std::string cookie_domain; | 2653 std::string cookie_domain; |
| 2666 std::string domain_string; | 2654 std::string domain_string; |
| 2667 if (pc.HasDomain()) { | 2655 if (pc.HasDomain()) { |
| 2668 domain_string = pc.Domain(); | 2656 domain_string = pc.Domain(); |
| 2669 } | 2657 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 2690 | 2678 |
| 2691 return url.GetOrigin().ReplaceComponents(replacements).spec(); | 2679 return url.GetOrigin().ReplaceComponents(replacements).spec(); |
| 2692 } | 2680 } |
| 2693 | 2681 |
| 2694 void CookieMonster::CanonicalCookie::SetSessionCookieExpiryTime() { | 2682 void CookieMonster::CanonicalCookie::SetSessionCookieExpiryTime() { |
| 2695 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | 2683 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
| 2696 // Mobile apps can sometimes be shut down without any warning, so the session | 2684 // Mobile apps can sometimes be shut down without any warning, so the session |
| 2697 // cookie has to be persistent and given a default expiration time. | 2685 // cookie has to be persistent and given a default expiration time. |
| 2698 expiry_date_ = base::Time::Now() + | 2686 expiry_date_ = base::Time::Now() + |
| 2699 base::TimeDelta::FromDays(kPersistentSessionCookieExpiryInDays); | 2687 base::TimeDelta::FromDays(kPersistentSessionCookieExpiryInDays); |
| 2700 has_expires_ = true; | |
| 2701 #endif | 2688 #endif |
| 2702 } | 2689 } |
| 2703 | 2690 |
| 2704 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( | 2691 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( |
| 2705 const GURL& url, | 2692 const GURL& url, |
| 2706 const ParsedCookie& pc) { | 2693 const ParsedCookie& pc) { |
| 2707 if (!pc.IsValid()) { | 2694 if (!pc.IsValid()) { |
| 2708 return NULL; | 2695 return NULL; |
| 2709 } | 2696 } |
| 2710 | 2697 |
| 2711 std::string domain_string; | 2698 std::string domain_string; |
| 2712 if (!GetCookieDomain(url, pc, &domain_string)) { | 2699 if (!GetCookieDomain(url, pc, &domain_string)) { |
| 2713 return NULL; | 2700 return NULL; |
| 2714 } | 2701 } |
| 2715 std::string path_string = CanonPath(url, pc); | 2702 std::string path_string = CanonPath(url, pc); |
| 2716 std::string mac_key = pc.HasMACKey() ? pc.MACKey() : std::string(); | 2703 std::string mac_key = pc.HasMACKey() ? pc.MACKey() : std::string(); |
| 2717 std::string mac_algorithm = pc.HasMACAlgorithm() ? | 2704 std::string mac_algorithm = pc.HasMACAlgorithm() ? |
| 2718 pc.MACAlgorithm() : std::string(); | 2705 pc.MACAlgorithm() : std::string(); |
| 2719 Time creation_time = Time::Now(); | 2706 Time creation_time = Time::Now(); |
| 2720 Time expiration_time; | 2707 Time expiration_time; |
| 2721 if (pc.HasExpires()) | 2708 if (pc.HasExpires()) |
| 2722 expiration_time = net::CookieMonster::ParseCookieTime(pc.Expires()); | 2709 expiration_time = net::CookieMonster::ParseCookieTime(pc.Expires()); |
| 2723 | 2710 |
| 2724 return (Create(url, pc.Name(), pc.Value(), domain_string, path_string, | 2711 return (Create(url, pc.Name(), pc.Value(), domain_string, path_string, |
| 2725 mac_key, mac_algorithm, creation_time, expiration_time, | 2712 mac_key, mac_algorithm, creation_time, expiration_time, |
| 2726 pc.IsSecure(), pc.IsHttpOnly(), !expiration_time.is_null())); | 2713 pc.IsSecure(), pc.IsHttpOnly())); |
| 2727 } | 2714 } |
| 2728 | 2715 |
| 2729 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( | 2716 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( |
| 2730 const GURL& url, | 2717 const GURL& url, |
| 2731 const std::string& name, | 2718 const std::string& name, |
| 2732 const std::string& value, | 2719 const std::string& value, |
| 2733 const std::string& domain, | 2720 const std::string& domain, |
| 2734 const std::string& path, | 2721 const std::string& path, |
| 2735 const std::string& mac_key, | 2722 const std::string& mac_key, |
| 2736 const std::string& mac_algorithm, | 2723 const std::string& mac_algorithm, |
| 2737 const base::Time& creation, | 2724 const base::Time& creation, |
| 2738 const base::Time& expiration, | 2725 const base::Time& expiration, |
| 2739 bool secure, | 2726 bool secure, |
| 2740 bool http_only, | 2727 bool http_only) { |
| 2741 bool is_persistent) { | |
| 2742 // Expect valid attribute tokens and values, as defined by the ParsedCookie | 2728 // Expect valid attribute tokens and values, as defined by the ParsedCookie |
| 2743 // logic, otherwise don't create the cookie. | 2729 // logic, otherwise don't create the cookie. |
| 2744 std::string parsed_name = ParsedCookie::ParseTokenString(name); | 2730 std::string parsed_name = ParsedCookie::ParseTokenString(name); |
| 2745 if (parsed_name != name) | 2731 if (parsed_name != name) |
| 2746 return NULL; | 2732 return NULL; |
| 2747 std::string parsed_value = ParsedCookie::ParseValueString(value); | 2733 std::string parsed_value = ParsedCookie::ParseValueString(value); |
| 2748 if (parsed_value != value) | 2734 if (parsed_value != value) |
| 2749 return NULL; | 2735 return NULL; |
| 2750 | 2736 |
| 2751 std::string parsed_domain = ParsedCookie::ParseValueString(domain); | 2737 std::string parsed_domain = ParsedCookie::ParseValueString(domain); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2769 url_parse::Component path_component(0, cookie_path.length()); | 2755 url_parse::Component path_component(0, cookie_path.length()); |
| 2770 url_canon::RawCanonOutputT<char> canon_path; | 2756 url_canon::RawCanonOutputT<char> canon_path; |
| 2771 url_parse::Component canon_path_component; | 2757 url_parse::Component canon_path_component; |
| 2772 url_canon::CanonicalizePath(cookie_path.data(), path_component, | 2758 url_canon::CanonicalizePath(cookie_path.data(), path_component, |
| 2773 &canon_path, &canon_path_component); | 2759 &canon_path, &canon_path_component); |
| 2774 cookie_path = std::string(canon_path.data() + canon_path_component.begin, | 2760 cookie_path = std::string(canon_path.data() + canon_path_component.begin, |
| 2775 canon_path_component.len); | 2761 canon_path_component.len); |
| 2776 | 2762 |
| 2777 return new CanonicalCookie(url, parsed_name, parsed_value, cookie_domain, | 2763 return new CanonicalCookie(url, parsed_name, parsed_value, cookie_domain, |
| 2778 cookie_path, mac_key, mac_algorithm, creation, | 2764 cookie_path, mac_key, mac_algorithm, creation, |
| 2779 expiration, creation, secure, http_only, | 2765 expiration, creation, secure, http_only); |
| 2780 !expiration.is_null(), is_persistent); | |
| 2781 } | 2766 } |
| 2782 | 2767 |
| 2783 bool CookieMonster::CanonicalCookie::IsOnPath( | 2768 bool CookieMonster::CanonicalCookie::IsOnPath( |
| 2784 const std::string& url_path) const { | 2769 const std::string& url_path) const { |
| 2785 | 2770 |
| 2786 // A zero length would be unsafe for our trailing '/' checks, and | 2771 // A zero length would be unsafe for our trailing '/' checks, and |
| 2787 // would also make no sense for our prefix match. The code that | 2772 // would also make no sense for our prefix match. The code that |
| 2788 // creates a CanonicalCookie should make sure the path is never zero length, | 2773 // creates a CanonicalCookie should make sure the path is never zero length, |
| 2789 // but we double check anyway. | 2774 // but we double check anyway. |
| 2790 if (path_.empty()) | 2775 if (path_.empty()) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2855 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2840 std::string CookieMonster::CanonicalCookie::DebugString() const { |
| 2856 return base::StringPrintf( | 2841 return base::StringPrintf( |
| 2857 "name: %s value: %s domain: %s path: %s creation: %" | 2842 "name: %s value: %s domain: %s path: %s creation: %" |
| 2858 PRId64, | 2843 PRId64, |
| 2859 name_.c_str(), value_.c_str(), | 2844 name_.c_str(), value_.c_str(), |
| 2860 domain_.c_str(), path_.c_str(), | 2845 domain_.c_str(), path_.c_str(), |
| 2861 static_cast<int64>(creation_date_.ToTimeT())); | 2846 static_cast<int64>(creation_date_.ToTimeT())); |
| 2862 } | 2847 } |
| 2863 | 2848 |
| 2864 } // namespace | 2849 } // namespace |
| OLD | NEW |