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/metrics/stats_counters.h"
16 #include "base/profiler/scoped_tracker.h"
17 #include "base/stl_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/synchronization/lock.h"
20 #include "base/values.h"
21 #include "net/base/auth.h"
22 #include "net/base/chunked_upload_data_stream.h"
23 #include "net/base/host_port_pair.h"
24 #include "net/base/load_flags.h"
25 #include "net/base/load_timing_info.h"
26 #include "net/base/net_errors.h"
27 #include "net/base/net_log.h"
28 #include "net/base/network_change_notifier.h"
29 #include "net/base/network_delegate.h"
30 #include "net/base/upload_data_stream.h"
31 #include "net/http/http_response_headers.h"
32 #include "net/http/http_util.h"
33 #include "net/ssl/ssl_cert_request_info.h"
34 #include "net/url_request/redirect_info.h"
35 #include "net/url_request/url_request_context.h"
36 #include "net/url_request/url_request_error_job.h"
37 #include "net/url_request/url_request_job.h"
38 #include "net/url_request/url_request_job_manager.h"
39 #include "net/url_request/url_request_netlog_params.h"
40 #include "net/url_request/url_request_redirect_job.h"
49 // Max number of http redirects to follow. Same number as gecko.
50 const int kMaxRedirects
= 20;
52 // Discard headers which have meaning in POST (Content-Length, Content-Type,
54 void StripPostSpecificHeaders(HttpRequestHeaders
* headers
) {
55 // These are headers that may be attached to a POST.
56 headers
->RemoveHeader(HttpRequestHeaders::kContentLength
);
57 headers
->RemoveHeader(HttpRequestHeaders::kContentType
);
58 headers
->RemoveHeader(HttpRequestHeaders::kOrigin
);
61 // TODO(battre): Delete this, see http://crbug.com/89321:
62 // This counter keeps track of the identifiers used for URL requests so far.
63 // 0 is reserved to represent an invalid ID.
64 uint64 g_next_url_request_identifier
= 1;
66 // This lock protects g_next_url_request_identifier.
67 base::LazyInstance
<base::Lock
>::Leaky
68 g_next_url_request_identifier_lock
= LAZY_INSTANCE_INITIALIZER
;
70 // Returns an prior unused identifier for URL requests.
71 uint64
GenerateURLRequestIdentifier() {
72 base::AutoLock
lock(g_next_url_request_identifier_lock
.Get());
73 return g_next_url_request_identifier
++;
76 // True once the first URLRequest was started.
77 bool g_url_requests_started
= false;
79 // True if cookies are accepted by default.
80 bool g_default_can_use_cookies
= true;
82 // When the URLRequest first assempts load timing information, it has the times
83 // at which each event occurred. The API requires the time which the request
84 // was blocked on each phase. This function handles the conversion.
86 // In the case of reusing a SPDY session, old proxy results may have been
87 // reused, so proxy resolution times may be before the request was started.
89 // Due to preconnect and late binding, it is also possible for the connection
90 // attempt to start before a request has been started, or proxy resolution
93 // This functions fixes both those cases.
94 void ConvertRealLoadTimesToBlockingTimes(
95 net::LoadTimingInfo
* load_timing_info
) {
96 DCHECK(!load_timing_info
->request_start
.is_null());
98 // Earliest time possible for the request to be blocking on connect events.
99 base::TimeTicks block_on_connect
= load_timing_info
->request_start
;
101 if (!load_timing_info
->proxy_resolve_start
.is_null()) {
102 DCHECK(!load_timing_info
->proxy_resolve_end
.is_null());
104 // Make sure the proxy times are after request start.
105 if (load_timing_info
->proxy_resolve_start
< load_timing_info
->request_start
)
106 load_timing_info
->proxy_resolve_start
= load_timing_info
->request_start
;
107 if (load_timing_info
->proxy_resolve_end
< load_timing_info
->request_start
)
108 load_timing_info
->proxy_resolve_end
= load_timing_info
->request_start
;
110 // Connect times must also be after the proxy times.
111 block_on_connect
= load_timing_info
->proxy_resolve_end
;
114 // Make sure connection times are after start and proxy times.
116 net::LoadTimingInfo::ConnectTiming
* connect_timing
=
117 &load_timing_info
->connect_timing
;
118 if (!connect_timing
->dns_start
.is_null()) {
119 DCHECK(!connect_timing
->dns_end
.is_null());
120 if (connect_timing
->dns_start
< block_on_connect
)
121 connect_timing
->dns_start
= block_on_connect
;
122 if (connect_timing
->dns_end
< block_on_connect
)
123 connect_timing
->dns_end
= block_on_connect
;
126 if (!connect_timing
->connect_start
.is_null()) {
127 DCHECK(!connect_timing
->connect_end
.is_null());
128 if (connect_timing
->connect_start
< block_on_connect
)
129 connect_timing
->connect_start
= block_on_connect
;
130 if (connect_timing
->connect_end
< block_on_connect
)
131 connect_timing
->connect_end
= block_on_connect
;
134 if (!connect_timing
->ssl_start
.is_null()) {
135 DCHECK(!connect_timing
->ssl_end
.is_null());
136 if (connect_timing
->ssl_start
< block_on_connect
)
137 connect_timing
->ssl_start
= block_on_connect
;
138 if (connect_timing
->ssl_end
< block_on_connect
)
139 connect_timing
->ssl_end
= block_on_connect
;
145 ///////////////////////////////////////////////////////////////////////////////
146 // URLRequest::Delegate
148 void URLRequest::Delegate::OnReceivedRedirect(URLRequest
* request
,
149 const RedirectInfo
& redirect_info
,
150 bool* defer_redirect
) {
153 void URLRequest::Delegate::OnAuthRequired(URLRequest
* request
,
154 AuthChallengeInfo
* auth_info
) {
155 request
->CancelAuth();
158 void URLRequest::Delegate::OnCertificateRequested(
160 SSLCertRequestInfo
* cert_request_info
) {
164 void URLRequest::Delegate::OnSSLCertificateError(URLRequest
* request
,
165 const SSLInfo
& ssl_info
,
170 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest
* request
,
174 ///////////////////////////////////////////////////////////////////////////////
177 URLRequest::~URLRequest() {
180 if (network_delegate_
) {
181 network_delegate_
->NotifyURLRequestDestroyed(this);
183 job_
->NotifyURLRequestDestroyed();
189 int deleted
= context_
->url_requests()->erase(this);
190 CHECK_EQ(1, deleted
);
193 // Log error only on failure, not cancellation, as even successful requests
194 // are "cancelled" on destruction.
195 if (status_
.status() == URLRequestStatus::FAILED
)
196 net_error
= status_
.error();
197 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE
, net_error
);
200 void URLRequest::EnableChunkedUpload() {
201 DCHECK(!upload_data_stream_
|| upload_data_stream_
->is_chunked());
202 if (!upload_data_stream_
) {
203 upload_chunked_data_stream_
= new ChunkedUploadDataStream(0);
204 upload_data_stream_
.reset(upload_chunked_data_stream_
);
208 void URLRequest::AppendChunkToUpload(const char* bytes
,
210 bool is_last_chunk
) {
211 DCHECK(upload_data_stream_
);
212 DCHECK(upload_data_stream_
->is_chunked());
213 DCHECK_GT(bytes_len
, 0);
214 upload_chunked_data_stream_
->AppendData(bytes
, bytes_len
, is_last_chunk
);
217 void URLRequest::set_upload(scoped_ptr
<UploadDataStream
> upload
) {
218 DCHECK(!upload
->is_chunked());
219 upload_data_stream_
= upload
.Pass();
222 const UploadDataStream
* URLRequest::get_upload() const {
223 return upload_data_stream_
.get();
226 bool URLRequest::has_upload() const {
227 return upload_data_stream_
.get() != NULL
;
230 void URLRequest::SetExtraRequestHeaderById(int id
, const string
& value
,
232 DCHECK(!is_pending_
|| is_redirecting_
);
233 NOTREACHED() << "implement me!";
236 void URLRequest::SetExtraRequestHeaderByName(const string
& name
,
239 DCHECK(!is_pending_
|| is_redirecting_
);
241 extra_request_headers_
.SetHeader(name
, value
);
243 extra_request_headers_
.SetHeaderIfMissing(name
, value
);
247 void URLRequest::RemoveRequestHeaderByName(const string
& name
) {
248 DCHECK(!is_pending_
|| is_redirecting_
);
249 extra_request_headers_
.RemoveHeader(name
);
252 void URLRequest::SetExtraRequestHeaders(
253 const HttpRequestHeaders
& headers
) {
254 DCHECK(!is_pending_
);
255 extra_request_headers_
= headers
;
257 // NOTE: This method will likely become non-trivial once the other setters
258 // for request headers are implemented.
261 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders
* headers
) const {
265 return job_
->GetFullRequestHeaders(headers
);
268 int64
URLRequest::GetTotalReceivedBytes() const {
272 return job_
->GetTotalReceivedBytes();
275 LoadStateWithParam
URLRequest::GetLoadState() const {
276 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
277 // delegate before it has been started.
278 if (calling_delegate_
|| !blocked_by_
.empty()) {
279 return LoadStateWithParam(
280 LOAD_STATE_WAITING_FOR_DELEGATE
,
281 use_blocked_by_as_load_param_
? base::UTF8ToUTF16(blocked_by_
) :
284 return LoadStateWithParam(job_
.get() ? job_
->GetLoadState() : LOAD_STATE_IDLE
,
288 base::Value
* URLRequest::GetStateAsValue() const {
289 base::DictionaryValue
* dict
= new base::DictionaryValue();
290 dict
->SetString("url", original_url().possibly_invalid_spec());
292 if (url_chain_
.size() > 1) {
293 base::ListValue
* list
= new base::ListValue();
294 for (std::vector
<GURL
>::const_iterator url
= url_chain_
.begin();
295 url
!= url_chain_
.end(); ++url
) {
296 list
->AppendString(url
->possibly_invalid_spec());
298 dict
->Set("url_chain", list
);
301 dict
->SetInteger("load_flags", load_flags_
);
303 LoadStateWithParam load_state
= GetLoadState();
304 dict
->SetInteger("load_state", load_state
.state
);
305 if (!load_state
.param
.empty())
306 dict
->SetString("load_state_param", load_state
.param
);
307 if (!blocked_by_
.empty())
308 dict
->SetString("delegate_info", blocked_by_
);
310 dict
->SetString("method", method_
);
311 dict
->SetBoolean("has_upload", has_upload());
312 dict
->SetBoolean("is_pending", is_pending_
);
314 // Add the status of the request. The status should always be IO_PENDING, and
315 // the error should always be OK, unless something is holding onto a request
316 // that has finished or a request was leaked. Neither of these should happen.
317 switch (status_
.status()) {
318 case URLRequestStatus::SUCCESS
:
319 dict
->SetString("status", "SUCCESS");
321 case URLRequestStatus::IO_PENDING
:
322 dict
->SetString("status", "IO_PENDING");
324 case URLRequestStatus::CANCELED
:
325 dict
->SetString("status", "CANCELED");
327 case URLRequestStatus::FAILED
:
328 dict
->SetString("status", "FAILED");
331 if (status_
.error() != OK
)
332 dict
->SetInteger("net_error", status_
.error());
336 void URLRequest::LogBlockedBy(const char* blocked_by
) {
338 DCHECK_GT(strlen(blocked_by
), 0u);
340 // Only log information to NetLog during startup and certain deferring calls
341 // to delegates. For all reads but the first, do nothing.
342 if (!calling_delegate_
&& !response_info_
.request_time
.is_null())
346 blocked_by_
= blocked_by
;
347 use_blocked_by_as_load_param_
= false;
350 NetLog::TYPE_DELEGATE_INFO
,
351 NetLog::StringCallback("delegate_info", &blocked_by_
));
354 void URLRequest::LogAndReportBlockedBy(const char* source
) {
355 LogBlockedBy(source
);
356 use_blocked_by_as_load_param_
= true;
359 void URLRequest::LogUnblocked() {
360 if (blocked_by_
.empty())
363 net_log_
.EndEvent(NetLog::TYPE_DELEGATE_INFO
);
367 UploadProgress
URLRequest::GetUploadProgress() const {
369 // We haven't started or the request was cancelled
370 return UploadProgress();
372 if (final_upload_progress_
.position()) {
373 // The first job completed and none of the subsequent series of
374 // GETs when following redirects will upload anything, so we return the
375 // cached results from the initial job, the POST.
376 return final_upload_progress_
;
378 return job_
->GetUploadProgress();
381 void URLRequest::GetResponseHeaderById(int id
, string
* value
) {
383 NOTREACHED() << "implement me!";
386 void URLRequest::GetResponseHeaderByName(const string
& name
, string
* value
) {
388 if (response_info_
.headers
.get()) {
389 response_info_
.headers
->GetNormalizedHeader(name
, value
);
395 void URLRequest::GetAllResponseHeaders(string
* headers
) {
397 if (response_info_
.headers
.get()) {
398 response_info_
.headers
->GetNormalizedHeaders(headers
);
404 HostPortPair
URLRequest::GetSocketAddress() const {
406 return job_
->GetSocketAddress();
409 HttpResponseHeaders
* URLRequest::response_headers() const {
410 return response_info_
.headers
.get();
413 void URLRequest::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
414 *load_timing_info
= load_timing_info_
;
417 bool URLRequest::GetResponseCookies(ResponseCookies
* cookies
) {
419 return job_
->GetResponseCookies(cookies
);
422 void URLRequest::GetMimeType(string
* mime_type
) const {
424 job_
->GetMimeType(mime_type
);
427 void URLRequest::GetCharset(string
* charset
) const {
429 job_
->GetCharset(charset
);
432 int URLRequest::GetResponseCode() const {
434 return job_
->GetResponseCode();
437 void URLRequest::SetLoadFlags(int flags
) {
438 if ((load_flags_
& LOAD_IGNORE_LIMITS
) != (flags
& LOAD_IGNORE_LIMITS
)) {
440 DCHECK(flags
& LOAD_IGNORE_LIMITS
);
441 DCHECK_EQ(priority_
, MAXIMUM_PRIORITY
);
445 // This should be a no-op given the above DCHECKs, but do this
446 // anyway for release mode.
447 if ((load_flags_
& LOAD_IGNORE_LIMITS
) != 0)
448 SetPriority(MAXIMUM_PRIORITY
);
452 void URLRequest::SetDefaultCookiePolicyToBlock() {
453 CHECK(!g_url_requests_started
);
454 g_default_can_use_cookies
= false;
458 bool URLRequest::IsHandledProtocol(const std::string
& scheme
) {
459 return URLRequestJobManager::SupportsScheme(scheme
);
463 bool URLRequest::IsHandledURL(const GURL
& url
) {
464 if (!url
.is_valid()) {
465 // We handle error cases.
469 return IsHandledProtocol(url
.scheme());
472 void URLRequest::set_first_party_for_cookies(
473 const GURL
& first_party_for_cookies
) {
474 DCHECK(!is_pending_
);
475 first_party_for_cookies_
= first_party_for_cookies
;
478 void URLRequest::set_first_party_url_policy(
479 FirstPartyURLPolicy first_party_url_policy
) {
480 DCHECK(!is_pending_
);
481 first_party_url_policy_
= first_party_url_policy
;
484 void URLRequest::set_method(const std::string
& method
) {
485 DCHECK(!is_pending_
);
490 std::string
URLRequest::ComputeMethodForRedirect(
491 const std::string
& method
,
492 int http_status_code
) {
493 // For 303 redirects, all request methods except HEAD are converted to GET,
494 // as per the latest httpbis draft. The draft also allows POST requests to
495 // be converted to GETs when following 301/302 redirects, for historical
496 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and
497 // the httpbis draft say to prompt the user to confirm the generation of new
498 // requests, other than GET and HEAD requests, but IE omits these prompts and
500 // See: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
501 if ((http_status_code
== 303 && method
!= "HEAD") ||
502 ((http_status_code
== 301 || http_status_code
== 302) &&
509 void URLRequest::SetReferrer(const std::string
& referrer
) {
510 DCHECK(!is_pending_
);
511 GURL
referrer_url(referrer
);
512 if (referrer_url
.is_valid()) {
513 referrer_
= referrer_url
.GetAsReferrer().spec();
515 referrer_
= referrer
;
519 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy
) {
520 DCHECK(!is_pending_
);
521 referrer_policy_
= referrer_policy
;
524 void URLRequest::set_delegate(Delegate
* delegate
) {
525 delegate_
= delegate
;
528 void URLRequest::Start() {
529 // Some values can be NULL, but the job factory must not be.
530 DCHECK(context_
->job_factory());
532 // Anything that sets |blocked_by_| before start should have cleaned up after
534 DCHECK(blocked_by_
.empty());
536 g_url_requests_started
= true;
537 response_info_
.request_time
= base::Time::Now();
539 load_timing_info_
= LoadTimingInfo();
540 load_timing_info_
.request_start_time
= response_info_
.request_time
;
541 load_timing_info_
.request_start
= base::TimeTicks::Now();
543 // Only notify the delegate for the initial request.
544 if (network_delegate_
) {
546 int error
= network_delegate_
->NotifyBeforeURLRequest(
547 this, before_request_callback_
, &delegate_redirect_url_
);
548 // If ERR_IO_PENDING is returned, the delegate will invoke
549 // |before_request_callback_| later.
550 if (error
!= ERR_IO_PENDING
)
551 BeforeRequestComplete(error
);
555 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
556 this, network_delegate_
));
559 ///////////////////////////////////////////////////////////////////////////////
561 URLRequest::URLRequest(const GURL
& url
,
562 RequestPriority priority
,
564 const URLRequestContext
* context
,
565 CookieStore
* cookie_store
,
566 NetworkDelegate
* network_delegate
)
568 network_delegate_(network_delegate
? network_delegate
569 : context
->network_delegate()),
570 net_log_(BoundNetLog::Make(context
->net_log(),
571 NetLog::SOURCE_URL_REQUEST
)),
574 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
),
575 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL
),
576 load_flags_(LOAD_NORMAL
),
579 is_redirecting_(false),
580 redirect_limit_(kMaxRedirects
),
582 identifier_(GenerateURLRequestIdentifier()),
583 calling_delegate_(false),
584 use_blocked_by_as_load_param_(false),
585 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete
,
586 base::Unretained(this))),
587 has_notified_completion_(false),
588 received_response_content_length_(0),
589 creation_time_(base::TimeTicks::Now()),
590 notified_before_network_start_(false),
591 cookie_store_(cookie_store
? cookie_store
: context
->cookie_store()) {
592 SIMPLE_STATS_COUNTER("URLRequestCount");
594 // Sanity check out environment.
595 DCHECK(base::MessageLoop::current())
596 << "The current base::MessageLoop must exist";
598 context
->url_requests()->insert(this);
599 net_log_
.BeginEvent(NetLog::TYPE_REQUEST_ALIVE
);
602 void URLRequest::BeforeRequestComplete(int error
) {
604 DCHECK_NE(ERR_IO_PENDING
, error
);
606 // Check that there are no callbacks to already canceled requests.
607 DCHECK_NE(URLRequestStatus::CANCELED
, status_
.status());
609 OnCallToDelegateComplete();
612 std::string
source("delegate");
613 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
,
614 NetLog::StringCallback("source", &source
));
615 StartJob(new URLRequestErrorJob(this, network_delegate_
, error
));
616 } else if (!delegate_redirect_url_
.is_empty()) {
618 new_url
.Swap(&delegate_redirect_url_
);
620 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
621 this, network_delegate_
, new_url
,
622 // Use status code 307 to preserve the method, so POST requests work.
623 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT
, "Delegate");
626 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
627 this, network_delegate_
));
631 void URLRequest::StartJob(URLRequestJob
* job
) {
632 DCHECK(!is_pending_
);
636 NetLog::TYPE_URL_REQUEST_START_JOB
,
637 base::Bind(&NetLogURLRequestStartCallback
,
638 &url(), &method_
, load_flags_
, priority_
,
639 upload_data_stream_
? upload_data_stream_
->identifier() : -1));
642 job_
->SetExtraRequestHeaders(extra_request_headers_
);
643 job_
->SetPriority(priority_
);
645 if (upload_data_stream_
.get())
646 job_
->SetUpload(upload_data_stream_
.get());
649 is_redirecting_
= false;
651 response_info_
.was_cached
= false;
653 if (GURL(referrer_
) != URLRequestJob::ComputeReferrerForRedirect(
654 referrer_policy_
, referrer_
, url())) {
655 if (!network_delegate_
||
656 !network_delegate_
->CancelURLRequestWithPolicyViolatingReferrerHeader(
657 *this, url(), GURL(referrer_
))) {
660 // We need to clear the referrer anyway to avoid an infinite recursion
661 // when starting the error job.
663 std::string
source("delegate");
664 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
,
665 NetLog::StringCallback("source", &source
));
666 RestartWithJob(new URLRequestErrorJob(
667 this, network_delegate_
, ERR_BLOCKED_BY_CLIENT
));
672 // Don't allow errors to be sent from within Start().
673 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
674 // we probably don't want this: they should be sent asynchronously so
675 // the caller does not get reentered.
679 void URLRequest::Restart() {
680 // Should only be called if the original job didn't make any progress.
681 DCHECK(job_
.get() && !job_
->has_response_started());
683 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_
));
686 void URLRequest::RestartWithJob(URLRequestJob
*job
) {
687 DCHECK(job
->request() == this);
692 void URLRequest::Cancel() {
693 DoCancel(ERR_ABORTED
, SSLInfo());
696 void URLRequest::CancelWithError(int error
) {
697 DoCancel(error
, SSLInfo());
700 void URLRequest::CancelWithSSLError(int error
, const SSLInfo
& ssl_info
) {
701 // This should only be called on a started request.
702 if (!is_pending_
|| !job_
.get() || job_
->has_response_started()) {
706 DoCancel(error
, ssl_info
);
709 void URLRequest::DoCancel(int error
, const SSLInfo
& ssl_info
) {
711 // If cancelled while calling a delegate, clear delegate info.
712 if (calling_delegate_
) {
714 OnCallToDelegateComplete();
717 // If the URL request already has an error status, then canceling is a no-op.
718 // Plus, we don't want to change the error status once it has been set.
719 if (status_
.is_success()) {
720 status_
.set_status(URLRequestStatus::CANCELED
);
721 status_
.set_error(error
);
722 response_info_
.ssl_info
= ssl_info
;
724 // If the request hasn't already been completed, log a cancellation event.
725 if (!has_notified_completion_
) {
726 // Don't log an error code on ERR_ABORTED, since that's redundant.
727 net_log_
.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED
,
728 error
== ERR_ABORTED
? OK
: error
);
732 if (is_pending_
&& job_
.get())
735 // We need to notify about the end of this job here synchronously. The
736 // Job sends an asynchronous notification but by the time this is processed,
737 // our |context_| is NULL.
738 NotifyRequestCompleted();
740 // The Job will call our NotifyDone method asynchronously. This is done so
741 // that the Delegate implementation can call Cancel without having to worry
742 // about being called recursively.
745 bool URLRequest::Read(IOBuffer
* dest
, int dest_size
, int* bytes_read
) {
750 // If this is the first read, end the delegate call that may have started in
751 // OnResponseStarted.
752 OnCallToDelegateComplete();
754 // This handles a cancel that happens while paused.
755 // TODO(ahendrickson): DCHECK() that it is not done after
756 // http://crbug.com/115705 is fixed.
760 if (dest_size
== 0) {
761 // Caller is not too bright. I guess we've done what they asked.
765 // Once the request fails or is cancelled, read will just return 0 bytes
766 // to indicate end of stream.
767 if (!status_
.is_success()) {
771 bool rv
= job_
->Read(dest
, dest_size
, bytes_read
);
772 // If rv is false, the status cannot be success.
773 DCHECK(rv
|| status_
.status() != URLRequestStatus::SUCCESS
);
774 if (rv
&& *bytes_read
<= 0 && status_
.is_success())
775 NotifyRequestCompleted();
779 void URLRequest::StopCaching() {
784 void URLRequest::NotifyReceivedRedirect(const RedirectInfo
& redirect_info
,
785 bool* defer_redirect
) {
786 is_redirecting_
= true;
788 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
790 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
791 this, network_delegate_
, redirect_info
.new_url
);
794 } else if (delegate_
) {
797 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
798 tracked_objects::ScopedTracker
tracking_profile(
799 FROM_HERE_WITH_EXPLICIT_FUNCTION(
800 "423948 URLRequest::Delegate::OnReceivedRedirect"));
801 delegate_
->OnReceivedRedirect(this, redirect_info
, defer_redirect
);
802 // |this| may be have been destroyed here.
806 void URLRequest::NotifyBeforeNetworkStart(bool* defer
) {
807 if (delegate_
&& !notified_before_network_start_
) {
810 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
812 tracked_objects::ScopedTracker
tracking_profile(
813 FROM_HERE_WITH_EXPLICIT_FUNCTION(
814 "423948 URLRequest::Delegate::OnBeforeNetworkStart"));
815 delegate_
->OnBeforeNetworkStart(this, defer
);
818 OnCallToDelegateComplete();
819 notified_before_network_start_
= true;
823 void URLRequest::ResumeNetworkStart() {
825 DCHECK(notified_before_network_start_
);
827 OnCallToDelegateComplete();
828 job_
->ResumeNetworkStart();
831 void URLRequest::NotifyResponseStarted() {
833 if (!status_
.is_success())
834 net_error
= status_
.error();
835 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB
,
839 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
840 this, network_delegate_
);
845 // In some cases (e.g. an event was canceled), we might have sent the
846 // completion event and receive a NotifyResponseStarted() later.
847 if (!has_notified_completion_
&& status_
.is_success()) {
848 if (network_delegate_
)
849 network_delegate_
->NotifyResponseStarted(this);
852 // Notify in case the entire URL Request has been finished.
853 if (!has_notified_completion_
&& !status_
.is_success())
854 NotifyRequestCompleted();
857 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
859 tracked_objects::ScopedTracker
tracking_profile(
860 FROM_HERE_WITH_EXPLICIT_FUNCTION(
861 "423948 URLRequest::Delegate::OnResponseStarted"));
862 delegate_
->OnResponseStarted(this);
863 // Nothing may appear below this line as OnResponseStarted may delete
869 void URLRequest::FollowDeferredRedirect() {
871 CHECK(status_
.is_success());
873 job_
->FollowDeferredRedirect();
876 void URLRequest::SetAuth(const AuthCredentials
& credentials
) {
878 DCHECK(job_
->NeedsAuth());
880 job_
->SetAuth(credentials
);
883 void URLRequest::CancelAuth() {
885 DCHECK(job_
->NeedsAuth());
890 void URLRequest::ContinueWithCertificate(X509Certificate
* client_cert
) {
893 job_
->ContinueWithCertificate(client_cert
);
896 void URLRequest::ContinueDespiteLastError() {
899 job_
->ContinueDespiteLastError();
902 void URLRequest::PrepareToRestart() {
905 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
907 net_log_
.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB
);
911 response_info_
= HttpResponseInfo();
912 response_info_
.request_time
= base::Time::Now();
914 load_timing_info_
= LoadTimingInfo();
915 load_timing_info_
.request_start_time
= response_info_
.request_time
;
916 load_timing_info_
.request_start
= base::TimeTicks::Now();
918 status_
= URLRequestStatus();
922 void URLRequest::OrphanJob() {
923 // When calling this function, please check that URLRequestHttpJob is
924 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
925 // the call back. This is currently guaranteed by the following strategies:
926 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
927 // be receiving any headers at that time.
928 // - OrphanJob is called in ~URLRequest, in this case
929 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
930 // that the callback becomes invalid.
932 job_
->DetachRequest(); // ensures that the job will not call us again
936 int URLRequest::Redirect(const RedirectInfo
& redirect_info
) {
937 // Matches call in NotifyReceivedRedirect.
938 OnCallToDelegateComplete();
939 if (net_log_
.IsLogging()) {
941 NetLog::TYPE_URL_REQUEST_REDIRECTED
,
942 NetLog::StringCallback("location",
943 &redirect_info
.new_url
.possibly_invalid_spec()));
946 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
947 if (network_delegate_
)
948 network_delegate_
->NotifyBeforeRedirect(this, redirect_info
.new_url
);
950 if (redirect_limit_
<= 0) {
951 DVLOG(1) << "disallowing redirect: exceeds limit";
952 return ERR_TOO_MANY_REDIRECTS
;
955 if (!redirect_info
.new_url
.is_valid())
956 return ERR_INVALID_URL
;
958 if (!job_
->IsSafeRedirect(redirect_info
.new_url
)) {
959 DVLOG(1) << "disallowing redirect: unsafe protocol";
960 return ERR_UNSAFE_REDIRECT
;
963 if (!final_upload_progress_
.position())
964 final_upload_progress_
= job_
->GetUploadProgress();
967 if (redirect_info
.new_method
!= method_
) {
968 // TODO(davidben): This logic still needs to be replicated at the consumers.
969 if (method_
== "POST") {
970 // If being switched from POST, must remove headers that were specific to
971 // the POST and don't have meaning in other methods. For example the
972 // inclusion of a multipart Content-Type header in GET can cause problems
973 // with some servers:
974 // http://code.google.com/p/chromium/issues/detail?id=843
975 StripPostSpecificHeaders(&extra_request_headers_
);
977 upload_data_stream_
.reset();
978 method_
= redirect_info
.new_method
;
981 referrer_
= redirect_info
.new_referrer
;
982 first_party_for_cookies_
= redirect_info
.new_first_party_for_cookies
;
984 url_chain_
.push_back(redirect_info
.new_url
);
991 const URLRequestContext
* URLRequest::context() const {
995 int64
URLRequest::GetExpectedContentSize() const {
996 int64 expected_content_size
= -1;
998 expected_content_size
= job_
->expected_content_size();
1000 return expected_content_size
;
1003 void URLRequest::SetPriority(RequestPriority priority
) {
1004 DCHECK_GE(priority
, MINIMUM_PRIORITY
);
1005 DCHECK_LE(priority
, MAXIMUM_PRIORITY
);
1007 if ((load_flags_
& LOAD_IGNORE_LIMITS
) && (priority
!= MAXIMUM_PRIORITY
)) {
1009 // Maintain the invariant that requests with IGNORE_LIMITS set
1010 // have MAXIMUM_PRIORITY for release mode.
1014 if (priority_
== priority
)
1017 priority_
= priority
;
1019 net_log_
.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY
,
1020 NetLog::IntegerCallback("priority", priority_
));
1021 job_
->SetPriority(priority_
);
1025 bool URLRequest::GetHSTSRedirect(GURL
* redirect_url
) const {
1026 const GURL
& url
= this->url();
1027 if (!url
.SchemeIs("http"))
1029 TransportSecurityState
* state
= context()->transport_security_state();
1030 if (state
&& state
->ShouldUpgradeToSSL(url
.host())) {
1031 url::Replacements
<char> replacements
;
1032 const char kNewScheme
[] = "https";
1033 replacements
.SetScheme(kNewScheme
, url::Component(0, strlen(kNewScheme
)));
1034 *redirect_url
= url
.ReplaceComponents(replacements
);
1040 void URLRequest::NotifyAuthRequired(AuthChallengeInfo
* auth_info
) {
1041 NetworkDelegate::AuthRequiredResponse rv
=
1042 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
1043 auth_info_
= auth_info
;
1044 if (network_delegate_
) {
1046 rv
= network_delegate_
->NotifyAuthRequired(
1049 base::Bind(&URLRequest::NotifyAuthRequiredComplete
,
1050 base::Unretained(this)),
1051 &auth_credentials_
);
1052 if (rv
== NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING
)
1056 NotifyAuthRequiredComplete(rv
);
1059 void URLRequest::NotifyAuthRequiredComplete(
1060 NetworkDelegate::AuthRequiredResponse result
) {
1061 OnCallToDelegateComplete();
1063 // Check that there are no callbacks to already canceled requests.
1064 DCHECK_NE(URLRequestStatus::CANCELED
, status_
.status());
1066 // NotifyAuthRequired may be called multiple times, such as
1067 // when an authentication attempt fails. Clear out the data
1068 // so it can be reset on another round.
1069 AuthCredentials credentials
= auth_credentials_
;
1070 auth_credentials_
= AuthCredentials();
1071 scoped_refptr
<AuthChallengeInfo
> auth_info
;
1072 auth_info
.swap(auth_info_
);
1075 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
:
1076 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1077 // didn't take an action.
1079 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
1081 tracked_objects::ScopedTracker
tracking_profile(
1082 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1083 "423948 URLRequest::Delegate::OnAuthRequired"));
1084 delegate_
->OnAuthRequired(this, auth_info
.get());
1088 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
:
1089 SetAuth(credentials
);
1092 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
:
1096 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING
:
1102 void URLRequest::NotifyCertificateRequested(
1103 SSLCertRequestInfo
* cert_request_info
) {
1105 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1106 tracked_objects::ScopedTracker
tracking_profile(
1107 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1108 "423948 URLRequest::Delegate::OnCertificateRequested"));
1109 delegate_
->OnCertificateRequested(this, cert_request_info
);
1113 void URLRequest::NotifySSLCertificateError(const SSLInfo
& ssl_info
,
1116 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1117 tracked_objects::ScopedTracker
tracking_profile(
1118 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1119 "423948 URLRequest::Delegate::OnSSLCertificateError"));
1120 delegate_
->OnSSLCertificateError(this, ssl_info
, fatal
);
1124 bool URLRequest::CanGetCookies(const CookieList
& cookie_list
) const {
1125 DCHECK(!(load_flags_
& LOAD_DO_NOT_SEND_COOKIES
));
1126 if (network_delegate_
) {
1127 return network_delegate_
->CanGetCookies(*this, cookie_list
);
1129 return g_default_can_use_cookies
;
1132 bool URLRequest::CanSetCookie(const std::string
& cookie_line
,
1133 CookieOptions
* options
) const {
1134 DCHECK(!(load_flags_
& LOAD_DO_NOT_SAVE_COOKIES
));
1135 if (network_delegate_
) {
1136 return network_delegate_
->CanSetCookie(*this, cookie_line
, options
);
1138 return g_default_can_use_cookies
;
1141 bool URLRequest::CanEnablePrivacyMode() const {
1142 if (network_delegate_
) {
1143 return network_delegate_
->CanEnablePrivacyMode(url(),
1144 first_party_for_cookies_
);
1146 return !g_default_can_use_cookies
;
1150 void URLRequest::NotifyReadCompleted(int bytes_read
) {
1151 // Notify in case the entire URL Request has been finished.
1152 if (bytes_read
<= 0)
1153 NotifyRequestCompleted();
1155 // Notify NetworkChangeNotifier that we just received network data.
1156 // This is to identify cases where the NetworkChangeNotifier thinks we
1157 // are off-line but we are still receiving network data (crbug.com/124069),
1158 // and to get rough network connection measurements.
1159 if (bytes_read
> 0 && !was_cached())
1160 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read
);
1163 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1164 tracked_objects::ScopedTracker
tracking_profile(
1165 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1166 "423948 URLRequest::Delegate::OnReadCompleted"));
1167 delegate_
->OnReadCompleted(this, bytes_read
);
1170 // Nothing below this line as OnReadCompleted may delete |this|.
1173 void URLRequest::OnHeadersComplete() {
1174 // Cache load timing information now, as information will be lost once the
1175 // socket is closed and the ClientSocketHandle is Reset, which will happen
1176 // once the body is complete. The start times should already be populated.
1178 // Keep a copy of the two times the URLRequest sets.
1179 base::TimeTicks request_start
= load_timing_info_
.request_start
;
1180 base::Time request_start_time
= load_timing_info_
.request_start_time
;
1182 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1183 // consistent place to start from.
1184 load_timing_info_
= LoadTimingInfo();
1185 job_
->GetLoadTimingInfo(&load_timing_info_
);
1187 load_timing_info_
.request_start
= request_start
;
1188 load_timing_info_
.request_start_time
= request_start_time
;
1190 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_
);
1194 void URLRequest::NotifyRequestCompleted() {
1195 // TODO(battre): Get rid of this check, according to willchan it should
1197 if (has_notified_completion_
)
1200 is_pending_
= false;
1201 is_redirecting_
= false;
1202 has_notified_completion_
= true;
1203 if (network_delegate_
)
1204 network_delegate_
->NotifyCompleted(this, job_
.get() != NULL
);
1207 void URLRequest::OnCallToDelegate() {
1208 DCHECK(!calling_delegate_
);
1209 DCHECK(blocked_by_
.empty());
1210 calling_delegate_
= true;
1211 net_log_
.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE
);
1214 void URLRequest::OnCallToDelegateComplete() {
1215 // This should have been cleared before resuming the request.
1216 DCHECK(blocked_by_
.empty());
1217 if (!calling_delegate_
)
1219 calling_delegate_
= false;
1220 net_log_
.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE
);
1223 void URLRequest::set_stack_trace(const base::debug::StackTrace
& stack_trace
) {
1224 base::debug::StackTrace
* stack_trace_copy
=
1225 new base::debug::StackTrace(NULL
, 0);
1226 *stack_trace_copy
= stack_trace
;
1227 stack_trace_
.reset(stack_trace_copy
);
1230 const base::debug::StackTrace
* URLRequest::stack_trace() const {
1231 return stack_trace_
.get();