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 #include "net/url_request/url_request.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/debug/stack_trace.h"
12 #include "base/lazy_instance.h"
13 #include "base/memory/singleton.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/profiler/scoped_tracker.h"
16 #include "base/stl_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/synchronization/lock.h"
19 #include "base/values.h"
20 #include "net/base/auth.h"
21 #include "net/base/chunked_upload_data_stream.h"
22 #include "net/base/host_port_pair.h"
23 #include "net/base/load_flags.h"
24 #include "net/base/load_timing_info.h"
25 #include "net/base/net_errors.h"
26 #include "net/base/net_log.h"
27 #include "net/base/network_change_notifier.h"
28 #include "net/base/network_delegate.h"
29 #include "net/base/upload_data_stream.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_util.h"
32 #include "net/ssl/ssl_cert_request_info.h"
33 #include "net/url_request/redirect_info.h"
34 #include "net/url_request/url_request_context.h"
35 #include "net/url_request/url_request_error_job.h"
36 #include "net/url_request/url_request_job.h"
37 #include "net/url_request/url_request_job_manager.h"
38 #include "net/url_request/url_request_netlog_params.h"
39 #include "net/url_request/url_request_redirect_job.h"
48 // Max number of http redirects to follow. Same number as gecko.
49 const int kMaxRedirects
= 20;
51 // Discard headers which have meaning in POST (Content-Length, Content-Type,
53 void StripPostSpecificHeaders(HttpRequestHeaders
* headers
) {
54 // These are headers that may be attached to a POST.
55 headers
->RemoveHeader(HttpRequestHeaders::kContentLength
);
56 headers
->RemoveHeader(HttpRequestHeaders::kContentType
);
57 headers
->RemoveHeader(HttpRequestHeaders::kOrigin
);
60 // TODO(battre): Delete this, see http://crbug.com/89321:
61 // This counter keeps track of the identifiers used for URL requests so far.
62 // 0 is reserved to represent an invalid ID.
63 uint64 g_next_url_request_identifier
= 1;
65 // This lock protects g_next_url_request_identifier.
66 base::LazyInstance
<base::Lock
>::Leaky
67 g_next_url_request_identifier_lock
= LAZY_INSTANCE_INITIALIZER
;
69 // Returns an prior unused identifier for URL requests.
70 uint64
GenerateURLRequestIdentifier() {
71 base::AutoLock
lock(g_next_url_request_identifier_lock
.Get());
72 return g_next_url_request_identifier
++;
75 // True once the first URLRequest was started.
76 bool g_url_requests_started
= false;
78 // True if cookies are accepted by default.
79 bool g_default_can_use_cookies
= true;
81 // When the URLRequest first assempts load timing information, it has the times
82 // at which each event occurred. The API requires the time which the request
83 // was blocked on each phase. This function handles the conversion.
85 // In the case of reusing a SPDY session, old proxy results may have been
86 // reused, so proxy resolution times may be before the request was started.
88 // Due to preconnect and late binding, it is also possible for the connection
89 // attempt to start before a request has been started, or proxy resolution
92 // This functions fixes both those cases.
93 void ConvertRealLoadTimesToBlockingTimes(
94 net::LoadTimingInfo
* load_timing_info
) {
95 DCHECK(!load_timing_info
->request_start
.is_null());
97 // Earliest time possible for the request to be blocking on connect events.
98 base::TimeTicks block_on_connect
= load_timing_info
->request_start
;
100 if (!load_timing_info
->proxy_resolve_start
.is_null()) {
101 DCHECK(!load_timing_info
->proxy_resolve_end
.is_null());
103 // Make sure the proxy times are after request start.
104 if (load_timing_info
->proxy_resolve_start
< load_timing_info
->request_start
)
105 load_timing_info
->proxy_resolve_start
= load_timing_info
->request_start
;
106 if (load_timing_info
->proxy_resolve_end
< load_timing_info
->request_start
)
107 load_timing_info
->proxy_resolve_end
= load_timing_info
->request_start
;
109 // Connect times must also be after the proxy times.
110 block_on_connect
= load_timing_info
->proxy_resolve_end
;
113 // Make sure connection times are after start and proxy times.
115 net::LoadTimingInfo::ConnectTiming
* connect_timing
=
116 &load_timing_info
->connect_timing
;
117 if (!connect_timing
->dns_start
.is_null()) {
118 DCHECK(!connect_timing
->dns_end
.is_null());
119 if (connect_timing
->dns_start
< block_on_connect
)
120 connect_timing
->dns_start
= block_on_connect
;
121 if (connect_timing
->dns_end
< block_on_connect
)
122 connect_timing
->dns_end
= block_on_connect
;
125 if (!connect_timing
->connect_start
.is_null()) {
126 DCHECK(!connect_timing
->connect_end
.is_null());
127 if (connect_timing
->connect_start
< block_on_connect
)
128 connect_timing
->connect_start
= block_on_connect
;
129 if (connect_timing
->connect_end
< block_on_connect
)
130 connect_timing
->connect_end
= block_on_connect
;
133 if (!connect_timing
->ssl_start
.is_null()) {
134 DCHECK(!connect_timing
->ssl_end
.is_null());
135 if (connect_timing
->ssl_start
< block_on_connect
)
136 connect_timing
->ssl_start
= block_on_connect
;
137 if (connect_timing
->ssl_end
< block_on_connect
)
138 connect_timing
->ssl_end
= block_on_connect
;
144 ///////////////////////////////////////////////////////////////////////////////
145 // URLRequest::Delegate
147 void URLRequest::Delegate::OnReceivedRedirect(URLRequest
* request
,
148 const RedirectInfo
& redirect_info
,
149 bool* defer_redirect
) {
152 void URLRequest::Delegate::OnAuthRequired(URLRequest
* request
,
153 AuthChallengeInfo
* auth_info
) {
154 request
->CancelAuth();
157 void URLRequest::Delegate::OnCertificateRequested(
159 SSLCertRequestInfo
* cert_request_info
) {
163 void URLRequest::Delegate::OnSSLCertificateError(URLRequest
* request
,
164 const SSLInfo
& ssl_info
,
169 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest
* request
,
173 ///////////////////////////////////////////////////////////////////////////////
176 URLRequest::~URLRequest() {
179 if (network_delegate_
) {
180 network_delegate_
->NotifyURLRequestDestroyed(this);
182 job_
->NotifyURLRequestDestroyed();
188 int deleted
= context_
->url_requests()->erase(this);
189 CHECK_EQ(1, deleted
);
192 // Log error only on failure, not cancellation, as even successful requests
193 // are "cancelled" on destruction.
194 if (status_
.status() == URLRequestStatus::FAILED
)
195 net_error
= status_
.error();
196 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE
, net_error
);
199 void URLRequest::EnableChunkedUpload() {
200 DCHECK(!upload_data_stream_
|| upload_data_stream_
->is_chunked());
201 if (!upload_data_stream_
) {
202 upload_chunked_data_stream_
= new ChunkedUploadDataStream(0);
203 upload_data_stream_
.reset(upload_chunked_data_stream_
);
207 void URLRequest::AppendChunkToUpload(const char* bytes
,
209 bool is_last_chunk
) {
210 DCHECK(upload_data_stream_
);
211 DCHECK(upload_data_stream_
->is_chunked());
212 upload_chunked_data_stream_
->AppendData(bytes
, bytes_len
, is_last_chunk
);
215 void URLRequest::set_upload(scoped_ptr
<UploadDataStream
> upload
) {
216 DCHECK(!upload
->is_chunked());
217 upload_data_stream_
= upload
.Pass();
220 const UploadDataStream
* URLRequest::get_upload() const {
221 return upload_data_stream_
.get();
224 bool URLRequest::has_upload() const {
225 return upload_data_stream_
.get() != NULL
;
228 void URLRequest::SetExtraRequestHeaderByName(const string
& name
,
231 DCHECK(!is_pending_
|| is_redirecting_
);
233 extra_request_headers_
.SetHeader(name
, value
);
235 extra_request_headers_
.SetHeaderIfMissing(name
, value
);
239 void URLRequest::RemoveRequestHeaderByName(const string
& name
) {
240 DCHECK(!is_pending_
|| is_redirecting_
);
241 extra_request_headers_
.RemoveHeader(name
);
244 void URLRequest::SetExtraRequestHeaders(
245 const HttpRequestHeaders
& headers
) {
246 DCHECK(!is_pending_
);
247 extra_request_headers_
= headers
;
249 // NOTE: This method will likely become non-trivial once the other setters
250 // for request headers are implemented.
253 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders
* headers
) const {
257 return job_
->GetFullRequestHeaders(headers
);
260 int64
URLRequest::GetTotalReceivedBytes() const {
264 return job_
->GetTotalReceivedBytes();
267 LoadStateWithParam
URLRequest::GetLoadState() const {
268 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
269 // delegate before it has been started.
270 if (calling_delegate_
|| !blocked_by_
.empty()) {
271 return LoadStateWithParam(
272 LOAD_STATE_WAITING_FOR_DELEGATE
,
273 use_blocked_by_as_load_param_
? base::UTF8ToUTF16(blocked_by_
) :
276 return LoadStateWithParam(job_
.get() ? job_
->GetLoadState() : LOAD_STATE_IDLE
,
280 base::Value
* URLRequest::GetStateAsValue() const {
281 base::DictionaryValue
* dict
= new base::DictionaryValue();
282 dict
->SetString("url", original_url().possibly_invalid_spec());
284 if (url_chain_
.size() > 1) {
285 base::ListValue
* list
= new base::ListValue();
286 for (std::vector
<GURL
>::const_iterator url
= url_chain_
.begin();
287 url
!= url_chain_
.end(); ++url
) {
288 list
->AppendString(url
->possibly_invalid_spec());
290 dict
->Set("url_chain", list
);
293 dict
->SetInteger("load_flags", load_flags_
);
295 LoadStateWithParam load_state
= GetLoadState();
296 dict
->SetInteger("load_state", load_state
.state
);
297 if (!load_state
.param
.empty())
298 dict
->SetString("load_state_param", load_state
.param
);
299 if (!blocked_by_
.empty())
300 dict
->SetString("delegate_info", blocked_by_
);
302 dict
->SetString("method", method_
);
303 dict
->SetBoolean("has_upload", has_upload());
304 dict
->SetBoolean("is_pending", is_pending_
);
306 // Add the status of the request. The status should always be IO_PENDING, and
307 // the error should always be OK, unless something is holding onto a request
308 // that has finished or a request was leaked. Neither of these should happen.
309 switch (status_
.status()) {
310 case URLRequestStatus::SUCCESS
:
311 dict
->SetString("status", "SUCCESS");
313 case URLRequestStatus::IO_PENDING
:
314 dict
->SetString("status", "IO_PENDING");
316 case URLRequestStatus::CANCELED
:
317 dict
->SetString("status", "CANCELED");
319 case URLRequestStatus::FAILED
:
320 dict
->SetString("status", "FAILED");
323 if (status_
.error() != OK
)
324 dict
->SetInteger("net_error", status_
.error());
328 void URLRequest::LogBlockedBy(const char* blocked_by
) {
330 DCHECK_GT(strlen(blocked_by
), 0u);
332 // Only log information to NetLog during startup and certain deferring calls
333 // to delegates. For all reads but the first, do nothing.
334 if (!calling_delegate_
&& !response_info_
.request_time
.is_null())
338 blocked_by_
= blocked_by
;
339 use_blocked_by_as_load_param_
= false;
342 NetLog::TYPE_DELEGATE_INFO
,
343 NetLog::StringCallback("delegate_info", &blocked_by_
));
346 void URLRequest::LogAndReportBlockedBy(const char* source
) {
347 LogBlockedBy(source
);
348 use_blocked_by_as_load_param_
= true;
351 void URLRequest::LogUnblocked() {
352 if (blocked_by_
.empty())
355 net_log_
.EndEvent(NetLog::TYPE_DELEGATE_INFO
);
359 UploadProgress
URLRequest::GetUploadProgress() const {
361 // We haven't started or the request was cancelled
362 return UploadProgress();
364 if (final_upload_progress_
.position()) {
365 // The first job completed and none of the subsequent series of
366 // GETs when following redirects will upload anything, so we return the
367 // cached results from the initial job, the POST.
368 return final_upload_progress_
;
370 return job_
->GetUploadProgress();
373 void URLRequest::GetResponseHeaderByName(const string
& name
, string
* value
) {
375 if (response_info_
.headers
.get()) {
376 response_info_
.headers
->GetNormalizedHeader(name
, value
);
382 HostPortPair
URLRequest::GetSocketAddress() const {
384 return job_
->GetSocketAddress();
387 HttpResponseHeaders
* URLRequest::response_headers() const {
388 return response_info_
.headers
.get();
391 void URLRequest::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
392 *load_timing_info
= load_timing_info_
;
395 bool URLRequest::GetResponseCookies(ResponseCookies
* cookies
) {
397 return job_
->GetResponseCookies(cookies
);
400 void URLRequest::GetMimeType(string
* mime_type
) const {
402 job_
->GetMimeType(mime_type
);
405 void URLRequest::GetCharset(string
* charset
) const {
407 job_
->GetCharset(charset
);
410 int URLRequest::GetResponseCode() const {
412 return job_
->GetResponseCode();
415 void URLRequest::SetLoadFlags(int flags
) {
416 if ((load_flags_
& LOAD_IGNORE_LIMITS
) != (flags
& LOAD_IGNORE_LIMITS
)) {
418 DCHECK(flags
& LOAD_IGNORE_LIMITS
);
419 DCHECK_EQ(priority_
, MAXIMUM_PRIORITY
);
423 // This should be a no-op given the above DCHECKs, but do this
424 // anyway for release mode.
425 if ((load_flags_
& LOAD_IGNORE_LIMITS
) != 0)
426 SetPriority(MAXIMUM_PRIORITY
);
430 void URLRequest::SetDefaultCookiePolicyToBlock() {
431 CHECK(!g_url_requests_started
);
432 g_default_can_use_cookies
= false;
436 bool URLRequest::IsHandledProtocol(const std::string
& scheme
) {
437 return URLRequestJobManager::SupportsScheme(scheme
);
441 bool URLRequest::IsHandledURL(const GURL
& url
) {
442 if (!url
.is_valid()) {
443 // We handle error cases.
447 return IsHandledProtocol(url
.scheme());
450 void URLRequest::set_first_party_for_cookies(
451 const GURL
& first_party_for_cookies
) {
452 DCHECK(!is_pending_
);
453 first_party_for_cookies_
= first_party_for_cookies
;
456 void URLRequest::set_first_party_url_policy(
457 FirstPartyURLPolicy first_party_url_policy
) {
458 DCHECK(!is_pending_
);
459 first_party_url_policy_
= first_party_url_policy
;
462 void URLRequest::set_method(const std::string
& method
) {
463 DCHECK(!is_pending_
);
467 void URLRequest::SetReferrer(const std::string
& referrer
) {
468 DCHECK(!is_pending_
);
469 GURL
referrer_url(referrer
);
470 if (referrer_url
.is_valid()) {
471 referrer_
= referrer_url
.GetAsReferrer().spec();
473 referrer_
= referrer
;
477 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy
) {
478 DCHECK(!is_pending_
);
479 referrer_policy_
= referrer_policy
;
482 void URLRequest::set_delegate(Delegate
* delegate
) {
483 delegate_
= delegate
;
486 void URLRequest::Start() {
487 // Some values can be NULL, but the job factory must not be.
488 DCHECK(context_
->job_factory());
490 // Anything that sets |blocked_by_| before start should have cleaned up after
492 DCHECK(blocked_by_
.empty());
494 g_url_requests_started
= true;
495 response_info_
.request_time
= base::Time::Now();
497 load_timing_info_
= LoadTimingInfo();
498 load_timing_info_
.request_start_time
= response_info_
.request_time
;
499 load_timing_info_
.request_start
= base::TimeTicks::Now();
501 // Only notify the delegate for the initial request.
502 if (network_delegate_
) {
504 int error
= network_delegate_
->NotifyBeforeURLRequest(
505 this, before_request_callback_
, &delegate_redirect_url_
);
506 // If ERR_IO_PENDING is returned, the delegate will invoke
507 // |before_request_callback_| later.
508 if (error
!= ERR_IO_PENDING
)
509 BeforeRequestComplete(error
);
513 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
514 this, network_delegate_
));
517 ///////////////////////////////////////////////////////////////////////////////
519 URLRequest::URLRequest(const GURL
& url
,
520 RequestPriority priority
,
522 const URLRequestContext
* context
,
523 CookieStore
* cookie_store
,
524 NetworkDelegate
* network_delegate
)
526 network_delegate_(network_delegate
? network_delegate
527 : context
->network_delegate()),
528 net_log_(BoundNetLog::Make(context
->net_log(),
529 NetLog::SOURCE_URL_REQUEST
)),
532 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
),
533 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL
),
534 load_flags_(LOAD_NORMAL
),
537 is_redirecting_(false),
538 redirect_limit_(kMaxRedirects
),
540 identifier_(GenerateURLRequestIdentifier()),
541 calling_delegate_(false),
542 use_blocked_by_as_load_param_(false),
543 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete
,
544 base::Unretained(this))),
545 has_notified_completion_(false),
546 received_response_content_length_(0),
547 creation_time_(base::TimeTicks::Now()),
548 notified_before_network_start_(false),
549 cookie_store_(cookie_store
? cookie_store
: context
->cookie_store()) {
550 // Sanity check out environment.
551 DCHECK(base::MessageLoop::current())
552 << "The current base::MessageLoop must exist";
554 context
->url_requests()->insert(this);
555 net_log_
.BeginEvent(NetLog::TYPE_REQUEST_ALIVE
);
558 void URLRequest::BeforeRequestComplete(int error
) {
560 DCHECK_NE(ERR_IO_PENDING
, error
);
562 // Check that there are no callbacks to already canceled requests.
563 DCHECK_NE(URLRequestStatus::CANCELED
, status_
.status());
565 OnCallToDelegateComplete();
568 std::string
source("delegate");
569 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
,
570 NetLog::StringCallback("source", &source
));
571 StartJob(new URLRequestErrorJob(this, network_delegate_
, error
));
572 } else if (!delegate_redirect_url_
.is_empty()) {
574 new_url
.Swap(&delegate_redirect_url_
);
576 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
577 this, network_delegate_
, new_url
,
578 // Use status code 307 to preserve the method, so POST requests work.
579 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT
, "Delegate");
582 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
583 this, network_delegate_
));
587 void URLRequest::StartJob(URLRequestJob
* job
) {
588 DCHECK(!is_pending_
);
592 NetLog::TYPE_URL_REQUEST_START_JOB
,
593 base::Bind(&NetLogURLRequestStartCallback
,
594 &url(), &method_
, load_flags_
, priority_
,
595 upload_data_stream_
? upload_data_stream_
->identifier() : -1));
598 job_
->SetExtraRequestHeaders(extra_request_headers_
);
599 job_
->SetPriority(priority_
);
601 if (upload_data_stream_
.get())
602 job_
->SetUpload(upload_data_stream_
.get());
605 is_redirecting_
= false;
607 response_info_
.was_cached
= false;
609 if (GURL(referrer_
) != URLRequestJob::ComputeReferrerForRedirect(
610 referrer_policy_
, referrer_
, url())) {
611 if (!network_delegate_
||
612 !network_delegate_
->CancelURLRequestWithPolicyViolatingReferrerHeader(
613 *this, url(), GURL(referrer_
))) {
616 // We need to clear the referrer anyway to avoid an infinite recursion
617 // when starting the error job.
619 std::string
source("delegate");
620 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
,
621 NetLog::StringCallback("source", &source
));
622 RestartWithJob(new URLRequestErrorJob(
623 this, network_delegate_
, ERR_BLOCKED_BY_CLIENT
));
628 // Don't allow errors to be sent from within Start().
629 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
630 // we probably don't want this: they should be sent asynchronously so
631 // the caller does not get reentered.
635 void URLRequest::Restart() {
636 // Should only be called if the original job didn't make any progress.
637 DCHECK(job_
.get() && !job_
->has_response_started());
639 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_
));
642 void URLRequest::RestartWithJob(URLRequestJob
*job
) {
643 DCHECK(job
->request() == this);
648 void URLRequest::Cancel() {
649 DoCancel(ERR_ABORTED
, SSLInfo());
652 void URLRequest::CancelWithError(int error
) {
653 DoCancel(error
, SSLInfo());
656 void URLRequest::CancelWithSSLError(int error
, const SSLInfo
& ssl_info
) {
657 // This should only be called on a started request.
658 if (!is_pending_
|| !job_
.get() || job_
->has_response_started()) {
662 DoCancel(error
, ssl_info
);
665 void URLRequest::DoCancel(int error
, const SSLInfo
& ssl_info
) {
667 // If cancelled while calling a delegate, clear delegate info.
668 if (calling_delegate_
) {
670 OnCallToDelegateComplete();
673 // If the URL request already has an error status, then canceling is a no-op.
674 // Plus, we don't want to change the error status once it has been set.
675 if (status_
.is_success()) {
676 status_
.set_status(URLRequestStatus::CANCELED
);
677 status_
.set_error(error
);
678 response_info_
.ssl_info
= ssl_info
;
680 // If the request hasn't already been completed, log a cancellation event.
681 if (!has_notified_completion_
) {
682 // Don't log an error code on ERR_ABORTED, since that's redundant.
683 net_log_
.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED
,
684 error
== ERR_ABORTED
? OK
: error
);
688 if (is_pending_
&& job_
.get())
691 // We need to notify about the end of this job here synchronously. The
692 // Job sends an asynchronous notification but by the time this is processed,
693 // our |context_| is NULL.
694 NotifyRequestCompleted();
696 // The Job will call our NotifyDone method asynchronously. This is done so
697 // that the Delegate implementation can call Cancel without having to worry
698 // about being called recursively.
701 bool URLRequest::Read(IOBuffer
* dest
, int dest_size
, int* bytes_read
) {
706 // If this is the first read, end the delegate call that may have started in
707 // OnResponseStarted.
708 OnCallToDelegateComplete();
710 // This handles a cancel that happens while paused.
711 // TODO(ahendrickson): DCHECK() that it is not done after
712 // http://crbug.com/115705 is fixed.
716 if (dest_size
== 0) {
717 // Caller is not too bright. I guess we've done what they asked.
721 // Once the request fails or is cancelled, read will just return 0 bytes
722 // to indicate end of stream.
723 if (!status_
.is_success()) {
727 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
728 tracked_objects::ScopedTracker
tracking_profile1(
729 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read1"));
731 bool rv
= job_
->Read(dest
, dest_size
, bytes_read
);
732 // If rv is false, the status cannot be success.
733 DCHECK(rv
|| status_
.status() != URLRequestStatus::SUCCESS
);
735 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
736 tracked_objects::ScopedTracker
tracking_profile2(
737 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read2"));
739 if (rv
&& *bytes_read
<= 0 && status_
.is_success())
740 NotifyRequestCompleted();
744 void URLRequest::StopCaching() {
749 void URLRequest::NotifyReceivedRedirect(const RedirectInfo
& redirect_info
,
750 bool* defer_redirect
) {
751 is_redirecting_
= true;
753 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
755 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
756 this, network_delegate_
, redirect_info
.new_url
);
759 } else if (delegate_
) {
762 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
763 tracked_objects::ScopedTracker
tracking_profile(
764 FROM_HERE_WITH_EXPLICIT_FUNCTION(
765 "423948 URLRequest::Delegate::OnReceivedRedirect"));
766 delegate_
->OnReceivedRedirect(this, redirect_info
, defer_redirect
);
767 // |this| may be have been destroyed here.
771 void URLRequest::NotifyBeforeNetworkStart(bool* defer
) {
772 if (delegate_
&& !notified_before_network_start_
) {
775 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
777 tracked_objects::ScopedTracker
tracking_profile(
778 FROM_HERE_WITH_EXPLICIT_FUNCTION(
779 "423948 URLRequest::Delegate::OnBeforeNetworkStart"));
780 delegate_
->OnBeforeNetworkStart(this, defer
);
783 OnCallToDelegateComplete();
784 notified_before_network_start_
= true;
788 void URLRequest::ResumeNetworkStart() {
790 DCHECK(notified_before_network_start_
);
792 OnCallToDelegateComplete();
793 job_
->ResumeNetworkStart();
796 void URLRequest::NotifyResponseStarted() {
798 if (!status_
.is_success())
799 net_error
= status_
.error();
800 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB
,
804 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
805 this, network_delegate_
);
810 // In some cases (e.g. an event was canceled), we might have sent the
811 // completion event and receive a NotifyResponseStarted() later.
812 if (!has_notified_completion_
&& status_
.is_success()) {
813 if (network_delegate_
)
814 network_delegate_
->NotifyResponseStarted(this);
817 // Notify in case the entire URL Request has been finished.
818 if (!has_notified_completion_
&& !status_
.is_success())
819 NotifyRequestCompleted();
822 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
824 tracked_objects::ScopedTracker
tracking_profile(
825 FROM_HERE_WITH_EXPLICIT_FUNCTION(
826 "423948 URLRequest::Delegate::OnResponseStarted"));
827 delegate_
->OnResponseStarted(this);
828 // Nothing may appear below this line as OnResponseStarted may delete
834 void URLRequest::FollowDeferredRedirect() {
836 CHECK(status_
.is_success());
838 job_
->FollowDeferredRedirect();
841 void URLRequest::SetAuth(const AuthCredentials
& credentials
) {
843 DCHECK(job_
->NeedsAuth());
845 job_
->SetAuth(credentials
);
848 void URLRequest::CancelAuth() {
850 DCHECK(job_
->NeedsAuth());
855 void URLRequest::ContinueWithCertificate(X509Certificate
* client_cert
) {
858 job_
->ContinueWithCertificate(client_cert
);
861 void URLRequest::ContinueDespiteLastError() {
864 job_
->ContinueDespiteLastError();
867 void URLRequest::PrepareToRestart() {
870 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
872 net_log_
.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB
);
876 response_info_
= HttpResponseInfo();
877 response_info_
.request_time
= base::Time::Now();
879 load_timing_info_
= LoadTimingInfo();
880 load_timing_info_
.request_start_time
= response_info_
.request_time
;
881 load_timing_info_
.request_start
= base::TimeTicks::Now();
883 status_
= URLRequestStatus();
887 void URLRequest::OrphanJob() {
888 // When calling this function, please check that URLRequestHttpJob is
889 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
890 // the call back. This is currently guaranteed by the following strategies:
891 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
892 // be receiving any headers at that time.
893 // - OrphanJob is called in ~URLRequest, in this case
894 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
895 // that the callback becomes invalid.
897 job_
->DetachRequest(); // ensures that the job will not call us again
901 int URLRequest::Redirect(const RedirectInfo
& redirect_info
) {
902 // Matches call in NotifyReceivedRedirect.
903 OnCallToDelegateComplete();
904 if (net_log_
.IsLogging()) {
906 NetLog::TYPE_URL_REQUEST_REDIRECTED
,
907 NetLog::StringCallback("location",
908 &redirect_info
.new_url
.possibly_invalid_spec()));
911 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
912 if (network_delegate_
)
913 network_delegate_
->NotifyBeforeRedirect(this, redirect_info
.new_url
);
915 if (redirect_limit_
<= 0) {
916 DVLOG(1) << "disallowing redirect: exceeds limit";
917 return ERR_TOO_MANY_REDIRECTS
;
920 if (!redirect_info
.new_url
.is_valid())
921 return ERR_INVALID_URL
;
923 if (!job_
->IsSafeRedirect(redirect_info
.new_url
)) {
924 DVLOG(1) << "disallowing redirect: unsafe protocol";
925 return ERR_UNSAFE_REDIRECT
;
928 if (!final_upload_progress_
.position())
929 final_upload_progress_
= job_
->GetUploadProgress();
932 if (redirect_info
.new_method
!= method_
) {
933 // TODO(davidben): This logic still needs to be replicated at the consumers.
934 if (method_
== "POST") {
935 // If being switched from POST, must remove headers that were specific to
936 // the POST and don't have meaning in other methods. For example the
937 // inclusion of a multipart Content-Type header in GET can cause problems
938 // with some servers:
939 // http://code.google.com/p/chromium/issues/detail?id=843
940 StripPostSpecificHeaders(&extra_request_headers_
);
942 upload_data_stream_
.reset();
943 method_
= redirect_info
.new_method
;
946 referrer_
= redirect_info
.new_referrer
;
947 first_party_for_cookies_
= redirect_info
.new_first_party_for_cookies
;
949 url_chain_
.push_back(redirect_info
.new_url
);
956 const URLRequestContext
* URLRequest::context() const {
960 int64
URLRequest::GetExpectedContentSize() const {
961 int64 expected_content_size
= -1;
963 expected_content_size
= job_
->expected_content_size();
965 return expected_content_size
;
968 void URLRequest::SetPriority(RequestPriority priority
) {
969 DCHECK_GE(priority
, MINIMUM_PRIORITY
);
970 DCHECK_LE(priority
, MAXIMUM_PRIORITY
);
972 if ((load_flags_
& LOAD_IGNORE_LIMITS
) && (priority
!= MAXIMUM_PRIORITY
)) {
974 // Maintain the invariant that requests with IGNORE_LIMITS set
975 // have MAXIMUM_PRIORITY for release mode.
979 if (priority_
== priority
)
982 priority_
= priority
;
984 net_log_
.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY
,
985 NetLog::IntegerCallback("priority", priority_
));
986 job_
->SetPriority(priority_
);
990 bool URLRequest::GetHSTSRedirect(GURL
* redirect_url
) const {
991 const GURL
& url
= this->url();
992 bool scheme_is_http
= url
.SchemeIs("http");
993 if (!scheme_is_http
&& !url
.SchemeIs("ws"))
995 TransportSecurityState
* state
= context()->transport_security_state();
996 if (state
&& state
->ShouldUpgradeToSSL(url
.host())) {
997 GURL::Replacements replacements
;
998 const char* new_scheme
= scheme_is_http
? "https" : "wss";
999 replacements
.SetSchemeStr(new_scheme
);
1000 *redirect_url
= url
.ReplaceComponents(replacements
);
1006 void URLRequest::NotifyAuthRequired(AuthChallengeInfo
* auth_info
) {
1007 NetworkDelegate::AuthRequiredResponse rv
=
1008 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
1009 auth_info_
= auth_info
;
1010 if (network_delegate_
) {
1012 rv
= network_delegate_
->NotifyAuthRequired(
1015 base::Bind(&URLRequest::NotifyAuthRequiredComplete
,
1016 base::Unretained(this)),
1017 &auth_credentials_
);
1018 if (rv
== NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING
)
1022 NotifyAuthRequiredComplete(rv
);
1025 void URLRequest::NotifyAuthRequiredComplete(
1026 NetworkDelegate::AuthRequiredResponse result
) {
1027 OnCallToDelegateComplete();
1029 // Check that there are no callbacks to already canceled requests.
1030 DCHECK_NE(URLRequestStatus::CANCELED
, status_
.status());
1032 // NotifyAuthRequired may be called multiple times, such as
1033 // when an authentication attempt fails. Clear out the data
1034 // so it can be reset on another round.
1035 AuthCredentials credentials
= auth_credentials_
;
1036 auth_credentials_
= AuthCredentials();
1037 scoped_refptr
<AuthChallengeInfo
> auth_info
;
1038 auth_info
.swap(auth_info_
);
1041 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
:
1042 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1043 // didn't take an action.
1045 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
1047 tracked_objects::ScopedTracker
tracking_profile(
1048 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1049 "423948 URLRequest::Delegate::OnAuthRequired"));
1050 delegate_
->OnAuthRequired(this, auth_info
.get());
1054 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
:
1055 SetAuth(credentials
);
1058 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
:
1062 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING
:
1068 void URLRequest::NotifyCertificateRequested(
1069 SSLCertRequestInfo
* cert_request_info
) {
1071 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1072 tracked_objects::ScopedTracker
tracking_profile(
1073 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1074 "423948 URLRequest::Delegate::OnCertificateRequested"));
1075 delegate_
->OnCertificateRequested(this, cert_request_info
);
1079 void URLRequest::NotifySSLCertificateError(const SSLInfo
& ssl_info
,
1082 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1083 tracked_objects::ScopedTracker
tracking_profile(
1084 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1085 "423948 URLRequest::Delegate::OnSSLCertificateError"));
1086 delegate_
->OnSSLCertificateError(this, ssl_info
, fatal
);
1090 bool URLRequest::CanGetCookies(const CookieList
& cookie_list
) const {
1091 DCHECK(!(load_flags_
& LOAD_DO_NOT_SEND_COOKIES
));
1092 if (network_delegate_
) {
1093 return network_delegate_
->CanGetCookies(*this, cookie_list
);
1095 return g_default_can_use_cookies
;
1098 bool URLRequest::CanSetCookie(const std::string
& cookie_line
,
1099 CookieOptions
* options
) const {
1100 DCHECK(!(load_flags_
& LOAD_DO_NOT_SAVE_COOKIES
));
1101 if (network_delegate_
) {
1102 return network_delegate_
->CanSetCookie(*this, cookie_line
, options
);
1104 return g_default_can_use_cookies
;
1107 bool URLRequest::CanEnablePrivacyMode() const {
1108 if (network_delegate_
) {
1109 return network_delegate_
->CanEnablePrivacyMode(url(),
1110 first_party_for_cookies_
);
1112 return !g_default_can_use_cookies
;
1116 void URLRequest::NotifyReadCompleted(int bytes_read
) {
1117 // Notify in case the entire URL Request has been finished.
1118 if (bytes_read
<= 0)
1119 NotifyRequestCompleted();
1121 // Notify NetworkChangeNotifier that we just received network data.
1122 // This is to identify cases where the NetworkChangeNotifier thinks we
1123 // are off-line but we are still receiving network data (crbug.com/124069),
1124 // and to get rough network connection measurements.
1125 if (bytes_read
> 0 && !was_cached())
1126 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read
);
1129 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1130 tracked_objects::ScopedTracker
tracking_profile(
1131 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1132 "423948 URLRequest::Delegate::OnReadCompleted"));
1133 delegate_
->OnReadCompleted(this, bytes_read
);
1136 // Nothing below this line as OnReadCompleted may delete |this|.
1139 void URLRequest::OnHeadersComplete() {
1140 // Cache load timing information now, as information will be lost once the
1141 // socket is closed and the ClientSocketHandle is Reset, which will happen
1142 // once the body is complete. The start times should already be populated.
1144 // Keep a copy of the two times the URLRequest sets.
1145 base::TimeTicks request_start
= load_timing_info_
.request_start
;
1146 base::Time request_start_time
= load_timing_info_
.request_start_time
;
1148 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1149 // consistent place to start from.
1150 load_timing_info_
= LoadTimingInfo();
1151 job_
->GetLoadTimingInfo(&load_timing_info_
);
1153 load_timing_info_
.request_start
= request_start
;
1154 load_timing_info_
.request_start_time
= request_start_time
;
1156 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_
);
1160 void URLRequest::NotifyRequestCompleted() {
1161 // TODO(battre): Get rid of this check, according to willchan it should
1163 if (has_notified_completion_
)
1166 is_pending_
= false;
1167 is_redirecting_
= false;
1168 has_notified_completion_
= true;
1169 if (network_delegate_
)
1170 network_delegate_
->NotifyCompleted(this, job_
.get() != NULL
);
1173 void URLRequest::OnCallToDelegate() {
1174 DCHECK(!calling_delegate_
);
1175 DCHECK(blocked_by_
.empty());
1176 calling_delegate_
= true;
1177 net_log_
.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE
);
1180 void URLRequest::OnCallToDelegateComplete() {
1181 // This should have been cleared before resuming the request.
1182 DCHECK(blocked_by_
.empty());
1183 if (!calling_delegate_
)
1185 calling_delegate_
= false;
1186 net_log_
.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE
);
1189 void URLRequest::set_stack_trace(const base::debug::StackTrace
& stack_trace
) {
1190 base::debug::StackTrace
* stack_trace_copy
=
1191 new base::debug::StackTrace(NULL
, 0);
1192 *stack_trace_copy
= stack_trace
;
1193 stack_trace_
.reset(stack_trace_copy
);
1196 const base::debug::StackTrace
* URLRequest::stack_trace() const {
1197 return stack_trace_
.get();