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/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/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/url_request_context.h"
34 #include "net/url_request/url_request_error_job.h"
35 #include "net/url_request/url_request_job.h"
36 #include "net/url_request/url_request_job_manager.h"
37 #include "net/url_request/url_request_netlog_params.h"
38 #include "net/url_request/url_request_redirect_job.h"
47 // Max number of http redirects to follow. Same number as gecko.
48 const int kMaxRedirects
= 20;
50 // Discard headers which have meaning in POST (Content-Length, Content-Type,
52 void StripPostSpecificHeaders(HttpRequestHeaders
* headers
) {
53 // These are headers that may be attached to a POST.
54 headers
->RemoveHeader(HttpRequestHeaders::kContentLength
);
55 headers
->RemoveHeader(HttpRequestHeaders::kContentType
);
56 headers
->RemoveHeader(HttpRequestHeaders::kOrigin
);
59 // TODO(battre): Delete this, see http://crbug.com/89321:
60 // This counter keeps track of the identifiers used for URL requests so far.
61 // 0 is reserved to represent an invalid ID.
62 uint64 g_next_url_request_identifier
= 1;
64 // This lock protects g_next_url_request_identifier.
65 base::LazyInstance
<base::Lock
>::Leaky
66 g_next_url_request_identifier_lock
= LAZY_INSTANCE_INITIALIZER
;
68 // Returns an prior unused identifier for URL requests.
69 uint64
GenerateURLRequestIdentifier() {
70 base::AutoLock
lock(g_next_url_request_identifier_lock
.Get());
71 return g_next_url_request_identifier
++;
74 // True once the first URLRequest was started.
75 bool g_url_requests_started
= false;
77 // True if cookies are accepted by default.
78 bool g_default_can_use_cookies
= true;
80 // When the URLRequest first assempts load timing information, it has the times
81 // at which each event occurred. The API requires the time which the request
82 // was blocked on each phase. This function handles the conversion.
84 // In the case of reusing a SPDY session or HTTP pipeline, old proxy results may
85 // have been reused, so proxy resolution times may be before the request was
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 URLRequest::ProtocolFactory
*
145 URLRequest::Deprecated::RegisterProtocolFactory(const std::string
& scheme
,
146 ProtocolFactory
* factory
) {
147 return URLRequest::RegisterProtocolFactory(scheme
, factory
);
150 void URLRequest::Deprecated::RegisterRequestInterceptor(
151 Interceptor
* interceptor
) {
152 URLRequest::RegisterRequestInterceptor(interceptor
);
155 void URLRequest::Deprecated::UnregisterRequestInterceptor(
156 Interceptor
* interceptor
) {
157 URLRequest::UnregisterRequestInterceptor(interceptor
);
160 ///////////////////////////////////////////////////////////////////////////////
161 // URLRequest::Interceptor
163 URLRequestJob
* URLRequest::Interceptor::MaybeInterceptRedirect(
165 NetworkDelegate
* network_delegate
,
166 const GURL
& location
) {
170 URLRequestJob
* URLRequest::Interceptor::MaybeInterceptResponse(
171 URLRequest
* request
, NetworkDelegate
* network_delegate
) {
175 ///////////////////////////////////////////////////////////////////////////////
176 // URLRequest::Delegate
178 void URLRequest::Delegate::OnReceivedRedirect(URLRequest
* request
,
180 bool* defer_redirect
) {
183 void URLRequest::Delegate::OnAuthRequired(URLRequest
* request
,
184 AuthChallengeInfo
* auth_info
) {
185 request
->CancelAuth();
188 void URLRequest::Delegate::OnCertificateRequested(
190 SSLCertRequestInfo
* cert_request_info
) {
194 void URLRequest::Delegate::OnSSLCertificateError(URLRequest
* request
,
195 const SSLInfo
& ssl_info
,
200 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest
* request
,
204 ///////////////////////////////////////////////////////////////////////////////
207 URLRequest::URLRequest(const GURL
& url
,
208 RequestPriority priority
,
210 const URLRequestContext
* context
)
212 network_delegate_(context
->network_delegate()),
213 net_log_(BoundNetLog::Make(context
->net_log(),
214 NetLog::SOURCE_URL_REQUEST
)),
217 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
),
218 load_flags_(LOAD_NORMAL
),
221 is_redirecting_(false),
222 redirect_limit_(kMaxRedirects
),
224 identifier_(GenerateURLRequestIdentifier()),
225 calling_delegate_(false),
226 use_blocked_by_as_load_param_(false),
227 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete
,
228 base::Unretained(this))),
229 has_notified_completion_(false),
230 received_response_content_length_(0),
231 creation_time_(base::TimeTicks::Now()),
232 notified_before_network_start_(false) {
233 SIMPLE_STATS_COUNTER("URLRequestCount");
235 // Sanity check out environment.
236 DCHECK(base::MessageLoop::current())
237 << "The current base::MessageLoop must exist";
240 context
->url_requests()->insert(this);
242 net_log_
.BeginEvent(NetLog::TYPE_REQUEST_ALIVE
);
245 URLRequest::~URLRequest() {
248 if (network_delegate_
) {
249 network_delegate_
->NotifyURLRequestDestroyed(this);
251 job_
->NotifyURLRequestDestroyed();
257 int deleted
= context_
->url_requests()->erase(this);
258 CHECK_EQ(1, deleted
);
261 // Log error only on failure, not cancellation, as even successful requests
262 // are "cancelled" on destruction.
263 if (status_
.status() == URLRequestStatus::FAILED
)
264 net_error
= status_
.error();
265 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE
, net_error
);
269 URLRequest::ProtocolFactory
* URLRequest::RegisterProtocolFactory(
270 const string
& scheme
, ProtocolFactory
* factory
) {
271 return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme
,
276 void URLRequest::RegisterRequestInterceptor(Interceptor
* interceptor
) {
277 URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor
);
281 void URLRequest::UnregisterRequestInterceptor(Interceptor
* interceptor
) {
282 URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor(
286 void URLRequest::EnableChunkedUpload() {
287 DCHECK(!upload_data_stream_
|| upload_data_stream_
->is_chunked());
288 if (!upload_data_stream_
) {
289 upload_data_stream_
.reset(
290 new UploadDataStream(UploadDataStream::CHUNKED
, 0));
294 void URLRequest::AppendChunkToUpload(const char* bytes
,
296 bool is_last_chunk
) {
297 DCHECK(upload_data_stream_
);
298 DCHECK(upload_data_stream_
->is_chunked());
299 DCHECK_GT(bytes_len
, 0);
300 upload_data_stream_
->AppendChunk(bytes
, bytes_len
, is_last_chunk
);
303 void URLRequest::set_upload(scoped_ptr
<UploadDataStream
> upload
) {
304 DCHECK(!upload
->is_chunked());
305 upload_data_stream_
= upload
.Pass();
308 const UploadDataStream
* URLRequest::get_upload() const {
309 return upload_data_stream_
.get();
312 bool URLRequest::has_upload() const {
313 return upload_data_stream_
.get() != NULL
;
316 void URLRequest::SetExtraRequestHeaderById(int id
, const string
& value
,
318 DCHECK(!is_pending_
|| is_redirecting_
);
319 NOTREACHED() << "implement me!";
322 void URLRequest::SetExtraRequestHeaderByName(const string
& name
,
325 DCHECK(!is_pending_
|| is_redirecting_
);
327 extra_request_headers_
.SetHeader(name
, value
);
329 extra_request_headers_
.SetHeaderIfMissing(name
, value
);
333 void URLRequest::RemoveRequestHeaderByName(const string
& name
) {
334 DCHECK(!is_pending_
|| is_redirecting_
);
335 extra_request_headers_
.RemoveHeader(name
);
338 void URLRequest::SetExtraRequestHeaders(
339 const HttpRequestHeaders
& headers
) {
340 DCHECK(!is_pending_
);
341 extra_request_headers_
= headers
;
343 // NOTE: This method will likely become non-trivial once the other setters
344 // for request headers are implemented.
347 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders
* headers
) const {
351 return job_
->GetFullRequestHeaders(headers
);
354 int64
URLRequest::GetTotalReceivedBytes() const {
358 return job_
->GetTotalReceivedBytes();
361 LoadStateWithParam
URLRequest::GetLoadState() const {
362 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
363 // delegate before it has been started.
364 if (calling_delegate_
|| !blocked_by_
.empty()) {
365 return LoadStateWithParam(
366 LOAD_STATE_WAITING_FOR_DELEGATE
,
367 use_blocked_by_as_load_param_
? base::UTF8ToUTF16(blocked_by_
) :
370 return LoadStateWithParam(job_
.get() ? job_
->GetLoadState() : LOAD_STATE_IDLE
,
374 base::Value
* URLRequest::GetStateAsValue() const {
375 base::DictionaryValue
* dict
= new base::DictionaryValue();
376 dict
->SetString("url", original_url().possibly_invalid_spec());
378 if (url_chain_
.size() > 1) {
379 base::ListValue
* list
= new base::ListValue();
380 for (std::vector
<GURL
>::const_iterator url
= url_chain_
.begin();
381 url
!= url_chain_
.end(); ++url
) {
382 list
->AppendString(url
->possibly_invalid_spec());
384 dict
->Set("url_chain", list
);
387 dict
->SetInteger("load_flags", load_flags_
);
389 LoadStateWithParam load_state
= GetLoadState();
390 dict
->SetInteger("load_state", load_state
.state
);
391 if (!load_state
.param
.empty())
392 dict
->SetString("load_state_param", load_state
.param
);
393 if (!blocked_by_
.empty())
394 dict
->SetString("delegate_info", blocked_by_
);
396 dict
->SetString("method", method_
);
397 dict
->SetBoolean("has_upload", has_upload());
398 dict
->SetBoolean("is_pending", is_pending_
);
400 // Add the status of the request. The status should always be IO_PENDING, and
401 // the error should always be OK, unless something is holding onto a request
402 // that has finished or a request was leaked. Neither of these should happen.
403 switch (status_
.status()) {
404 case URLRequestStatus::SUCCESS
:
405 dict
->SetString("status", "SUCCESS");
407 case URLRequestStatus::IO_PENDING
:
408 dict
->SetString("status", "IO_PENDING");
410 case URLRequestStatus::CANCELED
:
411 dict
->SetString("status", "CANCELED");
413 case URLRequestStatus::FAILED
:
414 dict
->SetString("status", "FAILED");
417 if (status_
.error() != OK
)
418 dict
->SetInteger("net_error", status_
.error());
422 void URLRequest::LogBlockedBy(const char* blocked_by
) {
424 DCHECK_GT(strlen(blocked_by
), 0u);
426 // Only log information to NetLog during startup and certain deferring calls
427 // to delegates. For all reads but the first, do nothing.
428 if (!calling_delegate_
&& !response_info_
.request_time
.is_null())
432 blocked_by_
= blocked_by
;
433 use_blocked_by_as_load_param_
= false;
436 NetLog::TYPE_DELEGATE_INFO
,
437 NetLog::StringCallback("delegate_info", &blocked_by_
));
440 void URLRequest::LogAndReportBlockedBy(const char* source
) {
441 LogBlockedBy(source
);
442 use_blocked_by_as_load_param_
= true;
445 void URLRequest::LogUnblocked() {
446 if (blocked_by_
.empty())
449 net_log_
.EndEvent(NetLog::TYPE_DELEGATE_INFO
);
453 UploadProgress
URLRequest::GetUploadProgress() const {
455 // We haven't started or the request was cancelled
456 return UploadProgress();
458 if (final_upload_progress_
.position()) {
459 // The first job completed and none of the subsequent series of
460 // GETs when following redirects will upload anything, so we return the
461 // cached results from the initial job, the POST.
462 return final_upload_progress_
;
464 return job_
->GetUploadProgress();
467 void URLRequest::GetResponseHeaderById(int id
, string
* value
) {
469 NOTREACHED() << "implement me!";
472 void URLRequest::GetResponseHeaderByName(const string
& name
, string
* value
) {
474 if (response_info_
.headers
.get()) {
475 response_info_
.headers
->GetNormalizedHeader(name
, value
);
481 void URLRequest::GetAllResponseHeaders(string
* headers
) {
483 if (response_info_
.headers
.get()) {
484 response_info_
.headers
->GetNormalizedHeaders(headers
);
490 HostPortPair
URLRequest::GetSocketAddress() const {
492 return job_
->GetSocketAddress();
495 HttpResponseHeaders
* URLRequest::response_headers() const {
496 return response_info_
.headers
.get();
499 void URLRequest::GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const {
500 *load_timing_info
= load_timing_info_
;
503 bool URLRequest::GetResponseCookies(ResponseCookies
* cookies
) {
505 return job_
->GetResponseCookies(cookies
);
508 void URLRequest::GetMimeType(string
* mime_type
) {
510 job_
->GetMimeType(mime_type
);
513 void URLRequest::GetCharset(string
* charset
) {
515 job_
->GetCharset(charset
);
518 int URLRequest::GetResponseCode() const {
520 return job_
->GetResponseCode();
523 void URLRequest::SetLoadFlags(int flags
) {
524 if ((load_flags_
& LOAD_IGNORE_LIMITS
) != (flags
& LOAD_IGNORE_LIMITS
)) {
526 DCHECK(flags
& LOAD_IGNORE_LIMITS
);
527 DCHECK_EQ(priority_
, MAXIMUM_PRIORITY
);
531 // This should be a no-op given the above DCHECKs, but do this
532 // anyway for release mode.
533 if ((load_flags_
& LOAD_IGNORE_LIMITS
) != 0)
534 SetPriority(MAXIMUM_PRIORITY
);
538 void URLRequest::SetDefaultCookiePolicyToBlock() {
539 CHECK(!g_url_requests_started
);
540 g_default_can_use_cookies
= false;
544 bool URLRequest::IsHandledProtocol(const std::string
& scheme
) {
545 return URLRequestJobManager::GetInstance()->SupportsScheme(scheme
);
549 bool URLRequest::IsHandledURL(const GURL
& url
) {
550 if (!url
.is_valid()) {
551 // We handle error cases.
555 return IsHandledProtocol(url
.scheme());
558 void URLRequest::set_first_party_for_cookies(
559 const GURL
& first_party_for_cookies
) {
560 first_party_for_cookies_
= first_party_for_cookies
;
563 void URLRequest::set_method(const std::string
& method
) {
564 DCHECK(!is_pending_
);
569 std::string
URLRequest::ComputeMethodForRedirect(
570 const std::string
& method
,
571 int http_status_code
) {
572 // For 303 redirects, all request methods except HEAD are converted to GET,
573 // as per the latest httpbis draft. The draft also allows POST requests to
574 // be converted to GETs when following 301/302 redirects, for historical
575 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and
576 // the httpbis draft say to prompt the user to confirm the generation of new
577 // requests, other than GET and HEAD requests, but IE omits these prompts and
579 // See: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
580 if ((http_status_code
== 303 && method
!= "HEAD") ||
581 ((http_status_code
== 301 || http_status_code
== 302) &&
588 void URLRequest::SetReferrer(const std::string
& referrer
) {
589 DCHECK(!is_pending_
);
590 referrer_
= referrer
;
591 // Ensure that we do not send URL fragment, username and password
592 // fields in the referrer.
593 GURL
referrer_url(referrer
);
594 UMA_HISTOGRAM_BOOLEAN("Net.URLRequest_SetReferrer_IsEmptyOrValid",
595 referrer_url
.is_empty() || referrer_url
.is_valid());
596 if (referrer_url
.is_valid() && (referrer_url
.has_ref() ||
597 referrer_url
.has_username() || referrer_url
.has_password())) {
598 GURL::Replacements referrer_mods
;
599 referrer_mods
.ClearRef();
600 referrer_mods
.ClearUsername();
601 referrer_mods
.ClearPassword();
602 referrer_url
= referrer_url
.ReplaceComponents(referrer_mods
);
603 referrer_
= referrer_url
.spec();
607 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy
) {
608 DCHECK(!is_pending_
);
609 referrer_policy_
= referrer_policy
;
612 void URLRequest::set_delegate(Delegate
* delegate
) {
613 delegate_
= delegate
;
616 void URLRequest::Start() {
617 DCHECK_EQ(network_delegate_
, context_
->network_delegate());
618 // Anything that sets |blocked_by_| before start should have cleaned up after
620 DCHECK(blocked_by_
.empty());
622 g_url_requests_started
= true;
623 response_info_
.request_time
= base::Time::Now();
625 load_timing_info_
= LoadTimingInfo();
626 load_timing_info_
.request_start_time
= response_info_
.request_time
;
627 load_timing_info_
.request_start
= base::TimeTicks::Now();
629 // Only notify the delegate for the initial request.
630 if (network_delegate_
) {
632 int error
= network_delegate_
->NotifyBeforeURLRequest(
633 this, before_request_callback_
, &delegate_redirect_url_
);
634 // If ERR_IO_PENDING is returned, the delegate will invoke
635 // |before_request_callback_| later.
636 if (error
!= ERR_IO_PENDING
)
637 BeforeRequestComplete(error
);
641 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
642 this, network_delegate_
));
645 ///////////////////////////////////////////////////////////////////////////////
647 void URLRequest::BeforeRequestComplete(int error
) {
649 DCHECK_NE(ERR_IO_PENDING
, error
);
650 DCHECK_EQ(network_delegate_
, context_
->network_delegate());
652 // Check that there are no callbacks to already canceled requests.
653 DCHECK_NE(URLRequestStatus::CANCELED
, status_
.status());
655 OnCallToDelegateComplete();
658 std::string
source("delegate");
659 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
,
660 NetLog::StringCallback("source", &source
));
661 StartJob(new URLRequestErrorJob(this, network_delegate_
, error
));
662 } else if (!delegate_redirect_url_
.is_empty()) {
664 new_url
.Swap(&delegate_redirect_url_
);
666 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
667 this, network_delegate_
, new_url
,
668 // Use status code 307 to preserve the method, so POST requests work.
669 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT
);
672 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
673 this, network_delegate_
));
677 void URLRequest::StartJob(URLRequestJob
* job
) {
678 DCHECK(!is_pending_
);
682 NetLog::TYPE_URL_REQUEST_START_JOB
,
683 base::Bind(&NetLogURLRequestStartCallback
,
684 &url(), &method_
, load_flags_
, priority_
,
685 upload_data_stream_
? upload_data_stream_
->identifier() : -1));
688 job_
->SetExtraRequestHeaders(extra_request_headers_
);
689 job_
->SetPriority(priority_
);
691 if (upload_data_stream_
.get())
692 job_
->SetUpload(upload_data_stream_
.get());
695 is_redirecting_
= false;
697 response_info_
.was_cached
= false;
699 // If the referrer is secure, but the requested URL is not, the referrer
700 // policy should be something non-default. If you hit this, please file a
702 if (referrer_policy_
==
703 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
&&
704 GURL(referrer_
).SchemeIsSecure() && !url().SchemeIsSecure()) {
705 #if !defined(OFFICIAL_BUILD)
706 LOG(FATAL
) << "Trying to send secure referrer for insecure load";
711 // Don't allow errors to be sent from within Start().
712 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
713 // we probably don't want this: they should be sent asynchronously so
714 // the caller does not get reentered.
718 void URLRequest::Restart() {
719 // Should only be called if the original job didn't make any progress.
720 DCHECK(job_
.get() && !job_
->has_response_started());
722 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_
));
725 void URLRequest::RestartWithJob(URLRequestJob
*job
) {
726 DCHECK(job
->request() == this);
731 void URLRequest::Cancel() {
732 DoCancel(ERR_ABORTED
, SSLInfo());
735 void URLRequest::CancelWithError(int error
) {
736 DoCancel(error
, SSLInfo());
739 void URLRequest::CancelWithSSLError(int error
, const SSLInfo
& ssl_info
) {
740 // This should only be called on a started request.
741 if (!is_pending_
|| !job_
.get() || job_
->has_response_started()) {
745 DoCancel(error
, ssl_info
);
748 void URLRequest::DoCancel(int error
, const SSLInfo
& ssl_info
) {
750 // If cancelled while calling a delegate, clear delegate info.
751 if (calling_delegate_
) {
753 OnCallToDelegateComplete();
756 // If the URL request already has an error status, then canceling is a no-op.
757 // Plus, we don't want to change the error status once it has been set.
758 if (status_
.is_success()) {
759 status_
.set_status(URLRequestStatus::CANCELED
);
760 status_
.set_error(error
);
761 response_info_
.ssl_info
= ssl_info
;
763 // If the request hasn't already been completed, log a cancellation event.
764 if (!has_notified_completion_
) {
765 // Don't log an error code on ERR_ABORTED, since that's redundant.
766 net_log_
.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED
,
767 error
== ERR_ABORTED
? OK
: error
);
771 if (is_pending_
&& job_
.get())
774 // We need to notify about the end of this job here synchronously. The
775 // Job sends an asynchronous notification but by the time this is processed,
776 // our |context_| is NULL.
777 NotifyRequestCompleted();
779 // The Job will call our NotifyDone method asynchronously. This is done so
780 // that the Delegate implementation can call Cancel without having to worry
781 // about being called recursively.
784 bool URLRequest::Read(IOBuffer
* dest
, int dest_size
, int* bytes_read
) {
789 // If this is the first read, end the delegate call that may have started in
790 // OnResponseStarted.
791 OnCallToDelegateComplete();
793 // This handles a cancel that happens while paused.
794 // TODO(ahendrickson): DCHECK() that it is not done after
795 // http://crbug.com/115705 is fixed.
799 if (dest_size
== 0) {
800 // Caller is not too bright. I guess we've done what they asked.
804 // Once the request fails or is cancelled, read will just return 0 bytes
805 // to indicate end of stream.
806 if (!status_
.is_success()) {
810 bool rv
= job_
->Read(dest
, dest_size
, bytes_read
);
811 // If rv is false, the status cannot be success.
812 DCHECK(rv
|| status_
.status() != URLRequestStatus::SUCCESS
);
813 if (rv
&& *bytes_read
<= 0 && status_
.is_success())
814 NotifyRequestCompleted();
818 void URLRequest::StopCaching() {
823 void URLRequest::NotifyReceivedRedirect(const GURL
& location
,
824 bool* defer_redirect
) {
825 is_redirecting_
= true;
828 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
829 this, network_delegate_
, location
);
832 } else if (delegate_
) {
834 delegate_
->OnReceivedRedirect(this, location
, defer_redirect
);
835 // |this| may be have been destroyed here.
839 void URLRequest::NotifyBeforeNetworkStart(bool* defer
) {
840 if (delegate_
&& !notified_before_network_start_
) {
842 delegate_
->OnBeforeNetworkStart(this, defer
);
844 OnCallToDelegateComplete();
845 notified_before_network_start_
= true;
849 void URLRequest::ResumeNetworkStart() {
851 DCHECK(notified_before_network_start_
);
853 OnCallToDelegateComplete();
854 job_
->ResumeNetworkStart();
857 void URLRequest::NotifyResponseStarted() {
859 if (!status_
.is_success())
860 net_error
= status_
.error();
861 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB
,
865 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
866 this, network_delegate_
);
871 // In some cases (e.g. an event was canceled), we might have sent the
872 // completion event and receive a NotifyResponseStarted() later.
873 if (!has_notified_completion_
&& status_
.is_success()) {
874 if (network_delegate_
)
875 network_delegate_
->NotifyResponseStarted(this);
878 // Notify in case the entire URL Request has been finished.
879 if (!has_notified_completion_
&& !status_
.is_success())
880 NotifyRequestCompleted();
883 delegate_
->OnResponseStarted(this);
884 // Nothing may appear below this line as OnResponseStarted may delete
890 void URLRequest::FollowDeferredRedirect() {
892 CHECK(status_
.is_success());
894 job_
->FollowDeferredRedirect();
897 void URLRequest::SetAuth(const AuthCredentials
& credentials
) {
899 DCHECK(job_
->NeedsAuth());
901 job_
->SetAuth(credentials
);
904 void URLRequest::CancelAuth() {
906 DCHECK(job_
->NeedsAuth());
911 void URLRequest::ContinueWithCertificate(X509Certificate
* client_cert
) {
914 job_
->ContinueWithCertificate(client_cert
);
917 void URLRequest::ContinueDespiteLastError() {
920 job_
->ContinueDespiteLastError();
923 void URLRequest::PrepareToRestart() {
926 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
928 net_log_
.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB
);
932 response_info_
= HttpResponseInfo();
933 response_info_
.request_time
= base::Time::Now();
935 load_timing_info_
= LoadTimingInfo();
936 load_timing_info_
.request_start_time
= response_info_
.request_time
;
937 load_timing_info_
.request_start
= base::TimeTicks::Now();
939 status_
= URLRequestStatus();
943 void URLRequest::OrphanJob() {
944 // When calling this function, please check that URLRequestHttpJob is
945 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
946 // the call back. This is currently guaranteed by the following strategies:
947 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
948 // be receiving any headers at that time.
949 // - OrphanJob is called in ~URLRequest, in this case
950 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
951 // that the callback becomes invalid.
953 job_
->DetachRequest(); // ensures that the job will not call us again
957 int URLRequest::Redirect(const GURL
& location
, int http_status_code
) {
958 // Matches call in NotifyReceivedRedirect.
959 OnCallToDelegateComplete();
960 if (net_log_
.IsLoggingAllEvents()) {
962 NetLog::TYPE_URL_REQUEST_REDIRECTED
,
963 NetLog::StringCallback("location", &location
.possibly_invalid_spec()));
966 if (network_delegate_
)
967 network_delegate_
->NotifyBeforeRedirect(this, location
);
969 if (redirect_limit_
<= 0) {
970 DVLOG(1) << "disallowing redirect: exceeds limit";
971 return ERR_TOO_MANY_REDIRECTS
;
974 if (!location
.is_valid())
975 return ERR_INVALID_URL
;
977 if (!job_
->IsSafeRedirect(location
)) {
978 DVLOG(1) << "disallowing redirect: unsafe protocol";
979 return ERR_UNSAFE_REDIRECT
;
982 if (!final_upload_progress_
.position())
983 final_upload_progress_
= job_
->GetUploadProgress();
986 std::string
new_method(ComputeMethodForRedirect(method_
, http_status_code
));
987 if (new_method
!= method_
) {
988 if (method_
== "POST") {
989 // If being switched from POST, must remove headers that were specific to
990 // the POST and don't have meaning in other methods. For example the
991 // inclusion of a multipart Content-Type header in GET can cause problems
992 // with some servers:
993 // http://code.google.com/p/chromium/issues/detail?id=843
994 StripPostSpecificHeaders(&extra_request_headers_
);
996 upload_data_stream_
.reset();
997 method_
.swap(new_method
);
1000 // Suppress the referrer if we're redirecting out of https.
1001 if (referrer_policy_
==
1002 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
&&
1003 GURL(referrer_
).SchemeIsSecure() && !location
.SchemeIsSecure()) {
1007 url_chain_
.push_back(location
);
1014 const URLRequestContext
* URLRequest::context() const {
1018 int64
URLRequest::GetExpectedContentSize() const {
1019 int64 expected_content_size
= -1;
1021 expected_content_size
= job_
->expected_content_size();
1023 return expected_content_size
;
1026 void URLRequest::SetPriority(RequestPriority priority
) {
1027 DCHECK_GE(priority
, MINIMUM_PRIORITY
);
1028 DCHECK_LE(priority
, MAXIMUM_PRIORITY
);
1030 if ((load_flags_
& LOAD_IGNORE_LIMITS
) && (priority
!= MAXIMUM_PRIORITY
)) {
1032 // Maintain the invariant that requests with IGNORE_LIMITS set
1033 // have MAXIMUM_PRIORITY for release mode.
1037 if (priority_
== priority
)
1040 priority_
= priority
;
1042 net_log_
.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY
,
1043 NetLog::IntegerCallback("priority", priority_
));
1044 job_
->SetPriority(priority_
);
1048 bool URLRequest::GetHSTSRedirect(GURL
* redirect_url
) const {
1049 const GURL
& url
= this->url();
1050 if (!url
.SchemeIs("http"))
1052 TransportSecurityState::DomainState domain_state
;
1053 if (context()->transport_security_state() &&
1054 context()->transport_security_state()->GetDomainState(
1056 SSLConfigService::IsSNIAvailable(context()->ssl_config_service()),
1058 domain_state
.ShouldUpgradeToSSL()) {
1059 url_canon::Replacements
<char> replacements
;
1060 const char kNewScheme
[] = "https";
1061 replacements
.SetScheme(kNewScheme
,
1062 url_parse::Component(0, strlen(kNewScheme
)));
1063 *redirect_url
= url
.ReplaceComponents(replacements
);
1069 void URLRequest::NotifyAuthRequired(AuthChallengeInfo
* auth_info
) {
1070 NetworkDelegate::AuthRequiredResponse rv
=
1071 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
1072 auth_info_
= auth_info
;
1073 if (network_delegate_
) {
1075 rv
= network_delegate_
->NotifyAuthRequired(
1078 base::Bind(&URLRequest::NotifyAuthRequiredComplete
,
1079 base::Unretained(this)),
1080 &auth_credentials_
);
1081 if (rv
== NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING
)
1085 NotifyAuthRequiredComplete(rv
);
1088 void URLRequest::NotifyAuthRequiredComplete(
1089 NetworkDelegate::AuthRequiredResponse result
) {
1090 OnCallToDelegateComplete();
1092 // Check that there are no callbacks to already canceled requests.
1093 DCHECK_NE(URLRequestStatus::CANCELED
, status_
.status());
1095 // NotifyAuthRequired may be called multiple times, such as
1096 // when an authentication attempt fails. Clear out the data
1097 // so it can be reset on another round.
1098 AuthCredentials credentials
= auth_credentials_
;
1099 auth_credentials_
= AuthCredentials();
1100 scoped_refptr
<AuthChallengeInfo
> auth_info
;
1101 auth_info
.swap(auth_info_
);
1104 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
:
1105 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1106 // didn't take an action.
1108 delegate_
->OnAuthRequired(this, auth_info
.get());
1111 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
:
1112 SetAuth(credentials
);
1115 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
:
1119 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING
:
1125 void URLRequest::NotifyCertificateRequested(
1126 SSLCertRequestInfo
* cert_request_info
) {
1128 delegate_
->OnCertificateRequested(this, cert_request_info
);
1131 void URLRequest::NotifySSLCertificateError(const SSLInfo
& ssl_info
,
1134 delegate_
->OnSSLCertificateError(this, ssl_info
, fatal
);
1137 bool URLRequest::CanGetCookies(const CookieList
& cookie_list
) const {
1138 DCHECK(!(load_flags_
& LOAD_DO_NOT_SEND_COOKIES
));
1139 if (network_delegate_
) {
1140 return network_delegate_
->CanGetCookies(*this, cookie_list
);
1142 return g_default_can_use_cookies
;
1145 bool URLRequest::CanSetCookie(const std::string
& cookie_line
,
1146 CookieOptions
* options
) const {
1147 DCHECK(!(load_flags_
& LOAD_DO_NOT_SAVE_COOKIES
));
1148 if (network_delegate_
) {
1149 return network_delegate_
->CanSetCookie(*this, cookie_line
, options
);
1151 return g_default_can_use_cookies
;
1154 bool URLRequest::CanEnablePrivacyMode() const {
1155 if (network_delegate_
) {
1156 return network_delegate_
->CanEnablePrivacyMode(url(),
1157 first_party_for_cookies_
);
1159 return !g_default_can_use_cookies
;
1163 void URLRequest::NotifyReadCompleted(int bytes_read
) {
1164 // Notify in case the entire URL Request has been finished.
1165 if (bytes_read
<= 0)
1166 NotifyRequestCompleted();
1168 // Notify NetworkChangeNotifier that we just received network data.
1169 // This is to identify cases where the NetworkChangeNotifier thinks we
1170 // are off-line but we are still receiving network data (crbug.com/124069),
1171 // and to get rough network connection measurements.
1172 if (bytes_read
> 0 && !was_cached())
1173 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read
);
1176 delegate_
->OnReadCompleted(this, bytes_read
);
1178 // Nothing below this line as OnReadCompleted may delete |this|.
1181 void URLRequest::OnHeadersComplete() {
1182 // Cache load timing information now, as information will be lost once the
1183 // socket is closed and the ClientSocketHandle is Reset, which will happen
1184 // once the body is complete. The start times should already be populated.
1186 // Keep a copy of the two times the URLRequest sets.
1187 base::TimeTicks request_start
= load_timing_info_
.request_start
;
1188 base::Time request_start_time
= load_timing_info_
.request_start_time
;
1190 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1191 // consistent place to start from.
1192 load_timing_info_
= LoadTimingInfo();
1193 job_
->GetLoadTimingInfo(&load_timing_info_
);
1195 load_timing_info_
.request_start
= request_start
;
1196 load_timing_info_
.request_start_time
= request_start_time
;
1198 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_
);
1202 void URLRequest::NotifyRequestCompleted() {
1203 // TODO(battre): Get rid of this check, according to willchan it should
1205 if (has_notified_completion_
)
1208 is_pending_
= false;
1209 is_redirecting_
= false;
1210 has_notified_completion_
= true;
1211 if (network_delegate_
)
1212 network_delegate_
->NotifyCompleted(this, job_
.get() != NULL
);
1215 void URLRequest::OnCallToDelegate() {
1216 DCHECK(!calling_delegate_
);
1217 DCHECK(blocked_by_
.empty());
1218 calling_delegate_
= true;
1219 net_log_
.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE
);
1222 void URLRequest::OnCallToDelegateComplete() {
1223 // This should have been cleared before resuming the request.
1224 DCHECK(blocked_by_
.empty());
1225 if (!calling_delegate_
)
1227 calling_delegate_
= false;
1228 net_log_
.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE
);
1231 void URLRequest::set_stack_trace(const base::debug::StackTrace
& stack_trace
) {
1232 base::debug::StackTrace
* stack_trace_copy
=
1233 new base::debug::StackTrace(NULL
, 0);
1234 *stack_trace_copy
= stack_trace
;
1235 stack_trace_
.reset(stack_trace_copy
);
1238 const base::debug::StackTrace
* URLRequest::stack_trace() const {
1239 return stack_trace_
.get();