| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "platform/loader/fetch/FetchUtils.h" | 5 #include "platform/loader/fetch/FetchUtils.h" |
| 6 | 6 |
| 7 #include "platform/HTTPNames.h" | 7 #include "platform/HTTPNames.h" |
| 8 #include "platform/network/HTTPHeaderMap.h" | 8 #include "platform/network/HTTPHeaderMap.h" |
| 9 #include "platform/network/HTTPParsers.h" | 9 #include "platform/network/HTTPParsers.h" |
| 10 #include "platform/wtf/HashSet.h" | 10 #include "platform/wtf/HashSet.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // "A simple header is a header whose name is either one of `Accept`, | 88 // "A simple header is a header whose name is either one of `Accept`, |
| 89 // `Accept-Language`, and `Content-Language`, or whose name is | 89 // `Accept-Language`, and `Content-Language`, or whose name is |
| 90 // `Content-Type` and value, once parsed, is one of | 90 // `Content-Type` and value, once parsed, is one of |
| 91 // `application/x-www-form-urlencoded`, `multipart/form-data`, and | 91 // `application/x-www-form-urlencoded`, `multipart/form-data`, and |
| 92 // `text/plain`." | 92 // `text/plain`." |
| 93 // Treat 'Save-Data' as a simple header, since it is added by Chrome when | 93 // Treat 'Save-Data' as a simple header, since it is added by Chrome when |
| 94 // Data Saver feature is enabled. | 94 // Data Saver feature is enabled. |
| 95 // Treat inspector headers as a simple headers, since they are added by blink | 95 // Treat inspector headers as a simple headers, since they are added by blink |
| 96 // when the inspector is open. | 96 // when the inspector is open. |
| 97 | 97 |
| 98 if (EqualIgnoringCase(name, "accept") || | 98 if (DeprecatedEqualIgnoringCase(name, "accept") || |
| 99 EqualIgnoringCase(name, "accept-language") || | 99 DeprecatedEqualIgnoringCase(name, "accept-language") || |
| 100 EqualIgnoringCase(name, "content-language") || | 100 DeprecatedEqualIgnoringCase(name, "content-language") || |
| 101 EqualIgnoringCase( | 101 DeprecatedEqualIgnoringCase( |
| 102 name, HTTPNames::X_DevTools_Emulate_Network_Conditions_Client_Id) || | 102 name, HTTPNames::X_DevTools_Emulate_Network_Conditions_Client_Id) || |
| 103 EqualIgnoringCase(name, HTTPNames::X_DevTools_Request_Id) || | 103 DeprecatedEqualIgnoringCase(name, HTTPNames::X_DevTools_Request_Id) || |
| 104 EqualIgnoringCase(name, "save-data")) | 104 DeprecatedEqualIgnoringCase(name, "save-data")) |
| 105 return true; | 105 return true; |
| 106 | 106 |
| 107 if (EqualIgnoringCase(name, "content-type")) | 107 if (DeprecatedEqualIgnoringCase(name, "content-type")) |
| 108 return IsSimpleContentType(value); | 108 return IsSimpleContentType(value); |
| 109 | 109 |
| 110 return false; | 110 return false; |
| 111 } | 111 } |
| 112 | 112 |
| 113 bool FetchUtils::IsSimpleContentType(const AtomicString& media_type) { | 113 bool FetchUtils::IsSimpleContentType(const AtomicString& media_type) { |
| 114 AtomicString mime_type = ExtractMIMETypeFromMediaType(media_type); | 114 AtomicString mime_type = ExtractMIMETypeFromMediaType(media_type); |
| 115 return EqualIgnoringCase(mime_type, "application/x-www-form-urlencoded") || | 115 return DeprecatedEqualIgnoringCase(mime_type, |
| 116 EqualIgnoringCase(mime_type, "multipart/form-data") || | 116 "application/x-www-form-urlencoded") || |
| 117 EqualIgnoringCase(mime_type, "text/plain"); | 117 DeprecatedEqualIgnoringCase(mime_type, "multipart/form-data") || |
| 118 DeprecatedEqualIgnoringCase(mime_type, "text/plain"); |
| 118 } | 119 } |
| 119 | 120 |
| 120 bool FetchUtils::IsSimpleRequest(const String& method, | 121 bool FetchUtils::IsSimpleRequest(const String& method, |
| 121 const HTTPHeaderMap& header_map) { | 122 const HTTPHeaderMap& header_map) { |
| 122 if (!IsSimpleMethod(method)) | 123 if (!IsSimpleMethod(method)) |
| 123 return false; | 124 return false; |
| 124 | 125 |
| 125 for (const auto& header : header_map) { | 126 for (const auto& header : header_map) { |
| 126 // Preflight is required for MIME types that can not be sent via form | 127 // Preflight is required for MIME types that can not be sent via form |
| 127 // submission. | 128 // submission. |
| 128 if (!IsSimpleHeader(header.key, header.value)) | 129 if (!IsSimpleHeader(header.key, header.value)) |
| 129 return false; | 130 return false; |
| 130 } | 131 } |
| 131 | 132 |
| 132 return true; | 133 return true; |
| 133 } | 134 } |
| 134 | 135 |
| 135 bool FetchUtils::IsForbiddenMethod(const String& method) { | 136 bool FetchUtils::IsForbiddenMethod(const String& method) { |
| 136 // http://fetch.spec.whatwg.org/#forbidden-method | 137 // http://fetch.spec.whatwg.org/#forbidden-method |
| 137 // "A forbidden method is a method that is a byte case-insensitive match" | 138 // "A forbidden method is a method that is a byte case-insensitive match" |
| 138 // for one of `CONNECT`, `TRACE`, and `TRACK`." | 139 // for one of `CONNECT`, `TRACE`, and `TRACK`." |
| 139 return EqualIgnoringCase(method, "TRACE") || | 140 return DeprecatedEqualIgnoringCase(method, "TRACE") || |
| 140 EqualIgnoringCase(method, "TRACK") || | 141 DeprecatedEqualIgnoringCase(method, "TRACK") || |
| 141 EqualIgnoringCase(method, "CONNECT"); | 142 DeprecatedEqualIgnoringCase(method, "CONNECT"); |
| 142 } | 143 } |
| 143 | 144 |
| 144 bool FetchUtils::IsForbiddenHeaderName(const String& name) { | 145 bool FetchUtils::IsForbiddenHeaderName(const String& name) { |
| 145 // http://fetch.spec.whatwg.org/#forbidden-header-name | 146 // http://fetch.spec.whatwg.org/#forbidden-header-name |
| 146 // "A forbidden header name is a header names that is one of: | 147 // "A forbidden header name is a header names that is one of: |
| 147 // `Accept-Charset`, `Accept-Encoding`, `Access-Control-Request-Headers`, | 148 // `Accept-Charset`, `Accept-Encoding`, `Access-Control-Request-Headers`, |
| 148 // `Access-Control-Request-Method`, `Connection`, | 149 // `Access-Control-Request-Method`, `Connection`, |
| 149 // `Content-Length, Cookie`, `Cookie2`, `Date`, `DNT`, `Expect`, `Host`, | 150 // `Content-Length, Cookie`, `Cookie2`, `Date`, `DNT`, `Expect`, `Host`, |
| 150 // `Keep-Alive`, `Origin`, `Referer`, `TE`, `Trailer`, | 151 // `Keep-Alive`, `Origin`, `Referer`, `TE`, `Trailer`, |
| 151 // `Transfer-Encoding`, `Upgrade`, `User-Agent`, `Via` | 152 // `Transfer-Encoding`, `Upgrade`, `User-Agent`, `Via` |
| 152 // or starts with `Proxy-` or `Sec-` (including when it is just `Proxy-` or | 153 // or starts with `Proxy-` or `Sec-` (including when it is just `Proxy-` or |
| 153 // `Sec-`)." | 154 // `Sec-`)." |
| 154 | 155 |
| 155 return ForbiddenHeaderNames::Get().Has(name); | 156 return ForbiddenHeaderNames::Get().Has(name); |
| 156 } | 157 } |
| 157 | 158 |
| 158 bool FetchUtils::IsForbiddenResponseHeaderName(const String& name) { | 159 bool FetchUtils::IsForbiddenResponseHeaderName(const String& name) { |
| 159 // http://fetch.spec.whatwg.org/#forbidden-response-header-name | 160 // http://fetch.spec.whatwg.org/#forbidden-response-header-name |
| 160 // "A forbidden response header name is a header name that is one of: | 161 // "A forbidden response header name is a header name that is one of: |
| 161 // `Set-Cookie`, `Set-Cookie2`" | 162 // `Set-Cookie`, `Set-Cookie2`" |
| 162 | 163 |
| 163 return EqualIgnoringCase(name, "set-cookie") || | 164 return DeprecatedEqualIgnoringCase(name, "set-cookie") || |
| 164 EqualIgnoringCase(name, "set-cookie2"); | 165 DeprecatedEqualIgnoringCase(name, "set-cookie2"); |
| 165 } | 166 } |
| 166 | 167 |
| 167 bool FetchUtils::IsSimpleOrForbiddenRequest(const String& method, | 168 bool FetchUtils::IsSimpleOrForbiddenRequest(const String& method, |
| 168 const HTTPHeaderMap& header_map) { | 169 const HTTPHeaderMap& header_map) { |
| 169 if (!IsSimpleMethod(method)) | 170 if (!IsSimpleMethod(method)) |
| 170 return false; | 171 return false; |
| 171 | 172 |
| 172 for (const auto& header : header_map) { | 173 for (const auto& header : header_map) { |
| 173 if (!IsSimpleHeader(header.key, header.value) && | 174 if (!IsSimpleHeader(header.key, header.value) && |
| 174 !IsForbiddenHeaderName(header.key)) | 175 !IsForbiddenHeaderName(header.key)) |
| 175 return false; | 176 return false; |
| 176 } | 177 } |
| 177 | 178 |
| 178 return true; | 179 return true; |
| 179 } | 180 } |
| 180 | 181 |
| 181 AtomicString FetchUtils::NormalizeMethod(const AtomicString& method) { | 182 AtomicString FetchUtils::NormalizeMethod(const AtomicString& method) { |
| 182 // https://fetch.spec.whatwg.org/#concept-method-normalize | 183 // https://fetch.spec.whatwg.org/#concept-method-normalize |
| 183 | 184 |
| 184 // We place GET and POST first because they are more commonly used than | 185 // We place GET and POST first because they are more commonly used than |
| 185 // others. | 186 // others. |
| 186 const char* const kMethods[] = { | 187 const char* const kMethods[] = { |
| 187 "GET", "POST", "DELETE", "HEAD", "OPTIONS", "PUT", | 188 "GET", "POST", "DELETE", "HEAD", "OPTIONS", "PUT", |
| 188 }; | 189 }; |
| 189 | 190 |
| 190 for (const auto& known : kMethods) { | 191 for (const auto& known : kMethods) { |
| 191 if (EqualIgnoringCase(method, known)) { | 192 if (DeprecatedEqualIgnoringCase(method, known)) { |
| 192 // Don't bother allocating a new string if it's already all | 193 // Don't bother allocating a new string if it's already all |
| 193 // uppercase. | 194 // uppercase. |
| 194 return method == known ? method : known; | 195 return method == known ? method : known; |
| 195 } | 196 } |
| 196 } | 197 } |
| 197 return method; | 198 return method; |
| 198 } | 199 } |
| 199 | 200 |
| 200 String FetchUtils::NormalizeHeaderValue(const String& value) { | 201 String FetchUtils::NormalizeHeaderValue(const String& value) { |
| 201 // https://fetch.spec.whatwg.org/#concept-header-value-normalize | 202 // https://fetch.spec.whatwg.org/#concept-header-value-normalize |
| 202 // Strip leading and trailing whitespace from header value. | 203 // Strip leading and trailing whitespace from header value. |
| 203 // HTTP whitespace bytes are 0x09, 0x0A, 0x0D, and 0x20. | 204 // HTTP whitespace bytes are 0x09, 0x0A, 0x0D, and 0x20. |
| 204 | 205 |
| 205 return value.StripWhiteSpace(IsHTTPWhitespace); | 206 return value.StripWhiteSpace(IsHTTPWhitespace); |
| 206 } | 207 } |
| 207 | 208 |
| 208 } // namespace blink | 209 } // namespace blink |
| OLD | NEW |