[email protected] | 0a0b254 | 2012-01-03 08:25:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 5 | #include "content/browser/loader/buffered_resource_handler.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 6 | |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 5203e600 | 2009-07-29 03:42:00 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 11 | #include "base/metrics/histogram.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
[email protected] | 254ed74 | 2011-08-16 18:45:27 | [diff] [blame] | 13 | #include "content/browser/download/download_resource_handler.h" |
[email protected] | 14365e3 | 2012-01-24 01:12:22 | [diff] [blame] | 14 | #include "content/browser/download/download_stats.h" |
[email protected] | 3b45550 | 2012-12-11 18:22:58 | [diff] [blame] | 15 | #include "content/browser/loader/certificate_resource_handler.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 16 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 17 | #include "content/browser/loader/resource_request_info_impl.h" |
[email protected] | 6edb6488 | 2014-06-12 22:57:54 | [diff] [blame] | 18 | #include "content/browser/loader/stream_resource_handler.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 19 | #include "content/public/browser/content_browser_client.h" |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 20 | #include "content/public/browser/download_item.h" |
[email protected] | 29a5ffc8 | 2012-03-13 19:35:58 | [diff] [blame] | 21 | #include "content/public/browser/download_save_info.h" |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 22 | #include "content/public/browser/download_url_parameters.h" |
raymes | 65f5da4 | 2014-11-25 00:30:34 | [diff] [blame] | 23 | #include "content/public/browser/plugin_service.h" |
[email protected] | ce96786 | 2012-02-09 22:47:05 | [diff] [blame] | 24 | #include "content/public/browser/resource_context.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 25 | #include "content/public/browser/resource_dispatcher_host_delegate.h" |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 26 | #include "content/public/common/resource_response.h" |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 27 | #include "content/public/common/webplugininfo.h" |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 28 | #include "net/base/io_buffer.h" |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 29 | #include "net/base/mime_sniffer.h" |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 30 | #include "net/base/mime_util.h" |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 31 | #include "net/base/net_errors.h" |
[email protected] | 28ca9fb | 2012-01-28 14:50:24 | [diff] [blame] | 32 | #include "net/http/http_content_disposition.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 33 | #include "net/http/http_response_headers.h" |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 34 | |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 35 | namespace content { |
| 36 | |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | void RecordSnifferMetrics(bool sniffing_blocked, |
| 40 | bool we_would_like_to_sniff, |
| 41 | const std::string& mime_type) { |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 42 | static base::HistogramBase* nosniff_usage(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 43 | if (!nosniff_usage) |
| 44 | nosniff_usage = base::BooleanHistogram::FactoryGet( |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 45 | "nosniff.usage", base::HistogramBase::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 46 | nosniff_usage->AddBoolean(sniffing_blocked); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 47 | |
| 48 | if (sniffing_blocked) { |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 49 | static base::HistogramBase* nosniff_otherwise(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 50 | if (!nosniff_otherwise) |
| 51 | nosniff_otherwise = base::BooleanHistogram::FactoryGet( |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 52 | "nosniff.otherwise", base::HistogramBase::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 53 | nosniff_otherwise->AddBoolean(we_would_like_to_sniff); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 54 | |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 55 | static base::HistogramBase* nosniff_empty_mime_type(NULL); |
[email protected] | 81ce9f3b | 2011-04-05 04:48:53 | [diff] [blame] | 56 | if (!nosniff_empty_mime_type) |
| 57 | nosniff_empty_mime_type = base::BooleanHistogram::FactoryGet( |
| 58 | "nosniff.empty_mime_type", |
[email protected] | de41555 | 2013-01-23 04:12:17 | [diff] [blame] | 59 | base::HistogramBase::kUmaTargetedHistogramFlag); |
[email protected] | e8829a19 | 2009-12-06 00:09:37 | [diff] [blame] | 60 | nosniff_empty_mime_type->AddBoolean(mime_type.empty()); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 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) { |
| 70 | } |
| 71 | |
| 72 | private: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 73 | ~DependentIOBuffer() override {} |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 74 | |
| 75 | scoped_refptr<net::IOBuffer> buf_; |
| 76 | }; |
| 77 | |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 78 | } // namespace |
| 79 | |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 80 | BufferedResourceHandler::BufferedResourceHandler( |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 81 | scoped_ptr<ResourceHandler> next_handler, |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 82 | ResourceDispatcherHostImpl* host, |
raymes | 65f5da4 | 2014-11-25 00:30:34 | [diff] [blame] | 83 | PluginService* plugin_service, |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 84 | net::URLRequest* request) |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 85 | : LayeredResourceHandler(request, next_handler.Pass()), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 86 | state_(STATE_STARTING), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 87 | host_(host), |
raymes | 65f5da4 | 2014-11-25 00:30:34 | [diff] [blame] | 88 | plugin_service_(plugin_service), |
[email protected] | dd9b546 | 2009-09-25 17:22:23 | [diff] [blame] | 89 | read_buffer_size_(0), |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 90 | bytes_read_(0), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 91 | must_download_(false), |
[email protected] | 5c1d3e5 | 2012-08-01 05:14:27 | [diff] [blame] | 92 | must_download_is_set_(false), |
| 93 | weak_ptr_factory_(this) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | BufferedResourceHandler::~BufferedResourceHandler() { |
| 97 | } |
| 98 | |
| 99 | void BufferedResourceHandler::SetController(ResourceController* controller) { |
| 100 | ResourceHandler::SetController(controller); |
| 101 | |
| 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); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 109 | bool BufferedResourceHandler::OnResponseStarted(ResourceResponse* response, |
| 110 | bool* defer) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 111 | response_ = response; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 112 | |
ricea | 68c95ddb2 | 2015-04-13 16:41:03 | [diff] [blame] | 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. |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 116 | if (!(response_->head.headers.get() && |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 117 | response_->head.headers->response_code() == 304)) { |
| 118 | if (ShouldSniffContent()) { |
| 119 | state_ = STATE_BUFFERING; |
| 120 | return true; |
| 121 | } |
| 122 | |
| 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"); |
| 128 | } |
[email protected] | 3ff67926 | 2012-10-12 23:40:05 | [diff] [blame] | 129 | |
| 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"); |
| 134 | } |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 135 | } |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 136 | |
| 137 | state_ = STATE_PROCESSING; |
| 138 | return ProcessResponse(defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 141 | // We'll let the original event handler provide a buffer, and reuse it for |
| 142 | // subsequent reads until we're done buffering. |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 143 | bool BufferedResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 144 | int* buf_size, |
| 145 | int min_size) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 146 | if (state_ == STATE_STREAMING) |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 147 | return next_handler_->OnWillRead(buf, buf_size, min_size); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 148 | |
| 149 | DCHECK_EQ(-1, min_size); |
| 150 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 151 | if (read_buffer_.get()) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 152 | CHECK_LT(bytes_read_, read_buffer_size_); |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 153 | *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 154 | *buf_size = read_buffer_size_ - bytes_read_; |
| 155 | } else { |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 156 | if (!next_handler_->OnWillRead(buf, buf_size, min_size)) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 157 | return false; |
| 158 | |
| 159 | read_buffer_ = *buf; |
| 160 | read_buffer_size_ = *buf_size; |
| 161 | DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 162 | } |
[email protected] | 6719905 | 2009-06-12 21:15:33 | [diff] [blame] | 163 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 166 | bool BufferedResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 167 | if (state_ == STATE_STREAMING) |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 168 | return next_handler_->OnReadCompleted(bytes_read, defer); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 169 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 170 | DCHECK_EQ(state_, STATE_BUFFERING); |
| 171 | bytes_read_ += bytes_read; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 172 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 173 | if (!DetermineMimeType() && (bytes_read > 0)) |
| 174 | return true; // Needs more data, so keep buffering. |
| 175 | |
| 176 | state_ = STATE_PROCESSING; |
| 177 | return ProcessResponse(defer); |
| 178 | } |
| 179 | |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 180 | void BufferedResourceHandler::OnResponseCompleted( |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 181 | const net::URLRequestStatus& status, |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 182 | const std::string& security_info, |
| 183 | bool* defer) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 184 | // Upon completion, act like a pass-through handler in case the downstream |
| 185 | // handler defers OnResponseCompleted. |
| 186 | state_ = STATE_STREAMING; |
| 187 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 188 | next_handler_->OnResponseCompleted(status, security_info, defer); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void BufferedResourceHandler::Resume() { |
| 192 | switch (state_) { |
| 193 | case STATE_BUFFERING: |
| 194 | case STATE_PROCESSING: |
| 195 | NOTREACHED(); |
| 196 | break; |
| 197 | case STATE_REPLAYING: |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 198 | base::MessageLoop::current()->PostTask( |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 199 | FROM_HERE, |
| 200 | base::Bind(&BufferedResourceHandler::CallReplayReadCompleted, |
[email protected] | 5c1d3e5 | 2012-08-01 05:14:27 | [diff] [blame] | 201 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 202 | break; |
| 203 | case STATE_STARTING: |
| 204 | case STATE_STREAMING: |
| 205 | controller()->Resume(); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void BufferedResourceHandler::Cancel() { |
| 211 | controller()->Cancel(); |
| 212 | } |
| 213 | |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 214 | void BufferedResourceHandler::CancelAndIgnore() { |
| 215 | controller()->CancelAndIgnore(); |
| 216 | } |
| 217 | |
[email protected] | af3d496 | 2012-10-19 10:57:26 | [diff] [blame] | 218 | void BufferedResourceHandler::CancelWithError(int error_code) { |
| 219 | controller()->CancelWithError(error_code); |
| 220 | } |
| 221 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 222 | bool BufferedResourceHandler::ProcessResponse(bool* defer) { |
| 223 | DCHECK_EQ(STATE_PROCESSING, state_); |
| 224 | |
| 225 | // TODO(darin): Stop special-casing 304 responses. |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 226 | if (!(response_->head.headers.get() && |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 227 | response_->head.headers->response_code() == 304)) { |
| 228 | if (!SelectNextHandler(defer)) |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 229 | return false; |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 230 | if (*defer) |
[email protected] | 98d6f15 | 2011-09-29 19:35:51 | [diff] [blame] | 231 | return true; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | state_ = STATE_REPLAYING; |
| 235 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 236 | if (!next_handler_->OnResponseStarted(response_.get(), defer)) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 237 | return false; |
| 238 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 239 | if (!read_buffer_.get()) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 240 | state_ = STATE_STREAMING; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 241 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 244 | if (!*defer) |
| 245 | return ReplayReadCompleted(defer); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 246 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 247 | return true; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 250 | bool BufferedResourceHandler::ShouldSniffContent() { |
| 251 | const std::string& mime_type = response_->head.mime_type; |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 252 | |
| 253 | std::string content_type_options; |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 254 | request()->GetResponseHeaderByName("x-content-type-options", |
| 255 | &content_type_options); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 256 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 257 | bool sniffing_blocked = |
[email protected] | df80704 | 2014-08-13 16:48:41 | [diff] [blame] | 258 | LowerCaseEqualsASCII(content_type_options, "nosniff"); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 259 | bool we_would_like_to_sniff = |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 260 | net::ShouldSniffMimeType(request()->url(), mime_type); |
[email protected] | 30629139 | 2009-01-17 19:15:36 | [diff] [blame] | 261 | |
| 262 | RecordSnifferMetrics(sniffing_blocked, we_would_like_to_sniff, mime_type); |
| 263 | |
| 264 | if (!sniffing_blocked && we_would_like_to_sniff) { |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 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. |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 268 | VLOG(1) << "To buffer: " << request()->url().spec(); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 269 | return true; |
| 270 | } |
| 271 | |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 275 | bool BufferedResourceHandler::DetermineMimeType() { |
| 276 | DCHECK_EQ(STATE_BUFFERING, state_); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 277 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 278 | const std::string& type_hint = response_->head.mime_type; |
| 279 | |
| 280 | std::string new_type; |
| 281 | bool made_final_decision = |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 282 | net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 283 | type_hint, &new_type); |
| 284 | |
| 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); |
| 289 | |
| 290 | return made_final_decision; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 291 | } |
| 292 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 293 | bool BufferedResourceHandler::SelectNextHandler(bool* defer) { |
| 294 | DCHECK(!response_->head.mime_type.empty()); |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 295 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 296 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 297 | const std::string& mime_type = response_->head.mime_type; |
[email protected] | a755e107 | 2009-10-23 16:58:37 | [diff] [blame] | 298 | |
[email protected] | 3b45550 | 2012-12-11 18:22:58 | [diff] [blame] | 299 | if (net::IsSupportedCertificateMimeType(mime_type)) { |
| 300 | // Install certificate file. |
[email protected] | 6f3ac60 | 2014-02-13 21:38:05 | [diff] [blame] | 301 | info->set_is_download(true); |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 302 | scoped_ptr<ResourceHandler> handler( |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 303 | new CertificateResourceHandler(request())); |
[email protected] | 6edb6488 | 2014-06-12 22:57:54 | [diff] [blame] | 304 | return UseAlternateNextHandler(handler.Pass(), std::string()); |
[email protected] | 5a9d58fa | 2012-05-29 15:15:04 | [diff] [blame] | 305 | } |
| 306 | |
raymes | 9325d7ca3 | 2014-11-21 01:02:42 | [diff] [blame] | 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()); |
deepak.m1 | e3557dd | 2015-04-13 13:09:08 | [diff] [blame] | 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); |
| 316 | } |
raymes | 9325d7ca3 | 2014-11-21 01:02:42 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 319 | if (!info->allow_download()) |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 320 | return true; |
| 321 | |
raymes | 9325d7ca3 | 2014-11-21 01:02:42 | [diff] [blame] | 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); |
| 327 | |
[email protected] | 3ca004f7 | 2012-12-18 10:35:21 | [diff] [blame] | 328 | bool must_download = MustDownload(); |
| 329 | if (!must_download) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 330 | if (net::IsSupportedMimeType(mime_type)) |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 331 | return true; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 332 | |
deepak.m1 | e3557dd | 2015-04-13 13:09:08 | [diff] [blame] | 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); |
| 338 | } |
| 339 | |
| 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; |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 350 | return true; |
deepak.m1 | e3557dd | 2015-04-13 13:09:08 | [diff] [blame] | 351 | } |
| 352 | if (has_plugin) |
| 353 | return true; |
| 354 | #endif |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 355 | } |
| 356 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 357 | // Install download handler |
| 358 | info->set_is_download(true); |
| 359 | scoped_ptr<ResourceHandler> handler( |
| 360 | host_->CreateResourceHandlerForDownload( |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 361 | request(), |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 362 | true, // is_content_initiated |
[email protected] | 3ca004f7 | 2012-12-18 10:35:21 | [diff] [blame] | 363 | must_download, |
thestig | c4cac8f | 2014-09-04 21:17:50 | [diff] [blame] | 364 | DownloadItem::kInvalidId, |
[email protected] | c873c86 | 2012-10-17 15:32:12 | [diff] [blame] | 365 | scoped_ptr<DownloadSaveInfo>(new DownloadSaveInfo()), |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 366 | DownloadUrlParameters::OnStartedCallback())); |
[email protected] | 6edb6488 | 2014-06-12 22:57:54 | [diff] [blame] | 367 | return UseAlternateNextHandler(handler.Pass(), std::string()); |
[email protected] | 35fa6a2 | 2009-08-15 00:04:01 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 370 | bool BufferedResourceHandler::UseAlternateNextHandler( |
[email protected] | 6edb6488 | 2014-06-12 22:57:54 | [diff] [blame] | 371 | scoped_ptr<ResourceHandler> new_handler, |
| 372 | const std::string& payload_for_old_handler) { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 373 | if (response_->head.headers.get() && // Can be NULL if FTP. |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 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. |
[email protected] | 550cddd7 | 2014-02-20 02:29:09 | [diff] [blame] | 380 | request()->CancelWithError(net::ERR_INVALID_RESPONSE); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 384 | // Inform the original ResourceHandler that this will be handled entirely by |
| 385 | // the new ResourceHandler. |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 386 | // TODO(darin): We should probably check the return values of these. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 387 | bool defer_ignored = false; |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 388 | next_handler_->OnResponseStarted(response_.get(), &defer_ignored); |
[email protected] | 6f3ac60 | 2014-02-13 21:38:05 | [diff] [blame] | 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. |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 392 | DCHECK(!defer_ignored); |
[email protected] | 6edb6488 | 2014-06-12 22:57:54 | [diff] [blame] | 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; |
| 401 | |
[email protected] | 1b5cd52 | 2014-06-20 08:20:19 | [diff] [blame] | 402 | next_handler_->OnWillRead(&buf, &size, -1); |
[email protected] | 6edb6488 | 2014-06-12 22:57:54 | [diff] [blame] | 403 | CHECK_GE(size, static_cast<int>(payload_for_old_handler.length())); |
| 404 | |
| 405 | memcpy(buf->data(), payload_for_old_handler.c_str(), |
| 406 | payload_for_old_handler.length()); |
| 407 | |
| 408 | next_handler_->OnReadCompleted(payload_for_old_handler.length(), |
| 409 | &defer_ignored); |
| 410 | DCHECK(!defer_ignored); |
| 411 | |
| 412 | net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0); |
| 413 | next_handler_->OnResponseCompleted(status, std::string(), &defer_ignored); |
| 414 | DCHECK(!defer_ignored); |
| 415 | } |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 416 | |
| 417 | // This is handled entirely within the new ResourceHandler, so just reset the |
| 418 | // original ResourceHandler. |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 419 | next_handler_ = new_handler.Pass(); |
| 420 | next_handler_->SetController(this); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 421 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 422 | return CopyReadBufferToNextHandler(); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 425 | bool BufferedResourceHandler::ReplayReadCompleted(bool* defer) { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 426 | DCHECK(read_buffer_.get()); |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 427 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 428 | bool result = next_handler_->OnReadCompleted(bytes_read_, defer); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 429 | |
| 430 | read_buffer_ = NULL; |
| 431 | read_buffer_size_ = 0; |
| 432 | bytes_read_ = 0; |
| 433 | |
| 434 | state_ = STATE_STREAMING; |
| 435 | |
| 436 | return result; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 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(); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | bool BufferedResourceHandler::MustDownload() { |
| 450 | if (must_download_is_set_) |
| 451 | return must_download_; |
| 452 | |
| 453 | must_download_is_set_ = true; |
| 454 | |
| 455 | std::string disposition; |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 456 | request()->GetResponseHeaderByName("content-disposition", &disposition); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 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( |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 462 | request()->url(), response_->head.mime_type)) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 463 | must_download_ = true; |
| 464 | } else { |
| 465 | must_download_ = false; |
| 466 | } |
| 467 | |
| 468 | return must_download_; |
| 469 | } |
| 470 | |
deepak.m1 | e3557dd | 2015-04-13 13:09:08 | [diff] [blame] | 471 | bool BufferedResourceHandler::HasSupportingPlugin(bool* stale) { |
| 472 | #if defined(ENABLE_PLUGINS) |
| 473 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 474 | |
| 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 |
| 486 | } |
| 487 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 488 | bool BufferedResourceHandler::CopyReadBufferToNextHandler() { |
[email protected] | 17b674e0 | 2014-05-10 04:30:02 | [diff] [blame] | 489 | if (!read_buffer_.get()) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 490 | return true; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 491 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 492 | scoped_refptr<net::IOBuffer> buf; |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 493 | int buf_len = 0; |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 494 | if (!next_handler_->OnWillRead(&buf, &buf_len, bytes_read_)) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 495 | return false; |
| 496 | |
| 497 | CHECK((buf_len >= bytes_read_) && (bytes_read_ >= 0)); |
| 498 | memcpy(buf->data(), read_buffer_->data(), bytes_read_); |
| 499 | return true; |
[email protected] | 0dc72bbe | 2010-04-08 15:29:17 | [diff] [blame] | 500 | } |
| 501 | |
[email protected] | d33e7cc | 2011-09-23 01:43:56 | [diff] [blame] | 502 | void BufferedResourceHandler::OnPluginsLoaded( |
[email protected] | d7bd3e5 | 2013-07-21 04:29:20 | [diff] [blame] | 503 | const std::vector<WebPluginInfo>& plugins) { |
[email protected] | ae9c092 | 2014-02-13 22:40:23 | [diff] [blame] | 504 | request()->LogUnblocked(); |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 505 | bool defer = false; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 506 | if (!ProcessResponse(&defer)) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 507 | controller()->Cancel(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 508 | } else if (!defer) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 509 | controller()->Resume(); |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 510 | } |
[email protected] | e3c404b | 2008-12-23 01:07:32 | [diff] [blame] | 511 | } |
[email protected] | d18720a | 2012-01-06 09:53:55 | [diff] [blame] | 512 | |
| 513 | } // namespace content |