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 #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 Loading... |
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 |
| 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); |
| 144 |
| 145 // Same as above. |
| 146 static bool StrictUnquote(const std::string& str, std::string* out); |
| 147 |
137 // The reverse of Unquote() -- escapes and surrounds with " | 148 // The reverse of Unquote() -- escapes and surrounds with " |
138 static std::string Quote(const std::string& str); | 149 static std::string Quote(const std::string& str); |
139 | 150 |
140 // Returns the start of the status line, or -1 if no status line was found. | 151 // 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 | 152 // This allows for 4 bytes of junk to precede the status line (which is what |
142 // mozilla does too). | 153 // mozilla does too). |
143 static int LocateStartOfStatusLine(const char* buf, int buf_len); | 154 static int LocateStartOfStatusLine(const char* buf, int buf_len); |
144 | 155 |
145 // Returns index beyond the end-of-headers marker or -1 if not found. RFC | 156 // 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 | 157 // 2616 defines the end-of-headers marker as a double CRLF; however, some |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 // String iterators returned from this class' methods may be invalidated upon | 342 // String iterators returned from this class' methods may be invalidated upon |
332 // calls to GetNext() or after the NameValuePairsIterator is destroyed. | 343 // calls to GetNext() or after the NameValuePairsIterator is destroyed. |
333 class NET_EXPORT NameValuePairsIterator { | 344 class NET_EXPORT NameValuePairsIterator { |
334 public: | 345 public: |
335 // Whether or not values are optional. VALUES_OPTIONAL allows | 346 // Whether or not values are optional. VALUES_OPTIONAL allows |
336 // e.g. name1=value1;name2;name3=value3, whereas VALUES_NOT_OPTIONAL | 347 // e.g. name1=value1;name2;name3=value3, whereas VALUES_NOT_OPTIONAL |
337 // will treat it as a parse error because name2 does not have a | 348 // will treat it as a parse error because name2 does not have a |
338 // corresponding equals sign. | 349 // corresponding equals sign. |
339 enum OptionalValues { VALUES_OPTIONAL, VALUES_NOT_OPTIONAL }; | 350 enum OptionalValues { VALUES_OPTIONAL, VALUES_NOT_OPTIONAL }; |
340 | 351 |
| 352 // Whether or not unmatched quotes should be considered a failure. By |
| 353 // default this class is pretty lenient and does a best effort to parse |
| 354 // values with mismatched quotes. When set to STRICT_QUOTES a value with |
| 355 // mismatched or otherwise invalid quotes is considered a parse error. |
| 356 enum StrictQuotes { STRICT_QUOTES, NON_STRICT_QUOTES }; |
| 357 |
341 NameValuePairsIterator(std::string::const_iterator begin, | 358 NameValuePairsIterator(std::string::const_iterator begin, |
342 std::string::const_iterator end, | 359 std::string::const_iterator end, |
343 char delimiter, | 360 char delimiter, |
344 OptionalValues optional_values); | 361 OptionalValues optional_values, |
| 362 StrictQuotes strict_quotes = NON_STRICT_QUOTES); |
345 | 363 |
346 // Treats values as not optional by default (VALUES_NOT_OPTIONAL). | 364 // Treats values as not optional by default (VALUES_NOT_OPTIONAL). |
347 NameValuePairsIterator(std::string::const_iterator begin, | 365 NameValuePairsIterator(std::string::const_iterator begin, |
348 std::string::const_iterator end, | 366 std::string::const_iterator end, |
349 char delimiter); | 367 char delimiter); |
350 | 368 |
351 NameValuePairsIterator(const NameValuePairsIterator& other); | 369 NameValuePairsIterator(const NameValuePairsIterator& other); |
352 | 370 |
353 ~NameValuePairsIterator(); | 371 ~NameValuePairsIterator(); |
354 | 372 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 // Do not store iterators into this string. The NameValuePairsIterator | 414 // Do not store iterators into this string. The NameValuePairsIterator |
397 // is copyable/assignable, and if copied the copy's iterators would point | 415 // is copyable/assignable, and if copied the copy's iterators would point |
398 // into the original's unquoted_value_ member. | 416 // into the original's unquoted_value_ member. |
399 std::string unquoted_value_; | 417 std::string unquoted_value_; |
400 | 418 |
401 bool value_is_quoted_; | 419 bool value_is_quoted_; |
402 | 420 |
403 // True if values are required for each name/value pair; false if a | 421 // True if values are required for each name/value pair; false if a |
404 // name is permitted to appear without a corresponding value. | 422 // name is permitted to appear without a corresponding value. |
405 bool values_optional_; | 423 bool values_optional_; |
| 424 |
| 425 bool strict_quotes_; |
406 }; | 426 }; |
407 }; | 427 }; |
408 | 428 |
409 } // namespace net | 429 } // namespace net |
410 | 430 |
411 #endif // NET_HTTP_HTTP_UTIL_H_ | 431 #endif // NET_HTTP_HTTP_UTIL_H_ |
OLD | NEW |