Remove DnsConfigServiceTest.GetSystemConfig test
[chromium-blink-merge.git] / content / child / web_url_loader_impl.cc
blob4280dbc4363ab75fbbd9067a758e3238e768dd9b
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"
7 #include <algorithm>
8 #include <deque>
9 #include <string>
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"
52 using base::Time;
53 using base::TimeTicks;
54 using blink::WebData;
55 using blink::WebHTTPBody;
56 using blink::WebHTTPHeaderVisitor;
57 using blink::WebHTTPLoadInfo;
58 using blink::WebReferrerPolicy;
59 using blink::WebSecurityPolicy;
60 using blink::WebString;
61 using blink::WebURL;
62 using blink::WebURLError;
63 using blink::WebURLLoadTiming;
64 using blink::WebURLLoader;
65 using blink::WebURLLoaderClient;
66 using blink::WebURLRequest;
67 using blink::WebURLResponse;
69 namespace content {
71 // Utilities ------------------------------------------------------------------
73 namespace {
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) {
114 switch (priority) {
115 case WebURLRequest::PriorityVeryHigh:
116 return net::HIGHEST;
118 case WebURLRequest::PriorityHigh:
119 return net::MEDIUM;
121 case WebURLRequest::PriorityMedium:
122 return net::LOW;
124 case WebURLRequest::PriorityLow:
125 return net::LOWEST;
127 case WebURLRequest::PriorityVeryLow:
128 return net::IDLE;
130 case WebURLRequest::PriorityUnresolved:
131 default:
132 NOTREACHED();
133 return net::LOW;
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,
141 std::string* data) {
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;
150 std::string charset;
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)
156 return result;
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;
165 return net::OK;
168 #define STATIC_ASSERT_MATCHING_ENUMS(content_name, blink_name) \
169 static_assert( \
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());
285 } // namespace
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>,
293 public RequestPeer {
294 public:
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; }
302 void Cancel();
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;
326 private:
327 friend class base::RefCounted<Context>;
328 ~Context() override;
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
348 // mechanism.
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_;
353 int request_id_;
356 WebURLLoaderImpl::Context::Context(
357 WebURLLoaderImpl* loader,
358 ResourceDispatcher* resource_dispatcher,
359 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
360 : loader_(loader),
361 client_(NULL),
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),
367 request_id_(-1) {
370 void WebURLLoaderImpl::Context::Cancel() {
371 if (resource_dispatcher_ && // NULL in unittest.
372 request_id_ != -1) {
373 resource_dispatcher_->Cancel(request_id_);
374 request_id_ = -1;
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.
386 client_ = NULL;
387 loader_ = NULL;
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(
408 request_id_,
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);
421 return false;
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
438 // browser.
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);
455 } else {
456 task_runner_->PostTask(FROM_HERE,
457 base::Bind(&Context::HandleDataURL, this));
459 return;
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
465 // block above.
466 DCHECK_IMPLIES(base::CommandLine::ForCurrentProcess()->HasSwitch(
467 switches::kEnableBrowserSideNavigation),
468 stream_override_.get() ||
469 request.frameType() == WebURLRequest::FrameTypeNone);
471 GURL referrer_url(
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);
520 return;
523 request_id_ = resource_dispatcher_->StartAsync(
524 request_info, request_body.get(), this);
527 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) {
528 if (client_)
529 client_->didSendData(loader_, position, size);
532 bool WebURLLoaderImpl::Context::OnReceivedRedirect(
533 const net::RedirectInfo& redirect_info,
534 const ResourceResponseInfo& info) {
535 if (!client_)
536 return false;
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),
556 referrer_policy_);
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());
576 return true;
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());
582 return false;
585 void WebURLLoaderImpl::Context::OnReceivedResponse(
586 const ResourceResponseInfo& initial_info) {
587 if (!client_)
588 return;
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");
610 } else {
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
618 // it.
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_,
631 &consumer);
632 if (result != MOJO_RESULT_OK) {
633 // TODO(yhirano): Handle the error.
634 return;
636 client_->didReceiveResponse(
637 loader_, response, new WebDataConsumerHandleImpl(consumer.Pass()));
638 } else {
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.
644 if (!client_)
645 return;
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;
654 std::string charset;
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" &&
668 !show_raw_listing) {
669 ftp_listing_delegate_.reset(
670 new FtpDirectoryListingResponseDelegate(client_, loader_, response));
674 void WebURLLoaderImpl::Context::OnDownloadedData(int len,
675 int encoded_data_length) {
676 if (client_)
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();
684 if (!client_)
685 return;
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_) {
692 client_->didFail(
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);
710 } else {
711 client_->didReceiveData(loader_, payload, data_length, encoded_data_length);
715 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
716 const char* data, int len) {
717 if (client_)
718 client_->didReceiveCachedMetadata(loader_, data, len);
721 void WebURLLoaderImpl::Context::OnCompletedRequest(
722 int error_code,
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);
742 if (client_) {
743 if (error_code != net::OK) {
744 client_->didFail(
745 loader_,
746 CreateWebURLError(request_.url(), stale_copy_in_cache, error_code));
747 } else {
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);
757 } else {
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))
775 return false;
777 // The fast paths for data URL, Start() and HandleDataURL(), don't support
778 // the downloadToFile option.
779 if (request_.downloadToFile())
780 return false;
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)
785 return false;
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
789 // download.
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://
796 // to the browser.
797 if (request_.frameType() == WebURLRequest::FrameTypeTopLevel)
798 return false;
799 #endif
801 if (request_.frameType() != WebURLRequest::FrameTypeTopLevel &&
802 request_.frameType() != WebURLRequest::FrameTypeNested)
803 return true;
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))
808 return true;
810 return false;
813 void WebURLLoaderImpl::Context::HandleDataURL() {
814 DCHECK_NE(defers_loading_, DEFERRED_DATA);
815 if (defers_loading_ == SHOULD_DEFER) {
816 defers_loading_ = DEFERRED_DATA;
817 return;
820 ResourceResponseInfo info;
821 std::string data;
823 int error_code = GetInfoFromDataURL(request_.url(), &info, &data);
825 if (error_code == net::OK) {
826 OnReceivedResponse(info);
827 if (!data.empty())
828 OnReceivedData(
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,
837 size_t size) {
838 if (body_stream_buffer_.empty() && size == 0) {
839 // Nothing to do.
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),
852 &num_bytes_writable,
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)
866 return rv;
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);
877 } else {
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,
890 data + size);
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)));
902 return rv;
905 void WebURLLoaderImpl::Context::OnHandleGotWritable(MojoResult result) {
906 if (result != MOJO_RESULT_OK) {
907 if (client_) {
908 client_->didFail(
909 loader_, CreateWebURLError(request_.url(), false, net::ERR_FAILED));
910 // |this| can be deleted here.
912 return;
915 if (body_stream_buffer_.empty())
916 return;
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();
923 if (client_) {
924 // TODO(yhirano): Pass appropriate arguments.
925 client_->didFinishLoading(loader_, 0, 0);
926 // |this| can be deleted here.
929 } else {
930 if (client_) {
931 client_->didFail(
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() {
947 cancel();
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 response->setLoadTiming(timing);
1000 if (info.devtools_info.get()) {
1001 WebHTTPLoadInfo load_info;
1003 load_info.setHTTPStatusCode(info.devtools_info->http_status_code);
1004 load_info.setHTTPStatusText(WebString::fromLatin1(
1005 info.devtools_info->http_status_text));
1006 load_info.setEncodedDataLength(info.encoded_data_length);
1008 load_info.setRequestHeadersText(WebString::fromLatin1(
1009 info.devtools_info->request_headers_text));
1010 load_info.setResponseHeadersText(WebString::fromLatin1(
1011 info.devtools_info->response_headers_text));
1012 const HeadersVector& request_headers = info.devtools_info->request_headers;
1013 for (HeadersVector::const_iterator it = request_headers.begin();
1014 it != request_headers.end(); ++it) {
1015 load_info.addRequestHeader(WebString::fromLatin1(it->first),
1016 WebString::fromLatin1(it->second));
1018 const HeadersVector& response_headers =
1019 info.devtools_info->response_headers;
1020 for (HeadersVector::const_iterator it = response_headers.begin();
1021 it != response_headers.end(); ++it) {
1022 load_info.addResponseHeader(WebString::fromLatin1(it->first),
1023 WebString::fromLatin1(it->second));
1025 load_info.setNPNNegotiatedProtocol(WebString::fromLatin1(
1026 info.npn_negotiated_protocol));
1027 response->setHTTPLoadInfo(load_info);
1030 const net::HttpResponseHeaders* headers = info.headers.get();
1031 if (!headers)
1032 return;
1034 WebURLResponse::HTTPVersion version = WebURLResponse::Unknown;
1035 if (headers->GetHttpVersion() == net::HttpVersion(0, 9))
1036 version = WebURLResponse::HTTP_0_9;
1037 else if (headers->GetHttpVersion() == net::HttpVersion(1, 0))
1038 version = WebURLResponse::HTTP_1_0;
1039 else if (headers->GetHttpVersion() == net::HttpVersion(1, 1))
1040 version = WebURLResponse::HTTP_1_1;
1041 response->setHTTPVersion(version);
1042 response->setHTTPStatusCode(headers->response_code());
1043 response->setHTTPStatusText(WebString::fromLatin1(headers->GetStatusText()));
1045 // TODO(darin): We should leverage HttpResponseHeaders for this, and this
1046 // should be using the same code as ResourceDispatcherHost.
1047 // TODO(jungshik): Figure out the actual value of the referrer charset and
1048 // pass it to GetSuggestedFilename.
1049 std::string value;
1050 headers->EnumerateHeader(NULL, "content-disposition", &value);
1051 response->setSuggestedFileName(
1052 net::GetSuggestedFilename(url,
1053 value,
1054 std::string(), // referrer_charset
1055 std::string(), // suggested_name
1056 std::string(), // mime_type
1057 std::string())); // default_name
1059 Time time_val;
1060 if (headers->GetLastModifiedValue(&time_val))
1061 response->setLastModifiedDate(time_val.ToDoubleT());
1063 // Build up the header map.
1064 void* iter = NULL;
1065 std::string name;
1066 while (headers->EnumerateHeaderLines(&iter, &name, &value)) {
1067 response->addHTTPHeaderField(WebString::fromLatin1(name),
1068 WebString::fromLatin1(value));
1072 void WebURLLoaderImpl::loadSynchronously(const WebURLRequest& request,
1073 WebURLResponse& response,
1074 WebURLError& error,
1075 WebData& data) {
1076 SyncLoadResponse sync_load_response;
1077 context_->Start(request, &sync_load_response);
1079 const GURL& final_url = sync_load_response.url;
1081 // TODO(tc): For file loads, we may want to include a more descriptive
1082 // status code or status text.
1083 int error_code = sync_load_response.error_code;
1084 if (error_code != net::OK) {
1085 response.setURL(final_url);
1086 error.domain = WebString::fromUTF8(net::kErrorDomain);
1087 error.reason = error_code;
1088 error.unreachableURL = final_url;
1089 return;
1092 PopulateURLResponse(final_url, sync_load_response, &response);
1094 data.assign(sync_load_response.data.data(),
1095 sync_load_response.data.size());
1098 void WebURLLoaderImpl::loadAsynchronously(const WebURLRequest& request,
1099 WebURLLoaderClient* client) {
1100 DCHECK(!context_->client());
1102 context_->set_client(client);
1103 context_->Start(request, NULL);
1106 void WebURLLoaderImpl::cancel() {
1107 context_->Cancel();
1110 void WebURLLoaderImpl::setDefersLoading(bool value) {
1111 context_->SetDefersLoading(value);
1114 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority,
1115 int intra_priority_value) {
1116 context_->DidChangePriority(new_priority, intra_priority_value);
1119 bool WebURLLoaderImpl::attachThreadedDataReceiver(
1120 blink::WebThreadedDataReceiver* threaded_data_receiver) {
1121 return context_->AttachThreadedDataReceiver(threaded_data_receiver);
1124 } // namespace content