[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | // Portions of this code based on Mozilla: |
| 6 | // (netwerk/cookie/src/nsCookieService.cpp) |
| 7 | /* ***** BEGIN LICENSE BLOCK ***** |
| 8 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 9 | * |
| 10 | * The contents of this file are subject to the Mozilla Public License Version |
| 11 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 12 | * the License. You may obtain a copy of the License at |
| 13 | * http://www.mozilla.org/MPL/ |
| 14 | * |
| 15 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 16 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 17 | * for the specific language governing rights and limitations under the |
| 18 | * License. |
| 19 | * |
| 20 | * The Original Code is mozilla.org code. |
| 21 | * |
| 22 | * The Initial Developer of the Original Code is |
| 23 | * Netscape Communications Corporation. |
| 24 | * Portions created by the Initial Developer are Copyright (C) 2003 |
| 25 | * the Initial Developer. All Rights Reserved. |
| 26 | * |
| 27 | * Contributor(s): |
| 28 | * Daniel Witte ([email protected]) |
| 29 | * Michiel van Leeuwen ([email protected]) |
| 30 | * |
| 31 | * Alternatively, the contents of this file may be used under the terms of |
| 32 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 33 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 34 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 35 | * of those above. If you wish to allow use of your version of this file only |
| 36 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 37 | * use your version of this file under the terms of the MPL, indicate your |
| 38 | * decision by deleting the provisions above and replace them with the notice |
| 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 |
| 41 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 42 | * |
| 43 | * ***** END LICENSE BLOCK ***** */ |
| 44 | |
| 45 | #include "net/base/cookie_monster.h" |
| 46 | |
| 47 | #include <algorithm> |
| 48 | |
| 49 | #include "base/basictypes.h" |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 50 | #include "base/format_macros.h" |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 51 | #include "base/histogram.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 52 | #include "base/logging.h" |
| 53 | #include "base/scoped_ptr.h" |
| 54 | #include "base/string_tokenizer.h" |
| 55 | #include "base/string_util.h" |
| 56 | #include "googleurl/src/gurl.h" |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 57 | #include "googleurl/src/url_canon.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | #include "net/base/net_util.h" |
| 59 | #include "net/base/registry_controlled_domain.h" |
| 60 | |
| 61 | // #define COOKIE_LOGGING_ENABLED |
| 62 | #ifdef COOKIE_LOGGING_ENABLED |
| 63 | #define COOKIE_DLOG(severity) DLOG_IF(INFO, 1) |
| 64 | #else |
| 65 | #define COOKIE_DLOG(severity) DLOG_IF(INFO, 0) |
| 66 | #endif |
| 67 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 68 | using base::Time; |
| 69 | using base::TimeDelta; |
| 70 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 71 | namespace net { |
| 72 | |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 73 | namespace { |
| 74 | |
[email protected] | e32306c5 | 2008-11-06 16:59:05 | [diff] [blame] | 75 | // Cookie garbage collection thresholds. Based off of the Mozilla defaults. |
| 76 | // It might seem scary to have a high purge value, but really it's not. You |
| 77 | // just make sure that you increase the max to cover the increase in purge, |
| 78 | // and we would have been purging the same amount of cookies. We're just |
| 79 | // going through the garbage collection process less often. |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 80 | const size_t kNumCookiesPerHost = 70; // ~50 cookies |
| 81 | const size_t kNumCookiesPerHostPurge = 20; |
| 82 | const size_t kNumCookiesTotal = 3300; // ~3000 cookies |
| 83 | const size_t kNumCookiesTotalPurge = 300; |
[email protected] | e32306c5 | 2008-11-06 16:59:05 | [diff] [blame] | 84 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 85 | // Default minimum delay after updating a cookie's LastAccessDate before we |
| 86 | // will update it again. |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 87 | const int kDefaultAccessUpdateThresholdSeconds = 60; |
| 88 | |
| 89 | // Comparator to sort cookies from highest creation date to lowest |
| 90 | // creation date. |
| 91 | struct OrderByCreationTimeDesc { |
| 92 | bool operator()(const CookieMonster::CookieMap::iterator& a, |
| 93 | const CookieMonster::CookieMap::iterator& b) const { |
| 94 | return a->second->CreationDate() > b->second->CreationDate(); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | } // namespace |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 99 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 100 | // static |
| 101 | bool CookieMonster::enable_file_scheme_ = false; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 102 | |
| 103 | // static |
| 104 | void CookieMonster::EnableFileScheme() { |
| 105 | enable_file_scheme_ = true; |
| 106 | } |
| 107 | |
[email protected] | 0f7066e | 2010-03-25 08:31:47 | [diff] [blame] | 108 | CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 109 | : initialized_(false), |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 110 | store_(store), |
| 111 | last_access_threshold_( |
[email protected] | 0f7066e | 2010-03-25 08:31:47 | [diff] [blame] | 112 | TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), |
| 113 | delegate_(delegate) { |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 114 | SetDefaultCookieableSchemes(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | CookieMonster::~CookieMonster() { |
| 118 | DeleteAll(false); |
| 119 | } |
| 120 | |
| 121 | void CookieMonster::InitStore() { |
| 122 | DCHECK(store_) << "Store must exist to initialize"; |
| 123 | |
| 124 | // Initialize the store and sync in any saved persistent cookies. We don't |
| 125 | // care if it's expired, insert it so it can be garbage collected, removed, |
| 126 | // and sync'd. |
| 127 | std::vector<KeyedCanonicalCookie> cookies; |
[email protected] | e32306c5 | 2008-11-06 16:59:05 | [diff] [blame] | 128 | // Reserve space for the maximum amount of cookies a database should have. |
| 129 | // This prevents multiple vector growth / copies as we append cookies. |
| 130 | cookies.reserve(kNumCookiesTotal); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 131 | store_->Load(&cookies); |
| 132 | for (std::vector<KeyedCanonicalCookie>::const_iterator it = cookies.begin(); |
| 133 | it != cookies.end(); ++it) { |
| 134 | InternalInsertCookie(it->first, it->second, false); |
| 135 | } |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 136 | |
| 137 | // After importing cookies from the PersistentCookieStore, verify that |
| 138 | // none of our constraints are violated. |
| 139 | // |
| 140 | // In particular, the backing store might have given us duplicate cookies. |
| 141 | EnsureCookiesMapIsValid(); |
| 142 | } |
| 143 | |
| 144 | void CookieMonster::EnsureCookiesMapIsValid() { |
| 145 | int num_duplicates_trimmed = 0; |
| 146 | |
| 147 | // Iterate through all the of the cookies, grouped by host. |
| 148 | CookieMap::iterator prev_range_end = cookies_.begin(); |
| 149 | while (prev_range_end != cookies_.end()) { |
| 150 | CookieMap::iterator cur_range_begin = prev_range_end; |
| 151 | const std::string key = cur_range_begin->first; // Keep a copy. |
| 152 | CookieMap::iterator cur_range_end = cookies_.upper_bound(key); |
| 153 | prev_range_end = cur_range_end; |
| 154 | |
| 155 | // Ensure no equivalent cookies for this host. |
| 156 | num_duplicates_trimmed += |
| 157 | TrimDuplicateCookiesForHost(key, cur_range_begin, cur_range_end); |
| 158 | } |
| 159 | |
| 160 | // Record how many duplicates were found in the database. |
| 161 | UMA_HISTOGRAM_COUNTS_10000("Net.NumDuplicateCookiesInDb", |
| 162 | num_duplicates_trimmed); |
| 163 | |
| 164 | // TODO(eroman): Should also verify that there are no cookies with the same |
| 165 | // creation time, since that is assumed to be unique by the rest of the code. |
| 166 | } |
| 167 | |
| 168 | // Our strategy to find duplicates is: |
| 169 | // (1) Build a map from (cookiename, cookiepath) to |
| 170 | // {list of cookies with this signature, sorted by creation time}. |
| 171 | // (2) For each list with more than 1 entry, keep the cookie having the |
| 172 | // most recent creation time, and delete the others. |
| 173 | int CookieMonster::TrimDuplicateCookiesForHost( |
| 174 | const std::string& key, |
| 175 | CookieMap::iterator begin, |
| 176 | CookieMap::iterator end) { |
| 177 | |
| 178 | // Two cookies are considered equivalent if they have the same name and path. |
| 179 | typedef std::pair<std::string, std::string> CookieSignature; |
| 180 | |
| 181 | // List of cookies ordered by creation time. |
| 182 | typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieList; |
| 183 | |
| 184 | // Helper map we populate to find the duplicates. |
| 185 | typedef std::map<CookieSignature, CookieList> EquivalenceMap; |
| 186 | EquivalenceMap equivalent_cookies; |
| 187 | |
| 188 | // The number of duplicate cookies that have been found. |
| 189 | int num_duplicates = 0; |
| 190 | |
| 191 | // Iterate through all of the cookies in our range, and insert them into |
| 192 | // the equivalence map. |
| 193 | for (CookieMap::iterator it = begin; it != end; ++it) { |
| 194 | DCHECK_EQ(key, it->first); |
| 195 | CanonicalCookie* cookie = it->second; |
| 196 | |
| 197 | CookieSignature signature(cookie->Name(), cookie->Path()); |
| 198 | CookieList& list = equivalent_cookies[signature]; |
| 199 | |
| 200 | // We found a duplicate! |
| 201 | if (!list.empty()) |
| 202 | num_duplicates++; |
| 203 | |
| 204 | // We save the iterator into |cookies_| rather than the actual cookie |
| 205 | // pointer, since we may need to delete it later. |
| 206 | list.insert(it); |
| 207 | } |
| 208 | |
| 209 | // If there were no duplicates, we are done! |
| 210 | if (num_duplicates == 0) |
| 211 | return 0; |
| 212 | |
| 213 | // Otherwise, delete all the duplicate cookies, both from our in-memory store |
| 214 | // and from the backing store. |
| 215 | for (EquivalenceMap::iterator it = equivalent_cookies.begin(); |
| 216 | it != equivalent_cookies.end(); |
| 217 | ++it) { |
| 218 | const CookieSignature& signature = it->first; |
| 219 | CookieList& dupes = it->second; |
| 220 | |
| 221 | if (dupes.size() <= 1) |
| 222 | continue; // This cookiename/path has no duplicates. |
| 223 | |
| 224 | // Since |dups| is sorted by creation time (descending), the first cookie |
| 225 | // is the most recent one, so we will keep it. The rest are duplicates. |
| 226 | dupes.erase(dupes.begin()); |
| 227 | |
| 228 | LOG(ERROR) << StringPrintf("Found %d duplicate cookies for host='%s', " |
| 229 | "with {name='%s', path='%s'}", |
| 230 | static_cast<int>(dupes.size()), |
| 231 | key.c_str(), |
| 232 | signature.first.c_str(), |
| 233 | signature.second.c_str()); |
| 234 | |
| 235 | // Remove all the cookies identified by |dupes|. It is valid to delete our |
| 236 | // list of iterators one at a time, since |cookies_| is a multimap (they |
| 237 | // don't invalidate existing iterators following deletion). |
| 238 | for (CookieList::iterator dupes_it = dupes.begin(); |
| 239 | dupes_it != dupes.end(); |
| 240 | ++dupes_it) { |
| 241 | InternalDeleteCookie(*dupes_it, true /*sync_to_store*/); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return num_duplicates; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 246 | } |
| 247 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 248 | void CookieMonster::SetDefaultCookieableSchemes() { |
| 249 | // Note: file must be the last scheme. |
| 250 | static const char* kDefaultCookieableSchemes[] = { "http", "https", "file" }; |
| 251 | int num_schemes = enable_file_scheme_ ? 3 : 2; |
| 252 | SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); |
| 253 | } |
| 254 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 255 | // The system resolution is not high enough, so we can have multiple |
| 256 | // set cookies that result in the same system time. When this happens, we |
| 257 | // increment by one Time unit. Let's hope computers don't get too fast. |
| 258 | Time CookieMonster::CurrentTime() { |
| 259 | return std::max(Time::Now(), |
| 260 | Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); |
| 261 | } |
| 262 | |
| 263 | // Parse a cookie expiration time. We try to be lenient, but we need to |
| 264 | // assume some order to distinguish the fields. The basic rules: |
| 265 | // - The month name must be present and prefix the first 3 letters of the |
| 266 | // full month name (jan for January, jun for June). |
| 267 | // - If the year is <= 2 digits, it must occur after the day of month. |
| 268 | // - The time must be of the format hh:mm:ss. |
| 269 | // An average cookie expiration will look something like this: |
| 270 | // Sat, 15-Apr-17 21:01:22 GMT |
| 271 | Time CookieMonster::ParseCookieTime(const std::string& time_string) { |
| 272 | static const char* kMonths[] = { "jan", "feb", "mar", "apr", "may", "jun", |
| 273 | "jul", "aug", "sep", "oct", "nov", "dec" }; |
| 274 | static const int kMonthsLen = arraysize(kMonths); |
| 275 | // We want to be pretty liberal, and support most non-ascii and non-digit |
| 276 | // characters as a delimiter. We can't treat : as a delimiter, because it |
| 277 | // is the delimiter for hh:mm:ss, and we want to keep this field together. |
| 278 | // We make sure to include - and +, since they could prefix numbers. |
| 279 | // If the cookie attribute came in in quotes (ex expires="XXX"), the quotes |
| 280 | // will be preserved, and we will get them here. So we make sure to include |
| 281 | // quote characters, and also \ for anything that was internally escaped. |
| 282 | static const char* kDelimiters = "\t !\"#$%&'()*+,-./;<=>?@[\\]^_`{|}~"; |
| 283 | |
| 284 | Time::Exploded exploded = {0}; |
| 285 | |
| 286 | StringTokenizer tokenizer(time_string, kDelimiters); |
| 287 | |
| 288 | bool found_day_of_month = false; |
| 289 | bool found_month = false; |
| 290 | bool found_time = false; |
| 291 | bool found_year = false; |
| 292 | |
| 293 | while (tokenizer.GetNext()) { |
| 294 | const std::string token = tokenizer.token(); |
| 295 | DCHECK(!token.empty()); |
| 296 | bool numerical = IsAsciiDigit(token[0]); |
| 297 | |
| 298 | // String field |
| 299 | if (!numerical) { |
| 300 | if (!found_month) { |
| 301 | for (int i = 0; i < kMonthsLen; ++i) { |
| 302 | // Match prefix, so we could match January, etc |
[email protected] | 7e3dcd9 | 2008-12-30 13:13:34 | [diff] [blame] | 303 | if (base::strncasecmp(token.c_str(), kMonths[i], 3) == 0) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 304 | exploded.month = i + 1; |
| 305 | found_month = true; |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } else { |
| 310 | // If we've gotten here, it means we've already found and parsed our |
| 311 | // month, and we have another string, which we would expect to be the |
| 312 | // the time zone name. According to the RFC and my experiments with |
| 313 | // how sites format their expirations, we don't have much of a reason |
| 314 | // to support timezones. We don't want to ever barf on user input, |
| 315 | // but this DCHECK should pass for well-formed data. |
| 316 | // DCHECK(token == "GMT"); |
| 317 | } |
| 318 | // Numeric field w/ a colon |
| 319 | } else if (token.find(':') != std::string::npos) { |
| 320 | if (!found_time && |
[email protected] | d862fd9 | 2008-08-21 18:15:35 | [diff] [blame] | 321 | #ifdef COMPILER_MSVC |
| 322 | sscanf_s( |
| 323 | #else |
| 324 | sscanf( |
| 325 | #endif |
| 326 | token.c_str(), "%2u:%2u:%2u", &exploded.hour, |
| 327 | &exploded.minute, &exploded.second) == 3) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 328 | found_time = true; |
| 329 | } else { |
| 330 | // We should only ever encounter one time-like thing. If we're here, |
| 331 | // it means we've found a second, which shouldn't happen. We keep |
| 332 | // the first. This check should be ok for well-formed input: |
| 333 | // NOTREACHED(); |
| 334 | } |
| 335 | // Numeric field |
| 336 | } else { |
| 337 | // Overflow with atoi() is unspecified, so we enforce a max length. |
| 338 | if (!found_day_of_month && token.length() <= 2) { |
| 339 | exploded.day_of_month = atoi(token.c_str()); |
| 340 | found_day_of_month = true; |
| 341 | } else if (!found_year && token.length() <= 5) { |
| 342 | exploded.year = atoi(token.c_str()); |
| 343 | found_year = true; |
| 344 | } else { |
| 345 | // If we're here, it means we've either found an extra numeric field, |
| 346 | // or a numeric field which was too long. For well-formed input, the |
| 347 | // following check would be reasonable: |
| 348 | // NOTREACHED(); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | if (!found_day_of_month || !found_month || !found_time || !found_year) { |
| 354 | // We didn't find all of the fields we need. For well-formed input, the |
| 355 | // following check would be reasonable: |
| 356 | // NOTREACHED() << "Cookie parse expiration failed: " << time_string; |
| 357 | return Time(); |
| 358 | } |
| 359 | |
| 360 | // Normalize the year to expand abbreviated years to the full year. |
| 361 | if (exploded.year >= 69 && exploded.year <= 99) |
| 362 | exploded.year += 1900; |
| 363 | if (exploded.year >= 0 && exploded.year <= 68) |
| 364 | exploded.year += 2000; |
| 365 | |
| 366 | // If our values are within their correct ranges, we got our time. |
| 367 | if (exploded.day_of_month >= 1 && exploded.day_of_month <= 31 && |
| 368 | exploded.month >= 1 && exploded.month <= 12 && |
| 369 | exploded.year >= 1601 && exploded.year <= 30827 && |
| 370 | exploded.hour <= 23 && exploded.minute <= 59 && exploded.second <= 59) { |
| 371 | return Time::FromUTCExploded(exploded); |
| 372 | } |
| 373 | |
| 374 | // One of our values was out of expected range. For well-formed input, |
| 375 | // the following check would be reasonable: |
| 376 | // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; |
| 377 | |
| 378 | return Time(); |
| 379 | } |
| 380 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 381 | bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { |
| 382 | return (domain_string.empty() || domain_string[0] != '.'); |
| 383 | } |
| 384 | |
[email protected] | 69bb587 | 2010-01-12 20:33:52 | [diff] [blame] | 385 | // Returns the effective TLD+1 for a given host. This only makes sense for http |
| 386 | // and https schemes. For other schemes, the host will be returned unchanged |
| 387 | // (minus any leading .). |
| 388 | static std::string GetEffectiveDomain(const std::string& scheme, |
| 389 | const std::string& host) { |
| 390 | if (scheme == "http" || scheme == "https") |
| 391 | return RegistryControlledDomainService::GetDomainAndRegistry(host); |
| 392 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 393 | if (!CookieMonster::DomainIsHostOnly(host)) |
[email protected] | 69bb587 | 2010-01-12 20:33:52 | [diff] [blame] | 394 | return host.substr(1); |
| 395 | return host; |
| 396 | } |
| 397 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 398 | // Determine the cookie domain key to use for setting a cookie with the |
| 399 | // specified domain attribute string. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 400 | // On success returns true, and sets cookie_domain_key to either a |
| 401 | // -host cookie key (ex: "google.com") |
| 402 | // -domain cookie key (ex: ".google.com") |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 403 | static bool GetCookieDomainKeyWithString(const GURL& url, |
| 404 | const std::string& domain_string, |
| 405 | std::string* cookie_domain_key) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 406 | const std::string url_host(url.host()); |
[email protected] | c3a756b6 | 2009-01-23 10:50:51 | [diff] [blame] | 407 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 408 | // If no domain was specified in the domain string, default to a host cookie. |
[email protected] | c3a756b6 | 2009-01-23 10:50:51 | [diff] [blame] | 409 | // We match IE/Firefox in allowing a domain=IPADDR if it matches the url |
| 410 | // ip address hostname exactly. It should be treated as a host cookie. |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 411 | if (domain_string.empty() || |
| 412 | (url.HostIsIPAddress() && url_host == domain_string)) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 413 | *cookie_domain_key = url_host; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 414 | DCHECK(CookieMonster::DomainIsHostOnly(*cookie_domain_key)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 415 | return true; |
| 416 | } |
| 417 | |
| 418 | // Get the normalized domain specified in cookie line. |
| 419 | // Note: The RFC says we can reject a cookie if the domain |
| 420 | // attribute does not start with a dot. IE/FF/Safari however, allow a cookie |
| 421 | // of the form domain=my.domain.com, treating it the same as |
| 422 | // domain=.my.domain.com -- for compatibility we do the same here. Firefox |
| 423 | // also treats domain=.....my.domain.com like domain=.my.domain.com, but |
| 424 | // neither IE nor Safari do this, and we don't either. |
[email protected] | 01dbd93 | 2009-06-23 22:52:42 | [diff] [blame] | 425 | url_canon::CanonHostInfo ignored; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 426 | std::string cookie_domain(net::CanonicalizeHost(domain_string, &ignored)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 427 | if (cookie_domain.empty()) |
| 428 | return false; |
| 429 | if (cookie_domain[0] != '.') |
| 430 | cookie_domain = "." + cookie_domain; |
| 431 | |
| 432 | // Ensure |url| and |cookie_domain| have the same domain+registry. |
[email protected] | 69bb587 | 2010-01-12 20:33:52 | [diff] [blame] | 433 | const std::string url_scheme(url.scheme()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 434 | const std::string url_domain_and_registry( |
[email protected] | 69bb587 | 2010-01-12 20:33:52 | [diff] [blame] | 435 | GetEffectiveDomain(url_scheme, url_host)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 436 | if (url_domain_and_registry.empty()) |
| 437 | return false; // IP addresses/intranet hosts can't set domain cookies. |
| 438 | const std::string cookie_domain_and_registry( |
[email protected] | 69bb587 | 2010-01-12 20:33:52 | [diff] [blame] | 439 | GetEffectiveDomain(url_scheme, cookie_domain)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 440 | if (url_domain_and_registry != cookie_domain_and_registry) |
| 441 | return false; // Can't set a cookie on a different domain + registry. |
| 442 | |
| 443 | // Ensure |url_host| is |cookie_domain| or one of its subdomains. Given that |
| 444 | // we know the domain+registry are the same from the above checks, this is |
| 445 | // basically a simple string suffix check. |
| 446 | if ((url_host.length() < cookie_domain.length()) ? |
| 447 | (cookie_domain != ("." + url_host)) : |
| 448 | url_host.compare(url_host.length() - cookie_domain.length(), |
| 449 | cookie_domain.length(), cookie_domain)) |
| 450 | return false; |
| 451 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 452 | *cookie_domain_key = cookie_domain; |
| 453 | return true; |
| 454 | } |
| 455 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 456 | // Determine the cookie domain key to use for setting the specified cookie. |
| 457 | static bool GetCookieDomainKey(const GURL& url, |
| 458 | const CookieMonster::ParsedCookie& pc, |
| 459 | std::string* cookie_domain_key) { |
| 460 | std::string domain_string; |
| 461 | if (pc.HasDomain()) |
| 462 | domain_string = pc.Domain(); |
| 463 | return GetCookieDomainKeyWithString(url, domain_string, cookie_domain_key); |
| 464 | } |
| 465 | |
| 466 | static std::string CanonPathWithString(const GURL& url, |
| 467 | const std::string& path_string) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 468 | // The RFC says the path should be a prefix of the current URL path. |
| 469 | // However, Mozilla allows you to set any path for compatibility with |
| 470 | // broken websites. We unfortunately will mimic this behavior. We try |
| 471 | // to be generous and accept cookies with an invalid path attribute, and |
| 472 | // default the path to something reasonable. |
| 473 | |
| 474 | // The path was supplied in the cookie, we'll take it. |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 475 | if (!path_string.empty() && path_string[0] == '/') |
| 476 | return path_string; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 477 | |
| 478 | // The path was not supplied in the cookie or invalid, we will default |
| 479 | // to the current URL path. |
| 480 | // """Defaults to the path of the request URL that generated the |
| 481 | // Set-Cookie response, up to, but not including, the |
| 482 | // right-most /.""" |
| 483 | // How would this work for a cookie on /? We will include it then. |
| 484 | const std::string& url_path = url.path(); |
| 485 | |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 486 | size_t idx = url_path.find_last_of('/'); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 487 | |
| 488 | // The cookie path was invalid or a single '/'. |
| 489 | if (idx == 0 || idx == std::string::npos) |
| 490 | return std::string("/"); |
| 491 | |
| 492 | // Return up to the rightmost '/'. |
| 493 | return url_path.substr(0, idx); |
| 494 | } |
| 495 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 496 | static std::string CanonPath(const GURL& url, |
| 497 | const CookieMonster::ParsedCookie& pc) { |
| 498 | std::string path_string; |
| 499 | if (pc.HasPath()) |
| 500 | path_string = pc.Path(); |
| 501 | return CanonPathWithString(url, path_string); |
| 502 | } |
| 503 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 504 | static Time CanonExpiration(const CookieMonster::ParsedCookie& pc, |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 505 | const Time& current, |
| 506 | const CookieOptions& options) { |
| 507 | if (options.force_session()) |
| 508 | return Time(); |
| 509 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 510 | // First, try the Max-Age attribute. |
| 511 | uint64 max_age = 0; |
| 512 | if (pc.HasMaxAge() && |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 513 | #ifdef COMPILER_MSVC |
| 514 | sscanf_s( |
[email protected] | d862fd9 | 2008-08-21 18:15:35 | [diff] [blame] | 515 | #else |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 516 | sscanf( |
[email protected] | d862fd9 | 2008-08-21 18:15:35 | [diff] [blame] | 517 | #endif |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 518 | pc.MaxAge().c_str(), " %" PRIu64, &max_age) == 1) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 519 | return current + TimeDelta::FromSeconds(max_age); |
| 520 | } |
| 521 | |
| 522 | // Try the Expires attribute. |
| 523 | if (pc.HasExpires()) |
| 524 | return CookieMonster::ParseCookieTime(pc.Expires()); |
| 525 | |
| 526 | // Invalid or no expiration, persistent cookie. |
| 527 | return Time(); |
| 528 | } |
| 529 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 530 | bool CookieMonster::HasCookieableScheme(const GURL& url) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 531 | // Make sure the request is on a cookie-able url scheme. |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 532 | for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 533 | // We matched a scheme. |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 534 | if (url.SchemeIs(cookieable_schemes_[i].c_str())) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 535 | // We've matched a supported scheme. |
| 536 | return true; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // The scheme didn't match any in our whitelist. |
| 541 | COOKIE_DLOG(WARNING) << "Unsupported cookie scheme: " << url.scheme(); |
| 542 | return false; |
| 543 | } |
| 544 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 545 | void CookieMonster::SetCookieableSchemes( |
| 546 | const char* schemes[], size_t num_schemes) { |
| 547 | cookieable_schemes_.clear(); |
| 548 | cookieable_schemes_.insert(cookieable_schemes_.end(), |
| 549 | schemes, schemes + num_schemes); |
| 550 | } |
| 551 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 552 | bool CookieMonster::SetCookieWithCreationTimeAndOptions( |
| 553 | const GURL& url, |
| 554 | const std::string& cookie_line, |
| 555 | const Time& creation_time_or_null, |
| 556 | const CookieOptions& options) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 557 | if (!HasCookieableScheme(url)) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | |
| 561 | AutoLock autolock(lock_); |
| 562 | InitIfNecessary(); |
| 563 | |
| 564 | COOKIE_DLOG(INFO) << "SetCookie() line: " << cookie_line; |
| 565 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 566 | Time creation_time = creation_time_or_null; |
| 567 | if (creation_time.is_null()) { |
| 568 | creation_time = CurrentTime(); |
| 569 | last_time_seen_ = creation_time; |
| 570 | } |
| 571 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 572 | // Parse the cookie. |
| 573 | ParsedCookie pc(cookie_line); |
| 574 | |
| 575 | if (!pc.IsValid()) { |
| 576 | COOKIE_DLOG(WARNING) << "Couldn't parse cookie"; |
| 577 | return false; |
| 578 | } |
| 579 | |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 580 | if (options.exclude_httponly() && pc.IsHttpOnly()) { |
| 581 | COOKIE_DLOG(INFO) << "SetCookie() not setting httponly cookie"; |
| 582 | return false; |
| 583 | } |
| 584 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 585 | std::string cookie_domain; |
| 586 | if (!GetCookieDomainKey(url, pc, &cookie_domain)) { |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | std::string cookie_path = CanonPath(url, pc); |
| 591 | |
| 592 | scoped_ptr<CanonicalCookie> cc; |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 593 | Time cookie_expires = CanonExpiration(pc, creation_time, options); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 594 | |
| 595 | cc.reset(new CanonicalCookie(pc.Name(), pc.Value(), cookie_path, |
| 596 | pc.IsSecure(), pc.IsHttpOnly(), |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 597 | creation_time, creation_time, |
| 598 | !cookie_expires.is_null(), cookie_expires)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 599 | |
| 600 | if (!cc.get()) { |
| 601 | COOKIE_DLOG(WARNING) << "Failed to allocate CanonicalCookie"; |
| 602 | return false; |
| 603 | } |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 604 | return SetCanonicalCookie(&cc, cookie_domain, creation_time, options); |
| 605 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 606 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 607 | bool CookieMonster::SetCookieWithDetails( |
| 608 | const GURL& url, const std::string& name, const std::string& value, |
| 609 | const std::string& domain, const std::string& path, |
| 610 | const base::Time& expiration_time, bool secure, bool http_only) { |
| 611 | if (!HasCookieableScheme(url)) |
| 612 | return false; |
| 613 | |
| 614 | // Expect a valid domain attribute with no illegal characters. |
| 615 | std::string parsed_domain = ParsedCookie::ParseValueString(domain); |
| 616 | if (parsed_domain != domain) |
| 617 | return false; |
| 618 | std::string cookie_domain; |
| 619 | if (!GetCookieDomainKeyWithString(url, parsed_domain, &cookie_domain)) |
| 620 | return false; |
| 621 | |
| 622 | AutoLock autolock(lock_); |
| 623 | InitIfNecessary(); |
| 624 | |
| 625 | Time creation_time = CurrentTime(); |
| 626 | last_time_seen_ = creation_time; |
| 627 | |
| 628 | scoped_ptr<CanonicalCookie> cc; |
| 629 | cc.reset(CanonicalCookie::Create( |
| 630 | url, name, value, path, creation_time, expiration_time, |
| 631 | secure, http_only)); |
| 632 | |
| 633 | if (!cc.get()) |
| 634 | return false; |
| 635 | |
| 636 | CookieOptions options; |
| 637 | options.set_include_httponly(); |
| 638 | return SetCanonicalCookie(&cc, cookie_domain, creation_time, options); |
| 639 | } |
| 640 | |
| 641 | bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc, |
| 642 | const std::string& cookie_domain, |
| 643 | const Time& creation_time, |
| 644 | const CookieOptions& options) { |
| 645 | if (DeleteAnyEquivalentCookie(cookie_domain, **cc, |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 646 | options.exclude_httponly())) { |
| 647 | COOKIE_DLOG(INFO) << "SetCookie() not clobbering httponly cookie"; |
| 648 | return false; |
| 649 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 650 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 651 | COOKIE_DLOG(INFO) << "SetCookie() cc: " << (*cc)->DebugString(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 652 | |
| 653 | // Realize that we might be setting an expired cookie, and the only point |
| 654 | // was to delete the cookie which we've already done. |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 655 | if (!(*cc)->IsExpired(creation_time)) |
| 656 | InternalInsertCookie(cookie_domain, cc->release(), true); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 657 | |
| 658 | // We assume that hopefully setting a cookie will be less common than |
| 659 | // querying a cookie. Since setting a cookie can put us over our limits, |
| 660 | // make sure that we garbage collect... We can also make the assumption that |
| 661 | // if a cookie was set, in the common case it will be used soon after, |
| 662 | // and we will purge the expired cookies in GetCookies(). |
| 663 | GarbageCollect(creation_time, cookie_domain); |
| 664 | |
| 665 | return true; |
| 666 | } |
| 667 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 668 | void CookieMonster::InternalInsertCookie(const std::string& key, |
| 669 | CanonicalCookie* cc, |
| 670 | bool sync_to_store) { |
| 671 | if (cc->IsPersistent() && store_ && sync_to_store) |
| 672 | store_->AddCookie(key, *cc); |
| 673 | cookies_.insert(CookieMap::value_type(key, cc)); |
[email protected] | 0f7066e | 2010-03-25 08:31:47 | [diff] [blame] | 674 | if (delegate_.get()) |
| 675 | delegate_->OnCookieChanged(key, *cc, false); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 676 | } |
| 677 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 678 | void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc) { |
| 679 | // Based off the Mozilla code. When a cookie has been accessed recently, |
| 680 | // don't bother updating its access time again. This reduces the number of |
| 681 | // updates we do during pageload, which in turn reduces the chance our storage |
| 682 | // backend will hit its batch thresholds and be forced to update. |
| 683 | const Time current = Time::Now(); |
| 684 | if ((current - cc->LastAccessDate()) < last_access_threshold_) |
| 685 | return; |
| 686 | |
| 687 | cc->SetLastAccessDate(current); |
| 688 | if (cc->IsPersistent() && store_) |
| 689 | store_->UpdateCookieAccessTime(*cc); |
| 690 | } |
| 691 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 692 | void CookieMonster::InternalDeleteCookie(CookieMap::iterator it, |
| 693 | bool sync_to_store) { |
| 694 | CanonicalCookie* cc = it->second; |
| 695 | COOKIE_DLOG(INFO) << "InternalDeleteCookie() cc: " << cc->DebugString(); |
| 696 | if (cc->IsPersistent() && store_ && sync_to_store) |
| 697 | store_->DeleteCookie(*cc); |
[email protected] | 0f7066e | 2010-03-25 08:31:47 | [diff] [blame] | 698 | if (delegate_.get()) |
| 699 | delegate_->OnCookieChanged(it->first, *cc, true); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 700 | cookies_.erase(it); |
| 701 | delete cc; |
| 702 | } |
| 703 | |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 704 | bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key, |
| 705 | const CanonicalCookie& ecc, |
| 706 | bool skip_httponly) { |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 707 | bool found_equivalent_cookie = false; |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 708 | bool skipped_httponly = false; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 709 | for (CookieMapItPair its = cookies_.equal_range(key); |
| 710 | its.first != its.second; ) { |
| 711 | CookieMap::iterator curit = its.first; |
| 712 | CanonicalCookie* cc = curit->second; |
| 713 | ++its.first; |
| 714 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 715 | if (ecc.IsEquivalent(*cc)) { |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 716 | // We should never have more than one equivalent cookie, since they should |
| 717 | // overwrite each other. |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 718 | CHECK(!found_equivalent_cookie) << |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 719 | "Duplicate equivalent cookies found, cookie store is corrupted."; |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 720 | if (skip_httponly && cc->IsHttpOnly()) { |
| 721 | skipped_httponly = true; |
| 722 | } else { |
| 723 | InternalDeleteCookie(curit, true); |
| 724 | } |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 725 | found_equivalent_cookie = true; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 726 | } |
| 727 | } |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 728 | return skipped_httponly; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 729 | } |
| 730 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 731 | int CookieMonster::GarbageCollect(const Time& current, |
| 732 | const std::string& key) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 733 | int num_deleted = 0; |
| 734 | |
| 735 | // Collect garbage for this key. |
| 736 | if (cookies_.count(key) > kNumCookiesPerHost) { |
| 737 | COOKIE_DLOG(INFO) << "GarbageCollect() key: " << key; |
| 738 | num_deleted += GarbageCollectRange(current, cookies_.equal_range(key), |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 739 | kNumCookiesPerHost, kNumCookiesPerHostPurge); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | // Collect garbage for everything. |
| 743 | if (cookies_.size() > kNumCookiesTotal) { |
| 744 | COOKIE_DLOG(INFO) << "GarbageCollect() everything"; |
| 745 | num_deleted += GarbageCollectRange(current, |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 746 | CookieMapItPair(cookies_.begin(), cookies_.end()), kNumCookiesTotal, |
| 747 | kNumCookiesTotalPurge); |
| 748 | } |
| 749 | |
| 750 | return num_deleted; |
| 751 | } |
| 752 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 753 | static bool LRUCookieSorter(const CookieMonster::CookieMap::iterator& it1, |
| 754 | const CookieMonster::CookieMap::iterator& it2) { |
| 755 | // Cookies accessed less recently should be deleted first. |
| 756 | if (it1->second->LastAccessDate() != it2->second->LastAccessDate()) |
| 757 | return it1->second->LastAccessDate() < it2->second->LastAccessDate(); |
| 758 | |
| 759 | // In rare cases we might have two cookies with identical last access times. |
| 760 | // To preserve the stability of the sort, in these cases prefer to delete |
| 761 | // older cookies over newer ones. CreationDate() is guaranteed to be unique. |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 762 | return it1->second->CreationDate() < it2->second->CreationDate(); |
| 763 | } |
| 764 | |
| 765 | int CookieMonster::GarbageCollectRange(const Time& current, |
| 766 | const CookieMapItPair& itpair, |
| 767 | size_t num_max, |
| 768 | size_t num_purge) { |
| 769 | // First, delete anything that's expired. |
| 770 | std::vector<CookieMap::iterator> cookie_its; |
| 771 | int num_deleted = GarbageCollectExpired(current, itpair, &cookie_its); |
| 772 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 773 | // If the range still has too many cookies, delete the least recently used. |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 774 | if (cookie_its.size() > num_max) { |
| 775 | COOKIE_DLOG(INFO) << "GarbageCollectRange() Deep Garbage Collect."; |
| 776 | // Purge down to (|num_max| - |num_purge|) total cookies. |
| 777 | DCHECK(num_purge <= num_max); |
| 778 | num_purge += cookie_its.size() - num_max; |
| 779 | |
| 780 | std::partial_sort(cookie_its.begin(), cookie_its.begin() + num_purge, |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 781 | cookie_its.end(), LRUCookieSorter); |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 782 | for (size_t i = 0; i < num_purge; ++i) |
| 783 | InternalDeleteCookie(cookie_its[i], true); |
| 784 | |
| 785 | num_deleted += num_purge; |
| 786 | } |
| 787 | |
| 788 | return num_deleted; |
| 789 | } |
| 790 | |
| 791 | int CookieMonster::GarbageCollectExpired( |
| 792 | const Time& current, |
| 793 | const CookieMapItPair& itpair, |
| 794 | std::vector<CookieMap::iterator>* cookie_its) { |
| 795 | int num_deleted = 0; |
| 796 | for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) { |
| 797 | CookieMap::iterator curit = it; |
| 798 | ++it; |
| 799 | |
| 800 | if (curit->second->IsExpired(current)) { |
| 801 | InternalDeleteCookie(curit, true); |
| 802 | ++num_deleted; |
| 803 | } else if (cookie_its) { |
| 804 | cookie_its->push_back(curit); |
| 805 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | return num_deleted; |
| 809 | } |
| 810 | |
| 811 | int CookieMonster::DeleteAll(bool sync_to_store) { |
| 812 | AutoLock autolock(lock_); |
| 813 | InitIfNecessary(); |
| 814 | |
| 815 | int num_deleted = 0; |
| 816 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 817 | CookieMap::iterator curit = it; |
| 818 | ++it; |
| 819 | InternalDeleteCookie(curit, sync_to_store); |
| 820 | ++num_deleted; |
| 821 | } |
| 822 | |
| 823 | return num_deleted; |
| 824 | } |
| 825 | |
| 826 | int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, |
| 827 | const Time& delete_end, |
| 828 | bool sync_to_store) { |
| 829 | AutoLock autolock(lock_); |
| 830 | InitIfNecessary(); |
| 831 | |
| 832 | int num_deleted = 0; |
| 833 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 834 | CookieMap::iterator curit = it; |
| 835 | CanonicalCookie* cc = curit->second; |
| 836 | ++it; |
| 837 | |
| 838 | if (cc->CreationDate() >= delete_begin && |
| 839 | (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
| 840 | InternalDeleteCookie(curit, sync_to_store); |
| 841 | ++num_deleted; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | return num_deleted; |
| 846 | } |
| 847 | |
| 848 | int CookieMonster::DeleteAllCreatedAfter(const Time& delete_begin, |
| 849 | bool sync_to_store) { |
| 850 | return DeleteAllCreatedBetween(delete_begin, Time(), sync_to_store); |
| 851 | } |
| 852 | |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 853 | int CookieMonster::DeleteAllForURL(const GURL& url, |
| 854 | bool sync_to_store) { |
| 855 | AutoLock autolock(lock_); |
| 856 | InitIfNecessary(); |
| 857 | CookieList cookies = InternalGetAllCookiesForURL(url); |
| 858 | int num_deleted = 0; |
| 859 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 860 | CookieMap::iterator curit = it; |
| 861 | ++it; |
| 862 | InternalDeleteCookie(curit, sync_to_store); |
| 863 | } |
| 864 | return num_deleted; |
| 865 | } |
| 866 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 867 | bool CookieMonster::DeleteCookie(const std::string& domain, |
| 868 | const CanonicalCookie& cookie, |
| 869 | bool sync_to_store) { |
| 870 | AutoLock autolock(lock_); |
| 871 | InitIfNecessary(); |
| 872 | |
| 873 | for (CookieMapItPair its = cookies_.equal_range(domain); |
| 874 | its.first != its.second; ++its.first) { |
| 875 | // The creation date acts as our unique index... |
| 876 | if (its.first->second->CreationDate() == cookie.CreationDate()) { |
| 877 | InternalDeleteCookie(its.first, sync_to_store); |
| 878 | return true; |
| 879 | } |
| 880 | } |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | // Mozilla sorts on the path length (longest first), and then it |
| 885 | // sorts by creation time (oldest first). |
| 886 | // The RFC says the sort order for the domain attribute is undefined. |
| 887 | static bool CookieSorter(CookieMonster::CanonicalCookie* cc1, |
| 888 | CookieMonster::CanonicalCookie* cc2) { |
| 889 | if (cc1->Path().length() == cc2->Path().length()) |
| 890 | return cc1->CreationDate() < cc2->CreationDate(); |
| 891 | return cc1->Path().length() > cc2->Path().length(); |
| 892 | } |
| 893 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 894 | bool CookieMonster::SetCookieWithOptions(const GURL& url, |
| 895 | const std::string& cookie_line, |
| 896 | const CookieOptions& options) { |
| 897 | return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
| 898 | } |
| 899 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 900 | // Currently our cookie datastructure is based on Mozilla's approach. We have a |
| 901 | // hash keyed on the cookie's domain, and for any query we walk down the domain |
| 902 | // components and probe for cookies until we reach the TLD, where we stop. |
| 903 | // For example, a.b.blah.com, we would probe |
| 904 | // - a.b.blah.com |
| 905 | // - .a.b.blah.com (TODO should we check this first or second?) |
| 906 | // - .b.blah.com |
| 907 | // - .blah.com |
| 908 | // There are some alternative datastructures we could try, like a |
| 909 | // search/prefix trie, where we reverse the hostname and query for all |
| 910 | // keys that are a prefix of our hostname. I think the hash probing |
| 911 | // should be fast and simple enough for now. |
| 912 | std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 913 | const CookieOptions& options) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 914 | if (!HasCookieableScheme(url)) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 915 | return std::string(); |
| 916 | } |
| 917 | |
| 918 | // Get the cookies for this host and its domain(s). |
| 919 | std::vector<CanonicalCookie*> cookies; |
| 920 | FindCookiesForHostAndDomain(url, options, &cookies); |
| 921 | std::sort(cookies.begin(), cookies.end(), CookieSorter); |
| 922 | |
| 923 | std::string cookie_line; |
| 924 | for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
| 925 | it != cookies.end(); ++it) { |
| 926 | if (it != cookies.begin()) |
| 927 | cookie_line += "; "; |
| 928 | // In Mozilla if you set a cookie like AAAA, it will have an empty token |
| 929 | // and a value of AAAA. When it sends the cookie back, it will send AAAA, |
| 930 | // so we need to avoid sending =AAAA for a blank token value. |
| 931 | if (!(*it)->Name().empty()) |
| 932 | cookie_line += (*it)->Name() + "="; |
| 933 | cookie_line += (*it)->Value(); |
| 934 | } |
| 935 | |
| 936 | COOKIE_DLOG(INFO) << "GetCookies() result: " << cookie_line; |
| 937 | |
| 938 | return cookie_line; |
| 939 | } |
| 940 | |
[email protected] | 971713e | 2009-10-29 16:07:21 | [diff] [blame] | 941 | void CookieMonster::DeleteCookie(const GURL& url, |
| 942 | const std::string& cookie_name) { |
| 943 | if (!HasCookieableScheme(url)) |
| 944 | return; |
| 945 | |
[email protected] | e8bac55 | 2009-10-30 16:15:39 | [diff] [blame] | 946 | CookieOptions options; |
| 947 | options.set_include_httponly(); |
| 948 | // Get the cookies for this host and its domain(s). |
| 949 | std::vector<CanonicalCookie*> cookies; |
| 950 | FindCookiesForHostAndDomain(url, options, &cookies); |
| 951 | std::set<CanonicalCookie*> matching_cookies; |
| 952 | |
| 953 | for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
| 954 | it != cookies.end(); ++it) { |
| 955 | if ((*it)->Name() != cookie_name) |
| 956 | continue; |
| 957 | if (url.path().find((*it)->Path())) |
| 958 | continue; |
| 959 | matching_cookies.insert(*it); |
| 960 | } |
| 961 | |
| 962 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 963 | CookieMap::iterator curit = it; |
| 964 | ++it; |
| 965 | if (matching_cookies.find(curit->second) != matching_cookies.end()) |
| 966 | InternalDeleteCookie(curit, true); |
[email protected] | 971713e | 2009-10-29 16:07:21 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 970 | CookieMonster::CookieList CookieMonster::GetAllCookies() { |
| 971 | AutoLock autolock(lock_); |
| 972 | InitIfNecessary(); |
| 973 | |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 974 | // This function is being called to scrape the cookie list for management UI |
| 975 | // or similar. We shouldn't show expired cookies in this list since it will |
| 976 | // just be confusing to users, and this function is called rarely enough (and |
| 977 | // is already slow enough) that it's OK to take the time to garbage collect |
| 978 | // the expired cookies now. |
| 979 | // |
| 980 | // Note that this does not prune cookies to be below our limits (if we've |
| 981 | // exceeded them) the way that calling GarbageCollect() would. |
| 982 | GarbageCollectExpired(Time::Now(), |
| 983 | CookieMapItPair(cookies_.begin(), cookies_.end()), |
| 984 | NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 985 | |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 986 | CookieList cookie_list; |
| 987 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 988 | cookie_list.push_back(CookieListPair(it->first, *it->second)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 989 | |
| 990 | return cookie_list; |
| 991 | } |
| 992 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 993 | CookieMonster::CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { |
[email protected] | 86d9b0d | 2010-02-03 18:12:07 | [diff] [blame] | 994 | AutoLock autolock(lock_); |
| 995 | InitIfNecessary(); |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 996 | return InternalGetAllCookiesForURL(url); |
[email protected] | 79a087a | 2010-02-03 17:08:19 | [diff] [blame] | 997 | } |
| 998 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 999 | void CookieMonster::FindCookiesForHostAndDomain( |
| 1000 | const GURL& url, |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 1001 | const CookieOptions& options, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1002 | std::vector<CanonicalCookie*>* cookies) { |
| 1003 | AutoLock autolock(lock_); |
| 1004 | InitIfNecessary(); |
| 1005 | |
| 1006 | const Time current_time(CurrentTime()); |
| 1007 | |
| 1008 | // Query for the full host, For example: 'a.c.blah.com'. |
| 1009 | std::string key(url.host()); |
| 1010 | FindCookiesForKey(key, url, options, current_time, cookies); |
| 1011 | |
| 1012 | // See if we can search for domain cookies, i.e. if the host has a TLD + 1. |
[email protected] | 69bb587 | 2010-01-12 20:33:52 | [diff] [blame] | 1013 | const std::string domain(GetEffectiveDomain(url.scheme(), key)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1014 | if (domain.empty()) |
| 1015 | return; |
| 1016 | DCHECK_LE(domain.length(), key.length()); |
| 1017 | DCHECK_EQ(0, key.compare(key.length() - domain.length(), domain.length(), |
| 1018 | domain)); |
| 1019 | |
| 1020 | // Walk through the string and query at the dot points (GURL should have |
| 1021 | // canonicalized the dots, so this should be safe). Stop once we reach the |
| 1022 | // domain + registry; we can't write cookies past this point, and with some |
| 1023 | // registrars other domains can, in which case we don't want to read their |
| 1024 | // cookies. |
| 1025 | for (key = "." + key; key.length() > domain.length(); ) { |
| 1026 | FindCookiesForKey(key, url, options, current_time, cookies); |
| 1027 | const size_t next_dot = key.find('.', 1); // Skip over leading dot. |
| 1028 | key.erase(0, next_dot); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | void CookieMonster::FindCookiesForKey( |
| 1033 | const std::string& key, |
| 1034 | const GURL& url, |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 1035 | const CookieOptions& options, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1036 | const Time& current, |
| 1037 | std::vector<CanonicalCookie*>* cookies) { |
| 1038 | bool secure = url.SchemeIsSecure(); |
| 1039 | |
| 1040 | for (CookieMapItPair its = cookies_.equal_range(key); |
| 1041 | its.first != its.second; ) { |
| 1042 | CookieMap::iterator curit = its.first; |
| 1043 | CanonicalCookie* cc = curit->second; |
| 1044 | ++its.first; |
| 1045 | |
| 1046 | // If the cookie is expired, delete it. |
| 1047 | if (cc->IsExpired(current)) { |
| 1048 | InternalDeleteCookie(curit, true); |
| 1049 | continue; |
| 1050 | } |
| 1051 | |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 1052 | // Filter out HttpOnly cookies, per options. |
| 1053 | if (options.exclude_httponly() && cc->IsHttpOnly()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1054 | continue; |
| 1055 | |
| 1056 | // Filter out secure cookies unless we're https. |
| 1057 | if (!secure && cc->IsSecure()) |
| 1058 | continue; |
| 1059 | |
| 1060 | if (!cc->IsOnPath(url.path())) |
| 1061 | continue; |
| 1062 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1063 | // Add this cookie to the set of matching cookies. Since we're reading the |
| 1064 | // cookie, update its last access time. |
| 1065 | InternalUpdateCookieAccessTime(cc); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1066 | cookies->push_back(cc); |
| 1067 | } |
| 1068 | } |
| 1069 | |
[email protected] | 79a087a | 2010-02-03 17:08:19 | [diff] [blame] | 1070 | void CookieMonster::FindRawCookies(const std::string& key, |
| 1071 | bool include_secure, |
[email protected] | bfcaed4 | 2010-02-04 17:38:20 | [diff] [blame] | 1072 | const std::string& path, |
[email protected] | 79a087a | 2010-02-03 17:08:19 | [diff] [blame] | 1073 | CookieList* list) { |
| 1074 | for (CookieMapItPair its = cookies_.equal_range(key); |
| 1075 | its.first != its.second; ++its.first) { |
| 1076 | CanonicalCookie* cc = its.first->second; |
[email protected] | bfcaed4 | 2010-02-04 17:38:20 | [diff] [blame] | 1077 | if (!include_secure && cc->IsSecure()) |
| 1078 | continue; |
| 1079 | if (!cc->IsOnPath(path)) |
| 1080 | continue; |
| 1081 | list->push_back(CookieListPair(key, *cc)); |
[email protected] | 79a087a | 2010-02-03 17:08:19 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 1085 | CookieMonster::CookieList CookieMonster::InternalGetAllCookiesForURL( |
| 1086 | const GURL& url) { |
| 1087 | // Do not return removed cookies. |
| 1088 | GarbageCollectExpired(Time::Now(), |
| 1089 | CookieMapItPair(cookies_.begin(), cookies_.end()), |
| 1090 | NULL); |
| 1091 | |
| 1092 | CookieList cookie_list; |
| 1093 | if (!HasCookieableScheme(url)) |
| 1094 | return cookie_list; |
| 1095 | |
| 1096 | bool secure = url.SchemeIsSecure(); |
| 1097 | |
| 1098 | // Query for the full host, For example: 'a.c.blah.com'. |
| 1099 | std::string key(url.host()); |
| 1100 | FindRawCookies(key, secure, url.path(), &cookie_list); |
| 1101 | |
| 1102 | // See if we can search for domain cookies, i.e. if the host has a TLD + 1. |
| 1103 | const std::string domain(GetEffectiveDomain(url.scheme(), key)); |
| 1104 | if (domain.empty()) |
| 1105 | return cookie_list; |
| 1106 | |
| 1107 | // Use same logic as in FindCookiesForHostAndDomain. |
| 1108 | DCHECK_LE(domain.length(), key.length()); |
| 1109 | DCHECK_EQ(0, key.compare(key.length() - domain.length(), domain.length(), |
| 1110 | domain)); |
| 1111 | for (key = "." + key; key.length() > domain.length(); ) { |
| 1112 | FindRawCookies(key, secure, url.path(), &cookie_list); |
| 1113 | const size_t next_dot = key.find('.', 1); // Skip over leading dot. |
| 1114 | key.erase(0, next_dot); |
| 1115 | } |
| 1116 | return cookie_list; |
| 1117 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1118 | |
| 1119 | CookieMonster::ParsedCookie::ParsedCookie(const std::string& cookie_line) |
| 1120 | : is_valid_(false), |
| 1121 | path_index_(0), |
| 1122 | domain_index_(0), |
| 1123 | expires_index_(0), |
| 1124 | maxage_index_(0), |
| 1125 | secure_index_(0), |
| 1126 | httponly_index_(0) { |
| 1127 | |
| 1128 | if (cookie_line.size() > kMaxCookieSize) { |
| 1129 | LOG(INFO) << "Not parsing cookie, too large: " << cookie_line.size(); |
| 1130 | return; |
| 1131 | } |
| 1132 | |
| 1133 | ParseTokenValuePairs(cookie_line); |
| 1134 | if (pairs_.size() > 0) { |
| 1135 | is_valid_ = true; |
| 1136 | SetupAttributes(); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | // Returns true if |c| occurs in |chars| |
| 1141 | // TODO maybe make this take an iterator, could check for end also? |
| 1142 | static inline bool CharIsA(const char c, const char* chars) { |
| 1143 | return strchr(chars, c) != NULL; |
| 1144 | } |
| 1145 | // Seek the iterator to the first occurrence of a character in |chars|. |
| 1146 | // Returns true if it hit the end, false otherwise. |
| 1147 | static inline bool SeekTo(std::string::const_iterator* it, |
| 1148 | const std::string::const_iterator& end, |
| 1149 | const char* chars) { |
| 1150 | for (; *it != end && !CharIsA(**it, chars); ++(*it)); |
| 1151 | return *it == end; |
| 1152 | } |
| 1153 | // Seek the iterator to the first occurrence of a character not in |chars|. |
| 1154 | // Returns true if it hit the end, false otherwise. |
| 1155 | static inline bool SeekPast(std::string::const_iterator* it, |
| 1156 | const std::string::const_iterator& end, |
| 1157 | const char* chars) { |
| 1158 | for (; *it != end && CharIsA(**it, chars); ++(*it)); |
| 1159 | return *it == end; |
| 1160 | } |
| 1161 | static inline bool SeekBackPast(std::string::const_iterator* it, |
| 1162 | const std::string::const_iterator& end, |
| 1163 | const char* chars) { |
| 1164 | for (; *it != end && CharIsA(**it, chars); --(*it)); |
| 1165 | return *it == end; |
| 1166 | } |
| 1167 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1168 | const char CookieMonster::ParsedCookie::kTerminator[] = "\n\r\0"; |
| 1169 | const int CookieMonster::ParsedCookie::kTerminatorLen = |
| 1170 | sizeof(kTerminator) - 1; |
| 1171 | const char CookieMonster::ParsedCookie::kWhitespace[] = " \t"; |
| 1172 | const char CookieMonster::ParsedCookie::kValueSeparator[] = ";"; |
| 1173 | const char CookieMonster::ParsedCookie::kTokenSeparator[] = ";="; |
| 1174 | |
| 1175 | std::string::const_iterator CookieMonster::ParsedCookie::FindFirstTerminator( |
| 1176 | const std::string& s) { |
| 1177 | std::string::const_iterator end = s.end(); |
| 1178 | size_t term_pos = |
| 1179 | s.find_first_of(std::string(kTerminator, kTerminatorLen)); |
| 1180 | if (term_pos != std::string::npos) { |
| 1181 | // We found a character we should treat as an end of string. |
| 1182 | end = s.begin() + term_pos; |
| 1183 | } |
| 1184 | return end; |
| 1185 | } |
| 1186 | |
| 1187 | bool CookieMonster::ParsedCookie::ParseToken( |
| 1188 | std::string::const_iterator* it, |
| 1189 | const std::string::const_iterator& end, |
| 1190 | std::string::const_iterator* token_start, |
| 1191 | std::string::const_iterator* token_end) { |
| 1192 | DCHECK(it && token_start && token_end); |
| 1193 | std::string::const_iterator token_real_end; |
| 1194 | |
| 1195 | // Seek past any whitespace before the "token" (the name). |
| 1196 | // token_start should point at the first character in the token |
| 1197 | if (SeekPast(it, end, kWhitespace)) |
| 1198 | return false; // No token, whitespace or empty. |
| 1199 | *token_start = *it; |
| 1200 | |
| 1201 | // Seek over the token, to the token separator. |
| 1202 | // token_real_end should point at the token separator, i.e. '='. |
| 1203 | // If it == end after the seek, we probably have a token-value. |
| 1204 | SeekTo(it, end, kTokenSeparator); |
| 1205 | token_real_end = *it; |
| 1206 | |
| 1207 | // Ignore any whitespace between the token and the token separator. |
| 1208 | // token_end should point after the last interesting token character, |
| 1209 | // pointing at either whitespace, or at '=' (and equal to token_real_end). |
| 1210 | if (*it != *token_start) { // We could have an empty token name. |
| 1211 | --(*it); // Go back before the token separator. |
| 1212 | // Skip over any whitespace to the first non-whitespace character. |
| 1213 | SeekBackPast(it, *token_start, kWhitespace); |
| 1214 | // Point after it. |
| 1215 | ++(*it); |
| 1216 | } |
| 1217 | *token_end = *it; |
| 1218 | |
| 1219 | // Seek us back to the end of the token. |
| 1220 | *it = token_real_end; |
| 1221 | return true; |
| 1222 | } |
| 1223 | |
| 1224 | void CookieMonster::ParsedCookie::ParseValue( |
| 1225 | std::string::const_iterator* it, |
| 1226 | const std::string::const_iterator& end, |
| 1227 | std::string::const_iterator* value_start, |
| 1228 | std::string::const_iterator* value_end) { |
| 1229 | DCHECK(it && value_start && value_end); |
| 1230 | |
| 1231 | // Seek past any whitespace that might in-between the token and value. |
| 1232 | SeekPast(it, end, kWhitespace); |
| 1233 | // value_start should point at the first character of the value. |
| 1234 | *value_start = *it; |
| 1235 | |
| 1236 | // It is unclear exactly how quoted string values should be handled. |
| 1237 | // Major browsers do different things, for example, Firefox supports |
| 1238 | // semicolons embedded in a quoted value, while IE does not. Looking at |
| 1239 | // the specs, RFC 2109 and 2965 allow for a quoted-string as the value. |
| 1240 | // However, these specs were apparently written after browsers had |
| 1241 | // implemented cookies, and they seem very distant from the reality of |
| 1242 | // what is actually implemented and used on the web. The original spec |
| 1243 | // from Netscape is possibly what is closest to the cookies used today. |
| 1244 | // This spec didn't have explicit support for double quoted strings, and |
| 1245 | // states that ; is not allowed as part of a value. We had originally |
| 1246 | // implement the Firefox behavior (A="B;C"; -> A="B;C";). However, since |
| 1247 | // there is no standard that makes sense, we decided to follow the behavior |
| 1248 | // of IE and Safari, which is closer to the original Netscape proposal. |
| 1249 | // This means that A="B;C" -> A="B;. This also makes the code much simpler |
| 1250 | // and reduces the possibility for invalid cookies, where other browsers |
| 1251 | // like Opera currently reject those invalid cookies (ex A="B" "C";). |
| 1252 | |
| 1253 | // Just look for ';' to terminate ('=' allowed). |
| 1254 | // We can hit the end, maybe they didn't terminate. |
| 1255 | SeekTo(it, end, kValueSeparator); |
| 1256 | |
| 1257 | // Will be pointed at the ; seperator or the end. |
| 1258 | *value_end = *it; |
| 1259 | |
| 1260 | // Ignore any unwanted whitespace after the value. |
| 1261 | if (*value_end != *value_start) { // Could have an empty value |
| 1262 | --(*value_end); |
| 1263 | SeekBackPast(value_end, *value_start, kWhitespace); |
| 1264 | ++(*value_end); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | std::string CookieMonster::ParsedCookie::ParseTokenString( |
| 1269 | const std::string& token) { |
| 1270 | std::string::const_iterator it = token.begin(); |
| 1271 | std::string::const_iterator end = FindFirstTerminator(token); |
| 1272 | |
| 1273 | std::string::const_iterator token_start, token_end; |
| 1274 | if (ParseToken(&it, end, &token_start, &token_end)) |
| 1275 | return std::string(token_start, token_end); |
| 1276 | return std::string(); |
| 1277 | } |
| 1278 | |
| 1279 | std::string CookieMonster::ParsedCookie::ParseValueString( |
| 1280 | const std::string& value) { |
| 1281 | std::string::const_iterator it = value.begin(); |
| 1282 | std::string::const_iterator end = FindFirstTerminator(value); |
| 1283 | |
| 1284 | std::string::const_iterator value_start, value_end; |
| 1285 | ParseValue(&it, end, &value_start, &value_end); |
| 1286 | return std::string(value_start, value_end); |
| 1287 | } |
| 1288 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1289 | // Parse all token/value pairs and populate pairs_. |
| 1290 | void CookieMonster::ParsedCookie::ParseTokenValuePairs( |
| 1291 | const std::string& cookie_line) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1292 | pairs_.clear(); |
| 1293 | |
| 1294 | // Ok, here we go. We should be expecting to be starting somewhere |
| 1295 | // before the cookie line, not including any header name... |
| 1296 | std::string::const_iterator start = cookie_line.begin(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1297 | std::string::const_iterator it = start; |
| 1298 | |
| 1299 | // TODO Make sure we're stripping \r\n in the network code. Then we |
| 1300 | // can log any unexpected terminators. |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1301 | std::string::const_iterator end = FindFirstTerminator(cookie_line); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1302 | |
| 1303 | for (int pair_num = 0; pair_num < kMaxPairs && it != end; ++pair_num) { |
| 1304 | TokenValuePair pair; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1305 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1306 | std::string::const_iterator token_start, token_end; |
| 1307 | if (!ParseToken(&it, end, &token_start, &token_end)) |
| 1308 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1309 | |
| 1310 | if (it == end || *it != '=') { |
| 1311 | // We have a token-value, we didn't have any token name. |
| 1312 | if (pair_num == 0) { |
| 1313 | // For the first time around, we want to treat single values |
| 1314 | // as a value with an empty name. (Mozilla bug 169091). |
| 1315 | // IE seems to also have this behavior, ex "AAA", and "AAA=10" will |
| 1316 | // set 2 different cookies, and setting "BBB" will then replace "AAA". |
| 1317 | pair.first = ""; |
| 1318 | // Rewind to the beginning of what we thought was the token name, |
| 1319 | // and let it get parsed as a value. |
| 1320 | it = token_start; |
| 1321 | } else { |
| 1322 | // Any not-first attribute we want to treat a value as a |
| 1323 | // name with an empty value... This is so something like |
| 1324 | // "secure;" will get parsed as a Token name, and not a value. |
| 1325 | pair.first = std::string(token_start, token_end); |
| 1326 | } |
| 1327 | } else { |
| 1328 | // We have a TOKEN=VALUE. |
| 1329 | pair.first = std::string(token_start, token_end); |
| 1330 | ++it; // Skip past the '='. |
| 1331 | } |
| 1332 | |
| 1333 | // OK, now try to parse a value. |
| 1334 | std::string::const_iterator value_start, value_end; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1335 | ParseValue(&it, end, &value_start, &value_end); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1336 | // OK, we're finished with a Token/Value. |
| 1337 | pair.second = std::string(value_start, value_end); |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1338 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1339 | // From RFC2109: "Attributes (names) (attr) are case-insensitive." |
| 1340 | if (pair_num != 0) |
| 1341 | StringToLowerASCII(&pair.first); |
| 1342 | pairs_.push_back(pair); |
| 1343 | |
| 1344 | // We've processed a token/value pair, we're either at the end of |
| 1345 | // the string or a ValueSeparator like ';', which we want to skip. |
| 1346 | if (it != end) |
| 1347 | ++it; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | void CookieMonster::ParsedCookie::SetupAttributes() { |
| 1352 | static const char kPathTokenName[] = "path"; |
| 1353 | static const char kDomainTokenName[] = "domain"; |
| 1354 | static const char kExpiresTokenName[] = "expires"; |
| 1355 | static const char kMaxAgeTokenName[] = "max-age"; |
| 1356 | static const char kSecureTokenName[] = "secure"; |
| 1357 | static const char kHttpOnlyTokenName[] = "httponly"; |
| 1358 | |
| 1359 | // We skip over the first token/value, the user supplied one. |
| 1360 | for (size_t i = 1; i < pairs_.size(); ++i) { |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1361 | if (pairs_[i].first == kPathTokenName) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1362 | path_index_ = i; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1363 | } else if (pairs_[i].first == kDomainTokenName) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1364 | domain_index_ = i; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1365 | } else if (pairs_[i].first == kExpiresTokenName) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1366 | expires_index_ = i; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1367 | } else if (pairs_[i].first == kMaxAgeTokenName) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1368 | maxage_index_ = i; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1369 | } else if (pairs_[i].first == kSecureTokenName) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1370 | secure_index_ = i; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1371 | } else if (pairs_[i].first == kHttpOnlyTokenName) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1372 | httponly_index_ = i; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1373 | } else { |
| 1374 | /* some attribute we don't know or don't care about. */ |
| 1375 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | // Create a cookie-line for the cookie. For debugging only! |
| 1380 | // If we want to use this for something more than debugging, we |
| 1381 | // should rewrite it better... |
| 1382 | std::string CookieMonster::ParsedCookie::DebugString() const { |
| 1383 | std::string out; |
| 1384 | for (PairList::const_iterator it = pairs_.begin(); |
| 1385 | it != pairs_.end(); ++it) { |
| 1386 | out.append(it->first); |
| 1387 | out.append("="); |
| 1388 | out.append(it->second); |
| 1389 | out.append("; "); |
| 1390 | } |
| 1391 | return out; |
| 1392 | } |
| 1393 | |
[email protected] | a986cc3 | 2010-02-12 23:55:47 | [diff] [blame] | 1394 | CookieMonster::CanonicalCookie::CanonicalCookie(const GURL& url, |
| 1395 | const ParsedCookie& pc) |
| 1396 | : name_(pc.Name()), |
| 1397 | value_(pc.Value()), |
| 1398 | path_(CanonPath(url, pc)), |
| 1399 | creation_date_(Time::Now()), |
| 1400 | last_access_date_(Time()), |
| 1401 | has_expires_(pc.HasExpires()), |
| 1402 | secure_(pc.IsSecure()), |
| 1403 | httponly_(pc.IsHttpOnly()) { |
| 1404 | if (has_expires_) |
| 1405 | expiry_date_ = CanonExpiration(pc, creation_date_, CookieOptions()); |
| 1406 | } |
| 1407 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame^] | 1408 | CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( |
| 1409 | const GURL& url, const std::string& name, const std::string& value, |
| 1410 | const std::string& path, const base::Time& creation_time, |
| 1411 | const base::Time& expiration_time, bool secure, bool http_only) { |
| 1412 | // Expect valid attribute tokens and values, as defined by the ParsedCookie |
| 1413 | // logic, otherwise don't create the cookie. |
| 1414 | std::string parsed_name = ParsedCookie::ParseTokenString(name); |
| 1415 | if (parsed_name != name) |
| 1416 | return NULL; |
| 1417 | std::string parsed_value = ParsedCookie::ParseValueString(value); |
| 1418 | if (parsed_value != value) |
| 1419 | return NULL; |
| 1420 | std::string parsed_path = ParsedCookie::ParseValueString(path); |
| 1421 | if (parsed_path != path) |
| 1422 | return NULL; |
| 1423 | |
| 1424 | std::string cookie_path = CanonPathWithString(url, parsed_path); |
| 1425 | // Expect that the path was either not specified (empty), or is valid. |
| 1426 | if (!parsed_path.empty() && cookie_path != parsed_path) |
| 1427 | return NULL; |
| 1428 | // Canonicalize path again to make sure it escapes characters as needed. |
| 1429 | url_parse::Component path_component(0, cookie_path.length()); |
| 1430 | url_canon::RawCanonOutputT<char> canon_path; |
| 1431 | url_parse::Component canon_path_component; |
| 1432 | url_canon::CanonicalizePath(cookie_path.data(), path_component, |
| 1433 | &canon_path, &canon_path_component); |
| 1434 | cookie_path = std::string(canon_path.data() + canon_path_component.begin, |
| 1435 | canon_path_component.len); |
| 1436 | |
| 1437 | return new CanonicalCookie(parsed_name, parsed_value, cookie_path, |
| 1438 | secure, http_only, creation_time, creation_time, |
| 1439 | !expiration_time.is_null(), expiration_time); |
| 1440 | } |
| 1441 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1442 | bool CookieMonster::CanonicalCookie::IsOnPath( |
| 1443 | const std::string& url_path) const { |
| 1444 | |
| 1445 | // A zero length would be unsafe for our trailing '/' checks, and |
| 1446 | // would also make no sense for our prefix match. The code that |
| 1447 | // creates a CanonicalCookie should make sure the path is never zero length, |
| 1448 | // but we double check anyway. |
| 1449 | if (path_.empty()) |
| 1450 | return false; |
| 1451 | |
| 1452 | // The Mozilla code broke it into 3 cases, if it's strings lengths |
| 1453 | // are less than, equal, or greater. I think this is simpler: |
| 1454 | |
| 1455 | // Make sure the cookie path is a prefix of the url path. If the |
| 1456 | // url path is shorter than the cookie path, then the cookie path |
| 1457 | // can't be a prefix. |
| 1458 | if (url_path.find(path_) != 0) |
| 1459 | return false; |
| 1460 | |
| 1461 | // Now we know that url_path is >= cookie_path, and that cookie_path |
| 1462 | // is a prefix of url_path. If they are the are the same length then |
| 1463 | // they are identical, otherwise we need an additional check: |
| 1464 | |
| 1465 | // In order to avoid in correctly matching a cookie path of /blah |
| 1466 | // with a request path of '/blahblah/', we need to make sure that either |
| 1467 | // the cookie path ends in a trailing '/', or that we prefix up to a '/' |
| 1468 | // in the url path. Since we know that the url path length is greater |
| 1469 | // than the cookie path length, it's safe to index one byte past. |
| 1470 | if (path_.length() != url_path.length() && |
| 1471 | path_[path_.length() - 1] != '/' && |
| 1472 | url_path[path_.length()] != '/') |
| 1473 | return false; |
| 1474 | |
| 1475 | return true; |
| 1476 | } |
| 1477 | |
| 1478 | std::string CookieMonster::CanonicalCookie::DebugString() const { |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 1479 | return StringPrintf("name: %s value: %s path: %s creation: %" PRId64, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1480 | name_.c_str(), value_.c_str(), path_.c_str(), |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 1481 | static_cast<int64>(creation_date_.ToTimeT())); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1482 | } |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 1483 | |
| 1484 | } // namespace |