Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / loader / buffered_resource_handler.cc
bloba38eaa915ea449de9272b85d1356ff658b9f41ec
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"
7 #include <vector>
9 #include "base/bind.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/public/browser/content_browser_client.h"
20 #include "content/public/browser/download_item.h"
21 #include "content/public/browser/download_save_info.h"
22 #include "content/public/browser/download_url_parameters.h"
23 #include "content/public/browser/plugin_service.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"
35 namespace content {
37 namespace {
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);
43 if (!nosniff_usage)
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 {
66 public:
67 DependentIOBuffer(net::IOBuffer* buf, int offset)
68 : net::WrappedIOBuffer(buf->data() + offset),
69 buf_(buf) {
72 private:
73 ~DependentIOBuffer() override {}
75 scoped_refptr<net::IOBuffer> buf_;
78 } // namespace
80 BufferedResourceHandler::BufferedResourceHandler(
81 scoped_ptr<ResourceHandler> next_handler,
82 ResourceDispatcherHostImpl* host,
83 PluginService* plugin_service,
84 net::URLRequest* request)
85 : LayeredResourceHandler(request, next_handler.Pass()),
86 state_(STATE_STARTING),
87 host_(host),
88 plugin_service_(plugin_service),
89 read_buffer_size_(0),
90 bytes_read_(0),
91 must_download_(false),
92 must_download_is_set_(false),
93 weak_ptr_factory_(this) {
96 BufferedResourceHandler::~BufferedResourceHandler() {
99 void BufferedResourceHandler::SetController(ResourceController* controller) {
100 ResourceHandler::SetController(controller);
102 // Downstream handlers see us as their ResourceController, which allows us to
103 // consume part or all of the resource response, and then later replay it to
104 // downstream handler.
105 DCHECK(next_handler_.get());
106 next_handler_->SetController(this);
109 bool BufferedResourceHandler::OnResponseStarted(ResourceResponse* response,
110 bool* defer) {
111 response_ = response;
113 // A 304 response should not contain a Content-Type header (RFC 7232 section
114 // 4.1). The following code may incorrectly attempt to add a Content-Type to
115 // the response, and so must be skipped for 304 responses.
116 if (!(response_->head.headers.get() &&
117 response_->head.headers->response_code() == 304)) {
118 if (ShouldSniffContent()) {
119 state_ = STATE_BUFFERING;
120 return true;
123 if (response_->head.mime_type.empty()) {
124 // Ugg. The server told us not to sniff the content but didn't give us
125 // a mime type. What's a browser to do? Turns out, we're supposed to
126 // treat the response as "text/plain". This is the most secure option.
127 response_->head.mime_type.assign("text/plain");
130 // Treat feed types as text/plain.
131 if (response_->head.mime_type == "application/rss+xml" ||
132 response_->head.mime_type == "application/atom+xml") {
133 response_->head.mime_type.assign("text/plain");
137 state_ = STATE_PROCESSING;
138 return ProcessResponse(defer);
141 // We'll let the original event handler provide a buffer, and reuse it for
142 // subsequent reads until we're done buffering.
143 bool BufferedResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
144 int* buf_size,
145 int min_size) {
146 if (state_ == STATE_STREAMING)
147 return next_handler_->OnWillRead(buf, buf_size, min_size);
149 DCHECK_EQ(-1, min_size);
151 if (read_buffer_.get()) {
152 CHECK_LT(bytes_read_, read_buffer_size_);
153 *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_);
154 *buf_size = read_buffer_size_ - bytes_read_;
155 } else {
156 if (!next_handler_->OnWillRead(buf, buf_size, min_size))
157 return false;
159 read_buffer_ = *buf;
160 read_buffer_size_ = *buf_size;
161 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2);
163 return true;
166 bool BufferedResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
167 if (state_ == STATE_STREAMING)
168 return next_handler_->OnReadCompleted(bytes_read, defer);
170 DCHECK_EQ(state_, STATE_BUFFERING);
171 bytes_read_ += bytes_read;
173 if (!DetermineMimeType() && (bytes_read > 0))
174 return true; // Needs more data, so keep buffering.
176 state_ = STATE_PROCESSING;
177 return ProcessResponse(defer);
180 void BufferedResourceHandler::OnResponseCompleted(
181 const net::URLRequestStatus& status,
182 const std::string& security_info,
183 bool* defer) {
184 // Upon completion, act like a pass-through handler in case the downstream
185 // handler defers OnResponseCompleted.
186 state_ = STATE_STREAMING;
188 next_handler_->OnResponseCompleted(status, security_info, defer);
191 void BufferedResourceHandler::Resume() {
192 switch (state_) {
193 case STATE_BUFFERING:
194 case STATE_PROCESSING:
195 NOTREACHED();
196 break;
197 case STATE_REPLAYING:
198 base::MessageLoop::current()->PostTask(
199 FROM_HERE,
200 base::Bind(&BufferedResourceHandler::CallReplayReadCompleted,
201 weak_ptr_factory_.GetWeakPtr()));
202 break;
203 case STATE_STARTING:
204 case STATE_STREAMING:
205 controller()->Resume();
206 break;
210 void BufferedResourceHandler::Cancel() {
211 controller()->Cancel();
214 void BufferedResourceHandler::CancelAndIgnore() {
215 controller()->CancelAndIgnore();
218 void BufferedResourceHandler::CancelWithError(int error_code) {
219 controller()->CancelWithError(error_code);
222 bool BufferedResourceHandler::ProcessResponse(bool* defer) {
223 DCHECK_EQ(STATE_PROCESSING, state_);
225 // TODO(darin): Stop special-casing 304 responses.
226 if (!(response_->head.headers.get() &&
227 response_->head.headers->response_code() == 304)) {
228 if (!SelectNextHandler(defer))
229 return false;
230 if (*defer)
231 return true;
234 state_ = STATE_REPLAYING;
236 if (!next_handler_->OnResponseStarted(response_.get(), defer))
237 return false;
239 if (!read_buffer_.get()) {
240 state_ = STATE_STREAMING;
241 return true;
244 if (!*defer)
245 return ReplayReadCompleted(defer);
247 return true;
250 bool BufferedResourceHandler::ShouldSniffContent() {
251 const std::string& mime_type = response_->head.mime_type;
253 std::string content_type_options;
254 request()->GetResponseHeaderByName("x-content-type-options",
255 &content_type_options);
257 bool sniffing_blocked =
258 LowerCaseEqualsASCII(content_type_options, "nosniff");
259 bool we_would_like_to_sniff =
260 net::ShouldSniffMimeType(request()->url(), mime_type);
262 RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type);
264 if (!sniffing_blocked && we_would_like_to_sniff) {
265 // We're going to look at the data before deciding what the content type
266 // is. That means we need to delay sending the ResponseStarted message
267 // over the IPC channel.
268 VLOG(1) << "To buffer: " << request()->url().spec();
269 return true;
272 return false;
275 bool BufferedResourceHandler::DetermineMimeType() {
276 DCHECK_EQ(STATE_BUFFERING, state_);
278 const std::string& type_hint = response_->head.mime_type;
280 std::string new_type;
281 bool made_final_decision =
282 net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(),
283 type_hint, &new_type);
285 // SniffMimeType() returns false if there is not enough data to determine
286 // the mime type. However, even if it returns false, it returns a new type
287 // that is probably better than the current one.
288 response_->head.mime_type.assign(new_type);
290 return made_final_decision;
293 bool BufferedResourceHandler::SelectNextHandler(bool* defer) {
294 DCHECK(!response_->head.mime_type.empty());
296 ResourceRequestInfoImpl* info = GetRequestInfo();
297 const std::string& mime_type = response_->head.mime_type;
299 if (net::IsSupportedCertificateMimeType(mime_type)) {
300 // Install certificate file.
301 info->set_is_download(true);
302 scoped_ptr<ResourceHandler> handler(
303 new CertificateResourceHandler(request()));
304 return UseAlternateNextHandler(handler.Pass(), std::string());
307 // Allow requests for object/embed tags to be intercepted as streams.
308 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) {
309 DCHECK(!info->allow_download());
310 std::string payload;
311 scoped_ptr<ResourceHandler> handler(
312 host_->MaybeInterceptAsStream(request(), response_.get(), &payload));
313 if (handler) {
314 DCHECK(!net::IsSupportedMimeType(mime_type));
315 return UseAlternateNextHandler(handler.Pass(), payload);
319 if (!info->allow_download())
320 return true;
322 // info->allow_download() == true implies
323 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or
324 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME.
325 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME ||
326 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME);
328 bool must_download = MustDownload();
329 if (!must_download) {
330 if (net::IsSupportedMimeType(mime_type))
331 return true;
333 std::string payload;
334 scoped_ptr<ResourceHandler> handler(
335 host_->MaybeInterceptAsStream(request(), response_.get(), &payload));
336 if (handler) {
337 return UseAlternateNextHandler(handler.Pass(), payload);
340 #if defined(ENABLE_PLUGINS)
341 bool stale;
342 bool has_plugin = HasSupportingPlugin(&stale);
343 if (stale) {
344 // Refresh the plugins asynchronously.
345 plugin_service_->GetPlugins(
346 base::Bind(&BufferedResourceHandler::OnPluginsLoaded,
347 weak_ptr_factory_.GetWeakPtr()));
348 request()->LogBlockedBy("BufferedResourceHandler");
349 *defer = true;
350 return true;
352 if (has_plugin)
353 return true;
354 #endif
357 // Install download handler
358 info->set_is_download(true);
359 scoped_ptr<ResourceHandler> handler(
360 host_->CreateResourceHandlerForDownload(
361 request(),
362 true, // is_content_initiated
363 must_download,
364 DownloadItem::kInvalidId,
365 scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()),
366 DownloadUrlParameters::OnStartedCallback()));
367 return UseAlternateNextHandler(handler.Pass(), std::string());
370 bool BufferedResourceHandler::UseAlternateNextHandler(
371 scoped_ptr<ResourceHandler> new_handler,
372 const std::string& payload_for_old_handler) {
373 if (response_->head.headers.get() && // Can be NULL if FTP.
374 response_->head.headers->response_code() / 100 != 2) {
375 // The response code indicates that this is an error page, but we don't
376 // know how to display the content. We follow Firefox here and show our
377 // own error page instead of triggering a download.
378 // TODO(abarth): We should abstract the response_code test, but this kind
379 // of check is scattered throughout our codebase.
380 request()->CancelWithError(net::ERR_INVALID_RESPONSE);
381 return false;
384 // Inform the original ResourceHandler that this will be handled entirely by
385 // the new ResourceHandler.
386 // TODO(darin): We should probably check the return values of these.
387 bool defer_ignored = false;
388 next_handler_->OnResponseStarted(response_.get(), &defer_ignored);
389 // Although deferring OnResponseStarted is legal, the only downstream handler
390 // which does so is CrossSiteResourceHandler. Cross-site transitions should
391 // not trigger when switching handlers.
392 DCHECK(!defer_ignored);
393 if (payload_for_old_handler.empty()) {
394 net::URLRequestStatus status(net::URLRequestStatus::CANCELED,
395 net::ERR_ABORTED);
396 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
397 DCHECK(!defer_ignored);
398 } else {
399 scoped_refptr<net::IOBuffer> buf;
400 int size = 0;
402 next_handler_->OnWillRead(&buf, &size, -1);
403 CHECK_GE(size, static_cast<int>(payload_for_old_handler.length()));
405 memcpy(buf->data(), payload_for_old_handler.c_str(),
406 payload_for_old_handler.length());
408 next_handler_->OnReadCompleted(payload_for_old_handler.length(),
409 &defer_ignored);
410 DCHECK(!defer_ignored);
412 net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
413 next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored);
414 DCHECK(!defer_ignored);
417 // This is handled entirely within the new ResourceHandler, so just reset the
418 // original ResourceHandler.
419 next_handler_ = new_handler.Pass();
420 next_handler_->SetController(this);
422 return CopyReadBufferToNextHandler();
425 bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) {
426 DCHECK(read_buffer_.get());
428 bool result = next_handler_->OnReadCompleted(bytes_read_, defer);
430 read_buffer_ = NULL;
431 read_buffer_size_ = 0;
432 bytes_read_ = 0;
434 state_ = STATE_STREAMING;
436 return result;
439 void BufferedResourceHandler::CallReplayReadCompleted() {
440 bool defer = false;
441 if (!ReplayReadCompleted(&defer)) {
442 controller()->Cancel();
443 } else if (!defer) {
444 state_ = STATE_STREAMING;
445 controller()->Resume();
449 bool BufferedResourceHandler::MustDownload() {
450 if (must_download_is_set_)
451 return must_download_;
453 must_download_is_set_ = true;
455 std::string disposition;
456 request()->GetResponseHeaderByName("content-disposition", &disposition);
457 if (!disposition.empty() &&
458 net::HttpContentDisposition(disposition, std::string()).is_attachment()) {
459 must_download_ = true;
460 } else if (host_->delegate() &&
461 host_->delegate()->ShouldForceDownloadResource(
462 request()->url(), response_->head.mime_type)) {
463 must_download_ = true;
464 } else {
465 must_download_ = false;
468 return must_download_;
471 bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) {
472 #if defined(ENABLE_PLUGINS)
473 ResourceRequestInfoImpl* info = GetRequestInfo();
475 bool allow_wildcard = false;
476 WebPluginInfo plugin;
477 return plugin_service_->GetPluginInfo(
478 info->GetChildID(), info->GetRenderFrameID(), info->GetContext(),
479 request()->url(), GURL(), response_->head.mime_type, allow_wildcard,
480 stale, &plugin, NULL);
481 #else
482 if (stale)
483 *stale = false;
484 return false;
485 #endif
488 bool BufferedResourceHandler::CopyReadBufferToNextHandler() {
489 if (!read_buffer_.get())
490 return true;
492 scoped_refptr<net::IOBuffer> buf;
493 int buf_len = 0;
494 if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_))
495 return false;
497 CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0));
498 memcpy(buf->data(), read_buffer_->data(), bytes_read_);
499 return true;
502 void BufferedResourceHandler::OnPluginsLoaded(
503 const std::vector<WebPluginInfo>& plugins) {
504 request()->LogUnblocked();
505 bool defer = false;
506 if (!ProcessResponse(&defer)) {
507 controller()->Cancel();
508 } else if (!defer) {
509 controller()->Resume();
513 } // namespace content