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/string_util.h" | 48 #include "base/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 kPriorityTokenName[] = "priority"; |
58 | 59 |
59 const char kTerminator[] = "\n\r\0"; | 60 const char kTerminator[] = "\n\r\0"; |
60 const int kTerminatorLen = sizeof(kTerminator) - 1; | 61 const int kTerminatorLen = sizeof(kTerminator) - 1; |
61 const char kWhitespace[] = " \t"; | 62 const char kWhitespace[] = " \t"; |
62 const char kValueSeparator[] = ";"; | 63 const char kValueSeparator[] = ";"; |
63 const char kTokenSeparator[] = ";="; | 64 const char kTokenSeparator[] = ";="; |
64 | 65 |
65 // Returns true if |c| occurs in |chars| | 66 // Returns true if |c| occurs in |chars| |
66 // TODO(erikwright): maybe make this take an iterator, could check for end also? | 67 // TODO(erikwright): maybe make this take an iterator, could check for end also? |
67 inline bool CharIsA(const char c, const char* chars) { | 68 inline bool CharIsA(const char c, const char* chars) { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 } // namespace | 149 } // namespace |
149 | 150 |
150 namespace net { | 151 namespace net { |
151 | 152 |
152 ParsedCookie::ParsedCookie(const std::string& cookie_line) | 153 ParsedCookie::ParsedCookie(const std::string& cookie_line) |
153 : path_index_(0), | 154 : path_index_(0), |
154 domain_index_(0), | 155 domain_index_(0), |
155 expires_index_(0), | 156 expires_index_(0), |
156 maxage_index_(0), | 157 maxage_index_(0), |
157 secure_index_(0), | 158 secure_index_(0), |
158 httponly_index_(0) { | 159 httponly_index_(0), |
| 160 priority_index_(0) { |
159 | 161 |
160 if (cookie_line.size() > kMaxCookieSize) { | 162 if (cookie_line.size() > kMaxCookieSize) { |
161 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size(); | 163 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size(); |
162 return; | 164 return; |
163 } | 165 } |
164 | 166 |
165 ParseTokenValuePairs(cookie_line); | 167 ParseTokenValuePairs(cookie_line); |
166 if (!pairs_.empty()) | 168 if (!pairs_.empty()) |
167 SetupAttributes(); | 169 SetupAttributes(); |
168 } | 170 } |
169 | 171 |
170 ParsedCookie::~ParsedCookie() { | 172 ParsedCookie::~ParsedCookie() { |
171 } | 173 } |
172 | 174 |
173 bool ParsedCookie::IsValid() const { | 175 bool ParsedCookie::IsValid() const { |
174 return !pairs_.empty(); | 176 return !pairs_.empty(); |
175 } | 177 } |
176 | 178 |
| 179 CookiePriority ParsedCookie::Priority() const { |
| 180 return (priority_index_ == 0) ? PRIORITY_DEFAULT : |
| 181 StringToCookiePriority(pairs_[priority_index_].second); |
| 182 } |
| 183 |
177 bool ParsedCookie::SetName(const std::string& name) { | 184 bool ParsedCookie::SetName(const std::string& name) { |
178 if (!IsValidToken(name)) | 185 if (!IsValidToken(name)) |
179 return false; | 186 return false; |
180 if (pairs_.empty()) | 187 if (pairs_.empty()) |
181 pairs_.push_back(std::make_pair("", "")); | 188 pairs_.push_back(std::make_pair("", "")); |
182 pairs_[0].first = name; | 189 pairs_[0].first = name; |
183 return true; | 190 return true; |
184 } | 191 } |
185 | 192 |
186 bool ParsedCookie::SetValue(const std::string& value) { | 193 bool ParsedCookie::SetValue(const std::string& value) { |
(...skipping 22 matching lines...) Expand all Loading... |
209 } | 216 } |
210 | 217 |
211 bool ParsedCookie::SetIsSecure(bool is_secure) { | 218 bool ParsedCookie::SetIsSecure(bool is_secure) { |
212 return SetBool(&secure_index_, kSecureTokenName, is_secure); | 219 return SetBool(&secure_index_, kSecureTokenName, is_secure); |
213 } | 220 } |
214 | 221 |
215 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) { | 222 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) { |
216 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only); | 223 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only); |
217 } | 224 } |
218 | 225 |
| 226 bool ParsedCookie::SetPriority(const std::string& priority) { |
| 227 return SetString(&priority_index_, kPriorityTokenName, priority); |
| 228 } |
| 229 |
219 std::string ParsedCookie::ToCookieLine() const { | 230 std::string ParsedCookie::ToCookieLine() const { |
220 std::string out; | 231 std::string out; |
221 for (PairList::const_iterator it = pairs_.begin(); | 232 for (PairList::const_iterator it = pairs_.begin(); |
222 it != pairs_.end(); ++it) { | 233 it != pairs_.end(); ++it) { |
223 if (!out.empty()) | 234 if (!out.empty()) |
224 out.append("; "); | 235 out.append("; "); |
225 out.append(it->first); | 236 out.append(it->first); |
226 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName) { | 237 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName) { |
227 out.append("="); | 238 out.append("="); |
228 out.append(it->second); | 239 out.append(it->second); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 } else if (pairs_[i].first == kDomainTokenName) { | 404 } else if (pairs_[i].first == kDomainTokenName) { |
394 domain_index_ = i; | 405 domain_index_ = i; |
395 } else if (pairs_[i].first == kExpiresTokenName) { | 406 } else if (pairs_[i].first == kExpiresTokenName) { |
396 expires_index_ = i; | 407 expires_index_ = i; |
397 } else if (pairs_[i].first == kMaxAgeTokenName) { | 408 } else if (pairs_[i].first == kMaxAgeTokenName) { |
398 maxage_index_ = i; | 409 maxage_index_ = i; |
399 } else if (pairs_[i].first == kSecureTokenName) { | 410 } else if (pairs_[i].first == kSecureTokenName) { |
400 secure_index_ = i; | 411 secure_index_ = i; |
401 } else if (pairs_[i].first == kHttpOnlyTokenName) { | 412 } else if (pairs_[i].first == kHttpOnlyTokenName) { |
402 httponly_index_ = i; | 413 httponly_index_ = i; |
| 414 } else if (pairs_[i].first == kPriorityTokenName) { |
| 415 priority_index_ = i; |
403 } else { | 416 } else { |
404 /* some attribute we don't know or don't care about. */ | 417 /* some attribute we don't know or don't care about. */ |
405 } | 418 } |
406 } | 419 } |
407 } | 420 } |
408 | 421 |
409 bool ParsedCookie::SetString(size_t* index, | 422 bool ParsedCookie::SetString(size_t* index, |
410 const std::string& key, | 423 const std::string& key, |
411 const std::string& value) { | 424 const std::string& value) { |
412 if (value.empty()) { | 425 if (value.empty()) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 } | 458 } |
446 | 459 |
447 void ParsedCookie::ClearAttributePair(size_t index) { | 460 void ParsedCookie::ClearAttributePair(size_t index) { |
448 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared. | 461 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared. |
449 // Cookie attributes that don't have a value at the moment, are represented | 462 // Cookie attributes that don't have a value at the moment, are represented |
450 // with an index being equal to 0. | 463 // with an index being equal to 0. |
451 if (index == 0) | 464 if (index == 0) |
452 return; | 465 return; |
453 | 466 |
454 size_t* indexes[] = { &path_index_, &domain_index_, &expires_index_, | 467 size_t* indexes[] = { &path_index_, &domain_index_, &expires_index_, |
455 &maxage_index_, &secure_index_, &httponly_index_ }; | 468 &maxage_index_, &secure_index_, &httponly_index_, |
| 469 &priority_index_ }; |
456 for (size_t i = 0; i < arraysize(indexes); ++i) { | 470 for (size_t i = 0; i < arraysize(indexes); ++i) { |
457 if (*indexes[i] == index) | 471 if (*indexes[i] == index) |
458 *indexes[i] = 0; | 472 *indexes[i] = 0; |
459 else if (*indexes[i] > index) | 473 else if (*indexes[i] > index) |
460 --*indexes[i]; | 474 --*indexes[i]; |
461 } | 475 } |
462 pairs_.erase(pairs_.begin() + index); | 476 pairs_.erase(pairs_.begin() + index); |
463 } | 477 } |
464 | 478 |
465 } // namespace | 479 } // namespace |
OLD | NEW |