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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 // Returns true if |c| occurs in |chars| | 66 // Returns true if |c| occurs in |chars| |
67 // 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? |
68 inline bool CharIsA(const char c, const char* chars) { | 68 inline bool CharIsA(const char c, const char* chars) { |
69 return strchr(chars, c) != NULL; | 69 return strchr(chars, c) != NULL; |
70 } | 70 } |
71 // Seek the iterator to the first occurrence of a character in |chars|. | 71 // Seek the iterator to the first occurrence of a character in |chars|. |
72 // Returns true if it hit the end, false otherwise. | 72 // Returns true if it hit the end, false otherwise. |
73 inline bool SeekTo(std::string::const_iterator* it, | 73 inline bool SeekTo(std::string::const_iterator* it, |
74 const std::string::const_iterator& end, | 74 const std::string::const_iterator& end, |
75 const char* chars) { | 75 const char* chars) { |
76 for (; *it != end && !CharIsA(**it, chars); ++(*it)) {} | 76 for (; *it != end && !CharIsA(**it, chars); ++(*it)) { |
| 77 } |
77 return *it == end; | 78 return *it == end; |
78 } | 79 } |
79 // Seek the iterator to the first occurrence of a character not in |chars|. | 80 // Seek the iterator to the first occurrence of a character not in |chars|. |
80 // Returns true if it hit the end, false otherwise. | 81 // Returns true if it hit the end, false otherwise. |
81 inline bool SeekPast(std::string::const_iterator* it, | 82 inline bool SeekPast(std::string::const_iterator* it, |
82 const std::string::const_iterator& end, | 83 const std::string::const_iterator& end, |
83 const char* chars) { | 84 const char* chars) { |
84 for (; *it != end && CharIsA(**it, chars); ++(*it)) {} | 85 for (; *it != end && CharIsA(**it, chars); ++(*it)) { |
| 86 } |
85 return *it == end; | 87 return *it == end; |
86 } | 88 } |
87 inline bool SeekBackPast(std::string::const_iterator* it, | 89 inline bool SeekBackPast(std::string::const_iterator* it, |
88 const std::string::const_iterator& end, | 90 const std::string::const_iterator& end, |
89 const char* chars) { | 91 const char* chars) { |
90 for (; *it != end && CharIsA(**it, chars); --(*it)) {} | 92 for (; *it != end && CharIsA(**it, chars); --(*it)) { |
| 93 } |
91 return *it == end; | 94 return *it == end; |
92 } | 95 } |
93 | 96 |
94 // Validate whether |value| is a valid token according to [RFC2616], | 97 // Validate whether |value| is a valid token according to [RFC2616], |
95 // Section 2.2. | 98 // Section 2.2. |
96 bool IsValidToken(const std::string& value) { | 99 bool IsValidToken(const std::string& value) { |
97 if (value.empty()) | 100 if (value.empty()) |
98 return false; | 101 return false; |
99 | 102 |
100 // Check that |value| has no separators. | 103 // Check that |value| has no separators. |
(...skipping 12 matching lines...) Expand all Loading... |
113 | 116 |
114 // Validate value, which may be according to RFC 6265 | 117 // Validate value, which may be according to RFC 6265 |
115 // cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE ) | 118 // cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE ) |
116 // cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E | 119 // cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E |
117 // ; US-ASCII characters excluding CTLs, | 120 // ; US-ASCII characters excluding CTLs, |
118 // ; whitespace DQUOTE, comma, semicolon, | 121 // ; whitespace DQUOTE, comma, semicolon, |
119 // ; and backslash | 122 // ; and backslash |
120 bool IsValidCookieValue(const std::string& value) { | 123 bool IsValidCookieValue(const std::string& value) { |
121 // Number of characters to skip in validation at beginning and end of string. | 124 // Number of characters to skip in validation at beginning and end of string. |
122 size_t skip = 0; | 125 size_t skip = 0; |
123 if (value.size() >= 2 && *value.begin() == '"' && *(value.end()-1) == '"') | 126 if (value.size() >= 2 && *value.begin() == '"' && *(value.end() - 1) == '"') |
124 skip = 1; | 127 skip = 1; |
125 for (std::string::const_iterator i = value.begin() + skip; | 128 for (std::string::const_iterator i = value.begin() + skip; |
126 i != value.end() - skip; ++i) { | 129 i != value.end() - skip; ++i) { |
127 bool valid_octet = | 130 bool valid_octet = |
128 (*i == 0x21 || | 131 (*i == 0x21 || (*i >= 0x23 && *i <= 0x2B) || |
129 (*i >= 0x23 && *i <= 0x2B) || | 132 (*i >= 0x2D && *i <= 0x3A) || (*i >= 0x3C && *i <= 0x5B) || |
130 (*i >= 0x2D && *i <= 0x3A) || | |
131 (*i >= 0x3C && *i <= 0x5B) || | |
132 (*i >= 0x5D && *i <= 0x7E)); | 133 (*i >= 0x5D && *i <= 0x7E)); |
133 if (!valid_octet) | 134 if (!valid_octet) |
134 return false; | 135 return false; |
135 } | 136 } |
136 return true; | 137 return true; |
137 } | 138 } |
138 | 139 |
139 bool IsControlCharacter(unsigned char c) { | 140 bool IsControlCharacter(unsigned char c) { |
140 return c <= 31; | 141 return c <= 31; |
141 } | 142 } |
(...skipping 13 matching lines...) Expand all Loading... |
155 namespace net { | 156 namespace net { |
156 | 157 |
157 ParsedCookie::ParsedCookie(const std::string& cookie_line) | 158 ParsedCookie::ParsedCookie(const std::string& cookie_line) |
158 : path_index_(0), | 159 : path_index_(0), |
159 domain_index_(0), | 160 domain_index_(0), |
160 expires_index_(0), | 161 expires_index_(0), |
161 maxage_index_(0), | 162 maxage_index_(0), |
162 secure_index_(0), | 163 secure_index_(0), |
163 httponly_index_(0), | 164 httponly_index_(0), |
164 priority_index_(0) { | 165 priority_index_(0) { |
165 | |
166 if (cookie_line.size() > kMaxCookieSize) { | 166 if (cookie_line.size() > kMaxCookieSize) { |
167 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size(); | 167 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size(); |
168 return; | 168 return; |
169 } | 169 } |
170 | 170 |
171 ParseTokenValuePairs(cookie_line); | 171 ParseTokenValuePairs(cookie_line); |
172 if (!pairs_.empty()) | 172 if (!pairs_.empty()) |
173 SetupAttributes(); | 173 SetupAttributes(); |
174 } | 174 } |
175 | 175 |
176 ParsedCookie::~ParsedCookie() { | 176 ParsedCookie::~ParsedCookie() { |
177 } | 177 } |
178 | 178 |
179 bool ParsedCookie::IsValid() const { | 179 bool ParsedCookie::IsValid() const { |
180 return !pairs_.empty(); | 180 return !pairs_.empty(); |
181 } | 181 } |
182 | 182 |
183 CookiePriority ParsedCookie::Priority() const { | 183 CookiePriority ParsedCookie::Priority() const { |
184 return (priority_index_ == 0) ? COOKIE_PRIORITY_DEFAULT : | 184 return (priority_index_ == 0) |
185 StringToCookiePriority(pairs_[priority_index_].second); | 185 ? COOKIE_PRIORITY_DEFAULT |
| 186 : StringToCookiePriority(pairs_[priority_index_].second); |
186 } | 187 } |
187 | 188 |
188 bool ParsedCookie::SetName(const std::string& name) { | 189 bool ParsedCookie::SetName(const std::string& name) { |
189 if (!IsValidToken(name)) | 190 if (!IsValidToken(name)) |
190 return false; | 191 return false; |
191 if (pairs_.empty()) | 192 if (pairs_.empty()) |
192 pairs_.push_back(std::make_pair("", "")); | 193 pairs_.push_back(std::make_pair("", "")); |
193 pairs_[0].first = name; | 194 pairs_[0].first = name; |
194 return true; | 195 return true; |
195 } | 196 } |
(...skipping 30 matching lines...) Expand all Loading... |
226 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) { | 227 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) { |
227 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only); | 228 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only); |
228 } | 229 } |
229 | 230 |
230 bool ParsedCookie::SetPriority(const std::string& priority) { | 231 bool ParsedCookie::SetPriority(const std::string& priority) { |
231 return SetString(&priority_index_, kPriorityTokenName, priority); | 232 return SetString(&priority_index_, kPriorityTokenName, priority); |
232 } | 233 } |
233 | 234 |
234 std::string ParsedCookie::ToCookieLine() const { | 235 std::string ParsedCookie::ToCookieLine() const { |
235 std::string out; | 236 std::string out; |
236 for (PairList::const_iterator it = pairs_.begin(); | 237 for (PairList::const_iterator it = pairs_.begin(); it != pairs_.end(); ++it) { |
237 it != pairs_.end(); ++it) { | |
238 if (!out.empty()) | 238 if (!out.empty()) |
239 out.append("; "); | 239 out.append("; "); |
240 out.append(it->first); | 240 out.append(it->first); |
241 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName) { | 241 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName) { |
242 out.append("="); | 242 out.append("="); |
243 out.append(it->second); | 243 out.append(it->second); |
244 } | 244 } |
245 } | 245 } |
246 return out; | 246 return out; |
247 } | 247 } |
248 | 248 |
249 std::string::const_iterator ParsedCookie::FindFirstTerminator( | 249 std::string::const_iterator ParsedCookie::FindFirstTerminator( |
250 const std::string& s) { | 250 const std::string& s) { |
251 std::string::const_iterator end = s.end(); | 251 std::string::const_iterator end = s.end(); |
252 size_t term_pos = | 252 size_t term_pos = s.find_first_of(std::string(kTerminator, kTerminatorLen)); |
253 s.find_first_of(std::string(kTerminator, kTerminatorLen)); | |
254 if (term_pos != std::string::npos) { | 253 if (term_pos != std::string::npos) { |
255 // We found a character we should treat as an end of string. | 254 // We found a character we should treat as an end of string. |
256 end = s.begin() + term_pos; | 255 end = s.begin() + term_pos; |
257 } | 256 } |
258 return end; | 257 return end; |
259 } | 258 } |
260 | 259 |
261 bool ParsedCookie::ParseToken(std::string::const_iterator* it, | 260 bool ParsedCookie::ParseToken(std::string::const_iterator* it, |
262 const std::string::const_iterator& end, | 261 const std::string::const_iterator& end, |
263 std::string::const_iterator* token_start, | 262 std::string::const_iterator* token_start, |
(...skipping 10 matching lines...) Expand all Loading... |
274 // Seek over the token, to the token separator. | 273 // Seek over the token, to the token separator. |
275 // token_real_end should point at the token separator, i.e. '='. | 274 // token_real_end should point at the token separator, i.e. '='. |
276 // If it == end after the seek, we probably have a token-value. | 275 // If it == end after the seek, we probably have a token-value. |
277 SeekTo(it, end, kTokenSeparator); | 276 SeekTo(it, end, kTokenSeparator); |
278 token_real_end = *it; | 277 token_real_end = *it; |
279 | 278 |
280 // Ignore any whitespace between the token and the token separator. | 279 // Ignore any whitespace between the token and the token separator. |
281 // token_end should point after the last interesting token character, | 280 // token_end should point after the last interesting token character, |
282 // pointing at either whitespace, or at '=' (and equal to token_real_end). | 281 // pointing at either whitespace, or at '=' (and equal to token_real_end). |
283 if (*it != *token_start) { // We could have an empty token name. | 282 if (*it != *token_start) { // We could have an empty token name. |
284 --(*it); // Go back before the token separator. | 283 --(*it); // Go back before the token separator. |
285 // Skip over any whitespace to the first non-whitespace character. | 284 // Skip over any whitespace to the first non-whitespace character. |
286 SeekBackPast(it, *token_start, kWhitespace); | 285 SeekBackPast(it, *token_start, kWhitespace); |
287 // Point after it. | 286 // Point after it. |
288 ++(*it); | 287 ++(*it); |
289 } | 288 } |
290 *token_end = *it; | 289 *token_end = *it; |
291 | 290 |
292 // Seek us back to the end of the token. | 291 // Seek us back to the end of the token. |
293 *it = token_real_end; | 292 *it = token_real_end; |
294 return true; | 293 return true; |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 const std::string& key, | 441 const std::string& key, |
443 const std::string& value) { | 442 const std::string& value) { |
444 if (value.empty()) { | 443 if (value.empty()) { |
445 ClearAttributePair(*index); | 444 ClearAttributePair(*index); |
446 return true; | 445 return true; |
447 } else { | 446 } else { |
448 return SetAttributePair(index, key, value); | 447 return SetAttributePair(index, key, value); |
449 } | 448 } |
450 } | 449 } |
451 | 450 |
452 bool ParsedCookie::SetBool(size_t* index, | 451 bool ParsedCookie::SetBool(size_t* index, const std::string& key, bool value) { |
453 const std::string& key, | |
454 bool value) { | |
455 if (!value) { | 452 if (!value) { |
456 ClearAttributePair(*index); | 453 ClearAttributePair(*index); |
457 return true; | 454 return true; |
458 } else { | 455 } else { |
459 return SetAttributePair(index, key, std::string()); | 456 return SetAttributePair(index, key, std::string()); |
460 } | 457 } |
461 } | 458 } |
462 | 459 |
463 bool ParsedCookie::SetAttributePair(size_t* index, | 460 bool ParsedCookie::SetAttributePair(size_t* index, |
464 const std::string& key, | 461 const std::string& key, |
(...skipping 11 matching lines...) Expand all Loading... |
476 return true; | 473 return true; |
477 } | 474 } |
478 | 475 |
479 void ParsedCookie::ClearAttributePair(size_t index) { | 476 void ParsedCookie::ClearAttributePair(size_t index) { |
480 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared. | 477 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared. |
481 // Cookie attributes that don't have a value at the moment, are represented | 478 // Cookie attributes that don't have a value at the moment, are represented |
482 // with an index being equal to 0. | 479 // with an index being equal to 0. |
483 if (index == 0) | 480 if (index == 0) |
484 return; | 481 return; |
485 | 482 |
486 size_t* indexes[] = { &path_index_, &domain_index_, &expires_index_, | 483 size_t* indexes[] = {&path_index_, |
487 &maxage_index_, &secure_index_, &httponly_index_, | 484 &domain_index_, |
488 &priority_index_ }; | 485 &expires_index_, |
| 486 &maxage_index_, |
| 487 &secure_index_, |
| 488 &httponly_index_, |
| 489 &priority_index_}; |
489 for (size_t i = 0; i < arraysize(indexes); ++i) { | 490 for (size_t i = 0; i < arraysize(indexes); ++i) { |
490 if (*indexes[i] == index) | 491 if (*indexes[i] == index) |
491 *indexes[i] = 0; | 492 *indexes[i] = 0; |
492 else if (*indexes[i] > index) | 493 else if (*indexes[i] > index) |
493 --*indexes[i]; | 494 --*indexes[i]; |
494 } | 495 } |
495 pairs_.erase(pairs_.begin() + index); | 496 pairs_.erase(pairs_.begin() + index); |
496 } | 497 } |
497 | 498 |
498 } // namespace | 499 } // namespace |
OLD | NEW |