Chromium Code Reviews| 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" |
| 11 #include "net/base/mime_util.h" | 11 #include "net/base/mime_util.h" |
| 12 #include "net/base/sdch_net_log_params.h" | |
| 12 #include "net/filter/gzip_filter.h" | 13 #include "net/filter/gzip_filter.h" |
| 13 #include "net/filter/sdch_filter.h" | 14 #include "net/filter/sdch_filter.h" |
| 14 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
| 15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 16 | 17 |
| 18 namespace net { | |
| 19 | |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 19 // Filter types (using canonical lower case only): | 22 // Filter types (using canonical lower case only): |
| 20 const char kDeflate[] = "deflate"; | 23 const char kDeflate[] = "deflate"; |
| 21 const char kGZip[] = "gzip"; | 24 const char kGZip[] = "gzip"; |
| 22 const char kXGZip[] = "x-gzip"; | 25 const char kXGZip[] = "x-gzip"; |
| 23 const char kSdch[] = "sdch"; | 26 const char kSdch[] = "sdch"; |
| 24 // compress and x-compress are currently not supported. If we decide to support | 27 // compress and x-compress are currently not supported. If we decide to support |
| 25 // them, we'll need the same mime type compatibility hack we have for gzip. For | 28 // them, we'll need the same mime type compatibility hack we have for gzip. For |
| 26 // more information, see Firefox's nsHttpChannel::ProcessNormal. | 29 // more information, see Firefox's nsHttpChannel::ProcessNormal. |
| 27 | 30 |
| 28 // Mime types: | 31 // Mime types: |
| 29 const char kApplicationXGzip[] = "application/x-gzip"; | 32 const char kApplicationXGzip[] = "application/x-gzip"; |
| 30 const char kApplicationGzip[] = "application/gzip"; | 33 const char kApplicationGzip[] = "application/gzip"; |
| 31 const char kApplicationXGunzip[] = "application/x-gunzip"; | 34 const char kApplicationXGunzip[] = "application/x-gunzip"; |
| 32 const char kTextHtml[] = "text/html"; | 35 const char kTextHtml[] = "text/html"; |
| 33 | 36 |
| 34 // Buffer size allocated when de-compressing data. | 37 // Buffer size allocated when de-compressing data. |
| 35 const int kFilterBufSize = 32 * 1024; | 38 const int kFilterBufSize = 32 * 1024; |
| 36 | 39 |
| 40 void LogSdchProblem(const FilterContext& filter_context, | |
| 41 SdchManager::ProblemCodes problem) { | |
| 42 SdchManager::SdchErrorRecovery(problem); | |
| 43 filter_context.GetNetLog().AddEvent( | |
| 44 NetLog::TYPE_SDCH_RESOURCE_ERROR, | |
| 45 base::Bind(&NetLogSdchResourceProblemCallback, problem)); | |
| 46 } | |
| 47 | |
| 37 } // namespace | 48 } // namespace |
| 38 | 49 |
| 39 namespace net { | |
| 40 | |
| 41 FilterContext::~FilterContext() { | 50 FilterContext::~FilterContext() { |
| 42 } | 51 } |
| 43 | 52 |
| 44 Filter::~Filter() {} | 53 Filter::~Filter() {} |
| 45 | 54 |
| 46 // static | 55 // static |
| 47 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, | 56 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, |
| 48 const FilterContext& filter_context) { | 57 const FilterContext& filter_context) { |
| 49 if (filter_types.empty()) | 58 if (filter_types.empty()) |
| 50 return NULL; | 59 return NULL; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 } | 163 } |
| 155 | 164 |
| 156 // static | 165 // static |
| 157 void Filter::FixupEncodingTypes( | 166 void Filter::FixupEncodingTypes( |
| 158 const FilterContext& filter_context, | 167 const FilterContext& filter_context, |
| 159 std::vector<FilterType>* encoding_types) { | 168 std::vector<FilterType>* encoding_types) { |
| 160 std::string mime_type; | 169 std::string mime_type; |
| 161 bool success = filter_context.GetMimeType(&mime_type); | 170 bool success = filter_context.GetMimeType(&mime_type); |
| 162 DCHECK(success || mime_type.empty()); | 171 DCHECK(success || mime_type.empty()); |
| 163 | 172 |
| 173 GURL url; | |
| 174 success = filter_context.GetURL(&url); | |
|
Randy Smith (Not in Mondays)
2014/08/13 17:35:46
Why the change?
baranovich
2014/08/13 19:13:48
Done.
| |
| 175 | |
| 164 if ((1 == encoding_types->size()) && | 176 if ((1 == encoding_types->size()) && |
| 165 (FILTER_TYPE_GZIP == encoding_types->front())) { | 177 (FILTER_TYPE_GZIP == encoding_types->front())) { |
| 166 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || | 178 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || |
| 167 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || | 179 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || |
| 168 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) | 180 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) |
| 169 // The server has told us that it sent us gziped content with a gzip | 181 // 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 | 182 // content encoding. Sadly, Apache mistakenly sets these headers for all |
| 171 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore | 183 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore |
| 172 // the Content-Encoding here. | 184 // the Content-Encoding here. |
| 173 encoding_types->clear(); | 185 encoding_types->clear(); |
| 174 | 186 |
| 175 GURL url; | 187 DCHECK(success); |
| 176 std::string disposition; | 188 std::string disposition; |
| 177 success = filter_context.GetURL(&url); | |
| 178 DCHECK(success); | |
| 179 filter_context.GetContentDisposition(&disposition); | 189 filter_context.GetContentDisposition(&disposition); |
| 180 // Don't supply a MIME type here, since that may cause disk IO. | 190 // Don't supply a MIME type here, since that may cause disk IO. |
| 181 base::FilePath::StringType extension = | 191 base::FilePath::StringType extension = |
| 182 GenerateFileExtensionUnsafe(url, disposition, "UTF-8", "", "", ""); | 192 GenerateFileExtensionUnsafe(url, disposition, "UTF-8", "", "", ""); |
| 183 | 193 |
| 184 if (filter_context.IsDownload()) { | 194 if (filter_context.IsDownload()) { |
| 185 // We don't want to decompress gzipped files when the user explicitly | 195 // We don't want to decompress gzipped files when the user explicitly |
| 186 // asks to download them. | 196 // asks to download them. |
| 187 // For the case of svgz files, we use the extension to distinguish | 197 // 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. | 198 // between svgz files and svg files compressed with gzip by the server. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 203 !IsSupportedMimeType(mime_type)) | 213 !IsSupportedMimeType(mime_type)) |
| 204 encoding_types->clear(); | 214 encoding_types->clear(); |
| 205 } | 215 } |
| 206 } | 216 } |
| 207 | 217 |
| 208 // If the request was for SDCH content, then we might need additional fixups. | 218 // If the request was for SDCH content, then we might need additional fixups. |
| 209 if (!filter_context.IsSdchResponse()) { | 219 if (!filter_context.IsSdchResponse()) { |
| 210 // It was not an SDCH request, so we'll just record stats. | 220 // It was not an SDCH request, so we'll just record stats. |
| 211 if (1 < encoding_types->size()) { | 221 if (1 < encoding_types->size()) { |
| 212 // Multiple filters were intended to only be used for SDCH (thus far!) | 222 // Multiple filters were intended to only be used for SDCH (thus far!) |
| 213 SdchManager::SdchErrorRecovery( | 223 LogSdchProblem(filter_context, |
| 214 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST); | 224 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST); |
| 215 } | 225 } |
| 216 if ((1 == encoding_types->size()) && | 226 if ((1 == encoding_types->size()) && |
| 217 (FILTER_TYPE_SDCH == encoding_types->front())) { | 227 (FILTER_TYPE_SDCH == encoding_types->front())) { |
| 218 SdchManager::SdchErrorRecovery( | 228 LogSdchProblem(filter_context, |
| 219 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); | 229 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); |
| 220 } | 230 } |
| 221 return; | 231 return; |
| 222 } | 232 } |
| 223 | 233 |
| 224 // The request was tagged as an SDCH request, which means the server supplied | 234 // 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 | 235 // 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 | 236 // very strange things to the request, or the response, so we have to handle |
| 227 // them gracefully. | 237 // them gracefully. |
| 228 | 238 |
| 229 // If content encoding included SDCH, then everything is "relatively" fine. | 239 // If content encoding included SDCH, then everything is "relatively" fine. |
| 230 if (!encoding_types->empty() && | 240 if (!encoding_types->empty() && |
| 231 (FILTER_TYPE_SDCH == encoding_types->front())) { | 241 (FILTER_TYPE_SDCH == encoding_types->front())) { |
| 232 // Some proxies (found currently in Argentina) strip the Content-Encoding | 242 // Some proxies (found currently in Argentina) strip the Content-Encoding |
| 233 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed | 243 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed |
| 234 // payload. To handle this gracefully, we simulate the "probably" deleted | 244 // payload. To handle this gracefully, we simulate the "probably" deleted |
| 235 // ",gzip" by appending a tentative gzip decode, which will default to a | 245 // ",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. | 246 // no-op pass through filter if it doesn't get gzip headers where expected. |
| 237 if (1 == encoding_types->size()) { | 247 if (1 == encoding_types->size()) { |
| 238 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); | 248 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); |
| 239 SdchManager::SdchErrorRecovery( | 249 LogSdchProblem(filter_context, |
| 240 SdchManager::OPTIONAL_GUNZIP_ENCODING_ADDED); | 250 SdchManager::OPTIONAL_GUNZIP_ENCODING_ADDED); |
| 241 } | 251 } |
| 242 return; | 252 return; |
| 243 } | 253 } |
| 244 | 254 |
| 245 // There are now several cases to handle for an SDCH request. Foremost, if | 255 // 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 | 256 // 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) | 257 // 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 | 258 // 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 | 259 // 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 | 260 // 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 | 274 // 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 | 275 // 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) | 276 // 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 | 277 // supported server side on paths that only send HTML content, this mode has |
| 268 // never surfaced in the wild (and is unlikely to). | 278 // never surfaced in the wild (and is unlikely to). |
| 269 // We will gather a lot of stats as we perform the fixups | 279 // We will gather a lot of stats as we perform the fixups |
| 270 if (StartsWithASCII(mime_type, kTextHtml, false)) { | 280 if (StartsWithASCII(mime_type, kTextHtml, false)) { |
| 271 // Suspicious case: Advertised dictionary, but server didn't use sdch, and | 281 // Suspicious case: Advertised dictionary, but server didn't use sdch, and |
| 272 // we're HTML tagged. | 282 // we're HTML tagged. |
| 273 if (encoding_types->empty()) { | 283 if (encoding_types->empty()) { |
| 274 SdchManager::SdchErrorRecovery( | 284 LogSdchProblem(filter_context, SdchManager::ADDED_CONTENT_ENCODING); |
| 275 SdchManager::ADDED_CONTENT_ENCODING); | |
| 276 } else if (1 == encoding_types->size()) { | 285 } else if (1 == encoding_types->size()) { |
| 277 SdchManager::SdchErrorRecovery( | 286 LogSdchProblem(filter_context, SdchManager::FIXED_CONTENT_ENCODING); |
| 278 SdchManager::FIXED_CONTENT_ENCODING); | |
| 279 } else { | 287 } else { |
| 280 SdchManager::SdchErrorRecovery( | 288 LogSdchProblem(filter_context, SdchManager::FIXED_CONTENT_ENCODINGS); |
| 281 SdchManager::FIXED_CONTENT_ENCODINGS); | |
| 282 } | 289 } |
| 283 } else { | 290 } else { |
| 284 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding | 291 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding |
| 285 // was not marked for SDCH processing: Why did the server suggest an SDCH | 292 // was not marked for SDCH processing: Why did the server suggest an SDCH |
| 286 // dictionary in the first place??. Also, the content isn't | 293 // dictionary in the first place??. Also, the content isn't |
| 287 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for | 294 // 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 | 295 // HTML: Did some anti-virus system strip this tag (sometimes they strip |
| 289 // accept-encoding headers on the request)?? Does the content encoding not | 296 // 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 | 297 // 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). | 298 // fixup to a binary file, but it probably really is text/html (some how). |
| 292 if (encoding_types->empty()) { | 299 if (encoding_types->empty()) { |
| 293 SdchManager::SdchErrorRecovery( | 300 LogSdchProblem(filter_context, |
| 294 SdchManager::BINARY_ADDED_CONTENT_ENCODING); | 301 SdchManager::BINARY_ADDED_CONTENT_ENCODING); |
| 295 } else if (1 == encoding_types->size()) { | 302 } else if (1 == encoding_types->size()) { |
| 296 SdchManager::SdchErrorRecovery( | 303 LogSdchProblem(filter_context, |
| 297 SdchManager::BINARY_FIXED_CONTENT_ENCODING); | 304 SdchManager::BINARY_FIXED_CONTENT_ENCODING); |
| 298 } else { | 305 } else { |
| 299 SdchManager::SdchErrorRecovery( | 306 LogSdchProblem(filter_context, |
| 300 SdchManager::BINARY_FIXED_CONTENT_ENCODINGS); | 307 SdchManager::BINARY_FIXED_CONTENT_ENCODINGS); |
| 301 } | 308 } |
| 302 } | 309 } |
| 303 | 310 |
| 304 // Leave the existing encoding type to be processed first, and add our | 311 // Leave the existing encoding type to be processed first, and add our |
| 305 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will | 312 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will |
| 306 // perform a second layer of gzip encoding atop the server's sdch,gzip | 313 // 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 | 314 // 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 | 315 // result we'll need (in that case) to do the gunzip, plus our tentative |
| 309 // gunzip and tentative SDCH decoding. | 316 // gunzip and tentative SDCH decoding. |
| 310 // This approach nicely handles the empty() list as well, and should work with | 317 // 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 | 407 |
| 401 void Filter::PushDataIntoNextFilter() { | 408 void Filter::PushDataIntoNextFilter() { |
| 402 IOBuffer* next_buffer = next_filter_->stream_buffer(); | 409 IOBuffer* next_buffer = next_filter_->stream_buffer(); |
| 403 int next_size = next_filter_->stream_buffer_size(); | 410 int next_size = next_filter_->stream_buffer_size(); |
| 404 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); | 411 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); |
| 405 if (FILTER_ERROR != last_status_) | 412 if (FILTER_ERROR != last_status_) |
| 406 next_filter_->FlushStreamBuffer(next_size); | 413 next_filter_->FlushStreamBuffer(next_size); |
| 407 } | 414 } |
| 408 | 415 |
| 409 } // namespace net | 416 } // namespace net |
| OLD | NEW |