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

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

Issue 754433003: Update from https://crrev.com/305340 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years 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
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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 return FILTER_ERROR; 146 return FILTER_ERROR;
147 } 147 }
148 *dest_len = dest_buffer_capacity; // Reset the input/output parameter. 148 *dest_len = dest_buffer_capacity; // Reset the input/output parameter.
149 next_filter_->ReadData(dest_buffer, dest_len); 149 next_filter_->ReadData(dest_buffer, dest_len);
150 if (FILTER_NEED_MORE_DATA == last_status_) 150 if (FILTER_NEED_MORE_DATA == last_status_)
151 return next_filter_->last_status(); 151 return next_filter_->last_status();
152 152
153 // In the case where this filter has data internally, and is indicating such 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 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 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 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 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 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 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 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.) 161 // outputs data, or it runs out of data and reports that it NEED_MORE_DATA.)
162 } while (FILTER_OK == last_status_ && 162 } while (FILTER_OK == last_status_ &&
163 FILTER_NEED_MORE_DATA == next_filter_->last_status() && 163 FILTER_NEED_MORE_DATA == next_filter_->last_status() &&
164 0 == *dest_len); 164 0 == *dest_len);
165 165
166 if (next_filter_->last_status() == FILTER_ERROR) 166 if (next_filter_->last_status() == FILTER_ERROR)
167 return FILTER_ERROR; 167 return FILTER_ERROR;
168 return FILTER_OK; 168 return FILTER_OK;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 std::string mime_type; 210 std::string mime_type;
211 bool success = filter_context.GetMimeType(&mime_type); 211 bool success = filter_context.GetMimeType(&mime_type);
212 DCHECK(success || mime_type.empty()); 212 DCHECK(success || mime_type.empty());
213 213
214 if ((1 == encoding_types->size()) && 214 if ((1 == encoding_types->size()) &&
215 (FILTER_TYPE_GZIP == encoding_types->front())) { 215 (FILTER_TYPE_GZIP == encoding_types->front())) {
216 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) || 216 if (LowerCaseEqualsASCII(mime_type, kApplicationXGzip) ||
217 LowerCaseEqualsASCII(mime_type, kApplicationGzip) || 217 LowerCaseEqualsASCII(mime_type, kApplicationGzip) ||
218 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip)) 218 LowerCaseEqualsASCII(mime_type, kApplicationXGunzip))
219 // The server has told us that it sent us gziped content with a gzip 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 220 // content encoding. Sadly, Apache mistakenly sets these headers for all
221 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore 221 // .gz files. We match Firefox's nsHttpChannel::ProcessNormal and ignore
222 // the Content-Encoding here. 222 // the Content-Encoding here.
223 encoding_types->clear(); 223 encoding_types->clear();
224 224
225 GURL url; 225 GURL url;
226 std::string disposition; 226 std::string disposition;
227 success = filter_context.GetURL(&url); 227 success = filter_context.GetURL(&url);
228 DCHECK(success); 228 DCHECK(success);
229 filter_context.GetContentDisposition(&disposition); 229 filter_context.GetContentDisposition(&disposition);
230 // Don't supply a MIME type here, since that may cause disk IO. 230 // Don't supply a MIME type here, since that may cause disk IO.
231 base::FilePath::StringType extension = 231 base::FilePath::StringType extension =
(...skipping 17 matching lines...) Expand all
249 // However, if it's not a supported mime type, then we will attempt to 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. 250 // download it, and in that case, don't decompress .gz/.tgz files.
251 if ((EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) || 251 if ((EndsWith(extension, FILE_PATH_LITERAL(".gz"), false) ||
252 LowerCaseEqualsASCII(extension, ".tgz")) && 252 LowerCaseEqualsASCII(extension, ".tgz")) &&
253 !IsSupportedMimeType(mime_type)) 253 !IsSupportedMimeType(mime_type))
254 encoding_types->clear(); 254 encoding_types->clear();
255 } 255 }
256 } 256 }
257 257
258 // If the request was for SDCH content, then we might need additional fixups. 258 // If the request was for SDCH content, then we might need additional fixups.
259 if (!filter_context.SdchResponseExpected()) { 259 if (!filter_context.SdchDictionariesAdvertised()) {
260 // It was not an SDCH request, so we'll just record stats. 260 // It was not an SDCH request, so we'll just record stats.
261 if (1 < encoding_types->size()) { 261 if (1 < encoding_types->size()) {
262 // Multiple filters were intended to only be used for SDCH (thus far!) 262 // Multiple filters were intended to only be used for SDCH (thus far!)
263 LogSdchProblem(filter_context, SDCH_MULTIENCODING_FOR_NON_SDCH_REQUEST); 263 LogSdchProblem(filter_context, SDCH_MULTIENCODING_FOR_NON_SDCH_REQUEST);
264 } 264 }
265 if ((1 == encoding_types->size()) && 265 if ((1 == encoding_types->size()) &&
266 (FILTER_TYPE_SDCH == encoding_types->front())) { 266 (FILTER_TYPE_SDCH == encoding_types->front())) {
267 LogSdchProblem(filter_context, 267 LogSdchProblem(filter_context,
268 SDCH_SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST); 268 SDCH_SDCH_CONTENT_ENCODE_FOR_NON_SDCH_REQUEST);
269 } 269 }
270 return; 270 return;
271 } 271 }
272 272
273 // The request was tagged as an SDCH request, which means the server supplied 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 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 275 // very strange things to the request, or the response, so we have to handle
276 // them gracefully. 276 // them gracefully.
277 277
278 // If content encoding included SDCH, then everything is "relatively" fine. 278 // If content encoding included SDCH, then everything is "relatively" fine.
279 if (!encoding_types->empty() && 279 if (!encoding_types->empty() &&
280 (FILTER_TYPE_SDCH == encoding_types->front())) { 280 (FILTER_TYPE_SDCH == encoding_types->front())) {
281 // Some proxies (found currently in Argentina) strip the Content-Encoding 281 // Some proxies (found currently in Argentina) strip the Content-Encoding
282 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed 282 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed
283 // payload. To handle this gracefully, we simulate the "probably" deleted 283 // payload. To handle this gracefully, we simulate the "probably" deleted
284 // ",gzip" by appending a tentative gzip decode, which will default to a 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. 285 // no-op pass through filter if it doesn't get gzip headers where expected.
286 if (1 == encoding_types->size()) { 286 if (1 == encoding_types->size()) {
287 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH); 287 encoding_types->push_back(FILTER_TYPE_GZIP_HELPING_SDCH);
288 LogSdchProblem(filter_context, SDCH_OPTIONAL_GUNZIP_ENCODING_ADDED); 288 LogSdchProblem(filter_context, SDCH_OPTIONAL_GUNZIP_ENCODING_ADDED);
289 } 289 }
290 return; 290 return;
291 } 291 }
292 292
293 // There are now several cases to handle for an SDCH request. Foremost, if 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 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) 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 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 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 298 // add any, as those filters sniff the content, and act as pass-through
299 // filters if headers are not found. 299 // filters if headers are not found.
300 300
301 // If the outbound GET is not modified, then the server will generally try to 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 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 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 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 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 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 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 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 309 // change the content, such as by re-gzipping it, and that may happen along
310 // with corruption of the stated content encoding (wow!). 310 // with corruption of the stated content encoding (wow!).
311 311
312 // The one unresolved failure mode comes when we advertise a dictionary, and 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 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) 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 315 // supported server side on paths that only send HTML content, this mode has
316 // never surfaced in the wild (and is unlikely to). 316 // never surfaced in the wild (and is unlikely to).
317 // We will gather a lot of stats as we perform the fixups 317 // We will gather a lot of stats as we perform the fixups
318 if (StartsWithASCII(mime_type, kTextHtml, false)) { 318 if (StartsWithASCII(mime_type, kTextHtml, false)) {
319 // Suspicious case: Advertised dictionary, but server didn't use sdch, and 319 // Suspicious case: Advertised dictionary, but server didn't use sdch, and
320 // we're HTML tagged. 320 // we're HTML tagged.
321 if (encoding_types->empty()) { 321 if (encoding_types->empty()) {
322 LogSdchProblem(filter_context, SDCH_ADDED_CONTENT_ENCODING); 322 LogSdchProblem(filter_context, SDCH_ADDED_CONTENT_ENCODING);
323 } else if (1 == encoding_types->size()) { 323 } else if (1 == encoding_types->size()) {
324 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODING); 324 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODING);
325 } else { 325 } else {
326 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODINGS); 326 LogSdchProblem(filter_context, SDCH_FIXED_CONTENT_ENCODINGS);
327 } 327 }
328 } else { 328 } else {
329 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding 329 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding
330 // was not marked for SDCH processing: Why did the server suggest an SDCH 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 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 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 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 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 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). 336 // fixup to a binary file, but it probably really is text/html (some how).
337 if (encoding_types->empty()) { 337 if (encoding_types->empty()) {
338 LogSdchProblem(filter_context, SDCH_BINARY_ADDED_CONTENT_ENCODING); 338 LogSdchProblem(filter_context, SDCH_BINARY_ADDED_CONTENT_ENCODING);
339 } else if (1 == encoding_types->size()) { 339 } else if (1 == encoding_types->size()) {
340 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODING); 340 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODING);
341 } else { 341 } else {
342 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODINGS); 342 LogSdchProblem(filter_context, SDCH_BINARY_FIXED_CONTENT_ENCODINGS);
343 } 343 }
344 } 344 }
345 345
346 // Leave the existing encoding type to be processed first, and add our 346 // Leave the existing encoding type to be processed first, and add our
347 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will 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 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 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 350 // result we'll need (in that case) to do the gunzip, plus our tentative
351 // gunzip and tentative SDCH decoding. 351 // gunzip and tentative SDCH decoding.
352 // This approach nicely handles the empty() list as well, and should work with 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 353 // other (as yet undiscovered) proxies the choose to re-compressed with some
354 // other encoding (such as bzip2, etc.). 354 // other encoding (such as bzip2, etc.).
355 encoding_types->insert(encoding_types->begin(), 355 encoding_types->insert(encoding_types->begin(),
356 FILTER_TYPE_GZIP_HELPING_SDCH); 356 FILTER_TYPE_GZIP_HELPING_SDCH);
357 encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE); 357 encoding_types->insert(encoding_types->begin(), FILTER_TYPE_SDCH_POSSIBLE);
358 return; 358 return;
359 } 359 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 452
453 void Filter::PushDataIntoNextFilter() { 453 void Filter::PushDataIntoNextFilter() {
454 IOBuffer* next_buffer = next_filter_->stream_buffer(); 454 IOBuffer* next_buffer = next_filter_->stream_buffer();
455 int next_size = next_filter_->stream_buffer_size(); 455 int next_size = next_filter_->stream_buffer_size();
456 last_status_ = ReadFilteredData(next_buffer->data(), &next_size); 456 last_status_ = ReadFilteredData(next_buffer->data(), &next_size);
457 if (FILTER_ERROR != last_status_) 457 if (FILTER_ERROR != last_status_)
458 next_filter_->FlushStreamBuffer(next_size); 458 next_filter_->FlushStreamBuffer(next_size);
459 } 459 }
460 460
461 } // namespace net 461 } // 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