Chromium Code Reviews| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 #include "base/strings/string_util.h" | 48 #include "base/strings/string_util.h" |
| 49 | 49 |
| 50 namespace { | 50 namespace { |
| 51 | 51 |
| 52 const char kPathTokenName[] = "path"; | 52 const char kPathTokenName[] = "path"; |
| 53 const char kDomainTokenName[] = "domain"; | 53 const char kDomainTokenName[] = "domain"; |
| 54 const char kExpiresTokenName[] = "expires"; | 54 const char kExpiresTokenName[] = "expires"; |
| 55 const char kMaxAgeTokenName[] = "max-age"; | 55 const char kMaxAgeTokenName[] = "max-age"; |
| 56 const char kSecureTokenName[] = "secure"; | 56 const char kSecureTokenName[] = "secure"; |
| 57 const char kHttpOnlyTokenName[] = "httponly"; | 57 const char kHttpOnlyTokenName[] = "httponly"; |
| 58 const char kFirstPartyOnlyTokenName[] = "first-party-only"; | 58 const char kSameSiteTokenName[] = "same_site"; |
|
Mike West
2016/01/29 06:39:37
This should be `samesite`, not `same_site`. Hooray
| |
| 59 const char kPriorityTokenName[] = "priority"; | 59 const char kPriorityTokenName[] = "priority"; |
| 60 | 60 |
| 61 const char kTerminator[] = "\n\r\0"; | 61 const char kTerminator[] = "\n\r\0"; |
| 62 const int kTerminatorLen = sizeof(kTerminator) - 1; | 62 const int kTerminatorLen = sizeof(kTerminator) - 1; |
| 63 const char kWhitespace[] = " \t"; | 63 const char kWhitespace[] = " \t"; |
| 64 const char kValueSeparator[] = ";"; | 64 const char kValueSeparator[] = ";"; |
| 65 const char kTokenSeparator[] = ";="; | 65 const char kTokenSeparator[] = ";="; |
| 66 | 66 |
| 67 // Returns true if |c| occurs in |chars| | 67 // Returns true if |c| occurs in |chars| |
| 68 // TODO(erikwright): maybe make this take an iterator, could check for end also? | 68 // TODO(erikwright): maybe make this take an iterator, could check for end also? |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 | 156 |
| 157 namespace net { | 157 namespace net { |
| 158 | 158 |
| 159 ParsedCookie::ParsedCookie(const std::string& cookie_line) | 159 ParsedCookie::ParsedCookie(const std::string& cookie_line) |
| 160 : path_index_(0), | 160 : path_index_(0), |
| 161 domain_index_(0), | 161 domain_index_(0), |
| 162 expires_index_(0), | 162 expires_index_(0), |
| 163 maxage_index_(0), | 163 maxage_index_(0), |
| 164 secure_index_(0), | 164 secure_index_(0), |
| 165 httponly_index_(0), | 165 httponly_index_(0), |
| 166 firstpartyonly_index_(0), | 166 same_site_index_(0), |
| 167 priority_index_(0) { | 167 priority_index_(0) { |
| 168 if (cookie_line.size() > kMaxCookieSize) { | 168 if (cookie_line.size() > kMaxCookieSize) { |
| 169 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size(); | 169 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size(); |
| 170 return; | 170 return; |
| 171 } | 171 } |
| 172 | 172 |
| 173 ParseTokenValuePairs(cookie_line); | 173 ParseTokenValuePairs(cookie_line); |
| 174 if (!pairs_.empty()) | 174 if (!pairs_.empty()) |
| 175 SetupAttributes(); | 175 SetupAttributes(); |
| 176 } | 176 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 } | 223 } |
| 224 | 224 |
| 225 bool ParsedCookie::SetIsSecure(bool is_secure) { | 225 bool ParsedCookie::SetIsSecure(bool is_secure) { |
| 226 return SetBool(&secure_index_, kSecureTokenName, is_secure); | 226 return SetBool(&secure_index_, kSecureTokenName, is_secure); |
| 227 } | 227 } |
| 228 | 228 |
| 229 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) { | 229 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) { |
| 230 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only); | 230 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only); |
| 231 } | 231 } |
| 232 | 232 |
| 233 bool ParsedCookie::SetIsFirstPartyOnly(bool is_first_party_only) { | 233 bool ParsedCookie::SetIsSameSite(bool is_same_site) { |
| 234 return SetBool(&firstpartyonly_index_, kFirstPartyOnlyTokenName, | 234 return SetBool(&same_site_index_, kSameSiteTokenName, is_same_site); |
| 235 is_first_party_only); | |
| 236 } | 235 } |
| 237 | 236 |
| 238 bool ParsedCookie::SetPriority(const std::string& priority) { | 237 bool ParsedCookie::SetPriority(const std::string& priority) { |
| 239 return SetString(&priority_index_, kPriorityTokenName, priority); | 238 return SetString(&priority_index_, kPriorityTokenName, priority); |
| 240 } | 239 } |
| 241 | 240 |
| 242 std::string ParsedCookie::ToCookieLine() const { | 241 std::string ParsedCookie::ToCookieLine() const { |
| 243 std::string out; | 242 std::string out; |
| 244 for (PairList::const_iterator it = pairs_.begin(); it != pairs_.end(); ++it) { | 243 for (PairList::const_iterator it = pairs_.begin(); it != pairs_.end(); ++it) { |
| 245 if (!out.empty()) | 244 if (!out.empty()) |
| 246 out.append("; "); | 245 out.append("; "); |
| 247 out.append(it->first); | 246 out.append(it->first); |
| 248 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName && | 247 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName && |
| 249 it->first != kFirstPartyOnlyTokenName) { | 248 it->first != kSameSiteTokenName) { |
| 250 out.append("="); | 249 out.append("="); |
| 251 out.append(it->second); | 250 out.append(it->second); |
| 252 } | 251 } |
| 253 } | 252 } |
| 254 return out; | 253 return out; |
| 255 } | 254 } |
| 256 | 255 |
| 257 std::string::const_iterator ParsedCookie::FindFirstTerminator( | 256 std::string::const_iterator ParsedCookie::FindFirstTerminator( |
| 258 const std::string& s) { | 257 const std::string& s) { |
| 259 std::string::const_iterator end = s.end(); | 258 std::string::const_iterator end = s.end(); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 430 } else if (pairs_[i].first == kDomainTokenName) { | 429 } else if (pairs_[i].first == kDomainTokenName) { |
| 431 domain_index_ = i; | 430 domain_index_ = i; |
| 432 } else if (pairs_[i].first == kExpiresTokenName) { | 431 } else if (pairs_[i].first == kExpiresTokenName) { |
| 433 expires_index_ = i; | 432 expires_index_ = i; |
| 434 } else if (pairs_[i].first == kMaxAgeTokenName) { | 433 } else if (pairs_[i].first == kMaxAgeTokenName) { |
| 435 maxage_index_ = i; | 434 maxage_index_ = i; |
| 436 } else if (pairs_[i].first == kSecureTokenName) { | 435 } else if (pairs_[i].first == kSecureTokenName) { |
| 437 secure_index_ = i; | 436 secure_index_ = i; |
| 438 } else if (pairs_[i].first == kHttpOnlyTokenName) { | 437 } else if (pairs_[i].first == kHttpOnlyTokenName) { |
| 439 httponly_index_ = i; | 438 httponly_index_ = i; |
| 440 } else if (pairs_[i].first == kFirstPartyOnlyTokenName) { | 439 } else if (pairs_[i].first == kSameSiteTokenName) { |
| 441 firstpartyonly_index_ = i; | 440 same_site_index_ = i; |
| 442 } else if (pairs_[i].first == kPriorityTokenName) { | 441 } else if (pairs_[i].first == kPriorityTokenName) { |
| 443 priority_index_ = i; | 442 priority_index_ = i; |
| 444 } else { | 443 } else { |
| 445 /* some attribute we don't know or don't care about. */ | 444 /* some attribute we don't know or don't care about. */ |
| 446 } | 445 } |
| 447 } | 446 } |
| 448 } | 447 } |
| 449 | 448 |
| 450 bool ParsedCookie::SetString(size_t* index, | 449 bool ParsedCookie::SetString(size_t* index, |
| 451 const std::string& key, | 450 const std::string& key, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 483 return true; | 482 return true; |
| 484 } | 483 } |
| 485 | 484 |
| 486 void ParsedCookie::ClearAttributePair(size_t index) { | 485 void ParsedCookie::ClearAttributePair(size_t index) { |
| 487 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared. | 486 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared. |
| 488 // Cookie attributes that don't have a value at the moment, are represented | 487 // Cookie attributes that don't have a value at the moment, are represented |
| 489 // with an index being equal to 0. | 488 // with an index being equal to 0. |
| 490 if (index == 0) | 489 if (index == 0) |
| 491 return; | 490 return; |
| 492 | 491 |
| 493 size_t* indexes[] = {&path_index_, | 492 size_t* indexes[] = {&path_index_, &domain_index_, &expires_index_, |
| 494 &domain_index_, | 493 &maxage_index_, &secure_index_, &httponly_index_, |
| 495 &expires_index_, | 494 &same_site_index_, &priority_index_}; |
| 496 &maxage_index_, | |
| 497 &secure_index_, | |
| 498 &httponly_index_, | |
| 499 &firstpartyonly_index_, | |
| 500 &priority_index_}; | |
| 501 for (size_t i = 0; i < arraysize(indexes); ++i) { | 495 for (size_t i = 0; i < arraysize(indexes); ++i) { |
| 502 if (*indexes[i] == index) | 496 if (*indexes[i] == index) |
| 503 *indexes[i] = 0; | 497 *indexes[i] = 0; |
| 504 else if (*indexes[i] > index) | 498 else if (*indexes[i] > index) |
| 505 --*indexes[i]; | 499 --*indexes[i]; |
| 506 } | 500 } |
| 507 pairs_.erase(pairs_.begin() + index); | 501 pairs_.erase(pairs_.begin() + index); |
| 508 } | 502 } |
| 509 | 503 |
| 510 } // namespace | 504 } // namespace |
| OLD | NEW |