Probably broke Win7 Tests (dbg)(6). http://build.chromium.org/p/chromium.win/builders...
[chromium-blink-merge.git] / net / cookies / parsed_cookie.cc
blob60e0bbb66185da7a717b5d8ae685426a44c0603d
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
3 // found in the LICENSE file.
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
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/
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.
20 * The Original Code is mozilla.org code.
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.
27 * Contributor(s):
28 * Daniel Witte (dwitte@stanford.edu)
29 * Michiel van Leeuwen (mvl@exedo.nl)
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.
43 * ***** END LICENSE BLOCK ***** */
45 #include "net/cookies/parsed_cookie.h"
47 #include "base/logging.h"
48 #include "base/strings/string_util.h"
50 namespace {
52 const char kPathTokenName[] = "path";
53 const char kDomainTokenName[] = "domain";
54 const char kExpiresTokenName[] = "expires";
55 const char kMaxAgeTokenName[] = "max-age";
56 const char kSecureTokenName[] = "secure";
57 const char kHttpOnlyTokenName[] = "httponly";
58 const char kPriorityTokenName[] = "priority";
60 const char kTerminator[] = "\n\r\0";
61 const int kTerminatorLen = sizeof(kTerminator) - 1;
62 const char kWhitespace[] = " \t";
63 const char kValueSeparator[] = ";";
64 const char kTokenSeparator[] = ";=";
66 // Returns true if |c| occurs in |chars|
67 // TODO(erikwright): maybe make this take an iterator, could check for end also?
68 inline bool CharIsA(const char c, const char* chars) {
69 return strchr(chars, c) != NULL;
71 // Seek the iterator to the first occurrence of a character in |chars|.
72 // Returns true if it hit the end, false otherwise.
73 inline bool SeekTo(std::string::const_iterator* it,
74 const std::string::const_iterator& end,
75 const char* chars) {
76 for (; *it != end && !CharIsA(**it, chars); ++(*it)) {}
77 return *it == end;
79 // Seek the iterator to the first occurrence of a character not in |chars|.
80 // Returns true if it hit the end, false otherwise.
81 inline bool SeekPast(std::string::const_iterator* it,
82 const std::string::const_iterator& end,
83 const char* chars) {
84 for (; *it != end && CharIsA(**it, chars); ++(*it)) {}
85 return *it == end;
87 inline bool SeekBackPast(std::string::const_iterator* it,
88 const std::string::const_iterator& end,
89 const char* chars) {
90 for (; *it != end && CharIsA(**it, chars); --(*it)) {}
91 return *it == end;
94 // Validate whether |value| is a valid token according to [RFC2616],
95 // Section 2.2.
96 bool IsValidToken(const std::string& value) {
97 if (value.empty())
98 return false;
100 // Check that |value| has no separators.
101 std::string separators = "()<>@,;:\\\"/[]?={} \t";
102 if (value.find_first_of(separators) != std::string::npos)
103 return false;
105 // Check that |value| has no CTLs.
106 for (std::string::const_iterator i = value.begin(); i != value.end(); ++i) {
107 if ((*i >= 0 && *i <= 31) || *i >= 127)
108 return false;
111 return true;
114 // Validate value, which may be according to RFC 6265
115 // cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
116 // cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
117 // ; US-ASCII characters excluding CTLs,
118 // ; whitespace DQUOTE, comma, semicolon,
119 // ; and backslash
120 bool IsValidCookieValue(const std::string& value) {
121 // Number of characters to skip in validation at beginning and end of string.
122 size_t skip = 0;
123 if (value.size() >= 2 && *value.begin() == '"' && *(value.end()-1) == '"')
124 skip = 1;
125 for (std::string::const_iterator i = value.begin() + skip;
126 i != value.end() - skip; ++i) {
127 bool valid_octet =
128 (*i == 0x21 ||
129 (*i >= 0x23 && *i <= 0x2B) ||
130 (*i >= 0x2D && *i <= 0x3A) ||
131 (*i >= 0x3C && *i <= 0x5B) ||
132 (*i >= 0x5D && *i <= 0x7E));
133 if (!valid_octet)
134 return false;
136 return true;
139 bool IsControlCharacter(unsigned char c) {
140 return (c >= 0) && (c <= 31);
143 bool IsValidCookieAttributeValue(const std::string& value) {
144 // The greatest common denominator of cookie attribute values is
145 // <any CHAR except CTLs or ";"> according to RFC 6265.
146 for (std::string::const_iterator i = value.begin(); i != value.end(); ++i) {
147 if (IsControlCharacter(*i) || *i == ';')
148 return false;
150 return true;
153 } // namespace
155 namespace net {
157 ParsedCookie::ParsedCookie(const std::string& cookie_line)
158 : path_index_(0),
159 domain_index_(0),
160 expires_index_(0),
161 maxage_index_(0),
162 secure_index_(0),
163 httponly_index_(0),
164 priority_index_(0) {
166 if (cookie_line.size() > kMaxCookieSize) {
167 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size();
168 return;
171 ParseTokenValuePairs(cookie_line);
172 if (!pairs_.empty())
173 SetupAttributes();
176 ParsedCookie::~ParsedCookie() {
179 bool ParsedCookie::IsValid() const {
180 return !pairs_.empty();
183 CookiePriority ParsedCookie::Priority() const {
184 return (priority_index_ == 0) ? COOKIE_PRIORITY_DEFAULT :
185 StringToCookiePriority(pairs_[priority_index_].second);
188 bool ParsedCookie::SetName(const std::string& name) {
189 if (!IsValidToken(name))
190 return false;
191 if (pairs_.empty())
192 pairs_.push_back(std::make_pair("", ""));
193 pairs_[0].first = name;
194 return true;
197 bool ParsedCookie::SetValue(const std::string& value) {
198 if (!IsValidCookieValue(value))
199 return false;
200 if (pairs_.empty())
201 pairs_.push_back(std::make_pair("", ""));
202 pairs_[0].second = value;
203 return true;
206 bool ParsedCookie::SetPath(const std::string& path) {
207 return SetString(&path_index_, kPathTokenName, path);
210 bool ParsedCookie::SetDomain(const std::string& domain) {
211 return SetString(&domain_index_, kDomainTokenName, domain);
214 bool ParsedCookie::SetExpires(const std::string& expires) {
215 return SetString(&expires_index_, kExpiresTokenName, expires);
218 bool ParsedCookie::SetMaxAge(const std::string& maxage) {
219 return SetString(&maxage_index_, kMaxAgeTokenName, maxage);
222 bool ParsedCookie::SetIsSecure(bool is_secure) {
223 return SetBool(&secure_index_, kSecureTokenName, is_secure);
226 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) {
227 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only);
230 bool ParsedCookie::SetPriority(const std::string& priority) {
231 return SetString(&priority_index_, kPriorityTokenName, priority);
234 std::string ParsedCookie::ToCookieLine() const {
235 std::string out;
236 for (PairList::const_iterator it = pairs_.begin();
237 it != pairs_.end(); ++it) {
238 if (!out.empty())
239 out.append("; ");
240 out.append(it->first);
241 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName) {
242 out.append("=");
243 out.append(it->second);
246 return out;
249 std::string::const_iterator ParsedCookie::FindFirstTerminator(
250 const std::string& s) {
251 std::string::const_iterator end = s.end();
252 size_t term_pos =
253 s.find_first_of(std::string(kTerminator, kTerminatorLen));
254 if (term_pos != std::string::npos) {
255 // We found a character we should treat as an end of string.
256 end = s.begin() + term_pos;
258 return end;
261 bool ParsedCookie::ParseToken(std::string::const_iterator* it,
262 const std::string::const_iterator& end,
263 std::string::const_iterator* token_start,
264 std::string::const_iterator* token_end) {
265 DCHECK(it && token_start && token_end);
266 std::string::const_iterator token_real_end;
268 // Seek past any whitespace before the "token" (the name).
269 // token_start should point at the first character in the token
270 if (SeekPast(it, end, kWhitespace))
271 return false; // No token, whitespace or empty.
272 *token_start = *it;
274 // Seek over the token, to the token separator.
275 // token_real_end should point at the token separator, i.e. '='.
276 // If it == end after the seek, we probably have a token-value.
277 SeekTo(it, end, kTokenSeparator);
278 token_real_end = *it;
280 // Ignore any whitespace between the token and the token separator.
281 // token_end should point after the last interesting token character,
282 // pointing at either whitespace, or at '=' (and equal to token_real_end).
283 if (*it != *token_start) { // We could have an empty token name.
284 --(*it); // Go back before the token separator.
285 // Skip over any whitespace to the first non-whitespace character.
286 SeekBackPast(it, *token_start, kWhitespace);
287 // Point after it.
288 ++(*it);
290 *token_end = *it;
292 // Seek us back to the end of the token.
293 *it = token_real_end;
294 return true;
297 void ParsedCookie::ParseValue(std::string::const_iterator* it,
298 const std::string::const_iterator& end,
299 std::string::const_iterator* value_start,
300 std::string::const_iterator* value_end) {
301 DCHECK(it && value_start && value_end);
303 // Seek past any whitespace that might in-between the token and value.
304 SeekPast(it, end, kWhitespace);
305 // value_start should point at the first character of the value.
306 *value_start = *it;
308 // Just look for ';' to terminate ('=' allowed).
309 // We can hit the end, maybe they didn't terminate.
310 SeekTo(it, end, kValueSeparator);
312 // Will be pointed at the ; seperator or the end.
313 *value_end = *it;
315 // Ignore any unwanted whitespace after the value.
316 if (*value_end != *value_start) { // Could have an empty value
317 --(*value_end);
318 SeekBackPast(value_end, *value_start, kWhitespace);
319 ++(*value_end);
323 std::string ParsedCookie::ParseTokenString(const std::string& token) {
324 std::string::const_iterator it = token.begin();
325 std::string::const_iterator end = FindFirstTerminator(token);
327 std::string::const_iterator token_start, token_end;
328 if (ParseToken(&it, end, &token_start, &token_end))
329 return std::string(token_start, token_end);
330 return std::string();
333 std::string ParsedCookie::ParseValueString(const std::string& value) {
334 std::string::const_iterator it = value.begin();
335 std::string::const_iterator end = FindFirstTerminator(value);
337 std::string::const_iterator value_start, value_end;
338 ParseValue(&it, end, &value_start, &value_end);
339 return std::string(value_start, value_end);
342 // Parse all token/value pairs and populate pairs_.
343 void ParsedCookie::ParseTokenValuePairs(const std::string& cookie_line) {
344 pairs_.clear();
346 // Ok, here we go. We should be expecting to be starting somewhere
347 // before the cookie line, not including any header name...
348 std::string::const_iterator start = cookie_line.begin();
349 std::string::const_iterator it = start;
351 // TODO(erikwright): Make sure we're stripping \r\n in the network code.
352 // Then we can log any unexpected terminators.
353 std::string::const_iterator end = FindFirstTerminator(cookie_line);
355 for (int pair_num = 0; pair_num < kMaxPairs && it != end; ++pair_num) {
356 TokenValuePair pair;
358 std::string::const_iterator token_start, token_end;
359 if (!ParseToken(&it, end, &token_start, &token_end))
360 break;
362 if (it == end || *it != '=') {
363 // We have a token-value, we didn't have any token name.
364 if (pair_num == 0) {
365 // For the first time around, we want to treat single values
366 // as a value with an empty name. (Mozilla bug 169091).
367 // IE seems to also have this behavior, ex "AAA", and "AAA=10" will
368 // set 2 different cookies, and setting "BBB" will then replace "AAA".
369 pair.first = "";
370 // Rewind to the beginning of what we thought was the token name,
371 // and let it get parsed as a value.
372 it = token_start;
373 } else {
374 // Any not-first attribute we want to treat a value as a
375 // name with an empty value... This is so something like
376 // "secure;" will get parsed as a Token name, and not a value.
377 pair.first = std::string(token_start, token_end);
379 } else {
380 // We have a TOKEN=VALUE.
381 pair.first = std::string(token_start, token_end);
382 ++it; // Skip past the '='.
385 // OK, now try to parse a value.
386 std::string::const_iterator value_start, value_end;
387 ParseValue(&it, end, &value_start, &value_end);
389 // OK, we're finished with a Token/Value.
390 pair.second = std::string(value_start, value_end);
392 // From RFC2109: "Attributes (names) (attr) are case-insensitive."
393 if (pair_num != 0)
394 StringToLowerASCII(&pair.first);
395 // Ignore Set-Cookie directives contaning control characters. See
396 // http://crbug.com/238041.
397 if (!IsValidCookieAttributeValue(pair.first) ||
398 !IsValidCookieAttributeValue(pair.second)) {
399 pairs_.clear();
400 break;
403 pairs_.push_back(pair);
405 // We've processed a token/value pair, we're either at the end of
406 // the string or a ValueSeparator like ';', which we want to skip.
407 if (it != end)
408 ++it;
412 void ParsedCookie::SetupAttributes() {
413 // We skip over the first token/value, the user supplied one.
414 for (size_t i = 1; i < pairs_.size(); ++i) {
415 if (pairs_[i].first == kPathTokenName) {
416 path_index_ = i;
417 } else if (pairs_[i].first == kDomainTokenName) {
418 domain_index_ = i;
419 } else if (pairs_[i].first == kExpiresTokenName) {
420 expires_index_ = i;
421 } else if (pairs_[i].first == kMaxAgeTokenName) {
422 maxage_index_ = i;
423 } else if (pairs_[i].first == kSecureTokenName) {
424 secure_index_ = i;
425 } else if (pairs_[i].first == kHttpOnlyTokenName) {
426 httponly_index_ = i;
427 } else if (pairs_[i].first == kPriorityTokenName) {
428 priority_index_ = i;
429 } else {
430 /* some attribute we don't know or don't care about. */
435 bool ParsedCookie::SetString(size_t* index,
436 const std::string& key,
437 const std::string& value) {
438 if (value.empty()) {
439 ClearAttributePair(*index);
440 return true;
441 } else {
442 return SetAttributePair(index, key, value);
446 bool ParsedCookie::SetBool(size_t* index,
447 const std::string& key,
448 bool value) {
449 if (!value) {
450 ClearAttributePair(*index);
451 return true;
452 } else {
453 return SetAttributePair(index, key, std::string());
457 bool ParsedCookie::SetAttributePair(size_t* index,
458 const std::string& key,
459 const std::string& value) {
460 if (!(IsValidToken(key) && IsValidCookieAttributeValue(value)))
461 return false;
462 if (!IsValid())
463 return false;
464 if (*index) {
465 pairs_[*index].second = value;
466 } else {
467 pairs_.push_back(std::make_pair(key, value));
468 *index = pairs_.size() - 1;
470 return true;
473 void ParsedCookie::ClearAttributePair(size_t index) {
474 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared.
475 // Cookie attributes that don't have a value at the moment, are represented
476 // with an index being equal to 0.
477 if (index == 0)
478 return;
480 size_t* indexes[] = { &path_index_, &domain_index_, &expires_index_,
481 &maxage_index_, &secure_index_, &httponly_index_,
482 &priority_index_ };
483 for (size_t i = 0; i < arraysize(indexes); ++i) {
484 if (*indexes[i] == index)
485 *indexes[i] = 0;
486 else if (*indexes[i] > index)
487 --*indexes[i];
489 pairs_.erase(pairs_.begin() + index);
492 } // namespace