Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: net/cookies/parsed_cookie.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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;
130 ++i) {
127 bool valid_octet = 131 bool valid_octet =
128 (*i == 0x21 || 132 (*i == 0x21 || (*i >= 0x23 && *i <= 0x2B) ||
129 (*i >= 0x23 && *i <= 0x2B) || 133 (*i >= 0x2D && *i <= 0x3A) || (*i >= 0x3C && *i <= 0x5B) ||
130 (*i >= 0x2D && *i <= 0x3A) ||
131 (*i >= 0x3C && *i <= 0x5B) ||
132 (*i >= 0x5D && *i <= 0x7E)); 134 (*i >= 0x5D && *i <= 0x7E));
133 if (!valid_octet) 135 if (!valid_octet)
134 return false; 136 return false;
135 } 137 }
136 return true; 138 return true;
137 } 139 }
138 140
139 bool IsControlCharacter(unsigned char c) { 141 bool IsControlCharacter(unsigned char c) {
140 return (c >= 0) && (c <= 31); 142 return (c >= 0) && (c <= 31);
141 } 143 }
142 144
143 bool IsValidCookieAttributeValue(const std::string& value) { 145 bool IsValidCookieAttributeValue(const std::string& value) {
144 // The greatest common denominator of cookie attribute values is 146 // The greatest common denominator of cookie attribute values is
145 // <any CHAR except CTLs or ";"> according to RFC 6265. 147 // <any CHAR except CTLs or ";"> according to RFC 6265.
146 for (std::string::const_iterator i = value.begin(); i != value.end(); ++i) { 148 for (std::string::const_iterator i = value.begin(); i != value.end(); ++i) {
147 if (IsControlCharacter(*i) || *i == ';') 149 if (IsControlCharacter(*i) || *i == ';')
148 return false; 150 return false;
149 } 151 }
150 return true; 152 return true;
151 } 153 }
152 154
153 } // namespace 155 } // namespace
154 156
155 namespace net { 157 namespace net {
156 158
157 ParsedCookie::ParsedCookie(const std::string& cookie_line) 159 ParsedCookie::ParsedCookie(const std::string& cookie_line)
158 : path_index_(0), 160 : path_index_(0),
159 domain_index_(0), 161 domain_index_(0),
160 expires_index_(0), 162 expires_index_(0),
161 maxage_index_(0), 163 maxage_index_(0),
162 secure_index_(0), 164 secure_index_(0),
163 httponly_index_(0), 165 httponly_index_(0),
164 priority_index_(0) { 166 priority_index_(0) {
165
166 if (cookie_line.size() > kMaxCookieSize) { 167 if (cookie_line.size() > kMaxCookieSize) {
167 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size(); 168 VLOG(1) << "Not parsing cookie, too large: " << cookie_line.size();
168 return; 169 return;
169 } 170 }
170 171
171 ParseTokenValuePairs(cookie_line); 172 ParseTokenValuePairs(cookie_line);
172 if (!pairs_.empty()) 173 if (!pairs_.empty())
173 SetupAttributes(); 174 SetupAttributes();
174 } 175 }
175 176
176 ParsedCookie::~ParsedCookie() { 177 ParsedCookie::~ParsedCookie() {
177 } 178 }
178 179
179 bool ParsedCookie::IsValid() const { 180 bool ParsedCookie::IsValid() const {
180 return !pairs_.empty(); 181 return !pairs_.empty();
181 } 182 }
182 183
183 CookiePriority ParsedCookie::Priority() const { 184 CookiePriority ParsedCookie::Priority() const {
184 return (priority_index_ == 0) ? COOKIE_PRIORITY_DEFAULT : 185 return (priority_index_ == 0)
185 StringToCookiePriority(pairs_[priority_index_].second); 186 ? COOKIE_PRIORITY_DEFAULT
187 : StringToCookiePriority(pairs_[priority_index_].second);
186 } 188 }
187 189
188 bool ParsedCookie::SetName(const std::string& name) { 190 bool ParsedCookie::SetName(const std::string& name) {
189 if (!IsValidToken(name)) 191 if (!IsValidToken(name))
190 return false; 192 return false;
191 if (pairs_.empty()) 193 if (pairs_.empty())
192 pairs_.push_back(std::make_pair("", "")); 194 pairs_.push_back(std::make_pair("", ""));
193 pairs_[0].first = name; 195 pairs_[0].first = name;
194 return true; 196 return true;
195 } 197 }
(...skipping 30 matching lines...) Expand all
226 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) { 228 bool ParsedCookie::SetIsHttpOnly(bool is_http_only) {
227 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only); 229 return SetBool(&httponly_index_, kHttpOnlyTokenName, is_http_only);
228 } 230 }
229 231
230 bool ParsedCookie::SetPriority(const std::string& priority) { 232 bool ParsedCookie::SetPriority(const std::string& priority) {
231 return SetString(&priority_index_, kPriorityTokenName, priority); 233 return SetString(&priority_index_, kPriorityTokenName, priority);
232 } 234 }
233 235
234 std::string ParsedCookie::ToCookieLine() const { 236 std::string ParsedCookie::ToCookieLine() const {
235 std::string out; 237 std::string out;
236 for (PairList::const_iterator it = pairs_.begin(); 238 for (PairList::const_iterator it = pairs_.begin(); it != pairs_.end(); ++it) {
237 it != pairs_.end(); ++it) {
238 if (!out.empty()) 239 if (!out.empty())
239 out.append("; "); 240 out.append("; ");
240 out.append(it->first); 241 out.append(it->first);
241 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName) { 242 if (it->first != kSecureTokenName && it->first != kHttpOnlyTokenName) {
242 out.append("="); 243 out.append("=");
243 out.append(it->second); 244 out.append(it->second);
244 } 245 }
245 } 246 }
246 return out; 247 return out;
247 } 248 }
248 249
249 std::string::const_iterator ParsedCookie::FindFirstTerminator( 250 std::string::const_iterator ParsedCookie::FindFirstTerminator(
250 const std::string& s) { 251 const std::string& s) {
251 std::string::const_iterator end = s.end(); 252 std::string::const_iterator end = s.end();
252 size_t term_pos = 253 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) { 254 if (term_pos != std::string::npos) {
255 // We found a character we should treat as an end of string. 255 // We found a character we should treat as an end of string.
256 end = s.begin() + term_pos; 256 end = s.begin() + term_pos;
257 } 257 }
258 return end; 258 return end;
259 } 259 }
260 260
261 bool ParsedCookie::ParseToken(std::string::const_iterator* it, 261 bool ParsedCookie::ParseToken(std::string::const_iterator* it,
262 const std::string::const_iterator& end, 262 const std::string::const_iterator& end,
263 std::string::const_iterator* token_start, 263 std::string::const_iterator* token_start,
(...skipping 10 matching lines...) Expand all
274 // Seek over the token, to the token separator. 274 // Seek over the token, to the token separator.
275 // token_real_end should point at the token separator, i.e. '='. 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. 276 // If it == end after the seek, we probably have a token-value.
277 SeekTo(it, end, kTokenSeparator); 277 SeekTo(it, end, kTokenSeparator);
278 token_real_end = *it; 278 token_real_end = *it;
279 279
280 // Ignore any whitespace between the token and the token separator. 280 // Ignore any whitespace between the token and the token separator.
281 // token_end should point after the last interesting token character, 281 // token_end should point after the last interesting token character,
282 // pointing at either whitespace, or at '=' (and equal to token_real_end). 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. 283 if (*it != *token_start) { // We could have an empty token name.
284 --(*it); // Go back before the token separator. 284 --(*it); // Go back before the token separator.
285 // Skip over any whitespace to the first non-whitespace character. 285 // Skip over any whitespace to the first non-whitespace character.
286 SeekBackPast(it, *token_start, kWhitespace); 286 SeekBackPast(it, *token_start, kWhitespace);
287 // Point after it. 287 // Point after it.
288 ++(*it); 288 ++(*it);
289 } 289 }
290 *token_end = *it; 290 *token_end = *it;
291 291
292 // Seek us back to the end of the token. 292 // Seek us back to the end of the token.
293 *it = token_real_end; 293 *it = token_real_end;
294 return true; 294 return true;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 const std::string& key, 436 const std::string& key,
437 const std::string& value) { 437 const std::string& value) {
438 if (value.empty()) { 438 if (value.empty()) {
439 ClearAttributePair(*index); 439 ClearAttributePair(*index);
440 return true; 440 return true;
441 } else { 441 } else {
442 return SetAttributePair(index, key, value); 442 return SetAttributePair(index, key, value);
443 } 443 }
444 } 444 }
445 445
446 bool ParsedCookie::SetBool(size_t* index, 446 bool ParsedCookie::SetBool(size_t* index, const std::string& key, bool value) {
447 const std::string& key,
448 bool value) {
449 if (!value) { 447 if (!value) {
450 ClearAttributePair(*index); 448 ClearAttributePair(*index);
451 return true; 449 return true;
452 } else { 450 } else {
453 return SetAttributePair(index, key, std::string()); 451 return SetAttributePair(index, key, std::string());
454 } 452 }
455 } 453 }
456 454
457 bool ParsedCookie::SetAttributePair(size_t* index, 455 bool ParsedCookie::SetAttributePair(size_t* index,
458 const std::string& key, 456 const std::string& key,
(...skipping 11 matching lines...) Expand all
470 return true; 468 return true;
471 } 469 }
472 470
473 void ParsedCookie::ClearAttributePair(size_t index) { 471 void ParsedCookie::ClearAttributePair(size_t index) {
474 // The first pair (name/value of cookie at pairs_[0]) cannot be cleared. 472 // 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 473 // Cookie attributes that don't have a value at the moment, are represented
476 // with an index being equal to 0. 474 // with an index being equal to 0.
477 if (index == 0) 475 if (index == 0)
478 return; 476 return;
479 477
480 size_t* indexes[] = { &path_index_, &domain_index_, &expires_index_, 478 size_t* indexes[] = {&path_index_, &domain_index_, &expires_index_,
481 &maxage_index_, &secure_index_, &httponly_index_, 479 &maxage_index_, &secure_index_, &httponly_index_,
482 &priority_index_ }; 480 &priority_index_};
483 for (size_t i = 0; i < arraysize(indexes); ++i) { 481 for (size_t i = 0; i < arraysize(indexes); ++i) {
484 if (*indexes[i] == index) 482 if (*indexes[i] == index)
485 *indexes[i] = 0; 483 *indexes[i] = 0;
486 else if (*indexes[i] > index) 484 else if (*indexes[i] > index)
487 --*indexes[i]; 485 --*indexes[i];
488 } 486 }
489 pairs_.erase(pairs_.begin() + index); 487 pairs_.erase(pairs_.begin() + index);
490 } 488 }
491 489
492 } // namespace 490 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698