Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 #include "net/filter/sdch_policy_delegate.h" | |
| 6 | |
| 7 #include "base/metrics/histogram_macros.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 #include "base/values.h" | |
| 10 #include "net/base/sdch_net_log_params.h" | |
| 11 #include "net/base/sdch_problem_codes.h" | |
| 12 #include "net/log/net_log.h" | |
| 13 #include "net/log/net_log_event_type.h" | |
| 14 #include "sdch/open-vcdiff/src/google/vcdecoder.h" | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kRefreshHtml[] = | |
| 21 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>"; | |
| 22 // Mime types: | |
| 23 const char kTextHtml[] = "text/html"; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 SdchPolicyDelegate::SdchPolicyDelegate( | |
| 28 std::string mime_type, | |
| 29 const GURL& url, | |
| 30 bool is_cached_content, | |
| 31 SdchManager* sdch_manager, | |
| 32 std::unique_ptr<SdchManager::DictionarySet> dictionary_set, | |
| 33 int response_code, | |
| 34 const NetLogWithSource& net_log) | |
| 35 : mime_type_(mime_type), | |
| 36 url_(url), | |
| 37 is_cached_content_(is_cached_content), | |
| 38 sdch_manager_(sdch_manager), | |
| 39 dictionary_set_(std::move(dictionary_set)), | |
| 40 response_code_(response_code), | |
| 41 net_log_(net_log) {} | |
| 42 | |
| 43 SdchPolicyDelegate::~SdchPolicyDelegate() {} | |
| 44 | |
| 45 // static | |
| 46 void SdchPolicyDelegate::FixUpSdchContentEncodings( | |
| 47 const NetLogWithSource& net_log, | |
| 48 const std::string& mime_type, | |
| 49 SdchManager::DictionarySet* dictionary_set, | |
| 50 std::vector<SourceStream::SourceType>* types) { | |
| 51 // If content encoding included SDCH, then everything is "relatively" fine. | |
| 52 if (!types->empty() && types->front() == SourceStream::TYPE_SDCH) { | |
| 53 // Some proxies (found currently in Argentina) strip the Content-Encoding | |
| 54 // text from "sdch,gzip" to a mere "sdch" without modifying the compressed | |
| 55 // payload. To handle this gracefully, we simulate the "probably" deleted | |
| 56 // ",gzip" by appending a tentative gzip decode, which will default to a | |
| 57 // no-op pass through filter if it doesn't get gzip headers where | |
| 58 // expected. | |
| 59 if (1 == types->size()) { | |
| 60 types->push_back(SourceStream::TYPE_GZIP_FALLBACK); | |
| 61 LogSdchProblem(net_log, SDCH_OPTIONAL_GUNZIP_ENCODING_ADDED); | |
| 62 } | |
| 63 return; | |
| 64 } | |
| 65 | |
| 66 // If sdch dictionary is advertised, we might need to add some decoding, as | |
| 67 // some proxies strip encoding completely. | |
| 68 if (!dictionary_set) | |
| 69 return; | |
| 70 | |
| 71 // There are now several cases to handle for an SDCH request. Foremost, if | |
| 72 // the outbound request was stripped so as not to advertise support for | |
| 73 // encodings, we might get back content with no encoding, or (for example) | |
| 74 // just gzip. We have to be sure that any changes we make allow for such | |
| 75 // minimal coding to work. That issue is why we use TENTATIVE filters if we | |
| 76 // add any, as those filters sniff the content, and act as pass-through | |
| 77 // filters if headers are not found. | |
| 78 | |
| 79 // If the outbound GET is not modified, then the server will generally try to | |
| 80 // send us SDCH encoded content. As that content returns, there are several | |
| 81 // corruptions of the header "content-encoding" that proxies may perform (and | |
| 82 // have been detected in the wild). We already dealt with the a honest | |
| 83 // content encoding of "sdch,gzip" being corrupted into "sdch" with on change | |
| 84 // of the actual content. Another common corruption is to either disscard | |
| 85 // the accurate content encoding, or to replace it with gzip only (again, with | |
| 86 // no change in actual content). The last observed corruption it to actually | |
| 87 // change the content, such as by re-gzipping it, and that may happen along | |
| 88 // with corruption of the stated content encoding (wow!). | |
| 89 | |
| 90 // The one unresolved failure mode comes when we advertise a dictionary, and | |
| 91 // the server tries to *send* a gzipped file (not gzip encode content), and | |
| 92 // then we could do a gzip decode :-(. Since SDCH is only (currently) | |
| 93 // supported server side on paths that only send HTML content, this mode has | |
| 94 // never surfaced in the wild (and is unlikely to). | |
| 95 // We will gather a lot of stats as we perform the fixups | |
| 96 if (base::StartsWith(mime_type, kTextHtml, | |
| 97 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 98 // Suspicious case: Advertised dictionary, but server didn't use sdch, and | |
| 99 // we're HTML tagged. | |
| 100 if (types->empty()) { | |
| 101 LogSdchProblem(net_log, SDCH_ADDED_CONTENT_ENCODING); | |
| 102 } else if (1 == types->size()) { | |
| 103 LogSdchProblem(net_log, SDCH_FIXED_CONTENT_ENCODING); | |
| 104 } else { | |
| 105 LogSdchProblem(net_log, SDCH_FIXED_CONTENT_ENCODINGS); | |
| 106 } | |
| 107 } else { | |
| 108 // Remarkable case!?! We advertised an SDCH dictionary, content-encoding | |
| 109 // was not marked for SDCH processing: Why did the server suggest an SDCH | |
| 110 // dictionary in the first place??. Also, the content isn't | |
| 111 // tagged as HTML, despite the fact that SDCH encoding is mostly likely | |
| 112 // for HTML: Did some anti-virus system strip this tag (sometimes they | |
| 113 // strip accept-encoding headers on the request)?? Does the content | |
| 114 // encoding not start with "text/html" for some other reason?? We'll | |
| 115 // report this as a fixup to a binary file, but it probably really is | |
| 116 // text/html (some how). | |
| 117 if (types->empty()) { | |
| 118 LogSdchProblem(net_log, SDCH_BINARY_ADDED_CONTENT_ENCODING); | |
| 119 } else if (1 == types->size()) { | |
| 120 LogSdchProblem(net_log, SDCH_BINARY_FIXED_CONTENT_ENCODING); | |
| 121 } else { | |
| 122 LogSdchProblem(net_log, SDCH_BINARY_FIXED_CONTENT_ENCODINGS); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 // Leave the existing encoding type to be processed first, and add our | |
| 127 // tentative decodings to be done afterwards. Vodaphone UK reportedyl will | |
| 128 // perform a second layer of gzip encoding atop the server's sdch,gzip | |
| 129 // encoding, and then claim that the content encoding is a mere gzip. As a | |
| 130 // result we'll need (in that case) to do the gunzip, plus our tentative | |
| 131 // gunzip and tentative SDCH decoding. This approach nicely handles the | |
| 132 // empty() list as well, and should work with other (as yet undiscovered) | |
| 133 // proxies the choose to re-compressed with some other encoding (such as | |
| 134 // bzip2, etc.). | |
| 135 types->insert(types->begin(), SourceStream::TYPE_GZIP_FALLBACK); | |
| 136 types->insert(types->begin(), SourceStream::TYPE_SDCH_POSSIBLE); | |
| 137 } | |
| 138 | |
| 139 // Dictionary id errors are often the first indication that the SDCH stream has | |
| 140 // become corrupt. There are many possible causes: non-200 response codes, a | |
| 141 // cached non-SDCH-ified reply, or a response that claims to be SDCH but isn't | |
| 142 // actually. These are handled here by issuing a meta-refresh or swapping to the | |
| 143 // "passthrough" mode if appropriate, or failing the request if the error is | |
| 144 // unrecoverable. | |
| 145 SdchPolicyDelegate::ErrorRecovery SdchPolicyDelegate::OnDictionaryIdError( | |
| 146 bool possible_pass_through, | |
| 147 std::string* replace_output) { | |
| 148 if (possible_pass_through) { | |
| 149 LogCorruptionDetection(RESPONSE_TENTATIVE_SDCH); | |
| 150 return PASS_THROUGH; | |
| 151 } | |
| 152 // HTTP 404 might be an unencoded error page, so if decoding failed, pass it | |
| 153 // through. TODO(xunjieli): Remove this. crbug.com/516773. | |
| 154 if (response_code_ == 404) { | |
| 155 LogSdchProblem(net_log_, SDCH_PASS_THROUGH_404_CODE); | |
| 156 LogCorruptionDetection(RESPONSE_404); | |
| 157 return PASS_THROUGH; | |
| 158 } | |
| 159 | |
| 160 // HTTP !200 gets a meta-refresh for HTML. | |
| 161 // TODO(xunjieli): remove this. crbug.com/654393. | |
| 162 if (response_code_ != 200) { | |
| 163 LogCorruptionDetection(RESPONSE_NOT_200); | |
| 164 return IssueMetaRefreshIfPossible(replace_output); | |
| 165 } | |
| 166 | |
| 167 // If this is a cached result and the source hasn't requested a dictionary, it | |
| 168 // probably never had a dictionary to begin and is an unencoded response from | |
| 169 // earlier. | |
| 170 if (is_cached_content_) { | |
| 171 LogSdchProblem(net_log_, SDCH_PASS_THROUGH_OLD_CACHED); | |
| 172 LogCorruptionDetection(RESPONSE_OLD_UNENCODED); | |
| 173 return PASS_THROUGH; | |
| 174 } | |
| 175 | |
| 176 // The original request didn't advertise any dictionaries, but the | |
| 177 // response claimed to be SDCH. There is no way to repair this situation: the | |
| 178 // original request already didn't advertise any dictionaries, and retrying it | |
| 179 // would likely have the/ same result. Blacklist the domain and try passing | |
| 180 // through. | |
| 181 if (!dictionary_set_) { | |
| 182 sdch_manager_->BlacklistDomain(url_, SDCH_PASSING_THROUGH_NON_SDCH); | |
| 183 LogCorruptionDetection(RESPONSE_ENCODING_LIE); | |
| 184 return PASS_THROUGH; | |
| 185 } | |
| 186 return IssueMetaRefreshIfPossible(replace_output); | |
| 187 } | |
| 188 | |
| 189 // Dictionary fails to load when we have a plausible dictionay id. There are | |
| 190 // many possible causes: a cached SDCH-ified reply for which the SdchManager did | |
| 191 // not have the dictionary or a corrupted response. These are handled here by | |
| 192 // issuing a meta-refresh except the case where response code is 404. | |
| 193 SdchPolicyDelegate::ErrorRecovery SdchPolicyDelegate::OnGetDictionaryError( | |
| 194 bool possible_pass_through, | |
| 195 std::string* replace_output) { | |
| 196 if (possible_pass_through) { | |
| 197 LogCorruptionDetection(RESPONSE_TENTATIVE_SDCH); | |
| 198 return PASS_THROUGH; | |
| 199 } | |
| 200 // HTTP 404 might be an unencoded error page, so if decoding failed, pass it | |
| 201 // through. TODO(xunjieli): Remove this case crbug.com/516773. | |
| 202 if (response_code_ == 404) { | |
| 203 LogSdchProblem(net_log_, SDCH_PASS_THROUGH_404_CODE); | |
| 204 LogCorruptionDetection(RESPONSE_404); | |
| 205 return PASS_THROUGH; | |
| 206 } | |
| 207 return IssueMetaRefreshIfPossible(replace_output); | |
| 208 } | |
| 209 | |
| 210 SdchPolicyDelegate::ErrorRecovery SdchPolicyDelegate::OnDecodingError( | |
| 211 std::string* replace_output) { | |
| 212 // A decoding error, as opposed to a dictionary error, indicates a | |
| 213 // decompression failure partway through the payload of the SDCH stream, | |
| 214 // which means that the filter already witnessed a valid dictionary ID and | |
| 215 // successfully retrieved a dictionary for it. Decoding errors are not | |
| 216 // recoverable and it is not appropriate to stop decoding, so there are | |
| 217 // relatively few error cases here. | |
| 218 // | |
| 219 // In particular, a decoding error for an HTML payload is recoverable by | |
| 220 // issuing a meta-refresh, but to avoid having that happen too often, this | |
| 221 // class also temporarily blacklists the domain. A decoding error for a | |
| 222 // non-HTML payload is unrecoverable, so such an error gets a permanent | |
| 223 // blacklist entry. If the content was cached, no blacklisting is needed. | |
| 224 // TODO(xunjieli): This case should be removed. crbug.com/651821. | |
| 225 return IssueMetaRefreshIfPossible(replace_output); | |
| 226 } | |
| 227 | |
| 228 bool SdchPolicyDelegate::OnGetDictionary(const std::string& server_id, | |
| 229 const std::string** text) { | |
| 230 if (dictionary_set_) { | |
| 231 *text = dictionary_set_->GetDictionaryText(server_id); | |
| 232 if (*text) { | |
| 233 server_id_ = server_id; | |
| 234 return true; | |
| 235 } | |
| 236 } | |
| 237 // This is a hack. Naively, the dictionaries available for | |
| 238 // decoding should be only the ones advertised. However, there are | |
| 239 // cases, specifically resources encoded with old dictionaries living | |
| 240 // in the cache, that mean the full set of dictionaries should be made | |
| 241 // available for decoding. It's not known how often this happens; | |
| 242 // if it happens rarely enough, this code can be removed. | |
| 243 // | |
| 244 // TODO(rdsmith): Long-term, a better solution is necessary, since | |
| 245 // an entry in the cache being encoded with the dictionary doesn't | |
| 246 // guarantee that the dictionary is present. That solution probably | |
| 247 // involves storing unencoded resources in the cache, but might | |
| 248 // involve evicting encoded resources on dictionary removal. | |
| 249 // See http://crbug.com/383405. | |
| 250 SdchProblemCode rv = SDCH_OK; | |
| 251 unexpected_dictionary_set_ = | |
| 252 sdch_manager_->GetDictionarySetByHash(url_, server_id, &rv); | |
| 253 if (unexpected_dictionary_set_) { | |
| 254 *text = unexpected_dictionary_set_->GetDictionaryText(server_id); | |
| 255 LogSdchProblem(net_log_, is_cached_content_ | |
| 256 ? SDCH_UNADVERTISED_DICTIONARY_USED_CACHED | |
| 257 : SDCH_UNADVERTISED_DICTIONARY_USED); | |
| 258 if (*text) { | |
| 259 server_id_ = server_id; | |
| 260 return true; | |
| 261 } | |
| 262 } else { | |
| 263 LogSdchProblem(net_log_, SDCH_DICTIONARY_HASH_NOT_FOUND); | |
| 264 LogCorruptionDetection(RESPONSE_NO_DICTIONARY); | |
| 265 } | |
| 266 return false; | |
| 267 } | |
| 268 | |
| 269 void SdchPolicyDelegate::OnStreamDestroyed( | |
| 270 SdchSourceStream::InputState input_state, | |
| 271 const std::string& buffered_output, | |
| 272 open_vcdiff::VCDiffStreamingDecoder* decoder) { | |
| 273 if (decoder) { | |
| 274 if (!decoder->FinishDecoding()) { | |
| 275 LogSdchProblem(net_log_, SDCH_INCOMPLETE_SDCH_CONTENT); | |
| 276 // Make it possible for the user to hit reload, and get non-sdch content. | |
| 277 // Note this will "wear off" quickly enough, and is just meant to assure | |
| 278 // in some rare case that the user is not stuck. | |
| 279 sdch_manager_->BlacklistDomain(url_, SDCH_INCOMPLETE_SDCH_CONTENT); | |
| 280 } | |
| 281 } | |
| 282 // Filter chaining error, or premature teardown. | |
| 283 if (!buffered_output.empty()) | |
| 284 LogSdchProblem(net_log_, SDCH_UNFLUSHED_CONTENT); | |
| 285 | |
| 286 // FIXME(xunjieli): Why do we do an early return here? | |
| 287 if (is_cached_content_) { | |
| 288 // Not a real error, but it is useful to have this tally. | |
| 289 // TODO(jar): Remove this stat after SDCH stability is validated. | |
| 290 LogSdchProblem(net_log_, SDCH_CACHE_DECODED); | |
| 291 return; // We don't need timing stats, and we aready got ratios. | |
| 292 } | |
| 293 switch (input_state) { | |
| 294 case SdchSourceStream::STATE_DECODE: { | |
| 295 // TODO(xunjieli): Do we need to call URLRequestHttpJob::RecordPacketStats | |
|
xunjieli
2016/10/12 13:27:12
Randy: One problem with this approach is that the
Randy Smith (Not in Mondays)
2016/10/12 20:41:44
Oh, good point.
However, after some code chasing
Randy Smith (Not in Mondays)
2016/10/12 20:42:18
(For the record, I consider the above solution a h
xunjieli
2016/10/12 21:04:33
Thanks for looking into this! I have the same ques
xunjieli
2016/10/13 17:46:37
Done.
| |
| 296 | |
| 297 // Allow latency experiments to proceed. | |
| 298 sdch_manager_->SetAllowLatencyExperiment(url_, true); | |
| 299 | |
| 300 // Notify successful dictionary usage. | |
| 301 DCHECK(!server_id_.empty()); | |
| 302 sdch_manager_->OnDictionaryUsed(server_id_); | |
| 303 return; | |
| 304 } | |
| 305 case SdchSourceStream::STATE_LOAD_DICTIONARY: | |
| 306 LogSdchProblem(net_log_, SDCH_PRIOR_TO_DICTIONARY); | |
| 307 return; | |
| 308 case SdchSourceStream::STATE_OUTPUT_REPLACE: | |
| 309 case SdchSourceStream::STATE_PASS_THROUGH: | |
| 310 // Already accounted for when set. | |
| 311 return; | |
| 312 } // end of switch. | |
| 313 } | |
| 314 | |
| 315 // TODO(xunjieli): Remove meta refresh. crbug.com/651821. | |
| 316 SdchPolicyDelegate::ErrorRecovery | |
| 317 SdchPolicyDelegate::IssueMetaRefreshIfPossible(std::string* replace_output) { | |
| 318 // Errors for non-HTML payloads are unrecoverable and get the domain | |
| 319 // blacklisted indefinitely. | |
| 320 if (mime_type_.npos == mime_type_.find("text/html")) { | |
| 321 SdchProblemCode problem = | |
| 322 (is_cached_content_ ? SDCH_CACHED_META_REFRESH_UNSUPPORTED | |
| 323 : SDCH_META_REFRESH_UNSUPPORTED); | |
| 324 sdch_manager_->BlacklistDomainForever(url_, problem); | |
| 325 LogSdchProblem(net_log_, problem); | |
| 326 return NONE; | |
| 327 } | |
| 328 | |
| 329 if (is_cached_content_) { | |
| 330 // Cached content is a probably startup tab, so just get the fresh content | |
| 331 // and try again, without disabling SDCH. | |
| 332 LogSdchProblem(net_log_, SDCH_META_REFRESH_CACHED_RECOVERY); | |
| 333 } else { | |
| 334 // Since it wasn't in the cache, blacklist for some period to get the | |
| 335 // correct content. | |
| 336 sdch_manager_->BlacklistDomain(url_, SDCH_META_REFRESH_RECOVERY); | |
| 337 LogSdchProblem(net_log_, SDCH_META_REFRESH_RECOVERY); | |
| 338 } | |
| 339 | |
| 340 *replace_output = std::string(kRefreshHtml, strlen(kRefreshHtml)); | |
| 341 return REPLACE_OUTPUT; | |
| 342 } | |
| 343 | |
| 344 void SdchPolicyDelegate::LogSdchProblem(NetLogWithSource netlog, | |
| 345 SdchProblemCode problem) { | |
| 346 SdchManager::SdchErrorRecovery(problem); | |
| 347 netlog.AddEvent(NetLogEventType::SDCH_DECODING_ERROR, | |
| 348 base::Bind(&NetLogSdchResourceProblemCallback, problem)); | |
| 349 } | |
| 350 | |
| 351 void SdchPolicyDelegate::LogCorruptionDetection( | |
| 352 ResponseCorruptionDetectionCause cause) { | |
| 353 // Use if statement rather than ?: because UMA_HISTOGRAM_ENUMERATION | |
| 354 // caches the histogram name based on the call site. | |
| 355 if (is_cached_content_) { | |
| 356 UMA_HISTOGRAM_ENUMERATION("Sdch3.ResponseCorruptionDetection.Cached", cause, | |
| 357 RESPONSE_MAX); | |
| 358 } else { | |
| 359 UMA_HISTOGRAM_ENUMERATION("Sdch3.ResponseCorruptionDetection.Uncached", | |
| 360 cause, RESPONSE_MAX); | |
| 361 } | |
| 362 net_log_.AddEvent(NetLogEventType::SDCH_RESPONSE_CORRUPTION_DETECTION, | |
| 363 base::Bind(&NetLogResponseCorruptionDetectionCallback, | |
| 364 cause, is_cached_content_)); | |
| 365 } | |
| 366 | |
| 367 // static. | |
| 368 const char* SdchPolicyDelegate::ResponseCorruptionDetectionCauseToString( | |
| 369 ResponseCorruptionDetectionCause cause) { | |
| 370 const char* cause_string = "<unknown>"; | |
| 371 switch (cause) { | |
| 372 case RESPONSE_NONE: | |
| 373 cause_string = "NONE"; | |
| 374 break; | |
| 375 case RESPONSE_404: | |
| 376 cause_string = "404"; | |
| 377 break; | |
| 378 case RESPONSE_NOT_200: | |
| 379 cause_string = "NOT_200"; | |
| 380 break; | |
| 381 case RESPONSE_OLD_UNENCODED: | |
| 382 cause_string = "OLD_UNENCODED"; | |
| 383 break; | |
| 384 case RESPONSE_TENTATIVE_SDCH: | |
| 385 cause_string = "TENTATIVE_SDCH"; | |
| 386 break; | |
| 387 case RESPONSE_NO_DICTIONARY: | |
| 388 cause_string = "NO_DICTIONARY"; | |
| 389 break; | |
| 390 case RESPONSE_CORRUPT_SDCH: | |
| 391 cause_string = "CORRUPT_SDCH"; | |
| 392 break; | |
| 393 case RESPONSE_ENCODING_LIE: | |
| 394 cause_string = "ENCODING_LIE"; | |
| 395 break; | |
| 396 case RESPONSE_MAX: | |
| 397 cause_string = "<Error: max enum value>"; | |
| 398 break; | |
| 399 } | |
| 400 return cause_string; | |
| 401 } | |
| 402 | |
| 403 // static. | |
| 404 std::unique_ptr<base::Value> | |
| 405 SdchPolicyDelegate::NetLogResponseCorruptionDetectionCallback( | |
| 406 ResponseCorruptionDetectionCause cause, | |
| 407 bool cached, | |
| 408 NetLogCaptureMode capture_mode) { | |
| 409 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 410 dict->SetString("cause", ResponseCorruptionDetectionCauseToString(cause)); | |
| 411 dict->SetBoolean("cached", cached); | |
| 412 return std::move(dict); | |
| 413 } | |
| 414 | |
| 415 } // namespace net | |
| OLD | NEW |