Chromium Code Reviews| 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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/strings/string_piece.h" | |
| 15 #include "base/strings/string_tokenizer.h" | 16 #include "base/strings/string_tokenizer.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
| 18 #include "net/http/http_byte_range.h" | 19 #include "net/http/http_byte_range.h" |
| 19 #include "net/http/http_version.h" | 20 #include "net/http/http_version.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 // This is a macro to support extending this string literal at compile time. | 23 // This is a macro to support extending this string literal at compile time. |
| 23 // Please excuse me polluting your global namespace! | 24 // Please excuse me polluting your global namespace! |
| 24 #define HTTP_LWS " \t" | 25 #define HTTP_LWS " \t" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 // Scans the '\r\n'-delimited headers for the given header name. Returns | 71 // Scans the '\r\n'-delimited headers for the given header name. Returns |
| 71 // true if a match is found. Input is assumed to be well-formed. | 72 // true if a match is found. Input is assumed to be well-formed. |
| 72 // TODO(darin): kill this | 73 // TODO(darin): kill this |
| 73 static bool HasHeader(const std::string& headers, const char* name); | 74 static bool HasHeader(const std::string& headers, const char* name); |
| 74 | 75 |
| 75 // Returns true if it is safe to allow users and scripts to specify the header | 76 // Returns true if it is safe to allow users and scripts to specify the header |
| 76 // named |name|. | 77 // named |name|. |
| 77 static bool IsSafeHeader(const std::string& name); | 78 static bool IsSafeHeader(const std::string& name); |
| 78 | 79 |
| 79 // Returns true if |name| is a valid HTTP header name. | 80 // Returns true if |name| is a valid HTTP header name. |
| 80 static bool IsValidHeaderName(const std::string& name); | 81 static bool IsValidHeaderName(const base::StringPiece& name); |
| 81 | 82 |
| 82 // Returns false if |value| contains NUL or CRLF. This method does not perform | 83 // Returns false if |value| contains NUL or CRLF. This method does not perform |
| 83 // a fully RFC-2616-compliant header value validation. | 84 // a fully RFC-2616-compliant header value validation. |
| 84 static bool IsValidHeaderValue(const std::string& value); | 85 static bool IsValidHeaderValue(const base::StringPiece& value); |
| 85 | 86 |
| 86 // Strips all header lines from |headers| whose name matches | 87 // Strips all header lines from |headers| whose name matches |
| 87 // |headers_to_remove|. |headers_to_remove| is a list of null-terminated | 88 // |headers_to_remove|. |headers_to_remove| is a list of null-terminated |
| 88 // lower-case header names, with array length |headers_to_remove_len|. | 89 // lower-case header names, with array length |headers_to_remove_len|. |
| 89 // Returns the stripped header lines list, separated by "\r\n". | 90 // Returns the stripped header lines list, separated by "\r\n". |
| 90 static std::string StripHeaders(const std::string& headers, | 91 static std::string StripHeaders(const std::string& headers, |
| 91 const char* const headers_to_remove[], | 92 const char* const headers_to_remove[], |
| 92 size_t headers_to_remove_len); | 93 size_t headers_to_remove_len); |
| 93 | 94 |
| 94 // Multiple occurances of some headers cannot be coalesced into a comma- | 95 // Multiple occurances of some headers cannot be coalesced into a comma- |
| 95 // separated list since their values are (or contain) unquoted HTTP-date | 96 // separated list since their values are (or contain) unquoted HTTP-date |
| 96 // values, which may contain a comma (see RFC 2616 section 3.3.1). | 97 // values, which may contain a comma (see RFC 2616 section 3.3.1). |
| 97 static bool IsNonCoalescingHeader(std::string::const_iterator name_begin, | 98 static bool IsNonCoalescingHeader(std::string::const_iterator name_begin, |
| 98 std::string::const_iterator name_end); | 99 std::string::const_iterator name_end); |
| 99 static bool IsNonCoalescingHeader(const std::string& name) { | 100 static bool IsNonCoalescingHeader(const std::string& name) { |
| 100 return IsNonCoalescingHeader(name.begin(), name.end()); | 101 return IsNonCoalescingHeader(name.begin(), name.end()); |
| 101 } | 102 } |
| 102 | 103 |
| 103 // Return true if the character is HTTP "linear white space" (SP | HT). | 104 // Return true if the character is HTTP "linear white space" (SP | HT). |
| 104 // This definition corresponds with the HTTP_LWS macro, and does not match | 105 // This definition corresponds with the HTTP_LWS macro, and does not match |
| 105 // newlines. | 106 // newlines. |
| 106 static bool IsLWS(char c); | 107 static bool IsLWS(char c); |
| 107 | 108 |
| 108 // Trim HTTP_LWS chars from the beginning and end of the string. | 109 // Trim HTTP_LWS chars from the beginning and end of the string. |
| 109 static void TrimLWS(std::string::const_iterator* begin, | 110 static void TrimLWS(std::string::const_iterator* begin, |
| 110 std::string::const_iterator* end); | 111 std::string::const_iterator* end); |
| 112 static base::StringPiece TrimLWS(const base::StringPiece& string); | |
| 111 | 113 |
| 112 // Whether the character is the start of a quotation mark. | 114 // Whether the character is the start of a quotation mark. |
| 113 static bool IsQuote(char c); | 115 static bool IsQuote(char c); |
| 114 | 116 |
| 115 // Whether the string is a valid |token| as defined in RFC 2616 Sec 2.2. | 117 // Whether the string is a valid |token| as defined in RFC 2616 Sec 2.2. |
| 116 static bool IsToken(std::string::const_iterator begin, | 118 static bool IsToken(std::string::const_iterator begin, |
| 117 std::string::const_iterator end); | 119 std::string::const_iterator end) { |
| 118 static bool IsToken(const std::string& str) { | 120 return IsToken(base::StringPiece(&*begin, end - begin)); |
|
mmenke
2016/08/09 16:54:47
Is this guaranteed to work?
Adam Rice
2016/08/10 03:19:07
Interesting question. I had to go look at the C++
mmenke
2016/08/10 03:30:55
And std::string::const_iterators are required to r
Adam Rice
2016/08/10 05:22:58
To be fair, it is widely used: https://cs.chromium
| |
| 119 return IsToken(str.begin(), str.end()); | |
| 120 } | 121 } |
| 122 static bool IsToken(const base::StringPiece& str); | |
| 121 | 123 |
| 122 // Whether the string is a valid |parmname| as defined in RFC 5987 Sec 3.2.1. | 124 // Whether the string is a valid |parmname| as defined in RFC 5987 Sec 3.2.1. |
| 123 static bool IsParmName(std::string::const_iterator begin, | 125 static bool IsParmName(std::string::const_iterator begin, |
| 124 std::string::const_iterator end); | 126 std::string::const_iterator end); |
| 125 static bool IsParmName(const std::string& str) { | 127 static bool IsParmName(const std::string& str) { |
| 126 return IsParmName(str.begin(), str.end()); | 128 return IsParmName(str.begin(), str.end()); |
| 127 } | 129 } |
| 128 | 130 |
| 129 // RFC 2616 Sec 2.2: | 131 // RFC 2616 Sec 2.2: |
| 130 // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) | 132 // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 // True if quotes values are required to be properly quoted; false if | 442 // True if quotes values are required to be properly quoted; false if |
| 441 // mismatched quotes and other problems with quoted values should be more | 443 // mismatched quotes and other problems with quoted values should be more |
| 442 // or less gracefully treated as valid. | 444 // or less gracefully treated as valid. |
| 443 bool strict_quotes_; | 445 bool strict_quotes_; |
| 444 }; | 446 }; |
| 445 }; | 447 }; |
| 446 | 448 |
| 447 } // namespace net | 449 } // namespace net |
| 448 | 450 |
| 449 #endif // NET_HTTP_HTTP_UTIL_H_ | 451 #endif // NET_HTTP_HTTP_UTIL_H_ |
| OLD | NEW |