ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / net / url_request / url_request.cc
blob8351029509a28d65adb2290377b542281c8afed5
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->Cancel();
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 CookieStore* cookie_store,
523 NetworkDelegate* network_delegate)
524 : context_(context),
525 network_delegate_(network_delegate ? network_delegate
526 : context->network_delegate()),
527 net_log_(BoundNetLog::Make(context->net_log(),
528 NetLog::SOURCE_URL_REQUEST)),
529 url_chain_(1, url),
530 method_("GET"),
531 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
532 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
533 load_flags_(LOAD_NORMAL),
534 delegate_(delegate),
535 is_pending_(false),
536 is_redirecting_(false),
537 redirect_limit_(kMaxRedirects),
538 priority_(priority),
539 identifier_(GenerateURLRequestIdentifier()),
540 calling_delegate_(false),
541 use_blocked_by_as_load_param_(false),
542 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
543 base::Unretained(this))),
544 has_notified_completion_(false),
545 received_response_content_length_(0),
546 creation_time_(base::TimeTicks::Now()),
547 notified_before_network_start_(false),
548 cookie_store_(cookie_store ? cookie_store : context->cookie_store()) {
549 // Sanity check out environment.
550 DCHECK(base::MessageLoop::current())
551 << "The current base::MessageLoop must exist";
553 context->url_requests()->insert(this);
554 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
557 void URLRequest::BeforeRequestComplete(int error) {
558 DCHECK(!job_.get());
559 DCHECK_NE(ERR_IO_PENDING, error);
561 // Check that there are no callbacks to already canceled requests.
562 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
564 OnCallToDelegateComplete();
566 if (error != OK) {
567 std::string source("delegate");
568 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
569 NetLog::StringCallback("source", &source));
570 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
571 } else if (!delegate_redirect_url_.is_empty()) {
572 GURL new_url;
573 new_url.Swap(&delegate_redirect_url_);
575 URLRequestRedirectJob* job = new URLRequestRedirectJob(
576 this, network_delegate_, new_url,
577 // Use status code 307 to preserve the method, so POST requests work.
578 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
579 StartJob(job);
580 } else {
581 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
582 this, network_delegate_));
586 void URLRequest::StartJob(URLRequestJob* job) {
587 DCHECK(!is_pending_);
588 DCHECK(!job_.get());
590 net_log_.BeginEvent(
591 NetLog::TYPE_URL_REQUEST_START_JOB,
592 base::Bind(&NetLogURLRequestStartCallback,
593 &url(), &method_, load_flags_, priority_,
594 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
596 job_ = job;
597 job_->SetExtraRequestHeaders(extra_request_headers_);
598 job_->SetPriority(priority_);
600 if (upload_data_stream_.get())
601 job_->SetUpload(upload_data_stream_.get());
603 is_pending_ = true;
604 is_redirecting_ = false;
606 response_info_.was_cached = false;
608 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect(
609 referrer_policy_, referrer_, url())) {
610 if (!network_delegate_ ||
611 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
612 *this, url(), GURL(referrer_))) {
613 referrer_.clear();
614 } else {
615 // We need to clear the referrer anyway to avoid an infinite recursion
616 // when starting the error job.
617 referrer_.clear();
618 std::string source("delegate");
619 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
620 NetLog::StringCallback("source", &source));
621 RestartWithJob(new URLRequestErrorJob(
622 this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
623 return;
627 // Don't allow errors to be sent from within Start().
628 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
629 // we probably don't want this: they should be sent asynchronously so
630 // the caller does not get reentered.
631 job_->Start();
634 void URLRequest::Restart() {
635 // Should only be called if the original job didn't make any progress.
636 DCHECK(job_.get() && !job_->has_response_started());
637 RestartWithJob(
638 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
641 void URLRequest::RestartWithJob(URLRequestJob *job) {
642 DCHECK(job->request() == this);
643 PrepareToRestart();
644 StartJob(job);
647 void URLRequest::Cancel() {
648 DoCancel(ERR_ABORTED, SSLInfo());
651 void URLRequest::CancelWithError(int error) {
652 DoCancel(error, SSLInfo());
655 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
656 // This should only be called on a started request.
657 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
658 NOTREACHED();
659 return;
661 DoCancel(error, ssl_info);
664 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
665 DCHECK(error < 0);
666 // If cancelled while calling a delegate, clear delegate info.
667 if (calling_delegate_) {
668 LogUnblocked();
669 OnCallToDelegateComplete();
672 // If the URL request already has an error status, then canceling is a no-op.
673 // Plus, we don't want to change the error status once it has been set.
674 if (status_.is_success()) {
675 status_.set_status(URLRequestStatus::CANCELED);
676 status_.set_error(error);
677 response_info_.ssl_info = ssl_info;
679 // If the request hasn't already been completed, log a cancellation event.
680 if (!has_notified_completion_) {
681 // Don't log an error code on ERR_ABORTED, since that's redundant.
682 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
683 error == ERR_ABORTED ? OK : error);
687 if (is_pending_ && job_.get())
688 job_->Kill();
690 // We need to notify about the end of this job here synchronously. The
691 // Job sends an asynchronous notification but by the time this is processed,
692 // our |context_| is NULL.
693 NotifyRequestCompleted();
695 // The Job will call our NotifyDone method asynchronously. This is done so
696 // that the Delegate implementation can call Cancel without having to worry
697 // about being called recursively.
700 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
701 DCHECK(job_.get());
702 DCHECK(bytes_read);
703 *bytes_read = 0;
705 // If this is the first read, end the delegate call that may have started in
706 // OnResponseStarted.
707 OnCallToDelegateComplete();
709 // This handles a cancel that happens while paused.
710 // TODO(ahendrickson): DCHECK() that it is not done after
711 // http://crbug.com/115705 is fixed.
712 if (job_->is_done())
713 return false;
715 if (dest_size == 0) {
716 // Caller is not too bright. I guess we've done what they asked.
717 return true;
720 // Once the request fails or is cancelled, read will just return 0 bytes
721 // to indicate end of stream.
722 if (!status_.is_success()) {
723 return true;
726 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
727 tracked_objects::ScopedTracker tracking_profile1(
728 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read1"));
730 bool rv = job_->Read(dest, dest_size, bytes_read);
731 // If rv is false, the status cannot be success.
732 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
734 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
735 tracked_objects::ScopedTracker tracking_profile2(
736 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read2"));
738 if (rv && *bytes_read <= 0 && status_.is_success())
739 NotifyRequestCompleted();
740 return rv;
743 void URLRequest::StopCaching() {
744 DCHECK(job_.get());
745 job_->StopCaching();
748 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
749 bool* defer_redirect) {
750 is_redirecting_ = true;
752 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
753 URLRequestJob* job =
754 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
755 this, network_delegate_, redirect_info.new_url);
756 if (job) {
757 RestartWithJob(job);
758 } else if (delegate_) {
759 OnCallToDelegate();
761 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
762 tracked_objects::ScopedTracker tracking_profile(
763 FROM_HERE_WITH_EXPLICIT_FUNCTION(
764 "423948 URLRequest::Delegate::OnReceivedRedirect"));
765 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
766 // |this| may be have been destroyed here.
770 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
771 if (delegate_ && !notified_before_network_start_) {
772 OnCallToDelegate();
774 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
775 // fixed.
776 tracked_objects::ScopedTracker tracking_profile(
777 FROM_HERE_WITH_EXPLICIT_FUNCTION(
778 "423948 URLRequest::Delegate::OnBeforeNetworkStart"));
779 delegate_->OnBeforeNetworkStart(this, defer);
781 if (!*defer)
782 OnCallToDelegateComplete();
783 notified_before_network_start_ = true;
787 void URLRequest::ResumeNetworkStart() {
788 DCHECK(job_.get());
789 DCHECK(notified_before_network_start_);
791 OnCallToDelegateComplete();
792 job_->ResumeNetworkStart();
795 void URLRequest::NotifyResponseStarted() {
796 int net_error = OK;
797 if (!status_.is_success())
798 net_error = status_.error();
799 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
800 net_error);
802 URLRequestJob* job =
803 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
804 this, network_delegate_);
805 if (job) {
806 RestartWithJob(job);
807 } else {
808 if (delegate_) {
809 // In some cases (e.g. an event was canceled), we might have sent the
810 // completion event and receive a NotifyResponseStarted() later.
811 if (!has_notified_completion_ && status_.is_success()) {
812 if (network_delegate_)
813 network_delegate_->NotifyResponseStarted(this);
816 // Notify in case the entire URL Request has been finished.
817 if (!has_notified_completion_ && !status_.is_success())
818 NotifyRequestCompleted();
820 OnCallToDelegate();
821 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
822 // fixed.
823 tracked_objects::ScopedTracker tracking_profile(
824 FROM_HERE_WITH_EXPLICIT_FUNCTION(
825 "423948 URLRequest::Delegate::OnResponseStarted"));
826 delegate_->OnResponseStarted(this);
827 // Nothing may appear below this line as OnResponseStarted may delete
828 // |this|.
833 void URLRequest::FollowDeferredRedirect() {
834 CHECK(job_.get());
835 CHECK(status_.is_success());
837 job_->FollowDeferredRedirect();
840 void URLRequest::SetAuth(const AuthCredentials& credentials) {
841 DCHECK(job_.get());
842 DCHECK(job_->NeedsAuth());
844 job_->SetAuth(credentials);
847 void URLRequest::CancelAuth() {
848 DCHECK(job_.get());
849 DCHECK(job_->NeedsAuth());
851 job_->CancelAuth();
854 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
855 DCHECK(job_.get());
857 job_->ContinueWithCertificate(client_cert);
860 void URLRequest::ContinueDespiteLastError() {
861 DCHECK(job_.get());
863 job_->ContinueDespiteLastError();
866 void URLRequest::PrepareToRestart() {
867 DCHECK(job_.get());
869 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
870 // one.
871 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
873 OrphanJob();
875 response_info_ = HttpResponseInfo();
876 response_info_.request_time = base::Time::Now();
878 load_timing_info_ = LoadTimingInfo();
879 load_timing_info_.request_start_time = response_info_.request_time;
880 load_timing_info_.request_start = base::TimeTicks::Now();
882 status_ = URLRequestStatus();
883 is_pending_ = false;
886 void URLRequest::OrphanJob() {
887 // When calling this function, please check that URLRequestHttpJob is
888 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
889 // the call back. This is currently guaranteed by the following strategies:
890 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
891 // be receiving any headers at that time.
892 // - OrphanJob is called in ~URLRequest, in this case
893 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
894 // that the callback becomes invalid.
895 job_->Kill();
896 job_->DetachRequest(); // ensures that the job will not call us again
897 job_ = NULL;
900 int URLRequest::Redirect(const RedirectInfo& redirect_info) {
901 // Matches call in NotifyReceivedRedirect.
902 OnCallToDelegateComplete();
903 if (net_log_.IsLogging()) {
904 net_log_.AddEvent(
905 NetLog::TYPE_URL_REQUEST_REDIRECTED,
906 NetLog::StringCallback("location",
907 &redirect_info.new_url.possibly_invalid_spec()));
910 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
911 if (network_delegate_)
912 network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url);
914 if (redirect_limit_ <= 0) {
915 DVLOG(1) << "disallowing redirect: exceeds limit";
916 return ERR_TOO_MANY_REDIRECTS;
919 if (!redirect_info.new_url.is_valid())
920 return ERR_INVALID_URL;
922 if (!job_->IsSafeRedirect(redirect_info.new_url)) {
923 DVLOG(1) << "disallowing redirect: unsafe protocol";
924 return ERR_UNSAFE_REDIRECT;
927 if (!final_upload_progress_.position())
928 final_upload_progress_ = job_->GetUploadProgress();
929 PrepareToRestart();
931 if (redirect_info.new_method != method_) {
932 // TODO(davidben): This logic still needs to be replicated at the consumers.
933 if (method_ == "POST") {
934 // If being switched from POST, must remove headers that were specific to
935 // the POST and don't have meaning in other methods. For example the
936 // inclusion of a multipart Content-Type header in GET can cause problems
937 // with some servers:
938 // http://code.google.com/p/chromium/issues/detail?id=843
939 StripPostSpecificHeaders(&extra_request_headers_);
941 upload_data_stream_.reset();
942 method_ = redirect_info.new_method;
945 referrer_ = redirect_info.new_referrer;
946 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies;
948 url_chain_.push_back(redirect_info.new_url);
949 --redirect_limit_;
951 Start();
952 return OK;
955 const URLRequestContext* URLRequest::context() const {
956 return context_;
959 int64 URLRequest::GetExpectedContentSize() const {
960 int64 expected_content_size = -1;
961 if (job_.get())
962 expected_content_size = job_->expected_content_size();
964 return expected_content_size;
967 void URLRequest::SetPriority(RequestPriority priority) {
968 DCHECK_GE(priority, MINIMUM_PRIORITY);
969 DCHECK_LE(priority, MAXIMUM_PRIORITY);
971 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
972 NOTREACHED();
973 // Maintain the invariant that requests with IGNORE_LIMITS set
974 // have MAXIMUM_PRIORITY for release mode.
975 return;
978 if (priority_ == priority)
979 return;
981 priority_ = priority;
982 if (job_.get()) {
983 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
984 NetLog::IntegerCallback("priority", priority_));
985 job_->SetPriority(priority_);
989 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
990 const GURL& url = this->url();
991 bool scheme_is_http = url.SchemeIs("http");
992 if (!scheme_is_http && !url.SchemeIs("ws"))
993 return false;
994 TransportSecurityState* state = context()->transport_security_state();
995 if (state && state->ShouldUpgradeToSSL(url.host())) {
996 GURL::Replacements replacements;
997 const char* new_scheme = scheme_is_http ? "https" : "wss";
998 replacements.SetSchemeStr(new_scheme);
999 *redirect_url = url.ReplaceComponents(replacements);
1000 return true;
1002 return false;
1005 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1006 NetworkDelegate::AuthRequiredResponse rv =
1007 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1008 auth_info_ = auth_info;
1009 if (network_delegate_) {
1010 OnCallToDelegate();
1011 rv = network_delegate_->NotifyAuthRequired(
1012 this,
1013 *auth_info,
1014 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1015 base::Unretained(this)),
1016 &auth_credentials_);
1017 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1018 return;
1021 NotifyAuthRequiredComplete(rv);
1024 void URLRequest::NotifyAuthRequiredComplete(
1025 NetworkDelegate::AuthRequiredResponse result) {
1026 OnCallToDelegateComplete();
1028 // Check that there are no callbacks to already canceled requests.
1029 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1031 // NotifyAuthRequired may be called multiple times, such as
1032 // when an authentication attempt fails. Clear out the data
1033 // so it can be reset on another round.
1034 AuthCredentials credentials = auth_credentials_;
1035 auth_credentials_ = AuthCredentials();
1036 scoped_refptr<AuthChallengeInfo> auth_info;
1037 auth_info.swap(auth_info_);
1039 switch (result) {
1040 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1041 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1042 // didn't take an action.
1043 if (delegate_) {
1044 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
1045 // fixed.
1046 tracked_objects::ScopedTracker tracking_profile(
1047 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1048 "423948 URLRequest::Delegate::OnAuthRequired"));
1049 delegate_->OnAuthRequired(this, auth_info.get());
1051 break;
1053 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1054 SetAuth(credentials);
1055 break;
1057 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1058 CancelAuth();
1059 break;
1061 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1062 NOTREACHED();
1063 break;
1067 void URLRequest::NotifyCertificateRequested(
1068 SSLCertRequestInfo* cert_request_info) {
1069 if (delegate_) {
1070 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1071 tracked_objects::ScopedTracker tracking_profile(
1072 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1073 "423948 URLRequest::Delegate::OnCertificateRequested"));
1074 delegate_->OnCertificateRequested(this, cert_request_info);
1078 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1079 bool fatal) {
1080 if (delegate_) {
1081 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1082 tracked_objects::ScopedTracker tracking_profile(
1083 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1084 "423948 URLRequest::Delegate::OnSSLCertificateError"));
1085 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1089 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1090 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1091 if (network_delegate_) {
1092 return network_delegate_->CanGetCookies(*this, cookie_list);
1094 return g_default_can_use_cookies;
1097 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1098 CookieOptions* options) const {
1099 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1100 if (network_delegate_) {
1101 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1103 return g_default_can_use_cookies;
1106 bool URLRequest::CanEnablePrivacyMode() const {
1107 if (network_delegate_) {
1108 return network_delegate_->CanEnablePrivacyMode(url(),
1109 first_party_for_cookies_);
1111 return !g_default_can_use_cookies;
1115 void URLRequest::NotifyReadCompleted(int bytes_read) {
1116 // Notify in case the entire URL Request has been finished.
1117 if (bytes_read <= 0)
1118 NotifyRequestCompleted();
1120 // Notify NetworkChangeNotifier that we just received network data.
1121 // This is to identify cases where the NetworkChangeNotifier thinks we
1122 // are off-line but we are still receiving network data (crbug.com/124069),
1123 // and to get rough network connection measurements.
1124 if (bytes_read > 0 && !was_cached())
1125 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1127 if (delegate_) {
1128 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1129 tracked_objects::ScopedTracker tracking_profile(
1130 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1131 "423948 URLRequest::Delegate::OnReadCompleted"));
1132 delegate_->OnReadCompleted(this, bytes_read);
1135 // Nothing below this line as OnReadCompleted may delete |this|.
1138 void URLRequest::OnHeadersComplete() {
1139 // Cache load timing information now, as information will be lost once the
1140 // socket is closed and the ClientSocketHandle is Reset, which will happen
1141 // once the body is complete. The start times should already be populated.
1142 if (job_.get()) {
1143 // Keep a copy of the two times the URLRequest sets.
1144 base::TimeTicks request_start = load_timing_info_.request_start;
1145 base::Time request_start_time = load_timing_info_.request_start_time;
1147 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1148 // consistent place to start from.
1149 load_timing_info_ = LoadTimingInfo();
1150 job_->GetLoadTimingInfo(&load_timing_info_);
1152 load_timing_info_.request_start = request_start;
1153 load_timing_info_.request_start_time = request_start_time;
1155 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1159 void URLRequest::NotifyRequestCompleted() {
1160 // TODO(battre): Get rid of this check, according to willchan it should
1161 // not be needed.
1162 if (has_notified_completion_)
1163 return;
1165 is_pending_ = false;
1166 is_redirecting_ = false;
1167 has_notified_completion_ = true;
1168 if (network_delegate_)
1169 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1172 void URLRequest::OnCallToDelegate() {
1173 DCHECK(!calling_delegate_);
1174 DCHECK(blocked_by_.empty());
1175 calling_delegate_ = true;
1176 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1179 void URLRequest::OnCallToDelegateComplete() {
1180 // This should have been cleared before resuming the request.
1181 DCHECK(blocked_by_.empty());
1182 if (!calling_delegate_)
1183 return;
1184 calling_delegate_ = false;
1185 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1188 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1189 base::debug::StackTrace* stack_trace_copy =
1190 new base::debug::StackTrace(NULL, 0);
1191 *stack_trace_copy = stack_trace;
1192 stack_trace_.reset(stack_trace_copy);
1195 const base::debug::StackTrace* URLRequest::stack_trace() const {
1196 return stack_trace_.get();
1199 } // namespace net