[Sync] Switch Android sync tests to use IsSyncActive.
[chromium-blink-merge.git] / net / url_request / url_request.cc
blob254f65766453a7cfecb5de7de1c4345d23b978b3
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"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/debug/stack_trace.h"
12 #include "base/lazy_instance.h"
13 #include "base/memory/singleton.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/profiler/scoped_tracker.h"
16 #include "base/stl_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/synchronization/lock.h"
19 #include "base/values.h"
20 #include "net/base/auth.h"
21 #include "net/base/chunked_upload_data_stream.h"
22 #include "net/base/host_port_pair.h"
23 #include "net/base/load_flags.h"
24 #include "net/base/load_timing_info.h"
25 #include "net/base/net_errors.h"
26 #include "net/base/network_change_notifier.h"
27 #include "net/base/network_delegate.h"
28 #include "net/base/upload_data_stream.h"
29 #include "net/http/http_response_headers.h"
30 #include "net/http/http_util.h"
31 #include "net/log/net_log.h"
32 #include "net/ssl/ssl_cert_request_info.h"
33 #include "net/url_request/redirect_info.h"
34 #include "net/url_request/url_request_context.h"
35 #include "net/url_request/url_request_error_job.h"
36 #include "net/url_request/url_request_job.h"
37 #include "net/url_request/url_request_job_manager.h"
38 #include "net/url_request/url_request_netlog_params.h"
39 #include "net/url_request/url_request_redirect_job.h"
40 #include "url/gurl.h"
41 #include "url/origin.h"
43 using base::Time;
44 using std::string;
46 namespace net {
48 namespace {
50 // Max number of http redirects to follow. Same number as gecko.
51 const int kMaxRedirects = 20;
53 // Discard headers which have meaning in POST (Content-Length, Content-Type,
54 // Origin).
55 void StripPostSpecificHeaders(HttpRequestHeaders* headers) {
56 // These are headers that may be attached to a POST.
57 headers->RemoveHeader(HttpRequestHeaders::kContentLength);
58 headers->RemoveHeader(HttpRequestHeaders::kContentType);
59 // TODO(jww): This is Origin header removal is probably layering violation and
60 // should be refactored into //content. See https://crbug.com/471397.
61 headers->RemoveHeader(HttpRequestHeaders::kOrigin);
64 // TODO(battre): Delete this, see http://crbug.com/89321:
65 // This counter keeps track of the identifiers used for URL requests so far.
66 // 0 is reserved to represent an invalid ID.
67 uint64 g_next_url_request_identifier = 1;
69 // This lock protects g_next_url_request_identifier.
70 base::LazyInstance<base::Lock>::Leaky
71 g_next_url_request_identifier_lock = LAZY_INSTANCE_INITIALIZER;
73 // Returns an prior unused identifier for URL requests.
74 uint64 GenerateURLRequestIdentifier() {
75 base::AutoLock lock(g_next_url_request_identifier_lock.Get());
76 return g_next_url_request_identifier++;
79 // True once the first URLRequest was started.
80 bool g_url_requests_started = false;
82 // True if cookies are accepted by default.
83 bool g_default_can_use_cookies = true;
85 // When the URLRequest first assempts load timing information, it has the times
86 // at which each event occurred. The API requires the time which the request
87 // was blocked on each phase. This function handles the conversion.
89 // In the case of reusing a SPDY session, old proxy results may have been
90 // reused, so proxy resolution times may be before the request was started.
92 // Due to preconnect and late binding, it is also possible for the connection
93 // attempt to start before a request has been started, or proxy resolution
94 // completed.
96 // This functions fixes both those cases.
97 void ConvertRealLoadTimesToBlockingTimes(LoadTimingInfo* load_timing_info) {
98 DCHECK(!load_timing_info->request_start.is_null());
100 // Earliest time possible for the request to be blocking on connect events.
101 base::TimeTicks block_on_connect = load_timing_info->request_start;
103 if (!load_timing_info->proxy_resolve_start.is_null()) {
104 DCHECK(!load_timing_info->proxy_resolve_end.is_null());
106 // Make sure the proxy times are after request start.
107 if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
108 load_timing_info->proxy_resolve_start = load_timing_info->request_start;
109 if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
110 load_timing_info->proxy_resolve_end = load_timing_info->request_start;
112 // Connect times must also be after the proxy times.
113 block_on_connect = load_timing_info->proxy_resolve_end;
116 // Make sure connection times are after start and proxy times.
118 LoadTimingInfo::ConnectTiming* connect_timing =
119 &load_timing_info->connect_timing;
120 if (!connect_timing->dns_start.is_null()) {
121 DCHECK(!connect_timing->dns_end.is_null());
122 if (connect_timing->dns_start < block_on_connect)
123 connect_timing->dns_start = block_on_connect;
124 if (connect_timing->dns_end < block_on_connect)
125 connect_timing->dns_end = block_on_connect;
128 if (!connect_timing->connect_start.is_null()) {
129 DCHECK(!connect_timing->connect_end.is_null());
130 if (connect_timing->connect_start < block_on_connect)
131 connect_timing->connect_start = block_on_connect;
132 if (connect_timing->connect_end < block_on_connect)
133 connect_timing->connect_end = block_on_connect;
136 if (!connect_timing->ssl_start.is_null()) {
137 DCHECK(!connect_timing->ssl_end.is_null());
138 if (connect_timing->ssl_start < block_on_connect)
139 connect_timing->ssl_start = block_on_connect;
140 if (connect_timing->ssl_end < block_on_connect)
141 connect_timing->ssl_end = block_on_connect;
145 } // namespace
147 ///////////////////////////////////////////////////////////////////////////////
148 // URLRequest::Delegate
150 void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
151 const RedirectInfo& redirect_info,
152 bool* defer_redirect) {
155 void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
156 AuthChallengeInfo* auth_info) {
157 request->CancelAuth();
160 void URLRequest::Delegate::OnCertificateRequested(
161 URLRequest* request,
162 SSLCertRequestInfo* cert_request_info) {
163 request->CancelWithError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
166 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
167 const SSLInfo& ssl_info,
168 bool is_hsts_ok) {
169 request->Cancel();
172 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request,
173 bool* defer) {
176 ///////////////////////////////////////////////////////////////////////////////
177 // URLRequest
179 URLRequest::~URLRequest() {
180 Cancel();
182 if (network_delegate_) {
183 network_delegate_->NotifyURLRequestDestroyed(this);
184 if (job_.get())
185 job_->NotifyURLRequestDestroyed();
188 if (job_.get())
189 OrphanJob();
191 int deleted = context_->url_requests()->erase(this);
192 CHECK_EQ(1, deleted);
194 int net_error = OK;
195 // Log error only on failure, not cancellation, as even successful requests
196 // are "cancelled" on destruction.
197 if (status_.status() == URLRequestStatus::FAILED)
198 net_error = status_.error();
199 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
202 void URLRequest::EnableChunkedUpload() {
203 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked());
204 if (!upload_data_stream_) {
205 upload_chunked_data_stream_ = new ChunkedUploadDataStream(0);
206 upload_data_stream_.reset(upload_chunked_data_stream_);
210 void URLRequest::AppendChunkToUpload(const char* bytes,
211 int bytes_len,
212 bool is_last_chunk) {
213 DCHECK(upload_data_stream_);
214 DCHECK(upload_data_stream_->is_chunked());
215 upload_chunked_data_stream_->AppendData(bytes, bytes_len, is_last_chunk);
218 void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) {
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::SetExtraRequestHeaderByName(const string& name,
231 const string& value,
232 bool overwrite) {
233 DCHECK(!is_pending_ || is_redirecting_);
234 if (overwrite) {
235 extra_request_headers_.SetHeader(name, value);
236 } else {
237 extra_request_headers_.SetHeaderIfMissing(name, value);
241 void URLRequest::RemoveRequestHeaderByName(const string& name) {
242 DCHECK(!is_pending_ || is_redirecting_);
243 extra_request_headers_.RemoveHeader(name);
246 void URLRequest::SetExtraRequestHeaders(
247 const HttpRequestHeaders& headers) {
248 DCHECK(!is_pending_);
249 extra_request_headers_ = headers;
251 // NOTE: This method will likely become non-trivial once the other setters
252 // for request headers are implemented.
255 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
256 if (!job_.get())
257 return false;
259 return job_->GetFullRequestHeaders(headers);
262 int64 URLRequest::GetTotalReceivedBytes() const {
263 if (!job_.get())
264 return 0;
266 return job_->GetTotalReceivedBytes();
269 LoadStateWithParam URLRequest::GetLoadState() const {
270 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
271 // delegate before it has been started.
272 if (calling_delegate_ || !blocked_by_.empty()) {
273 return LoadStateWithParam(
274 LOAD_STATE_WAITING_FOR_DELEGATE,
275 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
276 base::string16());
278 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
279 base::string16());
282 scoped_ptr<base::Value> URLRequest::GetStateAsValue() const {
283 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
284 dict->SetString("url", original_url().possibly_invalid_spec());
286 if (url_chain_.size() > 1) {
287 scoped_ptr<base::ListValue> list(new base::ListValue());
288 for (const GURL& url : url_chain_) {
289 list->AppendString(url.possibly_invalid_spec());
291 dict->Set("url_chain", list.Pass());
294 dict->SetInteger("load_flags", load_flags_);
296 LoadStateWithParam load_state = GetLoadState();
297 dict->SetInteger("load_state", load_state.state);
298 if (!load_state.param.empty())
299 dict->SetString("load_state_param", load_state.param);
300 if (!blocked_by_.empty())
301 dict->SetString("delegate_info", blocked_by_);
303 dict->SetString("method", method_);
304 dict->SetBoolean("has_upload", has_upload());
305 dict->SetBoolean("is_pending", is_pending_);
307 // Add the status of the request. The status should always be IO_PENDING, and
308 // the error should always be OK, unless something is holding onto a request
309 // that has finished or a request was leaked. Neither of these should happen.
310 switch (status_.status()) {
311 case URLRequestStatus::SUCCESS:
312 dict->SetString("status", "SUCCESS");
313 break;
314 case URLRequestStatus::IO_PENDING:
315 dict->SetString("status", "IO_PENDING");
316 break;
317 case URLRequestStatus::CANCELED:
318 dict->SetString("status", "CANCELED");
319 break;
320 case URLRequestStatus::FAILED:
321 dict->SetString("status", "FAILED");
322 break;
324 if (status_.error() != OK)
325 dict->SetInteger("net_error", status_.error());
326 return dict.Pass();
329 void URLRequest::LogBlockedBy(const char* blocked_by) {
330 DCHECK(blocked_by);
331 DCHECK_GT(strlen(blocked_by), 0u);
333 // Only log information to NetLog during startup and certain deferring calls
334 // to delegates. For all reads but the first, do nothing.
335 if (!calling_delegate_ && !response_info_.request_time.is_null())
336 return;
338 LogUnblocked();
339 blocked_by_ = blocked_by;
340 use_blocked_by_as_load_param_ = false;
342 net_log_.BeginEvent(
343 NetLog::TYPE_DELEGATE_INFO,
344 NetLog::StringCallback("delegate_info", &blocked_by_));
347 void URLRequest::LogAndReportBlockedBy(const char* source) {
348 LogBlockedBy(source);
349 use_blocked_by_as_load_param_ = true;
352 void URLRequest::LogUnblocked() {
353 if (blocked_by_.empty())
354 return;
356 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
357 blocked_by_.clear();
360 UploadProgress URLRequest::GetUploadProgress() const {
361 if (!job_.get()) {
362 // We haven't started or the request was cancelled
363 return UploadProgress();
365 if (final_upload_progress_.position()) {
366 // The first job completed and none of the subsequent series of
367 // GETs when following redirects will upload anything, so we return the
368 // cached results from the initial job, the POST.
369 return final_upload_progress_;
371 return job_->GetUploadProgress();
374 void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
375 DCHECK(value);
376 if (response_info_.headers.get()) {
377 response_info_.headers->GetNormalizedHeader(name, value);
378 } else {
379 value->clear();
383 HostPortPair URLRequest::GetSocketAddress() const {
384 DCHECK(job_.get());
385 return job_->GetSocketAddress();
388 HttpResponseHeaders* URLRequest::response_headers() const {
389 return response_info_.headers.get();
392 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
393 *load_timing_info = load_timing_info_;
396 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
397 DCHECK(job_.get());
398 return job_->GetResponseCookies(cookies);
401 void URLRequest::GetMimeType(string* mime_type) const {
402 DCHECK(job_.get());
403 job_->GetMimeType(mime_type);
406 void URLRequest::GetCharset(string* charset) const {
407 DCHECK(job_.get());
408 job_->GetCharset(charset);
411 int URLRequest::GetResponseCode() const {
412 DCHECK(job_.get());
413 return job_->GetResponseCode();
416 void URLRequest::SetLoadFlags(int flags) {
417 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
418 DCHECK(!job_.get());
419 DCHECK(flags & LOAD_IGNORE_LIMITS);
420 DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
422 load_flags_ = flags;
424 // This should be a no-op given the above DCHECKs, but do this
425 // anyway for release mode.
426 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
427 SetPriority(MAXIMUM_PRIORITY);
430 // static
431 void URLRequest::SetDefaultCookiePolicyToBlock() {
432 CHECK(!g_url_requests_started);
433 g_default_can_use_cookies = false;
436 // static
437 bool URLRequest::IsHandledProtocol(const std::string& scheme) {
438 return URLRequestJobManager::SupportsScheme(scheme);
441 // static
442 bool URLRequest::IsHandledURL(const GURL& url) {
443 if (!url.is_valid()) {
444 // We handle error cases.
445 return true;
448 return IsHandledProtocol(url.scheme());
451 void URLRequest::set_first_party_for_cookies(
452 const GURL& first_party_for_cookies) {
453 DCHECK(!is_pending_);
454 first_party_for_cookies_ = first_party_for_cookies;
457 void URLRequest::set_first_party_url_policy(
458 FirstPartyURLPolicy first_party_url_policy) {
459 DCHECK(!is_pending_);
460 first_party_url_policy_ = first_party_url_policy;
463 void URLRequest::set_method(const std::string& method) {
464 DCHECK(!is_pending_);
465 method_ = method;
468 void URLRequest::SetReferrer(const std::string& referrer) {
469 DCHECK(!is_pending_);
470 GURL referrer_url(referrer);
471 if (referrer_url.is_valid()) {
472 referrer_ = referrer_url.GetAsReferrer().spec();
473 } else {
474 referrer_ = referrer;
478 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
479 DCHECK(!is_pending_);
480 referrer_policy_ = referrer_policy;
483 void URLRequest::set_delegate(Delegate* delegate) {
484 delegate_ = delegate;
487 void URLRequest::Start() {
488 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456327 is fixed.
489 tracked_objects::ScopedTracker tracking_profile(
490 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start"));
492 // Some values can be NULL, but the job factory must not be.
493 DCHECK(context_->job_factory());
495 // Anything that sets |blocked_by_| before start should have cleaned up after
496 // itself.
497 DCHECK(blocked_by_.empty());
499 g_url_requests_started = true;
500 response_info_.request_time = base::Time::Now();
502 load_timing_info_ = LoadTimingInfo();
503 load_timing_info_.request_start_time = response_info_.request_time;
504 load_timing_info_.request_start = base::TimeTicks::Now();
506 // Only notify the delegate for the initial request.
507 if (network_delegate_) {
508 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
509 tracked_objects::ScopedTracker tracking_profile25(
510 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start 2.5"));
512 OnCallToDelegate();
513 int error = network_delegate_->NotifyBeforeURLRequest(
514 this, before_request_callback_, &delegate_redirect_url_);
515 // If ERR_IO_PENDING is returned, the delegate will invoke
516 // |before_request_callback_| later.
517 if (error != ERR_IO_PENDING)
518 BeforeRequestComplete(error);
519 return;
522 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
523 tracked_objects::ScopedTracker tracking_profile2(
524 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start 2"));
526 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
527 this, network_delegate_));
530 ///////////////////////////////////////////////////////////////////////////////
532 URLRequest::URLRequest(const GURL& url,
533 RequestPriority priority,
534 Delegate* delegate,
535 const URLRequestContext* context,
536 NetworkDelegate* network_delegate)
537 : context_(context),
538 network_delegate_(network_delegate ? network_delegate
539 : context->network_delegate()),
540 net_log_(
541 BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST)),
542 url_chain_(1, url),
543 method_("GET"),
544 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
545 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
546 load_flags_(LOAD_NORMAL),
547 delegate_(delegate),
548 is_pending_(false),
549 is_redirecting_(false),
550 redirect_limit_(kMaxRedirects),
551 priority_(priority),
552 identifier_(GenerateURLRequestIdentifier()),
553 calling_delegate_(false),
554 use_blocked_by_as_load_param_(false),
555 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
556 base::Unretained(this))),
557 has_notified_completion_(false),
558 received_response_content_length_(0),
559 creation_time_(base::TimeTicks::Now()),
560 notified_before_network_start_(false),
561 // TODO(mmenke): Remove this after we figure out current causes of
562 // http://crbug.com/498289.
563 stack_trace_(new base::debug::StackTrace()) {
564 // Sanity check out environment.
565 DCHECK(base::MessageLoop::current())
566 << "The current base::MessageLoop must exist";
568 context->url_requests()->insert(this);
569 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
572 void URLRequest::BeforeRequestComplete(int error) {
573 DCHECK(!job_.get());
574 DCHECK_NE(ERR_IO_PENDING, error);
576 // Check that there are no callbacks to already canceled requests.
577 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
579 OnCallToDelegateComplete();
581 if (error != OK) {
582 std::string source("delegate");
583 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
584 NetLog::StringCallback("source", &source));
585 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
586 } else if (!delegate_redirect_url_.is_empty()) {
587 GURL new_url;
588 new_url.Swap(&delegate_redirect_url_);
590 URLRequestRedirectJob* job = new URLRequestRedirectJob(
591 this, network_delegate_, new_url,
592 // Use status code 307 to preserve the method, so POST requests work.
593 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
594 StartJob(job);
595 } else {
596 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
597 this, network_delegate_));
601 void URLRequest::StartJob(URLRequestJob* job) {
602 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
603 tracked_objects::ScopedTracker tracking_profile(
604 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::StartJob"));
606 DCHECK(!is_pending_);
607 DCHECK(!job_.get());
609 net_log_.BeginEvent(
610 NetLog::TYPE_URL_REQUEST_START_JOB,
611 base::Bind(&NetLogURLRequestStartCallback,
612 &url(), &method_, load_flags_, priority_,
613 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
615 job_ = job;
616 job_->SetExtraRequestHeaders(extra_request_headers_);
617 job_->SetPriority(priority_);
619 if (upload_data_stream_.get())
620 job_->SetUpload(upload_data_stream_.get());
622 is_pending_ = true;
623 is_redirecting_ = false;
625 response_info_.was_cached = false;
627 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect(
628 referrer_policy_, referrer_, url())) {
629 if (!network_delegate_ ||
630 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
631 *this, url(), GURL(referrer_))) {
632 referrer_.clear();
633 } else {
634 // We need to clear the referrer anyway to avoid an infinite recursion
635 // when starting the error job.
636 referrer_.clear();
637 std::string source("delegate");
638 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
639 NetLog::StringCallback("source", &source));
640 RestartWithJob(new URLRequestErrorJob(
641 this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
642 return;
646 // Don't allow errors to be sent from within Start().
647 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
648 // we probably don't want this: they should be sent asynchronously so
649 // the caller does not get reentered.
650 job_->Start();
653 void URLRequest::Restart() {
654 // Should only be called if the original job didn't make any progress.
655 DCHECK(job_.get() && !job_->has_response_started());
656 RestartWithJob(
657 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
660 void URLRequest::RestartWithJob(URLRequestJob *job) {
661 DCHECK(job->request() == this);
662 PrepareToRestart();
663 StartJob(job);
666 void URLRequest::Cancel() {
667 DoCancel(ERR_ABORTED, SSLInfo());
670 void URLRequest::CancelWithError(int error) {
671 DoCancel(error, SSLInfo());
674 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
675 // This should only be called on a started request.
676 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
677 NOTREACHED();
678 return;
680 DoCancel(error, ssl_info);
683 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
684 DCHECK(error < 0);
685 // If cancelled while calling a delegate, clear delegate info.
686 if (calling_delegate_) {
687 LogUnblocked();
688 OnCallToDelegateComplete();
691 // If the URL request already has an error status, then canceling is a no-op.
692 // Plus, we don't want to change the error status once it has been set.
693 if (status_.is_success()) {
694 status_ = URLRequestStatus(URLRequestStatus::CANCELED, error);
695 response_info_.ssl_info = ssl_info;
697 // If the request hasn't already been completed, log a cancellation event.
698 if (!has_notified_completion_) {
699 // Don't log an error code on ERR_ABORTED, since that's redundant.
700 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
701 error == ERR_ABORTED ? OK : error);
705 if (is_pending_ && job_.get())
706 job_->Kill();
708 // We need to notify about the end of this job here synchronously. The
709 // Job sends an asynchronous notification but by the time this is processed,
710 // our |context_| is NULL.
711 NotifyRequestCompleted();
713 // The Job will call our NotifyDone method asynchronously. This is done so
714 // that the Delegate implementation can call Cancel without having to worry
715 // about being called recursively.
718 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
719 DCHECK(job_.get());
720 DCHECK(bytes_read);
721 *bytes_read = 0;
723 // If this is the first read, end the delegate call that may have started in
724 // OnResponseStarted.
725 OnCallToDelegateComplete();
727 // This handles a cancel that happens while paused.
728 // TODO(ahendrickson): DCHECK() that it is not done after
729 // http://crbug.com/115705 is fixed.
730 if (job_->is_done())
731 return false;
733 if (dest_size == 0) {
734 // Caller is not too bright. I guess we've done what they asked.
735 return true;
738 // Once the request fails or is cancelled, read will just return 0 bytes
739 // to indicate end of stream.
740 if (!status_.is_success()) {
741 return true;
744 bool rv = job_->Read(dest, dest_size, bytes_read);
745 // If rv is false, the status cannot be success.
746 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
748 if (rv && *bytes_read <= 0 && status_.is_success())
749 NotifyRequestCompleted();
750 return rv;
753 void URLRequest::StopCaching() {
754 DCHECK(job_.get());
755 job_->StopCaching();
758 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
759 bool* defer_redirect) {
760 is_redirecting_ = true;
762 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
763 URLRequestJob* job =
764 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
765 this, network_delegate_, redirect_info.new_url);
766 if (job) {
767 RestartWithJob(job);
768 } else if (delegate_) {
769 OnCallToDelegate();
770 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
771 // |this| may be have been destroyed here.
775 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
776 if (delegate_ && !notified_before_network_start_) {
777 OnCallToDelegate();
778 delegate_->OnBeforeNetworkStart(this, defer);
779 if (!*defer)
780 OnCallToDelegateComplete();
781 notified_before_network_start_ = true;
785 void URLRequest::ResumeNetworkStart() {
786 DCHECK(job_.get());
787 DCHECK(notified_before_network_start_);
789 OnCallToDelegateComplete();
790 job_->ResumeNetworkStart();
793 void URLRequest::NotifyResponseStarted() {
794 int net_error = OK;
795 if (!status_.is_success())
796 net_error = status_.error();
797 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
798 net_error);
800 URLRequestJob* job =
801 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
802 this, network_delegate_);
803 if (job) {
804 RestartWithJob(job);
805 } else {
806 if (delegate_) {
807 // In some cases (e.g. an event was canceled), we might have sent the
808 // completion event and receive a NotifyResponseStarted() later.
809 if (!has_notified_completion_ && status_.is_success()) {
810 if (network_delegate_)
811 network_delegate_->NotifyResponseStarted(this);
814 // Notify in case the entire URL Request has been finished.
815 if (!has_notified_completion_ && !status_.is_success())
816 NotifyRequestCompleted();
818 OnCallToDelegate();
819 delegate_->OnResponseStarted(this);
820 // Nothing may appear below this line as OnResponseStarted may delete
821 // |this|.
826 void URLRequest::FollowDeferredRedirect() {
827 CHECK(job_.get());
828 CHECK(status_.is_success());
830 job_->FollowDeferredRedirect();
833 void URLRequest::SetAuth(const AuthCredentials& credentials) {
834 DCHECK(job_.get());
835 DCHECK(job_->NeedsAuth());
837 job_->SetAuth(credentials);
840 void URLRequest::CancelAuth() {
841 DCHECK(job_.get());
842 DCHECK(job_->NeedsAuth());
844 job_->CancelAuth();
847 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
848 DCHECK(job_.get());
850 job_->ContinueWithCertificate(client_cert);
853 void URLRequest::ContinueDespiteLastError() {
854 DCHECK(job_.get());
856 job_->ContinueDespiteLastError();
859 void URLRequest::PrepareToRestart() {
860 DCHECK(job_.get());
862 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
863 // one.
864 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
866 OrphanJob();
868 response_info_ = HttpResponseInfo();
869 response_info_.request_time = base::Time::Now();
871 load_timing_info_ = LoadTimingInfo();
872 load_timing_info_.request_start_time = response_info_.request_time;
873 load_timing_info_.request_start = base::TimeTicks::Now();
875 status_ = URLRequestStatus();
876 is_pending_ = false;
877 proxy_server_ = HostPortPair();
880 void URLRequest::OrphanJob() {
881 // When calling this function, please check that URLRequestHttpJob is
882 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
883 // the call back. This is currently guaranteed by the following strategies:
884 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
885 // be receiving any headers at that time.
886 // - OrphanJob is called in ~URLRequest, in this case
887 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
888 // that the callback becomes invalid.
889 job_->Kill();
890 job_->DetachRequest(); // ensures that the job will not call us again
891 job_ = NULL;
894 int URLRequest::Redirect(const RedirectInfo& redirect_info) {
895 // Matches call in NotifyReceivedRedirect.
896 OnCallToDelegateComplete();
897 if (net_log_.IsCapturing()) {
898 net_log_.AddEvent(
899 NetLog::TYPE_URL_REQUEST_REDIRECTED,
900 NetLog::StringCallback("location",
901 &redirect_info.new_url.possibly_invalid_spec()));
904 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
905 if (network_delegate_)
906 network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url);
908 if (redirect_limit_ <= 0) {
909 DVLOG(1) << "disallowing redirect: exceeds limit";
910 return ERR_TOO_MANY_REDIRECTS;
913 if (!redirect_info.new_url.is_valid())
914 return ERR_INVALID_URL;
916 if (!job_->IsSafeRedirect(redirect_info.new_url)) {
917 DVLOG(1) << "disallowing redirect: unsafe protocol";
918 return ERR_UNSAFE_REDIRECT;
921 if (!final_upload_progress_.position())
922 final_upload_progress_ = job_->GetUploadProgress();
923 PrepareToRestart();
925 if (redirect_info.new_method != method_) {
926 // TODO(davidben): This logic still needs to be replicated at the consumers.
927 if (method_ == "POST") {
928 // If being switched from POST, must remove headers that were specific to
929 // the POST and don't have meaning in other methods. For example the
930 // inclusion of a multipart Content-Type header in GET can cause problems
931 // with some servers:
932 // http://code.google.com/p/chromium/issues/detail?id=843
933 StripPostSpecificHeaders(&extra_request_headers_);
935 upload_data_stream_.reset();
936 method_ = redirect_info.new_method;
939 // Cross-origin redirects should not result in an Origin header value that is
940 // equal to the original request's Origin header. This is necessary to prevent
941 // a reflection of POST requests to bypass CSRF protections. If the header was
942 // not set to "null", a POST request from origin A to a malicious origin M
943 // could be redirected by M back to A.
945 // In the Section 4.2, Step 4.10 of the Fetch spec
946 // (https://fetch.spec.whatwg.org/#concept-http-fetch), it states that on
947 // cross-origin 301, 302, 303, 307, and 308 redirects, the user agent should
948 // set the request's origin to an "opaque identifier," which serializes to
949 // "null." This matches Firefox and IE behavior, although it supercedes the
950 // suggested behavior in RFC 6454, "The Web Origin Concept."
952 // See also https://crbug.com/465517.
954 // TODO(jww): This is probably layering violation and should be refactored
955 // into //content. See https://crbug.com/471397.
956 if (redirect_info.new_url.GetOrigin() != url().GetOrigin() &&
957 extra_request_headers_.HasHeader(HttpRequestHeaders::kOrigin)) {
958 extra_request_headers_.SetHeader(HttpRequestHeaders::kOrigin,
959 url::Origin().string());
962 referrer_ = redirect_info.new_referrer;
963 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies;
965 url_chain_.push_back(redirect_info.new_url);
966 --redirect_limit_;
968 Start();
969 return OK;
972 const URLRequestContext* URLRequest::context() const {
973 return context_;
976 int64 URLRequest::GetExpectedContentSize() const {
977 int64 expected_content_size = -1;
978 if (job_.get())
979 expected_content_size = job_->expected_content_size();
981 return expected_content_size;
984 void URLRequest::SetPriority(RequestPriority priority) {
985 DCHECK_GE(priority, MINIMUM_PRIORITY);
986 DCHECK_LE(priority, MAXIMUM_PRIORITY);
988 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
989 NOTREACHED();
990 // Maintain the invariant that requests with IGNORE_LIMITS set
991 // have MAXIMUM_PRIORITY for release mode.
992 return;
995 if (priority_ == priority)
996 return;
998 priority_ = priority;
999 if (job_.get()) {
1000 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
1001 NetLog::IntegerCallback("priority", priority_));
1002 job_->SetPriority(priority_);
1006 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
1007 const GURL& url = this->url();
1008 bool scheme_is_http = url.SchemeIs("http");
1009 if (!scheme_is_http && !url.SchemeIs("ws"))
1010 return false;
1011 TransportSecurityState* state = context()->transport_security_state();
1012 if (state && state->ShouldUpgradeToSSL(url.host())) {
1013 GURL::Replacements replacements;
1014 const char* new_scheme = scheme_is_http ? "https" : "wss";
1015 replacements.SetSchemeStr(new_scheme);
1016 *redirect_url = url.ReplaceComponents(replacements);
1017 return true;
1019 return false;
1022 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1023 NetworkDelegate::AuthRequiredResponse rv =
1024 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1025 auth_info_ = auth_info;
1026 if (network_delegate_) {
1027 OnCallToDelegate();
1028 rv = network_delegate_->NotifyAuthRequired(
1029 this,
1030 *auth_info,
1031 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1032 base::Unretained(this)),
1033 &auth_credentials_);
1034 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1035 return;
1038 NotifyAuthRequiredComplete(rv);
1041 void URLRequest::NotifyAuthRequiredComplete(
1042 NetworkDelegate::AuthRequiredResponse result) {
1043 OnCallToDelegateComplete();
1045 // Check that there are no callbacks to already canceled requests.
1046 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1048 // NotifyAuthRequired may be called multiple times, such as
1049 // when an authentication attempt fails. Clear out the data
1050 // so it can be reset on another round.
1051 AuthCredentials credentials = auth_credentials_;
1052 auth_credentials_ = AuthCredentials();
1053 scoped_refptr<AuthChallengeInfo> auth_info;
1054 auth_info.swap(auth_info_);
1056 switch (result) {
1057 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1058 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1059 // didn't take an action.
1060 if (delegate_)
1061 delegate_->OnAuthRequired(this, auth_info.get());
1062 break;
1064 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1065 SetAuth(credentials);
1066 break;
1068 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1069 CancelAuth();
1070 break;
1072 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1073 NOTREACHED();
1074 break;
1078 void URLRequest::NotifyCertificateRequested(
1079 SSLCertRequestInfo* cert_request_info) {
1080 if (delegate_)
1081 delegate_->OnCertificateRequested(this, cert_request_info);
1084 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1085 bool fatal) {
1086 if (delegate_)
1087 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1090 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1091 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1092 if (network_delegate_) {
1093 return network_delegate_->CanGetCookies(*this, cookie_list);
1095 return g_default_can_use_cookies;
1098 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1099 CookieOptions* options) const {
1100 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1101 if (network_delegate_) {
1102 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1104 return g_default_can_use_cookies;
1107 bool URLRequest::CanEnablePrivacyMode() const {
1108 if (network_delegate_) {
1109 return network_delegate_->CanEnablePrivacyMode(url(),
1110 first_party_for_cookies_);
1112 return !g_default_can_use_cookies;
1116 void URLRequest::NotifyReadCompleted(int bytes_read) {
1117 // Notify in case the entire URL Request has been finished.
1118 if (bytes_read <= 0)
1119 NotifyRequestCompleted();
1121 // Notify NetworkChangeNotifier that we just received network data.
1122 // This is to identify cases where the NetworkChangeNotifier thinks we
1123 // are off-line but we are still receiving network data (crbug.com/124069),
1124 // and to get rough network connection measurements.
1125 if (bytes_read > 0 && !was_cached())
1126 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1128 if (delegate_)
1129 delegate_->OnReadCompleted(this, bytes_read);
1131 // Nothing below this line as OnReadCompleted may delete |this|.
1134 void URLRequest::OnHeadersComplete() {
1135 // Cache load timing information now, as information will be lost once the
1136 // socket is closed and the ClientSocketHandle is Reset, which will happen
1137 // once the body is complete. The start times should already be populated.
1138 if (job_.get()) {
1139 // Keep a copy of the two times the URLRequest sets.
1140 base::TimeTicks request_start = load_timing_info_.request_start;
1141 base::Time request_start_time = load_timing_info_.request_start_time;
1143 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1144 // consistent place to start from.
1145 load_timing_info_ = LoadTimingInfo();
1146 job_->GetLoadTimingInfo(&load_timing_info_);
1148 load_timing_info_.request_start = request_start;
1149 load_timing_info_.request_start_time = request_start_time;
1151 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1155 void URLRequest::NotifyRequestCompleted() {
1156 // TODO(battre): Get rid of this check, according to willchan it should
1157 // not be needed.
1158 if (has_notified_completion_)
1159 return;
1161 is_pending_ = false;
1162 is_redirecting_ = false;
1163 has_notified_completion_ = true;
1164 if (network_delegate_)
1165 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1168 void URLRequest::OnCallToDelegate() {
1169 DCHECK(!calling_delegate_);
1170 DCHECK(blocked_by_.empty());
1171 calling_delegate_ = true;
1172 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1175 void URLRequest::OnCallToDelegateComplete() {
1176 // This should have been cleared before resuming the request.
1177 DCHECK(blocked_by_.empty());
1178 if (!calling_delegate_)
1179 return;
1180 calling_delegate_ = false;
1181 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1184 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1185 stack_trace_.reset(new base::debug::StackTrace(stack_trace));
1188 const base::debug::StackTrace* URLRequest::stack_trace() const {
1189 return stack_trace_.get();
1192 void URLRequest::GetConnectionAttempts(ConnectionAttempts* out) const {
1193 if (job_)
1194 job_->GetConnectionAttempts(out);
1195 else
1196 out->clear();
1199 } // namespace net