Removed 'anonymous' from namespace, added whitespace in thread_restrictions.cc
[chromium-blink-merge.git] / net / url_request / url_request.cc
blobc576b602ef80c4dff2cc3efd4b5f60c7f155f730
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 int64_t URLRequest::GetTotalSentBytes() const {
270 if (!job_.get())
271 return 0;
273 return job_->GetTotalSentBytes();
276 LoadStateWithParam URLRequest::GetLoadState() const {
277 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
278 // delegate before it has been started.
279 if (calling_delegate_ || !blocked_by_.empty()) {
280 return LoadStateWithParam(
281 LOAD_STATE_WAITING_FOR_DELEGATE,
282 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
283 base::string16());
285 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
286 base::string16());
289 scoped_ptr<base::Value> URLRequest::GetStateAsValue() const {
290 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
291 dict->SetString("url", original_url().possibly_invalid_spec());
293 if (url_chain_.size() > 1) {
294 scoped_ptr<base::ListValue> list(new base::ListValue());
295 for (const GURL& url : url_chain_) {
296 list->AppendString(url.possibly_invalid_spec());
298 dict->Set("url_chain", list.Pass());
301 dict->SetInteger("load_flags", load_flags_);
303 LoadStateWithParam load_state = GetLoadState();
304 dict->SetInteger("load_state", load_state.state);
305 if (!load_state.param.empty())
306 dict->SetString("load_state_param", load_state.param);
307 if (!blocked_by_.empty())
308 dict->SetString("delegate_info", blocked_by_);
310 dict->SetString("method", method_);
311 dict->SetBoolean("has_upload", has_upload());
312 dict->SetBoolean("is_pending", is_pending_);
314 // Add the status of the request. The status should always be IO_PENDING, and
315 // the error should always be OK, unless something is holding onto a request
316 // that has finished or a request was leaked. Neither of these should happen.
317 switch (status_.status()) {
318 case URLRequestStatus::SUCCESS:
319 dict->SetString("status", "SUCCESS");
320 break;
321 case URLRequestStatus::IO_PENDING:
322 dict->SetString("status", "IO_PENDING");
323 break;
324 case URLRequestStatus::CANCELED:
325 dict->SetString("status", "CANCELED");
326 break;
327 case URLRequestStatus::FAILED:
328 dict->SetString("status", "FAILED");
329 break;
331 if (status_.error() != OK)
332 dict->SetInteger("net_error", status_.error());
333 return dict.Pass();
336 void URLRequest::LogBlockedBy(const char* blocked_by) {
337 DCHECK(blocked_by);
338 DCHECK_GT(strlen(blocked_by), 0u);
340 // Only log information to NetLog during startup and certain deferring calls
341 // to delegates. For all reads but the first, do nothing.
342 if (!calling_delegate_ && !response_info_.request_time.is_null())
343 return;
345 LogUnblocked();
346 blocked_by_ = blocked_by;
347 use_blocked_by_as_load_param_ = false;
349 net_log_.BeginEvent(
350 NetLog::TYPE_DELEGATE_INFO,
351 NetLog::StringCallback("delegate_info", &blocked_by_));
354 void URLRequest::LogAndReportBlockedBy(const char* source) {
355 LogBlockedBy(source);
356 use_blocked_by_as_load_param_ = true;
359 void URLRequest::LogUnblocked() {
360 if (blocked_by_.empty())
361 return;
363 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
364 blocked_by_.clear();
367 UploadProgress URLRequest::GetUploadProgress() const {
368 if (!job_.get()) {
369 // We haven't started or the request was cancelled
370 return UploadProgress();
372 if (final_upload_progress_.position()) {
373 // The first job completed and none of the subsequent series of
374 // GETs when following redirects will upload anything, so we return the
375 // cached results from the initial job, the POST.
376 return final_upload_progress_;
378 return job_->GetUploadProgress();
381 void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
382 DCHECK(value);
383 if (response_info_.headers.get()) {
384 response_info_.headers->GetNormalizedHeader(name, value);
385 } else {
386 value->clear();
390 HostPortPair URLRequest::GetSocketAddress() const {
391 DCHECK(job_.get());
392 return job_->GetSocketAddress();
395 HttpResponseHeaders* URLRequest::response_headers() const {
396 return response_info_.headers.get();
399 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
400 *load_timing_info = load_timing_info_;
403 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
404 DCHECK(job_.get());
405 return job_->GetResponseCookies(cookies);
408 void URLRequest::GetMimeType(string* mime_type) const {
409 DCHECK(job_.get());
410 job_->GetMimeType(mime_type);
413 void URLRequest::GetCharset(string* charset) const {
414 DCHECK(job_.get());
415 job_->GetCharset(charset);
418 int URLRequest::GetResponseCode() const {
419 DCHECK(job_.get());
420 return job_->GetResponseCode();
423 void URLRequest::SetLoadFlags(int flags) {
424 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
425 DCHECK(!job_.get());
426 DCHECK(flags & LOAD_IGNORE_LIMITS);
427 DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
429 load_flags_ = flags;
431 // This should be a no-op given the above DCHECKs, but do this
432 // anyway for release mode.
433 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
434 SetPriority(MAXIMUM_PRIORITY);
437 // static
438 void URLRequest::SetDefaultCookiePolicyToBlock() {
439 CHECK(!g_url_requests_started);
440 g_default_can_use_cookies = false;
443 // static
444 bool URLRequest::IsHandledProtocol(const std::string& scheme) {
445 return URLRequestJobManager::SupportsScheme(scheme);
448 // static
449 bool URLRequest::IsHandledURL(const GURL& url) {
450 if (!url.is_valid()) {
451 // We handle error cases.
452 return true;
455 return IsHandledProtocol(url.scheme());
458 void URLRequest::set_first_party_for_cookies(
459 const GURL& first_party_for_cookies) {
460 DCHECK(!is_pending_);
461 first_party_for_cookies_ = first_party_for_cookies;
464 void URLRequest::set_first_party_url_policy(
465 FirstPartyURLPolicy first_party_url_policy) {
466 DCHECK(!is_pending_);
467 first_party_url_policy_ = first_party_url_policy;
470 void URLRequest::set_method(const std::string& method) {
471 DCHECK(!is_pending_);
472 method_ = method;
475 void URLRequest::SetReferrer(const std::string& referrer) {
476 DCHECK(!is_pending_);
477 GURL referrer_url(referrer);
478 if (referrer_url.is_valid()) {
479 referrer_ = referrer_url.GetAsReferrer().spec();
480 } else {
481 referrer_ = referrer;
485 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
486 DCHECK(!is_pending_);
487 referrer_policy_ = referrer_policy;
490 void URLRequest::set_delegate(Delegate* delegate) {
491 delegate_ = delegate;
494 void URLRequest::Start() {
495 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456327 is fixed.
496 tracked_objects::ScopedTracker tracking_profile(
497 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start"));
499 // Some values can be NULL, but the job factory must not be.
500 DCHECK(context_->job_factory());
502 // Anything that sets |blocked_by_| before start should have cleaned up after
503 // itself.
504 DCHECK(blocked_by_.empty());
506 g_url_requests_started = true;
507 response_info_.request_time = base::Time::Now();
509 load_timing_info_ = LoadTimingInfo();
510 load_timing_info_.request_start_time = response_info_.request_time;
511 load_timing_info_.request_start = base::TimeTicks::Now();
513 // Only notify the delegate for the initial request.
514 if (network_delegate_) {
515 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
516 tracked_objects::ScopedTracker tracking_profile25(
517 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start 2.5"));
519 OnCallToDelegate();
520 int error = network_delegate_->NotifyBeforeURLRequest(
521 this, before_request_callback_, &delegate_redirect_url_);
522 // If ERR_IO_PENDING is returned, the delegate will invoke
523 // |before_request_callback_| later.
524 if (error != ERR_IO_PENDING)
525 BeforeRequestComplete(error);
526 return;
529 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
530 tracked_objects::ScopedTracker tracking_profile2(
531 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start 2"));
533 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
534 this, network_delegate_));
537 ///////////////////////////////////////////////////////////////////////////////
539 URLRequest::URLRequest(const GURL& url,
540 RequestPriority priority,
541 Delegate* delegate,
542 const URLRequestContext* context,
543 NetworkDelegate* network_delegate)
544 : context_(context),
545 network_delegate_(network_delegate ? network_delegate
546 : context->network_delegate()),
547 net_log_(
548 BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST)),
549 url_chain_(1, url),
550 method_("GET"),
551 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
552 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
553 load_flags_(LOAD_NORMAL),
554 delegate_(delegate),
555 is_pending_(false),
556 is_redirecting_(false),
557 redirect_limit_(kMaxRedirects),
558 priority_(priority),
559 identifier_(GenerateURLRequestIdentifier()),
560 calling_delegate_(false),
561 use_blocked_by_as_load_param_(false),
562 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
563 base::Unretained(this))),
564 has_notified_completion_(false),
565 received_response_content_length_(0),
566 creation_time_(base::TimeTicks::Now()),
567 notified_before_network_start_(false) {
568 // Sanity check out environment.
569 DCHECK(base::MessageLoop::current())
570 << "The current base::MessageLoop must exist";
572 context->url_requests()->insert(this);
573 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
576 void URLRequest::BeforeRequestComplete(int error) {
577 DCHECK(!job_.get());
578 DCHECK_NE(ERR_IO_PENDING, error);
580 // Check that there are no callbacks to already canceled requests.
581 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
583 OnCallToDelegateComplete();
585 if (error != OK) {
586 std::string source("delegate");
587 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
588 NetLog::StringCallback("source", &source));
589 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
590 } else if (!delegate_redirect_url_.is_empty()) {
591 GURL new_url;
592 new_url.Swap(&delegate_redirect_url_);
594 URLRequestRedirectJob* job = new URLRequestRedirectJob(
595 this, network_delegate_, new_url,
596 // Use status code 307 to preserve the method, so POST requests work.
597 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
598 StartJob(job);
599 } else {
600 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
601 this, network_delegate_));
605 void URLRequest::StartJob(URLRequestJob* job) {
606 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
607 tracked_objects::ScopedTracker tracking_profile(
608 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::StartJob"));
610 DCHECK(!is_pending_);
611 DCHECK(!job_.get());
613 net_log_.BeginEvent(
614 NetLog::TYPE_URL_REQUEST_START_JOB,
615 base::Bind(&NetLogURLRequestStartCallback,
616 &url(), &method_, load_flags_, priority_,
617 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
619 job_ = job;
620 job_->SetExtraRequestHeaders(extra_request_headers_);
621 job_->SetPriority(priority_);
623 if (upload_data_stream_.get())
624 job_->SetUpload(upload_data_stream_.get());
626 is_pending_ = true;
627 is_redirecting_ = false;
629 response_info_.was_cached = false;
631 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect(
632 referrer_policy_, referrer_, url())) {
633 if (!network_delegate_ ||
634 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
635 *this, url(), GURL(referrer_))) {
636 referrer_.clear();
637 } else {
638 // We need to clear the referrer anyway to avoid an infinite recursion
639 // when starting the error job.
640 referrer_.clear();
641 std::string source("delegate");
642 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
643 NetLog::StringCallback("source", &source));
644 RestartWithJob(new URLRequestErrorJob(
645 this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
646 return;
650 // Don't allow errors to be sent from within Start().
651 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
652 // we probably don't want this: they should be sent asynchronously so
653 // the caller does not get reentered.
654 job_->Start();
657 void URLRequest::Restart() {
658 // Should only be called if the original job didn't make any progress.
659 DCHECK(job_.get() && !job_->has_response_started());
660 RestartWithJob(
661 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
664 void URLRequest::RestartWithJob(URLRequestJob *job) {
665 DCHECK(job->request() == this);
666 PrepareToRestart();
667 StartJob(job);
670 void URLRequest::Cancel() {
671 DoCancel(ERR_ABORTED, SSLInfo());
674 void URLRequest::CancelWithError(int error) {
675 DoCancel(error, SSLInfo());
678 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
679 // This should only be called on a started request.
680 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
681 NOTREACHED();
682 return;
684 DoCancel(error, ssl_info);
687 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
688 DCHECK(error < 0);
689 // If cancelled while calling a delegate, clear delegate info.
690 if (calling_delegate_) {
691 LogUnblocked();
692 OnCallToDelegateComplete();
695 // If the URL request already has an error status, then canceling is a no-op.
696 // Plus, we don't want to change the error status once it has been set.
697 if (status_.is_success()) {
698 status_ = URLRequestStatus(URLRequestStatus::CANCELED, error);
699 response_info_.ssl_info = ssl_info;
701 // If the request hasn't already been completed, log a cancellation event.
702 if (!has_notified_completion_) {
703 // Don't log an error code on ERR_ABORTED, since that's redundant.
704 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
705 error == ERR_ABORTED ? OK : error);
709 if (is_pending_ && job_.get())
710 job_->Kill();
712 // We need to notify about the end of this job here synchronously. The
713 // Job sends an asynchronous notification but by the time this is processed,
714 // our |context_| is NULL.
715 NotifyRequestCompleted();
717 // The Job will call our NotifyDone method asynchronously. This is done so
718 // that the Delegate implementation can call Cancel without having to worry
719 // about being called recursively.
722 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
723 DCHECK(job_.get());
724 DCHECK(bytes_read);
725 *bytes_read = 0;
727 // If this is the first read, end the delegate call that may have started in
728 // OnResponseStarted.
729 OnCallToDelegateComplete();
731 // This handles a cancel that happens while paused.
732 // TODO(ahendrickson): DCHECK() that it is not done after
733 // http://crbug.com/115705 is fixed.
734 if (job_->is_done())
735 return false;
737 if (dest_size == 0) {
738 // Caller is not too bright. I guess we've done what they asked.
739 return true;
742 // Once the request fails or is cancelled, read will just return 0 bytes
743 // to indicate end of stream.
744 if (!status_.is_success()) {
745 return true;
748 bool rv = job_->Read(dest, dest_size, bytes_read);
749 // If rv is false, the status cannot be success.
750 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
752 if (rv && *bytes_read <= 0 && status_.is_success())
753 NotifyRequestCompleted();
754 return rv;
757 void URLRequest::StopCaching() {
758 DCHECK(job_.get());
759 job_->StopCaching();
762 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
763 bool* defer_redirect) {
764 is_redirecting_ = true;
766 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
767 URLRequestJob* job =
768 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
769 this, network_delegate_, redirect_info.new_url);
770 if (job) {
771 RestartWithJob(job);
772 } else if (delegate_) {
773 OnCallToDelegate();
774 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
775 // |this| may be have been destroyed here.
779 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
780 if (delegate_ && !notified_before_network_start_) {
781 OnCallToDelegate();
782 delegate_->OnBeforeNetworkStart(this, defer);
783 if (!*defer)
784 OnCallToDelegateComplete();
785 notified_before_network_start_ = true;
789 void URLRequest::ResumeNetworkStart() {
790 DCHECK(job_.get());
791 DCHECK(notified_before_network_start_);
793 OnCallToDelegateComplete();
794 job_->ResumeNetworkStart();
797 void URLRequest::NotifyResponseStarted() {
798 int net_error = OK;
799 if (!status_.is_success())
800 net_error = status_.error();
801 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
802 net_error);
804 URLRequestJob* job =
805 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
806 this, network_delegate_);
807 if (job) {
808 RestartWithJob(job);
809 } else {
810 if (delegate_) {
811 // In some cases (e.g. an event was canceled), we might have sent the
812 // completion event and receive a NotifyResponseStarted() later.
813 if (!has_notified_completion_ && status_.is_success()) {
814 if (network_delegate_)
815 network_delegate_->NotifyResponseStarted(this);
818 // Notify in case the entire URL Request has been finished.
819 if (!has_notified_completion_ && !status_.is_success())
820 NotifyRequestCompleted();
822 OnCallToDelegate();
823 delegate_->OnResponseStarted(this);
824 // Nothing may appear below this line as OnResponseStarted may delete
825 // |this|.
830 void URLRequest::FollowDeferredRedirect() {
831 CHECK(job_.get());
832 CHECK(status_.is_success());
834 job_->FollowDeferredRedirect();
837 void URLRequest::SetAuth(const AuthCredentials& credentials) {
838 DCHECK(job_.get());
839 DCHECK(job_->NeedsAuth());
841 job_->SetAuth(credentials);
844 void URLRequest::CancelAuth() {
845 DCHECK(job_.get());
846 DCHECK(job_->NeedsAuth());
848 job_->CancelAuth();
851 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
852 DCHECK(job_.get());
854 job_->ContinueWithCertificate(client_cert);
857 void URLRequest::ContinueDespiteLastError() {
858 DCHECK(job_.get());
860 job_->ContinueDespiteLastError();
863 void URLRequest::PrepareToRestart() {
864 DCHECK(job_.get());
866 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
867 // one.
868 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
870 OrphanJob();
872 response_info_ = HttpResponseInfo();
873 response_info_.request_time = base::Time::Now();
875 load_timing_info_ = LoadTimingInfo();
876 load_timing_info_.request_start_time = response_info_.request_time;
877 load_timing_info_.request_start = base::TimeTicks::Now();
879 status_ = URLRequestStatus();
880 is_pending_ = false;
881 proxy_server_ = HostPortPair();
884 void URLRequest::OrphanJob() {
885 // When calling this function, please check that URLRequestHttpJob is
886 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
887 // the call back. This is currently guaranteed by the following strategies:
888 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
889 // be receiving any headers at that time.
890 // - OrphanJob is called in ~URLRequest, in this case
891 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
892 // that the callback becomes invalid.
893 job_->Kill();
894 job_->DetachRequest(); // ensures that the job will not call us again
895 job_ = NULL;
898 int URLRequest::Redirect(const RedirectInfo& redirect_info) {
899 // Matches call in NotifyReceivedRedirect.
900 OnCallToDelegateComplete();
901 if (net_log_.IsCapturing()) {
902 net_log_.AddEvent(
903 NetLog::TYPE_URL_REQUEST_REDIRECTED,
904 NetLog::StringCallback("location",
905 &redirect_info.new_url.possibly_invalid_spec()));
908 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
909 if (network_delegate_)
910 network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url);
912 if (redirect_limit_ <= 0) {
913 DVLOG(1) << "disallowing redirect: exceeds limit";
914 return ERR_TOO_MANY_REDIRECTS;
917 if (!redirect_info.new_url.is_valid())
918 return ERR_INVALID_URL;
920 if (!job_->IsSafeRedirect(redirect_info.new_url)) {
921 DVLOG(1) << "disallowing redirect: unsafe protocol";
922 return ERR_UNSAFE_REDIRECT;
925 if (!final_upload_progress_.position())
926 final_upload_progress_ = job_->GetUploadProgress();
927 PrepareToRestart();
929 if (redirect_info.new_method != method_) {
930 // TODO(davidben): This logic still needs to be replicated at the consumers.
931 if (method_ == "POST") {
932 // If being switched from POST, must remove headers that were specific to
933 // the POST and don't have meaning in other methods. For example the
934 // inclusion of a multipart Content-Type header in GET can cause problems
935 // with some servers:
936 // http://code.google.com/p/chromium/issues/detail?id=843
937 StripPostSpecificHeaders(&extra_request_headers_);
939 upload_data_stream_.reset();
940 method_ = redirect_info.new_method;
943 // Cross-origin redirects should not result in an Origin header value that is
944 // equal to the original request's Origin header. This is necessary to prevent
945 // a reflection of POST requests to bypass CSRF protections. If the header was
946 // not set to "null", a POST request from origin A to a malicious origin M
947 // could be redirected by M back to A.
949 // This behavior is specified in step 1 of step 10 of the 301, 302, 303, 307,
950 // 308 block of step 5 of Section 4.2 of Fetch[1] (which supercedes the
951 // behavior outlined in RFC 6454[2].
953 // [1]: https://fetch.spec.whatwg.org/#concept-http-fetch
954 // [2]: https://tools.ietf.org/html/rfc6454#section-7
956 // TODO(jww): This is a layering violation and should be refactored somewhere
957 // up into //net's embedder. https://crbug.com/471397
958 if (!url::Origin(redirect_info.new_url)
959 .IsSameOriginWith(url::Origin(url())) &&
960 extra_request_headers_.HasHeader(HttpRequestHeaders::kOrigin)) {
961 extra_request_headers_.SetHeader(HttpRequestHeaders::kOrigin,
962 url::Origin().Serialize());
965 referrer_ = redirect_info.new_referrer;
966 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies;
968 url_chain_.push_back(redirect_info.new_url);
969 --redirect_limit_;
971 Start();
972 return OK;
975 const URLRequestContext* URLRequest::context() const {
976 return context_;
979 int64 URLRequest::GetExpectedContentSize() const {
980 int64 expected_content_size = -1;
981 if (job_.get())
982 expected_content_size = job_->expected_content_size();
984 return expected_content_size;
987 void URLRequest::SetPriority(RequestPriority priority) {
988 DCHECK_GE(priority, MINIMUM_PRIORITY);
989 DCHECK_LE(priority, MAXIMUM_PRIORITY);
991 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
992 NOTREACHED();
993 // Maintain the invariant that requests with IGNORE_LIMITS set
994 // have MAXIMUM_PRIORITY for release mode.
995 return;
998 if (priority_ == priority)
999 return;
1001 priority_ = priority;
1002 if (job_.get()) {
1003 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
1004 NetLog::IntegerCallback("priority", priority_));
1005 job_->SetPriority(priority_);
1009 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
1010 const GURL& url = this->url();
1011 bool scheme_is_http = url.SchemeIs("http");
1012 if (!scheme_is_http && !url.SchemeIs("ws"))
1013 return false;
1014 TransportSecurityState* state = context()->transport_security_state();
1015 if (state && state->ShouldUpgradeToSSL(url.host())) {
1016 GURL::Replacements replacements;
1017 const char* new_scheme = scheme_is_http ? "https" : "wss";
1018 replacements.SetSchemeStr(new_scheme);
1019 *redirect_url = url.ReplaceComponents(replacements);
1020 return true;
1022 return false;
1025 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1026 NetworkDelegate::AuthRequiredResponse rv =
1027 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1028 auth_info_ = auth_info;
1029 if (network_delegate_) {
1030 OnCallToDelegate();
1031 rv = network_delegate_->NotifyAuthRequired(
1032 this,
1033 *auth_info,
1034 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1035 base::Unretained(this)),
1036 &auth_credentials_);
1037 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1038 return;
1041 NotifyAuthRequiredComplete(rv);
1044 void URLRequest::NotifyAuthRequiredComplete(
1045 NetworkDelegate::AuthRequiredResponse result) {
1046 OnCallToDelegateComplete();
1048 // Check that there are no callbacks to already canceled requests.
1049 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1051 // NotifyAuthRequired may be called multiple times, such as
1052 // when an authentication attempt fails. Clear out the data
1053 // so it can be reset on another round.
1054 AuthCredentials credentials = auth_credentials_;
1055 auth_credentials_ = AuthCredentials();
1056 scoped_refptr<AuthChallengeInfo> auth_info;
1057 auth_info.swap(auth_info_);
1059 switch (result) {
1060 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1061 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1062 // didn't take an action.
1063 if (delegate_)
1064 delegate_->OnAuthRequired(this, auth_info.get());
1065 break;
1067 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1068 SetAuth(credentials);
1069 break;
1071 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1072 CancelAuth();
1073 break;
1075 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1076 NOTREACHED();
1077 break;
1081 void URLRequest::NotifyCertificateRequested(
1082 SSLCertRequestInfo* cert_request_info) {
1083 if (delegate_)
1084 delegate_->OnCertificateRequested(this, cert_request_info);
1087 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1088 bool fatal) {
1089 if (delegate_)
1090 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1093 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1094 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1095 if (network_delegate_) {
1096 return network_delegate_->CanGetCookies(*this, cookie_list);
1098 return g_default_can_use_cookies;
1101 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1102 CookieOptions* options) const {
1103 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1104 if (network_delegate_) {
1105 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1107 return g_default_can_use_cookies;
1110 bool URLRequest::CanEnablePrivacyMode() const {
1111 if (network_delegate_) {
1112 return network_delegate_->CanEnablePrivacyMode(url(),
1113 first_party_for_cookies_);
1115 return !g_default_can_use_cookies;
1119 void URLRequest::NotifyReadCompleted(int bytes_read) {
1120 // Notify in case the entire URL Request has been finished.
1121 if (bytes_read <= 0)
1122 NotifyRequestCompleted();
1124 // Notify NetworkChangeNotifier that we just received network data.
1125 // This is to identify cases where the NetworkChangeNotifier thinks we
1126 // are off-line but we are still receiving network data (crbug.com/124069),
1127 // and to get rough network connection measurements.
1128 if (bytes_read > 0 && !was_cached())
1129 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1131 if (delegate_)
1132 delegate_->OnReadCompleted(this, bytes_read);
1134 // Nothing below this line as OnReadCompleted may delete |this|.
1137 void URLRequest::OnHeadersComplete() {
1138 // Cache load timing information now, as information will be lost once the
1139 // socket is closed and the ClientSocketHandle is Reset, which will happen
1140 // once the body is complete. The start times should already be populated.
1141 if (job_.get()) {
1142 // Keep a copy of the two times the URLRequest sets.
1143 base::TimeTicks request_start = load_timing_info_.request_start;
1144 base::Time request_start_time = load_timing_info_.request_start_time;
1146 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1147 // consistent place to start from.
1148 load_timing_info_ = LoadTimingInfo();
1149 job_->GetLoadTimingInfo(&load_timing_info_);
1151 load_timing_info_.request_start = request_start;
1152 load_timing_info_.request_start_time = request_start_time;
1154 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1158 void URLRequest::NotifyRequestCompleted() {
1159 // TODO(battre): Get rid of this check, according to willchan it should
1160 // not be needed.
1161 if (has_notified_completion_)
1162 return;
1164 is_pending_ = false;
1165 is_redirecting_ = false;
1166 has_notified_completion_ = true;
1167 if (network_delegate_)
1168 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1171 void URLRequest::OnCallToDelegate() {
1172 DCHECK(!calling_delegate_);
1173 DCHECK(blocked_by_.empty());
1174 calling_delegate_ = true;
1175 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1178 void URLRequest::OnCallToDelegateComplete() {
1179 // This should have been cleared before resuming the request.
1180 DCHECK(blocked_by_.empty());
1181 if (!calling_delegate_)
1182 return;
1183 calling_delegate_ = false;
1184 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1187 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1188 stack_trace_.reset(new base::debug::StackTrace(stack_trace));
1191 const base::debug::StackTrace* URLRequest::stack_trace() const {
1192 return stack_trace_.get();
1195 void URLRequest::GetConnectionAttempts(ConnectionAttempts* out) const {
1196 if (job_)
1197 job_->GetConnectionAttempts(out);
1198 else
1199 out->clear();
1202 } // namespace net