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