Revert of Revert of Use BoringSSL in the implementation of ClearKey for Chromecast...
[chromium-blink-merge.git] / net / url_request / url_request.cc
blob7496bb3914489c59be088f643db92a61a1b2562a
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 // Sanity check out environment.
562 DCHECK(base::MessageLoop::current())
563 << "The current base::MessageLoop must exist";
565 context->url_requests()->insert(this);
566 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
569 void URLRequest::BeforeRequestComplete(int error) {
570 DCHECK(!job_.get());
571 DCHECK_NE(ERR_IO_PENDING, error);
573 // Check that there are no callbacks to already canceled requests.
574 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
576 OnCallToDelegateComplete();
578 if (error != OK) {
579 std::string source("delegate");
580 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
581 NetLog::StringCallback("source", &source));
582 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
583 } else if (!delegate_redirect_url_.is_empty()) {
584 GURL new_url;
585 new_url.Swap(&delegate_redirect_url_);
587 URLRequestRedirectJob* job = new URLRequestRedirectJob(
588 this, network_delegate_, new_url,
589 // Use status code 307 to preserve the method, so POST requests work.
590 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
591 StartJob(job);
592 } else {
593 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
594 this, network_delegate_));
598 void URLRequest::StartJob(URLRequestJob* job) {
599 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
600 tracked_objects::ScopedTracker tracking_profile(
601 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::StartJob"));
603 DCHECK(!is_pending_);
604 DCHECK(!job_.get());
606 net_log_.BeginEvent(
607 NetLog::TYPE_URL_REQUEST_START_JOB,
608 base::Bind(&NetLogURLRequestStartCallback,
609 &url(), &method_, load_flags_, priority_,
610 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
612 job_ = job;
613 job_->SetExtraRequestHeaders(extra_request_headers_);
614 job_->SetPriority(priority_);
616 if (upload_data_stream_.get())
617 job_->SetUpload(upload_data_stream_.get());
619 is_pending_ = true;
620 is_redirecting_ = false;
622 response_info_.was_cached = false;
624 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect(
625 referrer_policy_, referrer_, url())) {
626 if (!network_delegate_ ||
627 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
628 *this, url(), GURL(referrer_))) {
629 referrer_.clear();
630 } else {
631 // We need to clear the referrer anyway to avoid an infinite recursion
632 // when starting the error job.
633 referrer_.clear();
634 std::string source("delegate");
635 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
636 NetLog::StringCallback("source", &source));
637 RestartWithJob(new URLRequestErrorJob(
638 this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
639 return;
643 // Don't allow errors to be sent from within Start().
644 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
645 // we probably don't want this: they should be sent asynchronously so
646 // the caller does not get reentered.
647 job_->Start();
650 void URLRequest::Restart() {
651 // Should only be called if the original job didn't make any progress.
652 DCHECK(job_.get() && !job_->has_response_started());
653 RestartWithJob(
654 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
657 void URLRequest::RestartWithJob(URLRequestJob *job) {
658 DCHECK(job->request() == this);
659 PrepareToRestart();
660 StartJob(job);
663 void URLRequest::Cancel() {
664 DoCancel(ERR_ABORTED, SSLInfo());
667 void URLRequest::CancelWithError(int error) {
668 DoCancel(error, SSLInfo());
671 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
672 // This should only be called on a started request.
673 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
674 NOTREACHED();
675 return;
677 DoCancel(error, ssl_info);
680 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
681 DCHECK(error < 0);
682 // If cancelled while calling a delegate, clear delegate info.
683 if (calling_delegate_) {
684 LogUnblocked();
685 OnCallToDelegateComplete();
688 // If the URL request already has an error status, then canceling is a no-op.
689 // Plus, we don't want to change the error status once it has been set.
690 if (status_.is_success()) {
691 status_.set_status(URLRequestStatus::CANCELED);
692 status_.set_error(error);
693 response_info_.ssl_info = ssl_info;
695 // If the request hasn't already been completed, log a cancellation event.
696 if (!has_notified_completion_) {
697 // Don't log an error code on ERR_ABORTED, since that's redundant.
698 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
699 error == ERR_ABORTED ? OK : error);
703 if (is_pending_ && job_.get())
704 job_->Kill();
706 // We need to notify about the end of this job here synchronously. The
707 // Job sends an asynchronous notification but by the time this is processed,
708 // our |context_| is NULL.
709 NotifyRequestCompleted();
711 // The Job will call our NotifyDone method asynchronously. This is done so
712 // that the Delegate implementation can call Cancel without having to worry
713 // about being called recursively.
716 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
717 DCHECK(job_.get());
718 DCHECK(bytes_read);
719 *bytes_read = 0;
721 // If this is the first read, end the delegate call that may have started in
722 // OnResponseStarted.
723 OnCallToDelegateComplete();
725 // This handles a cancel that happens while paused.
726 // TODO(ahendrickson): DCHECK() that it is not done after
727 // http://crbug.com/115705 is fixed.
728 if (job_->is_done())
729 return false;
731 if (dest_size == 0) {
732 // Caller is not too bright. I guess we've done what they asked.
733 return true;
736 // Once the request fails or is cancelled, read will just return 0 bytes
737 // to indicate end of stream.
738 if (!status_.is_success()) {
739 return true;
742 bool rv = job_->Read(dest, dest_size, bytes_read);
743 // If rv is false, the status cannot be success.
744 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
746 if (rv && *bytes_read <= 0 && status_.is_success())
747 NotifyRequestCompleted();
748 return rv;
751 void URLRequest::StopCaching() {
752 DCHECK(job_.get());
753 job_->StopCaching();
756 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
757 bool* defer_redirect) {
758 is_redirecting_ = true;
760 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
761 URLRequestJob* job =
762 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
763 this, network_delegate_, redirect_info.new_url);
764 if (job) {
765 RestartWithJob(job);
766 } else if (delegate_) {
767 OnCallToDelegate();
768 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
769 // |this| may be have been destroyed here.
773 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
774 if (delegate_ && !notified_before_network_start_) {
775 OnCallToDelegate();
776 delegate_->OnBeforeNetworkStart(this, defer);
777 if (!*defer)
778 OnCallToDelegateComplete();
779 notified_before_network_start_ = true;
783 void URLRequest::ResumeNetworkStart() {
784 DCHECK(job_.get());
785 DCHECK(notified_before_network_start_);
787 OnCallToDelegateComplete();
788 job_->ResumeNetworkStart();
791 void URLRequest::NotifyResponseStarted() {
792 int net_error = OK;
793 if (!status_.is_success())
794 net_error = status_.error();
795 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
796 net_error);
798 URLRequestJob* job =
799 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
800 this, network_delegate_);
801 if (job) {
802 RestartWithJob(job);
803 } else {
804 if (delegate_) {
805 // In some cases (e.g. an event was canceled), we might have sent the
806 // completion event and receive a NotifyResponseStarted() later.
807 if (!has_notified_completion_ && status_.is_success()) {
808 if (network_delegate_)
809 network_delegate_->NotifyResponseStarted(this);
812 // Notify in case the entire URL Request has been finished.
813 if (!has_notified_completion_ && !status_.is_success())
814 NotifyRequestCompleted();
816 OnCallToDelegate();
817 delegate_->OnResponseStarted(this);
818 // Nothing may appear below this line as OnResponseStarted may delete
819 // |this|.
824 void URLRequest::FollowDeferredRedirect() {
825 CHECK(job_.get());
826 CHECK(status_.is_success());
828 job_->FollowDeferredRedirect();
831 void URLRequest::SetAuth(const AuthCredentials& credentials) {
832 DCHECK(job_.get());
833 DCHECK(job_->NeedsAuth());
835 job_->SetAuth(credentials);
838 void URLRequest::CancelAuth() {
839 DCHECK(job_.get());
840 DCHECK(job_->NeedsAuth());
842 job_->CancelAuth();
845 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
846 DCHECK(job_.get());
848 job_->ContinueWithCertificate(client_cert);
851 void URLRequest::ContinueDespiteLastError() {
852 DCHECK(job_.get());
854 job_->ContinueDespiteLastError();
857 void URLRequest::PrepareToRestart() {
858 DCHECK(job_.get());
860 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
861 // one.
862 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
864 OrphanJob();
866 response_info_ = HttpResponseInfo();
867 response_info_.request_time = base::Time::Now();
869 load_timing_info_ = LoadTimingInfo();
870 load_timing_info_.request_start_time = response_info_.request_time;
871 load_timing_info_.request_start = base::TimeTicks::Now();
873 status_ = URLRequestStatus();
874 is_pending_ = false;
877 void URLRequest::OrphanJob() {
878 // When calling this function, please check that URLRequestHttpJob is
879 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
880 // the call back. This is currently guaranteed by the following strategies:
881 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
882 // be receiving any headers at that time.
883 // - OrphanJob is called in ~URLRequest, in this case
884 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
885 // that the callback becomes invalid.
886 job_->Kill();
887 job_->DetachRequest(); // ensures that the job will not call us again
888 job_ = NULL;
891 int URLRequest::Redirect(const RedirectInfo& redirect_info) {
892 // Matches call in NotifyReceivedRedirect.
893 OnCallToDelegateComplete();
894 if (net_log_.IsCapturing()) {
895 net_log_.AddEvent(
896 NetLog::TYPE_URL_REQUEST_REDIRECTED,
897 NetLog::StringCallback("location",
898 &redirect_info.new_url.possibly_invalid_spec()));
901 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
902 if (network_delegate_)
903 network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url);
905 if (redirect_limit_ <= 0) {
906 DVLOG(1) << "disallowing redirect: exceeds limit";
907 return ERR_TOO_MANY_REDIRECTS;
910 if (!redirect_info.new_url.is_valid())
911 return ERR_INVALID_URL;
913 if (!job_->IsSafeRedirect(redirect_info.new_url)) {
914 DVLOG(1) << "disallowing redirect: unsafe protocol";
915 return ERR_UNSAFE_REDIRECT;
918 if (!final_upload_progress_.position())
919 final_upload_progress_ = job_->GetUploadProgress();
920 PrepareToRestart();
922 if (redirect_info.new_method != method_) {
923 // TODO(davidben): This logic still needs to be replicated at the consumers.
924 if (method_ == "POST") {
925 // If being switched from POST, must remove headers that were specific to
926 // the POST and don't have meaning in other methods. For example the
927 // inclusion of a multipart Content-Type header in GET can cause problems
928 // with some servers:
929 // http://code.google.com/p/chromium/issues/detail?id=843
930 StripPostSpecificHeaders(&extra_request_headers_);
932 upload_data_stream_.reset();
933 method_ = redirect_info.new_method;
936 // Cross-origin redirects should not result in an Origin header value that is
937 // equal to the original request's Origin header. This is necessary to prevent
938 // a reflection of POST requests to bypass CSRF protections. If the header was
939 // not set to "null", a POST request from origin A to a malicious origin M
940 // could be redirected by M back to A.
942 // In the Section 4.2, Step 4.10 of the Fetch spec
943 // (https://fetch.spec.whatwg.org/#concept-http-fetch), it states that on
944 // cross-origin 301, 302, 303, 307, and 308 redirects, the user agent should
945 // set the request's origin to an "opaque identifier," which serializes to
946 // "null." This matches Firefox and IE behavior, although it supercedes the
947 // suggested behavior in RFC 6454, "The Web Origin Concept."
949 // See also https://crbug.com/465517.
951 // TODO(jww): This is probably layering violation and should be refactored
952 // into //content. See https://crbug.com/471397.
953 if (redirect_info.new_url.GetOrigin() != url().GetOrigin() &&
954 extra_request_headers_.HasHeader(HttpRequestHeaders::kOrigin)) {
955 extra_request_headers_.SetHeader(HttpRequestHeaders::kOrigin,
956 url::Origin().string());
959 referrer_ = redirect_info.new_referrer;
960 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies;
962 url_chain_.push_back(redirect_info.new_url);
963 --redirect_limit_;
965 Start();
966 return OK;
969 const URLRequestContext* URLRequest::context() const {
970 return context_;
973 int64 URLRequest::GetExpectedContentSize() const {
974 int64 expected_content_size = -1;
975 if (job_.get())
976 expected_content_size = job_->expected_content_size();
978 return expected_content_size;
981 void URLRequest::SetPriority(RequestPriority priority) {
982 DCHECK_GE(priority, MINIMUM_PRIORITY);
983 DCHECK_LE(priority, MAXIMUM_PRIORITY);
985 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
986 NOTREACHED();
987 // Maintain the invariant that requests with IGNORE_LIMITS set
988 // have MAXIMUM_PRIORITY for release mode.
989 return;
992 if (priority_ == priority)
993 return;
995 priority_ = priority;
996 if (job_.get()) {
997 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
998 NetLog::IntegerCallback("priority", priority_));
999 job_->SetPriority(priority_);
1003 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
1004 const GURL& url = this->url();
1005 bool scheme_is_http = url.SchemeIs("http");
1006 if (!scheme_is_http && !url.SchemeIs("ws"))
1007 return false;
1008 TransportSecurityState* state = context()->transport_security_state();
1009 if (state && state->ShouldUpgradeToSSL(url.host())) {
1010 GURL::Replacements replacements;
1011 const char* new_scheme = scheme_is_http ? "https" : "wss";
1012 replacements.SetSchemeStr(new_scheme);
1013 *redirect_url = url.ReplaceComponents(replacements);
1014 return true;
1016 return false;
1019 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1020 NetworkDelegate::AuthRequiredResponse rv =
1021 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1022 auth_info_ = auth_info;
1023 if (network_delegate_) {
1024 OnCallToDelegate();
1025 rv = network_delegate_->NotifyAuthRequired(
1026 this,
1027 *auth_info,
1028 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1029 base::Unretained(this)),
1030 &auth_credentials_);
1031 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1032 return;
1035 NotifyAuthRequiredComplete(rv);
1038 void URLRequest::NotifyAuthRequiredComplete(
1039 NetworkDelegate::AuthRequiredResponse result) {
1040 OnCallToDelegateComplete();
1042 // Check that there are no callbacks to already canceled requests.
1043 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1045 // NotifyAuthRequired may be called multiple times, such as
1046 // when an authentication attempt fails. Clear out the data
1047 // so it can be reset on another round.
1048 AuthCredentials credentials = auth_credentials_;
1049 auth_credentials_ = AuthCredentials();
1050 scoped_refptr<AuthChallengeInfo> auth_info;
1051 auth_info.swap(auth_info_);
1053 switch (result) {
1054 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1055 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1056 // didn't take an action.
1057 if (delegate_)
1058 delegate_->OnAuthRequired(this, auth_info.get());
1059 break;
1061 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1062 SetAuth(credentials);
1063 break;
1065 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1066 CancelAuth();
1067 break;
1069 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1070 NOTREACHED();
1071 break;
1075 void URLRequest::NotifyCertificateRequested(
1076 SSLCertRequestInfo* cert_request_info) {
1077 if (delegate_)
1078 delegate_->OnCertificateRequested(this, cert_request_info);
1081 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1082 bool fatal) {
1083 if (delegate_)
1084 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1087 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1088 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1089 if (network_delegate_) {
1090 return network_delegate_->CanGetCookies(*this, cookie_list);
1092 return g_default_can_use_cookies;
1095 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1096 CookieOptions* options) const {
1097 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1098 if (network_delegate_) {
1099 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1101 return g_default_can_use_cookies;
1104 bool URLRequest::CanEnablePrivacyMode() const {
1105 if (network_delegate_) {
1106 return network_delegate_->CanEnablePrivacyMode(url(),
1107 first_party_for_cookies_);
1109 return !g_default_can_use_cookies;
1113 void URLRequest::NotifyReadCompleted(int bytes_read) {
1114 // Notify in case the entire URL Request has been finished.
1115 if (bytes_read <= 0)
1116 NotifyRequestCompleted();
1118 // Notify NetworkChangeNotifier that we just received network data.
1119 // This is to identify cases where the NetworkChangeNotifier thinks we
1120 // are off-line but we are still receiving network data (crbug.com/124069),
1121 // and to get rough network connection measurements.
1122 if (bytes_read > 0 && !was_cached())
1123 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1125 if (delegate_)
1126 delegate_->OnReadCompleted(this, bytes_read);
1128 // Nothing below this line as OnReadCompleted may delete |this|.
1131 void URLRequest::OnHeadersComplete() {
1132 // Cache load timing information now, as information will be lost once the
1133 // socket is closed and the ClientSocketHandle is Reset, which will happen
1134 // once the body is complete. The start times should already be populated.
1135 if (job_.get()) {
1136 // Keep a copy of the two times the URLRequest sets.
1137 base::TimeTicks request_start = load_timing_info_.request_start;
1138 base::Time request_start_time = load_timing_info_.request_start_time;
1140 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1141 // consistent place to start from.
1142 load_timing_info_ = LoadTimingInfo();
1143 job_->GetLoadTimingInfo(&load_timing_info_);
1145 load_timing_info_.request_start = request_start;
1146 load_timing_info_.request_start_time = request_start_time;
1148 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1152 void URLRequest::NotifyRequestCompleted() {
1153 // TODO(battre): Get rid of this check, according to willchan it should
1154 // not be needed.
1155 if (has_notified_completion_)
1156 return;
1158 is_pending_ = false;
1159 is_redirecting_ = false;
1160 has_notified_completion_ = true;
1161 if (network_delegate_)
1162 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1165 void URLRequest::OnCallToDelegate() {
1166 DCHECK(!calling_delegate_);
1167 DCHECK(blocked_by_.empty());
1168 calling_delegate_ = true;
1169 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1172 void URLRequest::OnCallToDelegateComplete() {
1173 // This should have been cleared before resuming the request.
1174 DCHECK(blocked_by_.empty());
1175 if (!calling_delegate_)
1176 return;
1177 calling_delegate_ = false;
1178 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1181 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1182 base::debug::StackTrace* stack_trace_copy =
1183 new base::debug::StackTrace(NULL, 0);
1184 *stack_trace_copy = stack_trace;
1185 stack_trace_.reset(stack_trace_copy);
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