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

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 more 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>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
14 #include "base/strings/string_tokenizer.h" 15 #include "base/strings/string_tokenizer.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "net/base/net_export.h" 17 #include "net/base/net_export.h"
17 #include "net/http/http_byte_range.h" 18 #include "net/http/http_byte_range.h"
18 #include "net/http/http_version.h" 19 #include "net/http/http_version.h"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 21
21 // This is a macro to support extending this string literal at compile time. 22 // This is a macro to support extending this string literal at compile time.
22 // Please excuse me polluting your global namespace! 23 // Please excuse me polluting your global namespace!
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 // Whether the character is the start of a quotation mark. 117 // Whether the character is the start of a quotation mark.
117 static bool IsQuote(char c); 118 static bool IsQuote(char c);
118 119
119 // Whether the string is a valid |token| as defined in RFC 2616 Sec 2.2. 120 // Whether the string is a valid |token| as defined in RFC 2616 Sec 2.2.
120 static bool IsToken(std::string::const_iterator begin, 121 static bool IsToken(std::string::const_iterator begin,
121 std::string::const_iterator end); 122 std::string::const_iterator end);
122 static bool IsToken(const std::string& str) { 123 static bool IsToken(const std::string& str) {
123 return IsToken(str.begin(), str.end()); 124 return IsToken(str.begin(), str.end());
124 } 125 }
125 126
127 // Whether the string is a valid |parmname| as defined in RFC 5987 Sec 3.2.1.
128 static bool IsParmName(std::string::const_iterator begin,
129 std::string::const_iterator end);
130 static bool IsParmName(const std::string& str) {
131 return IsParmName(str.begin(), str.end());
132 }
133
126 // RFC 2616 Sec 2.2: 134 // RFC 2616 Sec 2.2:
127 // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) 135 // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
128 // Unquote() strips the surrounding quotemarks off a string, and unescapes 136 // Unquote() strips the surrounding quotemarks off a string, and unescapes
129 // any quoted-pair to obtain the value contained by the quoted-string. 137 // 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. 138 // If the input is not quoted, then it works like the identity function.
131 static std::string Unquote(std::string::const_iterator begin, 139 static std::string Unquote(std::string::const_iterator begin,
132 std::string::const_iterator end); 140 std::string::const_iterator end);
133 141
134 // Same as above. 142 // Same as above.
135 static std::string Unquote(const std::string& str); 143 static std::string Unquote(const std::string& str);
136 144
145 // Similar to Unquote(), but additionally validates that the string being
146 // unescaped actually is a valid quoted string. Returns false for an empty
147 // string, a string without quotes, a string with mismatched quotes, and
148 // a string with unescaped embeded quotes.
149 // Contrary to RFC 2616 this method does allow single quotes to enclose the
150 // string.
151 static bool StrictUnquote(std::string::const_iterator begin,
152 std::string::const_iterator end,
153 std::string* out) WARN_UNUSED_RESULT;
154
155 // Same as above.
156 static bool StrictUnquote(const std::string& str,
157 std::string* out) WARN_UNUSED_RESULT;
158
137 // The reverse of Unquote() -- escapes and surrounds with " 159 // The reverse of Unquote() -- escapes and surrounds with "
138 static std::string Quote(const std::string& str); 160 static std::string Quote(const std::string& str);
139 161
140 // Returns the start of the status line, or -1 if no status line was found. 162 // 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 163 // This allows for 4 bytes of junk to precede the status line (which is what
142 // mozilla does too). 164 // mozilla does too).
143 static int LocateStartOfStatusLine(const char* buf, int buf_len); 165 static int LocateStartOfStatusLine(const char* buf, int buf_len);
144 166
145 // Returns index beyond the end-of-headers marker or -1 if not found. RFC 167 // 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 168 // 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 347
326 // Iterates over a delimited sequence of name-value pairs in an HTTP header. 348 // 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 349 // 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 350 // token or quoted-string (the value). Arbitrary HTTP LWS is permitted outside
329 // of and between names, values, and delimiters. 351 // of and between names, values, and delimiters.
330 // 352 //
331 // String iterators returned from this class' methods may be invalidated upon 353 // String iterators returned from this class' methods may be invalidated upon
332 // calls to GetNext() or after the NameValuePairsIterator is destroyed. 354 // calls to GetNext() or after the NameValuePairsIterator is destroyed.
333 class NET_EXPORT NameValuePairsIterator { 355 class NET_EXPORT NameValuePairsIterator {
334 public: 356 public:
335 // Whether or not values are optional. VALUES_OPTIONAL allows 357 // Whether or not values are optional. Values::OPTIONAL allows
336 // e.g. name1=value1;name2;name3=value3, whereas VALUES_NOT_OPTIONAL 358 // 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 359 // will treat it as a parse error because name2 does not have a
338 // corresponding equals sign. 360 // corresponding equals sign.
339 enum OptionalValues { VALUES_OPTIONAL, VALUES_NOT_OPTIONAL }; 361 enum class Values { OPTIONAL, NOT_OPTIONAL };
362
363 // Whether or not unmatched quotes should be considered a failure. By
364 // default this class is pretty lenient and does a best effort to parse
365 // values with mismatched quotes. When set to STRICT a value with
366 // mismatched or otherwise invalid quotes is considered a parse error.
367 enum class Quotes { STRICT, NOT_STRICT };
340 368
341 NameValuePairsIterator(std::string::const_iterator begin, 369 NameValuePairsIterator(std::string::const_iterator begin,
342 std::string::const_iterator end, 370 std::string::const_iterator end,
343 char delimiter, 371 char delimiter,
344 OptionalValues optional_values); 372 Values optional_values,
373 Quotes strict_quotes);
345 374
346 // Treats values as not optional by default (VALUES_NOT_OPTIONAL). 375 // Treats values as not optional by default (Values::NOT_OPTIONAL) and
376 // treats quotes as not strict.
347 NameValuePairsIterator(std::string::const_iterator begin, 377 NameValuePairsIterator(std::string::const_iterator begin,
348 std::string::const_iterator end, 378 std::string::const_iterator end,
349 char delimiter); 379 char delimiter);
350 380
351 NameValuePairsIterator(const NameValuePairsIterator& other); 381 NameValuePairsIterator(const NameValuePairsIterator& other);
352 382
353 ~NameValuePairsIterator(); 383 ~NameValuePairsIterator();
354 384
355 // Advances the iterator to the next pair, if any. Returns true if there 385 // 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 386 // 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 426 // Do not store iterators into this string. The NameValuePairsIterator
397 // is copyable/assignable, and if copied the copy's iterators would point 427 // is copyable/assignable, and if copied the copy's iterators would point
398 // into the original's unquoted_value_ member. 428 // into the original's unquoted_value_ member.
399 std::string unquoted_value_; 429 std::string unquoted_value_;
400 430
401 bool value_is_quoted_; 431 bool value_is_quoted_;
402 432
403 // True if values are required for each name/value pair; false if a 433 // True if values are required for each name/value pair; false if a
404 // name is permitted to appear without a corresponding value. 434 // name is permitted to appear without a corresponding value.
405 bool values_optional_; 435 bool values_optional_;
436
437 // True if quotes values are required to be properly quoted; false if
438 // mismatched quotes and other problems with quoted values should be more
439 // or less gracefully treated as valid.
440 bool strict_quotes_;
406 }; 441 };
407 }; 442 };
408 443
409 } // namespace net 444 } // namespace net
410 445
411 #endif // NET_HTTP_HTTP_UTIL_H_ 446 #endif // NET_HTTP_HTTP_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698