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
3 // found in the LICENSE file.
5 // An implementation of WebURLLoader in terms of ResourceLoaderBridge.
7 #include "webkit/child/weburlloader_impl.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_util.h"
14 #include "base/time/time.h"
15 #include "net/base/data_url.h"
16 #include "net/base/load_flags.h"
17 #include "net/base/mime_util.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_util.h"
20 #include "net/http/http_response_headers.h"
21 #include "net/http/http_util.h"
22 #include "net/url_request/url_request.h"
23 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
24 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h"
25 #include "third_party/WebKit/public/platform/WebURL.h"
26 #include "third_party/WebKit/public/platform/WebURLError.h"
27 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
28 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
29 #include "third_party/WebKit/public/platform/WebURLRequest.h"
30 #include "third_party/WebKit/public/platform/WebURLResponse.h"
31 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
32 #include "webkit/child/ftp_directory_listing_response_delegate.h"
33 #include "webkit/child/multipart_response_delegate.h"
34 #include "webkit/child/resource_loader_bridge.h"
35 #include "webkit/child/webkitplatformsupport_impl.h"
36 #include "webkit/child/weburlrequest_extradata_impl.h"
37 #include "webkit/child/weburlresponse_extradata_impl.h"
38 #include "webkit/common/resource_request_body.h"
41 using base::TimeTicks;
43 using blink::WebHTTPBody;
44 using blink::WebHTTPHeaderVisitor;
45 using blink::WebHTTPLoadInfo;
46 using blink::WebReferrerPolicy;
47 using blink::WebSecurityPolicy;
48 using blink::WebString;
50 using blink::WebURLError;
51 using blink::WebURLLoadTiming;
52 using blink::WebURLLoader;
53 using blink::WebURLLoaderClient;
54 using blink::WebURLRequest;
55 using blink::WebURLResponse;
57 namespace webkit_glue {
59 // Utilities ------------------------------------------------------------------
63 const char kThrottledErrorDescription[] =
64 "Request throttled. Visit http://dev.chromium.org/throttling for more "
67 class HeaderFlattener : public WebHTTPHeaderVisitor {
69 explicit HeaderFlattener(int load_flags)
70 : load_flags_(load_flags),
71 has_accept_header_(false) {
74 virtual void visitHeader(const WebString& name, const WebString& value) {
75 // Headers are latin1.
76 const std::string& name_latin1 = name.latin1();
77 const std::string& value_latin1 = value.latin1();
79 // Skip over referrer headers found in the header map because we already
80 // pulled it out as a separate parameter.
81 if (LowerCaseEqualsASCII(name_latin1, "referer"))
84 // Skip over "Cache-Control: max-age=0" header if the corresponding
85 // load flag is already specified. FrameLoader sets both the flag and
86 // the extra header -- the extra header is redundant since our network
87 // implementation will add the necessary headers based on load flags.
88 // See http://code.google.com/p/chromium/issues/detail?id=3434.
89 if ((load_flags_ & net::LOAD_VALIDATE_CACHE) &&
90 LowerCaseEqualsASCII(name_latin1, "cache-control") &&
91 LowerCaseEqualsASCII(value_latin1, "max-age=0"))
94 if (LowerCaseEqualsASCII(name_latin1, "accept"))
95 has_accept_header_ = true;
98 buffer_.append("\r\n");
99 buffer_.append(name_latin1 + ": " + value_latin1);
102 const std::string& GetBuffer() {
103 // In some cases, WebKit doesn't add an Accept header, but not having the
104 // header confuses some web servers. See bug 808613.
105 if (!has_accept_header_) {
106 if (!buffer_.empty())
107 buffer_.append("\r\n");
108 buffer_.append("Accept: */*");
109 has_accept_header_ = true;
117 bool has_accept_header_;
120 // Extracts the information from a data: url.
121 bool GetInfoFromDataURL(const GURL& url,
122 ResourceResponseInfo* info,
125 std::string mime_type;
127 if (net::DataURL::Parse(url, &mime_type, &charset, data)) {
128 *error_code = net::OK;
129 // Assure same time for all time fields of data: URLs.
130 Time now = Time::Now();
131 info->load_timing.request_start = TimeTicks::Now();
132 info->load_timing.request_start_time = now;
133 info->request_time = now;
134 info->response_time = now;
135 info->headers = NULL;
136 info->mime_type.swap(mime_type);
137 info->charset.swap(charset);
138 info->security_info.clear();
139 info->content_length = data->length();
140 info->encoded_data_length = 0;
145 *error_code = net::ERR_INVALID_URL;
149 typedef ResourceDevToolsInfo::HeadersVector HeadersVector;
151 // Converts timing data from |load_timing| to the format used by WebKit.
152 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing,
153 WebURLLoadTiming* url_timing) {
154 DCHECK(!load_timing.request_start.is_null());
156 const TimeTicks kNullTicks;
157 url_timing->initialize();
158 url_timing->setRequestTime(
159 (load_timing.request_start - kNullTicks).InSecondsF());
160 url_timing->setProxyStart(
161 (load_timing.proxy_resolve_start - kNullTicks).InSecondsF());
162 url_timing->setProxyEnd(
163 (load_timing.proxy_resolve_end - kNullTicks).InSecondsF());
164 url_timing->setDNSStart(
165 (load_timing.connect_timing.dns_start - kNullTicks).InSecondsF());
166 url_timing->setDNSEnd(
167 (load_timing.connect_timing.dns_end - kNullTicks).InSecondsF());
168 url_timing->setConnectStart(
169 (load_timing.connect_timing.connect_start - kNullTicks).InSecondsF());
170 url_timing->setConnectEnd(
171 (load_timing.connect_timing.connect_end - kNullTicks).InSecondsF());
172 url_timing->setSSLStart(
173 (load_timing.connect_timing.ssl_start - kNullTicks).InSecondsF());
174 url_timing->setSSLEnd(
175 (load_timing.connect_timing.ssl_end - kNullTicks).InSecondsF());
176 url_timing->setSendStart(
177 (load_timing.send_start - kNullTicks).InSecondsF());
178 url_timing->setSendEnd(
179 (load_timing.send_end - kNullTicks).InSecondsF());
180 url_timing->setReceiveHeadersEnd(
181 (load_timing.receive_headers_end - kNullTicks).InSecondsF());
184 net::RequestPriority ConvertWebKitPriorityToNetPriority(
185 const WebURLRequest::Priority& priority) {
187 case WebURLRequest::PriorityVeryHigh:
190 case WebURLRequest::PriorityHigh:
193 case WebURLRequest::PriorityMedium:
196 case WebURLRequest::PriorityLow:
199 case WebURLRequest::PriorityVeryLow:
202 case WebURLRequest::PriorityUnresolved:
211 // WebURLLoaderImpl::Context --------------------------------------------------
213 // This inner class exists since the WebURLLoader may be deleted while inside a
214 // call to WebURLLoaderClient. The bridge requires its Peer to stay alive
215 // until it receives OnCompletedRequest.
216 class WebURLLoaderImpl::Context : public base::RefCounted<Context>,
217 public ResourceLoaderBridge::Peer {
219 explicit Context(WebURLLoaderImpl* loader);
221 WebURLLoaderClient* client() const { return client_; }
222 void set_client(WebURLLoaderClient* client) { client_ = client; }
225 void SetDefersLoading(bool value);
226 void DidChangePriority(WebURLRequest::Priority new_priority);
228 const WebURLRequest& request,
229 ResourceLoaderBridge::SyncLoadResponse* sync_load_response,
230 WebKitPlatformSupportImpl* platform);
232 // ResourceLoaderBridge::Peer methods:
233 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE;
234 virtual bool OnReceivedRedirect(
236 const ResourceResponseInfo& info,
237 bool* has_new_first_party_for_cookies,
238 GURL* new_first_party_for_cookies) OVERRIDE;
239 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE;
240 virtual void OnDownloadedData(int len, int encoded_data_length) OVERRIDE;
241 virtual void OnReceivedData(const char* data,
243 int encoded_data_length) OVERRIDE;
244 virtual void OnReceivedCachedMetadata(const char* data, int len) OVERRIDE;
245 virtual void OnCompletedRequest(
247 bool was_ignored_by_handler,
248 bool stale_copy_in_cache,
249 const std::string& security_info,
250 const base::TimeTicks& completion_time,
251 int64 total_transfer_size) OVERRIDE;
254 friend class base::RefCounted<Context>;
255 virtual ~Context() {}
257 // We can optimize the handling of data URLs in most cases.
258 bool CanHandleDataURL(const GURL& url) const;
259 void HandleDataURL();
261 WebURLLoaderImpl* loader_;
262 WebURLRequest request_;
263 WebURLLoaderClient* client_;
264 WebReferrerPolicy referrer_policy_;
265 scoped_ptr<ResourceLoaderBridge> bridge_;
266 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_;
267 scoped_ptr<MultipartResponseDelegate> multipart_delegate_;
268 scoped_ptr<ResourceLoaderBridge> completed_bridge_;
271 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader)
274 referrer_policy_(blink::WebReferrerPolicyDefault) {
277 void WebURLLoaderImpl::Context::Cancel() {
278 // The bridge will still send OnCompletedRequest, which will Release() us, so
279 // we don't do that here.
283 // Ensure that we do not notify the multipart delegate anymore as it has
284 // its own pointer to the client.
285 if (multipart_delegate_)
286 multipart_delegate_->Cancel();
288 // Do not make any further calls to the client.
293 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) {
295 bridge_->SetDefersLoading(value);
298 void WebURLLoaderImpl::Context::DidChangePriority(
299 WebURLRequest::Priority new_priority) {
301 bridge_->DidChangePriority(
302 ConvertWebKitPriorityToNetPriority(new_priority));
305 void WebURLLoaderImpl::Context::Start(
306 const WebURLRequest& request,
307 ResourceLoaderBridge::SyncLoadResponse* sync_load_response,
308 WebKitPlatformSupportImpl* platform) {
309 DCHECK(!bridge_.get());
311 request_ = request; // Save the request.
313 GURL url = request.url();
314 if (url.SchemeIs("data") && CanHandleDataURL(url)) {
315 if (sync_load_response) {
316 // This is a sync load. Do the work now.
317 sync_load_response->url = url;
319 GetInfoFromDataURL(sync_load_response->url, sync_load_response,
320 &sync_load_response->data,
321 &sync_load_response->error_code);
323 AddRef(); // Balanced in OnCompletedRequest
324 base::MessageLoop::current()->PostTask(
325 FROM_HERE, base::Bind(&Context::HandleDataURL, this));
331 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1());
332 const std::string& method = request.httpMethod().latin1();
334 int load_flags = net::LOAD_NORMAL;
335 switch (request.cachePolicy()) {
336 case WebURLRequest::ReloadIgnoringCacheData:
337 // Required by LayoutTests/http/tests/misc/refresh-headers.php
338 load_flags |= net::LOAD_VALIDATE_CACHE;
340 case WebURLRequest::ReturnCacheDataElseLoad:
341 load_flags |= net::LOAD_PREFERRING_CACHE;
343 case WebURLRequest::ReturnCacheDataDontLoad:
344 load_flags |= net::LOAD_ONLY_FROM_CACHE;
346 case WebURLRequest::UseProtocolCachePolicy:
350 if (request.reportUploadProgress())
351 load_flags |= net::LOAD_ENABLE_UPLOAD_PROGRESS;
352 if (request.reportLoadTiming())
353 load_flags |= net::LOAD_ENABLE_LOAD_TIMING;
354 if (request.reportRawHeaders())
355 load_flags |= net::LOAD_REPORT_RAW_HEADERS;
357 if (!request.allowCookies() || !request.allowStoredCredentials()) {
358 load_flags |= net::LOAD_DO_NOT_SAVE_COOKIES;
359 load_flags |= net::LOAD_DO_NOT_SEND_COOKIES;
362 if (!request.allowStoredCredentials())
363 load_flags |= net::LOAD_DO_NOT_SEND_AUTH_DATA;
365 if (request.targetType() == WebURLRequest::TargetIsXHR &&
366 (url.has_username() || url.has_password())) {
367 load_flags |= net::LOAD_DO_NOT_PROMPT_FOR_LOGIN;
370 HeaderFlattener flattener(load_flags);
371 request.visitHTTPHeaderFields(&flattener);
373 // TODO(brettw) this should take parameter encoding into account when
374 // creating the GURLs.
376 ResourceLoaderBridge::RequestInfo request_info;
377 request_info.method = method;
378 request_info.url = url;
379 request_info.first_party_for_cookies = request.firstPartyForCookies();
380 request_info.referrer = referrer_url;
381 request_info.headers = flattener.GetBuffer();
382 request_info.load_flags = load_flags;
383 // requestor_pid only needs to be non-zero if the request originates outside
384 // the render process, so we can use requestorProcessID even for requests
385 // from in-process plugins.
386 request_info.requestor_pid = request.requestorProcessID();
387 request_info.request_type =
388 ResourceType::FromTargetType(request.targetType());
389 request_info.priority =
390 ConvertWebKitPriorityToNetPriority(request.priority());
391 request_info.appcache_host_id = request.appCacheHostID();
392 request_info.routing_id = request.requestorID();
393 request_info.download_to_file = request.downloadToFile();
394 request_info.has_user_gesture = request.hasUserGesture();
395 request_info.extra_data = request.extraData();
396 referrer_policy_ = request.referrerPolicy();
397 request_info.referrer_policy = request.referrerPolicy();
398 bridge_.reset(platform->CreateResourceLoader(request_info));
400 if (!request.httpBody().isNull()) {
401 // GET and HEAD requests shouldn't have http bodies.
402 DCHECK(method != "GET" && method != "HEAD");
403 const WebHTTPBody& httpBody = request.httpBody();
405 WebHTTPBody::Element element;
406 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody;
407 while (httpBody.elementAt(i++, element)) {
408 switch (element.type) {
409 case WebHTTPBody::Element::TypeData:
410 if (!element.data.isEmpty()) {
411 // WebKit sometimes gives up empty data to append. These aren't
412 // necessary so we just optimize those out here.
413 request_body->AppendBytes(
414 element.data.data(), static_cast<int>(element.data.size()));
417 case WebHTTPBody::Element::TypeFile:
418 if (element.fileLength == -1) {
419 request_body->AppendFileRange(
420 base::FilePath::FromUTF16Unsafe(element.filePath),
421 0, kuint64max, base::Time());
423 request_body->AppendFileRange(
424 base::FilePath::FromUTF16Unsafe(element.filePath),
425 static_cast<uint64>(element.fileStart),
426 static_cast<uint64>(element.fileLength),
427 base::Time::FromDoubleT(element.modificationTime));
430 case WebHTTPBody::Element::TypeFileSystemURL: {
431 GURL file_system_url = element.fileSystemURL;
432 DCHECK(file_system_url.SchemeIsFileSystem());
433 request_body->AppendFileSystemFileRange(
435 static_cast<uint64>(element.fileStart),
436 static_cast<uint64>(element.fileLength),
437 base::Time::FromDoubleT(element.modificationTime));
440 case WebHTTPBody::Element::TypeBlob:
441 request_body->AppendBlob(element.blobUUID.utf8());
447 request_body->set_identifier(request.httpBody().identifier());
448 bridge_->SetRequestBody(request_body.get());
451 if (sync_load_response) {
452 bridge_->SyncLoad(sync_load_response);
456 if (bridge_->Start(this)) {
457 AddRef(); // Balanced in OnCompletedRequest
463 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) {
465 client_->didSendData(loader_, position, size);
468 bool WebURLLoaderImpl::Context::OnReceivedRedirect(
470 const ResourceResponseInfo& info,
471 bool* has_new_first_party_for_cookies,
472 GURL* new_first_party_for_cookies) {
476 WebURLResponse response;
477 response.initialize();
478 PopulateURLResponse(request_.url(), info, &response);
480 // TODO(darin): We lack sufficient information to construct the actual
481 // request that resulted from the redirect.
482 WebURLRequest new_request(new_url);
483 new_request.setFirstPartyForCookies(request_.firstPartyForCookies());
484 new_request.setDownloadToFile(request_.downloadToFile());
486 WebString referrer_string = WebString::fromUTF8("Referer");
487 WebString referrer = WebSecurityPolicy::generateReferrerHeader(
490 request_.httpHeaderField(referrer_string));
491 if (!referrer.isEmpty())
492 new_request.setHTTPReferrer(referrer, referrer_policy_);
494 std::string method = request_.httpMethod().utf8();
495 std::string new_method = net::URLRequest::ComputeMethodForRedirect(
496 method, response.httpStatusCode());
497 new_request.setHTTPMethod(WebString::fromUTF8(new_method));
498 if (new_method == method)
499 new_request.setHTTPBody(request_.httpBody());
501 client_->willSendRequest(loader_, new_request, response);
502 request_ = new_request;
503 *has_new_first_party_for_cookies = true;
504 *new_first_party_for_cookies = request_.firstPartyForCookies();
506 // Only follow the redirect if WebKit left the URL unmodified.
507 if (new_url == GURL(new_request.url()))
510 // We assume that WebKit only changes the URL to suppress a redirect, and we
511 // assume that it does so by setting it to be invalid.
512 DCHECK(!new_request.url().isValid());
516 void WebURLLoaderImpl::Context::OnReceivedResponse(
517 const ResourceResponseInfo& info) {
521 WebURLResponse response;
522 response.initialize();
523 PopulateURLResponse(request_.url(), info, &response);
525 bool show_raw_listing = (GURL(request_.url()).query() == "raw");
527 if (info.mime_type == "text/vnd.chromium.ftp-dir") {
528 if (show_raw_listing) {
529 // Set the MIME type to plain text to prevent any active content.
530 response.setMIMEType("text/plain");
532 // We're going to produce a parsed listing in HTML.
533 response.setMIMEType("text/html");
537 scoped_refptr<Context> protect(this);
538 client_->didReceiveResponse(loader_, response);
540 // We may have been cancelled after didReceiveResponse, which would leave us
541 // without a client and therefore without much need to do further handling.
545 DCHECK(!ftp_listing_delegate_.get());
546 DCHECK(!multipart_delegate_.get());
547 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") {
548 std::string content_type;
549 info.headers->EnumerateHeader(NULL, "content-type", &content_type);
551 std::string mime_type;
553 bool had_charset = false;
554 std::string boundary;
555 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset,
556 &had_charset, &boundary);
557 base::TrimString(boundary, " \"", &boundary);
559 // If there's no boundary, just handle the request normally. In the gecko
560 // code, nsMultiMixedConv::OnStartRequest throws an exception.
561 if (!boundary.empty()) {
562 multipart_delegate_.reset(
563 new MultipartResponseDelegate(client_, loader_, response, boundary));
565 } else if (info.mime_type == "text/vnd.chromium.ftp-dir" &&
567 ftp_listing_delegate_.reset(
568 new FtpDirectoryListingResponseDelegate(client_, loader_, response));
572 void WebURLLoaderImpl::Context::OnDownloadedData(int len,
573 int encoded_data_length) {
575 client_->didDownloadData(loader_, len, encoded_data_length);
578 void WebURLLoaderImpl::Context::OnReceivedData(const char* data,
580 int encoded_data_length) {
584 if (ftp_listing_delegate_) {
585 // The FTP listing delegate will make the appropriate calls to
586 // client_->didReceiveData and client_->didReceiveResponse.
587 ftp_listing_delegate_->OnReceivedData(data, data_length);
588 } else if (multipart_delegate_) {
589 // The multipart delegate will make the appropriate calls to
590 // client_->didReceiveData and client_->didReceiveResponse.
591 multipart_delegate_->OnReceivedData(data, data_length, encoded_data_length);
593 client_->didReceiveData(loader_, data, data_length, encoded_data_length);
597 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
598 const char* data, int len) {
600 client_->didReceiveCachedMetadata(loader_, data, len);
603 void WebURLLoaderImpl::Context::OnCompletedRequest(
605 bool was_ignored_by_handler,
606 bool stale_copy_in_cache,
607 const std::string& security_info,
608 const base::TimeTicks& completion_time,
609 int64 total_transfer_size) {
610 if (ftp_listing_delegate_) {
611 ftp_listing_delegate_->OnCompletedRequest();
612 ftp_listing_delegate_.reset(NULL);
613 } else if (multipart_delegate_) {
614 multipart_delegate_->OnCompletedRequest();
615 multipart_delegate_.reset(NULL);
618 // Prevent any further IPC to the browser now that we're complete, but
619 // don't delete it to keep any downloaded temp files alive.
620 DCHECK(!completed_bridge_.get());
621 completed_bridge_.swap(bridge_);
624 if (error_code != net::OK) {
625 client_->didFail(loader_, CreateError(request_.url(),
629 client_->didFinishLoading(
630 loader_, (completion_time - TimeTicks()).InSecondsF(),
631 total_transfer_size);
635 // We are done with the bridge now, and so we need to release the reference
636 // to ourselves that we took on behalf of the bridge. This may cause our
641 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const {
642 DCHECK(url.SchemeIs("data"));
644 // Optimize for the case where we can handle a data URL locally. We must
645 // skip this for data URLs targetted at frames since those could trigger a
648 // NOTE: We special case MIME types we can render both for performance
649 // reasons as well as to support unit tests, which do not have an underlying
650 // ResourceLoaderBridge implementation.
652 #if defined(OS_ANDROID)
653 // For compatibility reasons on Android we need to expose top-level data://
655 if (request_.targetType() == WebURLRequest::TargetIsMainFrame)
659 if (request_.targetType() != WebURLRequest::TargetIsMainFrame &&
660 request_.targetType() != WebURLRequest::TargetIsSubframe)
663 std::string mime_type, unused_charset;
664 if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) &&
665 net::IsSupportedMimeType(mime_type))
671 void WebURLLoaderImpl::Context::HandleDataURL() {
672 ResourceResponseInfo info;
676 if (GetInfoFromDataURL(request_.url(), &info, &data, &error_code)) {
677 OnReceivedResponse(info);
679 OnReceivedData(data.data(), data.size(), 0);
682 OnCompletedRequest(error_code, false, false, info.security_info,
683 base::TimeTicks::Now(), 0);
686 // WebURLLoaderImpl -----------------------------------------------------------
688 WebURLLoaderImpl::WebURLLoaderImpl(WebKitPlatformSupportImpl* platform)
689 : context_(new Context(this)),
690 platform_(platform) {
693 WebURLLoaderImpl::~WebURLLoaderImpl() {
697 WebURLError WebURLLoaderImpl::CreateError(const WebURL& unreachable_url,
698 bool stale_copy_in_cache,
701 error.domain = WebString::fromUTF8(net::kErrorDomain);
702 error.reason = reason;
703 error.unreachableURL = unreachable_url;
704 error.staleCopyInCache = stale_copy_in_cache;
705 if (reason == net::ERR_ABORTED) {
706 error.isCancellation = true;
707 } else if (reason == net::ERR_TEMPORARILY_THROTTLED) {
708 error.localizedDescription = WebString::fromUTF8(
709 kThrottledErrorDescription);
711 error.localizedDescription = WebString::fromUTF8(
712 net::ErrorToString(reason));
717 void WebURLLoaderImpl::PopulateURLResponse(const GURL& url,
718 const ResourceResponseInfo& info,
719 WebURLResponse* response) {
720 response->setURL(url);
721 response->setResponseTime(info.response_time.ToDoubleT());
722 response->setMIMEType(WebString::fromUTF8(info.mime_type));
723 response->setTextEncodingName(WebString::fromUTF8(info.charset));
724 response->setExpectedContentLength(info.content_length);
725 response->setSecurityInfo(info.security_info);
726 response->setAppCacheID(info.appcache_id);
727 response->setAppCacheManifestURL(info.appcache_manifest_url);
728 response->setWasCached(!info.load_timing.request_start_time.is_null() &&
729 info.response_time < info.load_timing.request_start_time);
730 response->setRemoteIPAddress(
731 WebString::fromUTF8(info.socket_address.host()));
732 response->setRemotePort(info.socket_address.port());
733 response->setConnectionID(info.load_timing.socket_log_id);
734 response->setConnectionReused(info.load_timing.socket_reused);
735 response->setDownloadFilePath(info.download_file_path.AsUTF16Unsafe());
736 WebURLResponseExtraDataImpl* extra_data =
737 new WebURLResponseExtraDataImpl(info.npn_negotiated_protocol);
738 response->setExtraData(extra_data);
739 extra_data->set_was_fetched_via_spdy(info.was_fetched_via_spdy);
740 extra_data->set_was_npn_negotiated(info.was_npn_negotiated);
741 extra_data->set_was_alternate_protocol_available(
742 info.was_alternate_protocol_available);
743 extra_data->set_connection_info(info.connection_info);
744 extra_data->set_was_fetched_via_proxy(info.was_fetched_via_proxy);
746 // If there's no received headers end time, don't set load timing. This is
747 // the case for non-HTTP requests, requests that don't go over the wire, and
748 // certain error cases.
749 if (!info.load_timing.receive_headers_end.is_null()) {
750 WebURLLoadTiming timing;
751 PopulateURLLoadTiming(info.load_timing, &timing);
752 response->setLoadTiming(timing);
755 if (info.devtools_info.get()) {
756 WebHTTPLoadInfo load_info;
758 load_info.setHTTPStatusCode(info.devtools_info->http_status_code);
759 load_info.setHTTPStatusText(WebString::fromLatin1(
760 info.devtools_info->http_status_text));
761 load_info.setEncodedDataLength(info.encoded_data_length);
763 load_info.setRequestHeadersText(WebString::fromLatin1(
764 info.devtools_info->request_headers_text));
765 load_info.setResponseHeadersText(WebString::fromLatin1(
766 info.devtools_info->response_headers_text));
767 const HeadersVector& request_headers = info.devtools_info->request_headers;
768 for (HeadersVector::const_iterator it = request_headers.begin();
769 it != request_headers.end(); ++it) {
770 load_info.addRequestHeader(WebString::fromLatin1(it->first),
771 WebString::fromLatin1(it->second));
773 const HeadersVector& response_headers =
774 info.devtools_info->response_headers;
775 for (HeadersVector::const_iterator it = response_headers.begin();
776 it != response_headers.end(); ++it) {
777 load_info.addResponseHeader(WebString::fromLatin1(it->first),
778 WebString::fromLatin1(it->second));
780 response->setHTTPLoadInfo(load_info);
783 const net::HttpResponseHeaders* headers = info.headers.get();
787 WebURLResponse::HTTPVersion version = WebURLResponse::Unknown;
788 if (headers->GetHttpVersion() == net::HttpVersion(0, 9))
789 version = WebURLResponse::HTTP_0_9;
790 else if (headers->GetHttpVersion() == net::HttpVersion(1, 0))
791 version = WebURLResponse::HTTP_1_0;
792 else if (headers->GetHttpVersion() == net::HttpVersion(1, 1))
793 version = WebURLResponse::HTTP_1_1;
794 response->setHTTPVersion(version);
795 response->setHTTPStatusCode(headers->response_code());
796 response->setHTTPStatusText(WebString::fromLatin1(headers->GetStatusText()));
798 // TODO(darin): We should leverage HttpResponseHeaders for this, and this
799 // should be using the same code as ResourceDispatcherHost.
800 // TODO(jungshik): Figure out the actual value of the referrer charset and
801 // pass it to GetSuggestedFilename.
803 headers->EnumerateHeader(NULL, "content-disposition", &value);
804 response->setSuggestedFileName(
805 net::GetSuggestedFilename(url,
807 std::string(), // referrer_charset
808 std::string(), // suggested_name
809 std::string(), // mime_type
810 std::string())); // default_name
813 if (headers->GetLastModifiedValue(&time_val))
814 response->setLastModifiedDate(time_val.ToDoubleT());
816 // Build up the header map.
819 while (headers->EnumerateHeaderLines(&iter, &name, &value)) {
820 response->addHTTPHeaderField(WebString::fromLatin1(name),
821 WebString::fromLatin1(value));
825 void WebURLLoaderImpl::loadSynchronously(const WebURLRequest& request,
826 WebURLResponse& response,
829 ResourceLoaderBridge::SyncLoadResponse sync_load_response;
830 context_->Start(request, &sync_load_response, platform_);
832 const GURL& final_url = sync_load_response.url;
834 // TODO(tc): For file loads, we may want to include a more descriptive
835 // status code or status text.
836 int error_code = sync_load_response.error_code;
837 if (error_code != net::OK) {
838 response.setURL(final_url);
839 error.domain = WebString::fromUTF8(net::kErrorDomain);
840 error.reason = error_code;
841 error.unreachableURL = final_url;
845 PopulateURLResponse(final_url, sync_load_response, &response);
847 data.assign(sync_load_response.data.data(),
848 sync_load_response.data.size());
851 void WebURLLoaderImpl::loadAsynchronously(const WebURLRequest& request,
852 WebURLLoaderClient* client) {
853 DCHECK(!context_->client());
855 context_->set_client(client);
856 context_->Start(request, NULL, platform_);
859 void WebURLLoaderImpl::cancel() {
863 void WebURLLoaderImpl::setDefersLoading(bool value) {
864 context_->SetDefersLoading(value);
867 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority) {
868 context_->DidChangePriority(new_priority);
871 } // namespace webkit_glue