blob: adb96087854dff053caeda14b3561ffd62e03331 [file] [log] [blame]
[email protected]0a0b2542012-01-03 08:25:221// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]e3c404b2008-12-23 01:07:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]1d8a3d1f2011-02-19 07:11:525#include "content/browser/renderer_host/buffered_resource_handler.h"
[email protected]e3c404b2008-12-23 01:07:326
[email protected]dd9b5462009-09-25 17:22:237#include <vector>
8
[email protected]d33e7cc2011-09-23 01:43:569#include "base/bind.h"
[email protected]5203e6002009-07-29 03:42:0010#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3811#include "base/metrics/histogram.h"
[email protected]319d9e6f2009-02-18 19:47:2112#include "base/string_util.h"
[email protected]254ed742011-08-16 18:45:2713#include "content/browser/download/download_resource_handler.h"
[email protected]14365e32012-01-24 01:12:2214#include "content/browser/download/download_stats.h"
[email protected]e67385f2011-12-21 06:00:5615#include "content/browser/plugin_service_impl.h"
[email protected]ea114722012-03-12 01:11:2516#include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
[email protected]60cf2db2012-03-07 21:24:1817#include "content/browser/renderer_host/resource_request_info_impl.h"
[email protected]1d8a3d1f2011-02-19 07:11:5218#include "content/browser/renderer_host/x509_user_cert_resource_handler.h"
[email protected]c38831a12011-10-28 12:44:4919#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:4420#include "content/public/browser/content_browser_client.h"
[email protected]29a5ffc82012-03-13 19:35:5821#include "content/public/browser/download_save_info.h"
[email protected]ce967862012-02-09 22:47:0522#include "content/public/browser/resource_context.h"
[email protected]1bd0ef12011-10-20 05:24:1723#include "content/public/browser/resource_dispatcher_host_delegate.h"
[email protected]2336ffe2011-11-24 01:23:3424#include "content/public/common/resource_response.h"
[email protected]dd9b5462009-09-25 17:22:2325#include "net/base/io_buffer.h"
[email protected]9dea9e1f2009-01-29 00:30:4726#include "net/base/mime_sniffer.h"
[email protected]35fa6a22009-08-15 00:04:0127#include "net/base/mime_util.h"
[email protected]dd9b5462009-09-25 17:22:2328#include "net/base/net_errors.h"
[email protected]28ca9fb2012-01-28 14:50:2429#include "net/http/http_content_disposition.h"
[email protected]319d9e6f2009-02-18 19:47:2130#include "net/http/http_response_headers.h"
[email protected]d33e7cc2011-09-23 01:43:5631#include "webkit/plugins/webplugininfo.h"
[email protected]e3c404b2008-12-23 01:07:3232
[email protected]d18720a2012-01-06 09:53:5533namespace content {
34
[email protected]306291392009-01-17 19:15:3635namespace {
36
37void RecordSnifferMetrics(bool sniffing_blocked,
38 bool we_would_like_to_sniff,
39 const std::string& mime_type) {
[email protected]81ce9f3b2011-04-05 04:48:5340 static base::Histogram* nosniff_usage(NULL);
41 if (!nosniff_usage)
42 nosniff_usage = base::BooleanHistogram::FactoryGet(
43 "nosniff.usage", base::Histogram::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:3744 nosniff_usage->AddBoolean(sniffing_blocked);
[email protected]306291392009-01-17 19:15:3645
46 if (sniffing_blocked) {
[email protected]81ce9f3b2011-04-05 04:48:5347 static base::Histogram* nosniff_otherwise(NULL);
48 if (!nosniff_otherwise)
49 nosniff_otherwise = base::BooleanHistogram::FactoryGet(
50 "nosniff.otherwise", base::Histogram::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:3751 nosniff_otherwise->AddBoolean(we_would_like_to_sniff);
[email protected]306291392009-01-17 19:15:3652
[email protected]81ce9f3b2011-04-05 04:48:5353 static base::Histogram* nosniff_empty_mime_type(NULL);
54 if (!nosniff_empty_mime_type)
55 nosniff_empty_mime_type = base::BooleanHistogram::FactoryGet(
56 "nosniff.empty_mime_type",
57 base::Histogram::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:3758 nosniff_empty_mime_type->AddBoolean(mime_type.empty());
[email protected]306291392009-01-17 19:15:3659 }
60}
61
[email protected]926360f2012-06-29 04:45:0262// Used to write into an existing IOBuffer at a given offset.
63class DependentIOBuffer : public net::WrappedIOBuffer {
64 public:
65 DependentIOBuffer(net::IOBuffer* buf, int offset)
66 : net::WrappedIOBuffer(buf->data() + offset),
67 buf_(buf) {
68 }
69
70 private:
71 ~DependentIOBuffer() {}
72
73 scoped_refptr<net::IOBuffer> buf_;
74};
75
[email protected]306291392009-01-17 19:15:3676} // namespace
77
[email protected]ea114722012-03-12 01:11:2578BufferedResourceHandler::BufferedResourceHandler(
[email protected]d651cbc2012-05-31 21:33:0579 scoped_ptr<ResourceHandler> next_handler,
[email protected]ea114722012-03-12 01:11:2580 ResourceDispatcherHostImpl* host,
81 net::URLRequest* request)
[email protected]d651cbc2012-05-31 21:33:0582 : LayeredResourceHandler(next_handler.Pass()),
[email protected]926360f2012-06-29 04:45:0283 state_(STATE_STARTING),
[email protected]e3c404b2008-12-23 01:07:3284 host_(host),
85 request_(request),
[email protected]dd9b5462009-09-25 17:22:2386 read_buffer_size_(0),
[email protected]e3c404b2008-12-23 01:07:3287 bytes_read_(0),
[email protected]926360f2012-06-29 04:45:0288 must_download_(false),
89 must_download_is_set_(false) {
90}
91
92BufferedResourceHandler::~BufferedResourceHandler() {
93}
94
95void BufferedResourceHandler::SetController(ResourceController* controller) {
96 ResourceHandler::SetController(controller);
97
98 // Downstream handlers see us as their ResourceController, which allows us to
99 // consume part or all of the resource response, and then later replay it to
100 // downstream handler.
101 DCHECK(next_handler_.get());
102 next_handler_->SetController(this);
[email protected]e3c404b2008-12-23 01:07:32103}
104
[email protected]2336ffe2011-11-24 01:23:34105bool BufferedResourceHandler::OnResponseStarted(
106 int request_id,
[email protected]22b305442012-05-21 18:02:59107 ResourceResponse* response,
108 bool* defer) {
[email protected]e3c404b2008-12-23 01:07:32109 response_ = response;
[email protected]22b305442012-05-21 18:02:59110
[email protected]926360f2012-06-29 04:45:02111 // TODO(darin): It is very odd to special-case 304 responses at this level.
112 // We do so only because the code always has, see r24977 and r29355. The
113 // fact that 204 is no longer special-cased this way suggests that 304 need
114 // not be special-cased either.
115 //
116 // The network stack only forwards 304 responses that were not received in
117 // response to a conditional request (i.e., If-Modified-Since). Other 304
118 // responses end up being translated to 200 or whatever the cached response
119 // code happens to be. It should be very rare to see a 304 at this level.
[email protected]22b305442012-05-21 18:02:59120
[email protected]926360f2012-06-29 04:45:02121 if (!(response_->head.headers &&
122 response_->head.headers->response_code() == 304)) {
123 if (ShouldSniffContent()) {
124 state_ = STATE_BUFFERING;
125 return true;
126 }
127
128 if (response_->head.mime_type.empty()) {
129 // Ugg. The server told us not to sniff the content but didn't give us
130 // a mime type. What's a browser to do? Turns out, we're supposed to
131 // treat the response as "text/plain". This is the most secure option.
132 response_->head.mime_type.assign("text/plain");
133 }
[email protected]22b305442012-05-21 18:02:59134 }
[email protected]926360f2012-06-29 04:45:02135
136 state_ = STATE_PROCESSING;
137 return ProcessResponse(defer);
[email protected]e3c404b2008-12-23 01:07:32138}
139
[email protected]e3c404b2008-12-23 01:07:32140// We'll let the original event handler provide a buffer, and reuse it for
141// subsequent reads until we're done buffering.
[email protected]9dea9e1f2009-01-29 00:30:47142bool BufferedResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf,
143 int* buf_size, int min_size) {
[email protected]926360f2012-06-29 04:45:02144 if (state_ == STATE_STREAMING)
145 return next_handler_->OnWillRead(request_id, buf, buf_size, min_size);
146
147 DCHECK_EQ(-1, min_size);
148
149 if (read_buffer_) {
150 CHECK_LT(bytes_read_, read_buffer_size_);
151 *buf = new DependentIOBuffer(read_buffer_, bytes_read_);
152 *buf_size = read_buffer_size_ - bytes_read_;
153 } else {
154 if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size))
155 return false;
156
157 read_buffer_ = *buf;
158 read_buffer_size_ = *buf_size;
159 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2);
[email protected]e3c404b2008-12-23 01:07:32160 }
[email protected]67199052009-06-12 21:15:33161 return true;
[email protected]e3c404b2008-12-23 01:07:32162}
163
[email protected]926360f2012-06-29 04:45:02164bool BufferedResourceHandler::OnReadCompleted(int request_id, int bytes_read,
[email protected]22b305442012-05-21 18:02:59165 bool* defer) {
[email protected]926360f2012-06-29 04:45:02166 if (state_ == STATE_STREAMING)
167 return next_handler_->OnReadCompleted(request_id, bytes_read, defer);
[email protected]e3c404b2008-12-23 01:07:32168
[email protected]926360f2012-06-29 04:45:02169 DCHECK_EQ(state_, STATE_BUFFERING);
170 bytes_read_ += bytes_read;
[email protected]e3c404b2008-12-23 01:07:32171
[email protected]926360f2012-06-29 04:45:02172 if (!DetermineMimeType() && (bytes_read > 0))
173 return true; // Needs more data, so keep buffering.
174
175 state_ = STATE_PROCESSING;
176 return ProcessResponse(defer);
177}
178
179bool BufferedResourceHandler::OnResponseCompleted(
180 int request_id,
181 const net::URLRequestStatus& status,
182 const std::string& security_info) {
183 // Upon completion, act like a pass-through handler in case the downstream
184 // handler defers OnResponseCompleted.
185 state_ = STATE_STREAMING;
186
187 return next_handler_->OnResponseCompleted(request_id, status, security_info);
188}
189
190void BufferedResourceHandler::Resume() {
191 switch (state_) {
192 case STATE_BUFFERING:
193 case STATE_PROCESSING:
194 NOTREACHED();
195 break;
196 case STATE_REPLAYING:
197 MessageLoop::current()->PostTask(
198 FROM_HERE,
199 base::Bind(&BufferedResourceHandler::CallReplayReadCompleted,
200 AsWeakPtr()));
201 break;
202 case STATE_STARTING:
203 case STATE_STREAMING:
204 controller()->Resume();
205 break;
206 }
207}
208
209void BufferedResourceHandler::Cancel() {
210 controller()->Cancel();
211}
212
213bool BufferedResourceHandler::ProcessResponse(bool* defer) {
214 DCHECK_EQ(STATE_PROCESSING, state_);
215
216 // TODO(darin): Stop special-casing 304 responses.
217 if (!(response_->head.headers &&
218 response_->head.headers->response_code() == 304)) {
219 if (!SelectNextHandler(defer))
[email protected]e3c404b2008-12-23 01:07:32220 return false;
[email protected]22b305442012-05-21 18:02:59221 if (*defer)
[email protected]98d6f152011-09-29 19:35:51222 return true;
[email protected]926360f2012-06-29 04:45:02223 }
224
225 state_ = STATE_REPLAYING;
226
227 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID();
228 if (!next_handler_->OnResponseStarted(request_id, response_, defer))
229 return false;
230
231 if (!read_buffer_) {
232 state_ = STATE_STREAMING;
[email protected]35fa6a22009-08-15 00:04:01233 return true;
[email protected]e3c404b2008-12-23 01:07:32234 }
235
[email protected]926360f2012-06-29 04:45:02236 if (!*defer)
237 return ReplayReadCompleted(defer);
[email protected]5e5e0c82012-01-25 09:12:29238
[email protected]926360f2012-06-29 04:45:02239 return true;
[email protected]e3c404b2008-12-23 01:07:32240}
241
[email protected]926360f2012-06-29 04:45:02242bool BufferedResourceHandler::ShouldSniffContent() {
243 const std::string& mime_type = response_->head.mime_type;
[email protected]e3c404b2008-12-23 01:07:32244
245 std::string content_type_options;
246 request_->GetResponseHeaderByName("x-content-type-options",
247 &content_type_options);
[email protected]306291392009-01-17 19:15:36248
[email protected]926360f2012-06-29 04:45:02249 bool sniffing_blocked =
[email protected]423bd5b842009-01-23 17:30:50250 LowerCaseEqualsASCII(content_type_options, "nosniff");
[email protected]926360f2012-06-29 04:45:02251 bool we_would_like_to_sniff =
252 net::ShouldSniffMimeType(request_->url(), mime_type);
[email protected]306291392009-01-17 19:15:36253
254 RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type);
255
256 if (!sniffing_blocked && we_would_like_to_sniff) {
[email protected]e3c404b2008-12-23 01:07:32257 // We're going to look at the data before deciding what the content type
258 // is. That means we need to delay sending the ResponseStarted message
259 // over the IPC channel.
[email protected]238667042010-10-23 01:40:00260 VLOG(1) << "To buffer: " << request_->url().spec();
[email protected]e3c404b2008-12-23 01:07:32261 return true;
262 }
263
[email protected]e3c404b2008-12-23 01:07:32264 return false;
265}
266
[email protected]926360f2012-06-29 04:45:02267bool BufferedResourceHandler::DetermineMimeType() {
268 DCHECK_EQ(STATE_BUFFERING, state_);
[email protected]35fa6a22009-08-15 00:04:01269
[email protected]926360f2012-06-29 04:45:02270 const std::string& type_hint = response_->head.mime_type;
271
272 std::string new_type;
273 bool made_final_decision =
274 net::SniffMimeType(read_buffer_->data(), bytes_read_, request_->url(),
275 type_hint, &new_type);
276
277 // SniffMimeType() returns false if there is not enough data to determine
278 // the mime type. However, even if it returns false, it returns a new type
279 // that is probably better than the current one.
280 response_->head.mime_type.assign(new_type);
281
282 return made_final_decision;
[email protected]35fa6a22009-08-15 00:04:01283}
284
[email protected]926360f2012-06-29 04:45:02285bool BufferedResourceHandler::SelectNextHandler(bool* defer) {
286 DCHECK(!response_->head.mime_type.empty());
[email protected]e3c404b2008-12-23 01:07:32287
[email protected]926360f2012-06-29 04:45:02288 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_);
289 const std::string& mime_type = response_->head.mime_type;
[email protected]a755e1072009-10-23 16:58:37290
[email protected]a755e1072009-10-23 16:58:37291 if (mime_type == "application/x-x509-user-cert") {
[email protected]926360f2012-06-29 04:45:02292 // Install X509 handler.
[email protected]d651cbc2012-05-31 21:33:05293 scoped_ptr<ResourceHandler> handler(
[email protected]ea114722012-03-12 01:11:25294 new X509UserCertResourceHandler(request_,
295 info->GetChildID(),
[email protected]d651cbc2012-05-31 21:33:05296 info->GetRouteID()));
[email protected]926360f2012-06-29 04:45:02297 return UseAlternateNextHandler(handler.Pass());
[email protected]5a9d58fa2012-05-29 15:15:04298 }
299
[email protected]926360f2012-06-29 04:45:02300 if (!info->allow_download())
[email protected]5e5e0c82012-01-25 09:12:29301 return true;
302
[email protected]926360f2012-06-29 04:45:02303 if (!MustDownload()) {
304 if (net::IsSupportedMimeType(mime_type))
[email protected]35fa6a22009-08-15 00:04:01305 return true;
[email protected]35fa6a22009-08-15 00:04:01306
[email protected]926360f2012-06-29 04:45:02307 bool stale;
308 bool has_plugin = HasSupportingPlugin(&stale);
[email protected]68598072011-07-29 08:21:28309 if (stale) {
[email protected]926360f2012-06-29 04:45:02310 // Refresh the plugins asynchronously.
311 PluginServiceImpl::GetInstance()->GetPlugins(
312 base::Bind(&BufferedResourceHandler::OnPluginsLoaded, AsWeakPtr()));
313 *defer = true;
[email protected]35fa6a22009-08-15 00:04:01314 return true;
315 }
[email protected]926360f2012-06-29 04:45:02316 if (has_plugin)
317 return true;
[email protected]35fa6a22009-08-15 00:04:01318 }
319
[email protected]926360f2012-06-29 04:45:02320 // Install download handler
321 info->set_is_download(true);
322 scoped_ptr<ResourceHandler> handler(
323 host_->CreateResourceHandlerForDownload(
324 request_,
325 true, // is_content_initiated
326 DownloadSaveInfo(),
327 DownloadResourceHandler::OnStartedCallback()));
328 return UseAlternateNextHandler(handler.Pass());
[email protected]35fa6a22009-08-15 00:04:01329}
330
[email protected]926360f2012-06-29 04:45:02331bool BufferedResourceHandler::UseAlternateNextHandler(
332 scoped_ptr<ResourceHandler> new_handler) {
333 if (response_->head.headers && // Can be NULL if FTP.
334 response_->head.headers->response_code() / 100 != 2) {
335 // The response code indicates that this is an error page, but we don't
336 // know how to display the content. We follow Firefox here and show our
337 // own error page instead of triggering a download.
338 // TODO(abarth): We should abstract the response_code test, but this kind
339 // of check is scattered throughout our codebase.
340 request_->CancelWithError(net::ERR_FILE_NOT_FOUND);
341 return false;
342 }
343
344 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID();
345
[email protected]0dc72bbe2010-04-08 15:29:17346 // Inform the original ResourceHandler that this will be handled entirely by
347 // the new ResourceHandler.
[email protected]5e5e0c82012-01-25 09:12:29348 // TODO(darin): We should probably check the return values of these.
[email protected]22b305442012-05-21 18:02:59349 bool defer_ignored = false;
350 next_handler_->OnResponseStarted(request_id, response_, &defer_ignored);
351 DCHECK(!defer_ignored);
[email protected]f90bf0d92011-01-13 02:12:44352 net::URLRequestStatus status(net::URLRequestStatus::HANDLED_EXTERNALLY, 0);
[email protected]5e5e0c82012-01-25 09:12:29353 next_handler_->OnResponseCompleted(request_id, status, std::string());
[email protected]0dc72bbe2010-04-08 15:29:17354
[email protected]0dc72bbe2010-04-08 15:29:17355 // This is handled entirely within the new ResourceHandler, so just reset the
356 // original ResourceHandler.
[email protected]926360f2012-06-29 04:45:02357 next_handler_ = new_handler.Pass();
358 next_handler_->SetController(this);
[email protected]5e5e0c82012-01-25 09:12:29359
[email protected]926360f2012-06-29 04:45:02360 return CopyReadBufferToNextHandler(request_id);
[email protected]5e5e0c82012-01-25 09:12:29361}
362
[email protected]926360f2012-06-29 04:45:02363bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) {
364 DCHECK(read_buffer_);
[email protected]5e5e0c82012-01-25 09:12:29365
[email protected]926360f2012-06-29 04:45:02366 int request_id = ResourceRequestInfo::ForRequest(request_)->GetRequestID();
367 bool result = next_handler_->OnReadCompleted(request_id, bytes_read_, defer);
368
369 read_buffer_ = NULL;
370 read_buffer_size_ = 0;
371 bytes_read_ = 0;
372
373 state_ = STATE_STREAMING;
374
375 return result;
[email protected]5e5e0c82012-01-25 09:12:29376}
377
[email protected]926360f2012-06-29 04:45:02378void BufferedResourceHandler::CallReplayReadCompleted() {
379 bool defer = false;
380 if (!ReplayReadCompleted(&defer)) {
381 controller()->Cancel();
382 } else if (!defer) {
383 state_ = STATE_STREAMING;
384 controller()->Resume();
385 }
386}
387
388bool BufferedResourceHandler::MustDownload() {
389 if (must_download_is_set_)
390 return must_download_;
391
392 must_download_is_set_ = true;
393
394 std::string disposition;
395 request_->GetResponseHeaderByName("content-disposition", &disposition);
396 if (!disposition.empty() &&
397 net::HttpContentDisposition(disposition, std::string()).is_attachment()) {
398 must_download_ = true;
399 } else if (host_->delegate() &&
400 host_->delegate()->ShouldForceDownloadResource(
401 request_->url(), response_->head.mime_type)) {
402 must_download_ = true;
403 } else {
404 must_download_ = false;
405 }
406
407 return must_download_;
408}
409
410bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) {
411 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request_);
412
413 bool allow_wildcard = false;
414 webkit::WebPluginInfo plugin;
415 return PluginServiceImpl::GetInstance()->GetPluginInfo(
416 info->GetChildID(), info->GetRouteID(), info->GetContext(),
417 request_->url(), GURL(), response_->head.mime_type, allow_wildcard,
418 stale, &plugin, NULL);
419}
420
421bool BufferedResourceHandler::CopyReadBufferToNextHandler(int request_id) {
[email protected]5e5e0c82012-01-25 09:12:29422 if (!bytes_read_)
[email protected]926360f2012-06-29 04:45:02423 return true;
[email protected]5e5e0c82012-01-25 09:12:29424
425 net::IOBuffer* buf = NULL;
426 int buf_len = 0;
[email protected]926360f2012-06-29 04:45:02427 if (!next_handler_->OnWillRead(request_id, &buf, &buf_len, bytes_read_))
428 return false;
429
430 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0));
431 memcpy(buf->data(), read_buffer_->data(), bytes_read_);
432 return true;
[email protected]0dc72bbe2010-04-08 15:29:17433}
434
[email protected]d33e7cc2011-09-23 01:43:56435void BufferedResourceHandler::OnPluginsLoaded(
436 const std::vector<webkit::WebPluginInfo>& plugins) {
[email protected]22b305442012-05-21 18:02:59437 bool defer = false;
[email protected]926360f2012-06-29 04:45:02438 if (!ProcessResponse(&defer)) {
[email protected]56f0b082012-06-14 07:12:32439 controller()->Cancel();
[email protected]926360f2012-06-29 04:45:02440 } else if (!defer) {
[email protected]56f0b082012-06-14 07:12:32441 controller()->Resume();
[email protected]22b305442012-05-21 18:02:59442 }
[email protected]e3c404b2008-12-23 01:07:32443}
[email protected]d18720a2012-01-06 09:53:55444
445} // namespace content