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. |
11 // | 11 // |
12 // In a filter chain, the data flows from first filter (held by the | 12 // In a filter chain, the data flows from first filter (held by the |
13 // caller) down the chain. When ReadData() is called on any filter | 13 // caller) down the chain. When ReadData() is called on any filter |
14 // except for the last filter, it proxies the call down the chain, | 14 // except for the last filter, it proxies the call down the chain, |
15 // filling in the input buffers of subsequent filters if needed (== | 15 // filling in the input buffers of subsequent filters if needed (== |
16 // that filter's last_status() value is FILTER_NEED_MORE_DATA) and | 16 // that filter's last_status() value is FILTER_NEED_MORE_DATA) and |
17 // available (== the current filter has data it can output). The last | 17 // available (== the current filter has data it can output). The last |
18 // Filter will then output data if possible, and return | 18 // Filter will then output data if possible, and return |
19 // FILTER_NEED_MORE_DATA if not. Because the indirection pushes | 19 // FILTER_NEED_MORE_DATA if not. Because the indirection pushes |
20 // data along the filter chain at each level if it's available and the | 20 // data along the filter chain at each level if it's available and the |
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" | 31 #include "net/base/sdch_net_log_params.h" |
32 #include "net/filter/gzip_filter.h" | 32 #include "net/filter/gzip_filter.h" |
33 #include "net/filter/sdch_filter.h" | 33 #include "net/filter/sdch_filter.h" |
34 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
35 #include "url/gurl.h" | 35 #include "url/gurl.h" |
36 | 36 |
37 namespace net { | 37 namespace net { |
38 | 38 |
39 namespace { | 39 namespace { |
40 | 40 |
41 // Filter types (using canonical lower case only): | 41 // Filter types (using canonical lower case only): |
42 const char kDeflate[] = "deflate"; | 42 const char kDeflate[] = "deflate"; |
43 const char kGZip[] = "gzip"; | 43 const char kGZip[] = "gzip"; |
44 const char kXGZip[] = "x-gzip"; | 44 const char kXGZip[] = "x-gzip"; |
45 const char kSdch[] = "sdch"; | 45 const char kSdch[] = "sdch"; |
46 // compress and x-compress are currently not supported. If we decide to support | 46 // 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 | 47 // them, we'll need the same mime type compatibility hack we have for gzip. For |
48 // more information, see Firefox's nsHttpChannel::ProcessNormal. | 48 // more information, see Firefox's nsHttpChannel::ProcessNormal. |
49 | 49 |
50 // Mime types: | 50 // Mime types: |
51 const char kApplicationXGzip[] = "application/x-gzip"; | 51 const char kApplicationXGzip[] = "application/x-gzip"; |
52 const char kApplicationGzip[] = "application/gzip"; | 52 const char kApplicationGzip[] = "application/gzip"; |
53 const char kApplicationXGunzip[] = "application/x-gunzip"; | 53 const char kApplicationXGunzip[] = "application/x-gunzip"; |
54 const char kTextHtml[] = "text/html"; | 54 const char kTextHtml[] = "text/html"; |
55 | 55 |
56 // Buffer size allocated when de-compressing data. | 56 // Buffer size allocated when de-compressing data. |
57 const int kFilterBufSize = 32 * 1024; | 57 const int kFilterBufSize = 32 * 1024; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 return FILTER_ERROR; | 128 return FILTER_ERROR; |
129 } | 129 } |
130 *dest_len = dest_buffer_capacity; // Reset the input/output parameter. | 130 *dest_len = dest_buffer_capacity; // Reset the input/output parameter. |
131 next_filter_->ReadData(dest_buffer, dest_len); | 131 next_filter_->ReadData(dest_buffer, dest_len); |
132 if (FILTER_NEED_MORE_DATA == last_status_) | 132 if (FILTER_NEED_MORE_DATA == last_status_) |
133 return next_filter_->last_status(); | 133 return next_filter_->last_status(); |
134 | 134 |
135 // In the case where this filter has data internally, and is indicating such | 135 // In the case where this filter has data internally, and is indicating such |
136 // with a last_status_ of FILTER_OK, but at the same time the next filter in | 136 // with a last_status_ of FILTER_OK, but at the same time the next filter in |
137 // the chain indicated it FILTER_NEED_MORE_DATA, we have to be cautious | 137 // the chain indicated it FILTER_NEED_MORE_DATA, we have to be cautious |
138 // about confusing the caller. The API confusion can appear if we return | 138 // about confusing the caller. The API confusion can appear if we return |
139 // FILTER_OK (suggesting we have more data in aggregate), but yet we don't | 139 // FILTER_OK (suggesting we have more data in aggregate), but yet we don't |
140 // populate our output buffer. When that is the case, we need to | 140 // populate our output buffer. When that is the case, we need to |
141 // alternately call our filter element, and the next_filter element until we | 141 // alternately call our filter element, and the next_filter element until we |
142 // get out of this state (by pumping data into the next filter until it | 142 // get out of this state (by pumping data into the next filter until it |
143 // outputs data, or it runs out of data and reports that it NEED_MORE_DATA.) | 143 // outputs data, or it runs out of data and reports that it NEED_MORE_DATA.) |
144 } while (FILTER_OK == last_status_ && | 144 } while (FILTER_OK == last_status_ && |
145 FILTER_NEED_MORE_DATA == next_filter_->last_status() && | 145 FILTER_NEED_MORE_DATA == next_filter_->last_status() && |
146 0 == *dest_len); | 146 0 == *dest_len); |
147 | 147 |
148 if (next_filter_->last_status() == FILTER_ERROR) | 148 if (next_filter_->last_status() == FILTER_ERROR) |
149 return FILTER_ERROR; | 149 return FILTER_ERROR; |
150 return FILTER_OK; | 150 return FILTER_OK; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 std::string mime_type; | 192 std::string mime_type; |
193 bool success = filter_context.GetMimeType(&mime_type); | 193 bool success = filter_context.GetMimeType(&mime_type); |
194 DCHECK(success || mime_type.empty()); | 194 DCHECK(success || mime_type.empty()); |
195 | 195 |
196 if ((1 == encoding_types->size()) && | 196 if ((1 == encoding_types->size()) && |
197 (FILTER_TYPE_GZIP == encoding_types->front())) { | 197 (FILTER_TYPE_GZIP == encoding_types->front())) { |
198 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || | 198 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || |
199 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || | 199 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || |
200 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) | 200 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) |
201 // The server has told us that it sent us gziped content with a gzip | 201 // The server has told us that it sent us gziped content with a gzip |
202 // content encoding. Sadly, Apache mistakenly sets these headers for all | 202 // content encoding. Sadly, Apache mistakenly sets these headers for all |
203 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore | 203 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore |
204 // the Content-Encoding here. | 204 // the Content-Encoding here. |
205 encoding_types->clear(); | 205 encoding_types->clear(); |
206 | 206 |
207 GURL url; | 207 GURL url; |
208 std::string disposition; | 208 std::string disposition; |
209 success = filter_context.GetURL(&url); | 209 success = filter_context.GetURL(&url); |
210 DCHECK(success); | 210 DCHECK(success); |
211 filter_context.GetContentDisposition(&disposition); | 211 filter_context.GetContentDisposition(&disposition); |
212 // Don't supply a MIME type here, since that may cause disk IO. | 212 // Don't supply a MIME type here, since that may cause disk IO. |
213 base::FilePath::StringType extension = | 213 base::FilePath::StringType extension = |
(...skipping 17 matching lines...) Expand all Loading... |
231 // However, if it's not a supported mime type, then we will attempt to | 231 // However, if it's not a supported mime type, then we will attempt to |
232 // download it, and in that case, don't decompress .gz/.tgz files. | 232 // download it, and in that case, don't decompress .gz/.tgz files. |
233 if ((EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || | 233 if ((EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || |
234 LowerCaseEqualsASCII(extension, ".tgz")) && | 234 LowerCaseEqualsASCII(extension, ".tgz")) && |
235 !IsSupportedMimeType(mime_type)) | 235 !IsSupportedMimeType(mime_type)) |
236 encoding_types->clear(); | 236 encoding_types->clear(); |
237 } | 237 } |
238 } | 238 } |
239 | 239 |
240 // If the request was for SDCH content, then we might need additional fixups. | 240 // If the request was for SDCH content, then we might need additional fixups. |
241 if (!filter_context.SdchResponseExpected()) { | 241 if (!filter_context.SdchDictionariesAdvertised()) { |
242 // It was not an SDCH request, so we'll just record stats. | 242 // It was not an SDCH request, so we'll just record stats. |
243 if (1 < encoding_types->size()) { | 243 if (1 < encoding_types->size()) { |
244 // Multiple filters were intended to only be used for SDCH (thus far!) | 244 // Multiple filters were intended to only be used for SDCH (thus far!) |
245 LogSdchProblem(filter_context, SDCH_MULTIENCODING_FOR_NON_SDCH_REQUEST); | 245 LogSdchProblem(filter_context, SDCH_MULTIENCODING_FOR_NON_SDCH_REQUEST); |
246 } | 246 } |
247 if ((1 == encoding_types->size()) && | 247 if ((1 == encoding_types->size()) && |
248 (FILTER_TYPE_SDCH == encoding_types->front())) { | 248 (FILTER_TYPE_SDCH == encoding_types->front())) { |
249 LogSdchProblem(filter_context, | 249 LogSdchProblem(filter_context, |
250 SDCH_SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); | 250 SDCH_SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); |
251 } | 251 } |
252 return; | 252 return; |
253 } | 253 } |
254 | 254 |
255 // The request was tagged as an SDCH request, which means the server supplied | 255 // 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 | 256 // 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 | 257 // very strange things to the request, or the response, so we have to handle |
258 // them gracefully. | 258 // them gracefully. |
259 | 259 |
260 // If content encoding included SDCH, then everything is "relatively" fine. | 260 // If content encoding included SDCH, then everything is "relatively" fine. |
261 if (!encoding_types->empty() && | 261 if (!encoding_types->empty() && |
262 (FILTER_TYPE_SDCH == encoding_types->front())) { | 262 (FILTER_TYPE_SDCH == encoding_types->front())) { |
263 // Some proxies (found currently in Argentina) strip the Content-Encoding | 263 // Some proxies (found currently in Argentina) strip the Content-Encoding |
264 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed | 264 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed |
265 // payload. To handle this gracefully, we simulate the "probably" deleted | 265 // payload. To handle this gracefully, we simulate the "probably" deleted |
266 // ",gzip" by appending a tentative gzip decode, which will default to a | 266 // ",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. | 267 // no-op pass through filter if it doesn't get gzip headers where expected. |
268 if (1 == encoding_types->size()) { | 268 if (1 == encoding_types->size()) { |
269 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); | 269 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); |
270 LogSdchProblem(filter_context, SDCH_OPTIONAL_GUNZIP_ENCODING_ADDED); | 270 LogSdchProblem(filter_context, SDCH_OPTIONAL_GUNZIP_ENCODING_ADDED); |
271 } | 271 } |
272 return; | 272 return; |
273 } | 273 } |
274 | 274 |
275 // There are now several cases to handle for an SDCH request. Foremost, if | 275 // 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 | 276 // 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) | 277 // 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 | 278 // 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 | 279 // 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 | 280 // add any, as those filters sniff the content, and act as pass-through |
281 // filters if headers are not found. | 281 // filters if headers are not found. |
282 | 282 |
283 // If the outbound GET is not modified, then the server will generally try to | 283 // If the outbound GET is not modified, then the server will generally try to |
284 // send us SDCH encoded content. As that content returns, there are several | 284 // send us SDCH encoded content. As that content returns, there are several |
285 // corruptions of the header "content-encoding" that proxies may perform (and | 285 // corruptions of the header "content-encoding" that proxies may perform (and |
286 // have been detected in the wild). We already dealt with the a honest | 286 // have been detected in the wild). We already dealt with the a honest |
287 // content encoding of "sdch,gzip" being corrupted into "sdch" with on change | 287 // content encoding of "sdch,gzip" being corrupted into "sdch" with on change |
288 // of the actual content. Another common corruption is to either disscard | 288 // of the actual content. Another common corruption is to either disscard |
289 // the accurate content encoding, or to replace it with gzip only (again, with | 289 // the accurate content encoding, or to replace it with gzip only (again, with |
290 // no change in actual content). The last observed corruption it to actually | 290 // no change in actual content). The last observed corruption it to actually |
291 // change the content, such as by re-gzipping it, and that may happen along | 291 // change the content, such as by re-gzipping it, and that may happen along |
292 // with corruption of the stated content encoding (wow!). | 292 // with corruption of the stated content encoding (wow!). |
293 | 293 |
294 // The one unresolved failure mode comes when we advertise a dictionary, and | 294 // 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 | 295 // 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) | 296 // 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 | 297 // supported server side on paths that only send HTML content, this mode has |
298 // never surfaced in the wild (and is unlikely to). | 298 // never surfaced in the wild (and is unlikely to). |
299 // We will gather a lot of stats as we perform the fixups | 299 // We will gather a lot of stats as we perform the fixups |
300 if (StartsWithASCII(mime_type, kTextHtml, false)) { | 300 if (StartsWithASCII(mime_type, kTextHtml, false)) { |
301 // Suspicious case: Advertised dictionary, but server didn't use sdch, and | 301 // Suspicious case: Advertised dictionary, but server didn't use sdch, and |
302 // we're HTML tagged. | 302 // we're HTML tagged. |
303 if (encoding_types->empty()) { | 303 if (encoding_types->empty()) { |
304 LogSdchProblem(filter_context, SDCH_ADDED_CONTENT_ENCODING); | 304 LogSdchProblem(filter_context, SDCH_ADDED_CONTENT_ENCODING); |
305 } else if (1 == encoding_types->size()) { | 305 } else if (1 == encoding_types->size()) { |
306 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODING); | 306 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODING); |
307 } else { | 307 } else { |
308 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODINGS); | 308 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODINGS); |
309 } | 309 } |
310 } else { | 310 } else { |
311 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding | 311 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding |
312 // was not marked for SDCH processing: Why did the server suggest an SDCH | 312 // was not marked for SDCH processing: Why did the server suggest an SDCH |
313 // dictionary in the first place??. Also, the content isn't | 313 // dictionary in the first place??. Also, the content isn't |
314 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for | 314 // 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 | 315 // HTML: Did some anti-virus system strip this tag (sometimes they strip |
316 // accept-encoding headers on the request)?? Does the content encoding not | 316 // 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 | 317 // 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). | 318 // fixup to a binary file, but it probably really is text/html (some how). |
319 if (encoding_types->empty()) { | 319 if (encoding_types->empty()) { |
320 LogSdchProblem(filter_context, SDCH_BINARY_ADDED_CONTENT_ENCODING); | 320 LogSdchProblem(filter_context, SDCH_BINARY_ADDED_CONTENT_ENCODING); |
321 } else if (1 == encoding_types->size()) { | 321 } else if (1 == encoding_types->size()) { |
322 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODING); | 322 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODING); |
323 } else { | 323 } else { |
324 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODINGS); | 324 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODINGS); |
325 } | 325 } |
326 } | 326 } |
327 | 327 |
328 // Leave the existing encoding type to be processed first, and add our | 328 // Leave the existing encoding type to be processed first, and add our |
329 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will | 329 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will |
330 // perform a second layer of gzip encoding atop the server's sdch,gzip | 330 // 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 | 331 // 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 | 332 // result we'll need (in that case) to do the gunzip, plus our tentative |
333 // gunzip and tentative SDCH decoding. | 333 // gunzip and tentative SDCH decoding. |
334 // This approach nicely handles the empty() list as well, and should work with | 334 // This approach nicely handles the empty() list as well, and should work with |
335 // other (as yet undiscovered) proxies the choose to re-compressed with some | 335 // other (as yet undiscovered) proxies the choose to re-compressed with some |
336 // other encoding (such as bzip2, etc.). | 336 // other encoding (such as bzip2, etc.). |
337 encoding_types->insert(encoding_types->begin(), | 337 encoding_types->insert(encoding_types->begin(), |
338 FILTER_TYPE_GZIP_HELPING_SDCH); | 338 FILTER_TYPE_GZIP_HELPING_SDCH); |
339 encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE); | 339 encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE); |
340 return; | 340 return; |
341 } | 341 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 | 424 |
425 void Filter::PushDataIntoNextFilter() { | 425 void Filter::PushDataIntoNextFilter() { |
426 IOBuffer* next_buffer = next_filter_->stream_buffer(); | 426 IOBuffer* next_buffer = next_filter_->stream_buffer(); |
427 int next_size = next_filter_->stream_buffer_size(); | 427 int next_size = next_filter_->stream_buffer_size(); |
428 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); | 428 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); |
429 if (FILTER_ERROR != last_status_) | 429 if (FILTER_ERROR != last_status_) |
430 next_filter_->FlushStreamBuffer(next_size); | 430 next_filter_->FlushStreamBuffer(next_size); |
431 } | 431 } |
432 | 432 |
433 } // namespace net | 433 } // namespace net |
OLD | NEW |