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