| 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 "net/filter/filter.h" | 5 #include "net/filter/filter.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "net/base/filename_util_unsafe.h" | 9 #include "net/base/filename_util_unsafe.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 } | 154 } |
| 155 | 155 |
| 156 // static | 156 // static |
| 157 void Filter::FixupEncodingTypes( | 157 void Filter::FixupEncodingTypes( |
| 158 const FilterContext& filter_context, | 158 const FilterContext& filter_context, |
| 159 std::vector<FilterType>* encoding_types) { | 159 std::vector<FilterType>* encoding_types) { |
| 160 std::string mime_type; | 160 std::string mime_type; |
| 161 bool success = filter_context.GetMimeType(&mime_type); | 161 bool success = filter_context.GetMimeType(&mime_type); |
| 162 DCHECK(success || mime_type.empty()); | 162 DCHECK(success || mime_type.empty()); |
| 163 | 163 |
| 164 GURL url; |
| 165 success = filter_context.GetURL(&url); |
| 166 |
| 164 if ((1 == encoding_types->size()) && | 167 if ((1 == encoding_types->size()) && |
| 165 (FILTER_TYPE_GZIP == encoding_types->front())) { | 168 (FILTER_TYPE_GZIP == encoding_types->front())) { |
| 166 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || | 169 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || |
| 167 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || | 170 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || |
| 168 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) | 171 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) |
| 169 // The server has told us that it sent us gziped content with a gzip | 172 // The server has told us that it sent us gziped content with a gzip |
| 170 // content encoding. Sadly, Apache mistakenly sets these headers for all | 173 // content encoding. Sadly, Apache mistakenly sets these headers for all |
| 171 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore | 174 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore |
| 172 // the Content-Encoding here. | 175 // the Content-Encoding here. |
| 173 encoding_types->clear(); | 176 encoding_types->clear(); |
| 174 | 177 |
| 175 GURL url; | 178 DCHECK(success); |
| 176 std::string disposition; | 179 std::string disposition; |
| 177 success = filter_context.GetURL(&url); | |
| 178 DCHECK(success); | |
| 179 filter_context.GetContentDisposition(&disposition); | 180 filter_context.GetContentDisposition(&disposition); |
| 180 // Don't supply a MIME type here, since that may cause disk IO. | 181 // Don't supply a MIME type here, since that may cause disk IO. |
| 181 base::FilePath::StringType extension = | 182 base::FilePath::StringType extension = |
| 182 GenerateFileExtensionUnsafe(url, disposition, "UTF-8", "", "", ""); | 183 GenerateFileExtensionUnsafe(url, disposition, "UTF-8", "", "", ""); |
| 183 | 184 |
| 184 if (filter_context.IsDownload()) { | 185 if (filter_context.IsDownload()) { |
| 185 // We don't want to decompress gzipped files when the user explicitly | 186 // We don't want to decompress gzipped files when the user explicitly |
| 186 // asks to download them. | 187 // asks to download them. |
| 187 // For the case of svgz files, we use the extension to distinguish | 188 // For the case of svgz files, we use the extension to distinguish |
| 188 // between svgz files and svg files compressed with gzip by the server. | 189 // between svgz files and svg files compressed with gzip by the server. |
| 189 // When viewing a .svgz file, we need to uncompress it, but we don't | 190 // When viewing a .svgz file, we need to uncompress it, but we don't |
| 190 // want to do that when downloading. | 191 // want to do that when downloading. |
| 191 // See Firefox's nonDecodableExtensions in nsExternalHelperAppService.cpp | 192 // See Firefox's nonDecodableExtensions in nsExternalHelperAppService.cpp |
| 192 if (EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || | 193 if (EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || |
| 193 LowerCaseEqualsASCII(extension, ".tgz") || | 194 LowerCaseEqualsASCII(extension, ".tgz") || |
| 194 LowerCaseEqualsASCII(extension, ".svgz")) | 195 LowerCaseEqualsASCII(extension, ".svgz")) |
| 195 encoding_types->clear(); | 196 encoding_types->clear(); |
| 196 } else { | 197 } else { |
| 197 // When the user does not explicitly ask to download a file, if we get a | 198 // When the user does not explicitly ask to download a file, if we get a |
| 198 // supported mime type, then we attempt to decompress in order to view it. | 199 // supported mime type, then we attempt to decompress in order to view it. |
| 199 // However, if it's not a supported mime type, then we will attempt to | 200 // However, if it's not a supported mime type, then we will attempt to |
| 200 // download it, and in that case, don't decompress .gz/.tgz files. | 201 // download it, and in that case, don't decompress .gz/.tgz files. |
| 201 if ((EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || | 202 if ((EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || |
| 202 LowerCaseEqualsASCII(extension, ".tgz")) && | 203 LowerCaseEqualsASCII(extension, ".tgz")) && |
| 203 !IsSupportedMimeType(mime_type)) | 204 !IsSupportedMimeType(mime_type)) |
| 204 encoding_types->clear(); | 205 encoding_types->clear(); |
| 205 } | 206 } |
| 206 } | 207 } |
| 207 | 208 |
| 209 SdchManager* sdch_manager = |
| 210 filter_context.GetURLRequestContext()->sdch_manager(); |
| 211 |
| 208 // If the request was for SDCH content, then we might need additional fixups. | 212 // If the request was for SDCH content, then we might need additional fixups. |
| 209 if (!filter_context.IsSdchResponse()) { | 213 if (!filter_context.IsSdchResponse()) { |
| 210 // It was not an SDCH request, so we'll just record stats. | 214 // It was not an SDCH request, so we'll just record stats. |
| 211 if (1 < encoding_types->size()) { | 215 if (1 < encoding_types->size()) { |
| 212 // Multiple filters were intended to only be used for SDCH (thus far!) | 216 // Multiple filters were intended to only be used for SDCH (thus far!) |
| 213 SdchManager::SdchErrorRecovery( | 217 sdch_manager->SdchErrorRecovery( |
| 214 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST); | 218 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST, url); |
| 215 } | 219 } |
| 216 if ((1 == encoding_types->size()) && | 220 if ((1 == encoding_types->size()) && |
| 217 (FILTER_TYPE_SDCH == encoding_types->front())) { | 221 (FILTER_TYPE_SDCH == encoding_types->front())) { |
| 218 SdchManager::SdchErrorRecovery( | 222 sdch_manager->SdchErrorRecovery( |
| 219 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); | 223 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST, url); |
| 220 } | 224 } |
| 221 return; | 225 return; |
| 222 } | 226 } |
| 223 | 227 |
| 224 // The request was tagged as an SDCH request, which means the server supplied | 228 // The request was tagged as an SDCH request, which means the server supplied |
| 225 // a dictionary, and we advertised it in the request. Some proxies will do | 229 // a dictionary, and we advertised it in the request. Some proxies will do |
| 226 // very strange things to the request, or the response, so we have to handle | 230 // very strange things to the request, or the response, so we have to handle |
| 227 // them gracefully. | 231 // them gracefully. |
| 228 | 232 |
| 229 // If content encoding included SDCH, then everything is "relatively" fine. | 233 // If content encoding included SDCH, then everything is "relatively" fine. |
| 230 if (!encoding_types->empty() && | 234 if (!encoding_types->empty() && |
| 231 (FILTER_TYPE_SDCH == encoding_types->front())) { | 235 (FILTER_TYPE_SDCH == encoding_types->front())) { |
| 232 // Some proxies (found currently in Argentina) strip the Content-Encoding | 236 // Some proxies (found currently in Argentina) strip the Content-Encoding |
| 233 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed | 237 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed |
| 234 // payload. To handle this gracefully, we simulate the "probably" deleted | 238 // payload. To handle this gracefully, we simulate the "probably" deleted |
| 235 // ",gzip" by appending a tentative gzip decode, which will default to a | 239 // ",gzip" by appending a tentative gzip decode, which will default to a |
| 236 // no-op pass through filter if it doesn't get gzip headers where expected. | 240 // no-op pass through filter if it doesn't get gzip headers where expected. |
| 237 if (1 == encoding_types->size()) { | 241 if (1 == encoding_types->size()) { |
| 238 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); | 242 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); |
| 239 SdchManager::SdchErrorRecovery( | 243 sdch_manager->SdchErrorRecovery( |
| 240 SdchManager::OPTIONAL_GUNZIP_ENCODING_ADDED); | 244 SdchManager::OPTIONAL_GUNZIP_ENCODING_ADDED, url); |
| 241 } | 245 } |
| 242 return; | 246 return; |
| 243 } | 247 } |
| 244 | 248 |
| 245 // There are now several cases to handle for an SDCH request. Foremost, if | 249 // There are now several cases to handle for an SDCH request. Foremost, if |
| 246 // the outbound request was stripped so as not to advertise support for | 250 // the outbound request was stripped so as not to advertise support for |
| 247 // encodings, we might get back content with no encoding, or (for example) | 251 // encodings, we might get back content with no encoding, or (for example) |
| 248 // just gzip. We have to be sure that any changes we make allow for such | 252 // just gzip. We have to be sure that any changes we make allow for such |
| 249 // minimal coding to work. That issue is why we use TENTATIVE filters if we | 253 // minimal coding to work. That issue is why we use TENTATIVE filters if we |
| 250 // add any, as those filters sniff the content, and act as pass-through | 254 // add any, as those filters sniff the content, and act as pass-through |
| (...skipping 13 matching lines...) Expand all Loading... |
| 264 // The one unresolved failure mode comes when we advertise a dictionary, and | 268 // The one unresolved failure mode comes when we advertise a dictionary, and |
| 265 // the server tries to *send* a gzipped file (not gzip encode content), and | 269 // the server tries to *send* a gzipped file (not gzip encode content), and |
| 266 // then we could do a gzip decode :-(. Since SDCH is only (currently) | 270 // then we could do a gzip decode :-(. Since SDCH is only (currently) |
| 267 // supported server side on paths that only send HTML content, this mode has | 271 // supported server side on paths that only send HTML content, this mode has |
| 268 // never surfaced in the wild (and is unlikely to). | 272 // never surfaced in the wild (and is unlikely to). |
| 269 // We will gather a lot of stats as we perform the fixups | 273 // We will gather a lot of stats as we perform the fixups |
| 270 if (StartsWithASCII(mime_type, kTextHtml, false)) { | 274 if (StartsWithASCII(mime_type, kTextHtml, false)) { |
| 271 // Suspicious case: Advertised dictionary, but server didn't use sdch, and | 275 // Suspicious case: Advertised dictionary, but server didn't use sdch, and |
| 272 // we're HTML tagged. | 276 // we're HTML tagged. |
| 273 if (encoding_types->empty()) { | 277 if (encoding_types->empty()) { |
| 274 SdchManager::SdchErrorRecovery( | 278 sdch_manager->SdchErrorRecovery( |
| 275 SdchManager::ADDED_CONTENT_ENCODING); | 279 SdchManager::ADDED_CONTENT_ENCODING, url); |
| 276 } else if (1 == encoding_types->size()) { | 280 } else if (1 == encoding_types->size()) { |
| 277 SdchManager::SdchErrorRecovery( | 281 sdch_manager->SdchErrorRecovery( |
| 278 SdchManager::FIXED_CONTENT_ENCODING); | 282 SdchManager::FIXED_CONTENT_ENCODING, url); |
| 279 } else { | 283 } else { |
| 280 SdchManager::SdchErrorRecovery( | 284 sdch_manager->SdchErrorRecovery( |
| 281 SdchManager::FIXED_CONTENT_ENCODINGS); | 285 SdchManager::FIXED_CONTENT_ENCODINGS, url); |
| 282 } | 286 } |
| 283 } else { | 287 } else { |
| 284 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding | 288 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding |
| 285 // was not marked for SDCH processing: Why did the server suggest an SDCH | 289 // was not marked for SDCH processing: Why did the server suggest an SDCH |
| 286 // dictionary in the first place??. Also, the content isn't | 290 // dictionary in the first place??. Also, the content isn't |
| 287 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for | 291 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for |
| 288 // HTML: Did some anti-virus system strip this tag (sometimes they strip | 292 // HTML: Did some anti-virus system strip this tag (sometimes they strip |
| 289 // accept-encoding headers on the request)?? Does the content encoding not | 293 // accept-encoding headers on the request)?? Does the content encoding not |
| 290 // start with "text/html" for some other reason?? We'll report this as a | 294 // start with "text/html" for some other reason?? We'll report this as a |
| 291 // fixup to a binary file, but it probably really is text/html (some how). | 295 // fixup to a binary file, but it probably really is text/html (some how). |
| 292 if (encoding_types->empty()) { | 296 if (encoding_types->empty()) { |
| 293 SdchManager::SdchErrorRecovery( | 297 sdch_manager->SdchErrorRecovery( |
| 294 SdchManager::BINARY_ADDED_CONTENT_ENCODING); | 298 SdchManager::BINARY_ADDED_CONTENT_ENCODING, url); |
| 295 } else if (1 == encoding_types->size()) { | 299 } else if (1 == encoding_types->size()) { |
| 296 SdchManager::SdchErrorRecovery( | 300 sdch_manager->SdchErrorRecovery( |
| 297 SdchManager::BINARY_FIXED_CONTENT_ENCODING); | 301 SdchManager::BINARY_FIXED_CONTENT_ENCODING, url); |
| 298 } else { | 302 } else { |
| 299 SdchManager::SdchErrorRecovery( | 303 sdch_manager->SdchErrorRecovery( |
| 300 SdchManager::BINARY_FIXED_CONTENT_ENCODINGS); | 304 SdchManager::BINARY_FIXED_CONTENT_ENCODINGS, url); |
| 301 } | 305 } |
| 302 } | 306 } |
| 303 | 307 |
| 304 // Leave the existing encoding type to be processed first, and add our | 308 // Leave the existing encoding type to be processed first, and add our |
| 305 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will | 309 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will |
| 306 // perform a second layer of gzip encoding atop the server's sdch,gzip | 310 // perform a second layer of gzip encoding atop the server's sdch,gzip |
| 307 // encoding, and then claim that the content encoding is a mere gzip. As a | 311 // encoding, and then claim that the content encoding is a mere gzip. As a |
| 308 // result we'll need (in that case) to do the gunzip, plus our tentative | 312 // result we'll need (in that case) to do the gunzip, plus our tentative |
| 309 // gunzip and tentative SDCH decoding. | 313 // gunzip and tentative SDCH decoding. |
| 310 // This approach nicely handles the empty() list as well, and should work with | 314 // This approach nicely handles the empty() list as well, and should work with |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 | 404 |
| 401 void Filter::PushDataIntoNextFilter() { | 405 void Filter::PushDataIntoNextFilter() { |
| 402 IOBuffer* next_buffer = next_filter_->stream_buffer(); | 406 IOBuffer* next_buffer = next_filter_->stream_buffer(); |
| 403 int next_size = next_filter_->stream_buffer_size(); | 407 int next_size = next_filter_->stream_buffer_size(); |
| 404 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); | 408 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); |
| 405 if (FILTER_ERROR != last_status_) | 409 if (FILTER_ERROR != last_status_) |
| 406 next_filter_->FlushStreamBuffer(next_size); | 410 next_filter_->FlushStreamBuffer(next_size); |
| 407 } | 411 } |
| 408 | 412 |
| 409 } // namespace net | 413 } // namespace net |
| OLD | NEW |