| 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 // The basic usage of the Filter interface is described in the comment at | 5 // The basic usage of the Filter interface is described in the comment at |
| 6 // the beginning of filter.h. If Filter::Factory is passed a vector of | 6 // the beginning of filter.h. If Filter::Factory is passed a vector of |
| 7 // size greater than 1, that interface is implemented by a series of filters | 7 // size greater than 1, that interface is implemented by a series of filters |
| 8 // connected in a chain. In such a case the first filter | 8 // connected in a chain. In such a case the first filter |
| 9 // in the chain proxies calls to ReadData() so that its return values | 9 // in the chain proxies calls to ReadData() so that its return values |
| 10 // apply to the entire chain. | 10 // apply to the entire chain. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // next filter needs it, a return value of FILTER_NEED_MORE_DATA from the | 21 // next filter needs it, a return value of FILTER_NEED_MORE_DATA from the |
| 22 // final filter will apply to the entire chain. | 22 // final filter will apply to the entire chain. |
| 23 | 23 |
| 24 #include "net/filter/filter.h" | 24 #include "net/filter/filter.h" |
| 25 | 25 |
| 26 #include "base/files/file_path.h" | 26 #include "base/files/file_path.h" |
| 27 #include "base/strings/string_util.h" | 27 #include "base/strings/string_util.h" |
| 28 #include "net/base/filename_util_unsafe.h" | 28 #include "net/base/filename_util_unsafe.h" |
| 29 #include "net/base/io_buffer.h" | 29 #include "net/base/io_buffer.h" |
| 30 #include "net/base/mime_util.h" | 30 #include "net/base/mime_util.h" |
| 31 #include "net/base/sdch_net_log_params.h" | |
| 32 #include "net/filter/gzip_filter.h" | 31 #include "net/filter/gzip_filter.h" |
| 33 #include "net/filter/sdch_filter.h" | 32 #include "net/filter/sdch_filter.h" |
| 34 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
| 35 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 36 | 35 |
| 37 namespace net { | |
| 38 | |
| 39 namespace { | 36 namespace { |
| 40 | 37 |
| 41 // Filter types (using canonical lower case only): | 38 // Filter types (using canonical lower case only): |
| 42 const char kDeflate[] = "deflate"; | 39 const char kDeflate[] = "deflate"; |
| 43 const char kGZip[] = "gzip"; | 40 const char kGZip[] = "gzip"; |
| 44 const char kXGZip[] = "x-gzip"; | 41 const char kXGZip[] = "x-gzip"; |
| 45 const char kSdch[] = "sdch"; | 42 const char kSdch[] = "sdch"; |
| 46 // compress and x-compress are currently not supported. If we decide to support | 43 // compress and x-compress are currently not supported. If we decide to support |
| 47 // them, we'll need the same mime type compatibility hack we have for gzip. For | 44 // them, we'll need the same mime type compatibility hack we have for gzip. For |
| 48 // more information, see Firefox's nsHttpChannel::ProcessNormal. | 45 // more information, see Firefox's nsHttpChannel::ProcessNormal. |
| 49 | 46 |
| 50 // Mime types: | 47 // Mime types: |
| 51 const char kApplicationXGzip[] = "application/x-gzip"; | 48 const char kApplicationXGzip[] = "application/x-gzip"; |
| 52 const char kApplicationGzip[] = "application/gzip"; | 49 const char kApplicationGzip[] = "application/gzip"; |
| 53 const char kApplicationXGunzip[] = "application/x-gunzip"; | 50 const char kApplicationXGunzip[] = "application/x-gunzip"; |
| 54 const char kTextHtml[] = "text/html"; | 51 const char kTextHtml[] = "text/html"; |
| 55 | 52 |
| 56 // Buffer size allocated when de-compressing data. | 53 // Buffer size allocated when de-compressing data. |
| 57 const int kFilterBufSize = 32 * 1024; | 54 const int kFilterBufSize = 32 * 1024; |
| 58 | 55 |
| 59 void LogSdchProblem(const FilterContext& filter_context, | 56 } // namespace |
| 60 SdchProblemCode problem) { | |
| 61 SdchManager::SdchErrorRecovery(problem); | |
| 62 filter_context.GetNetLog().AddEvent( | |
| 63 NetLog::TYPE_SDCH_DECODING_ERROR, | |
| 64 base::Bind(&NetLogSdchResourceProblemCallback, problem)); | |
| 65 } | |
| 66 | 57 |
| 67 } // namespace | 58 namespace net { |
| 68 | 59 |
| 69 FilterContext::~FilterContext() { | 60 FilterContext::~FilterContext() { |
| 70 } | 61 } |
| 71 | 62 |
| 72 Filter::~Filter() {} | 63 Filter::~Filter() {} |
| 73 | 64 |
| 74 // static | 65 // static |
| 75 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, | 66 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, |
| 76 const FilterContext& filter_context) { | 67 const FilterContext& filter_context) { |
| 77 if (filter_types.empty()) | 68 if (filter_types.empty()) |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 !IsSupportedMimeType(mime_type)) | 226 !IsSupportedMimeType(mime_type)) |
| 236 encoding_types->clear(); | 227 encoding_types->clear(); |
| 237 } | 228 } |
| 238 } | 229 } |
| 239 | 230 |
| 240 // If the request was for SDCH content, then we might need additional fixups. | 231 // If the request was for SDCH content, then we might need additional fixups. |
| 241 if (!filter_context.SdchResponseExpected()) { | 232 if (!filter_context.SdchResponseExpected()) { |
| 242 // It was not an SDCH request, so we'll just record stats. | 233 // It was not an SDCH request, so we'll just record stats. |
| 243 if (1 < encoding_types->size()) { | 234 if (1 < encoding_types->size()) { |
| 244 // Multiple filters were intended to only be used for SDCH (thus far!) | 235 // Multiple filters were intended to only be used for SDCH (thus far!) |
| 245 LogSdchProblem(filter_context, SDCH_MULTIENCODING_FOR_NON_SDCH_REQUEST); | 236 SdchManager::SdchErrorRecovery( |
| 237 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST); |
| 246 } | 238 } |
| 247 if ((1 == encoding_types->size()) && | 239 if ((1 == encoding_types->size()) && |
| 248 (FILTER_TYPE_SDCH == encoding_types->front())) { | 240 (FILTER_TYPE_SDCH == encoding_types->front())) { |
| 249 LogSdchProblem(filter_context, | 241 SdchManager::SdchErrorRecovery( |
| 250 SDCH_SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); | 242 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); |
| 251 } | 243 } |
| 252 return; | 244 return; |
| 253 } | 245 } |
| 254 | 246 |
| 255 // The request was tagged as an SDCH request, which means the server supplied | 247 // The request was tagged as an SDCH request, which means the server supplied |
| 256 // a dictionary, and we advertised it in the request. Some proxies will do | 248 // a dictionary, and we advertised it in the request. Some proxies will do |
| 257 // very strange things to the request, or the response, so we have to handle | 249 // very strange things to the request, or the response, so we have to handle |
| 258 // them gracefully. | 250 // them gracefully. |
| 259 | 251 |
| 260 // If content encoding included SDCH, then everything is "relatively" fine. | 252 // If content encoding included SDCH, then everything is "relatively" fine. |
| 261 if (!encoding_types->empty() && | 253 if (!encoding_types->empty() && |
| 262 (FILTER_TYPE_SDCH == encoding_types->front())) { | 254 (FILTER_TYPE_SDCH == encoding_types->front())) { |
| 263 // Some proxies (found currently in Argentina) strip the Content-Encoding | 255 // Some proxies (found currently in Argentina) strip the Content-Encoding |
| 264 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed | 256 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed |
| 265 // payload. To handle this gracefully, we simulate the "probably" deleted | 257 // payload. To handle this gracefully, we simulate the "probably" deleted |
| 266 // ",gzip" by appending a tentative gzip decode, which will default to a | 258 // ",gzip" by appending a tentative gzip decode, which will default to a |
| 267 // no-op pass through filter if it doesn't get gzip headers where expected. | 259 // no-op pass through filter if it doesn't get gzip headers where expected. |
| 268 if (1 == encoding_types->size()) { | 260 if (1 == encoding_types->size()) { |
| 269 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); | 261 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); |
| 270 LogSdchProblem(filter_context, SDCH_OPTIONAL_GUNZIP_ENCODING_ADDED); | 262 SdchManager::SdchErrorRecovery( |
| 263 SdchManager::OPTIONAL_GUNZIP_ENCODING_ADDED); |
| 271 } | 264 } |
| 272 return; | 265 return; |
| 273 } | 266 } |
| 274 | 267 |
| 275 // There are now several cases to handle for an SDCH request. Foremost, if | 268 // There are now several cases to handle for an SDCH request. Foremost, if |
| 276 // the outbound request was stripped so as not to advertise support for | 269 // the outbound request was stripped so as not to advertise support for |
| 277 // encodings, we might get back content with no encoding, or (for example) | 270 // encodings, we might get back content with no encoding, or (for example) |
| 278 // just gzip. We have to be sure that any changes we make allow for such | 271 // just gzip. We have to be sure that any changes we make allow for such |
| 279 // minimal coding to work. That issue is why we use TENTATIVE filters if we | 272 // minimal coding to work. That issue is why we use TENTATIVE filters if we |
| 280 // add any, as those filters sniff the content, and act as pass-through | 273 // add any, as those filters sniff the content, and act as pass-through |
| (...skipping 13 matching lines...) Expand all Loading... |
| 294 // The one unresolved failure mode comes when we advertise a dictionary, and | 287 // The one unresolved failure mode comes when we advertise a dictionary, and |
| 295 // the server tries to *send* a gzipped file (not gzip encode content), and | 288 // the server tries to *send* a gzipped file (not gzip encode content), and |
| 296 // then we could do a gzip decode :-(. Since SDCH is only (currently) | 289 // then we could do a gzip decode :-(. Since SDCH is only (currently) |
| 297 // supported server side on paths that only send HTML content, this mode has | 290 // supported server side on paths that only send HTML content, this mode has |
| 298 // never surfaced in the wild (and is unlikely to). | 291 // never surfaced in the wild (and is unlikely to). |
| 299 // We will gather a lot of stats as we perform the fixups | 292 // We will gather a lot of stats as we perform the fixups |
| 300 if (StartsWithASCII(mime_type, kTextHtml, false)) { | 293 if (StartsWithASCII(mime_type, kTextHtml, false)) { |
| 301 // Suspicious case: Advertised dictionary, but server didn't use sdch, and | 294 // Suspicious case: Advertised dictionary, but server didn't use sdch, and |
| 302 // we're HTML tagged. | 295 // we're HTML tagged. |
| 303 if (encoding_types->empty()) { | 296 if (encoding_types->empty()) { |
| 304 LogSdchProblem(filter_context, SDCH_ADDED_CONTENT_ENCODING); | 297 SdchManager::SdchErrorRecovery( |
| 298 SdchManager::ADDED_CONTENT_ENCODING); |
| 305 } else if (1 == encoding_types->size()) { | 299 } else if (1 == encoding_types->size()) { |
| 306 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODING); | 300 SdchManager::SdchErrorRecovery( |
| 301 SdchManager::FIXED_CONTENT_ENCODING); |
| 307 } else { | 302 } else { |
| 308 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODINGS); | 303 SdchManager::SdchErrorRecovery( |
| 304 SdchManager::FIXED_CONTENT_ENCODINGS); |
| 309 } | 305 } |
| 310 } else { | 306 } else { |
| 311 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding | 307 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding |
| 312 // was not marked for SDCH processing: Why did the server suggest an SDCH | 308 // was not marked for SDCH processing: Why did the server suggest an SDCH |
| 313 // dictionary in the first place??. Also, the content isn't | 309 // dictionary in the first place??. Also, the content isn't |
| 314 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for | 310 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for |
| 315 // HTML: Did some anti-virus system strip this tag (sometimes they strip | 311 // HTML: Did some anti-virus system strip this tag (sometimes they strip |
| 316 // accept-encoding headers on the request)?? Does the content encoding not | 312 // accept-encoding headers on the request)?? Does the content encoding not |
| 317 // start with "text/html" for some other reason?? We'll report this as a | 313 // start with "text/html" for some other reason?? We'll report this as a |
| 318 // fixup to a binary file, but it probably really is text/html (some how). | 314 // fixup to a binary file, but it probably really is text/html (some how). |
| 319 if (encoding_types->empty()) { | 315 if (encoding_types->empty()) { |
| 320 LogSdchProblem(filter_context, SDCH_BINARY_ADDED_CONTENT_ENCODING); | 316 SdchManager::SdchErrorRecovery( |
| 317 SdchManager::BINARY_ADDED_CONTENT_ENCODING); |
| 321 } else if (1 == encoding_types->size()) { | 318 } else if (1 == encoding_types->size()) { |
| 322 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODING); | 319 SdchManager::SdchErrorRecovery( |
| 320 SdchManager::BINARY_FIXED_CONTENT_ENCODING); |
| 323 } else { | 321 } else { |
| 324 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODINGS); | 322 SdchManager::SdchErrorRecovery( |
| 323 SdchManager::BINARY_FIXED_CONTENT_ENCODINGS); |
| 325 } | 324 } |
| 326 } | 325 } |
| 327 | 326 |
| 328 // Leave the existing encoding type to be processed first, and add our | 327 // Leave the existing encoding type to be processed first, and add our |
| 329 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will | 328 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will |
| 330 // perform a second layer of gzip encoding atop the server's sdch,gzip | 329 // perform a second layer of gzip encoding atop the server's sdch,gzip |
| 331 // encoding, and then claim that the content encoding is a mere gzip. As a | 330 // encoding, and then claim that the content encoding is a mere gzip. As a |
| 332 // result we'll need (in that case) to do the gunzip, plus our tentative | 331 // result we'll need (in that case) to do the gunzip, plus our tentative |
| 333 // gunzip and tentative SDCH decoding. | 332 // gunzip and tentative SDCH decoding. |
| 334 // This approach nicely handles the empty() list as well, and should work with | 333 // 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... |
| 424 | 423 |
| 425 void Filter::PushDataIntoNextFilter() { | 424 void Filter::PushDataIntoNextFilter() { |
| 426 IOBuffer* next_buffer = next_filter_->stream_buffer(); | 425 IOBuffer* next_buffer = next_filter_->stream_buffer(); |
| 427 int next_size = next_filter_->stream_buffer_size(); | 426 int next_size = next_filter_->stream_buffer_size(); |
| 428 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); | 427 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); |
| 429 if (FILTER_ERROR != last_status_) | 428 if (FILTER_ERROR != last_status_) |
| 430 next_filter_->FlushStreamBuffer(next_size); | 429 next_filter_->FlushStreamBuffer(next_size); |
| 431 } | 430 } |
| 432 | 431 |
| 433 } // namespace net | 432 } // namespace net |
| OLD | NEW |