Raise SIGKILL if cast_service doesn't Finalize() within timeout.
[chromium-blink-merge.git] / net / url_request / url_request.cc
blob455bdf255c01001b487498d62e24c170d472cfb8
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/net_log.h"
27 #include "net/base/network_change_notifier.h"
28 #include "net/base/network_delegate.h"
29 #include "net/base/upload_data_stream.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_util.h"
32 #include "net/ssl/ssl_cert_request_info.h"
33 #include "net/url_request/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"
41 using base::Time;
42 using std::string;
44 namespace net {
46 namespace {
48 // Max number of http redirects to follow. Same number as gecko.
49 const int kMaxRedirects = 20;
51 // Discard headers which have meaning in POST (Content-Length, Content-Type,
52 // Origin).
53 void StripPostSpecificHeaders(HttpRequestHeaders* headers) {
54 // These are headers that may be attached to a POST.
55 headers->RemoveHeader(HttpRequestHeaders::kContentLength);
56 headers->RemoveHeader(HttpRequestHeaders::kContentType);
57 headers->RemoveHeader(HttpRequestHeaders::kOrigin);
60 // TODO(battre): Delete this, see http://crbug.com/89321:
61 // This counter keeps track of the identifiers used for URL requests so far.
62 // 0 is reserved to represent an invalid ID.
63 uint64 g_next_url_request_identifier = 1;
65 // This lock protects g_next_url_request_identifier.
66 base::LazyInstance<base::Lock>::Leaky
67 g_next_url_request_identifier_lock = LAZY_INSTANCE_INITIALIZER;
69 // Returns an prior unused identifier for URL requests.
70 uint64 GenerateURLRequestIdentifier() {
71 base::AutoLock lock(g_next_url_request_identifier_lock.Get());
72 return g_next_url_request_identifier++;
75 // True once the first URLRequest was started.
76 bool g_url_requests_started = false;
78 // True if cookies are accepted by default.
79 bool g_default_can_use_cookies = true;
81 // When the URLRequest first assempts load timing information, it has the times
82 // at which each event occurred. The API requires the time which the request
83 // was blocked on each phase. This function handles the conversion.
85 // In the case of reusing a SPDY session, old proxy results may have been
86 // reused, so proxy resolution times may be before the request was started.
88 // Due to preconnect and late binding, it is also possible for the connection
89 // attempt to start before a request has been started, or proxy resolution
90 // completed.
92 // This functions fixes both those cases.
93 void ConvertRealLoadTimesToBlockingTimes(
94 net::LoadTimingInfo* load_timing_info) {
95 DCHECK(!load_timing_info->request_start.is_null());
97 // Earliest time possible for the request to be blocking on connect events.
98 base::TimeTicks block_on_connect = load_timing_info->request_start;
100 if (!load_timing_info->proxy_resolve_start.is_null()) {
101 DCHECK(!load_timing_info->proxy_resolve_end.is_null());
103 // Make sure the proxy times are after request start.
104 if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
105 load_timing_info->proxy_resolve_start = load_timing_info->request_start;
106 if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
107 load_timing_info->proxy_resolve_end = load_timing_info->request_start;
109 // Connect times must also be after the proxy times.
110 block_on_connect = load_timing_info->proxy_resolve_end;
113 // Make sure connection times are after start and proxy times.
115 net::LoadTimingInfo::ConnectTiming* connect_timing =
116 &load_timing_info->connect_timing;
117 if (!connect_timing->dns_start.is_null()) {
118 DCHECK(!connect_timing->dns_end.is_null());
119 if (connect_timing->dns_start < block_on_connect)
120 connect_timing->dns_start = block_on_connect;
121 if (connect_timing->dns_end < block_on_connect)
122 connect_timing->dns_end = block_on_connect;
125 if (!connect_timing->connect_start.is_null()) {
126 DCHECK(!connect_timing->connect_end.is_null());
127 if (connect_timing->connect_start < block_on_connect)
128 connect_timing->connect_start = block_on_connect;
129 if (connect_timing->connect_end < block_on_connect)
130 connect_timing->connect_end = block_on_connect;
133 if (!connect_timing->ssl_start.is_null()) {
134 DCHECK(!connect_timing->ssl_end.is_null());
135 if (connect_timing->ssl_start < block_on_connect)
136 connect_timing->ssl_start = block_on_connect;
137 if (connect_timing->ssl_end < block_on_connect)
138 connect_timing->ssl_end = block_on_connect;
142 } // namespace
144 ///////////////////////////////////////////////////////////////////////////////
145 // URLRequest::Delegate
147 void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
148 const RedirectInfo& redirect_info,
149 bool* defer_redirect) {
152 void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
153 AuthChallengeInfo* auth_info) {
154 request->CancelAuth();
157 void URLRequest::Delegate::OnCertificateRequested(
158 URLRequest* request,
159 SSLCertRequestInfo* cert_request_info) {
160 request->CancelWithError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
163 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
164 const SSLInfo& ssl_info,
165 bool is_hsts_ok) {
166 request->Cancel();
169 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request,
170 bool* defer) {
173 ///////////////////////////////////////////////////////////////////////////////
174 // URLRequest
176 URLRequest::~URLRequest() {
177 Cancel();
179 if (network_delegate_) {
180 network_delegate_->NotifyURLRequestDestroyed(this);
181 if (job_.get())
182 job_->NotifyURLRequestDestroyed();
185 if (job_.get())
186 OrphanJob();
188 int deleted = context_->url_requests()->erase(this);
189 CHECK_EQ(1, deleted);
191 int net_error = OK;
192 // Log error only on failure, not cancellation, as even successful requests
193 // are "cancelled" on destruction.
194 if (status_.status() == URLRequestStatus::FAILED)
195 net_error = status_.error();
196 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
199 void URLRequest::EnableChunkedUpload() {
200 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked());
201 if (!upload_data_stream_) {
202 upload_chunked_data_stream_ = new ChunkedUploadDataStream(0);
203 upload_data_stream_.reset(upload_chunked_data_stream_);
207 void URLRequest::AppendChunkToUpload(const char* bytes,
208 int bytes_len,
209 bool is_last_chunk) {
210 DCHECK(upload_data_stream_);
211 DCHECK(upload_data_stream_->is_chunked());
212 upload_chunked_data_stream_->AppendData(bytes, bytes_len, is_last_chunk);
215 void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) {
216 upload_data_stream_ = upload.Pass();
219 const UploadDataStream* URLRequest::get_upload() const {
220 return upload_data_stream_.get();
223 bool URLRequest::has_upload() const {
224 return upload_data_stream_.get() != NULL;
227 void URLRequest::SetExtraRequestHeaderByName(const string& name,
228 const string& value,
229 bool overwrite) {
230 DCHECK(!is_pending_ || is_redirecting_);
231 if (overwrite) {
232 extra_request_headers_.SetHeader(name, value);
233 } else {
234 extra_request_headers_.SetHeaderIfMissing(name, value);
238 void URLRequest::RemoveRequestHeaderByName(const string& name) {
239 DCHECK(!is_pending_ || is_redirecting_);
240 extra_request_headers_.RemoveHeader(name);
243 void URLRequest::SetExtraRequestHeaders(
244 const HttpRequestHeaders& headers) {
245 DCHECK(!is_pending_);
246 extra_request_headers_ = headers;
248 // NOTE: This method will likely become non-trivial once the other setters
249 // for request headers are implemented.
252 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
253 if (!job_.get())
254 return false;
256 return job_->GetFullRequestHeaders(headers);
259 int64 URLRequest::GetTotalReceivedBytes() const {
260 if (!job_.get())
261 return 0;
263 return job_->GetTotalReceivedBytes();
266 LoadStateWithParam URLRequest::GetLoadState() const {
267 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
268 // delegate before it has been started.
269 if (calling_delegate_ || !blocked_by_.empty()) {
270 return LoadStateWithParam(
271 LOAD_STATE_WAITING_FOR_DELEGATE,
272 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
273 base::string16());
275 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
276 base::string16());
279 base::Value* URLRequest::GetStateAsValue() const {
280 base::DictionaryValue* dict = new base::DictionaryValue();
281 dict->SetString("url", original_url().possibly_invalid_spec());
283 if (url_chain_.size() > 1) {
284 base::ListValue* list = new base::ListValue();
285 for (std::vector<GURL>::const_iterator url = url_chain_.begin();
286 url != url_chain_.end(); ++url) {
287 list->AppendString(url->possibly_invalid_spec());
289 dict->Set("url_chain", list);
292 dict->SetInteger("load_flags", load_flags_);
294 LoadStateWithParam load_state = GetLoadState();
295 dict->SetInteger("load_state", load_state.state);
296 if (!load_state.param.empty())
297 dict->SetString("load_state_param", load_state.param);
298 if (!blocked_by_.empty())
299 dict->SetString("delegate_info", blocked_by_);
301 dict->SetString("method", method_);
302 dict->SetBoolean("has_upload", has_upload());
303 dict->SetBoolean("is_pending", is_pending_);
305 // Add the status of the request. The status should always be IO_PENDING, and
306 // the error should always be OK, unless something is holding onto a request
307 // that has finished or a request was leaked. Neither of these should happen.
308 switch (status_.status()) {
309 case URLRequestStatus::SUCCESS:
310 dict->SetString("status", "SUCCESS");
311 break;
312 case URLRequestStatus::IO_PENDING:
313 dict->SetString("status", "IO_PENDING");
314 break;
315 case URLRequestStatus::CANCELED:
316 dict->SetString("status", "CANCELED");
317 break;
318 case URLRequestStatus::FAILED:
319 dict->SetString("status", "FAILED");
320 break;
322 if (status_.error() != OK)
323 dict->SetInteger("net_error", status_.error());
324 return dict;
327 void URLRequest::LogBlockedBy(const char* blocked_by) {
328 DCHECK(blocked_by);
329 DCHECK_GT(strlen(blocked_by), 0u);
331 // Only log information to NetLog during startup and certain deferring calls
332 // to delegates. For all reads but the first, do nothing.
333 if (!calling_delegate_ && !response_info_.request_time.is_null())
334 return;
336 LogUnblocked();
337 blocked_by_ = blocked_by;
338 use_blocked_by_as_load_param_ = false;
340 net_log_.BeginEvent(
341 NetLog::TYPE_DELEGATE_INFO,
342 NetLog::StringCallback("delegate_info", &blocked_by_));
345 void URLRequest::LogAndReportBlockedBy(const char* source) {
346 LogBlockedBy(source);
347 use_blocked_by_as_load_param_ = true;
350 void URLRequest::LogUnblocked() {
351 if (blocked_by_.empty())
352 return;
354 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
355 blocked_by_.clear();
358 UploadProgress URLRequest::GetUploadProgress() const {
359 if (!job_.get()) {
360 // We haven't started or the request was cancelled
361 return UploadProgress();
363 if (final_upload_progress_.position()) {
364 // The first job completed and none of the subsequent series of
365 // GETs when following redirects will upload anything, so we return the
366 // cached results from the initial job, the POST.
367 return final_upload_progress_;
369 return job_->GetUploadProgress();
372 void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
373 DCHECK(value);
374 if (response_info_.headers.get()) {
375 response_info_.headers->GetNormalizedHeader(name, value);
376 } else {
377 value->clear();
381 HostPortPair URLRequest::GetSocketAddress() const {
382 DCHECK(job_.get());
383 return job_->GetSocketAddress();
386 HttpResponseHeaders* URLRequest::response_headers() const {
387 return response_info_.headers.get();
390 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
391 *load_timing_info = load_timing_info_;
394 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
395 DCHECK(job_.get());
396 return job_->GetResponseCookies(cookies);
399 void URLRequest::GetMimeType(string* mime_type) const {
400 DCHECK(job_.get());
401 job_->GetMimeType(mime_type);
404 void URLRequest::GetCharset(string* charset) const {
405 DCHECK(job_.get());
406 job_->GetCharset(charset);
409 int URLRequest::GetResponseCode() const {
410 DCHECK(job_.get());
411 return job_->GetResponseCode();
414 void URLRequest::SetLoadFlags(int flags) {
415 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
416 DCHECK(!job_.get());
417 DCHECK(flags & LOAD_IGNORE_LIMITS);
418 DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
420 load_flags_ = flags;
422 // This should be a no-op given the above DCHECKs, but do this
423 // anyway for release mode.
424 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
425 SetPriority(MAXIMUM_PRIORITY);
428 // static
429 void URLRequest::SetDefaultCookiePolicyToBlock() {
430 CHECK(!g_url_requests_started);
431 g_default_can_use_cookies = false;
434 // static
435 bool URLRequest::IsHandledProtocol(const std::string& scheme) {
436 return URLRequestJobManager::SupportsScheme(scheme);
439 // static
440 bool URLRequest::IsHandledURL(const GURL& url) {
441 if (!url.is_valid()) {
442 // We handle error cases.
443 return true;
446 return IsHandledProtocol(url.scheme());
449 void URLRequest::set_first_party_for_cookies(
450 const GURL& first_party_for_cookies) {
451 DCHECK(!is_pending_);
452 first_party_for_cookies_ = first_party_for_cookies;
455 void URLRequest::set_first_party_url_policy(
456 FirstPartyURLPolicy first_party_url_policy) {
457 DCHECK(!is_pending_);
458 first_party_url_policy_ = first_party_url_policy;
461 void URLRequest::set_method(const std::string& method) {
462 DCHECK(!is_pending_);
463 method_ = method;
466 void URLRequest::SetReferrer(const std::string& referrer) {
467 DCHECK(!is_pending_);
468 GURL referrer_url(referrer);
469 if (referrer_url.is_valid()) {
470 referrer_ = referrer_url.GetAsReferrer().spec();
471 } else {
472 referrer_ = referrer;
476 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
477 DCHECK(!is_pending_);
478 referrer_policy_ = referrer_policy;
481 void URLRequest::set_delegate(Delegate* delegate) {
482 delegate_ = delegate;
485 void URLRequest::Start() {
486 // Some values can be NULL, but the job factory must not be.
487 DCHECK(context_->job_factory());
489 // Anything that sets |blocked_by_| before start should have cleaned up after
490 // itself.
491 DCHECK(blocked_by_.empty());
493 g_url_requests_started = true;
494 response_info_.request_time = base::Time::Now();
496 load_timing_info_ = LoadTimingInfo();
497 load_timing_info_.request_start_time = response_info_.request_time;
498 load_timing_info_.request_start = base::TimeTicks::Now();
500 // Only notify the delegate for the initial request.
501 if (network_delegate_) {
502 OnCallToDelegate();
503 int error = network_delegate_->NotifyBeforeURLRequest(
504 this, before_request_callback_, &delegate_redirect_url_);
505 // If ERR_IO_PENDING is returned, the delegate will invoke
506 // |before_request_callback_| later.
507 if (error != ERR_IO_PENDING)
508 BeforeRequestComplete(error);
509 return;
512 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
513 this, network_delegate_));
516 ///////////////////////////////////////////////////////////////////////////////
518 URLRequest::URLRequest(const GURL& url,
519 RequestPriority priority,
520 Delegate* delegate,
521 const URLRequestContext* context,
522 NetworkDelegate* network_delegate)
523 : context_(context),
524 network_delegate_(network_delegate ? network_delegate
525 : context->network_delegate()),
526 net_log_(
527 BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST)),
528 url_chain_(1, url),
529 method_("GET"),
530 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
531 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
532 load_flags_(LOAD_NORMAL),
533 delegate_(delegate),
534 is_pending_(false),
535 is_redirecting_(false),
536 redirect_limit_(kMaxRedirects),
537 priority_(priority),
538 identifier_(GenerateURLRequestIdentifier()),
539 calling_delegate_(false),
540 use_blocked_by_as_load_param_(false),
541 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
542 base::Unretained(this))),
543 has_notified_completion_(false),
544 received_response_content_length_(0),
545 creation_time_(base::TimeTicks::Now()),
546 notified_before_network_start_(false) {
547 // Sanity check out environment.
548 DCHECK(base::MessageLoop::current())
549 << "The current base::MessageLoop must exist";
551 context->url_requests()->insert(this);
552 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
555 void URLRequest::BeforeRequestComplete(int error) {
556 DCHECK(!job_.get());
557 DCHECK_NE(ERR_IO_PENDING, error);
559 // Check that there are no callbacks to already canceled requests.
560 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
562 OnCallToDelegateComplete();
564 if (error != OK) {
565 std::string source("delegate");
566 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
567 NetLog::StringCallback("source", &source));
568 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
569 } else if (!delegate_redirect_url_.is_empty()) {
570 GURL new_url;
571 new_url.Swap(&delegate_redirect_url_);
573 URLRequestRedirectJob* job = new URLRequestRedirectJob(
574 this, network_delegate_, new_url,
575 // Use status code 307 to preserve the method, so POST requests work.
576 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
577 StartJob(job);
578 } else {
579 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
580 this, network_delegate_));
584 void URLRequest::StartJob(URLRequestJob* job) {
585 DCHECK(!is_pending_);
586 DCHECK(!job_.get());
588 net_log_.BeginEvent(
589 NetLog::TYPE_URL_REQUEST_START_JOB,
590 base::Bind(&NetLogURLRequestStartCallback,
591 &url(), &method_, load_flags_, priority_,
592 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
594 job_ = job;
595 job_->SetExtraRequestHeaders(extra_request_headers_);
596 job_->SetPriority(priority_);
598 if (upload_data_stream_.get())
599 job_->SetUpload(upload_data_stream_.get());
601 is_pending_ = true;
602 is_redirecting_ = false;
604 response_info_.was_cached = false;
606 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect(
607 referrer_policy_, referrer_, url())) {
608 if (!network_delegate_ ||
609 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
610 *this, url(), GURL(referrer_))) {
611 referrer_.clear();
612 } else {
613 // We need to clear the referrer anyway to avoid an infinite recursion
614 // when starting the error job.
615 referrer_.clear();
616 std::string source("delegate");
617 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
618 NetLog::StringCallback("source", &source));
619 RestartWithJob(new URLRequestErrorJob(
620 this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
621 return;
625 // Don't allow errors to be sent from within Start().
626 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
627 // we probably don't want this: they should be sent asynchronously so
628 // the caller does not get reentered.
629 job_->Start();
632 void URLRequest::Restart() {
633 // Should only be called if the original job didn't make any progress.
634 DCHECK(job_.get() && !job_->has_response_started());
635 RestartWithJob(
636 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
639 void URLRequest::RestartWithJob(URLRequestJob *job) {
640 DCHECK(job->request() == this);
641 PrepareToRestart();
642 StartJob(job);
645 void URLRequest::Cancel() {
646 DoCancel(ERR_ABORTED, SSLInfo());
649 void URLRequest::CancelWithError(int error) {
650 DoCancel(error, SSLInfo());
653 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
654 // This should only be called on a started request.
655 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
656 NOTREACHED();
657 return;
659 DoCancel(error, ssl_info);
662 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
663 DCHECK(error < 0);
664 // If cancelled while calling a delegate, clear delegate info.
665 if (calling_delegate_) {
666 LogUnblocked();
667 OnCallToDelegateComplete();
670 // If the URL request already has an error status, then canceling is a no-op.
671 // Plus, we don't want to change the error status once it has been set.
672 if (status_.is_success()) {
673 status_.set_status(URLRequestStatus::CANCELED);
674 status_.set_error(error);
675 response_info_.ssl_info = ssl_info;
677 // If the request hasn't already been completed, log a cancellation event.
678 if (!has_notified_completion_) {
679 // Don't log an error code on ERR_ABORTED, since that's redundant.
680 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
681 error == ERR_ABORTED ? OK : error);
685 if (is_pending_ && job_.get())
686 job_->Kill();
688 // We need to notify about the end of this job here synchronously. The
689 // Job sends an asynchronous notification but by the time this is processed,
690 // our |context_| is NULL.
691 NotifyRequestCompleted();
693 // The Job will call our NotifyDone method asynchronously. This is done so
694 // that the Delegate implementation can call Cancel without having to worry
695 // about being called recursively.
698 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
699 DCHECK(job_.get());
700 DCHECK(bytes_read);
701 *bytes_read = 0;
703 // If this is the first read, end the delegate call that may have started in
704 // OnResponseStarted.
705 OnCallToDelegateComplete();
707 // This handles a cancel that happens while paused.
708 // TODO(ahendrickson): DCHECK() that it is not done after
709 // http://crbug.com/115705 is fixed.
710 if (job_->is_done())
711 return false;
713 if (dest_size == 0) {
714 // Caller is not too bright. I guess we've done what they asked.
715 return true;
718 // Once the request fails or is cancelled, read will just return 0 bytes
719 // to indicate end of stream.
720 if (!status_.is_success()) {
721 return true;
724 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
725 tracked_objects::ScopedTracker tracking_profile1(
726 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read1"));
728 bool rv = job_->Read(dest, dest_size, bytes_read);
729 // If rv is false, the status cannot be success.
730 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
732 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
733 tracked_objects::ScopedTracker tracking_profile2(
734 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read2"));
736 if (rv && *bytes_read <= 0 && status_.is_success())
737 NotifyRequestCompleted();
738 return rv;
741 void URLRequest::StopCaching() {
742 DCHECK(job_.get());
743 job_->StopCaching();
746 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
747 bool* defer_redirect) {
748 is_redirecting_ = true;
750 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
751 URLRequestJob* job =
752 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
753 this, network_delegate_, redirect_info.new_url);
754 if (job) {
755 RestartWithJob(job);
756 } else if (delegate_) {
757 OnCallToDelegate();
759 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
760 tracked_objects::ScopedTracker tracking_profile(
761 FROM_HERE_WITH_EXPLICIT_FUNCTION(
762 "423948 URLRequest::Delegate::OnReceivedRedirect"));
763 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
764 // |this| may be have been destroyed here.
768 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
769 if (delegate_ && !notified_before_network_start_) {
770 OnCallToDelegate();
772 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
773 // fixed.
774 tracked_objects::ScopedTracker tracking_profile(
775 FROM_HERE_WITH_EXPLICIT_FUNCTION(
776 "423948 URLRequest::Delegate::OnBeforeNetworkStart"));
777 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 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
820 // fixed.
821 tracked_objects::ScopedTracker tracking_profile(
822 FROM_HERE_WITH_EXPLICIT_FUNCTION(
823 "423948 URLRequest::Delegate::OnResponseStarted"));
824 delegate_->OnResponseStarted(this);
825 // Nothing may appear below this line as OnResponseStarted may delete
826 // |this|.
831 void URLRequest::FollowDeferredRedirect() {
832 CHECK(job_.get());
833 CHECK(status_.is_success());
835 job_->FollowDeferredRedirect();
838 void URLRequest::SetAuth(const AuthCredentials& credentials) {
839 DCHECK(job_.get());
840 DCHECK(job_->NeedsAuth());
842 job_->SetAuth(credentials);
845 void URLRequest::CancelAuth() {
846 DCHECK(job_.get());
847 DCHECK(job_->NeedsAuth());
849 job_->CancelAuth();
852 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
853 DCHECK(job_.get());
855 job_->ContinueWithCertificate(client_cert);
858 void URLRequest::ContinueDespiteLastError() {
859 DCHECK(job_.get());
861 job_->ContinueDespiteLastError();
864 void URLRequest::PrepareToRestart() {
865 DCHECK(job_.get());
867 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
868 // one.
869 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
871 OrphanJob();
873 response_info_ = HttpResponseInfo();
874 response_info_.request_time = base::Time::Now();
876 load_timing_info_ = LoadTimingInfo();
877 load_timing_info_.request_start_time = response_info_.request_time;
878 load_timing_info_.request_start = base::TimeTicks::Now();
880 status_ = URLRequestStatus();
881 is_pending_ = false;
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_.IsLogging()) {
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 referrer_ = redirect_info.new_referrer;
944 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies;
946 url_chain_.push_back(redirect_info.new_url);
947 --redirect_limit_;
949 Start();
950 return OK;
953 const URLRequestContext* URLRequest::context() const {
954 return context_;
957 int64 URLRequest::GetExpectedContentSize() const {
958 int64 expected_content_size = -1;
959 if (job_.get())
960 expected_content_size = job_->expected_content_size();
962 return expected_content_size;
965 void URLRequest::SetPriority(RequestPriority priority) {
966 DCHECK_GE(priority, MINIMUM_PRIORITY);
967 DCHECK_LE(priority, MAXIMUM_PRIORITY);
969 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
970 NOTREACHED();
971 // Maintain the invariant that requests with IGNORE_LIMITS set
972 // have MAXIMUM_PRIORITY for release mode.
973 return;
976 if (priority_ == priority)
977 return;
979 priority_ = priority;
980 if (job_.get()) {
981 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
982 NetLog::IntegerCallback("priority", priority_));
983 job_->SetPriority(priority_);
987 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
988 const GURL& url = this->url();
989 bool scheme_is_http = url.SchemeIs("http");
990 if (!scheme_is_http && !url.SchemeIs("ws"))
991 return false;
992 TransportSecurityState* state = context()->transport_security_state();
993 if (state && state->ShouldUpgradeToSSL(url.host())) {
994 GURL::Replacements replacements;
995 const char* new_scheme = scheme_is_http ? "https" : "wss";
996 replacements.SetSchemeStr(new_scheme);
997 *redirect_url = url.ReplaceComponents(replacements);
998 return true;
1000 return false;
1003 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1004 NetworkDelegate::AuthRequiredResponse rv =
1005 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1006 auth_info_ = auth_info;
1007 if (network_delegate_) {
1008 OnCallToDelegate();
1009 rv = network_delegate_->NotifyAuthRequired(
1010 this,
1011 *auth_info,
1012 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1013 base::Unretained(this)),
1014 &auth_credentials_);
1015 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1016 return;
1019 NotifyAuthRequiredComplete(rv);
1022 void URLRequest::NotifyAuthRequiredComplete(
1023 NetworkDelegate::AuthRequiredResponse result) {
1024 OnCallToDelegateComplete();
1026 // Check that there are no callbacks to already canceled requests.
1027 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1029 // NotifyAuthRequired may be called multiple times, such as
1030 // when an authentication attempt fails. Clear out the data
1031 // so it can be reset on another round.
1032 AuthCredentials credentials = auth_credentials_;
1033 auth_credentials_ = AuthCredentials();
1034 scoped_refptr<AuthChallengeInfo> auth_info;
1035 auth_info.swap(auth_info_);
1037 switch (result) {
1038 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1039 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1040 // didn't take an action.
1041 if (delegate_) {
1042 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
1043 // fixed.
1044 tracked_objects::ScopedTracker tracking_profile(
1045 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1046 "423948 URLRequest::Delegate::OnAuthRequired"));
1047 delegate_->OnAuthRequired(this, auth_info.get());
1049 break;
1051 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1052 SetAuth(credentials);
1053 break;
1055 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1056 CancelAuth();
1057 break;
1059 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1060 NOTREACHED();
1061 break;
1065 void URLRequest::NotifyCertificateRequested(
1066 SSLCertRequestInfo* cert_request_info) {
1067 if (delegate_) {
1068 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1069 tracked_objects::ScopedTracker tracking_profile(
1070 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1071 "423948 URLRequest::Delegate::OnCertificateRequested"));
1072 delegate_->OnCertificateRequested(this, cert_request_info);
1076 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1077 bool fatal) {
1078 if (delegate_) {
1079 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1080 tracked_objects::ScopedTracker tracking_profile(
1081 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1082 "423948 URLRequest::Delegate::OnSSLCertificateError"));
1083 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 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1127 tracked_objects::ScopedTracker tracking_profile(
1128 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1129 "423948 URLRequest::Delegate::OnReadCompleted"));
1130 delegate_->OnReadCompleted(this, bytes_read);
1133 // Nothing below this line as OnReadCompleted may delete |this|.
1136 void URLRequest::OnHeadersComplete() {
1137 // Cache load timing information now, as information will be lost once the
1138 // socket is closed and the ClientSocketHandle is Reset, which will happen
1139 // once the body is complete. The start times should already be populated.
1140 if (job_.get()) {
1141 // Keep a copy of the two times the URLRequest sets.
1142 base::TimeTicks request_start = load_timing_info_.request_start;
1143 base::Time request_start_time = load_timing_info_.request_start_time;
1145 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1146 // consistent place to start from.
1147 load_timing_info_ = LoadTimingInfo();
1148 job_->GetLoadTimingInfo(&load_timing_info_);
1150 load_timing_info_.request_start = request_start;
1151 load_timing_info_.request_start_time = request_start_time;
1153 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1157 void URLRequest::NotifyRequestCompleted() {
1158 // TODO(battre): Get rid of this check, according to willchan it should
1159 // not be needed.
1160 if (has_notified_completion_)
1161 return;
1163 is_pending_ = false;
1164 is_redirecting_ = false;
1165 has_notified_completion_ = true;
1166 if (network_delegate_)
1167 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1170 void URLRequest::OnCallToDelegate() {
1171 DCHECK(!calling_delegate_);
1172 DCHECK(blocked_by_.empty());
1173 calling_delegate_ = true;
1174 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1177 void URLRequest::OnCallToDelegateComplete() {
1178 // This should have been cleared before resuming the request.
1179 DCHECK(blocked_by_.empty());
1180 if (!calling_delegate_)
1181 return;
1182 calling_delegate_ = false;
1183 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1186 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1187 base::debug::StackTrace* stack_trace_copy =
1188 new base::debug::StackTrace(NULL, 0);
1189 *stack_trace_copy = stack_trace;
1190 stack_trace_.reset(stack_trace_copy);
1193 const base::debug::StackTrace* URLRequest::stack_trace() const {
1194 return stack_trace_.get();
1197 } // namespace net