1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/child/web_url_loader_impl.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_util.h"
17 #include "base/time/time.h"
18 #include "components/mime_util/mime_util.h"
19 #include "content/child/child_thread_impl.h"
20 #include "content/child/ftp_directory_listing_response_delegate.h"
21 #include "content/child/multipart_response_delegate.h"
22 #include "content/child/request_extra_data.h"
23 #include "content/child/request_info.h"
24 #include "content/child/resource_dispatcher.h"
25 #include "content/child/sync_load_response.h"
26 #include "content/child/web_data_consumer_handle_impl.h"
27 #include "content/child/web_url_request_util.h"
28 #include "content/child/weburlresponse_extradata_impl.h"
29 #include "content/common/resource_messages.h"
30 #include "content/common/resource_request_body.h"
31 #include "content/common/service_worker/service_worker_types.h"
32 #include "content/public/child/fixed_received_data.h"
33 #include "content/public/child/request_peer.h"
34 #include "content/public/common/content_switches.h"
35 #include "net/base/data_url.h"
36 #include "net/base/filename_util.h"
37 #include "net/base/net_errors.h"
38 #include "net/http/http_response_headers.h"
39 #include "net/http/http_util.h"
40 #include "net/url_request/redirect_info.h"
41 #include "net/url_request/url_request_data_job.h"
42 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h"
43 #include "third_party/WebKit/public/platform/WebURL.h"
44 #include "third_party/WebKit/public/platform/WebURLError.h"
45 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
46 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
47 #include "third_party/WebKit/public/platform/WebURLRequest.h"
48 #include "third_party/WebKit/public/platform/WebURLResponse.h"
49 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
50 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
53 using base::TimeTicks
;
55 using blink::WebHTTPBody
;
56 using blink::WebHTTPHeaderVisitor
;
57 using blink::WebHTTPLoadInfo
;
58 using blink::WebReferrerPolicy
;
59 using blink::WebSecurityPolicy
;
60 using blink::WebString
;
62 using blink::WebURLError
;
63 using blink::WebURLLoadTiming
;
64 using blink::WebURLLoader
;
65 using blink::WebURLLoaderClient
;
66 using blink::WebURLRequest
;
67 using blink::WebURLResponse
;
71 // Utilities ------------------------------------------------------------------
75 const size_t kBodyStreamPipeCapacity
= 4 * 1024;
77 using HeadersVector
= ResourceDevToolsInfo::HeadersVector
;
79 // Converts timing data from |load_timing| to the format used by WebKit.
80 void PopulateURLLoadTiming(const net::LoadTimingInfo
& load_timing
,
81 WebURLLoadTiming
* url_timing
) {
82 DCHECK(!load_timing
.request_start
.is_null());
84 const TimeTicks kNullTicks
;
85 url_timing
->initialize();
86 url_timing
->setRequestTime(
87 (load_timing
.request_start
- kNullTicks
).InSecondsF());
88 url_timing
->setProxyStart(
89 (load_timing
.proxy_resolve_start
- kNullTicks
).InSecondsF());
90 url_timing
->setProxyEnd(
91 (load_timing
.proxy_resolve_end
- kNullTicks
).InSecondsF());
92 url_timing
->setDNSStart(
93 (load_timing
.connect_timing
.dns_start
- kNullTicks
).InSecondsF());
94 url_timing
->setDNSEnd(
95 (load_timing
.connect_timing
.dns_end
- kNullTicks
).InSecondsF());
96 url_timing
->setConnectStart(
97 (load_timing
.connect_timing
.connect_start
- kNullTicks
).InSecondsF());
98 url_timing
->setConnectEnd(
99 (load_timing
.connect_timing
.connect_end
- kNullTicks
).InSecondsF());
100 url_timing
->setSSLStart(
101 (load_timing
.connect_timing
.ssl_start
- kNullTicks
).InSecondsF());
102 url_timing
->setSSLEnd(
103 (load_timing
.connect_timing
.ssl_end
- kNullTicks
).InSecondsF());
104 url_timing
->setSendStart(
105 (load_timing
.send_start
- kNullTicks
).InSecondsF());
106 url_timing
->setSendEnd(
107 (load_timing
.send_end
- kNullTicks
).InSecondsF());
108 url_timing
->setReceiveHeadersEnd(
109 (load_timing
.receive_headers_end
- kNullTicks
).InSecondsF());
112 net::RequestPriority
ConvertWebKitPriorityToNetPriority(
113 const WebURLRequest::Priority
& priority
) {
115 case WebURLRequest::PriorityVeryHigh
:
118 case WebURLRequest::PriorityHigh
:
121 case WebURLRequest::PriorityMedium
:
124 case WebURLRequest::PriorityLow
:
127 case WebURLRequest::PriorityVeryLow
:
130 case WebURLRequest::PriorityUnresolved
:
137 // Extracts info from a data scheme URL into |info| and |data|. Returns net::OK
138 // if successful. Returns a net error code otherwise. Exported only for testing.
139 int GetInfoFromDataURL(const GURL
& url
,
140 ResourceResponseInfo
* info
,
142 // Assure same time for all time fields of data: URLs.
143 Time now
= Time::Now();
144 info
->load_timing
.request_start
= TimeTicks::Now();
145 info
->load_timing
.request_start_time
= now
;
146 info
->request_time
= now
;
147 info
->response_time
= now
;
149 std::string mime_type
;
151 scoped_refptr
<net::HttpResponseHeaders
> headers(
152 new net::HttpResponseHeaders(std::string()));
153 int result
= net::URLRequestDataJob::BuildResponse(
154 url
, &mime_type
, &charset
, data
, headers
.get());
155 if (result
!= net::OK
)
158 info
->headers
= headers
;
159 info
->mime_type
.swap(mime_type
);
160 info
->charset
.swap(charset
);
161 info
->security_info
.clear();
162 info
->content_length
= data
->length();
163 info
->encoded_data_length
= 0;
168 #define STATIC_ASSERT_MATCHING_ENUMS(content_name, blink_name) \
170 static_cast<int>(content_name) == static_cast<int>(blink_name), \
171 "mismatching enums: " #content_name)
173 STATIC_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_SAME_ORIGIN
,
174 WebURLRequest::FetchRequestModeSameOrigin
);
175 STATIC_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_NO_CORS
,
176 WebURLRequest::FetchRequestModeNoCORS
);
177 STATIC_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_CORS
,
178 WebURLRequest::FetchRequestModeCORS
);
179 STATIC_ASSERT_MATCHING_ENUMS(
180 FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT
,
181 WebURLRequest::FetchRequestModeCORSWithForcedPreflight
);
183 FetchRequestMode
GetFetchRequestMode(const WebURLRequest
& request
) {
184 return static_cast<FetchRequestMode
>(request
.fetchRequestMode());
187 STATIC_ASSERT_MATCHING_ENUMS(FETCH_CREDENTIALS_MODE_OMIT
,
188 WebURLRequest::FetchCredentialsModeOmit
);
189 STATIC_ASSERT_MATCHING_ENUMS(FETCH_CREDENTIALS_MODE_SAME_ORIGIN
,
190 WebURLRequest::FetchCredentialsModeSameOrigin
);
191 STATIC_ASSERT_MATCHING_ENUMS(FETCH_CREDENTIALS_MODE_INCLUDE
,
192 WebURLRequest::FetchCredentialsModeInclude
);
194 FetchCredentialsMode
GetFetchCredentialsMode(const WebURLRequest
& request
) {
195 return static_cast<FetchCredentialsMode
>(request
.fetchCredentialsMode());
198 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_FRAME_TYPE_AUXILIARY
,
199 WebURLRequest::FrameTypeAuxiliary
);
200 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_FRAME_TYPE_NESTED
,
201 WebURLRequest::FrameTypeNested
);
202 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_FRAME_TYPE_NONE
,
203 WebURLRequest::FrameTypeNone
);
204 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL
,
205 WebURLRequest::FrameTypeTopLevel
);
207 RequestContextFrameType
GetRequestContextFrameType(
208 const WebURLRequest
& request
) {
209 return static_cast<RequestContextFrameType
>(request
.frameType());
212 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_UNSPECIFIED
,
213 WebURLRequest::RequestContextUnspecified
);
214 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_AUDIO
,
215 WebURLRequest::RequestContextAudio
);
216 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_BEACON
,
217 WebURLRequest::RequestContextBeacon
);
218 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_CSP_REPORT
,
219 WebURLRequest::RequestContextCSPReport
);
220 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_DOWNLOAD
,
221 WebURLRequest::RequestContextDownload
);
222 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_EMBED
,
223 WebURLRequest::RequestContextEmbed
);
224 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_EVENT_SOURCE
,
225 WebURLRequest::RequestContextEventSource
);
226 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FAVICON
,
227 WebURLRequest::RequestContextFavicon
);
228 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FETCH
,
229 WebURLRequest::RequestContextFetch
);
230 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FONT
,
231 WebURLRequest::RequestContextFont
);
232 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FORM
,
233 WebURLRequest::RequestContextForm
);
234 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FRAME
,
235 WebURLRequest::RequestContextFrame
);
236 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_HYPERLINK
,
237 WebURLRequest::RequestContextHyperlink
);
238 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_IFRAME
,
239 WebURLRequest::RequestContextIframe
);
240 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_IMAGE
,
241 WebURLRequest::RequestContextImage
);
242 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_IMAGE_SET
,
243 WebURLRequest::RequestContextImageSet
);
244 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_IMPORT
,
245 WebURLRequest::RequestContextImport
);
246 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_INTERNAL
,
247 WebURLRequest::RequestContextInternal
);
248 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_LOCATION
,
249 WebURLRequest::RequestContextLocation
);
250 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_MANIFEST
,
251 WebURLRequest::RequestContextManifest
);
252 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_OBJECT
,
253 WebURLRequest::RequestContextObject
);
254 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_PING
,
255 WebURLRequest::RequestContextPing
);
256 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_PLUGIN
,
257 WebURLRequest::RequestContextPlugin
);
258 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_PREFETCH
,
259 WebURLRequest::RequestContextPrefetch
);
260 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_SCRIPT
,
261 WebURLRequest::RequestContextScript
);
262 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_SERVICE_WORKER
,
263 WebURLRequest::RequestContextServiceWorker
);
264 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_SHARED_WORKER
,
265 WebURLRequest::RequestContextSharedWorker
);
266 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_SUBRESOURCE
,
267 WebURLRequest::RequestContextSubresource
);
268 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_STYLE
,
269 WebURLRequest::RequestContextStyle
);
270 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_TRACK
,
271 WebURLRequest::RequestContextTrack
);
272 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_VIDEO
,
273 WebURLRequest::RequestContextVideo
);
274 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_WORKER
,
275 WebURLRequest::RequestContextWorker
);
276 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_XML_HTTP_REQUEST
,
277 WebURLRequest::RequestContextXMLHttpRequest
);
278 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_XSLT
,
279 WebURLRequest::RequestContextXSLT
);
281 RequestContextType
GetRequestContextType(const WebURLRequest
& request
) {
282 return static_cast<RequestContextType
>(request
.requestContext());
287 // WebURLLoaderImpl::Context --------------------------------------------------
289 // This inner class exists since the WebURLLoader may be deleted while inside a
290 // call to WebURLLoaderClient. Refcounting is to keep the context from being
291 // deleted if it may have work to do after calling into the client.
292 class WebURLLoaderImpl::Context
: public base::RefCounted
<Context
>,
295 Context(WebURLLoaderImpl
* loader
,
296 ResourceDispatcher
* resource_dispatcher
,
297 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
299 WebURLLoaderClient
* client() const { return client_
; }
300 void set_client(WebURLLoaderClient
* client
) { client_
= client
; }
303 void SetDefersLoading(bool value
);
304 void DidChangePriority(WebURLRequest::Priority new_priority
,
305 int intra_priority_value
);
306 bool AttachThreadedDataReceiver(
307 blink::WebThreadedDataReceiver
* threaded_data_receiver
);
308 void Start(const WebURLRequest
& request
,
309 SyncLoadResponse
* sync_load_response
);
311 // RequestPeer methods:
312 void OnUploadProgress(uint64 position
, uint64 size
) override
;
313 bool OnReceivedRedirect(const net::RedirectInfo
& redirect_info
,
314 const ResourceResponseInfo
& info
) override
;
315 void OnReceivedResponse(const ResourceResponseInfo
& info
) override
;
316 void OnDownloadedData(int len
, int encoded_data_length
) override
;
317 void OnReceivedData(scoped_ptr
<ReceivedData
> data
) override
;
318 void OnReceivedCachedMetadata(const char* data
, int len
) override
;
319 void OnCompletedRequest(int error_code
,
320 bool was_ignored_by_handler
,
321 bool stale_copy_in_cache
,
322 const std::string
& security_info
,
323 const base::TimeTicks
& completion_time
,
324 int64 total_transfer_size
) override
;
327 friend class base::RefCounted
<Context
>;
330 // We can optimize the handling of data URLs in most cases.
331 bool CanHandleDataURLRequestLocally() const;
332 void HandleDataURL();
333 MojoResult
WriteDataOnBodyStream(const char* data
, size_t size
);
334 void OnHandleGotWritable(MojoResult
);
336 WebURLLoaderImpl
* loader_
;
337 WebURLRequest request_
;
338 WebURLLoaderClient
* client_
;
339 ResourceDispatcher
* resource_dispatcher_
;
340 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
341 WebReferrerPolicy referrer_policy_
;
342 scoped_ptr
<FtpDirectoryListingResponseDelegate
> ftp_listing_delegate_
;
343 scoped_ptr
<MultipartResponseDelegate
> multipart_delegate_
;
344 scoped_ptr
<StreamOverrideParameters
> stream_override_
;
345 mojo::ScopedDataPipeProducerHandle body_stream_writer_
;
346 mojo::common::HandleWatcher body_stream_writer_watcher_
;
347 // TODO(yhirano): Delete this buffer after implementing the back-pressure
349 std::deque
<char> body_stream_buffer_
;
350 bool got_all_stream_body_data_
;
351 enum DeferState
{NOT_DEFERRING
, SHOULD_DEFER
, DEFERRED_DATA
};
352 DeferState defers_loading_
;
356 WebURLLoaderImpl::Context::Context(
357 WebURLLoaderImpl
* loader
,
358 ResourceDispatcher
* resource_dispatcher
,
359 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
362 resource_dispatcher_(resource_dispatcher
),
363 task_runner_(task_runner
),
364 referrer_policy_(blink::WebReferrerPolicyDefault
),
365 got_all_stream_body_data_(false),
366 defers_loading_(NOT_DEFERRING
),
370 void WebURLLoaderImpl::Context::Cancel() {
371 if (resource_dispatcher_
&& // NULL in unittest.
373 resource_dispatcher_
->Cancel(request_id_
);
377 // Ensure that we do not notify the multipart delegate anymore as it has
378 // its own pointer to the client.
379 if (multipart_delegate_
)
380 multipart_delegate_
->Cancel();
381 // Ditto for the ftp delegate.
382 if (ftp_listing_delegate_
)
383 ftp_listing_delegate_
->Cancel();
385 // Do not make any further calls to the client.
390 void WebURLLoaderImpl::Context::SetDefersLoading(bool value
) {
391 if (request_id_
!= -1)
392 resource_dispatcher_
->SetDefersLoading(request_id_
, value
);
393 if (value
&& defers_loading_
== NOT_DEFERRING
) {
394 defers_loading_
= SHOULD_DEFER
;
395 } else if (!value
&& defers_loading_
!= NOT_DEFERRING
) {
396 if (defers_loading_
== DEFERRED_DATA
) {
397 task_runner_
->PostTask(FROM_HERE
,
398 base::Bind(&Context::HandleDataURL
, this));
400 defers_loading_
= NOT_DEFERRING
;
404 void WebURLLoaderImpl::Context::DidChangePriority(
405 WebURLRequest::Priority new_priority
, int intra_priority_value
) {
406 if (request_id_
!= -1) {
407 resource_dispatcher_
->DidChangePriority(
409 ConvertWebKitPriorityToNetPriority(new_priority
),
410 intra_priority_value
);
414 bool WebURLLoaderImpl::Context::AttachThreadedDataReceiver(
415 blink::WebThreadedDataReceiver
* threaded_data_receiver
) {
416 if (request_id_
!= -1) {
417 return resource_dispatcher_
->AttachThreadedDataReceiver(
418 request_id_
, threaded_data_receiver
);
424 void WebURLLoaderImpl::Context::Start(const WebURLRequest
& request
,
425 SyncLoadResponse
* sync_load_response
) {
426 DCHECK(request_id_
== -1);
427 request_
= request
; // Save the request.
428 if (request
.extraData()) {
429 RequestExtraData
* extra_data
=
430 static_cast<RequestExtraData
*>(request
.extraData());
431 stream_override_
= extra_data
->TakeStreamOverrideOwnership();
434 GURL url
= request
.url();
436 // PlzNavigate: during navigation, the renderer should request a stream which
437 // contains the body of the response. The request has already been made by the
439 if (stream_override_
.get()) {
440 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
441 switches::kEnableBrowserSideNavigation
));
442 DCHECK(!sync_load_response
);
443 DCHECK_NE(WebURLRequest::FrameTypeNone
, request
.frameType());
444 DCHECK_EQ("GET", request
.httpMethod().latin1());
445 url
= stream_override_
->stream_url
;
448 if (CanHandleDataURLRequestLocally()) {
449 if (sync_load_response
) {
450 // This is a sync load. Do the work now.
451 sync_load_response
->url
= url
;
452 sync_load_response
->error_code
=
453 GetInfoFromDataURL(sync_load_response
->url
, sync_load_response
,
454 &sync_load_response
->data
);
456 task_runner_
->PostTask(FROM_HERE
,
457 base::Bind(&Context::HandleDataURL
, this));
462 // PlzNavigate: outside of tests, the only navigation requests going through
463 // the WebURLLoader are the ones created by CommitNavigation. Several browser
464 // tests load HTML directly through a data url which will be handled by the
466 DCHECK_IMPLIES(base::CommandLine::ForCurrentProcess()->HasSwitch(
467 switches::kEnableBrowserSideNavigation
),
468 stream_override_
.get() ||
469 request
.frameType() == WebURLRequest::FrameTypeNone
);
472 request
.httpHeaderField(WebString::fromUTF8("Referer")).latin1());
473 const std::string
& method
= request
.httpMethod().latin1();
475 // TODO(brettw) this should take parameter encoding into account when
476 // creating the GURLs.
478 // TODO(horo): Check credentials flag is unset when credentials mode is omit.
479 // Check credentials flag is set when credentials mode is include.
481 RequestInfo request_info
;
482 request_info
.method
= method
;
483 request_info
.url
= url
;
484 request_info
.first_party_for_cookies
= request
.firstPartyForCookies();
485 referrer_policy_
= request
.referrerPolicy();
486 request_info
.referrer
= Referrer(referrer_url
, referrer_policy_
);
487 request_info
.headers
= GetWebURLRequestHeaders(request
);
488 request_info
.load_flags
= GetLoadFlagsForWebURLRequest(request
);
489 request_info
.enable_load_timing
= true;
490 request_info
.enable_upload_progress
= request
.reportUploadProgress();
491 if (request
.requestContext() == WebURLRequest::RequestContextXMLHttpRequest
&&
492 (url
.has_username() || url
.has_password())) {
493 request_info
.do_not_prompt_for_login
= true;
495 // requestor_pid only needs to be non-zero if the request originates outside
496 // the render process, so we can use requestorProcessID even for requests
497 // from in-process plugins.
498 request_info
.requestor_pid
= request
.requestorProcessID();
499 request_info
.request_type
= WebURLRequestToResourceType(request
);
500 request_info
.priority
=
501 ConvertWebKitPriorityToNetPriority(request
.priority());
502 request_info
.appcache_host_id
= request
.appCacheHostID();
503 request_info
.routing_id
= request
.requestorID();
504 request_info
.download_to_file
= request
.downloadToFile();
505 request_info
.has_user_gesture
= request
.hasUserGesture();
506 request_info
.skip_service_worker
= request
.skipServiceWorker();
507 request_info
.should_reset_appcache
= request
.shouldResetAppCache();
508 request_info
.fetch_request_mode
= GetFetchRequestMode(request
);
509 request_info
.fetch_credentials_mode
= GetFetchCredentialsMode(request
);
510 request_info
.fetch_request_context_type
= GetRequestContextType(request
);
511 request_info
.fetch_frame_type
= GetRequestContextFrameType(request
);
512 request_info
.extra_data
= request
.extraData();
514 scoped_refptr
<ResourceRequestBody
> request_body
=
515 GetRequestBodyForWebURLRequest(request
).get();
517 if (sync_load_response
) {
518 resource_dispatcher_
->StartSync(
519 request_info
, request_body
.get(), sync_load_response
);
523 request_id_
= resource_dispatcher_
->StartAsync(
524 request_info
, request_body
.get(), this);
527 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position
, uint64 size
) {
529 client_
->didSendData(loader_
, position
, size
);
532 bool WebURLLoaderImpl::Context::OnReceivedRedirect(
533 const net::RedirectInfo
& redirect_info
,
534 const ResourceResponseInfo
& info
) {
538 WebURLResponse response
;
539 response
.initialize();
540 PopulateURLResponse(request_
.url(), info
, &response
);
542 // TODO(darin): We lack sufficient information to construct the actual
543 // request that resulted from the redirect.
544 WebURLRequest
new_request(redirect_info
.new_url
);
545 new_request
.setFirstPartyForCookies(
546 redirect_info
.new_first_party_for_cookies
);
547 new_request
.setDownloadToFile(request_
.downloadToFile());
548 new_request
.setRequestContext(request_
.requestContext());
549 new_request
.setFrameType(request_
.frameType());
550 new_request
.setSkipServiceWorker(request_
.skipServiceWorker());
551 new_request
.setShouldResetAppCache(request_
.shouldResetAppCache());
552 new_request
.setFetchRequestMode(request_
.fetchRequestMode());
553 new_request
.setFetchCredentialsMode(request_
.fetchCredentialsMode());
555 new_request
.setHTTPReferrer(WebString::fromUTF8(redirect_info
.new_referrer
),
558 std::string old_method
= request_
.httpMethod().utf8();
559 new_request
.setHTTPMethod(WebString::fromUTF8(redirect_info
.new_method
));
560 if (redirect_info
.new_method
== old_method
)
561 new_request
.setHTTPBody(request_
.httpBody());
563 // Protect from deletion during call to willSendRequest.
564 scoped_refptr
<Context
> protect(this);
566 client_
->willSendRequest(loader_
, new_request
, response
);
567 request_
= new_request
;
569 // Only follow the redirect if WebKit left the URL unmodified.
570 if (redirect_info
.new_url
== GURL(new_request
.url())) {
571 // First-party cookie logic moved from DocumentLoader in Blink to
572 // net::URLRequest in the browser. Assert that Blink didn't try to change it
573 // to something else.
574 DCHECK_EQ(redirect_info
.new_first_party_for_cookies
.spec(),
575 request_
.firstPartyForCookies().string().utf8());
579 // We assume that WebKit only changes the URL to suppress a redirect, and we
580 // assume that it does so by setting it to be invalid.
581 DCHECK(!new_request
.url().isValid());
585 void WebURLLoaderImpl::Context::OnReceivedResponse(
586 const ResourceResponseInfo
& initial_info
) {
590 ResourceResponseInfo info
= initial_info
;
592 // PlzNavigate: during navigations, the ResourceResponse has already been
593 // received on the browser side, and has been passed down to the renderer.
594 if (stream_override_
.get()) {
595 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
596 switches::kEnableBrowserSideNavigation
));
597 info
= stream_override_
->response
;
600 WebURLResponse response
;
601 response
.initialize();
602 PopulateURLResponse(request_
.url(), info
, &response
);
604 bool show_raw_listing
= (GURL(request_
.url()).query() == "raw");
606 if (info
.mime_type
== "text/vnd.chromium.ftp-dir") {
607 if (show_raw_listing
) {
608 // Set the MIME type to plain text to prevent any active content.
609 response
.setMIMEType("text/plain");
611 // We're going to produce a parsed listing in HTML.
612 response
.setMIMEType("text/html");
616 // Prevent |this| from being destroyed if the client destroys the loader,
617 // ether in didReceiveResponse, or when the multipart/ftp delegate calls into
619 scoped_refptr
<Context
> protect(this);
621 if (request_
.useStreamOnResponse()) {
622 MojoCreateDataPipeOptions options
;
623 options
.struct_size
= sizeof(MojoCreateDataPipeOptions
);
624 options
.flags
= MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE
;
625 options
.element_num_bytes
= 1;
626 options
.capacity_num_bytes
= kBodyStreamPipeCapacity
;
628 mojo::ScopedDataPipeConsumerHandle consumer
;
629 MojoResult result
= mojo::CreateDataPipe(&options
,
630 &body_stream_writer_
,
632 if (result
!= MOJO_RESULT_OK
) {
633 // TODO(yhirano): Handle the error.
636 client_
->didReceiveResponse(
637 loader_
, response
, new WebDataConsumerHandleImpl(consumer
.Pass()));
639 client_
->didReceiveResponse(loader_
, response
);
642 // We may have been cancelled after didReceiveResponse, which would leave us
643 // without a client and therefore without much need to do further handling.
647 DCHECK(!ftp_listing_delegate_
.get());
648 DCHECK(!multipart_delegate_
.get());
649 if (info
.headers
.get() && info
.mime_type
== "multipart/x-mixed-replace") {
650 std::string content_type
;
651 info
.headers
->EnumerateHeader(NULL
, "content-type", &content_type
);
653 std::string mime_type
;
655 bool had_charset
= false;
656 std::string boundary
;
657 net::HttpUtil::ParseContentType(content_type
, &mime_type
, &charset
,
658 &had_charset
, &boundary
);
659 base::TrimString(boundary
, " \"", &boundary
);
661 // If there's no boundary, just handle the request normally. In the gecko
662 // code, nsMultiMixedConv::OnStartRequest throws an exception.
663 if (!boundary
.empty()) {
664 multipart_delegate_
.reset(
665 new MultipartResponseDelegate(client_
, loader_
, response
, boundary
));
667 } else if (info
.mime_type
== "text/vnd.chromium.ftp-dir" &&
669 ftp_listing_delegate_
.reset(
670 new FtpDirectoryListingResponseDelegate(client_
, loader_
, response
));
674 void WebURLLoaderImpl::Context::OnDownloadedData(int len
,
675 int encoded_data_length
) {
677 client_
->didDownloadData(loader_
, len
, encoded_data_length
);
680 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr
<ReceivedData
> data
) {
681 const char* payload
= data
->payload();
682 int data_length
= data
->length();
683 int encoded_data_length
= data
->encoded_length();
687 if (request_
.useStreamOnResponse()) {
688 // We don't support ftp_listening_delegate_ and multipart_delegate_ for now.
689 // TODO(yhirano): Support ftp listening and multipart.
690 MojoResult rv
= WriteDataOnBodyStream(payload
, data_length
);
691 if (rv
!= MOJO_RESULT_OK
&& client_
) {
693 loader_
, CreateWebURLError(request_
.url(), false, net::ERR_FAILED
));
695 } else if (ftp_listing_delegate_
) {
696 // The FTP listing delegate will make the appropriate calls to
697 // client_->didReceiveData and client_->didReceiveResponse. Since the
698 // delegate may want to do work after sending data to the delegate, keep
699 // |this| and the delegate alive until it's finished handling the data.
700 scoped_refptr
<Context
> protect(this);
701 ftp_listing_delegate_
->OnReceivedData(payload
, data_length
);
702 } else if (multipart_delegate_
) {
703 // The multipart delegate will make the appropriate calls to
704 // client_->didReceiveData and client_->didReceiveResponse. Since the
705 // delegate may want to do work after sending data to the delegate, keep
706 // |this| and the delegate alive until it's finished handling the data.
707 scoped_refptr
<Context
> protect(this);
708 multipart_delegate_
->OnReceivedData(payload
, data_length
,
709 encoded_data_length
);
711 client_
->didReceiveData(loader_
, payload
, data_length
, encoded_data_length
);
715 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
716 const char* data
, int len
) {
718 client_
->didReceiveCachedMetadata(loader_
, data
, len
);
721 void WebURLLoaderImpl::Context::OnCompletedRequest(
723 bool was_ignored_by_handler
,
724 bool stale_copy_in_cache
,
725 const std::string
& security_info
,
726 const base::TimeTicks
& completion_time
,
727 int64 total_transfer_size
) {
728 // The WebURLLoaderImpl may be deleted in any of the calls to the client or
729 // the delegates below (As they also may call in to the client). Keep |this|
730 // alive in that case, to avoid a crash. If that happens, the request will be
731 // cancelled and |client_| will be set to NULL.
732 scoped_refptr
<Context
> protect(this);
734 if (ftp_listing_delegate_
) {
735 ftp_listing_delegate_
->OnCompletedRequest();
736 ftp_listing_delegate_
.reset(NULL
);
737 } else if (multipart_delegate_
) {
738 multipart_delegate_
->OnCompletedRequest();
739 multipart_delegate_
.reset(NULL
);
743 if (error_code
!= net::OK
) {
746 CreateWebURLError(request_
.url(), stale_copy_in_cache
, error_code
));
748 if (request_
.useStreamOnResponse()) {
749 got_all_stream_body_data_
= true;
750 if (body_stream_buffer_
.empty()) {
751 // Close the handle to notify the end of data.
752 body_stream_writer_
.reset();
753 client_
->didFinishLoading(
754 loader_
, (completion_time
- TimeTicks()).InSecondsF(),
755 total_transfer_size
);
758 client_
->didFinishLoading(
759 loader_
, (completion_time
- TimeTicks()).InSecondsF(),
760 total_transfer_size
);
766 WebURLLoaderImpl::Context::~Context() {
767 if (request_id_
>= 0) {
768 resource_dispatcher_
->RemovePendingRequest(request_id_
);
772 bool WebURLLoaderImpl::Context::CanHandleDataURLRequestLocally() const {
773 GURL url
= request_
.url();
774 if (!url
.SchemeIs(url::kDataScheme
))
777 // The fast paths for data URL, Start() and HandleDataURL(), don't support
778 // the downloadToFile option.
779 if (request_
.downloadToFile())
782 // Data url requests from object tags may need to be intercepted as streams
783 // and so need to be sent to the browser.
784 if (request_
.requestContext() == WebURLRequest::RequestContextObject
)
787 // Optimize for the case where we can handle a data URL locally. We must
788 // skip this for data URLs targetted at frames since those could trigger a
791 // NOTE: We special case MIME types we can render both for performance
792 // reasons as well as to support unit tests.
794 #if defined(OS_ANDROID)
795 // For compatibility reasons on Android we need to expose top-level data://
797 if (request_
.frameType() == WebURLRequest::FrameTypeTopLevel
)
801 if (request_
.frameType() != WebURLRequest::FrameTypeTopLevel
&&
802 request_
.frameType() != WebURLRequest::FrameTypeNested
)
805 std::string mime_type
, unused_charset
;
806 if (net::DataURL::Parse(request_
.url(), &mime_type
, &unused_charset
, NULL
) &&
807 mime_util::IsSupportedMimeType(mime_type
))
813 void WebURLLoaderImpl::Context::HandleDataURL() {
814 DCHECK_NE(defers_loading_
, DEFERRED_DATA
);
815 if (defers_loading_
== SHOULD_DEFER
) {
816 defers_loading_
= DEFERRED_DATA
;
820 ResourceResponseInfo info
;
823 int error_code
= GetInfoFromDataURL(request_
.url(), &info
, &data
);
825 if (error_code
== net::OK
) {
826 OnReceivedResponse(info
);
829 make_scoped_ptr(new FixedReceivedData(data
.data(), data
.size(), 0)));
832 OnCompletedRequest(error_code
, false, false, info
.security_info
,
833 base::TimeTicks::Now(), 0);
836 MojoResult
WebURLLoaderImpl::Context::WriteDataOnBodyStream(const char* data
,
838 if (body_stream_buffer_
.empty() && size
== 0) {
840 return MOJO_RESULT_OK
;
843 if (!body_stream_writer_
.is_valid()) {
844 // The handle is already cleared.
845 return MOJO_RESULT_OK
;
848 char* buffer
= nullptr;
849 uint32_t num_bytes_writable
= 0;
850 MojoResult rv
= mojo::BeginWriteDataRaw(body_stream_writer_
.get(),
851 reinterpret_cast<void**>(&buffer
),
853 MOJO_WRITE_DATA_FLAG_NONE
);
854 if (rv
== MOJO_RESULT_SHOULD_WAIT
) {
855 body_stream_buffer_
.insert(body_stream_buffer_
.end(), data
, data
+ size
);
856 body_stream_writer_watcher_
.Start(
857 body_stream_writer_
.get(),
858 MOJO_HANDLE_SIGNAL_WRITABLE
,
859 MOJO_DEADLINE_INDEFINITE
,
860 base::Bind(&WebURLLoaderImpl::Context::OnHandleGotWritable
,
861 base::Unretained(this)));
862 return MOJO_RESULT_OK
;
865 if (rv
!= MOJO_RESULT_OK
)
868 uint32_t num_bytes_to_write
= 0;
869 if (num_bytes_writable
< body_stream_buffer_
.size()) {
870 auto begin
= body_stream_buffer_
.begin();
871 auto end
= body_stream_buffer_
.begin() + num_bytes_writable
;
873 std::copy(begin
, end
, buffer
);
874 num_bytes_to_write
= num_bytes_writable
;
875 body_stream_buffer_
.erase(begin
, end
);
876 body_stream_buffer_
.insert(body_stream_buffer_
.end(), data
, data
+ size
);
878 std::copy(body_stream_buffer_
.begin(), body_stream_buffer_
.end(), buffer
);
879 num_bytes_writable
-= body_stream_buffer_
.size();
880 num_bytes_to_write
+= body_stream_buffer_
.size();
881 buffer
+= body_stream_buffer_
.size();
882 body_stream_buffer_
.clear();
884 size_t num_newbytes_to_write
=
885 std::min(size
, static_cast<size_t>(num_bytes_writable
));
886 std::copy(data
, data
+ num_newbytes_to_write
, buffer
);
887 num_bytes_to_write
+= num_newbytes_to_write
;
888 body_stream_buffer_
.insert(body_stream_buffer_
.end(),
889 data
+ num_newbytes_to_write
,
893 rv
= mojo::EndWriteDataRaw(body_stream_writer_
.get(), num_bytes_to_write
);
894 if (rv
== MOJO_RESULT_OK
&& !body_stream_buffer_
.empty()) {
895 body_stream_writer_watcher_
.Start(
896 body_stream_writer_
.get(),
897 MOJO_HANDLE_SIGNAL_WRITABLE
,
898 MOJO_DEADLINE_INDEFINITE
,
899 base::Bind(&WebURLLoaderImpl::Context::OnHandleGotWritable
,
900 base::Unretained(this)));
905 void WebURLLoaderImpl::Context::OnHandleGotWritable(MojoResult result
) {
906 if (result
!= MOJO_RESULT_OK
) {
909 loader_
, CreateWebURLError(request_
.url(), false, net::ERR_FAILED
));
910 // |this| can be deleted here.
915 if (body_stream_buffer_
.empty())
918 MojoResult rv
= WriteDataOnBodyStream(nullptr, 0);
919 if (rv
== MOJO_RESULT_OK
) {
920 if (got_all_stream_body_data_
&& body_stream_buffer_
.empty()) {
921 // Close the handle to notify the end of data.
922 body_stream_writer_
.reset();
924 // TODO(yhirano): Pass appropriate arguments.
925 client_
->didFinishLoading(loader_
, 0, 0);
926 // |this| can be deleted here.
932 loader_
, CreateWebURLError(request_
.url(), false, net::ERR_FAILED
));
933 // |this| can be deleted here.
938 // WebURLLoaderImpl -----------------------------------------------------------
940 WebURLLoaderImpl::WebURLLoaderImpl(
941 ResourceDispatcher
* resource_dispatcher
,
942 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
)
943 : context_(new Context(this, resource_dispatcher
, task_runner
)) {
946 WebURLLoaderImpl::~WebURLLoaderImpl() {
950 void WebURLLoaderImpl::PopulateURLResponse(const GURL
& url
,
951 const ResourceResponseInfo
& info
,
952 WebURLResponse
* response
) {
953 response
->setURL(url
);
954 response
->setResponseTime(info
.response_time
.ToInternalValue());
955 response
->setMIMEType(WebString::fromUTF8(info
.mime_type
));
956 response
->setTextEncodingName(WebString::fromUTF8(info
.charset
));
957 response
->setExpectedContentLength(info
.content_length
);
958 response
->setSecurityInfo(info
.security_info
);
959 response
->setAppCacheID(info
.appcache_id
);
960 response
->setAppCacheManifestURL(info
.appcache_manifest_url
);
961 response
->setWasCached(!info
.load_timing
.request_start_time
.is_null() &&
962 info
.response_time
< info
.load_timing
.request_start_time
);
963 response
->setRemoteIPAddress(
964 WebString::fromUTF8(info
.socket_address
.host()));
965 response
->setRemotePort(info
.socket_address
.port());
966 response
->setConnectionID(info
.load_timing
.socket_log_id
);
967 response
->setConnectionReused(info
.load_timing
.socket_reused
);
968 response
->setDownloadFilePath(info
.download_file_path
.AsUTF16Unsafe());
969 response
->setWasFetchedViaSPDY(info
.was_fetched_via_spdy
);
970 response
->setWasFetchedViaServiceWorker(info
.was_fetched_via_service_worker
);
971 response
->setWasFallbackRequiredByServiceWorker(
972 info
.was_fallback_required_by_service_worker
);
973 response
->setServiceWorkerResponseType(info
.response_type_via_service_worker
);
974 response
->setOriginalURLViaServiceWorker(
975 info
.original_url_via_service_worker
);
977 WebURLResponseExtraDataImpl
* extra_data
=
978 new WebURLResponseExtraDataImpl(info
.npn_negotiated_protocol
);
979 response
->setExtraData(extra_data
);
980 extra_data
->set_was_fetched_via_spdy(info
.was_fetched_via_spdy
);
981 extra_data
->set_was_npn_negotiated(info
.was_npn_negotiated
);
982 extra_data
->set_was_alternate_protocol_available(
983 info
.was_alternate_protocol_available
);
984 extra_data
->set_connection_info(info
.connection_info
);
985 extra_data
->set_was_fetched_via_proxy(info
.was_fetched_via_proxy
);
986 extra_data
->set_proxy_server(info
.proxy_server
);
988 // If there's no received headers end time, don't set load timing. This is
989 // the case for non-HTTP requests, requests that don't go over the wire, and
990 // certain error cases.
991 if (!info
.load_timing
.receive_headers_end
.is_null()) {
992 WebURLLoadTiming timing
;
993 PopulateURLLoadTiming(info
.load_timing
, &timing
);
994 const TimeTicks kNullTicks
;
995 timing
.setWorkerStart(
996 (info
.service_worker_start_time
- kNullTicks
).InSecondsF());
997 timing
.setWorkerReady(
998 (info
.service_worker_ready_time
- kNullTicks
).InSecondsF());
999 response
->setLoadTiming(timing
);
1002 if (info
.devtools_info
.get()) {
1003 WebHTTPLoadInfo load_info
;
1005 load_info
.setHTTPStatusCode(info
.devtools_info
->http_status_code
);
1006 load_info
.setHTTPStatusText(WebString::fromLatin1(
1007 info
.devtools_info
->http_status_text
));
1008 load_info
.setEncodedDataLength(info
.encoded_data_length
);
1010 load_info
.setRequestHeadersText(WebString::fromLatin1(
1011 info
.devtools_info
->request_headers_text
));
1012 load_info
.setResponseHeadersText(WebString::fromLatin1(
1013 info
.devtools_info
->response_headers_text
));
1014 const HeadersVector
& request_headers
= info
.devtools_info
->request_headers
;
1015 for (HeadersVector::const_iterator it
= request_headers
.begin();
1016 it
!= request_headers
.end(); ++it
) {
1017 load_info
.addRequestHeader(WebString::fromLatin1(it
->first
),
1018 WebString::fromLatin1(it
->second
));
1020 const HeadersVector
& response_headers
=
1021 info
.devtools_info
->response_headers
;
1022 for (HeadersVector::const_iterator it
= response_headers
.begin();
1023 it
!= response_headers
.end(); ++it
) {
1024 load_info
.addResponseHeader(WebString::fromLatin1(it
->first
),
1025 WebString::fromLatin1(it
->second
));
1027 load_info
.setNPNNegotiatedProtocol(WebString::fromLatin1(
1028 info
.npn_negotiated_protocol
));
1029 response
->setHTTPLoadInfo(load_info
);
1032 const net::HttpResponseHeaders
* headers
= info
.headers
.get();
1036 WebURLResponse::HTTPVersion version
= WebURLResponse::Unknown
;
1037 if (headers
->GetHttpVersion() == net::HttpVersion(0, 9))
1038 version
= WebURLResponse::HTTP_0_9
;
1039 else if (headers
->GetHttpVersion() == net::HttpVersion(1, 0))
1040 version
= WebURLResponse::HTTP_1_0
;
1041 else if (headers
->GetHttpVersion() == net::HttpVersion(1, 1))
1042 version
= WebURLResponse::HTTP_1_1
;
1043 response
->setHTTPVersion(version
);
1044 response
->setHTTPStatusCode(headers
->response_code());
1045 response
->setHTTPStatusText(WebString::fromLatin1(headers
->GetStatusText()));
1047 // TODO(darin): We should leverage HttpResponseHeaders for this, and this
1048 // should be using the same code as ResourceDispatcherHost.
1049 // TODO(jungshik): Figure out the actual value of the referrer charset and
1050 // pass it to GetSuggestedFilename.
1052 headers
->EnumerateHeader(NULL
, "content-disposition", &value
);
1053 response
->setSuggestedFileName(
1054 net::GetSuggestedFilename(url
,
1056 std::string(), // referrer_charset
1057 std::string(), // suggested_name
1058 std::string(), // mime_type
1059 std::string())); // default_name
1062 if (headers
->GetLastModifiedValue(&time_val
))
1063 response
->setLastModifiedDate(time_val
.ToDoubleT());
1065 // Build up the header map.
1068 while (headers
->EnumerateHeaderLines(&iter
, &name
, &value
)) {
1069 response
->addHTTPHeaderField(WebString::fromLatin1(name
),
1070 WebString::fromLatin1(value
));
1074 void WebURLLoaderImpl::loadSynchronously(const WebURLRequest
& request
,
1075 WebURLResponse
& response
,
1078 SyncLoadResponse sync_load_response
;
1079 context_
->Start(request
, &sync_load_response
);
1081 const GURL
& final_url
= sync_load_response
.url
;
1083 // TODO(tc): For file loads, we may want to include a more descriptive
1084 // status code or status text.
1085 int error_code
= sync_load_response
.error_code
;
1086 if (error_code
!= net::OK
) {
1087 response
.setURL(final_url
);
1088 error
.domain
= WebString::fromUTF8(net::kErrorDomain
);
1089 error
.reason
= error_code
;
1090 error
.unreachableURL
= final_url
;
1094 PopulateURLResponse(final_url
, sync_load_response
, &response
);
1096 data
.assign(sync_load_response
.data
.data(),
1097 sync_load_response
.data
.size());
1100 void WebURLLoaderImpl::loadAsynchronously(const WebURLRequest
& request
,
1101 WebURLLoaderClient
* client
) {
1102 DCHECK(!context_
->client());
1104 context_
->set_client(client
);
1105 context_
->Start(request
, NULL
);
1108 void WebURLLoaderImpl::cancel() {
1112 void WebURLLoaderImpl::setDefersLoading(bool value
) {
1113 context_
->SetDefersLoading(value
);
1116 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority
,
1117 int intra_priority_value
) {
1118 context_
->DidChangePriority(new_priority
, intra_priority_value
);
1121 bool WebURLLoaderImpl::attachThreadedDataReceiver(
1122 blink::WebThreadedDataReceiver
* threaded_data_receiver
) {
1123 return context_
->AttachThreadedDataReceiver(threaded_data_receiver
);
1126 } // namespace content