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. |
(...skipping 14 matching lines...) Expand all Loading... |
203 !IsSupportedMimeType(mime_type)) | 204 !IsSupportedMimeType(mime_type)) |
204 encoding_types->clear(); | 205 encoding_types->clear(); |
205 } | 206 } |
206 } | 207 } |
207 | 208 |
208 // If the request was for SDCH content, then we might need additional fixups. | 209 // If the request was for SDCH content, then we might need additional fixups. |
209 if (!filter_context.IsSdchResponse()) { | 210 if (!filter_context.IsSdchResponse()) { |
210 // It was not an SDCH request, so we'll just record stats. | 211 // It was not an SDCH request, so we'll just record stats. |
211 if (1 < encoding_types->size()) { | 212 if (1 < encoding_types->size()) { |
212 // Multiple filters were intended to only be used for SDCH (thus far!) | 213 // Multiple filters were intended to only be used for SDCH (thus far!) |
213 SdchManager::SdchErrorRecovery( | 214 filter_context.LogSdchProblem( |
214 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST); | 215 SdchManager::MULTIENCODING_FOR_NON_SDCH_REQUEST); |
215 } | 216 } |
216 if ((1 == encoding_types->size()) && | 217 if ((1 == encoding_types->size()) && |
217 (FILTER_TYPE_SDCH == encoding_types->front())) { | 218 (FILTER_TYPE_SDCH == encoding_types->front())) { |
218 SdchManager::SdchErrorRecovery( | 219 filter_context.LogSdchProblem( |
219 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); | 220 SdchManager::SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); |
220 } | 221 } |
221 return; | 222 return; |
222 } | 223 } |
223 | 224 |
224 // The request was tagged as an SDCH request, which means the server supplied | 225 // 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 | 226 // 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 | 227 // very strange things to the request, or the response, so we have to handle |
227 // them gracefully. | 228 // them gracefully. |
228 | 229 |
229 // If content encoding included SDCH, then everything is "relatively" fine. | 230 // If content encoding included SDCH, then everything is "relatively" fine. |
230 if (!encoding_types->empty() && | 231 if (!encoding_types->empty() && |
231 (FILTER_TYPE_SDCH == encoding_types->front())) { | 232 (FILTER_TYPE_SDCH == encoding_types->front())) { |
232 // Some proxies (found currently in Argentina) strip the Content-Encoding | 233 // Some proxies (found currently in Argentina) strip the Content-Encoding |
233 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed | 234 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed |
234 // payload. To handle this gracefully, we simulate the "probably" deleted | 235 // payload. To handle this gracefully, we simulate the "probably" deleted |
235 // ",gzip" by appending a tentative gzip decode, which will default to a | 236 // ",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. | 237 // no-op pass through filter if it doesn't get gzip headers where expected. |
237 if (1 == encoding_types->size()) { | 238 if (1 == encoding_types->size()) { |
238 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); | 239 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); |
239 SdchManager::SdchErrorRecovery( | 240 filter_context.LogSdchProblem( |
240 SdchManager::OPTIONAL_GUNZIP_ENCODING_ADDED); | 241 SdchManager::OPTIONAL_GUNZIP_ENCODING_ADDED); |
241 } | 242 } |
242 return; | 243 return; |
243 } | 244 } |
244 | 245 |
245 // There are now several cases to handle for an SDCH request. Foremost, if | 246 // 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 | 247 // 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) | 248 // 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 | 249 // 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 | 250 // minimal coding to work. That issue is why we use TENTATIVE filters if we |
(...skipping 14 matching lines...) Expand all Loading... |
264 // The one unresolved failure mode comes when we advertise a dictionary, and | 265 // 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 | 266 // 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) | 267 // 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 | 268 // supported server side on paths that only send HTML content, this mode has |
268 // never surfaced in the wild (and is unlikely to). | 269 // never surfaced in the wild (and is unlikely to). |
269 // We will gather a lot of stats as we perform the fixups | 270 // We will gather a lot of stats as we perform the fixups |
270 if (StartsWithASCII(mime_type, kTextHtml, false)) { | 271 if (StartsWithASCII(mime_type, kTextHtml, false)) { |
271 // Suspicious case: Advertised dictionary, but server didn't use sdch, and | 272 // Suspicious case: Advertised dictionary, but server didn't use sdch, and |
272 // we're HTML tagged. | 273 // we're HTML tagged. |
273 if (encoding_types->empty()) { | 274 if (encoding_types->empty()) { |
274 SdchManager::SdchErrorRecovery( | 275 filter_context.LogSdchProblem(SdchManager::ADDED_CONTENT_ENCODING); |
275 SdchManager::ADDED_CONTENT_ENCODING); | |
276 } else if (1 == encoding_types->size()) { | 276 } else if (1 == encoding_types->size()) { |
277 SdchManager::SdchErrorRecovery( | 277 filter_context.LogSdchProblem(SdchManager::FIXED_CONTENT_ENCODING); |
278 SdchManager::FIXED_CONTENT_ENCODING); | |
279 } else { | 278 } else { |
280 SdchManager::SdchErrorRecovery( | 279 filter_context.LogSdchProblem(SdchManager::FIXED_CONTENT_ENCODINGS); |
281 SdchManager::FIXED_CONTENT_ENCODINGS); | |
282 } | 280 } |
283 } else { | 281 } else { |
284 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding | 282 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding |
285 // was not marked for SDCH processing: Why did the server suggest an SDCH | 283 // was not marked for SDCH processing: Why did the server suggest an SDCH |
286 // dictionary in the first place??. Also, the content isn't | 284 // dictionary in the first place??. Also, the content isn't |
287 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for | 285 // 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 | 286 // HTML: Did some anti-virus system strip this tag (sometimes they strip |
289 // accept-encoding headers on the request)?? Does the content encoding not | 287 // 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 | 288 // 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). | 289 // fixup to a binary file, but it probably really is text/html (some how). |
292 if (encoding_types->empty()) { | 290 if (encoding_types->empty()) { |
293 SdchManager::SdchErrorRecovery( | 291 filter_context.LogSdchProblem(SdchManager::BINARY_ADDED_CONTENT_ENCODING); |
294 SdchManager::BINARY_ADDED_CONTENT_ENCODING); | |
295 } else if (1 == encoding_types->size()) { | 292 } else if (1 == encoding_types->size()) { |
296 SdchManager::SdchErrorRecovery( | 293 filter_context.LogSdchProblem(SdchManager::BINARY_FIXED_CONTENT_ENCODING); |
297 SdchManager::BINARY_FIXED_CONTENT_ENCODING); | |
298 } else { | 294 } else { |
299 SdchManager::SdchErrorRecovery( | 295 filter_context.LogSdchProblem( |
300 SdchManager::BINARY_FIXED_CONTENT_ENCODINGS); | 296 SdchManager::BINARY_FIXED_CONTENT_ENCODINGS); |
301 } | 297 } |
302 } | 298 } |
303 | 299 |
304 // Leave the existing encoding type to be processed first, and add our | 300 // Leave the existing encoding type to be processed first, and add our |
305 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will | 301 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will |
306 // perform a second layer of gzip encoding atop the server's sdch,gzip | 302 // 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 | 303 // 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 | 304 // result we'll need (in that case) to do the gunzip, plus our tentative |
309 // gunzip and tentative SDCH decoding. | 305 // gunzip and tentative SDCH decoding. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 | 396 |
401 void Filter::PushDataIntoNextFilter() { | 397 void Filter::PushDataIntoNextFilter() { |
402 IOBuffer* next_buffer = next_filter_->stream_buffer(); | 398 IOBuffer* next_buffer = next_filter_->stream_buffer(); |
403 int next_size = next_filter_->stream_buffer_size(); | 399 int next_size = next_filter_->stream_buffer_size(); |
404 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); | 400 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); |
405 if (FILTER_ERROR != last_status_) | 401 if (FILTER_ERROR != last_status_) |
406 next_filter_->FlushStreamBuffer(next_size); | 402 next_filter_->FlushStreamBuffer(next_size); |
407 } | 403 } |
408 | 404 |
409 } // namespace net | 405 } // namespace net |
OLD | NEW |