Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: net/filter/filter.cc

Issue 1662763002: [ON HOLD] Implement pull-based design for content decoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/filter/filter.h ('k') | net/filter/filter_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "base/values.h"
29 #include "net/base/io_buffer.h"
30 #include "net/base/sdch_net_log_params.h"
31 #include "net/filter/brotli_filter.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 kBrotli[] = "br";
43 const char kDeflate[] = "deflate";
44 const char kGZip[] = "gzip";
45 const char kXGZip[] = "x-gzip";
46 const char kSdch[] = "sdch";
47 // compress and x-compress are currently not supported. If we decide to support
48 // them, we'll need the same mime type compatibility hack we have for gzip. For
49 // more information, see Firefox's nsHttpChannel::ProcessNormal.
50
51 // Mime types:
52 const char kTextHtml[] = "text/html";
53
54 // Buffer size allocated when de-compressing data.
55 const int kFilterBufSize = 32 * 1024;
56
57 void LogSdchProblem(const FilterContext& filter_context,
58 SdchProblemCode problem) {
59 SdchManager::SdchErrorRecovery(problem);
60 filter_context.GetNetLog().AddEvent(
61 NetLog::TYPE_SDCH_DECODING_ERROR,
62 base::Bind(&NetLogSdchResourceProblemCallback, problem));
63 }
64
65 std::string FilterTypeAsString(Filter::FilterType type_id) {
66 switch (type_id) {
67 case Filter::FILTER_TYPE_BROTLI:
68 return "FILTER_TYPE_BROTLI";
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 case Filter::FILTER_TYPE_MAX:
82 return "FILTER_TYPE_MAX";
83 }
84 return "";
85 }
86
87 } // namespace
88
89 FilterContext::~FilterContext() {
90 }
91
92 Filter::~Filter() {}
93
94 // static
95 std::unique_ptr<Filter> Filter::Factory(
96 const std::vector<FilterType>& filter_types,
97 const FilterContext& filter_context) {
98 if (filter_types.empty())
99 return nullptr;
100
101 std::unique_ptr<Filter> filter_list = nullptr; // Linked list of filters.
102 for (size_t i = 0; i < filter_types.size(); i++) {
103 filter_list = PrependNewFilter(filter_types[i], filter_context,
104 kFilterBufSize, std::move(filter_list));
105 if (!filter_list)
106 return nullptr;
107 }
108 return filter_list;
109 }
110
111 // static
112 std::unique_ptr<Filter> Filter::GZipFactory() {
113 return InitGZipFilter(FILTER_TYPE_GZIP, kFilterBufSize);
114 }
115
116 // static
117 std::unique_ptr<Filter> Filter::FactoryForTests(
118 const std::vector<FilterType>& filter_types,
119 const FilterContext& filter_context,
120 int buffer_size) {
121 if (filter_types.empty())
122 return nullptr;
123
124 std::unique_ptr<Filter> filter_list; // Linked list of filters.
125 for (size_t i = 0; i < filter_types.size(); i++) {
126 filter_list = PrependNewFilter(filter_types[i], filter_context, buffer_size,
127 std::move(filter_list));
128 if (!filter_list)
129 return nullptr;
130 }
131 return filter_list;
132 }
133
134 Filter::FilterStatus Filter::ReadData(char* dest_buffer, int* dest_len) {
135 const int dest_buffer_capacity = *dest_len;
136 if (last_status_ == FILTER_ERROR)
137 return last_status_;
138 if (!next_filter_.get())
139 return last_status_ = ReadFilteredData(dest_buffer, dest_len);
140
141 // This filter needs more data, but it's not clear that the rest of
142 // the chain does; delegate the actual status return to the next filter.
143 if (last_status_ == FILTER_NEED_MORE_DATA && !stream_data_len()) {
144 last_status_ = next_filter_->ReadData(dest_buffer, dest_len);
145 return last_status_;
146 }
147
148 do {
149 if (next_filter_->last_status() == FILTER_NEED_MORE_DATA) {
150 PushDataIntoNextFilter();
151 if (FILTER_ERROR == last_status_) {
152 *dest_len = 0;
153 return FILTER_ERROR;
154 }
155 }
156 *dest_len = dest_buffer_capacity; // Reset the input/output parameter.
157
158 next_filter_->ReadData(dest_buffer, dest_len);
159 if (FILTER_NEED_MORE_DATA == last_status_) {
160 last_status_ = next_filter_->last_status();
161 return last_status_;
162 }
163
164 // In the case where this filter has data internally, and is indicating such
165 // with a last_status_ of FILTER_OK, but at the same time the next filter in
166 // the chain indicated it FILTER_NEED_MORE_DATA, we have to be cautious
167 // about confusing the caller. The API confusion can appear if we return
168 // FILTER_OK (suggesting we have more data in aggregate), but yet we don't
169 // populate our output buffer. When that is the case, we need to
170 // alternately call our filter element, and the next_filter element until we
171 // get out of this state (by pumping data into the next filter until it
172 // outputs data, or it runs out of data and reports that it NEED_MORE_DATA.)
173 } while (FILTER_OK == last_status_ &&
174 FILTER_NEED_MORE_DATA == next_filter_->last_status() &&
175 0 == *dest_len);
176
177 if (next_filter_->last_status() == FILTER_ERROR) {
178 last_status_ = FILTER_ERROR;
179 *dest_len = 0;
180 return FILTER_ERROR;
181 }
182 last_status_ = FILTER_OK;
183 return FILTER_OK;
184 }
185
186 bool Filter::FlushStreamBuffer(int stream_data_len) {
187 DCHECK_LE(stream_data_len, stream_buffer_size_);
188 if (stream_data_len <= 0 || stream_data_len > stream_buffer_size_)
189 return false;
190
191 DCHECK(stream_buffer());
192 // Bail out if there is more data in the stream buffer to be filtered.
193 if (!stream_buffer() || stream_data_len_)
194 return false;
195
196 next_stream_data_ = stream_buffer()->data();
197 stream_data_len_ = stream_data_len;
198 last_status_ = FILTER_OK;
199 return true;
200 }
201
202 // static
203 Filter::FilterType Filter::ConvertEncodingToType(
204 const std::string& filter_type) {
205 FilterType type_id;
206 if (base::LowerCaseEqualsASCII(filter_type, kBrotli)) {
207 type_id = FILTER_TYPE_BROTLI;
208 } else if (base::LowerCaseEqualsASCII(filter_type, kDeflate)) {
209 type_id = FILTER_TYPE_DEFLATE;
210 } else if (base::LowerCaseEqualsASCII(filter_type, kGZip) ||
211 base::LowerCaseEqualsASCII(filter_type, kXGZip)) {
212 type_id = FILTER_TYPE_GZIP;
213 } else if (base::LowerCaseEqualsASCII(filter_type, kSdch)) {
214 type_id = FILTER_TYPE_SDCH;
215 } else {
216 // Note we also consider "identity" and "uncompressed" UNSUPPORTED as
217 // filter should be disabled in such cases.
218 type_id = FILTER_TYPE_UNSUPPORTED;
219 }
220 return type_id;
221 }
222
223 // static
224 void Filter::FixupEncodingTypes(
225 const FilterContext& filter_context,
226 std::vector<FilterType>* encoding_types) {
227 std::string mime_type;
228 bool success = filter_context.GetMimeType(&mime_type);
229 DCHECK(success || mime_type.empty());
230
231 // If the request was for SDCH content, then we might need additional fixups.
232 if (!filter_context.SdchDictionariesAdvertised()) {
233 // It was not an SDCH request, so we'll just record stats.
234 if (1 < encoding_types->size()) {
235 // Multiple filters were intended to only be used for SDCH (thus far!)
236 LogSdchProblem(filter_context, SDCH_MULTIENCODING_FOR_NON_SDCH_REQUEST);
237 }
238 if ((1 == encoding_types->size()) &&
239 (FILTER_TYPE_SDCH == encoding_types->front())) {
240 LogSdchProblem(filter_context,
241 SDCH_SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST);
242 }
243 return;
244 }
245
246 // The request was tagged as an SDCH request, which means the server supplied
247 // a dictionary, and we advertised it in the request. Some proxies will do
248 // very strange things to the request, or the response, so we have to handle
249 // them gracefully.
250
251 // If content encoding included SDCH, then everything is "relatively" fine.
252 if (!encoding_types->empty() &&
253 (FILTER_TYPE_SDCH == encoding_types->front())) {
254 // Some proxies (found currently in Argentina) strip the Content-Encoding
255 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed
256 // payload. To handle this gracefully, we simulate the "probably" deleted
257 // ",gzip" by appending a tentative gzip decode, which will default to a
258 // no-op pass through filter if it doesn't get gzip headers where expected.
259 if (1 == encoding_types->size()) {
260 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH);
261 LogSdchProblem(filter_context, SDCH_OPTIONAL_GUNZIP_ENCODING_ADDED);
262 }
263 return;
264 }
265
266 // There are now several cases to handle for an SDCH request. Foremost, if
267 // the outbound request was stripped so as not to advertise support for
268 // encodings, we might get back content with no encoding, or (for example)
269 // just gzip. We have to be sure that any changes we make allow for such
270 // minimal coding to work. That issue is why we use TENTATIVE filters if we
271 // add any, as those filters sniff the content, and act as pass-through
272 // filters if headers are not found.
273
274 // If the outbound GET is not modified, then the server will generally try to
275 // send us SDCH encoded content. As that content returns, there are several
276 // corruptions of the header "content-encoding" that proxies may perform (and
277 // have been detected in the wild). We already dealt with the a honest
278 // content encoding of "sdch,gzip" being corrupted into "sdch" with on change
279 // of the actual content. Another common corruption is to either disscard
280 // the accurate content encoding, or to replace it with gzip only (again, with
281 // no change in actual content). The last observed corruption it to actually
282 // change the content, such as by re-gzipping it, and that may happen along
283 // with corruption of the stated content encoding (wow!).
284
285 // The one unresolved failure mode comes when we advertise a dictionary, and
286 // the server tries to *send* a gzipped file (not gzip encode content), and
287 // then we could do a gzip decode :-(. Since SDCH is only (currently)
288 // supported server side on paths that only send HTML content, this mode has
289 // never surfaced in the wild (and is unlikely to).
290 // We will gather a lot of stats as we perform the fixups
291 if (base::StartsWith(mime_type, kTextHtml,
292 base::CompareCase::INSENSITIVE_ASCII)) {
293 // Suspicious case: Advertised dictionary, but server didn't use sdch, and
294 // we're HTML tagged.
295 if (encoding_types->empty()) {
296 LogSdchProblem(filter_context, SDCH_ADDED_CONTENT_ENCODING);
297 } else if (1 == encoding_types->size()) {
298 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODING);
299 } else {
300 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODINGS);
301 }
302 } else {
303 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding
304 // was not marked for SDCH processing: Why did the server suggest an SDCH
305 // dictionary in the first place??. Also, the content isn't
306 // tagged as HTML, despite the fact that SDCH encoding is mostly likely for
307 // HTML: Did some anti-virus system strip this tag (sometimes they strip
308 // accept-encoding headers on the request)?? Does the content encoding not
309 // start with "text/html" for some other reason?? We'll report this as a
310 // fixup to a binary file, but it probably really is text/html (some how).
311 if (encoding_types->empty()) {
312 LogSdchProblem(filter_context, SDCH_BINARY_ADDED_CONTENT_ENCODING);
313 } else if (1 == encoding_types->size()) {
314 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODING);
315 } else {
316 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODINGS);
317 }
318 }
319
320 // Leave the existing encoding type to be processed first, and add our
321 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will
322 // perform a second layer of gzip encoding atop the server's sdch,gzip
323 // encoding, and then claim that the content encoding is a mere gzip. As a
324 // result we'll need (in that case) to do the gunzip, plus our tentative
325 // gunzip and tentative SDCH decoding.
326 // This approach nicely handles the empty() list as well, and should work with
327 // other (as yet undiscovered) proxies the choose to re-compressed with some
328 // other encoding (such as bzip2, etc.).
329 encoding_types->insert(encoding_types->begin(),
330 FILTER_TYPE_GZIP_HELPING_SDCH);
331 encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE);
332 return;
333 }
334
335 std::string Filter::OrderedFilterList() const {
336 if (next_filter_) {
337 return FilterTypeAsString(type_id_) + "," +
338 next_filter_->OrderedFilterList();
339 } else {
340 return FilterTypeAsString(type_id_);
341 }
342 }
343
344 Filter::Filter(FilterType type_id)
345 : stream_buffer_(nullptr),
346 stream_buffer_size_(0),
347 next_stream_data_(nullptr),
348 stream_data_len_(0),
349 last_status_(FILTER_NEED_MORE_DATA),
350 type_id_(type_id) {}
351
352 Filter::FilterStatus Filter::CopyOut(char* dest_buffer, int* dest_len) {
353 int out_len;
354 int input_len = *dest_len;
355 *dest_len = 0;
356
357 if (0 == stream_data_len_)
358 return Filter::FILTER_NEED_MORE_DATA;
359
360 out_len = std::min(input_len, stream_data_len_);
361 memcpy(dest_buffer, next_stream_data_, out_len);
362 *dest_len += out_len;
363 stream_data_len_ -= out_len;
364 if (0 == stream_data_len_) {
365 next_stream_data_ = nullptr;
366 return Filter::FILTER_NEED_MORE_DATA;
367 } else {
368 next_stream_data_ += out_len;
369 return Filter::FILTER_OK;
370 }
371 }
372
373 // static
374 std::unique_ptr<Filter> Filter::InitBrotliFilter(FilterType type_id,
375 int buffer_size) {
376 std::unique_ptr<Filter> brotli_filter(CreateBrotliFilter(type_id));
377 if (!brotli_filter.get())
378 return nullptr;
379
380 brotli_filter->InitBuffer(buffer_size);
381 return brotli_filter;
382 }
383
384 // static
385 std::unique_ptr<Filter> Filter::InitGZipFilter(FilterType type_id,
386 int buffer_size) {
387 std::unique_ptr<GZipFilter> gz_filter(new GZipFilter(type_id));
388 gz_filter->InitBuffer(buffer_size);
389 return gz_filter->InitDecoding(type_id) ? std::move(gz_filter) : nullptr;
390 }
391
392 // static
393 std::unique_ptr<Filter> Filter::InitSdchFilter(
394 FilterType type_id,
395 const FilterContext& filter_context,
396 int buffer_size) {
397 std::unique_ptr<SdchFilter> sdch_filter(
398 new SdchFilter(type_id, filter_context));
399 sdch_filter->InitBuffer(buffer_size);
400 return sdch_filter->InitDecoding(type_id) ? std::move(sdch_filter) : nullptr;
401 }
402
403 // static
404 std::unique_ptr<Filter> Filter::PrependNewFilter(
405 FilterType type_id,
406 const FilterContext& filter_context,
407 int buffer_size,
408 std::unique_ptr<Filter> filter_list) {
409 std::unique_ptr<Filter> first_filter; // Soon to be start of chain.
410 switch (type_id) {
411 case FILTER_TYPE_BROTLI:
412 first_filter = InitBrotliFilter(type_id, buffer_size);
413 break;
414 case FILTER_TYPE_GZIP_HELPING_SDCH:
415 case FILTER_TYPE_DEFLATE:
416 case FILTER_TYPE_GZIP:
417 first_filter = InitGZipFilter(type_id, buffer_size);
418 break;
419 case FILTER_TYPE_SDCH:
420 case FILTER_TYPE_SDCH_POSSIBLE:
421 if (filter_context.GetURLRequestContext()->sdch_manager()) {
422 first_filter = InitSdchFilter(type_id, filter_context, buffer_size);
423 }
424 break;
425 default:
426 break;
427 }
428
429 if (!first_filter.get())
430 return nullptr;
431
432 first_filter->next_filter_ = std::move(filter_list);
433 return first_filter;
434 }
435
436 void Filter::InitBuffer(int buffer_size) {
437 DCHECK(!stream_buffer());
438 DCHECK_GT(buffer_size, 0);
439 stream_buffer_ = new IOBuffer(buffer_size);
440 stream_buffer_size_ = buffer_size;
441 }
442
443 void Filter::PushDataIntoNextFilter() {
444 IOBuffer* next_buffer = next_filter_->stream_buffer();
445 int next_size = next_filter_->stream_buffer_size();
446 last_status_ = ReadFilteredData(next_buffer->data(), &next_size);
447 if (FILTER_ERROR != last_status_)
448 next_filter_->FlushStreamBuffer(next_size);
449 }
450
451 } // namespace net
OLDNEW
« no previous file with comments | « net/filter/filter.h ('k') | net/filter/filter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698