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
3 // found in the LICENSE file.
5 #include "content/browser/loader/buffered_resource_handler.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_util.h"
13 #include "content/browser/download/download_resource_handler.h"
14 #include "content/browser/download/download_stats.h"
15 #include "content/browser/loader/certificate_resource_handler.h"
16 #include "content/browser/loader/resource_dispatcher_host_impl.h"
17 #include "content/browser/loader/resource_request_info_impl.h"
18 #include "content/browser/loader/stream_resource_handler.h"
19 #include "content/browser/plugin_service_impl.h"
20 #include "content/public/browser/content_browser_client.h"
21 #include "content/public/browser/download_item.h"
22 #include "content/public/browser/download_save_info.h"
23 #include "content/public/browser/download_url_parameters.h"
24 #include "content/public/browser/resource_context.h"
25 #include "content/public/browser/resource_dispatcher_host_delegate.h"
26 #include "content/public/common/resource_response.h"
27 #include "content/public/common/webplugininfo.h"
28 #include "net/base/io_buffer.h"
29 #include "net/base/mime_sniffer.h"
30 #include "net/base/mime_util.h"
31 #include "net/base/net_errors.h"
32 #include "net/http/http_content_disposition.h"
33 #include "net/http/http_response_headers.h"
39 void RecordSnifferMetrics(bool sniffing_blocked
,
40 bool we_would_like_to_sniff
,
41 const std::string
& mime_type
) {
42 static base::HistogramBase
* nosniff_usage(NULL
);
44 nosniff_usage
= base::BooleanHistogram::FactoryGet(
45 "nosniff.usage", base::HistogramBase::kUmaTargetedHistogramFlag
);
46 nosniff_usage
->AddBoolean(sniffing_blocked
);
48 if (sniffing_blocked
) {
49 static base::HistogramBase
* nosniff_otherwise(NULL
);
50 if (!nosniff_otherwise
)
51 nosniff_otherwise
= base::BooleanHistogram::FactoryGet(
52 "nosniff.otherwise", base::HistogramBase::kUmaTargetedHistogramFlag
);
53 nosniff_otherwise
->AddBoolean(we_would_like_to_sniff
);
55 static base::HistogramBase
* nosniff_empty_mime_type(NULL
);
56 if (!nosniff_empty_mime_type
)
57 nosniff_empty_mime_type
= base::BooleanHistogram::FactoryGet(
58 "nosniff.empty_mime_type",
59 base::HistogramBase::kUmaTargetedHistogramFlag
);
60 nosniff_empty_mime_type
->AddBoolean(mime_type
.empty());
64 // Used to write into an existing IOBuffer at a given offset.
65 class DependentIOBuffer
: public net::WrappedIOBuffer
{
67 DependentIOBuffer(net::IOBuffer
* buf
, int offset
)
68 : net::WrappedIOBuffer(buf
->data() + offset
),
73 virtual ~DependentIOBuffer() {}
75 scoped_refptr
<net::IOBuffer
> buf_
;
80 BufferedResourceHandler::BufferedResourceHandler(
81 scoped_ptr
<ResourceHandler
> next_handler
,
82 ResourceDispatcherHostImpl
* host
,
83 net::URLRequest
* request
)
84 : LayeredResourceHandler(request
, next_handler
.Pass()),
85 state_(STATE_STARTING
),
89 must_download_(false),
90 must_download_is_set_(false),
91 weak_ptr_factory_(this) {
94 BufferedResourceHandler::~BufferedResourceHandler() {
97 void BufferedResourceHandler::SetController(ResourceController
* controller
) {
98 ResourceHandler::SetController(controller
);
100 // Downstream handlers see us as their ResourceController, which allows us to
101 // consume part or all of the resource response, and then later replay it to
102 // downstream handler.
103 DCHECK(next_handler_
.get());
104 next_handler_
->SetController(this);
107 bool BufferedResourceHandler::OnResponseStarted(ResourceResponse
* response
,
109 response_
= response
;
111 // 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.
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.
121 if (!(response_
->head
.headers
.get() &&
122 response_
->head
.headers
->response_code() == 304)) {
123 if (ShouldSniffContent()) {
124 state_
= STATE_BUFFERING
;
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");
135 // Treat feed types as text/plain.
136 if (response_
->head
.mime_type
== "application/rss+xml" ||
137 response_
->head
.mime_type
== "application/atom+xml") {
138 response_
->head
.mime_type
.assign("text/plain");
142 state_
= STATE_PROCESSING
;
143 return ProcessResponse(defer
);
146 // We'll let the original event handler provide a buffer, and reuse it for
147 // subsequent reads until we're done buffering.
148 bool BufferedResourceHandler::OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
151 if (state_
== STATE_STREAMING
)
152 return next_handler_
->OnWillRead(buf
, buf_size
, min_size
);
154 DCHECK_EQ(-1, min_size
);
156 if (read_buffer_
.get()) {
157 CHECK_LT(bytes_read_
, read_buffer_size_
);
158 *buf
= new DependentIOBuffer(read_buffer_
.get(), bytes_read_
);
159 *buf_size
= read_buffer_size_
- bytes_read_
;
161 if (!next_handler_
->OnWillRead(buf
, buf_size
, min_size
))
165 read_buffer_size_
= *buf_size
;
166 DCHECK_GE(read_buffer_size_
, net::kMaxBytesToSniff
* 2);
171 bool BufferedResourceHandler::OnReadCompleted(int bytes_read
, bool* defer
) {
172 if (state_
== STATE_STREAMING
)
173 return next_handler_
->OnReadCompleted(bytes_read
, defer
);
175 DCHECK_EQ(state_
, STATE_BUFFERING
);
176 bytes_read_
+= bytes_read
;
178 if (!DetermineMimeType() && (bytes_read
> 0))
179 return true; // Needs more data, so keep buffering.
181 state_
= STATE_PROCESSING
;
182 return ProcessResponse(defer
);
185 void BufferedResourceHandler::OnResponseCompleted(
186 const net::URLRequestStatus
& status
,
187 const std::string
& security_info
,
189 // Upon completion, act like a pass-through handler in case the downstream
190 // handler defers OnResponseCompleted.
191 state_
= STATE_STREAMING
;
193 next_handler_
->OnResponseCompleted(status
, security_info
, defer
);
196 void BufferedResourceHandler::Resume() {
198 case STATE_BUFFERING
:
199 case STATE_PROCESSING
:
202 case STATE_REPLAYING
:
203 base::MessageLoop::current()->PostTask(
205 base::Bind(&BufferedResourceHandler::CallReplayReadCompleted
,
206 weak_ptr_factory_
.GetWeakPtr()));
209 case STATE_STREAMING
:
210 controller()->Resume();
215 void BufferedResourceHandler::Cancel() {
216 controller()->Cancel();
219 void BufferedResourceHandler::CancelAndIgnore() {
220 controller()->CancelAndIgnore();
223 void BufferedResourceHandler::CancelWithError(int error_code
) {
224 controller()->CancelWithError(error_code
);
227 bool BufferedResourceHandler::ProcessResponse(bool* defer
) {
228 DCHECK_EQ(STATE_PROCESSING
, state_
);
230 // TODO(darin): Stop special-casing 304 responses.
231 if (!(response_
->head
.headers
.get() &&
232 response_
->head
.headers
->response_code() == 304)) {
233 if (!SelectNextHandler(defer
))
239 state_
= STATE_REPLAYING
;
241 if (!next_handler_
->OnResponseStarted(response_
.get(), defer
))
244 if (!read_buffer_
.get()) {
245 state_
= STATE_STREAMING
;
250 return ReplayReadCompleted(defer
);
255 bool BufferedResourceHandler::ShouldSniffContent() {
256 const std::string
& mime_type
= response_
->head
.mime_type
;
258 std::string content_type_options
;
259 request()->GetResponseHeaderByName("x-content-type-options",
260 &content_type_options
);
262 bool sniffing_blocked
=
263 LowerCaseEqualsASCII(content_type_options
, "nosniff");
264 bool we_would_like_to_sniff
=
265 net::ShouldSniffMimeType(request()->url(), mime_type
);
267 RecordSnifferMetrics(sniffing_blocked
, we_would_like_to_sniff
, mime_type
);
269 if (!sniffing_blocked
&& we_would_like_to_sniff
) {
270 // We're going to look at the data before deciding what the content type
271 // is. That means we need to delay sending the ResponseStarted message
272 // over the IPC channel.
273 VLOG(1) << "To buffer: " << request()->url().spec();
280 bool BufferedResourceHandler::DetermineMimeType() {
281 DCHECK_EQ(STATE_BUFFERING
, state_
);
283 const std::string
& type_hint
= response_
->head
.mime_type
;
285 std::string new_type
;
286 bool made_final_decision
=
287 net::SniffMimeType(read_buffer_
->data(), bytes_read_
, request()->url(),
288 type_hint
, &new_type
);
290 // SniffMimeType() returns false if there is not enough data to determine
291 // the mime type. However, even if it returns false, it returns a new type
292 // that is probably better than the current one.
293 response_
->head
.mime_type
.assign(new_type
);
295 return made_final_decision
;
298 bool BufferedResourceHandler::SelectNextHandler(bool* defer
) {
299 DCHECK(!response_
->head
.mime_type
.empty());
301 ResourceRequestInfoImpl
* info
= GetRequestInfo();
302 const std::string
& mime_type
= response_
->head
.mime_type
;
304 if (net::IsSupportedCertificateMimeType(mime_type
)) {
305 // Install certificate file.
306 info
->set_is_download(true);
307 scoped_ptr
<ResourceHandler
> handler(
308 new CertificateResourceHandler(request()));
309 return UseAlternateNextHandler(handler
.Pass(), std::string());
312 if (!info
->allow_download())
315 bool must_download
= MustDownload();
316 if (!must_download
) {
317 if (net::IsSupportedMimeType(mime_type
))
321 scoped_ptr
<ResourceHandler
> handler(
322 host_
->MaybeInterceptAsStream(request(), response_
.get(), &payload
));
324 return UseAlternateNextHandler(handler
.Pass(), payload
);
327 #if defined(ENABLE_PLUGINS)
329 bool has_plugin
= HasSupportingPlugin(&stale
);
331 // Refresh the plugins asynchronously.
332 PluginServiceImpl::GetInstance()->GetPlugins(
333 base::Bind(&BufferedResourceHandler::OnPluginsLoaded
,
334 weak_ptr_factory_
.GetWeakPtr()));
335 request()->LogBlockedBy("BufferedResourceHandler");
344 // Install download handler
345 info
->set_is_download(true);
346 scoped_ptr
<ResourceHandler
> handler(
347 host_
->CreateResourceHandlerForDownload(
349 true, // is_content_initiated
351 content::DownloadItem::kInvalidId
,
352 scoped_ptr
<DownloadSaveInfo
>(new DownloadSaveInfo()),
353 DownloadUrlParameters::OnStartedCallback()));
354 return UseAlternateNextHandler(handler
.Pass(), std::string());
357 bool BufferedResourceHandler::UseAlternateNextHandler(
358 scoped_ptr
<ResourceHandler
> new_handler
,
359 const std::string
& payload_for_old_handler
) {
360 if (response_
->head
.headers
.get() && // Can be NULL if FTP.
361 response_
->head
.headers
->response_code() / 100 != 2) {
362 // The response code indicates that this is an error page, but we don't
363 // know how to display the content. We follow Firefox here and show our
364 // own error page instead of triggering a download.
365 // TODO(abarth): We should abstract the response_code test, but this kind
366 // of check is scattered throughout our codebase.
367 request()->CancelWithError(net::ERR_INVALID_RESPONSE
);
371 // Inform the original ResourceHandler that this will be handled entirely by
372 // the new ResourceHandler.
373 // TODO(darin): We should probably check the return values of these.
374 bool defer_ignored
= false;
375 next_handler_
->OnResponseStarted(response_
.get(), &defer_ignored
);
376 // Although deferring OnResponseStarted is legal, the only downstream handler
377 // which does so is CrossSiteResourceHandler. Cross-site transitions should
378 // not trigger when switching handlers.
379 DCHECK(!defer_ignored
);
380 if (payload_for_old_handler
.empty()) {
381 net::URLRequestStatus
status(net::URLRequestStatus::CANCELED
,
383 next_handler_
->OnResponseCompleted(status
, std::string(), &defer_ignored
);
384 DCHECK(!defer_ignored
);
386 scoped_refptr
<net::IOBuffer
> buf
;
389 next_handler_
->OnWillRead(&buf
, &size
, -1);
390 CHECK_GE(size
, static_cast<int>(payload_for_old_handler
.length()));
392 memcpy(buf
->data(), payload_for_old_handler
.c_str(),
393 payload_for_old_handler
.length());
395 next_handler_
->OnReadCompleted(payload_for_old_handler
.length(),
397 DCHECK(!defer_ignored
);
399 net::URLRequestStatus
status(net::URLRequestStatus::SUCCESS
, 0);
400 next_handler_
->OnResponseCompleted(status
, std::string(), &defer_ignored
);
401 DCHECK(!defer_ignored
);
404 // This is handled entirely within the new ResourceHandler, so just reset the
405 // original ResourceHandler.
406 next_handler_
= new_handler
.Pass();
407 next_handler_
->SetController(this);
409 return CopyReadBufferToNextHandler();
412 bool BufferedResourceHandler::ReplayReadCompleted(bool* defer
) {
413 DCHECK(read_buffer_
.get());
415 bool result
= next_handler_
->OnReadCompleted(bytes_read_
, defer
);
418 read_buffer_size_
= 0;
421 state_
= STATE_STREAMING
;
426 void BufferedResourceHandler::CallReplayReadCompleted() {
428 if (!ReplayReadCompleted(&defer
)) {
429 controller()->Cancel();
431 state_
= STATE_STREAMING
;
432 controller()->Resume();
436 bool BufferedResourceHandler::MustDownload() {
437 if (must_download_is_set_
)
438 return must_download_
;
440 must_download_is_set_
= true;
442 std::string disposition
;
443 request()->GetResponseHeaderByName("content-disposition", &disposition
);
444 if (!disposition
.empty() &&
445 net::HttpContentDisposition(disposition
, std::string()).is_attachment()) {
446 must_download_
= true;
447 } else if (host_
->delegate() &&
448 host_
->delegate()->ShouldForceDownloadResource(
449 request()->url(), response_
->head
.mime_type
)) {
450 must_download_
= true;
452 must_download_
= false;
455 return must_download_
;
458 bool BufferedResourceHandler::HasSupportingPlugin(bool* stale
) {
459 #if defined(ENABLE_PLUGINS)
460 ResourceRequestInfoImpl
* info
= GetRequestInfo();
462 bool allow_wildcard
= false;
463 WebPluginInfo plugin
;
464 return PluginServiceImpl::GetInstance()->GetPluginInfo(
465 info
->GetChildID(), info
->GetRenderFrameID(), info
->GetContext(),
466 request()->url(), GURL(), response_
->head
.mime_type
, allow_wildcard
,
467 stale
, &plugin
, NULL
);
475 bool BufferedResourceHandler::CopyReadBufferToNextHandler() {
476 if (!read_buffer_
.get())
479 scoped_refptr
<net::IOBuffer
> buf
;
481 if (!next_handler_
->OnWillRead(&buf
, &buf_len
, bytes_read_
))
484 CHECK((buf_len
>= bytes_read_
) && (bytes_read_
>= 0));
485 memcpy(buf
->data(), read_buffer_
->data(), bytes_read_
);
489 void BufferedResourceHandler::OnPluginsLoaded(
490 const std::vector
<WebPluginInfo
>& plugins
) {
491 request()->LogUnblocked();
493 if (!ProcessResponse(&defer
)) {
494 controller()->Cancel();
496 controller()->Resume();
500 } // namespace content