OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
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 | |
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 | |
9 // in the chain proxies calls to ReadData() so that its return values | |
10 // apply to the entire chain. | |
11 // | |
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 | |
14 // except for the last filter, it proxies the call down the chain, | |
15 // filling in the input buffers of subsequent filters if needed (== | |
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 | |
18 // Filter will then output data if possible, and return | |
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 | |
21 // next filter needs it, a return value of FILTER_NEED_MORE_DATA from the | |
22 // final filter will apply to the entire chain. | |
23 | |
24 #include "net/filter/filter.h" | |
25 | |
26 #include "base/files/file_path.h" | |
27 #include "base/strings/string_util.h" | |
28 #include "net/base/filename_util_unsafe.h" | |
29 #include "net/base/io_buffer.h" | |
30 #include "net/base/mime_util.h" | |
31 #include "net/base/sdch_net_log_params.h" | |
32 #include "net/filter/gzip_filter.h" | |
33 #include "net/filter/sdch_filter.h" | |
34 #include "net/url_request/url_request_context.h" | |
35 #include "url/gurl.h" | |
36 | |
37 namespace net { | |
38 | |
39 namespace { | |
40 | |
41 // Filter types (using canonical lower case only): | |
42 const char kDeflate[] = "deflate"; | |
43 const char kGZip[] = "gzip"; | |
44 const char kXGZip[] = "x-gzip"; | |
45 const char kSdch[] = "sdch"; | |
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 | |
48 // more information, see Firefox's nsHttpChannel::ProcessNormal. | |
49 | |
50 // Mime types: | |
51 const char kApplicationXGzip[] = "application/x-gzip"; | |
52 const char kApplicationGzip[] = "application/gzip"; | |
53 const char kApplicationXGunzip[] = "application/x-gunzip"; | |
54 const char kTextHtml[] = "text/html"; | |
55 | |
56 // Buffer size allocated when de-compressing data. | |
57 const int kFilterBufSize = 32 * 1024; | |
58 | |
59 void LogSdchProblem(const FilterContext& filter_context, | |
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 | |
67 std::string FilterTypeAsString(Filter::FilterType type_id) { | |
68 switch (type_id) { | |
69 case Filter::FILTER_TYPE_DEFLATE: | |
70 return "FILTER_TYPE_DEFLATE"; | |
71 case Filter::FILTER_TYPE_GZIP: | |
72 return "FILTER_TYPE_GZIP"; | |
73 case Filter::FILTER_TYPE_GZIP_HELPING_SDCH: | |
74 return "FILTER_TYPE_GZIP_HELPING_SDCH"; | |
75 case Filter::FILTER_TYPE_SDCH: | |
76 return "FILTER_TYPE_SDCH"; | |
77 case Filter::FILTER_TYPE_SDCH_POSSIBLE : | |
78 return "FILTER_TYPE_SDCH_POSSIBLE "; | |
79 case Filter::FILTER_TYPE_UNSUPPORTED: | |
80 return "FILTER_TYPE_UNSUPPORTED"; | |
81 } | |
82 return ""; | |
83 } | |
84 | |
85 } // namespace | |
86 | |
87 FilterContext::~FilterContext() { | |
88 } | |
89 | |
90 Filter::~Filter() {} | |
91 | |
92 // static | |
93 Filter* Filter::Factory(const std::vector<FilterType>& filter_types, | |
94 const FilterContext& filter_context) { | |
95 if (filter_types.empty()) | |
96 return NULL; | |
97 | |
98 Filter* filter_list = NULL; // Linked list of filters. | |
99 for (size_t i = 0; i < filter_types.size(); i++) { | |
100 filter_list = PrependNewFilter(filter_types[i], filter_context, | |
101 kFilterBufSize, filter_list); | |
102 if (!filter_list) | |
103 return NULL; | |
104 } | |
105 return filter_list; | |
106 } | |
107 | |
108 // static | |
109 Filter* Filter::GZipFactory() { | |
110 return InitGZipFilter(FILTER_TYPE_GZIP, kFilterBufSize); | |
111 } | |
112 | |
113 // static | |
114 Filter* Filter::FactoryForTests(const std::vector<FilterType>& filter_types, | |
115 const FilterContext& filter_context, | |
116 int buffer_size) { | |
117 if (filter_types.empty()) | |
118 return NULL; | |
119 | |
120 Filter* filter_list = NULL; // Linked list of filters. | |
121 for (size_t i = 0; i < filter_types.size(); i++) { | |
122 filter_list = PrependNewFilter(filter_types[i], filter_context, | |
123 buffer_size, filter_list); | |
124 if (!filter_list) | |
125 return NULL; | |
126 } | |
127 return filter_list; | |
128 } | |
129 | |
130 Filter::FilterStatus Filter::ReadData(char* dest_buffer, int* dest_len) { | |
131 const int dest_buffer_capacity = *dest_len; | |
132 if (last_status_ == FILTER_ERROR) | |
133 return last_status_; | |
134 if (!next_filter_.get()) | |
135 return last_status_ = ReadFilteredData(dest_buffer, dest_len); | |
136 | |
137 // This filter needs more data, but it's not clear that the rest of | |
138 // the chain does; delegate the actual status return to the next filter. | |
139 if (last_status_ == FILTER_NEED_MORE_DATA && !stream_data_len()) | |
140 return next_filter_->ReadData(dest_buffer, dest_len); | |
141 | |
142 do { | |
143 if (next_filter_->last_status() == FILTER_NEED_MORE_DATA) { | |
144 PushDataIntoNextFilter(); | |
145 if (FILTER_ERROR == last_status_) | |
146 return FILTER_ERROR; | |
147 } | |
148 *dest_len = dest_buffer_capacity; // Reset the input/output parameter. | |
149 next_filter_->ReadData(dest_buffer, dest_len); | |
150 if (FILTER_NEED_MORE_DATA == last_status_) | |
151 return next_filter_->last_status(); | |
152 | |
153 // In the case where this filter has data internally, and is indicating such | |
154 // with a last_status_ of FILTER_OK, but at the same time the next filter in | |
155 // the chain indicated it FILTER_NEED_MORE_DATA, we have to be cautious | |
156 // about confusing the caller. The API confusion can appear if we return | |
157 // FILTER_OK (suggesting we have more data in aggregate), but yet we don't | |
158 // populate our output buffer. When that is the case, we need to | |
159 // alternately call our filter element, and the next_filter element until we | |
160 // get out of this state (by pumping data into the next filter until it | |
161 // outputs data, or it runs out of data and reports that it NEED_MORE_DATA.) | |
162 } while (FILTER_OK == last_status_ && | |
163 FILTER_NEED_MORE_DATA == next_filter_->last_status() && | |
164 0 == *dest_len); | |
165 | |
166 if (next_filter_->last_status() == FILTER_ERROR) | |
167 return FILTER_ERROR; | |
168 return FILTER_OK; | |
169 } | |
170 | |
171 bool Filter::FlushStreamBuffer(int stream_data_len) { | |
172 DCHECK_LE(stream_data_len, stream_buffer_size_); | |
173 if (stream_data_len <= 0 || stream_data_len > stream_buffer_size_) | |
174 return false; | |
175 | |
176 DCHECK(stream_buffer()); | |
177 // Bail out if there is more data in the stream buffer to be filtered. | |
178 if (!stream_buffer() || stream_data_len_) | |
179 return false; | |
180 | |
181 next_stream_data_ = stream_buffer()->data(); | |
182 stream_data_len_ = stream_data_len; | |
183 last_status_ = FILTER_OK; | |
184 return true; | |
185 } | |
186 | |
187 // static | |
188 Filter::FilterType Filter::ConvertEncodingToType( | |
189 const std::string& filter_type) { | |
190 FilterType type_id; | |
191 if (LowerCaseEqualsASCII(filter_type, kDeflate)) { | |
192 type_id = FILTER_TYPE_DEFLATE; | |
193 } else if (LowerCaseEqualsASCII(filter_type, kGZip) || | |
194 LowerCaseEqualsASCII(filter_type, kXGZip)) { | |
195 type_id = FILTER_TYPE_GZIP; | |
196 } else if (LowerCaseEqualsASCII(filter_type, kSdch)) { | |
197 type_id = FILTER_TYPE_SDCH; | |
198 } else { | |
199 // Note we also consider "identity" and "uncompressed" UNSUPPORTED as | |
200 // filter should be disabled in such cases. | |
201 type_id = FILTER_TYPE_UNSUPPORTED; | |
202 } | |
203 return type_id; | |
204 } | |
205 | |
206 // static | |
207 void Filter::FixupEncodingTypes( | |
208 const FilterContext& filter_context, | |
209 std::vector<FilterType>* encoding_types) { | |
210 std::string mime_type; | |
211 bool success = filter_context.GetMimeType(&mime_type); | |
212 DCHECK(success || mime_type.empty()); | |
213 | |
214 if ((1 == encoding_types->size()) && | |
215 (FILTER_TYPE_GZIP == encoding_types->front())) { | |
216 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || | |
217 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || | |
218 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) | |
219 // The server has told us that it sent us gziped content with a gzip | |
220 // content encoding. Sadly, Apache mistakenly sets these headers for all | |
221 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore | |
222 // the Content-Encoding here. | |
223 encoding_types->clear(); | |
224 | |
225 GURL url; | |
226 std::string disposition; | |
227 success = filter_context.GetURL(&url); | |
228 DCHECK(success); | |
229 filter_context.GetContentDisposition(&disposition); | |
230 // Don't supply a MIME type here, since that may cause disk IO. | |
231 base::FilePath::StringType extension = | |
232 GenerateFileExtensionUnsafe(url, disposition, "UTF-8", "", "", ""); | |
233 | |
234 if (filter_context.IsDownload()) { | |
235 // We don't want to decompress gzipped files when the user explicitly | |
236 // asks to download them. | |
237 // For the case of svgz files, we use the extension to distinguish | |
238 // between svgz files and svg files compressed with gzip by the server. | |
239 // When viewing a .svgz file, we need to uncompress it, but we don't | |
240 // want to do that when downloading. | |
241 // See Firefox's nonDecodableExtensions in nsExternalHelperAppService.cpp | |
242 if (EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || | |
243 LowerCaseEqualsASCII(extension, ".tgz") || | |
244 LowerCaseEqualsASCII(extension, ".svgz")) | |
245 encoding_types->clear(); | |
246 } else { | |
247 // When the user does not explicitly ask to download a file, if we get a | |
248 // supported mime type, then we attempt to decompress in order to view it. | |
249 // However, if it's not a supported mime type, then we will attempt to | |
250 // download it, and in that case, don't decompress .gz/.tgz files. | |
251 if ((EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || | |
252 LowerCaseEqualsASCII(extension, ".tgz")) && | |
253 !IsSupportedMimeType(mime_type)) | |
254 encoding_types->clear(); | |
255 } | |
256 } | |
257 | |
258 // If the request was for SDCH content, then we might need additional fixups. | |
259 if (!filter_context.SdchDictionariesAdvertised()) { | |
260 // It was not an SDCH request, so we'll just record stats. | |
261 if (1 < encoding_types->size()) { | |
262 // Multiple filters were intended to only be used for SDCH (thus far!) | |
263 LogSdchProblem(filter_context, SDCH_MULTIENCODING_FOR_NON_SDCH_REQUEST); | |
264 } | |
265 if ((1 == encoding_types->size()) && | |
266 (FILTER_TYPE_SDCH == encoding_types->front())) { | |
267 LogSdchProblem(filter_context, | |
268 SDCH_SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); | |
269 } | |
270 return; | |
271 } | |
272 | |
273 // The request was tagged as an SDCH request, which means the server supplied | |
274 // a dictionary, and we advertised it in the request. Some proxies will do | |
275 // very strange things to the request, or the response, so we have to handle | |
276 // them gracefully. | |
277 | |
278 // If content encoding included SDCH, then everything is "relatively" fine. | |
279 if (!encoding_types->empty() && | |
280 (FILTER_TYPE_SDCH == encoding_types->front())) { | |
281 // Some proxies (found currently in Argentina) strip the Content-Encoding | |
282 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed | |
283 // payload. To handle this gracefully, we simulate the "probably" deleted | |
284 // ",gzip" by appending a tentative gzip decode, which will default to a | |
285 // no-op pass through filter if it doesn't get gzip headers where expected. | |
286 if (1 == encoding_types->size()) { | |
287 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); | |
288 LogSdchProblem(filter_context, SDCH_OPTIONAL_GUNZIP_ENCODING_ADDED); | |
289 } | |
290 return; | |
291 } | |
292 | |
293 // There are now several cases to handle for an SDCH request. Foremost, if | |
294 // the outbound request was stripped so as not to advertise support for | |
295 // encodings, we might get back content with no encoding, or (for example) | |
296 // just gzip. We have to be sure that any changes we make allow for such | |
297 // minimal coding to work. That issue is why we use TENTATIVE filters if we | |
298 // add any, as those filters sniff the content, and act as pass-through | |
299 // filters if headers are not found. | |
300 | |
301 // If the outbound GET is not modified, then the server will generally try to | |
302 // send us SDCH encoded content. As that content returns, there are several | |
303 // corruptions of the header "content-encoding" that proxies may perform (and | |
304 // have been detected in the wild). We already dealt with the a honest | |
305 // content encoding of "sdch,gzip" being corrupted into "sdch" with on change | |
306 // of the actual content. Another common corruption is to either disscard | |
307 // the accurate content encoding, or to replace it with gzip only (again, with | |
308 // no change in actual content). The last observed corruption it to actually | |
309 // change the content, such as by re-gzipping it, and that may happen along | |
310 // with corruption of the stated content encoding (wow!). | |
311 | |
312 // The one unresolved failure mode comes when we advertise a dictionary, and | |
313 // the server tries to *send* a gzipped file (not gzip encode content), and | |
314 // then we could do a gzip decode :-(. Since SDCH is only (currently) | |
315 // supported server side on paths that only send HTML content, this mode has | |
316 // never surfaced in the wild (and is unlikely to). | |
317 // We will gather a lot of stats as we perform the fixups | |
318 if (StartsWithASCII(mime_type, kTextHtml, false)) { | |
319 // Suspicious case: Advertised dictionary, but server didn't use sdch, and | |
320 // we're HTML tagged. | |
321 if (encoding_types->empty()) { | |
322 LogSdchProblem(filter_context, SDCH_ADDED_CONTENT_ENCODING); | |
323 } else if (1 == encoding_types->size()) { | |
324 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODING); | |
325 } else { | |
326 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODINGS); | |
327 } | |
328 } else { | |
329 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding | |
330 // was not marked for SDCH processing: Why did the server suggest an SDCH | |
331 // dictionary in the first place??. Also, the content isn't | |
332 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for | |
333 // HTML: Did some anti-virus system strip this tag (sometimes they strip | |
334 // accept-encoding headers on the request)?? Does the content encoding not | |
335 // start with "text/html" for some other reason?? We'll report this as a | |
336 // fixup to a binary file, but it probably really is text/html (some how). | |
337 if (encoding_types->empty()) { | |
338 LogSdchProblem(filter_context, SDCH_BINARY_ADDED_CONTENT_ENCODING); | |
339 } else if (1 == encoding_types->size()) { | |
340 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODING); | |
341 } else { | |
342 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODINGS); | |
343 } | |
344 } | |
345 | |
346 // Leave the existing encoding type to be processed first, and add our | |
347 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will | |
348 // perform a second layer of gzip encoding atop the server's sdch,gzip | |
349 // encoding, and then claim that the content encoding is a mere gzip. As a | |
350 // result we'll need (in that case) to do the gunzip, plus our tentative | |
351 // gunzip and tentative SDCH decoding. | |
352 // This approach nicely handles the empty() list as well, and should work with | |
353 // other (as yet undiscovered) proxies the choose to re-compressed with some | |
354 // other encoding (such as bzip2, etc.). | |
355 encoding_types->insert(encoding_types->begin(), | |
356 FILTER_TYPE_GZIP_HELPING_SDCH); | |
357 encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE); | |
358 return; | |
359 } | |
360 | |
361 std::string Filter::OrderedFilterList() const { | |
362 if (next_filter_) { | |
363 return FilterTypeAsString(type_id_) + "," + | |
364 next_filter_->OrderedFilterList(); | |
365 } else { | |
366 return FilterTypeAsString(type_id_); | |
367 } | |
368 } | |
369 | |
370 Filter::Filter(FilterType type_id) | |
371 : stream_buffer_(NULL), | |
372 stream_buffer_size_(0), | |
373 next_stream_data_(NULL), | |
374 stream_data_len_(0), | |
375 last_status_(FILTER_NEED_MORE_DATA), | |
376 type_id_(type_id) {} | |
377 | |
378 Filter::FilterStatus Filter::CopyOut(char* dest_buffer, int* dest_len) { | |
379 int out_len; | |
380 int input_len = *dest_len; | |
381 *dest_len = 0; | |
382 | |
383 if (0 == stream_data_len_) | |
384 return Filter::FILTER_NEED_MORE_DATA; | |
385 | |
386 out_len = std::min(input_len, stream_data_len_); | |
387 memcpy(dest_buffer, next_stream_data_, out_len); | |
388 *dest_len += out_len; | |
389 stream_data_len_ -= out_len; | |
390 if (0 == stream_data_len_) { | |
391 next_stream_data_ = NULL; | |
392 return Filter::FILTER_NEED_MORE_DATA; | |
393 } else { | |
394 next_stream_data_ += out_len; | |
395 return Filter::FILTER_OK; | |
396 } | |
397 } | |
398 | |
399 // static | |
400 Filter* Filter::InitGZipFilter(FilterType type_id, int buffer_size) { | |
401 scoped_ptr<GZipFilter> gz_filter(new GZipFilter(type_id)); | |
402 gz_filter->InitBuffer(buffer_size); | |
403 return gz_filter->InitDecoding(type_id) ? gz_filter.release() : NULL; | |
404 } | |
405 | |
406 // static | |
407 Filter* Filter::InitSdchFilter(FilterType type_id, | |
408 const FilterContext& filter_context, | |
409 int buffer_size) { | |
410 scoped_ptr<SdchFilter> sdch_filter(new SdchFilter(type_id, filter_context)); | |
411 sdch_filter->InitBuffer(buffer_size); | |
412 return sdch_filter->InitDecoding(type_id) ? sdch_filter.release() : NULL; | |
413 } | |
414 | |
415 // static | |
416 Filter* Filter::PrependNewFilter(FilterType type_id, | |
417 const FilterContext& filter_context, | |
418 int buffer_size, | |
419 Filter* filter_list) { | |
420 scoped_ptr<Filter> first_filter; // Soon to be start of chain. | |
421 switch (type_id) { | |
422 case FILTER_TYPE_GZIP_HELPING_SDCH: | |
423 case FILTER_TYPE_DEFLATE: | |
424 case FILTER_TYPE_GZIP: | |
425 first_filter.reset(InitGZipFilter(type_id, buffer_size)); | |
426 break; | |
427 case FILTER_TYPE_SDCH: | |
428 case FILTER_TYPE_SDCH_POSSIBLE: | |
429 if (filter_context.GetURLRequestContext()->sdch_manager() && | |
430 SdchManager::sdch_enabled()) { | |
431 first_filter.reset( | |
432 InitSdchFilter(type_id, filter_context, buffer_size)); | |
433 } | |
434 break; | |
435 default: | |
436 break; | |
437 } | |
438 | |
439 if (!first_filter.get()) | |
440 return NULL; | |
441 | |
442 first_filter->next_filter_.reset(filter_list); | |
443 return first_filter.release(); | |
444 } | |
445 | |
446 void Filter::InitBuffer(int buffer_size) { | |
447 DCHECK(!stream_buffer()); | |
448 DCHECK_GT(buffer_size, 0); | |
449 stream_buffer_ = new IOBuffer(buffer_size); | |
450 stream_buffer_size_ = buffer_size; | |
451 } | |
452 | |
453 void Filter::PushDataIntoNextFilter() { | |
454 IOBuffer* next_buffer = next_filter_->stream_buffer(); | |
455 int next_size = next_filter_->stream_buffer_size(); | |
456 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); | |
457 if (FILTER_ERROR != last_status_) | |
458 next_filter_->FlushStreamBuffer(next_size); | |
459 } | |
460 | |
461 } // namespace net | |
OLD | NEW |