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) | |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
In the original code, there is a data member possi
xunjieli
2016/10/05 13:44:56
Thanks for catching that. I have no idea why that
Randy Smith (Not in Mondays)
2016/10/07 18:46:25
Ok. After a slog through the original code, I've
xunjieli
2016/10/10 19:31:28
Done. The CL is at https://codereview.chromium.org
| |
23 : context_(std::move(context)) {} | |
24 | |
25 SdchPolicyDelegate::~SdchPolicyDelegate() {} | |
26 | |
27 // Dictionary errors are often the first indication that the SDCH stream has | |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Is it reasonable to separate this comment out into
xunjieli
2016/10/05 13:44:56
Done.
| |
28 // become corrupt. There are many possible causes: non-200 response codes, a | |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
How does a non-200 response code lead here? I wou
xunjieli
2016/10/05 13:44:56
Acknowledged. Not sure what the reason is. I belie
Randy Smith (Not in Mondays)
2016/10/07 18:46:25
TODO/bug?
xunjieli
2016/10/10 19:31:28
Done.
| |
29 // cached non-SDCH-ified reply, the server opting not to use SDCH, a cached | |
30 // SDCH-ified reply for which the SdchManager did not have the dictionary, a | |
31 // corrupted response, or a response that claims to be SDCH but isn't actually. | |
32 // These are handled here by issuing a meta-refresh or swapping to passthrough | |
33 // mode if appropriate, or failing the request if the error is unrecoverable. | |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Nice job turning the cascade in SdchFilter::ReadFi
xunjieli
2016/10/05 13:44:56
I'll give credit to Elly :)
| |
34 SdchPolicyDelegate::ErrorRecover SdchPolicyDelegate::OnDictionaryIdError( | |
35 SdchSourceStream* source, | |
36 std::string* replace_output) { | |
37 // HTTP 404 might be an unencoded error page, so if decoding failed, pass it | |
38 // through. | |
39 // TODO(xunjieli): Figure out whether this special case can be removed. | |
40 // crbug.com/516773. | |
41 if (context_->GetResponseCode() == 404) { | |
42 LogSdchProblem(SDCH_PASS_THROUGH_404_CODE); | |
43 return PASS_THROUGH; | |
44 } | |
45 | |
46 // HTTP !200 gets a meta-refresh for HTML and a hard failure otherwise. | |
47 if (context_->GetResponseCode() != 200) | |
48 return IssueMetaRefreshIfPossible(source, replace_output); | |
49 | |
50 // If this is a cached result and the source hasn't requested a dictionary, it | |
51 // probably never had a dictionary to begin and is an unencoded response from | |
52 // earlier. | |
53 if (context_->IsCachedContent()) { | |
54 LogSdchProblem(SDCH_PASS_THROUGH_OLD_CACHED); | |
55 return PASS_THROUGH; | |
56 } | |
57 | |
58 // The original request didn't advertise any dictionaries, but the | |
59 // response claimed to be SDCH. There is no way to repair this situation: the | |
60 // original request already didn't advertise any dictionaries, and retrying it | |
61 // would likely have the/ same result. Blacklist the domain and try passing | |
62 // through. | |
63 if (!context_->SdchDictionariesAdvertised()) { | |
64 GURL url; | |
65 if (context_->GetURL(&url)) { | |
66 context_->GetSdchManager()->BlacklistDomain( | |
67 url, SDCH_PASSING_THROUGH_NON_SDCH); | |
68 return PASS_THROUGH; | |
69 } | |
70 } | |
71 return NONE; | |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Why isn't this meta-refresh? I don't have confide
xunjieli
2016/10/05 13:44:56
Done.
| |
72 } | |
73 | |
74 SdchPolicyDelegate::ErrorRecover SdchPolicyDelegate::OnGetDictionaryError( | |
75 SdchSourceStream* source, | |
76 std::string* replace_output) { | |
77 // HTTP 404 might be an unencoded error page, so if decoding failed, pass it | |
78 // through. | |
79 // TODO(xunjieli): Figure out whether this special case can be removed. | |
80 // crbug.com/516773. | |
81 if (context_->GetResponseCode() == 404) { | |
82 LogSdchProblem(SDCH_PASS_THROUGH_404_CODE); | |
83 return PASS_THROUGH; | |
84 } | |
85 return IssueMetaRefreshIfPossible(source, replace_output); | |
86 } | |
87 | |
88 SdchPolicyDelegate::ErrorRecover SdchPolicyDelegate::OnDecodingError( | |
89 SdchSourceStream* source, | |
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. | |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Hmmm. You can bounce this for this CL if you want
xunjieli
2016/10/05 13:44:56
That's a good point. I think we can include this i
Randy Smith (Not in Mondays)
2016/10/07 18:46:25
Could you record the logic/specific sub-issue abov
xunjieli
2016/10/10 19:31:28
Done. Added in the bug.
| |
103 return IssueMetaRefreshIfPossible(source, 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::ErrorRecover SdchPolicyDelegate::IssueMetaRefreshIfPossible( | |
148 SdchSourceStream* source, | |
Randy Smith (Not in Mondays)
2016/10/04 20:06:29
Why does this function (and its callers) have |sou
xunjieli
2016/10/05 13:44:56
Done.
| |
149 std::string* replace_output) { | |
150 std::string mime_type; | |
151 GURL url; | |
152 if (!context_->GetMimeType(&mime_type) || !context_->GetURL(&url)) | |
153 return NONE; | |
154 | |
155 SdchManager* manager = context_->GetSdchManager(); | |
156 // Errors for non-HTML payloads are unrecoverable and get the domain | |
157 // blacklisted indefinitely. | |
158 if (mime_type.npos == mime_type.find("text/html")) { | |
159 SdchProblemCode problem = | |
160 (context_->IsCachedContent() ? SDCH_CACHED_META_REFRESH_UNSUPPORTED | |
161 : SDCH_META_REFRESH_UNSUPPORTED); | |
162 manager->BlacklistDomainForever(url, problem); | |
163 LogSdchProblem(problem); | |
164 return NONE; | |
165 } | |
166 | |
167 if (context_->IsCachedContent()) { | |
168 // Cached content is a probably startup tab, so just get the fresh content | |
169 // and try again, without disabling SDCH. | |
170 LogSdchProblem(SDCH_META_REFRESH_CACHED_RECOVERY); | |
171 } else { | |
172 // Since it wasn't in the cache, blacklist for some period to get the | |
173 // correct content. | |
174 manager->BlacklistDomain(url, SDCH_META_REFRESH_RECOVERY); | |
175 LogSdchProblem(SDCH_META_REFRESH_RECOVERY); | |
176 } | |
177 | |
178 *replace_output = std::string(kRefreshHtml, strlen(kRefreshHtml)); | |
179 return REPLACE_OUTPUT; | |
180 } | |
181 | |
182 void SdchPolicyDelegate::LogSdchProblem(SdchProblemCode problem) { | |
183 SdchManager::SdchErrorRecovery(problem); | |
184 context_->GetNetLog().AddEvent( | |
185 NetLogEventType::SDCH_DECODING_ERROR, | |
186 base::Bind(&NetLogSdchResourceProblemCallback, problem)); | |
187 } | |
188 | |
189 } // namespace net | |
OLD | NEW |