| 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/values.h" |
| 9 #include "net/base/sdch_net_log_params.h" |
| 10 #include "net/filter/sdch_source_stream.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char kRefreshHtml[] = |
| 17 "<head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0\"></head>"; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 SdchPolicyDelegate::SdchPolicyDelegate(std::unique_ptr<Context> context) |
| 22 : dictionary_requested_(false), context_(std::move(context)) {} |
| 23 |
| 24 SdchPolicyDelegate::~SdchPolicyDelegate() {} |
| 25 |
| 26 // Dictionary errors are often the first indication that the SDCH stream has |
| 27 // become corrupt. There are many possible causes: non-200 response codes, a |
| 28 // cached non-SDCH-ified reply, the server opting not to use SDCH, a cached |
| 29 // SDCH-ified reply for which the SdchManager did not have the dictionary, a |
| 30 // corrupted response, or a response that claims to be SDCH but isn't actually. |
| 31 // These are handled here by issuing a meta-refresh or swapping to passthrough |
| 32 // mode if appropriate, or failing the request if the error is unrecoverable. |
| 33 bool SdchPolicyDelegate::OnDictionaryError(SdchSourceStream* source) { |
| 34 // HTTP 404 might be an unencoded error page, so if decoding failed, pass it |
| 35 // through. |
| 36 // TODO(xunjieli): Figure out whether this special case can be removed. |
| 37 // crbug.com/516773. |
| 38 if (context_->GetResponseCode() == 404) { |
| 39 source->StopDecoding(); |
| 40 LogSdchProblem(SDCH_PASS_THROUGH_404_CODE); |
| 41 return true; |
| 42 } |
| 43 |
| 44 // HTTP !200 gets a meta-refresh for HTML and a hard failure otherwise. |
| 45 if (context_->GetResponseCode() != 200) |
| 46 return IssueMetaRefreshIfPossible(source); |
| 47 |
| 48 // If this is a cached result and the source hasn't requested a dictionary, it |
| 49 // probably never had a dictionary to begin and is an unencoded response from |
| 50 // earlier. If the source has requested a dictionary, but there was still a |
| 51 // dictionary error, then the SdchManager does not have it so this method |
| 52 // needs to issue a meta-refresh to get it. |
| 53 if (context_->IsCachedContent() && !dictionary_requested_) { |
| 54 source->StopDecoding(); |
| 55 LogSdchProblem(SDCH_PASS_THROUGH_OLD_CACHED); |
| 56 return true; |
| 57 } |
| 58 |
| 59 // Ow. The original request didn't advertise any dictionaries, but the |
| 60 // response claimed to be SDCH, but did not have a plausible dictionary hash. |
| 61 // There is no way to repair this situation: the original request already |
| 62 // didn't advertise any dictionaries, and retrying it would likely have the |
| 63 // same result. Blacklist the domain and try passing through. |
| 64 if (!context_->SdchDictionariesAdvertised() && !dictionary_requested_) { |
| 65 GURL url; |
| 66 if (!context_->GetURL(&url)) |
| 67 return false; |
| 68 source->StopDecoding(); |
| 69 context_->GetSdchManager()->BlacklistDomain(url, |
| 70 SDCH_PASSING_THROUGH_NON_SDCH); |
| 71 return true; |
| 72 } |
| 73 |
| 74 return IssueMetaRefreshIfPossible(source); |
| 75 } |
| 76 |
| 77 bool SdchPolicyDelegate::OnDecodingError(SdchSourceStream* source) { |
| 78 // A decoding error, as opposed to a dictionary error, indicates a |
| 79 // decompression failure partway through the payload of the SDCH stream, |
| 80 // which means that the filter already witnessed a valid dictionary ID and |
| 81 // successfully retrieved a dictionary for it. Decoding errors are not |
| 82 // recoverable and it is not appropriate to stop decoding, so there are |
| 83 // relatively few error cases here. |
| 84 // |
| 85 // In particular, a decoding error for an HTML payload is recoverable by |
| 86 // issuing a meta-refresh, but to avoid having that happen too often, this |
| 87 // class also temporarily blacklists the domain. A decoding error for a |
| 88 // non-HTML payload is unrecoverable, so such an error gets a permanent |
| 89 // blacklist entry. If the content was cached, no blacklisting is needed. |
| 90 return IssueMetaRefreshIfPossible(source); |
| 91 } |
| 92 |
| 93 bool SdchPolicyDelegate::OnGetDictionary(const std::string& server_id, |
| 94 const std::string** text) { |
| 95 dictionary_requested_ = true; |
| 96 SdchManager::DictionarySet* set = context_->SdchDictionariesAdvertised(); |
| 97 if (set) { |
| 98 *text = set->GetDictionaryText(server_id); |
| 99 if (*text) |
| 100 return true; |
| 101 } |
| 102 GURL url; |
| 103 if (!context_->GetURL(&url)) |
| 104 return false; |
| 105 // This is a hack. Naively, the dictionaries available for |
| 106 // decoding should be only the ones advertised. However, there are |
| 107 // cases, specifically resources encoded with old dictionaries living |
| 108 // in the cache, that mean the full set of dictionaries should be made |
| 109 // available for decoding. It's not known how often this happens; |
| 110 // if it happens rarely enough, this code can be removed. |
| 111 // |
| 112 // TODO(rdsmith): Long-term, a better solution is necessary, since |
| 113 // an entry in the cache being encoded with the dictionary doesn't |
| 114 // guarantee that the dictionary is present. That solution probably |
| 115 // involves storing unencoded resources in the cache, but might |
| 116 // involve evicting encoded resources on dictionary removal. |
| 117 // See http://crbug.com/383405. |
| 118 SdchProblemCode rv = SDCH_OK; |
| 119 unexpected_dictionary_set_ = |
| 120 context_->GetSdchManager()->GetDictionarySetByHash(url, server_id, &rv); |
| 121 if (unexpected_dictionary_set_) { |
| 122 *text = unexpected_dictionary_set_->GetDictionaryText(server_id); |
| 123 LogSdchProblem(context_->IsCachedContent() |
| 124 ? SDCH_UNADVERTISED_DICTIONARY_USED_CACHED |
| 125 : SDCH_UNADVERTISED_DICTIONARY_USED); |
| 126 if (*text) |
| 127 return true; |
| 128 } else { |
| 129 LogSdchProblem(SDCH_DICTIONARY_HASH_NOT_FOUND); |
| 130 } |
| 131 return false; |
| 132 } |
| 133 |
| 134 bool SdchPolicyDelegate::IssueMetaRefreshIfPossible(SdchSourceStream* source) { |
| 135 std::string mime_type; |
| 136 GURL url; |
| 137 if (!context_->GetMimeType(&mime_type) || !context_->GetURL(&url)) |
| 138 return false; |
| 139 SdchManager* manager = context_->GetSdchManager(); |
| 140 |
| 141 // Errors for non-HTML payloads are unrecoverable and get the domain |
| 142 // blacklisted indefinitely. |
| 143 if (mime_type.npos == mime_type.find("text/html")) { |
| 144 SdchProblemCode problem = |
| 145 (context_->IsCachedContent() ? SDCH_CACHED_META_REFRESH_UNSUPPORTED |
| 146 : SDCH_META_REFRESH_UNSUPPORTED); |
| 147 manager->BlacklistDomainForever(url, problem); |
| 148 LogSdchProblem(problem); |
| 149 return false; |
| 150 } |
| 151 |
| 152 if (context_->IsCachedContent()) { |
| 153 // Cached content is a probably startup tab, so just get the fresh content |
| 154 // and try again, without disabling SDCH. |
| 155 LogSdchProblem(SDCH_META_REFRESH_CACHED_RECOVERY); |
| 156 } else { |
| 157 // Since it wasn't in the cache, blacklist for some period to get the |
| 158 // correct content. |
| 159 manager->BlacklistDomain(url, SDCH_META_REFRESH_RECOVERY); |
| 160 LogSdchProblem(SDCH_META_REFRESH_RECOVERY); |
| 161 } |
| 162 |
| 163 source->ReplaceOutput(kRefreshHtml, strlen(kRefreshHtml)); |
| 164 return true; |
| 165 } |
| 166 |
| 167 void SdchPolicyDelegate::LogSdchProblem(SdchProblemCode problem) { |
| 168 SdchManager::SdchErrorRecovery(problem); |
| 169 context_->GetNetLog().AddEvent( |
| 170 NetLog::TYPE_SDCH_DECODING_ERROR, |
| 171 base::Bind(&NetLogSdchResourceProblemCallback, problem)); |
| 172 } |
| 173 |
| 174 } // namespace net |
| OLD | NEW |