Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: net/filter/sdch_policy_delegate.cc

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

Powered by Google App Engine
This is Rietveld 408576698