| 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" | |
| 11 #include "base/location.h" | |
| 12 #include "base/logging.h" | 10 #include "base/logging.h" |
| 13 #include "base/metrics/histogram_macros.h" | |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "components/mime_util/mime_util.h" | 12 #include "components/mime_util/mime_util.h" |
| 18 #include "content/browser/download/download_resource_handler.h" | |
| 19 #include "content/browser/download/download_stats.h" | |
| 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" | |
| 21 #include "content/browser/loader/resource_request_info_impl.h" | 13 #include "content/browser/loader/resource_request_info_impl.h" |
| 22 #include "content/browser/loader/stream_resource_handler.h" | |
| 23 #include "content/public/browser/content_browser_client.h" | |
| 24 #include "content/public/browser/download_item.h" | |
| 25 #include "content/public/browser/download_save_info.h" | |
| 26 #include "content/public/browser/download_url_parameters.h" | |
| 27 #include "content/public/browser/plugin_service.h" | |
| 28 #include "content/public/browser/resource_context.h" | |
| 29 #include "content/public/browser/resource_dispatcher_host_delegate.h" | |
| 30 #include "content/public/common/resource_response.h" | 14 #include "content/public/common/resource_response.h" |
| 31 #include "content/public/common/webplugininfo.h" | |
| 32 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 33 #include "net/base/mime_sniffer.h" | 16 #include "net/base/mime_sniffer.h" |
| 34 #include "net/base/mime_util.h" | 17 #include "net/base/mime_util.h" |
| 35 #include "net/base/net_errors.h" | |
| 36 #include "net/http/http_content_disposition.h" | 18 #include "net/http/http_content_disposition.h" |
| 37 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
| 38 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 39 | 21 |
| 40 namespace content { | 22 namespace content { |
| 41 | 23 |
| 42 namespace { | 24 namespace { |
| 43 | 25 |
| 44 const char kAcceptHeader[] = "Accept"; | 26 const char kAcceptHeader[] = "Accept"; |
| 45 const char kFrameAcceptHeader[] = | 27 const char kFrameAcceptHeader[] = |
| 46 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | 28 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
| 47 "*/*;q=0.8"; | 29 "*/*;q=0.8"; |
| 48 const char kStylesheetAcceptHeader[] = "text/css,*/*;q=0.1"; | 30 const char kStylesheetAcceptHeader[] = "text/css,*/*;q=0.1"; |
| 49 const char kImageAcceptHeader[] = "image/webp,image/*,*/*;q=0.8"; | 31 const char kImageAcceptHeader[] = "image/webp,image/*,*/*;q=0.8"; |
| 50 const char kDefaultAcceptHeader[] = "*/*"; | 32 const char kDefaultAcceptHeader[] = "*/*"; |
| 51 | 33 |
| 52 // Used to write into an existing IOBuffer at a given offset. | 34 // Used to write into an existing IOBuffer at a given offset. |
| 53 class DependentIOBuffer : public net::WrappedIOBuffer { | 35 class DependentIOBuffer : public net::WrappedIOBuffer { |
| 54 public: | 36 public: |
| 55 DependentIOBuffer(net::IOBuffer* buf, int offset) | 37 DependentIOBuffer(net::IOBuffer* buf, int offset) |
| 56 : net::WrappedIOBuffer(buf->data() + offset), | 38 : net::WrappedIOBuffer(buf->data() + offset), buf_(buf) {} |
| 57 buf_(buf) { | |
| 58 } | |
| 59 | 39 |
| 60 private: | 40 private: |
| 61 ~DependentIOBuffer() override {} | 41 ~DependentIOBuffer() override {} |
| 62 | 42 |
| 63 scoped_refptr<net::IOBuffer> buf_; | 43 scoped_refptr<net::IOBuffer> buf_; |
| 64 }; | 44 }; |
| 65 | 45 |
| 66 } // namespace | 46 } // namespace |
| 67 | 47 |
| 68 MimeTypeResourceHandler::MimeTypeResourceHandler( | 48 MimeSniffingResourceHandler::MimeSniffingResourceHandler( |
| 69 std::unique_ptr<ResourceHandler> next_handler, | 49 std::unique_ptr<ResourceHandler> next_handler, |
| 70 ResourceDispatcherHostImpl* host, | |
| 71 PluginService* plugin_service, | |
| 72 net::URLRequest* request) | 50 net::URLRequest* request) |
| 73 : LayeredResourceHandler(request, std::move(next_handler)), | 51 : LayeredResourceHandler(request, std::move(next_handler)), |
| 74 state_(STATE_STARTING), | 52 state_(STATE_STARTING), |
| 75 host_(host), | |
| 76 #if defined(ENABLE_PLUGINS) | |
| 77 plugin_service_(plugin_service), | |
| 78 #endif | |
| 79 read_buffer_size_(0), | 53 read_buffer_size_(0), |
| 80 bytes_read_(0), | 54 bytes_read_(0) {} |
| 81 must_download_(false), | |
| 82 must_download_is_set_(false), | |
| 83 weak_ptr_factory_(this) { | |
| 84 } | |
| 85 | 55 |
| 86 MimeTypeResourceHandler::~MimeTypeResourceHandler() { | 56 MimeSniffingResourceHandler::~MimeSniffingResourceHandler() {} |
| 87 } | |
| 88 | 57 |
| 89 void MimeTypeResourceHandler::SetController(ResourceController* controller) { | 58 void MimeSniffingResourceHandler::SetController( |
| 59 ResourceController* controller) { |
| 90 ResourceHandler::SetController(controller); | 60 ResourceHandler::SetController(controller); |
| 91 | 61 |
| 92 // Downstream handlers see us as their ResourceController, which allows us to | 62 // Downstream handlers see the MimeSniffingResourceHandler as their |
| 93 // consume part or all of the resource response, and then later replay it to | 63 // ResourceController, which allows it to consume part or all of the resource |
| 94 // downstream handler. | 64 // response, and then later replay it to downstream handler. |
| 95 DCHECK(next_handler_.get()); | 65 DCHECK(next_handler_.get()); |
| 96 next_handler_->SetController(this); | 66 next_handler_->SetController(this); |
| 97 } | 67 } |
| 98 | 68 |
| 99 bool MimeTypeResourceHandler::OnResponseStarted(ResourceResponse* response, | 69 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; | 70 const char* accept_value = nullptr; |
| 133 switch (GetRequestInfo()->GetResourceType()) { | 71 switch (GetRequestInfo()->GetResourceType()) { |
| 134 case RESOURCE_TYPE_MAIN_FRAME: | 72 case RESOURCE_TYPE_MAIN_FRAME: |
| 135 case RESOURCE_TYPE_SUB_FRAME: | 73 case RESOURCE_TYPE_SUB_FRAME: |
| 136 accept_value = kFrameAcceptHeader; | 74 accept_value = kFrameAcceptHeader; |
| 137 break; | 75 break; |
| 138 case RESOURCE_TYPE_STYLESHEET: | 76 case RESOURCE_TYPE_STYLESHEET: |
| 139 accept_value = kStylesheetAcceptHeader; | 77 accept_value = kStylesheetAcceptHeader; |
| 140 break; | 78 break; |
| 141 case RESOURCE_TYPE_IMAGE: | 79 case RESOURCE_TYPE_IMAGE: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 161 NOTREACHED(); | 99 NOTREACHED(); |
| 162 break; | 100 break; |
| 163 } | 101 } |
| 164 | 102 |
| 165 // The false parameter prevents overwriting an existing accept header value, | 103 // 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. | 104 // which is needed because JS can manually set an accept header on an XHR. |
| 167 request()->SetExtraRequestHeaderByName(kAcceptHeader, accept_value, false); | 105 request()->SetExtraRequestHeaderByName(kAcceptHeader, accept_value, false); |
| 168 return next_handler_->OnWillStart(url, defer); | 106 return next_handler_->OnWillStart(url, defer); |
| 169 } | 107 } |
| 170 | 108 |
| 171 bool MimeTypeResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 109 bool MimeSniffingResourceHandler::OnResponseStarted(ResourceResponse* response, |
| 172 int* buf_size, | 110 bool* defer) { |
| 173 int min_size) { | 111 DCHECK_EQ(STATE_STARTING, state_); |
| 112 response_ = response; |
| 113 |
| 114 state_ = STATE_BUFFERING; |
| 115 |
| 116 // A 304 response should not contain a Content-Type header (RFC 7232 section |
| 117 // 4.1). The following code may incorrectly attempt to add a Content-Type to |
| 118 // the response, and so must be skipped for 304 responses. |
| 119 if (!(response_->head.headers.get() && |
| 120 response_->head.headers->response_code() == 304)) { |
| 121 if (ShouldSniffContent()) |
| 122 return true; |
| 123 |
| 124 if (response_->head.mime_type.empty()) { |
| 125 // Ugg. The server told us not to sniff the content but didn't give us a |
| 126 // mime type. What's a browser to do? Turns out, we're supposed to |
| 127 // treat the response as "text/plain". This is the most secure option. |
| 128 response_->head.mime_type.assign("text/plain"); |
| 129 } |
| 130 |
| 131 // Treat feed types as text/plain. |
| 132 if (response_->head.mime_type == "application/rss+xml" || |
| 133 response_->head.mime_type == "application/atom+xml") { |
| 134 response_->head.mime_type.assign("text/plain"); |
| 135 } |
| 136 } |
| 137 |
| 138 return ProcessReplay(defer); |
| 139 } |
| 140 |
| 141 bool MimeSniffingResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 142 int* buf_size, |
| 143 int min_size) { |
| 174 if (state_ == STATE_STREAMING) | 144 if (state_ == STATE_STREAMING) |
| 175 return next_handler_->OnWillRead(buf, buf_size, min_size); | 145 return next_handler_->OnWillRead(buf, buf_size, min_size); |
| 176 | 146 |
| 177 DCHECK_EQ(-1, min_size); | 147 DCHECK_EQ(-1, min_size); |
| 178 | 148 |
| 179 if (read_buffer_.get()) { | 149 if (read_buffer_.get()) { |
| 180 CHECK_LT(bytes_read_, read_buffer_size_); | 150 CHECK_LT(bytes_read_, read_buffer_size_); |
| 181 *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_); | 151 *buf = new DependentIOBuffer(read_buffer_.get(), bytes_read_); |
| 182 *buf_size = read_buffer_size_ - bytes_read_; | 152 *buf_size = read_buffer_size_ - bytes_read_; |
| 183 } else { | 153 } else { |
| 184 if (!next_handler_->OnWillRead(buf, buf_size, min_size)) | 154 if (!next_handler_->OnWillRead(buf, buf_size, min_size)) |
| 185 return false; | 155 return false; |
| 186 | 156 |
| 187 read_buffer_ = *buf; | 157 read_buffer_ = *buf; |
| 188 read_buffer_size_ = *buf_size; | 158 read_buffer_size_ = *buf_size; |
| 189 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); | 159 DCHECK_GE(read_buffer_size_, net::kMaxBytesToSniff * 2); |
| 190 } | 160 } |
| 191 return true; | 161 return true; |
| 192 } | 162 } |
| 193 | 163 |
| 194 bool MimeTypeResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 164 bool MimeSniffingResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
| 195 if (state_ == STATE_STREAMING) | 165 if (state_ == STATE_STREAMING) |
| 196 return next_handler_->OnReadCompleted(bytes_read, defer); | 166 return next_handler_->OnReadCompleted(bytes_read, defer); |
| 197 | 167 |
| 198 DCHECK_EQ(state_, STATE_BUFFERING); | 168 DCHECK_EQ(state_, STATE_BUFFERING); |
| 199 bytes_read_ += bytes_read; | 169 bytes_read_ += bytes_read; |
| 200 | 170 |
| 201 if (!DetermineMimeType() && (bytes_read > 0)) | 171 const std::string& type_hint = response_->head.mime_type; |
| 202 return true; // Needs more data, so keep buffering. | |
| 203 | 172 |
| 204 state_ = STATE_PROCESSING; | 173 std::string new_type; |
| 205 return ProcessResponse(defer); | 174 bool made_final_decision = |
| 175 net::SniffMimeType(read_buffer_->data(), bytes_read_, request()->url(), |
| 176 type_hint, &new_type); |
| 177 |
| 178 // SniffMimeType() returns false if there is not enough data to determine |
| 179 // the mime type. However, even if it returns false, it returns a new type |
| 180 // that is probably better than the current one. |
| 181 response_->head.mime_type.assign(new_type); |
| 182 |
| 183 if (!made_final_decision && (bytes_read > 0)) |
| 184 return true; |
| 185 |
| 186 return ProcessReplay(defer); |
| 206 } | 187 } |
| 207 | 188 |
| 208 void MimeTypeResourceHandler::OnResponseCompleted( | 189 void MimeSniffingResourceHandler::OnResponseCompleted( |
| 209 const net::URLRequestStatus& status, | 190 const net::URLRequestStatus& status, |
| 210 const std::string& security_info, | 191 const std::string& security_info, |
| 211 bool* defer) { | 192 bool* defer) { |
| 212 // Upon completion, act like a pass-through handler in case the downstream | 193 // Upon completion, act like a pass-through handler in case the downstream |
| 213 // handler defers OnResponseCompleted. | 194 // handler defers OnResponseCompleted. |
| 214 state_ = STATE_STREAMING; | 195 state_ = STATE_STREAMING; |
| 215 | 196 |
| 216 next_handler_->OnResponseCompleted(status, security_info, defer); | 197 next_handler_->OnResponseCompleted(status, security_info, defer); |
| 217 } | 198 } |
| 218 | 199 |
| 219 void MimeTypeResourceHandler::Resume() { | 200 void MimeSniffingResourceHandler::Resume() { |
| 220 switch (state_) { | 201 // If no information is currently being transmitted to downstream handlers, |
| 221 case STATE_BUFFERING: | 202 // they should not attempt to resume the request. |
| 222 case STATE_PROCESSING: | 203 if (state_ == STATE_BUFFERING) { |
| 223 NOTREACHED(); | 204 NOTREACHED(); |
| 224 break; | 205 return; |
| 225 case STATE_REPLAYING: | 206 } |
| 226 base::ThreadTaskRunnerHandle::Get()->PostTask( | 207 |
| 227 FROM_HERE, | 208 // If the BufferingHandler is acting as a pass-through handler, just ask the |
| 228 base::Bind(&MimeTypeResourceHandler::CallReplayReadCompleted, | 209 // upwards ResourceController to resume the request. |
| 229 weak_ptr_factory_.GetWeakPtr())); | 210 if (state_ == STATE_STARTING || state_ == STATE_STREAMING) { |
| 230 break; | 211 controller()->Resume(); |
| 231 case STATE_STARTING: | 212 return; |
| 232 case STATE_STREAMING: | 213 } |
| 233 controller()->Resume(); | 214 |
| 234 break; | 215 // Otherwise proceed with the replay of the response. If it is successful, |
| 216 // resume the request. |
| 217 bool defer = false; |
| 218 if (!ProcessReplay(&defer)) { |
| 219 Cancel(); |
| 220 } else if (!defer) { |
| 221 DCHECK_EQ(STATE_STREAMING, state_); |
| 222 controller()->Resume(); |
| 235 } | 223 } |
| 236 } | 224 } |
| 237 | 225 |
| 238 void MimeTypeResourceHandler::Cancel() { | 226 void MimeSniffingResourceHandler::Cancel() { |
| 239 controller()->Cancel(); | 227 controller()->Cancel(); |
| 240 } | 228 } |
| 241 | 229 |
| 242 void MimeTypeResourceHandler::CancelAndIgnore() { | 230 void MimeSniffingResourceHandler::CancelAndIgnore() { |
| 243 controller()->CancelAndIgnore(); | 231 controller()->CancelAndIgnore(); |
| 244 } | 232 } |
| 245 | 233 |
| 246 void MimeTypeResourceHandler::CancelWithError(int error_code) { | 234 void MimeSniffingResourceHandler::CancelWithError(int error_code) { |
| 247 controller()->CancelWithError(error_code); | 235 controller()->CancelWithError(error_code); |
| 248 } | 236 } |
| 249 | 237 |
| 250 bool MimeTypeResourceHandler::ProcessResponse(bool* defer) { | 238 bool MimeSniffingResourceHandler::ProcessReplay(bool* defer) { |
| 251 DCHECK_EQ(STATE_PROCESSING, state_); | 239 bool return_value = true; |
| 252 | 240 while (!*defer && return_value && state_ != STATE_STREAMING) { |
| 253 // TODO(darin): Stop special-casing 304 responses. | 241 switch (state_) { |
| 254 if (!(response_->head.headers.get() && | 242 case STATE_BUFFERING: |
| 255 response_->head.headers->response_code() == 304)) { | 243 return_value = ReplayResponseReceived(defer); |
| 256 if (!SelectNextHandler(defer)) | 244 break; |
| 257 return false; | 245 case STATE_REPLAYING_RESPONSE_RECEIVED: |
| 258 if (*defer) | 246 return_value = ReplayReadCompleted(defer); |
| 259 return true; | 247 break; |
| 248 default: |
| 249 NOTREACHED(); |
| 250 break; |
| 251 } |
| 260 } | 252 } |
| 261 | 253 return return_value; |
| 262 state_ = STATE_REPLAYING; | |
| 263 | |
| 264 if (!next_handler_->OnResponseStarted(response_.get(), defer)) | |
| 265 return false; | |
| 266 | |
| 267 if (!read_buffer_.get()) { | |
| 268 state_ = STATE_STREAMING; | |
| 269 return true; | |
| 270 } | |
| 271 | |
| 272 if (!*defer) | |
| 273 return ReplayReadCompleted(defer); | |
| 274 | |
| 275 return true; | |
| 276 } | 254 } |
| 277 | 255 |
| 278 bool MimeTypeResourceHandler::ShouldSniffContent() { | 256 bool MimeSniffingResourceHandler::ReplayResponseReceived(bool* defer) { |
| 257 DCHECK_EQ(STATE_BUFFERING, state_); |
| 258 state_ = STATE_REPLAYING_RESPONSE_RECEIVED; |
| 259 return next_handler_->OnResponseStarted(response_.get(), defer); |
| 260 } |
| 261 |
| 262 bool MimeSniffingResourceHandler::ReplayReadCompleted(bool* defer) { |
| 263 DCHECK_EQ(STATE_REPLAYING_RESPONSE_RECEIVED, state_); |
| 264 |
| 265 state_ = STATE_STREAMING; |
| 266 |
| 267 if (!read_buffer_.get()) |
| 268 return true; |
| 269 |
| 270 bool result = next_handler_->OnReadCompleted(bytes_read_, defer); |
| 271 |
| 272 read_buffer_ = NULL; |
| 273 read_buffer_size_ = 0; |
| 274 bytes_read_ = 0; |
| 275 |
| 276 return result; |
| 277 } |
| 278 |
| 279 bool MimeSniffingResourceHandler::ShouldSniffContent() { |
| 279 const std::string& mime_type = response_->head.mime_type; | 280 const std::string& mime_type = response_->head.mime_type; |
| 280 | 281 |
| 281 std::string content_type_options; | 282 std::string content_type_options; |
| 282 request()->GetResponseHeaderByName("x-content-type-options", | 283 request()->GetResponseHeaderByName("x-content-type-options", |
| 283 &content_type_options); | 284 &content_type_options); |
| 284 | 285 |
| 285 bool sniffing_blocked = | 286 bool sniffing_blocked = |
| 286 base::LowerCaseEqualsASCII(content_type_options, "nosniff"); | 287 base::LowerCaseEqualsASCII(content_type_options, "nosniff"); |
| 287 bool we_would_like_to_sniff = | 288 bool we_would_like_to_sniff = |
| 288 net::ShouldSniffMimeType(request()->url(), mime_type); | 289 net::ShouldSniffMimeType(request()->url(), mime_type); |
| 289 | 290 |
| 290 if (!sniffing_blocked && we_would_like_to_sniff) { | 291 if (!sniffing_blocked && we_would_like_to_sniff) { |
| 291 // We're going to look at the data before deciding what the content type | 292 // 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 | 293 // is. That means we need to delay sending the ResponseStarted message |
| 293 // over the IPC channel. | 294 // over the IPC channel. |
| 294 VLOG(1) << "To buffer: " << request()->url().spec(); | 295 VLOG(1) << "To buffer: " << request()->url().spec(); |
| 295 return true; | 296 return true; |
| 296 } | 297 } |
| 297 | 298 |
| 298 return false; | 299 return false; |
| 299 } | 300 } |
| 300 | 301 |
| 301 bool MimeTypeResourceHandler::DetermineMimeType() { | |
| 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()); | |
| 364 | |
| 365 ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 366 const std::string& mime_type = response_->head.mime_type; | |
| 367 | |
| 368 // https://crbug.com/568184 - Temporary hack to track servers that aren't | |
| 369 // setting Content-Disposition when sending x-x509-user-cert and expecting | |
| 370 // the browser to automatically install certificates; this is being | |
| 371 // deprecated and will be removed upon full <keygen> removal. | |
| 372 if (mime_type == "application/x-x509-user-cert") { | |
| 373 UMA_HISTOGRAM_BOOLEAN( | |
| 374 "UserCert.ContentDisposition", | |
| 375 response_->head.headers->HasHeader("Content-Disposition")); | |
| 376 } | |
| 377 | |
| 378 // Allow requests for object/embed tags to be intercepted as streams. | |
| 379 if (info->GetResourceType() == content::RESOURCE_TYPE_OBJECT) { | |
| 380 DCHECK(!info->allow_download()); | |
| 381 | |
| 382 bool handled_by_plugin; | |
| 383 if (!SelectPluginHandler(defer, &handled_by_plugin)) | |
| 384 return false; | |
| 385 if (handled_by_plugin || *defer) | |
| 386 return true; | |
| 387 } | |
| 388 | |
| 389 if (!info->allow_download()) | |
| 390 return true; | |
| 391 | |
| 392 // info->allow_download() == true implies | |
| 393 // info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME or | |
| 394 // info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME. | |
| 395 DCHECK(info->GetResourceType() == RESOURCE_TYPE_MAIN_FRAME || | |
| 396 info->GetResourceType() == RESOURCE_TYPE_SUB_FRAME); | |
| 397 | |
| 398 bool must_download = MustDownload(); | |
| 399 if (!must_download) { | |
| 400 if (mime_util::IsSupportedMimeType(mime_type)) | |
| 401 return true; | |
| 402 | |
| 403 bool handled_by_plugin; | |
| 404 if (!SelectPluginHandler(defer, &handled_by_plugin)) | |
| 405 return false; | |
| 406 if (handled_by_plugin || *defer) | |
| 407 return true; | |
| 408 } | |
| 409 | |
| 410 // Install download handler | |
| 411 info->set_is_download(true); | |
| 412 std::unique_ptr<ResourceHandler> handler( | |
| 413 host_->CreateResourceHandlerForDownload(request(), | |
| 414 true, // is_content_initiated | |
| 415 must_download)); | |
| 416 return UseAlternateNextHandler(std::move(handler), std::string()); | |
| 417 } | |
| 418 | |
| 419 bool MimeTypeResourceHandler::UseAlternateNextHandler( | |
| 420 std::unique_ptr<ResourceHandler> new_handler, | |
| 421 const std::string& payload_for_old_handler) { | |
| 422 if (response_->head.headers.get() && // Can be NULL if FTP. | |
| 423 response_->head.headers->response_code() / 100 != 2) { | |
| 424 // The response code indicates that this is an error page, but we don't | |
| 425 // know how to display the content. We follow Firefox here and show our | |
| 426 // own error page instead of triggering a download. | |
| 427 // TODO(abarth): We should abstract the response_code test, but this kind | |
| 428 // of check is scattered throughout our codebase. | |
| 429 request()->CancelWithError(net::ERR_INVALID_RESPONSE); | |
| 430 return false; | |
| 431 } | |
| 432 | |
| 433 // Inform the original ResourceHandler that this will be handled entirely by | |
| 434 // the new ResourceHandler. | |
| 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 | |
| 451 next_handler_->OnWillRead(&buf, &size, -1); | |
| 452 CHECK_GE(size, static_cast<int>(payload_for_old_handler.length())); | |
| 453 | |
| 454 memcpy(buf->data(), payload_for_old_handler.c_str(), | |
| 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 } | |
| 465 | |
| 466 // This is handled entirely within the new ResourceHandler, so just reset the | |
| 467 // original ResourceHandler. | |
| 468 next_handler_ = std::move(new_handler); | |
| 469 next_handler_->SetController(this); | |
| 470 | |
| 471 return CopyReadBufferToNextHandler(); | |
| 472 } | |
| 473 | |
| 474 bool MimeTypeResourceHandler::ReplayReadCompleted(bool* defer) { | |
| 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_) | |
| 500 return must_download_; | |
| 501 | |
| 502 must_download_is_set_ = true; | |
| 503 | |
| 504 std::string disposition; | |
| 505 request()->GetResponseHeaderByName("content-disposition", &disposition); | |
| 506 if (!disposition.empty() && | |
| 507 net::HttpContentDisposition(disposition, std::string()).is_attachment()) { | |
| 508 must_download_ = true; | |
| 509 } else if (host_->delegate() && | |
| 510 host_->delegate()->ShouldForceDownloadResource( | |
| 511 request()->url(), response_->head.mime_type)) { | |
| 512 must_download_ = true; | |
| 513 } else { | |
| 514 must_download_ = false; | |
| 515 } | |
| 516 | |
| 517 return must_download_; | |
| 518 } | |
| 519 | |
| 520 bool MimeTypeResourceHandler::CopyReadBufferToNextHandler() { | |
| 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) { | |
| 536 request()->LogUnblocked(); | |
| 537 bool defer = false; | |
| 538 if (!ProcessResponse(&defer)) { | |
| 539 controller()->Cancel(); | |
| 540 } else if (!defer) { | |
| 541 controller()->Resume(); | |
| 542 } | |
| 543 } | |
| 544 | |
| 545 } // namespace content | 302 } // namespace content |
| OLD | NEW |