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/histogram.h"
16 #include "base/metrics/stats_counters.h"
17 #include "base/metrics/user_metrics.h"
18 #include "base/stl_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/synchronization/lock.h"
21 #include "base/values.h"
22 #include "net/base/auth.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/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 or HTTP pipeline, old proxy results may
86 // have been reused, so proxy resolution times may be before the request was
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 URLRequest::ProtocolFactory
*
146 URLRequest::Deprecated::RegisterProtocolFactory(const std::string
& scheme
,
147 ProtocolFactory
* factory
) {
148 return URLRequest::RegisterProtocolFactory(scheme
, factory
);
151 void URLRequest::Deprecated::RegisterRequestInterceptor(
152 Interceptor
* interceptor
) {
153 URLRequest::RegisterRequestInterceptor(interceptor
);
156 void URLRequest::Deprecated::UnregisterRequestInterceptor(
157 Interceptor
* interceptor
) {
158 URLRequest::UnregisterRequestInterceptor(interceptor
);
161 ///////////////////////////////////////////////////////////////////////////////
162 // URLRequest::Interceptor
164 URLRequestJob
* URLRequest::Interceptor::MaybeInterceptRedirect(
166 NetworkDelegate
* network_delegate
,
167 const GURL
& location
) {
171 URLRequestJob
* URLRequest::Interceptor::MaybeInterceptResponse(
172 URLRequest
* request
, NetworkDelegate
* network_delegate
) {
176 ///////////////////////////////////////////////////////////////////////////////
177 // URLRequest::Delegate
179 void URLRequest::Delegate::OnReceivedRedirect(URLRequest
* request
,
181 bool* defer_redirect
) {
184 void URLRequest::Delegate::OnAuthRequired(URLRequest
* request
,
185 AuthChallengeInfo
* auth_info
) {
186 request
->CancelAuth();
189 void URLRequest::Delegate::OnCertificateRequested(
191 SSLCertRequestInfo
* cert_request_info
) {
195 void URLRequest::Delegate::OnSSLCertificateError(URLRequest
* request
,
196 const SSLInfo
& ssl_info
,
201 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest
* request
,
205 ///////////////////////////////////////////////////////////////////////////////
208 URLRequest::URLRequest(const GURL
& url
,
209 RequestPriority priority
,
211 const URLRequestContext
* context
)
212 : identifier_(GenerateURLRequestIdentifier()) {
213 Init(url
, priority
, delegate
, context
, NULL
);
216 URLRequest::URLRequest(const GURL
& url
,
217 RequestPriority priority
,
219 const URLRequestContext
* context
,
220 CookieStore
* cookie_store
)
221 : identifier_(GenerateURLRequestIdentifier()) {
222 Init(url
, priority
, delegate
, context
, cookie_store
);
225 URLRequest::~URLRequest() {
228 if (network_delegate_
) {
229 network_delegate_
->NotifyURLRequestDestroyed(this);
231 job_
->NotifyURLRequestDestroyed();
237 int deleted
= context_
->url_requests()->erase(this);
238 CHECK_EQ(1, deleted
);
241 // Log error only on failure, not cancellation, as even successful requests
242 // are "cancelled" on destruction.
243 if (status_
.status() == URLRequestStatus::FAILED
)
244 net_error
= status_
.error();
245 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE
, net_error
);
249 URLRequest::ProtocolFactory
* URLRequest::RegisterProtocolFactory(
250 const string
& scheme
, ProtocolFactory
* factory
) {
251 return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme
,
256 void URLRequest::RegisterRequestInterceptor(Interceptor
* interceptor
) {
257 URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor
);
261 void URLRequest::UnregisterRequestInterceptor(Interceptor
* interceptor
) {
262 URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor(
266 void URLRequest::Init(const GURL
& url
,
267 RequestPriority priority
,
269 const URLRequestContext
* context
,
270 CookieStore
* cookie_store
) {
272 network_delegate_
= context
->network_delegate();
273 net_log_
= BoundNetLog::Make(context
->net_log(), NetLog::SOURCE_URL_REQUEST
);
274 url_chain_
.push_back(url
);
276 referrer_policy_
= CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
;
277 load_flags_
= LOAD_NORMAL
;
278 delegate_
= delegate
;
280 is_redirecting_
= false;
281 redirect_limit_
= kMaxRedirects
;
282 priority_
= priority
;
283 calling_delegate_
= false;
284 use_blocked_by_as_load_param_
=false;
285 before_request_callback_
= base::Bind(&URLRequest::BeforeRequestComplete
,
286 base::Unretained(this));
287 has_notified_completion_
= false;
288 received_response_content_length_
= 0;
289 creation_time_
= base::TimeTicks::Now();
290 notified_before_network_start_
= false;
292 SIMPLE_STATS_COUNTER("URLRequestCount");
294 // Sanity check out environment.
295 DCHECK(base::MessageLoop::current())
296 << "The current base::MessageLoop must exist";
299 context
->url_requests()->insert(this);
300 cookie_store_
= cookie_store
;
301 if (cookie_store_
== NULL
)
302 cookie_store_
= context
->cookie_store();
304 net_log_
.BeginEvent(NetLog::TYPE_REQUEST_ALIVE
);
307 void URLRequest::EnableChunkedUpload() {
308 DCHECK(!upload_data_stream_
|| upload_data_stream_
->is_chunked());
309 if (!upload_data_stream_
) {
310 upload_data_stream_
.reset(
311 new UploadDataStream(UploadDataStream::CHUNKED
, 0));
315 void URLRequest::AppendChunkToUpload(const char* bytes
,
317 bool is_last_chunk
) {
318 DCHECK(upload_data_stream_
);
319 DCHECK(upload_data_stream_
->is_chunked());
320 DCHECK_GT(bytes_len
, 0);
321 upload_data_stream_
->AppendChunk(bytes
, bytes_len
, is_last_chunk
);
324 void URLRequest::set_upload(scoped_ptr
<UploadDataStream
> upload
) {
325 DCHECK(!upload
->is_chunked());
326 upload_data_stream_
= upload
.Pass();
329 const UploadDataStream
* URLRequest::get_upload() const {
330 return upload_data_stream_
.get();
333 bool URLRequest::has_upload() const {
334 return upload_data_stream_
.get() != NULL
;
337 void URLRequest::SetExtraRequestHeaderById(int id
, const string
& value
,
339 DCHECK(!is_pending_
|| is_redirecting_
);
340 NOTREACHED() << "implement me!";
343 void URLRequest::SetExtraRequestHeaderByName(const string
& name
,
346 DCHECK(!is_pending_
|| is_redirecting_
);
348 extra_request_headers_
.SetHeader(name
, value
);
350 extra_request_headers_
.SetHeaderIfMissing(name
, value
);
354 void URLRequest::RemoveRequestHeaderByName(const string
& name
) {
355 DCHECK(!is_pending_
|| is_redirecting_
);
356 extra_request_headers_
.RemoveHeader(name
);
359 void URLRequest::SetExtraRequestHeaders(
360 const HttpRequestHeaders
& headers
) {
361 DCHECK(!is_pending_
);
362 extra_request_headers_
= headers
;
364 // NOTE: This method will likely become non-trivial once the other setters
365 // for request headers are implemented.
368 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders
* headers
) const {
372 return job_
->GetFullRequestHeaders(headers
);
375 int64
URLRequest::GetTotalReceivedBytes() const {
379 return job_
->GetTotalReceivedBytes();
382 LoadStateWithParam
URLRequest::GetLoadState() const {
383 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
384 // delegate before it has been started.
385 if (calling_delegate_
|| !blocked_by_
.empty()) {
386 return LoadStateWithParam(
387 LOAD_STATE_WAITING_FOR_DELEGATE
,
388 use_blocked_by_as_load_param_
? base::UTF8ToUTF16(blocked_by_
) :
391 return LoadStateWithParam(job_
.get() ? job_
->GetLoadState() : LOAD_STATE_IDLE
,
395 base::Value
* URLRequest::GetStateAsValue() const {
396 base::DictionaryValue
* dict
= new base::DictionaryValue();
397 dict
->SetString("url", original_url().possibly_invalid_spec());
399 if (url_chain_
.size() > 1) {
400 base::ListValue
* list
= new base::ListValue();
401 for (std::vector
<GURL
>::const_iterator url
= url_chain_
.begin();
402 url
!= url_chain_
.end(); ++url
) {
403 list
->AppendString(url
->possibly_invalid_spec());
405 dict
->Set("url_chain", list
);
408 dict
->SetInteger("load_flags", load_flags_
);
410 LoadStateWithParam load_state
= GetLoadState();
411 dict
->SetInteger("load_state", load_state
.state
);
412 if (!load_state
.param
.empty())
413 dict
->SetString("load_state_param", load_state
.param
);
414 if (!blocked_by_
.empty())
415 dict
->SetString("delegate_info", blocked_by_
);
417 dict
->SetString("method", method_
);
418 dict
->SetBoolean("has_upload", has_upload());
419 dict
->SetBoolean("is_pending", is_pending_
);
421 // Add the status of the request. The status should always be IO_PENDING, and
422 // the error should always be OK, unless something is holding onto a request
423 // that has finished or a request was leaked. Neither of these should happen.
424 switch (status_
.status()) {
425 case URLRequestStatus::SUCCESS
:
426 dict
->SetString("status", "SUCCESS");
428 case URLRequestStatus::IO_PENDING
:
429 dict
->SetString("status", "IO_PENDING");
431 case URLRequestStatus::CANCELED
:
432 dict
->SetString("status", "CANCELED");
434 case URLRequestStatus::FAILED
:
435 dict
->SetString("status", "FAILED");
438 if (status_
.error() != OK
)
439 dict
->SetInteger("net_error", status_
.error());
443 void URLRequest::LogBlockedBy(const char* blocked_by
) {
445 DCHECK_GT(strlen(blocked_by
), 0u);
447 // Only log information to NetLog during startup and certain deferring calls
448 // to delegates. For all reads but the first, do nothing.
449 if (!calling_delegate_
&& !response_info_
.request_time
.is_null())
453 blocked_by_
= blocked_by
;
454 use_blocked_by_as_load_param_
= false;
457 NetLog::TYPE_DELEGATE_INFO
,
458 NetLog::StringCallback("delegate_info", &blocked_by_
));
461 void URLRequest::LogAndReportBlockedBy(const char* source
) {
462 LogBlockedBy(source
);
463 use_blocked_by_as_load_param_
= true;
466 void URLRequest::LogUnblocked() {
467 if (blocked_by_
.empty())
470 net_log_
.EndEvent(NetLog::TYPE_DELEGATE_INFO
);
474 UploadProgress
URLRequest::GetUploadProgress() const {
476 // We haven't started or the request was cancelled
477 return UploadProgress();
479 if (final_upload_progress_
.position()) {
480 // The first job completed and none of the subsequent series of
481 // GETs when following redirects will upload anything, so we return the
482 // cached results from the initial job, the POST.
483 return final_upload_progress_
;
485 return job_
->GetUploadProgress();
488 void URLRequest::GetResponseHeaderById(int id
, string
* value
) {
490 NOTREACHED() << "implement me!";
493 void URLRequest::GetResponseHeaderByName(const string
& name
, string
* value
) {
495 if (response_info_
.headers
.get()) {
496 response_info_
.headers
->GetNormalizedHeader(name
, value
);
502 void URLRequest::GetAllResponseHeaders(string
* headers
) {
504 if (response_info_
.headers
.get()) {
505 response_info_
.headers
->GetNormalizedHeaders(headers
);
511 HostPortPair
URLRequest::GetSocketAddress() const {
513 return job_
->GetSocketAddress();
516 HttpResponseHeaders
* URLRequest::response_headers() const {
517 return response_info_
.headers
.get();
520 void URLRequest::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
521 *load_timing_info
= load_timing_info_
;
524 bool URLRequest::GetResponseCookies(ResponseCookies
* cookies
) {
526 return job_
->GetResponseCookies(cookies
);
529 void URLRequest::GetMimeType(string
* mime_type
) {
531 job_
->GetMimeType(mime_type
);
534 void URLRequest::GetCharset(string
* charset
) {
536 job_
->GetCharset(charset
);
539 int URLRequest::GetResponseCode() const {
541 return job_
->GetResponseCode();
544 void URLRequest::SetLoadFlags(int flags
) {
545 if ((load_flags_
& LOAD_IGNORE_LIMITS
) != (flags
& LOAD_IGNORE_LIMITS
)) {
547 DCHECK(flags
& LOAD_IGNORE_LIMITS
);
548 DCHECK_EQ(priority_
, MAXIMUM_PRIORITY
);
552 // This should be a no-op given the above DCHECKs, but do this
553 // anyway for release mode.
554 if ((load_flags_
& LOAD_IGNORE_LIMITS
) != 0)
555 SetPriority(MAXIMUM_PRIORITY
);
559 void URLRequest::SetDefaultCookiePolicyToBlock() {
560 CHECK(!g_url_requests_started
);
561 g_default_can_use_cookies
= false;
565 bool URLRequest::IsHandledProtocol(const std::string
& scheme
) {
566 return URLRequestJobManager::GetInstance()->SupportsScheme(scheme
);
570 bool URLRequest::IsHandledURL(const GURL
& url
) {
571 if (!url
.is_valid()) {
572 // We handle error cases.
576 return IsHandledProtocol(url
.scheme());
579 void URLRequest::set_first_party_for_cookies(
580 const GURL
& first_party_for_cookies
) {
581 first_party_for_cookies_
= first_party_for_cookies
;
584 void URLRequest::set_method(const std::string
& method
) {
585 DCHECK(!is_pending_
);
590 std::string
URLRequest::ComputeMethodForRedirect(
591 const std::string
& method
,
592 int http_status_code
) {
593 // For 303 redirects, all request methods except HEAD are converted to GET,
594 // as per the latest httpbis draft. The draft also allows POST requests to
595 // be converted to GETs when following 301/302 redirects, for historical
596 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and
597 // the httpbis draft say to prompt the user to confirm the generation of new
598 // requests, other than GET and HEAD requests, but IE omits these prompts and
600 // See: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
601 if ((http_status_code
== 303 && method
!= "HEAD") ||
602 ((http_status_code
== 301 || http_status_code
== 302) &&
609 void URLRequest::SetReferrer(const std::string
& referrer
) {
610 DCHECK(!is_pending_
);
611 GURL
referrer_url(referrer
);
612 UMA_HISTOGRAM_BOOLEAN("Net.URLRequest_SetReferrer_IsEmptyOrValid",
613 referrer_url
.is_empty() || referrer_url
.is_valid());
614 if (referrer_url
.is_valid()) {
615 referrer_
= referrer_url
.GetAsReferrer().spec();
617 referrer_
= referrer
;
621 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy
) {
622 DCHECK(!is_pending_
);
623 referrer_policy_
= referrer_policy
;
626 void URLRequest::set_delegate(Delegate
* delegate
) {
627 delegate_
= delegate
;
630 void URLRequest::Start() {
631 DCHECK_EQ(network_delegate_
, context_
->network_delegate());
632 // Anything that sets |blocked_by_| before start should have cleaned up after
634 DCHECK(blocked_by_
.empty());
636 g_url_requests_started
= true;
637 response_info_
.request_time
= base::Time::Now();
639 load_timing_info_
= LoadTimingInfo();
640 load_timing_info_
.request_start_time
= response_info_
.request_time
;
641 load_timing_info_
.request_start
= base::TimeTicks::Now();
643 // Only notify the delegate for the initial request.
644 if (network_delegate_
) {
646 int error
= network_delegate_
->NotifyBeforeURLRequest(
647 this, before_request_callback_
, &delegate_redirect_url_
);
648 // If ERR_IO_PENDING is returned, the delegate will invoke
649 // |before_request_callback_| later.
650 if (error
!= ERR_IO_PENDING
)
651 BeforeRequestComplete(error
);
655 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
656 this, network_delegate_
));
659 ///////////////////////////////////////////////////////////////////////////////
661 void URLRequest::BeforeRequestComplete(int error
) {
663 DCHECK_NE(ERR_IO_PENDING
, error
);
664 DCHECK_EQ(network_delegate_
, context_
->network_delegate());
666 // Check that there are no callbacks to already canceled requests.
667 DCHECK_NE(URLRequestStatus::CANCELED
, status_
.status());
669 OnCallToDelegateComplete();
672 std::string
source("delegate");
673 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
,
674 NetLog::StringCallback("source", &source
));
675 StartJob(new URLRequestErrorJob(this, network_delegate_
, error
));
676 } else if (!delegate_redirect_url_
.is_empty()) {
678 new_url
.Swap(&delegate_redirect_url_
);
680 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
681 this, network_delegate_
, new_url
,
682 // Use status code 307 to preserve the method, so POST requests work.
683 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT
, "Delegate");
686 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
687 this, network_delegate_
));
691 void URLRequest::StartJob(URLRequestJob
* job
) {
692 DCHECK(!is_pending_
);
696 NetLog::TYPE_URL_REQUEST_START_JOB
,
697 base::Bind(&NetLogURLRequestStartCallback
,
698 &url(), &method_
, load_flags_
, priority_
,
699 upload_data_stream_
? upload_data_stream_
->identifier() : -1));
702 job_
->SetExtraRequestHeaders(extra_request_headers_
);
703 job_
->SetPriority(priority_
);
705 if (upload_data_stream_
.get())
706 job_
->SetUpload(upload_data_stream_
.get());
709 is_redirecting_
= false;
711 response_info_
.was_cached
= false;
713 // If the referrer is secure, but the requested URL is not, the referrer
714 // policy should be something non-default. If you hit this, please file a
716 if (referrer_policy_
==
717 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
&&
718 GURL(referrer_
).SchemeIsSecure() && !url().SchemeIsSecure()) {
719 #if !defined(OFFICIAL_BUILD)
720 LOG(FATAL
) << "Trying to send secure referrer for insecure load";
724 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
727 // Don't allow errors to be sent from within Start().
728 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
729 // we probably don't want this: they should be sent asynchronously so
730 // the caller does not get reentered.
734 void URLRequest::Restart() {
735 // Should only be called if the original job didn't make any progress.
736 DCHECK(job_
.get() && !job_
->has_response_started());
738 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_
));
741 void URLRequest::RestartWithJob(URLRequestJob
*job
) {
742 DCHECK(job
->request() == this);
747 void URLRequest::Cancel() {
748 DoCancel(ERR_ABORTED
, SSLInfo());
751 void URLRequest::CancelWithError(int error
) {
752 DoCancel(error
, SSLInfo());
755 void URLRequest::CancelWithSSLError(int error
, const SSLInfo
& ssl_info
) {
756 // This should only be called on a started request.
757 if (!is_pending_
|| !job_
.get() || job_
->has_response_started()) {
761 DoCancel(error
, ssl_info
);
764 void URLRequest::DoCancel(int error
, const SSLInfo
& ssl_info
) {
766 // If cancelled while calling a delegate, clear delegate info.
767 if (calling_delegate_
) {
769 OnCallToDelegateComplete();
772 // If the URL request already has an error status, then canceling is a no-op.
773 // Plus, we don't want to change the error status once it has been set.
774 if (status_
.is_success()) {
775 status_
.set_status(URLRequestStatus::CANCELED
);
776 status_
.set_error(error
);
777 response_info_
.ssl_info
= ssl_info
;
779 // If the request hasn't already been completed, log a cancellation event.
780 if (!has_notified_completion_
) {
781 // Don't log an error code on ERR_ABORTED, since that's redundant.
782 net_log_
.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED
,
783 error
== ERR_ABORTED
? OK
: error
);
787 if (is_pending_
&& job_
.get())
790 // We need to notify about the end of this job here synchronously. The
791 // Job sends an asynchronous notification but by the time this is processed,
792 // our |context_| is NULL.
793 NotifyRequestCompleted();
795 // The Job will call our NotifyDone method asynchronously. This is done so
796 // that the Delegate implementation can call Cancel without having to worry
797 // about being called recursively.
800 bool URLRequest::Read(IOBuffer
* dest
, int dest_size
, int* bytes_read
) {
805 // If this is the first read, end the delegate call that may have started in
806 // OnResponseStarted.
807 OnCallToDelegateComplete();
809 // This handles a cancel that happens while paused.
810 // TODO(ahendrickson): DCHECK() that it is not done after
811 // http://crbug.com/115705 is fixed.
815 if (dest_size
== 0) {
816 // Caller is not too bright. I guess we've done what they asked.
820 // Once the request fails or is cancelled, read will just return 0 bytes
821 // to indicate end of stream.
822 if (!status_
.is_success()) {
826 bool rv
= job_
->Read(dest
, dest_size
, bytes_read
);
827 // If rv is false, the status cannot be success.
828 DCHECK(rv
|| status_
.status() != URLRequestStatus::SUCCESS
);
829 if (rv
&& *bytes_read
<= 0 && status_
.is_success())
830 NotifyRequestCompleted();
834 void URLRequest::StopCaching() {
839 void URLRequest::NotifyReceivedRedirect(const GURL
& location
,
840 bool* defer_redirect
) {
841 is_redirecting_
= true;
844 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
845 this, network_delegate_
, location
);
848 } else if (delegate_
) {
850 delegate_
->OnReceivedRedirect(this, location
, defer_redirect
);
851 // |this| may be have been destroyed here.
855 void URLRequest::NotifyBeforeNetworkStart(bool* defer
) {
856 if (delegate_
&& !notified_before_network_start_
) {
858 delegate_
->OnBeforeNetworkStart(this, defer
);
860 OnCallToDelegateComplete();
861 notified_before_network_start_
= true;
865 void URLRequest::ResumeNetworkStart() {
867 DCHECK(notified_before_network_start_
);
869 OnCallToDelegateComplete();
870 job_
->ResumeNetworkStart();
873 void URLRequest::NotifyResponseStarted() {
875 if (!status_
.is_success())
876 net_error
= status_
.error();
877 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB
,
881 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
882 this, network_delegate_
);
887 // In some cases (e.g. an event was canceled), we might have sent the
888 // completion event and receive a NotifyResponseStarted() later.
889 if (!has_notified_completion_
&& status_
.is_success()) {
890 if (network_delegate_
)
891 network_delegate_
->NotifyResponseStarted(this);
894 // Notify in case the entire URL Request has been finished.
895 if (!has_notified_completion_
&& !status_
.is_success())
896 NotifyRequestCompleted();
899 delegate_
->OnResponseStarted(this);
900 // Nothing may appear below this line as OnResponseStarted may delete
906 void URLRequest::FollowDeferredRedirect() {
908 CHECK(status_
.is_success());
910 job_
->FollowDeferredRedirect();
913 void URLRequest::SetAuth(const AuthCredentials
& credentials
) {
915 DCHECK(job_
->NeedsAuth());
917 job_
->SetAuth(credentials
);
920 void URLRequest::CancelAuth() {
922 DCHECK(job_
->NeedsAuth());
927 void URLRequest::ContinueWithCertificate(X509Certificate
* client_cert
) {
930 job_
->ContinueWithCertificate(client_cert
);
933 void URLRequest::ContinueDespiteLastError() {
936 job_
->ContinueDespiteLastError();
939 void URLRequest::PrepareToRestart() {
942 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
944 net_log_
.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB
);
948 response_info_
= HttpResponseInfo();
949 response_info_
.request_time
= base::Time::Now();
951 load_timing_info_
= LoadTimingInfo();
952 load_timing_info_
.request_start_time
= response_info_
.request_time
;
953 load_timing_info_
.request_start
= base::TimeTicks::Now();
955 status_
= URLRequestStatus();
959 void URLRequest::OrphanJob() {
960 // When calling this function, please check that URLRequestHttpJob is
961 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
962 // the call back. This is currently guaranteed by the following strategies:
963 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
964 // be receiving any headers at that time.
965 // - OrphanJob is called in ~URLRequest, in this case
966 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
967 // that the callback becomes invalid.
969 job_
->DetachRequest(); // ensures that the job will not call us again
973 int URLRequest::Redirect(const GURL
& location
, int http_status_code
) {
974 // Matches call in NotifyReceivedRedirect.
975 OnCallToDelegateComplete();
976 if (net_log_
.IsLogging()) {
978 NetLog::TYPE_URL_REQUEST_REDIRECTED
,
979 NetLog::StringCallback("location", &location
.possibly_invalid_spec()));
982 if (network_delegate_
)
983 network_delegate_
->NotifyBeforeRedirect(this, location
);
985 if (redirect_limit_
<= 0) {
986 DVLOG(1) << "disallowing redirect: exceeds limit";
987 return ERR_TOO_MANY_REDIRECTS
;
990 if (!location
.is_valid())
991 return ERR_INVALID_URL
;
993 if (!job_
->IsSafeRedirect(location
)) {
994 DVLOG(1) << "disallowing redirect: unsafe protocol";
995 return ERR_UNSAFE_REDIRECT
;
998 if (!final_upload_progress_
.position())
999 final_upload_progress_
= job_
->GetUploadProgress();
1002 std::string
new_method(ComputeMethodForRedirect(method_
, http_status_code
));
1003 if (new_method
!= method_
) {
1004 if (method_
== "POST") {
1005 // If being switched from POST, must remove headers that were specific to
1006 // the POST and don't have meaning in other methods. For example the
1007 // inclusion of a multipart Content-Type header in GET can cause problems
1008 // with some servers:
1009 // http://code.google.com/p/chromium/issues/detail?id=843
1010 StripPostSpecificHeaders(&extra_request_headers_
);
1012 upload_data_stream_
.reset();
1013 method_
.swap(new_method
);
1016 // Suppress the referrer if we're redirecting out of https.
1017 if (referrer_policy_
==
1018 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
&&
1019 GURL(referrer_
).SchemeIsSecure() && !location
.SchemeIsSecure()) {
1023 url_chain_
.push_back(location
);
1030 const URLRequestContext
* URLRequest::context() const {
1034 int64
URLRequest::GetExpectedContentSize() const {
1035 int64 expected_content_size
= -1;
1037 expected_content_size
= job_
->expected_content_size();
1039 return expected_content_size
;
1042 void URLRequest::SetPriority(RequestPriority priority
) {
1043 DCHECK_GE(priority
, MINIMUM_PRIORITY
);
1044 DCHECK_LE(priority
, MAXIMUM_PRIORITY
);
1046 if ((load_flags_
& LOAD_IGNORE_LIMITS
) && (priority
!= MAXIMUM_PRIORITY
)) {
1048 // Maintain the invariant that requests with IGNORE_LIMITS set
1049 // have MAXIMUM_PRIORITY for release mode.
1053 if (priority_
== priority
)
1056 priority_
= priority
;
1058 net_log_
.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY
,
1059 NetLog::IntegerCallback("priority", priority_
));
1060 job_
->SetPriority(priority_
);
1064 bool URLRequest::GetHSTSRedirect(GURL
* redirect_url
) const {
1065 const GURL
& url
= this->url();
1066 if (!url
.SchemeIs("http"))
1068 TransportSecurityState::DomainState domain_state
;
1069 if (context()->transport_security_state() &&
1070 context()->transport_security_state()->GetDomainState(
1072 SSLConfigService::IsSNIAvailable(context()->ssl_config_service()),
1074 domain_state
.ShouldUpgradeToSSL()) {
1075 url::Replacements
<char> replacements
;
1076 const char kNewScheme
[] = "https";
1077 replacements
.SetScheme(kNewScheme
, url::Component(0, strlen(kNewScheme
)));
1078 *redirect_url
= url
.ReplaceComponents(replacements
);
1084 void URLRequest::NotifyAuthRequired(AuthChallengeInfo
* auth_info
) {
1085 NetworkDelegate::AuthRequiredResponse rv
=
1086 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
1087 auth_info_
= auth_info
;
1088 if (network_delegate_
) {
1090 rv
= network_delegate_
->NotifyAuthRequired(
1093 base::Bind(&URLRequest::NotifyAuthRequiredComplete
,
1094 base::Unretained(this)),
1095 &auth_credentials_
);
1096 if (rv
== NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING
)
1100 NotifyAuthRequiredComplete(rv
);
1103 void URLRequest::NotifyAuthRequiredComplete(
1104 NetworkDelegate::AuthRequiredResponse result
) {
1105 OnCallToDelegateComplete();
1107 // Check that there are no callbacks to already canceled requests.
1108 DCHECK_NE(URLRequestStatus::CANCELED
, status_
.status());
1110 // NotifyAuthRequired may be called multiple times, such as
1111 // when an authentication attempt fails. Clear out the data
1112 // so it can be reset on another round.
1113 AuthCredentials credentials
= auth_credentials_
;
1114 auth_credentials_
= AuthCredentials();
1115 scoped_refptr
<AuthChallengeInfo
> auth_info
;
1116 auth_info
.swap(auth_info_
);
1119 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
:
1120 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1121 // didn't take an action.
1123 delegate_
->OnAuthRequired(this, auth_info
.get());
1126 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
:
1127 SetAuth(credentials
);
1130 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
:
1134 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING
:
1140 void URLRequest::NotifyCertificateRequested(
1141 SSLCertRequestInfo
* cert_request_info
) {
1143 delegate_
->OnCertificateRequested(this, cert_request_info
);
1146 void URLRequest::NotifySSLCertificateError(const SSLInfo
& ssl_info
,
1149 delegate_
->OnSSLCertificateError(this, ssl_info
, fatal
);
1152 bool URLRequest::CanGetCookies(const CookieList
& cookie_list
) const {
1153 DCHECK(!(load_flags_
& LOAD_DO_NOT_SEND_COOKIES
));
1154 if (network_delegate_
) {
1155 return network_delegate_
->CanGetCookies(*this, cookie_list
);
1157 return g_default_can_use_cookies
;
1160 bool URLRequest::CanSetCookie(const std::string
& cookie_line
,
1161 CookieOptions
* options
) const {
1162 DCHECK(!(load_flags_
& LOAD_DO_NOT_SAVE_COOKIES
));
1163 if (network_delegate_
) {
1164 return network_delegate_
->CanSetCookie(*this, cookie_line
, options
);
1166 return g_default_can_use_cookies
;
1169 bool URLRequest::CanEnablePrivacyMode() const {
1170 if (network_delegate_
) {
1171 return network_delegate_
->CanEnablePrivacyMode(url(),
1172 first_party_for_cookies_
);
1174 return !g_default_can_use_cookies
;
1178 void URLRequest::NotifyReadCompleted(int bytes_read
) {
1179 // Notify in case the entire URL Request has been finished.
1180 if (bytes_read
<= 0)
1181 NotifyRequestCompleted();
1183 // Notify NetworkChangeNotifier that we just received network data.
1184 // This is to identify cases where the NetworkChangeNotifier thinks we
1185 // are off-line but we are still receiving network data (crbug.com/124069),
1186 // and to get rough network connection measurements.
1187 if (bytes_read
> 0 && !was_cached())
1188 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read
);
1191 delegate_
->OnReadCompleted(this, bytes_read
);
1193 // Nothing below this line as OnReadCompleted may delete |this|.
1196 void URLRequest::OnHeadersComplete() {
1197 // Cache load timing information now, as information will be lost once the
1198 // socket is closed and the ClientSocketHandle is Reset, which will happen
1199 // once the body is complete. The start times should already be populated.
1201 // Keep a copy of the two times the URLRequest sets.
1202 base::TimeTicks request_start
= load_timing_info_
.request_start
;
1203 base::Time request_start_time
= load_timing_info_
.request_start_time
;
1205 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1206 // consistent place to start from.
1207 load_timing_info_
= LoadTimingInfo();
1208 job_
->GetLoadTimingInfo(&load_timing_info_
);
1210 load_timing_info_
.request_start
= request_start
;
1211 load_timing_info_
.request_start_time
= request_start_time
;
1213 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_
);
1217 void URLRequest::NotifyRequestCompleted() {
1218 // TODO(battre): Get rid of this check, according to willchan it should
1220 if (has_notified_completion_
)
1223 is_pending_
= false;
1224 is_redirecting_
= false;
1225 has_notified_completion_
= true;
1226 if (network_delegate_
)
1227 network_delegate_
->NotifyCompleted(this, job_
.get() != NULL
);
1230 void URLRequest::OnCallToDelegate() {
1231 DCHECK(!calling_delegate_
);
1232 DCHECK(blocked_by_
.empty());
1233 calling_delegate_
= true;
1234 net_log_
.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE
);
1237 void URLRequest::OnCallToDelegateComplete() {
1238 // This should have been cleared before resuming the request.
1239 DCHECK(blocked_by_
.empty());
1240 if (!calling_delegate_
)
1242 calling_delegate_
= false;
1243 net_log_
.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE
);
1246 void URLRequest::set_stack_trace(const base::debug::StackTrace
& stack_trace
) {
1247 base::debug::StackTrace
* stack_trace_copy
=
1248 new base::debug::StackTrace(NULL
, 0);
1249 *stack_trace_copy
= stack_trace
;
1250 stack_trace_
.reset(stack_trace_copy
);
1253 const base::debug::StackTrace
* URLRequest::stack_trace() const {
1254 return stack_trace_
.get();