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