OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/loader/mime_type_resource_handler.h" | 5 #include "content/browser/loader/mime_sniffing_resource_handler.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "components/mime_util/mime_util.h" | 17 #include "components/mime_util/mime_util.h" |
18 #include "content/browser/download/download_resource_handler.h" | 18 #include "content/browser/download/download_resource_handler.h" |
19 #include "content/browser/download/download_stats.h" | 19 #include "content/browser/download/download_stats.h" |
20 #include "content/browser/loader/intercepting_resource_handler.h" | |
20 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 21 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
21 #include "content/browser/loader/resource_request_info_impl.h" | 22 #include "content/browser/loader/resource_request_info_impl.h" |
22 #include "content/browser/loader/stream_resource_handler.h" | 23 #include "content/browser/loader/stream_resource_handler.h" |
23 #include "content/public/browser/content_browser_client.h" | 24 #include "content/public/browser/content_browser_client.h" |
24 #include "content/public/browser/download_item.h" | 25 #include "content/public/browser/download_item.h" |
25 #include "content/public/browser/download_save_info.h" | 26 #include "content/public/browser/download_save_info.h" |
26 #include "content/public/browser/download_url_parameters.h" | 27 #include "content/public/browser/download_url_parameters.h" |
27 #include "content/public/browser/plugin_service.h" | 28 #include "content/public/browser/plugin_service.h" |
28 #include "content/public/browser/resource_context.h" | 29 #include "content/public/browser/resource_context.h" |
29 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 30 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
30 #include "content/public/common/resource_response.h" | 31 #include "content/public/common/resource_response.h" |
31 #include "content/public/common/webplugininfo.h" | 32 #include "content/public/common/webplugininfo.h" |
32 #include "net/base/io_buffer.h" | 33 #include "net/base/io_buffer.h" |
33 #include "net/base/mime_sniffer.h" | 34 #include "net/base/mime_sniffer.h" |
34 #include "net/base/mime_util.h" | 35 #include "net/base/mime_util.h" |
35 #include "net/base/net_errors.h" | |
36 #include "net/http/http_content_disposition.h" | 36 #include "net/http/http_content_disposition.h" |
37 #include "net/http/http_response_headers.h" | 37 #include "net/http/http_response_headers.h" |
38 #include "net/url_request/url_request.h" | 38 #include "net/url_request/url_request.h" |
39 | 39 |
40 namespace content { | 40 namespace content { |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
44 const char kAcceptHeader[] = "Accept"; | 44 const char kAcceptHeader[] = "Accept"; |
45 const char kFrameAcceptHeader[] = | 45 const char kFrameAcceptHeader[] = |
46 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | 46 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
47 "*/*;q=0.8"; | 47 "*/*;q=0.8"; |
48 const char kStylesheetAcceptHeader[] = "text/css,*/*;q=0.1"; | 48 const char kStylesheetAcceptHeader[] = "text/css,*/*;q=0.1"; |
49 const char kImageAcceptHeader[] = "image/webp,image/*,*/*;q=0.8"; | 49 const char kImageAcceptHeader[] = "image/webp,image/*,*/*;q=0.8"; |
50 const char kDefaultAcceptHeader[] = "*/*"; | 50 const char kDefaultAcceptHeader[] = "*/*"; |
51 | 51 |
52 // Used to write into an existing IOBuffer at a given offset. | 52 // Used to write into an existing IOBuffer at a given offset. |
53 class DependentIOBuffer : public net::WrappedIOBuffer { | 53 class DependentIOBuffer : public net::WrappedIOBuffer { |
54 public: | 54 public: |
55 DependentIOBuffer(net::IOBuffer* buf, int offset) | 55 DependentIOBuffer(net::IOBuffer* buf, int offset) |
56 : net::WrappedIOBuffer(buf->data() + offset), | 56 : net::WrappedIOBuffer(buf->data() + offset), buf_(buf) {} |
57 buf_(buf) { | |
58 } | |
59 | 57 |
60 private: | 58 private: |
61 ~DependentIOBuffer() override {} | 59 ~DependentIOBuffer() override {} |
62 | 60 |
63 scoped_refptr<net::IOBuffer> buf_; | 61 scoped_refptr<net::IOBuffer> buf_; |
64 }; | 62 }; |
65 | 63 |
66 } // namespace | 64 } // namespace |
67 | 65 |
68 MimeTypeResourceHandler::MimeTypeResourceHandler( | 66 MimeSniffingResourceHandler::MimeSniffingResourceHandler( |
69 std::unique_ptr<ResourceHandler> next_handler, | 67 std::unique_ptr<ResourceHandler> next_handler, |
70 ResourceDispatcherHostImpl* host, | 68 ResourceDispatcherHostImpl* host, |
71 PluginService* plugin_service, | 69 PluginService* plugin_service, |
70 InterceptingResourceHandler* intercepting_handler, | |
72 net::URLRequest* request) | 71 net::URLRequest* request) |
73 : LayeredResourceHandler(request, std::move(next_handler)), | 72 : LayeredResourceHandler(request, std::move(next_handler)), |
74 state_(STATE_STARTING), | 73 state_(STATE_STARTING), |
75 host_(host), | 74 host_(host), |
76 #if defined(ENABLE_PLUGINS) | |
77 plugin_service_(plugin_service), | 75 plugin_service_(plugin_service), |
78 #endif | 76 must_download_(false), |
77 must_download_is_set_(false), | |
79 read_buffer_size_(0), | 78 read_buffer_size_(0), |
80 bytes_read_(0), | 79 bytes_read_(0), |
81 must_download_(false), | 80 intercepting_handler_(intercepting_handler), |
82 must_download_is_set_(false), | 81 weak_ptr_factory_(this) {} |
83 weak_ptr_factory_(this) { | |
84 } | |
85 | 82 |
86 MimeTypeResourceHandler::~MimeTypeResourceHandler() { | 83 MimeSniffingResourceHandler::~MimeSniffingResourceHandler() {} |
87 } | |
88 | 84 |
89 void MimeTypeResourceHandler::SetController(ResourceController* controller) { | 85 void MimeSniffingResourceHandler::SetController( |
86 ResourceController* controller) { | |
90 ResourceHandler::SetController(controller); | 87 ResourceHandler::SetController(controller); |
91 | 88 |
92 // Downstream handlers see us as their ResourceController, which allows us to | 89 // Downstream handlers see the MimeSniffingResourceHandler as their |
93 // consume part or all of the resource response, and then later replay it to | 90 // ResourceController, which allows it to consume part or all of the resource |
94 // downstream handler. | 91 // response, and then later replay it to downstream handler. |
95 DCHECK(next_handler_.get()); | 92 DCHECK(next_handler_.get()); |
96 next_handler_->SetController(this); | 93 next_handler_->SetController(this); |
97 } | 94 } |
98 | 95 |
99 bool MimeTypeResourceHandler::OnResponseStarted(ResourceResponse* response, | 96 bool MimeSniffingResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
100 bool* defer) { | |
101 response_ = response; | |
102 | |
103 // A 304 response should not contain a Content-Type header (RFC 7232 section | |
104 // 4.1). The following code may incorrectly attempt to add a Content-Type to | |
105 // the response, and so must be skipped for 304 responses. | |
106 if (!(response_->head.headers.get() && | |
107 response_->head.headers->response_code() == 304)) { | |
108 if (ShouldSniffContent()) { | |
109 state_ = STATE_BUFFERING; | |
110 return true; | |
111 } | |
112 | |
113 if (response_->head.mime_type.empty()) { | |
114 // Ugg. The server told us not to sniff the content but didn't give us | |
115 // a mime type. What's a browser to do? Turns out, we're supposed to | |
116 // treat the response as "text/plain". This is the most secure option. | |
117 response_->head.mime_type.assign("text/plain"); | |
118 } | |
119 | |
120 // Treat feed types as text/plain. | |
121 if (response_->head.mime_type == "application/rss+xml" || | |
122 response_->head.mime_type == "application/atom+xml") { | |
123 response_->head.mime_type.assign("text/plain"); | |
124 } | |
125 } | |
126 | |
127 state_ = STATE_PROCESSING; | |
128 return ProcessResponse(defer); | |
129 } | |
130 | |
131 bool MimeTypeResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
132 const char* accept_value = nullptr; | 97 const char* accept_value = nullptr; |
133 switch (GetRequestInfo()->GetResourceType()) { | 98 switch (GetRequestInfo()->GetResourceType()) { |
134 case RESOURCE_TYPE_MAIN_FRAME: | 99 case RESOURCE_TYPE_MAIN_FRAME: |
135 case RESOURCE_TYPE_SUB_FRAME: | 100 case RESOURCE_TYPE_SUB_FRAME: |
136 accept_value = kFrameAcceptHeader; | 101 accept_value = kFrameAcceptHeader; |
137 break; | 102 break; |
138 case RESOURCE_TYPE_STYLESHEET: | 103 case RESOURCE_TYPE_STYLESHEET: |
139 accept_value = kStylesheetAcceptHeader; | 104 accept_value = kStylesheetAcceptHeader; |
140 break; | 105 break; |
141 case RESOURCE_TYPE_IMAGE: | 106 case RESOURCE_TYPE_IMAGE: |
(...skipping 19 matching lines...) Expand all Loading... | |
161 NOTREACHED(); | 126 NOTREACHED(); |
162 break; | 127 break; |
163 } | 128 } |
164 | 129 |
165 // The false parameter prevents overwriting an existing accept header value, | 130 // The false parameter prevents overwriting an existing accept header value, |
166 // which is needed because JS can manually set an accept header on an XHR. | 131 // which is needed because JS can manually set an accept header on an XHR. |
167 request()->SetExtraRequestHeaderByName(kAcceptHeader, accept_value, false); | 132 request()->SetExtraRequestHeaderByName(kAcceptHeader, accept_value, false); |
168 return next_handler_->OnWillStart(url, defer); | 133 return next_handler_->OnWillStart(url, defer); |
169 } | 134 } |
170 | 135 |
171 bool MimeTypeResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 136 bool MimeSniffingResourceHandler::OnResponseStarted(ResourceResponse* response, |
172 int* buf_size, | 137 bool* defer) { |
173 int min_size) { | 138 DCHECK_EQ(STATE_STARTING, state_); |
139 response_ = response; | |
140 | |
141 state_ = STATE_BUFFERING; | |
142 | |
143 // A 304 response should not contain a Content-Type header (RFC 7232 section | |
144 // 4.1). The following code may incorrectly attempt to add a Content-Type to | |
145 // the response, and so must be skipped for 304 responses. | |
146 if (!(response_->head.headers.get() && | |
147 response_->head.headers->response_code() == 304)) { | |
148 if (ShouldSniffContent()) | |
149 return true; | |
150 | |
151 if (response_->head.mime_type.empty()) { | |
152 // Ugg. The server told us not to sniff the content but didn't give us a | |
153 // mime type. What's a browser to do? Turns out, we're supposed to | |
154 // treat the response as "text/plain". This is the most secure option. | |
155 response_->head.mime_type.assign("text/plain"); | |
156 } | |
157 | |
158 // Treat feed types as text/plain. | |
159 if (response_->head.mime_type == "application/rss+xml" || | |
160 response_->head.mime_type == "application/atom+xml") { | |
161 response_->head.mime_type.assign("text/plain"); | |
162 } | |
163 } | |
164 | |
165 return ProcessReplay(defer); | |
166 } | |
167 | |
168 bool MimeSniffingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
169 int* buf_size, | |
170 int min_size) { | |
174 if (state_ == STATE_STREAMING) | 171 if (state_ == STATE_STREAMING) |
175 return next_handler_->OnWillRead(buf, buf_size, min_size); | 172 return next_handler_->OnWillRead(buf, buf_size, min_size); |
176 | 173 |
177 DCHECK_EQ(-1, min_size); | 174 DCHECK_EQ(-1, min_size); |
178 | 175 |
179 if (read_buffer_.get()) { | 176 if (read_buffer_.get()) { |
180 CHECK_LT(bytes_read_, read_buffer_size_); | 177 CHECK_LT(bytes_read_, read_buffer_size_); |
181 *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_); | 178 *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_); |
182 *buf_size = read_buffer_size_ - bytes_read_; | 179 *buf_size = read_buffer_size_ - bytes_read_; |
183 } else { | 180 } else { |
184 if (!next_handler_->OnWillRead(buf, buf_size, min_size)) | 181 if (!next_handler_->OnWillRead(buf, buf_size, min_size)) |
185 return false; | 182 return false; |
186 | 183 |
187 read_buffer_ = *buf; | 184 read_buffer_ = *buf; |
188 read_buffer_size_ = *buf_size; | 185 read_buffer_size_ = *buf_size; |
189 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); | 186 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); |
190 } | 187 } |
191 return true; | 188 return true; |
192 } | 189 } |
193 | 190 |
194 bool MimeTypeResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 191 bool MimeSniffingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
195 if (state_ == STATE_STREAMING) | 192 if (state_ == STATE_STREAMING) |
196 return next_handler_->OnReadCompleted(bytes_read, defer); | 193 return next_handler_->OnReadCompleted(bytes_read, defer); |
197 | 194 |
198 DCHECK_EQ(state_, STATE_BUFFERING); | 195 DCHECK_EQ(state_, STATE_BUFFERING); |
199 bytes_read_ += bytes_read; | 196 bytes_read_ += bytes_read; |
200 | 197 |
201 if (!DetermineMimeType() && (bytes_read > 0)) | 198 const std::string& type_hint = response_->head.mime_type; |
202 return true; // Needs more data, so keep buffering. | |
203 | 199 |
204 state_ = STATE_PROCESSING; | 200 std::string new_type; |
205 return ProcessResponse(defer); | 201 bool made_final_decision = |
202 net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(), | |
203 type_hint, &new_type); | |
204 | |
205 // SniffMimeType() returns false if there is not enough data to determine | |
206 // the mime type. However, even if it returns false, it returns a new type | |
207 // that is probably better than the current one. | |
208 response_->head.mime_type.assign(new_type); | |
209 | |
210 if (!made_final_decision && (bytes_read > 0)) | |
211 return true; | |
212 | |
213 return ProcessReplay(defer); | |
206 } | 214 } |
207 | 215 |
208 void MimeTypeResourceHandler::OnResponseCompleted( | 216 void MimeSniffingResourceHandler::OnResponseCompleted( |
209 const net::URLRequestStatus& status, | 217 const net::URLRequestStatus& status, |
210 const std::string& security_info, | 218 const std::string& security_info, |
211 bool* defer) { | 219 bool* defer) { |
212 // Upon completion, act like a pass-through handler in case the downstream | 220 // Upon completion, act like a pass-through handler in case the downstream |
213 // handler defers OnResponseCompleted. | 221 // handler defers OnResponseCompleted. |
214 state_ = STATE_STREAMING; | 222 state_ = STATE_STREAMING; |
215 | 223 |
216 next_handler_->OnResponseCompleted(status, security_info, defer); | 224 next_handler_->OnResponseCompleted(status, security_info, defer); |
217 } | 225 } |
218 | 226 |
219 void MimeTypeResourceHandler::Resume() { | 227 void MimeSniffingResourceHandler::Resume() { |
220 switch (state_) { | 228 // If no information is currently being transmitted to downstream handlers, |
221 case STATE_BUFFERING: | 229 // they should not attempt to resume the request. |
222 case STATE_PROCESSING: | 230 if (state_ == STATE_BUFFERING) { |
223 NOTREACHED(); | 231 NOTREACHED(); |
224 break; | 232 return; |
225 case STATE_REPLAYING: | 233 } |
226 base::ThreadTaskRunnerHandle::Get()->PostTask( | 234 |
227 FROM_HERE, | 235 // If the BufferingHandler is acting as a pass-through handler, just ask the |
228 base::Bind(&MimeTypeResourceHandler::CallReplayReadCompleted, | 236 // upwards ResourceController to resume the request. |
229 weak_ptr_factory_.GetWeakPtr())); | 237 if (state_ == STATE_STARTING || state_ == STATE_STREAMING) { |
230 break; | 238 controller()->Resume(); |
231 case STATE_STARTING: | 239 return; |
232 case STATE_STREAMING: | 240 } |
233 controller()->Resume(); | 241 |
234 break; | 242 // Otherwise proceed with the replay of the response. If it is successful, |
243 // it will resume the request. | |
244 ProceedWithReplay(); | |
245 } | |
246 | |
247 void MimeSniffingResourceHandler::Cancel() { | |
248 controller()->Cancel(); | |
249 } | |
250 | |
251 void MimeSniffingResourceHandler::CancelAndIgnore() { | |
252 controller()->CancelAndIgnore(); | |
253 } | |
254 | |
255 void MimeSniffingResourceHandler::CancelWithError(int error_code) { | |
256 controller()->CancelWithError(error_code); | |
257 } | |
258 | |
259 void MimeSniffingResourceHandler::ProceedWithReplay() { | |
260 bool defer = false; | |
261 if (!ProcessReplay(&defer)) { | |
262 Cancel(); | |
263 } else if (!defer) { | |
264 DCHECK_EQ(STATE_STREAMING, state_); | |
265 controller()->Resume(); | |
235 } | 266 } |
236 } | 267 } |
237 | 268 |
238 void MimeTypeResourceHandler::Cancel() { | 269 bool MimeSniffingResourceHandler::ProcessReplay(bool* defer) { |
239 controller()->Cancel(); | 270 bool return_value = true; |
271 while (!*defer && return_value && state_ != STATE_STREAMING) { | |
272 switch (state_) { | |
273 case STATE_BUFFERING: | |
274 return_value = MaybeCheckForInterception(defer); | |
275 break; | |
276 case STATE_INTERCEPTION_CHECK_DONE: | |
277 return_value = ReplayResponseReceived(defer); | |
278 break; | |
279 case STATE_REPLAYING_RESPONSE_RECEIVED: | |
280 return_value = ReplayReadCompleted(defer); | |
281 break; | |
282 default: | |
283 NOTREACHED(); | |
284 break; | |
285 } | |
286 } | |
287 return return_value; | |
240 } | 288 } |
241 | 289 |
242 void MimeTypeResourceHandler::CancelAndIgnore() { | 290 bool MimeSniffingResourceHandler::MaybeCheckForInterception(bool* defer) { |
243 controller()->CancelAndIgnore(); | 291 DCHECK_EQ(STATE_BUFFERING, state_); |
244 } | 292 // If a request that can be intercepted failed the check for interception |
245 | 293 // step, it should be canceled. |
246 void MimeTypeResourceHandler::CancelWithError(int error_code) { | 294 if (CanBeIntercepted() && !CheckForInterception(defer)) |
mmenke
2016/07/18 18:50:42
Maybe move CanBeIntercepted() to the first line of
clamy
2016/07/19 14:24:55
Done.
| |
247 controller()->CancelWithError(error_code); | |
248 } | |
249 | |
250 bool MimeTypeResourceHandler::ProcessResponse(bool* defer) { | |
251 DCHECK_EQ(STATE_PROCESSING, state_); | |
252 | |
253 // TODO(darin): Stop special-casing 304 responses. | |
254 if (!(response_->head.headers.get() && | |
255 response_->head.headers->response_code() == 304)) { | |
256 if (!SelectNextHandler(defer)) | |
257 return false; | |
258 if (*defer) | |
259 return true; | |
260 } | |
261 | |
262 state_ = STATE_REPLAYING; | |
263 | |
264 if (!next_handler_->OnResponseStarted(response_.get(), defer)) | |
265 return false; | 295 return false; |
266 | 296 |
267 if (!read_buffer_.get()) { | |
268 state_ = STATE_STREAMING; | |
269 return true; | |
270 } | |
271 | |
272 if (!*defer) | 297 if (!*defer) |
273 return ReplayReadCompleted(defer); | 298 state_ = STATE_INTERCEPTION_CHECK_DONE; |
274 | 299 |
275 return true; | 300 return true; |
276 } | 301 } |
277 | 302 |
278 bool MimeTypeResourceHandler::ShouldSniffContent() { | 303 bool MimeSniffingResourceHandler::ReplayResponseReceived(bool* defer) { |
304 DCHECK_EQ(STATE_INTERCEPTION_CHECK_DONE, state_); | |
305 state_ = STATE_REPLAYING_RESPONSE_RECEIVED; | |
306 return next_handler_->OnResponseStarted(response_.get(), defer); | |
307 } | |
308 | |
309 bool MimeSniffingResourceHandler::ReplayReadCompleted(bool* defer) { | |
310 DCHECK_EQ(STATE_REPLAYING_RESPONSE_RECEIVED, state_); | |
311 | |
312 state_ = STATE_STREAMING; | |
313 | |
314 if (!read_buffer_.get()) | |
315 return true; | |
316 | |
317 bool result = next_handler_->OnReadCompleted(bytes_read_, defer); | |
318 | |
319 read_buffer_ = NULL; | |
320 read_buffer_size_ = 0; | |
321 bytes_read_ = 0; | |
322 | |
323 return result; | |
324 } | |
325 | |
326 bool MimeSniffingResourceHandler::ShouldSniffContent() { | |
279 const std::string& mime_type = response_->head.mime_type; | 327 const std::string& mime_type = response_->head.mime_type; |
280 | 328 |
281 std::string content_type_options; | 329 std::string content_type_options; |
282 request()->GetResponseHeaderByName("x-content-type-options", | 330 request()->GetResponseHeaderByName("x-content-type-options", |
283 &content_type_options); | 331 &content_type_options); |
284 | 332 |
285 bool sniffing_blocked = | 333 bool sniffing_blocked = |
286 base::LowerCaseEqualsASCII(content_type_options, "nosniff"); | 334 base::LowerCaseEqualsASCII(content_type_options, "nosniff"); |
287 bool we_would_like_to_sniff = | 335 bool we_would_like_to_sniff = |
288 net::ShouldSniffMimeType(request()->url(), mime_type); | 336 net::ShouldSniffMimeType(request()->url(), mime_type); |
289 | 337 |
290 if (!sniffing_blocked && we_would_like_to_sniff) { | 338 if (!sniffing_blocked && we_would_like_to_sniff) { |
291 // We're going to look at the data before deciding what the content type | 339 // We're going to look at the data before deciding what the content type |
292 // is. That means we need to delay sending the ResponseStarted message | 340 // is. That means we need to delay sending the ResponseStarted message |
293 // over the IPC channel. | 341 // over the IPC channel. |
294 VLOG(1) << "To buffer: " << request()->url().spec(); | 342 VLOG(1) << "To buffer: " << request()->url().spec(); |
295 return true; | 343 return true; |
296 } | 344 } |
297 | 345 |
298 return false; | 346 return false; |
299 } | 347 } |
300 | 348 |
301 bool MimeTypeResourceHandler::DetermineMimeType() { | 349 bool MimeSniffingResourceHandler::CheckForInterception(bool* defer) { |
302 DCHECK_EQ(STATE_BUFFERING, state_); | |
303 | |
304 const std::string& type_hint = response_->head.mime_type; | |
305 | |
306 std::string new_type; | |
307 bool made_final_decision = | |
308 net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(), | |
309 type_hint, &new_type); | |
310 | |
311 // SniffMimeType() returns false if there is not enough data to determine | |
312 // the mime type. However, even if it returns false, it returns a new type | |
313 // that is probably better than the current one. | |
314 response_->head.mime_type.assign(new_type); | |
315 | |
316 return made_final_decision; | |
317 } | |
318 | |
319 bool MimeTypeResourceHandler::SelectPluginHandler(bool* defer, | |
320 bool* handled_by_plugin) { | |
321 *handled_by_plugin = false; | |
322 #if defined(ENABLE_PLUGINS) | |
323 ResourceRequestInfoImpl* info = GetRequestInfo(); | |
324 bool allow_wildcard = false; | |
325 bool stale; | |
326 WebPluginInfo plugin; | |
327 bool has_plugin = plugin_service_->GetPluginInfo( | |
328 info->GetChildID(), info->GetRenderFrameID(), info->GetContext(), | |
329 request()->url(), GURL(), response_->head.mime_type, allow_wildcard, | |
330 &stale, &plugin, NULL); | |
331 | |
332 if (stale) { | |
333 // Refresh the plugins asynchronously. | |
334 plugin_service_->GetPlugins( | |
335 base::Bind(&MimeTypeResourceHandler::OnPluginsLoaded, | |
336 weak_ptr_factory_.GetWeakPtr())); | |
337 request()->LogBlockedBy("MimeTypeResourceHandler"); | |
338 *defer = true; | |
339 return true; | |
340 } | |
341 | |
342 if (has_plugin && plugin.type != WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN) { | |
343 *handled_by_plugin = true; | |
344 return true; | |
345 } | |
346 | |
347 // Attempt to intercept the request as a stream. | |
348 base::FilePath plugin_path; | |
349 if (has_plugin) | |
350 plugin_path = plugin.path; | |
351 std::string payload; | |
352 std::unique_ptr<ResourceHandler> handler(host_->MaybeInterceptAsStream( | |
353 plugin_path, request(), response_.get(), &payload)); | |
354 if (handler) { | |
355 *handled_by_plugin = true; | |
356 return UseAlternateNextHandler(std::move(handler), payload); | |
357 } | |
358 #endif | |
359 return true; | |
360 } | |
361 | |
362 bool MimeTypeResourceHandler::SelectNextHandler(bool* defer) { | |
363 DCHECK(!response_->head.mime_type.empty()); | 350 DCHECK(!response_->head.mime_type.empty()); |
364 | 351 |
365 ResourceRequestInfoImpl* info = GetRequestInfo(); | 352 ResourceRequestInfoImpl* info = GetRequestInfo(); |
366 const std::string& mime_type = response_->head.mime_type; | 353 const std::string& mime_type = response_->head.mime_type; |
367 | 354 |
368 // https://crbug.com/568184 - Temporary hack to track servers that aren't | 355 // https://crbug.com/568184 - Temporary hack to track servers that aren't |
369 // setting Content-Disposition when sending x-x509-user-cert and expecting | 356 // setting Content-Disposition when sending x-x509-user-cert and expecting |
370 // the browser to automatically install certificates; this is being | 357 // the browser to automatically install certificates; this is being |
371 // deprecated and will be removed upon full <keygen> removal. | 358 // deprecated and will be removed upon full <keygen> removal. |
372 if (mime_type == "application/x-x509-user-cert") { | 359 if (mime_type == "application/x-x509-user-cert") { |
373 UMA_HISTOGRAM_BOOLEAN( | 360 UMA_HISTOGRAM_BOOLEAN( |
374 "UserCert.ContentDisposition", | 361 "UserCert.ContentDisposition", |
375 response_->head.headers->HasHeader("Content-Disposition")); | 362 response_->head.headers->HasHeader("Content-Disposition")); |
376 } | 363 } |
377 | 364 |
378 // Allow requests for object/embed tags to be intercepted as streams. | 365 // Allow requests for object/embed tags to be intercepted as streams. |
379 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) { | 366 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) { |
380 DCHECK(!info->allow_download()); | 367 DCHECK(!info->allow_download()); |
381 | 368 |
382 bool handled_by_plugin; | 369 bool handled_by_plugin; |
383 if (!SelectPluginHandler(defer, &handled_by_plugin)) | 370 if (!CheckForPluginHandler(defer, &handled_by_plugin)) |
384 return false; | 371 return false; |
385 if (handled_by_plugin || *defer) | 372 if (handled_by_plugin || *defer) |
386 return true; | 373 return true; |
387 } | 374 } |
388 | 375 |
389 if (!info->allow_download()) | 376 if (!info->allow_download()) |
390 return true; | 377 return true; |
391 | 378 |
392 // info->allow_download() == true implies | 379 // info->allow_download() == true implies |
393 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or | 380 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or |
394 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME. | 381 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME. |
395 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME || | 382 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME || |
396 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME); | 383 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME); |
397 | 384 |
398 bool must_download = MustDownload(); | 385 bool must_download = MustDownload(); |
399 if (!must_download) { | 386 if (!must_download) { |
400 if (mime_util::IsSupportedMimeType(mime_type)) | 387 if (mime_util::IsSupportedMimeType(mime_type)) |
401 return true; | 388 return true; |
402 | 389 |
403 bool handled_by_plugin; | 390 bool handled_by_plugin; |
404 if (!SelectPluginHandler(defer, &handled_by_plugin)) | 391 if (!CheckForPluginHandler(defer, &handled_by_plugin)) |
405 return false; | 392 return false; |
406 if (handled_by_plugin || *defer) | 393 if (handled_by_plugin || *defer) |
407 return true; | 394 return true; |
408 } | 395 } |
409 | 396 |
410 // Install download handler | 397 // This is request is a download, |
mmenke
2016/07/18 18:50:42
This comment seems weird, just above a "CheckInter
clamy
2016/07/19 14:24:54
Added a blank line to make it clearer that it's no
| |
398 if (!CheckInterceptionIsValid()) | |
mmenke
2016/07/18 18:50:42
We have:
CanBeIntercepted(), CheckForInterception
clamy
2016/07/19 14:24:55
I went with CheckResponseIsNotProvisional. We do c
| |
399 return false; | |
400 | |
411 info->set_is_download(true); | 401 info->set_is_download(true); |
412 std::unique_ptr<ResourceHandler> handler( | 402 std::unique_ptr<ResourceHandler> handler( |
413 host_->CreateResourceHandlerForDownload(request(), | 403 host_->CreateResourceHandlerForDownload(request(), |
414 true, // is_content_initiated | 404 true, // is_content_initiated |
415 must_download)); | 405 must_download)); |
416 return UseAlternateNextHandler(std::move(handler), std::string()); | 406 intercepting_handler_->UseNewHandler(std::move(handler), std::string()); |
407 return true; | |
417 } | 408 } |
418 | 409 |
419 bool MimeTypeResourceHandler::UseAlternateNextHandler( | 410 bool MimeSniffingResourceHandler::CheckForPluginHandler( |
420 std::unique_ptr<ResourceHandler> new_handler, | 411 bool* defer, |
421 const std::string& payload_for_old_handler) { | 412 bool* handled_by_plugin) { |
422 if (response_->head.headers.get() && // Can be NULL if FTP. | 413 *handled_by_plugin = false; |
423 response_->head.headers->response_code() / 100 != 2) { | 414 #if defined(ENABLE_PLUGINS) |
424 // The response code indicates that this is an error page, but we don't | 415 ResourceRequestInfoImpl* info = GetRequestInfo(); |
425 // know how to display the content. We follow Firefox here and show our | 416 bool allow_wildcard = false; |
426 // own error page instead of triggering a download. | 417 bool stale; |
427 // TODO(abarth): We should abstract the response_code test, but this kind | 418 WebPluginInfo plugin; |
428 // of check is scattered throughout our codebase. | 419 bool has_plugin = plugin_service_->GetPluginInfo( |
429 request()->CancelWithError(net::ERR_INVALID_RESPONSE); | 420 info->GetChildID(), info->GetRenderFrameID(), info->GetContext(), |
421 request()->url(), GURL(), response_->head.mime_type, allow_wildcard, | |
422 &stale, &plugin, NULL); | |
423 | |
424 if (stale) { | |
425 // Refresh the plugins asynchronously. | |
426 plugin_service_->GetPlugins( | |
427 base::Bind(&MimeSniffingResourceHandler::OnPluginsLoaded, | |
428 weak_ptr_factory_.GetWeakPtr())); | |
429 request()->LogBlockedBy("MimeSniffingResourceHandler"); | |
430 *defer = true; | |
431 return true; | |
432 } | |
433 | |
434 if (has_plugin && plugin.type != WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN) { | |
435 *handled_by_plugin = true; | |
436 return true; | |
437 } | |
438 | |
439 // Attempt to intercept the request as a stream. | |
440 base::FilePath plugin_path; | |
441 if (has_plugin) | |
442 plugin_path = plugin.path; | |
443 std::string payload; | |
444 std::unique_ptr<ResourceHandler> handler(host_->MaybeInterceptAsStream( | |
445 plugin_path, request(), response_.get(), &payload)); | |
446 if (handler) { | |
447 if (!CheckInterceptionIsValid()) | |
448 return false; | |
449 *handled_by_plugin = true; | |
450 intercepting_handler_->UseNewHandler(std::move(handler), payload); | |
mmenke
2016/07/18 17:58:37
I'm not a big fan of having this dig into intercep
clamy
2016/07/19 14:24:55
We also need the info->is_stream bit that is set i
mmenke
2016/07/19 17:43:45
I'm not seeing where is_stream is used in this fil
| |
451 } | |
452 #endif | |
453 return true; | |
454 } | |
455 | |
456 bool MimeSniffingResourceHandler::CanBeIntercepted() { | |
457 if (response_->head.headers.get() && | |
458 response_->head.headers->response_code() == 304) { | |
430 return false; | 459 return false; |
431 } | 460 } |
432 | 461 |
433 // Inform the original ResourceHandler that this will be handled entirely by | 462 return true; |
434 // the new ResourceHandler. | 463 } |
435 // TODO(darin): We should probably check the return values of these. | |
436 bool defer_ignored = false; | |
437 next_handler_->OnResponseStarted(response_.get(), &defer_ignored); | |
438 // Although deferring OnResponseStarted is legal, the only downstream handler | |
439 // which does so is CrossSiteResourceHandler. Cross-site transitions should | |
440 // not trigger when switching handlers. | |
441 DCHECK(!defer_ignored); | |
442 if (payload_for_old_handler.empty()) { | |
443 net::URLRequestStatus status(net::URLRequestStatus::CANCELED, | |
444 net::ERR_ABORTED); | |
445 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); | |
446 DCHECK(!defer_ignored); | |
447 } else { | |
448 scoped_refptr<net::IOBuffer> buf; | |
449 int size = 0; | |
450 | 464 |
451 next_handler_->OnWillRead(&buf, &size, -1); | 465 bool MimeSniffingResourceHandler::CheckInterceptionIsValid() { |
452 CHECK_GE(size, static_cast<int>(payload_for_old_handler.length())); | 466 if (!response_->head.headers.get() || |
453 | 467 response_->head.headers->response_code() / 100 == 2) { |
454 memcpy(buf->data(), payload_for_old_handler.c_str(), | 468 return true; |
455 payload_for_old_handler.length()); | |
456 | |
457 next_handler_->OnReadCompleted(payload_for_old_handler.length(), | |
458 &defer_ignored); | |
459 DCHECK(!defer_ignored); | |
460 | |
461 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); | |
462 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); | |
463 DCHECK(!defer_ignored); | |
464 } | 469 } |
465 | 470 |
466 // This is handled entirely within the new ResourceHandler, so just reset the | 471 // The response code indicates that this is an error page, but we don't |
467 // original ResourceHandler. | 472 // know how to display the content. We follow Firefox here and show our |
468 next_handler_ = std::move(new_handler); | 473 // own error page instead of intercepting the request as a stream or a |
469 next_handler_->SetController(this); | 474 // download. |
470 | 475 // TODO(abarth): We should abstract the response_code test, but this kind |
471 return CopyReadBufferToNextHandler(); | 476 // of check is scattered throughout our codebase. |
477 request()->CancelWithError(net::ERR_INVALID_RESPONSE); | |
478 return false; | |
472 } | 479 } |
473 | 480 |
474 bool MimeTypeResourceHandler::ReplayReadCompleted(bool* defer) { | 481 bool MimeSniffingResourceHandler::MustDownload() { |
475 DCHECK(read_buffer_.get()); | |
476 | |
477 bool result = next_handler_->OnReadCompleted(bytes_read_, defer); | |
478 | |
479 read_buffer_ = NULL; | |
480 read_buffer_size_ = 0; | |
481 bytes_read_ = 0; | |
482 | |
483 state_ = STATE_STREAMING; | |
484 | |
485 return result; | |
486 } | |
487 | |
488 void MimeTypeResourceHandler::CallReplayReadCompleted() { | |
489 bool defer = false; | |
490 if (!ReplayReadCompleted(&defer)) { | |
491 controller()->Cancel(); | |
492 } else if (!defer) { | |
493 state_ = STATE_STREAMING; | |
494 controller()->Resume(); | |
495 } | |
496 } | |
497 | |
498 bool MimeTypeResourceHandler::MustDownload() { | |
499 if (must_download_is_set_) | 482 if (must_download_is_set_) |
500 return must_download_; | 483 return must_download_; |
501 | 484 |
502 must_download_is_set_ = true; | 485 must_download_is_set_ = true; |
503 | 486 |
504 std::string disposition; | 487 std::string disposition; |
505 request()->GetResponseHeaderByName("content-disposition", &disposition); | 488 request()->GetResponseHeaderByName("content-disposition", &disposition); |
506 if (!disposition.empty() && | 489 if (!disposition.empty() && |
507 net::HttpContentDisposition(disposition, std::string()).is_attachment()) { | 490 net::HttpContentDisposition(disposition, std::string()).is_attachment()) { |
508 must_download_ = true; | 491 must_download_ = true; |
509 } else if (host_->delegate() && | 492 } else if (host_->delegate() && |
510 host_->delegate()->ShouldForceDownloadResource( | 493 host_->delegate()->ShouldForceDownloadResource( |
511 request()->url(), response_->head.mime_type)) { | 494 request()->url(), response_->head.mime_type)) { |
512 must_download_ = true; | 495 must_download_ = true; |
513 } else { | 496 } else { |
514 must_download_ = false; | 497 must_download_ = false; |
515 } | 498 } |
516 | 499 |
517 return must_download_; | 500 return must_download_; |
518 } | 501 } |
519 | 502 |
520 bool MimeTypeResourceHandler::CopyReadBufferToNextHandler() { | 503 void MimeSniffingResourceHandler::OnPluginsLoaded( |
521 if (!read_buffer_.get()) | |
522 return true; | |
523 | |
524 scoped_refptr<net::IOBuffer> buf; | |
525 int buf_len = 0; | |
526 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_)) | |
527 return false; | |
528 | |
529 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); | |
530 memcpy(buf->data(), read_buffer_->data(), bytes_read_); | |
531 return true; | |
532 } | |
533 | |
534 void MimeTypeResourceHandler::OnPluginsLoaded( | |
535 const std::vector<WebPluginInfo>& plugins) { | 504 const std::vector<WebPluginInfo>& plugins) { |
505 // No longer blocking on the plugins being loaded. | |
536 request()->LogUnblocked(); | 506 request()->LogUnblocked(); |
537 bool defer = false; | 507 if (state_ == STATE_BUFFERING) |
538 if (!ProcessResponse(&defer)) { | 508 ProceedWithReplay(); |
539 controller()->Cancel(); | |
540 } else if (!defer) { | |
541 controller()->Resume(); | |
542 } | |
543 } | 509 } |
544 | 510 |
545 } // namespace content | 511 } // namespace content |
OLD | NEW |