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_sniffing_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" | 10 #include "base/bind.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 scoped_refptr<net::IOBuffer> buf_; | 61 scoped_refptr<net::IOBuffer> buf_; |
62 }; | 62 }; |
63 | 63 |
64 } // namespace | 64 } // namespace |
65 | 65 |
66 MimeSniffingResourceHandler::MimeSniffingResourceHandler( | 66 MimeSniffingResourceHandler::MimeSniffingResourceHandler( |
67 std::unique_ptr<ResourceHandler> next_handler, | 67 std::unique_ptr<ResourceHandler> next_handler, |
68 ResourceDispatcherHostImpl* host, | 68 ResourceDispatcherHostImpl* host, |
69 PluginService* plugin_service, | 69 PluginService* plugin_service, |
70 InterceptingResourceHandler* intercepting_handler, | 70 InterceptingResourceHandler* intercepting_handler, |
71 net::URLRequest* request) | 71 net::URLRequest* request, |
| 72 RequestContextType request_context_type) |
72 : LayeredResourceHandler(request, std::move(next_handler)), | 73 : LayeredResourceHandler(request, std::move(next_handler)), |
73 state_(STATE_STARTING), | 74 state_(STATE_STARTING), |
74 host_(host), | 75 host_(host), |
75 #if defined(ENABLE_PLUGINS) | 76 #if defined(ENABLE_PLUGINS) |
76 plugin_service_(plugin_service), | 77 plugin_service_(plugin_service), |
77 #endif | 78 #endif |
78 must_download_(false), | 79 must_download_(false), |
79 must_download_is_set_(false), | 80 must_download_is_set_(false), |
80 read_buffer_size_(0), | 81 read_buffer_size_(0), |
81 bytes_read_(0), | 82 bytes_read_(0), |
82 intercepting_handler_(intercepting_handler), | 83 intercepting_handler_(intercepting_handler), |
| 84 request_context_type_(request_context_type), |
83 weak_ptr_factory_(this) { | 85 weak_ptr_factory_(this) { |
84 } | 86 } |
85 | 87 |
86 MimeSniffingResourceHandler::~MimeSniffingResourceHandler() {} | 88 MimeSniffingResourceHandler::~MimeSniffingResourceHandler() {} |
87 | 89 |
88 void MimeSniffingResourceHandler::SetController( | 90 void MimeSniffingResourceHandler::SetController( |
89 ResourceController* controller) { | 91 ResourceController* controller) { |
90 ResourceHandler::SetController(controller); | 92 ResourceHandler::SetController(controller); |
91 | 93 |
92 // Downstream handlers see the MimeSniffingResourceHandler as their | 94 // Downstream handlers see the MimeSniffingResourceHandler as their |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 request()->SetExtraRequestHeaderByName(kAcceptHeader, accept_value, false); | 137 request()->SetExtraRequestHeaderByName(kAcceptHeader, accept_value, false); |
136 return next_handler_->OnWillStart(url, defer); | 138 return next_handler_->OnWillStart(url, defer); |
137 } | 139 } |
138 | 140 |
139 bool MimeSniffingResourceHandler::OnResponseStarted(ResourceResponse* response, | 141 bool MimeSniffingResourceHandler::OnResponseStarted(ResourceResponse* response, |
140 bool* defer) { | 142 bool* defer) { |
141 DCHECK_EQ(STATE_STARTING, state_); | 143 DCHECK_EQ(STATE_STARTING, state_); |
142 response_ = response; | 144 response_ = response; |
143 | 145 |
144 state_ = STATE_BUFFERING; | 146 state_ = STATE_BUFFERING; |
145 | |
146 // A 304 response should not contain a Content-Type header (RFC 7232 section | 147 // A 304 response should not contain a Content-Type header (RFC 7232 section |
147 // 4.1). The following code may incorrectly attempt to add a Content-Type to | 148 // 4.1). The following code may incorrectly attempt to add a Content-Type to |
148 // the response, and so must be skipped for 304 responses. | 149 // the response, and so must be skipped for 304 responses. |
149 if (!(response_->head.headers.get() && | 150 if (!(response_->head.headers.get() && |
150 response_->head.headers->response_code() == 304)) { | 151 response_->head.headers->response_code() == 304)) { |
151 if (ShouldSniffContent()) | 152 if (ShouldSniffContent()) |
152 return true; | 153 return true; |
153 | 154 |
154 if (response_->head.mime_type.empty()) { | 155 if (response_->head.mime_type.empty()) { |
155 // Ugg. The server told us not to sniff the content but didn't give us a | 156 // Ugg. The server told us not to sniff the content but didn't give us a |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 bool result = next_handler_->OnReadCompleted(bytes_read_, defer); | 320 bool result = next_handler_->OnReadCompleted(bytes_read_, defer); |
320 | 321 |
321 read_buffer_ = nullptr; | 322 read_buffer_ = nullptr; |
322 read_buffer_size_ = 0; | 323 read_buffer_size_ = 0; |
323 bytes_read_ = 0; | 324 bytes_read_ = 0; |
324 | 325 |
325 return result; | 326 return result; |
326 } | 327 } |
327 | 328 |
328 bool MimeSniffingResourceHandler::ShouldSniffContent() { | 329 bool MimeSniffingResourceHandler::ShouldSniffContent() { |
| 330 if (request_context_type_ == REQUEST_CONTEXT_TYPE_FETCH) { |
| 331 // MIME sniffing should be disabled for a request initiated by fetch(). |
| 332 return false; |
| 333 } |
| 334 |
329 const std::string& mime_type = response_->head.mime_type; | 335 const std::string& mime_type = response_->head.mime_type; |
330 | 336 |
331 std::string content_type_options; | 337 std::string content_type_options; |
332 request()->GetResponseHeaderByName("x-content-type-options", | 338 request()->GetResponseHeaderByName("x-content-type-options", |
333 &content_type_options); | 339 &content_type_options); |
334 | 340 |
335 bool sniffing_blocked = | 341 bool sniffing_blocked = |
336 base::LowerCaseEqualsASCII(content_type_options, "nosniff"); | 342 base::LowerCaseEqualsASCII(content_type_options, "nosniff"); |
337 bool we_would_like_to_sniff = | 343 bool we_would_like_to_sniff = |
338 net::ShouldSniffMimeType(request()->url(), mime_type); | 344 net::ShouldSniffMimeType(request()->url(), mime_type); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 | 514 |
509 void MimeSniffingResourceHandler::OnPluginsLoaded( | 515 void MimeSniffingResourceHandler::OnPluginsLoaded( |
510 const std::vector<WebPluginInfo>& plugins) { | 516 const std::vector<WebPluginInfo>& plugins) { |
511 // No longer blocking on the plugins being loaded. | 517 // No longer blocking on the plugins being loaded. |
512 request()->LogUnblocked(); | 518 request()->LogUnblocked(); |
513 if (state_ == STATE_BUFFERING) | 519 if (state_ == STATE_BUFFERING) |
514 AdvanceState(); | 520 AdvanceState(); |
515 } | 521 } |
516 | 522 |
517 } // namespace content | 523 } // namespace content |
OLD | NEW |