| Index: net/filter/sdch_policy_delegate.cc
|
| diff --git a/net/filter/sdch_policy_delegate.cc b/net/filter/sdch_policy_delegate.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..acfc8822c387c6c5ad1884e88a9c66aa4fda53bc
|
| --- /dev/null
|
| +++ b/net/filter/sdch_policy_delegate.cc
|
| @@ -0,0 +1,226 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/metrics/histogram_macros.h"
|
| +#include "net/filter/sdch_policy_delegate.h"
|
| +#include "net/filter/sdch_stream_source.h"
|
| +
|
| +namespace {
|
| +
|
| +const char kRefreshHtml[] =
|
| + "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>";
|
| +
|
| +// Measures the eventual fate of the stream handled by this delegate.
|
| +enum StreamFate {
|
| + STREAM_FATE_OK,
|
| + STREAM_FATE_STOP_DECODING,
|
| + STREAM_FATE_REFRESH,
|
| + STREAM_FATE_FAIL
|
| +};
|
| +
|
| +// Causes for the fates given above. FATE_OK is always CAUSE_NONE.
|
| +enum StreamFateCause {
|
| + STREAM_FATE_CAUSE_NONE,
|
| + STREAM_FATE_CAUSE_404,
|
| + STREAM_FATE_CAUSE_NOT_200,
|
| + STREAM_FATE_CAUSE_OLD_UNENCODED,
|
| + STREAM_FATE_CAUSE_TENTATIVE_SDCH,
|
| + STREAM_FATE_CAUSE_NO_DICTIONARY,
|
| + STREAM_FATE_CAUSE_CORRUPT_SDCH,
|
| + STREAM_FATE_CAUSE_ENCODING_LIE,
|
| + STREAM_FATE_CAUSE_MAX
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +namespace net {
|
| +
|
| +SdchPolicyDelegate::SdchPolicyDelegate(scoped_ptr<Context> context)
|
| + : dictionary_requested_(false),
|
| + context_(std::move(context)),
|
| + did_meta_refresh_(false),
|
| + did_stop_decoding_(false),
|
| + did_fail_request_(false) {}
|
| +
|
| +SdchPolicyDelegate::~SdchPolicyDelegate() {}
|
| +
|
| +// Dictionary errors are often the first indication that the SDCH stream has
|
| +// become corrupt. There are many possible causes: non-200 response codes, a
|
| +// cached non-SDCH-ified reply, the server opting not to use SDCH, a cached
|
| +// SDCH-ified reply for which the SdchManager did not have the dictionary, a
|
| +// corrupted response, or a response that claims to be SDCH but isn't actually.
|
| +// These are handled here by issuing a meta-refresh or swapping to passthrough
|
| +// mode if appropriate, or failing the request if the error is unrecoverable.
|
| +bool SdchPolicyDelegate::HandleDictionaryError(SdchStreamSource* source) {
|
| + GURL url;
|
| + if (!context_->GetURL(&url)) {
|
| + did_fail_request_ = true;
|
| + return false;
|
| + }
|
| +
|
| + LogSdchDictionaryError();
|
| +
|
| + // HTTP 404 might be an unencoded error page, so if decoding failed, pass it
|
| + // through. TODO(516773)
|
| + if (context_->GetResponseCode() == 404) {
|
| + did_stop_decoding_ = true;
|
| + source->StopDecoding();
|
| + return true;
|
| + }
|
| +
|
| + // HTTP !200 gets a meta-refresh for HTML and a hard failure otherwise.
|
| + if (context_->GetResponseCode() != 200)
|
| + return IssueMetaRefreshIfPossible(source);
|
| +
|
| + // If this is a cached result and the source hasn't requested a dictionary, it
|
| + // probably never had a dictionary to begin and is an unencoded response from
|
| + // earlier. If the source has requested a dictionary, but there was still a
|
| + // dictionary error, then the SdchManager does not have it so this method
|
| + // needs to issue a meta-refresh to get it.
|
| + if (context_->IsCachedContent() && !dictionary_requested_) {
|
| + did_stop_decoding_ = true;
|
| + source->StopDecoding();
|
| + return true;
|
| + }
|
| +
|
| + // Ow. The original request didn't advertise any dictionaries, but the
|
| + // response claimed to be SDCH, but did not have a plausible dictionary hash.
|
| + // There is no way to repair this situation: the original request already
|
| + // didn't advertise any dictionaries, and retrying it would likely have the
|
| + // same result. Blacklist the domain and try passing through.
|
| + if (!context_->SdchDictionariesAdvertised() && !dictionary_requested_) {
|
| + did_stop_decoding_ = true;
|
| + source->StopDecoding();
|
| + context_->GetSdchManager()->BlacklistDomain(url,
|
| + SDCH_PASSING_THROUGH_NON_SDCH);
|
| + return true;
|
| + }
|
| +
|
| + return IssueMetaRefreshIfPossible(source);
|
| +}
|
| +
|
| +// A decoding error, as opposed to a dictionary error, indicates a decompression
|
| +// failure partway through the payload of the SDCH stream, which means that the
|
| +// filter already witnessed a valid dictionary ID and successfully retrieved a
|
| +// dictionary for it. Decoding errors are not recoverable and it is not
|
| +// appropriate to stop decoding, so there are relatively few error cases here.
|
| +//
|
| +// In particular, a decoding error for an HTML payload is recoverable by issuing
|
| +// a meta-refresh, but to avoid having that happen too often, this class also
|
| +// temporarily blacklists the domain. A decoding error for a non-HTML payload is
|
| +// unrecoverable, so such an error gets a permanent blacklist entry. If the
|
| +// content was cached, no blacklisting is needed.
|
| +bool SdchPolicyDelegate::HandleDecodingError(SdchStreamSource* source) {
|
| + LogSdchDecodingError();
|
| + return IssueMetaRefreshIfPossible(source);
|
| +}
|
| +
|
| +bool SdchPolicyDelegate::GetDictionary(const std::string& server_id,
|
| + const std::string** text) {
|
| + dictionary_requested_ = true;
|
| + SdchManager::DictionarySet* set = context_->SdchDictionariesAdvertised();
|
| + if (set) {
|
| + *text = set->GetDictionaryText(server_id);
|
| + if (!(*text)->empty()) {
|
| + LOG(ERROR) << "GetDictionary success!";
|
| + return true;
|
| + }
|
| + }
|
| + GURL url;
|
| + if (!context_->GetURL(&url))
|
| + return false;
|
| + SdchProblemCode rv = SDCH_OK;
|
| + unexpected_dictionary_set_ =
|
| + context_->GetSdchManager()->GetDictionarySetByHash(url, server_id, &rv);
|
| + if (unexpected_dictionary_set_) {
|
| + *text = unexpected_dictionary_set_->GetDictionaryText(server_id);
|
| + // XXX: log fallback here (UNADVERTISED_DICTIONARY_USED etc)
|
| + if (!(*text)->empty())
|
| + return true;
|
| + }
|
| + // XXX: check for HASH_NOT_FOUND / HASH_MALFORMED here? or in
|
| + // SdchStreamSource?
|
| + return false;
|
| +}
|
| +
|
| +void SdchPolicyDelegate::NotifyStreamDone() {
|
| +// Don't understand what to do with these.
|
| +#if 0
|
| + if (did_fail_request_)
|
| + UMA_HISTOGRAM_COUNTS("Sdch4.StreamFate", STREAM_FATE_FAIL);
|
| + else if (did_meta_refresh_)
|
| + UMA_HISTOGRAM_COUNTS("Sdch4.StreamFate", STREAM_FATE_REFRESH);
|
| + else if (did_stop_decoding_)
|
| + UMA_HISTOGRAM_COUNTS("Sdch4.StreamFate", STREAM_FATE_STOP_DECODING);
|
| + else
|
| + UMA_HISTOGRAM_COUNTS("Sdch4.StreamFate", STREAM_FATE_OK);
|
| +#endif
|
| +}
|
| +
|
| +bool SdchPolicyDelegate::IssueMetaRefreshIfPossible(SdchStreamSource* source) {
|
| + std::string mime_type;
|
| + if (!context_->GetMimeType(&mime_type)) {
|
| + did_fail_request_ = true;
|
| + return false;
|
| + }
|
| + GURL url;
|
| + if (!context_->GetURL(&url)) {
|
| + did_fail_request_ = true;
|
| + return false;
|
| + }
|
| + SdchManager* manager = context_->GetSdchManager();
|
| +
|
| + // Errors for non-HTML payloads are unrecoverable and get the domain
|
| + // blacklisted indefinitely.
|
| + if (mime_type.npos == mime_type.find("text/html")) {
|
| + SdchProblemCode problem =
|
| + (context_->IsCachedContent() ? SDCH_CACHED_META_REFRESH_UNSUPPORTED
|
| + : SDCH_META_REFRESH_UNSUPPORTED);
|
| + manager->BlacklistDomainForever(url, problem);
|
| + did_fail_request_ = true;
|
| + return false;
|
| + }
|
| +
|
| + if (!context_->IsCachedContent())
|
| + manager->BlacklistDomain(url, SDCH_META_REFRESH_RECOVERY);
|
| +
|
| + source->ReplaceOutput(kRefreshHtml, strlen(kRefreshHtml));
|
| + did_meta_refresh_ = true;
|
| + return true;
|
| +}
|
| +
|
| +// Old histograms: Sdch3.
|
| +// FilterUseBeforeDisabling
|
| +// PartialBytesIn, PartialVcdiffIn, PartialVcdiffOut
|
| +// UnflushedBytesIn, UnflushedBufferSize, UnflushedVcdiffIn,
|
| +// UnflushedVcdiffOut
|
| +// Network_Decode_Ratio_a
|
| +// Network_Decode_Bytes_VcdiffOut_a
|
| +// NetworkBytesSavedByCompression
|
| +// ResponseCorruptionDetection.{Cached,Uncached}
|
| +
|
| +// Decoding errors only produce Sdch3.ResponseCorruptionDetection histograms. To
|
| +// have a decoding error, the dictionary header must have been valid, and the
|
| +// dictionary must have been loaded successfully, but the response doesn't
|
| +// decode successfully, so it is corrupt.
|
| +void SdchPolicyDelegate::LogSdchDecodingError() {
|
| + if (context_->IsCachedContent()) {
|
| + UMA_HISTOGRAM_ENUMERATION("Sdch3.ResponseCorruptionDetection.Cached",
|
| + STREAM_FATE_CAUSE_CORRUPT_SDCH,
|
| + STREAM_FATE_CAUSE_MAX);
|
| + } else {
|
| + UMA_HISTOGRAM_ENUMERATION("Sdch3.ResponseCorruptionDetection.Uncached",
|
| + STREAM_FATE_CAUSE_CORRUPT_SDCH,
|
| + STREAM_FATE_CAUSE_MAX);
|
| + }
|
| +}
|
| +
|
| +void SdchPolicyDelegate::LogSdchDictionaryError() {
|
| + std::string mime_type;
|
| + if (!context_->GetMimeType(&mime_type))
|
| + return;
|
| + // TODO(ellyjones): figure out what stats to gather here
|
| +}
|
| +
|
| +} // namespace net
|
|
|