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

Side by Side Diff: net/http/http_util.h

Issue 1811163002: Share link header parsing code between blink and content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@base-optional
Patch Set: address mmenke's comments Created 4 years, 8 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
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 #ifndef NET_HTTP_HTTP_UTIL_H_ 5 #ifndef NET_HTTP_HTTP_UTIL_H_
6 #define NET_HTTP_HTTP_UTIL_H_ 6 #define NET_HTTP_HTTP_UTIL_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <string> 10 #include <string>
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) 127 // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
128 // Unquote() strips the surrounding quotemarks off a string, and unescapes 128 // Unquote() strips the surrounding quotemarks off a string, and unescapes
129 // any quoted-pair to obtain the value contained by the quoted-string. 129 // any quoted-pair to obtain the value contained by the quoted-string.
130 // If the input is not quoted, then it works like the identity function. 130 // If the input is not quoted, then it works like the identity function.
131 static std::string Unquote(std::string::const_iterator begin, 131 static std::string Unquote(std::string::const_iterator begin,
132 std::string::const_iterator end); 132 std::string::const_iterator end);
133 133
134 // Same as above. 134 // Same as above.
135 static std::string Unquote(const std::string& str); 135 static std::string Unquote(const std::string& str);
136 136
137 // Similar to Unquote(), but additionally validates that the string being
138 // unescaped actually is a valid quoted string. Returns false for an empty
mmenke 2016/04/26 18:05:58 Think we should mention this still allows single q
Marijn Kruisselbrink 2016/04/27 01:49:49 Done
139 // string, a string without quotes, a string with mismatched quotes, and
140 // a string with unescaped embeded quotes.
141 static bool StrictUnquote(std::string::const_iterator begin,
142 std::string::const_iterator end,
143 std::string* out) WARN_UNUSED_RESULT;
mmenke 2016/04/26 18:05:58 nit: Include "base/macros.h" for WARN_UNUSED_RESU
Marijn Kruisselbrink 2016/04/27 01:49:49 Done
144
145 // Same as above.
146 static bool StrictUnquote(const std::string& str,
147 std::string* out) WARN_UNUSED_RESULT;
148
137 // The reverse of Unquote() -- escapes and surrounds with " 149 // The reverse of Unquote() -- escapes and surrounds with "
138 static std::string Quote(const std::string& str); 150 static std::string Quote(const std::string& str);
139 151
140 // Returns the start of the status line, or -1 if no status line was found. 152 // Returns the start of the status line, or -1 if no status line was found.
141 // This allows for 4 bytes of junk to precede the status line (which is what 153 // This allows for 4 bytes of junk to precede the status line (which is what
142 // mozilla does too). 154 // mozilla does too).
143 static int LocateStartOfStatusLine(const char* buf, int buf_len); 155 static int LocateStartOfStatusLine(const char* buf, int buf_len);
144 156
145 // Returns index beyond the end-of-headers marker or -1 if not found. RFC 157 // Returns index beyond the end-of-headers marker or -1 if not found. RFC
146 // 2616 defines the end-of-headers marker as a double CRLF; however, some 158 // 2616 defines the end-of-headers marker as a double CRLF; however, some
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 337
326 // Iterates over a delimited sequence of name-value pairs in an HTTP header. 338 // Iterates over a delimited sequence of name-value pairs in an HTTP header.
327 // Each pair consists of a token (the name), an equals sign, and either a 339 // Each pair consists of a token (the name), an equals sign, and either a
328 // token or quoted-string (the value). Arbitrary HTTP LWS is permitted outside 340 // token or quoted-string (the value). Arbitrary HTTP LWS is permitted outside
329 // of and between names, values, and delimiters. 341 // of and between names, values, and delimiters.
330 // 342 //
331 // String iterators returned from this class' methods may be invalidated upon 343 // String iterators returned from this class' methods may be invalidated upon
332 // calls to GetNext() or after the NameValuePairsIterator is destroyed. 344 // calls to GetNext() or after the NameValuePairsIterator is destroyed.
333 class NET_EXPORT NameValuePairsIterator { 345 class NET_EXPORT NameValuePairsIterator {
334 public: 346 public:
335 // Whether or not values are optional. VALUES_OPTIONAL allows 347 // Whether or not values are optional. Values::OPTIONAL allows
336 // e.g. name1=value1;name2;name3=value3, whereas VALUES_NOT_OPTIONAL 348 // e.g. name1=value1;name2;name3=value3, whereas Vaues::NOT_OPTIONAL
337 // will treat it as a parse error because name2 does not have a 349 // will treat it as a parse error because name2 does not have a
338 // corresponding equals sign. 350 // corresponding equals sign.
339 enum OptionalValues { VALUES_OPTIONAL, VALUES_NOT_OPTIONAL }; 351 enum class Values { OPTIONAL, NOT_OPTIONAL };
352
353 // Whether or not unmatched quotes should be considered a failure. By
354 // default this class is pretty lenient and does a best effort to parse
355 // values with mismatched quotes. When set to STRICT a value with
356 // mismatched or otherwise invalid quotes is considered a parse error.
357 enum class Quotes { STRICT, NOT_STRICT };
340 358
341 NameValuePairsIterator(std::string::const_iterator begin, 359 NameValuePairsIterator(std::string::const_iterator begin,
342 std::string::const_iterator end, 360 std::string::const_iterator end,
343 char delimiter, 361 char delimiter,
344 OptionalValues optional_values); 362 Values optional_values,
363 Quotes strict_quotes);
345 364
346 // Treats values as not optional by default (VALUES_NOT_OPTIONAL). 365 // Treats values as not optional by default (Values::NOT_OPTIONAL) and
366 // treats quotes as not strict.
347 NameValuePairsIterator(std::string::const_iterator begin, 367 NameValuePairsIterator(std::string::const_iterator begin,
348 std::string::const_iterator end, 368 std::string::const_iterator end,
349 char delimiter); 369 char delimiter);
350 370
351 NameValuePairsIterator(const NameValuePairsIterator& other); 371 NameValuePairsIterator(const NameValuePairsIterator& other);
352 372
353 ~NameValuePairsIterator(); 373 ~NameValuePairsIterator();
354 374
355 // Advances the iterator to the next pair, if any. Returns true if there 375 // Advances the iterator to the next pair, if any. Returns true if there
356 // is a next pair. Use name* and value* methods to access the resultant 376 // is a next pair. Use name* and value* methods to access the resultant
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 // Do not store iterators into this string. The NameValuePairsIterator 416 // Do not store iterators into this string. The NameValuePairsIterator
397 // is copyable/assignable, and if copied the copy's iterators would point 417 // is copyable/assignable, and if copied the copy's iterators would point
398 // into the original's unquoted_value_ member. 418 // into the original's unquoted_value_ member.
399 std::string unquoted_value_; 419 std::string unquoted_value_;
400 420
401 bool value_is_quoted_; 421 bool value_is_quoted_;
402 422
403 // True if values are required for each name/value pair; false if a 423 // True if values are required for each name/value pair; false if a
404 // name is permitted to appear without a corresponding value. 424 // name is permitted to appear without a corresponding value.
405 bool values_optional_; 425 bool values_optional_;
426
427 // True if quotes values are required to be properly quoted; false if
428 // mismatched quotes and other problems with quoted values should be more
429 // or less gracefully treated as valid.
430 bool strict_quotes_;
406 }; 431 };
407 }; 432 };
408 433
409 } // namespace net 434 } // namespace net
410 435
411 #endif // NET_HTTP_HTTP_UTIL_H_ 436 #endif // NET_HTTP_HTTP_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698