Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / net / url_request / url_request.cc
blob4a1e09346d8e936b577b60eeda2e77f42b838b50
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(
98 net::LoadTimingInfo* load_timing_info) {
99 DCHECK(!load_timing_info->request_start.is_null());
101 // Earliest time possible for the request to be blocking on connect events.
102 base::TimeTicks block_on_connect = load_timing_info->request_start;
104 if (!load_timing_info->proxy_resolve_start.is_null()) {
105 DCHECK(!load_timing_info->proxy_resolve_end.is_null());
107 // Make sure the proxy times are after request start.
108 if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
109 load_timing_info->proxy_resolve_start = load_timing_info->request_start;
110 if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
111 load_timing_info->proxy_resolve_end = load_timing_info->request_start;
113 // Connect times must also be after the proxy times.
114 block_on_connect = load_timing_info->proxy_resolve_end;
117 // Make sure connection times are after start and proxy times.
119 net::LoadTimingInfo::ConnectTiming* connect_timing =
120 &load_timing_info->connect_timing;
121 if (!connect_timing->dns_start.is_null()) {
122 DCHECK(!connect_timing->dns_end.is_null());
123 if (connect_timing->dns_start < block_on_connect)
124 connect_timing->dns_start = block_on_connect;
125 if (connect_timing->dns_end < block_on_connect)
126 connect_timing->dns_end = block_on_connect;
129 if (!connect_timing->connect_start.is_null()) {
130 DCHECK(!connect_timing->connect_end.is_null());
131 if (connect_timing->connect_start < block_on_connect)
132 connect_timing->connect_start = block_on_connect;
133 if (connect_timing->connect_end < block_on_connect)
134 connect_timing->connect_end = block_on_connect;
137 if (!connect_timing->ssl_start.is_null()) {
138 DCHECK(!connect_timing->ssl_end.is_null());
139 if (connect_timing->ssl_start < block_on_connect)
140 connect_timing->ssl_start = block_on_connect;
141 if (connect_timing->ssl_end < block_on_connect)
142 connect_timing->ssl_end = block_on_connect;
146 } // namespace
148 ///////////////////////////////////////////////////////////////////////////////
149 // URLRequest::Delegate
151 void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
152 const RedirectInfo& redirect_info,
153 bool* defer_redirect) {
156 void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
157 AuthChallengeInfo* auth_info) {
158 request->CancelAuth();
161 void URLRequest::Delegate::OnCertificateRequested(
162 URLRequest* request,
163 SSLCertRequestInfo* cert_request_info) {
164 request->CancelWithError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
167 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
168 const SSLInfo& ssl_info,
169 bool is_hsts_ok) {
170 request->Cancel();
173 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request,
174 bool* defer) {
177 ///////////////////////////////////////////////////////////////////////////////
178 // URLRequest
180 URLRequest::~URLRequest() {
181 Cancel();
183 if (network_delegate_) {
184 network_delegate_->NotifyURLRequestDestroyed(this);
185 if (job_.get())
186 job_->NotifyURLRequestDestroyed();
189 if (job_.get())
190 OrphanJob();
192 int deleted = context_->url_requests()->erase(this);
193 CHECK_EQ(1, deleted);
195 int net_error = OK;
196 // Log error only on failure, not cancellation, as even successful requests
197 // are "cancelled" on destruction.
198 if (status_.status() == URLRequestStatus::FAILED)
199 net_error = status_.error();
200 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
203 void URLRequest::EnableChunkedUpload() {
204 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked());
205 if (!upload_data_stream_) {
206 upload_chunked_data_stream_ = new ChunkedUploadDataStream(0);
207 upload_data_stream_.reset(upload_chunked_data_stream_);
211 void URLRequest::AppendChunkToUpload(const char* bytes,
212 int bytes_len,
213 bool is_last_chunk) {
214 DCHECK(upload_data_stream_);
215 DCHECK(upload_data_stream_->is_chunked());
216 upload_chunked_data_stream_->AppendData(bytes, bytes_len, is_last_chunk);
219 void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) {
220 upload_data_stream_ = upload.Pass();
223 const UploadDataStream* URLRequest::get_upload() const {
224 return upload_data_stream_.get();
227 bool URLRequest::has_upload() const {
228 return upload_data_stream_.get() != NULL;
231 void URLRequest::SetExtraRequestHeaderByName(const string& name,
232 const string& value,
233 bool overwrite) {
234 DCHECK(!is_pending_ || is_redirecting_);
235 if (overwrite) {
236 extra_request_headers_.SetHeader(name, value);
237 } else {
238 extra_request_headers_.SetHeaderIfMissing(name, value);
242 void URLRequest::RemoveRequestHeaderByName(const string& name) {
243 DCHECK(!is_pending_ || is_redirecting_);
244 extra_request_headers_.RemoveHeader(name);
247 void URLRequest::SetExtraRequestHeaders(
248 const HttpRequestHeaders& headers) {
249 DCHECK(!is_pending_);
250 extra_request_headers_ = headers;
252 // NOTE: This method will likely become non-trivial once the other setters
253 // for request headers are implemented.
256 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
257 if (!job_.get())
258 return false;
260 return job_->GetFullRequestHeaders(headers);
263 int64 URLRequest::GetTotalReceivedBytes() const {
264 if (!job_.get())
265 return 0;
267 return job_->GetTotalReceivedBytes();
270 LoadStateWithParam URLRequest::GetLoadState() const {
271 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
272 // delegate before it has been started.
273 if (calling_delegate_ || !blocked_by_.empty()) {
274 return LoadStateWithParam(
275 LOAD_STATE_WAITING_FOR_DELEGATE,
276 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
277 base::string16());
279 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
280 base::string16());
283 base::Value* URLRequest::GetStateAsValue() const {
284 base::DictionaryValue* dict = new base::DictionaryValue();
285 dict->SetString("url", original_url().possibly_invalid_spec());
287 if (url_chain_.size() > 1) {
288 base::ListValue* list = new base::ListValue();
289 for (std::vector<GURL>::const_iterator url = url_chain_.begin();
290 url != url_chain_.end(); ++url) {
291 list->AppendString(url->possibly_invalid_spec());
293 dict->Set("url_chain", list);
296 dict->SetInteger("load_flags", load_flags_);
298 LoadStateWithParam load_state = GetLoadState();
299 dict->SetInteger("load_state", load_state.state);
300 if (!load_state.param.empty())
301 dict->SetString("load_state_param", load_state.param);
302 if (!blocked_by_.empty())
303 dict->SetString("delegate_info", blocked_by_);
305 dict->SetString("method", method_);
306 dict->SetBoolean("has_upload", has_upload());
307 dict->SetBoolean("is_pending", is_pending_);
309 // Add the status of the request. The status should always be IO_PENDING, and
310 // the error should always be OK, unless something is holding onto a request
311 // that has finished or a request was leaked. Neither of these should happen.
312 switch (status_.status()) {
313 case URLRequestStatus::SUCCESS:
314 dict->SetString("status", "SUCCESS");
315 break;
316 case URLRequestStatus::IO_PENDING:
317 dict->SetString("status", "IO_PENDING");
318 break;
319 case URLRequestStatus::CANCELED:
320 dict->SetString("status", "CANCELED");
321 break;
322 case URLRequestStatus::FAILED:
323 dict->SetString("status", "FAILED");
324 break;
326 if (status_.error() != OK)
327 dict->SetInteger("net_error", status_.error());
328 return dict;
331 void URLRequest::LogBlockedBy(const char* blocked_by) {
332 DCHECK(blocked_by);
333 DCHECK_GT(strlen(blocked_by), 0u);
335 // Only log information to NetLog during startup and certain deferring calls
336 // to delegates. For all reads but the first, do nothing.
337 if (!calling_delegate_ && !response_info_.request_time.is_null())
338 return;
340 LogUnblocked();
341 blocked_by_ = blocked_by;
342 use_blocked_by_as_load_param_ = false;
344 net_log_.BeginEvent(
345 NetLog::TYPE_DELEGATE_INFO,
346 NetLog::StringCallback("delegate_info", &blocked_by_));
349 void URLRequest::LogAndReportBlockedBy(const char* source) {
350 LogBlockedBy(source);
351 use_blocked_by_as_load_param_ = true;
354 void URLRequest::LogUnblocked() {
355 if (blocked_by_.empty())
356 return;
358 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
359 blocked_by_.clear();
362 UploadProgress URLRequest::GetUploadProgress() const {
363 if (!job_.get()) {
364 // We haven't started or the request was cancelled
365 return UploadProgress();
367 if (final_upload_progress_.position()) {
368 // The first job completed and none of the subsequent series of
369 // GETs when following redirects will upload anything, so we return the
370 // cached results from the initial job, the POST.
371 return final_upload_progress_;
373 return job_->GetUploadProgress();
376 void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
377 DCHECK(value);
378 if (response_info_.headers.get()) {
379 response_info_.headers->GetNormalizedHeader(name, value);
380 } else {
381 value->clear();
385 HostPortPair URLRequest::GetSocketAddress() const {
386 DCHECK(job_.get());
387 return job_->GetSocketAddress();
390 HttpResponseHeaders* URLRequest::response_headers() const {
391 return response_info_.headers.get();
394 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
395 *load_timing_info = load_timing_info_;
398 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
399 DCHECK(job_.get());
400 return job_->GetResponseCookies(cookies);
403 void URLRequest::GetMimeType(string* mime_type) const {
404 DCHECK(job_.get());
405 job_->GetMimeType(mime_type);
408 void URLRequest::GetCharset(string* charset) const {
409 DCHECK(job_.get());
410 job_->GetCharset(charset);
413 int URLRequest::GetResponseCode() const {
414 DCHECK(job_.get());
415 return job_->GetResponseCode();
418 void URLRequest::SetLoadFlags(int flags) {
419 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
420 DCHECK(!job_.get());
421 DCHECK(flags & LOAD_IGNORE_LIMITS);
422 DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
424 load_flags_ = flags;
426 // This should be a no-op given the above DCHECKs, but do this
427 // anyway for release mode.
428 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
429 SetPriority(MAXIMUM_PRIORITY);
432 // static
433 void URLRequest::SetDefaultCookiePolicyToBlock() {
434 CHECK(!g_url_requests_started);
435 g_default_can_use_cookies = false;
438 // static
439 bool URLRequest::IsHandledProtocol(const std::string& scheme) {
440 return URLRequestJobManager::SupportsScheme(scheme);
443 // static
444 bool URLRequest::IsHandledURL(const GURL& url) {
445 if (!url.is_valid()) {
446 // We handle error cases.
447 return true;
450 return IsHandledProtocol(url.scheme());
453 void URLRequest::set_first_party_for_cookies(
454 const GURL& first_party_for_cookies) {
455 DCHECK(!is_pending_);
456 first_party_for_cookies_ = first_party_for_cookies;
459 void URLRequest::set_first_party_url_policy(
460 FirstPartyURLPolicy first_party_url_policy) {
461 DCHECK(!is_pending_);
462 first_party_url_policy_ = first_party_url_policy;
465 void URLRequest::set_method(const std::string& method) {
466 DCHECK(!is_pending_);
467 method_ = method;
470 void URLRequest::SetReferrer(const std::string& referrer) {
471 DCHECK(!is_pending_);
472 GURL referrer_url(referrer);
473 if (referrer_url.is_valid()) {
474 referrer_ = referrer_url.GetAsReferrer().spec();
475 } else {
476 referrer_ = referrer;
480 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
481 DCHECK(!is_pending_);
482 referrer_policy_ = referrer_policy;
485 void URLRequest::set_delegate(Delegate* delegate) {
486 delegate_ = delegate;
489 void URLRequest::Start() {
490 // Some values can be NULL, but the job factory must not be.
491 DCHECK(context_->job_factory());
493 // Anything that sets |blocked_by_| before start should have cleaned up after
494 // itself.
495 DCHECK(blocked_by_.empty());
497 g_url_requests_started = true;
498 response_info_.request_time = base::Time::Now();
500 load_timing_info_ = LoadTimingInfo();
501 load_timing_info_.request_start_time = response_info_.request_time;
502 load_timing_info_.request_start = base::TimeTicks::Now();
504 // Only notify the delegate for the initial request.
505 if (network_delegate_) {
506 OnCallToDelegate();
507 int error = network_delegate_->NotifyBeforeURLRequest(
508 this, before_request_callback_, &delegate_redirect_url_);
509 // If ERR_IO_PENDING is returned, the delegate will invoke
510 // |before_request_callback_| later.
511 if (error != ERR_IO_PENDING)
512 BeforeRequestComplete(error);
513 return;
516 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
517 this, network_delegate_));
520 ///////////////////////////////////////////////////////////////////////////////
522 URLRequest::URLRequest(const GURL& url,
523 RequestPriority priority,
524 Delegate* delegate,
525 const URLRequestContext* context,
526 NetworkDelegate* network_delegate)
527 : context_(context),
528 network_delegate_(network_delegate ? network_delegate
529 : context->network_delegate()),
530 net_log_(
531 BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST)),
532 url_chain_(1, url),
533 method_("GET"),
534 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
535 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
536 load_flags_(LOAD_NORMAL),
537 delegate_(delegate),
538 is_pending_(false),
539 is_redirecting_(false),
540 redirect_limit_(kMaxRedirects),
541 priority_(priority),
542 identifier_(GenerateURLRequestIdentifier()),
543 calling_delegate_(false),
544 use_blocked_by_as_load_param_(false),
545 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
546 base::Unretained(this))),
547 has_notified_completion_(false),
548 received_response_content_length_(0),
549 creation_time_(base::TimeTicks::Now()),
550 notified_before_network_start_(false) {
551 // Sanity check out environment.
552 DCHECK(base::MessageLoop::current())
553 << "The current base::MessageLoop must exist";
555 context->url_requests()->insert(this);
556 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
559 void URLRequest::BeforeRequestComplete(int error) {
560 DCHECK(!job_.get());
561 DCHECK_NE(ERR_IO_PENDING, error);
563 // Check that there are no callbacks to already canceled requests.
564 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
566 OnCallToDelegateComplete();
568 if (error != OK) {
569 std::string source("delegate");
570 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
571 NetLog::StringCallback("source", &source));
572 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
573 } else if (!delegate_redirect_url_.is_empty()) {
574 GURL new_url;
575 new_url.Swap(&delegate_redirect_url_);
577 URLRequestRedirectJob* job = new URLRequestRedirectJob(
578 this, network_delegate_, new_url,
579 // Use status code 307 to preserve the method, so POST requests work.
580 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
581 StartJob(job);
582 } else {
583 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
584 this, network_delegate_));
588 void URLRequest::StartJob(URLRequestJob* job) {
589 DCHECK(!is_pending_);
590 DCHECK(!job_.get());
592 net_log_.BeginEvent(
593 NetLog::TYPE_URL_REQUEST_START_JOB,
594 base::Bind(&NetLogURLRequestStartCallback,
595 &url(), &method_, load_flags_, priority_,
596 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
598 job_ = job;
599 job_->SetExtraRequestHeaders(extra_request_headers_);
600 job_->SetPriority(priority_);
602 if (upload_data_stream_.get())
603 job_->SetUpload(upload_data_stream_.get());
605 is_pending_ = true;
606 is_redirecting_ = false;
608 response_info_.was_cached = false;
610 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect(
611 referrer_policy_, referrer_, url())) {
612 if (!network_delegate_ ||
613 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
614 *this, url(), GURL(referrer_))) {
615 referrer_.clear();
616 } else {
617 // We need to clear the referrer anyway to avoid an infinite recursion
618 // when starting the error job.
619 referrer_.clear();
620 std::string source("delegate");
621 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
622 NetLog::StringCallback("source", &source));
623 RestartWithJob(new URLRequestErrorJob(
624 this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
625 return;
629 // Don't allow errors to be sent from within Start().
630 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
631 // we probably don't want this: they should be sent asynchronously so
632 // the caller does not get reentered.
633 job_->Start();
636 void URLRequest::Restart() {
637 // Should only be called if the original job didn't make any progress.
638 DCHECK(job_.get() && !job_->has_response_started());
639 RestartWithJob(
640 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
643 void URLRequest::RestartWithJob(URLRequestJob *job) {
644 DCHECK(job->request() == this);
645 PrepareToRestart();
646 StartJob(job);
649 void URLRequest::Cancel() {
650 DoCancel(ERR_ABORTED, SSLInfo());
653 void URLRequest::CancelWithError(int error) {
654 DoCancel(error, SSLInfo());
657 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
658 // This should only be called on a started request.
659 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
660 NOTREACHED();
661 return;
663 DoCancel(error, ssl_info);
666 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
667 DCHECK(error < 0);
668 // If cancelled while calling a delegate, clear delegate info.
669 if (calling_delegate_) {
670 LogUnblocked();
671 OnCallToDelegateComplete();
674 // If the URL request already has an error status, then canceling is a no-op.
675 // Plus, we don't want to change the error status once it has been set.
676 if (status_.is_success()) {
677 status_.set_status(URLRequestStatus::CANCELED);
678 status_.set_error(error);
679 response_info_.ssl_info = ssl_info;
681 // If the request hasn't already been completed, log a cancellation event.
682 if (!has_notified_completion_) {
683 // Don't log an error code on ERR_ABORTED, since that's redundant.
684 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
685 error == ERR_ABORTED ? OK : error);
689 if (is_pending_ && job_.get())
690 job_->Kill();
692 // We need to notify about the end of this job here synchronously. The
693 // Job sends an asynchronous notification but by the time this is processed,
694 // our |context_| is NULL.
695 NotifyRequestCompleted();
697 // The Job will call our NotifyDone method asynchronously. This is done so
698 // that the Delegate implementation can call Cancel without having to worry
699 // about being called recursively.
702 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
703 DCHECK(job_.get());
704 DCHECK(bytes_read);
705 *bytes_read = 0;
707 // If this is the first read, end the delegate call that may have started in
708 // OnResponseStarted.
709 OnCallToDelegateComplete();
711 // This handles a cancel that happens while paused.
712 // TODO(ahendrickson): DCHECK() that it is not done after
713 // http://crbug.com/115705 is fixed.
714 if (job_->is_done())
715 return false;
717 if (dest_size == 0) {
718 // Caller is not too bright. I guess we've done what they asked.
719 return true;
722 // Once the request fails or is cancelled, read will just return 0 bytes
723 // to indicate end of stream.
724 if (!status_.is_success()) {
725 return true;
728 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
729 tracked_objects::ScopedTracker tracking_profile1(
730 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read1"));
732 bool rv = job_->Read(dest, dest_size, bytes_read);
733 // If rv is false, the status cannot be success.
734 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
736 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
737 tracked_objects::ScopedTracker tracking_profile2(
738 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read2"));
740 if (rv && *bytes_read <= 0 && status_.is_success())
741 NotifyRequestCompleted();
742 return rv;
745 void URLRequest::StopCaching() {
746 DCHECK(job_.get());
747 job_->StopCaching();
750 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
751 bool* defer_redirect) {
752 is_redirecting_ = true;
754 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
755 URLRequestJob* job =
756 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
757 this, network_delegate_, redirect_info.new_url);
758 if (job) {
759 RestartWithJob(job);
760 } else if (delegate_) {
761 OnCallToDelegate();
763 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
764 tracked_objects::ScopedTracker tracking_profile(
765 FROM_HERE_WITH_EXPLICIT_FUNCTION(
766 "423948 URLRequest::Delegate::OnReceivedRedirect"));
767 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
768 // |this| may be have been destroyed here.
772 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
773 if (delegate_ && !notified_before_network_start_) {
774 OnCallToDelegate();
776 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
777 // fixed.
778 tracked_objects::ScopedTracker tracking_profile(
779 FROM_HERE_WITH_EXPLICIT_FUNCTION(
780 "423948 URLRequest::Delegate::OnBeforeNetworkStart"));
781 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 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
824 // fixed.
825 tracked_objects::ScopedTracker tracking_profile(
826 FROM_HERE_WITH_EXPLICIT_FUNCTION(
827 "423948 URLRequest::Delegate::OnResponseStarted"));
828 delegate_->OnResponseStarted(this);
829 // Nothing may appear below this line as OnResponseStarted may delete
830 // |this|.
835 void URLRequest::FollowDeferredRedirect() {
836 CHECK(job_.get());
837 CHECK(status_.is_success());
839 job_->FollowDeferredRedirect();
842 void URLRequest::SetAuth(const AuthCredentials& credentials) {
843 DCHECK(job_.get());
844 DCHECK(job_->NeedsAuth());
846 job_->SetAuth(credentials);
849 void URLRequest::CancelAuth() {
850 DCHECK(job_.get());
851 DCHECK(job_->NeedsAuth());
853 job_->CancelAuth();
856 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
857 DCHECK(job_.get());
859 job_->ContinueWithCertificate(client_cert);
862 void URLRequest::ContinueDespiteLastError() {
863 DCHECK(job_.get());
865 job_->ContinueDespiteLastError();
868 void URLRequest::PrepareToRestart() {
869 DCHECK(job_.get());
871 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
872 // one.
873 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
875 OrphanJob();
877 response_info_ = HttpResponseInfo();
878 response_info_.request_time = base::Time::Now();
880 load_timing_info_ = LoadTimingInfo();
881 load_timing_info_.request_start_time = response_info_.request_time;
882 load_timing_info_.request_start = base::TimeTicks::Now();
884 status_ = URLRequestStatus();
885 is_pending_ = false;
888 void URLRequest::OrphanJob() {
889 // When calling this function, please check that URLRequestHttpJob is
890 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
891 // the call back. This is currently guaranteed by the following strategies:
892 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
893 // be receiving any headers at that time.
894 // - OrphanJob is called in ~URLRequest, in this case
895 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
896 // that the callback becomes invalid.
897 job_->Kill();
898 job_->DetachRequest(); // ensures that the job will not call us again
899 job_ = NULL;
902 int URLRequest::Redirect(const RedirectInfo& redirect_info) {
903 // Matches call in NotifyReceivedRedirect.
904 OnCallToDelegateComplete();
905 if (net_log_.IsLogging()) {
906 net_log_.AddEvent(
907 NetLog::TYPE_URL_REQUEST_REDIRECTED,
908 NetLog::StringCallback("location",
909 &redirect_info.new_url.possibly_invalid_spec()));
912 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
913 if (network_delegate_)
914 network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url);
916 if (redirect_limit_ <= 0) {
917 DVLOG(1) << "disallowing redirect: exceeds limit";
918 return ERR_TOO_MANY_REDIRECTS;
921 if (!redirect_info.new_url.is_valid())
922 return ERR_INVALID_URL;
924 if (!job_->IsSafeRedirect(redirect_info.new_url)) {
925 DVLOG(1) << "disallowing redirect: unsafe protocol";
926 return ERR_UNSAFE_REDIRECT;
929 if (!final_upload_progress_.position())
930 final_upload_progress_ = job_->GetUploadProgress();
931 PrepareToRestart();
933 if (redirect_info.new_method != method_) {
934 // TODO(davidben): This logic still needs to be replicated at the consumers.
935 if (method_ == "POST") {
936 // If being switched from POST, must remove headers that were specific to
937 // the POST and don't have meaning in other methods. For example the
938 // inclusion of a multipart Content-Type header in GET can cause problems
939 // with some servers:
940 // http://code.google.com/p/chromium/issues/detail?id=843
941 StripPostSpecificHeaders(&extra_request_headers_);
943 upload_data_stream_.reset();
944 method_ = redirect_info.new_method;
947 // Cross-origin redirects should not result in an Origin header value that is
948 // equal to the original request's Origin header. This is necessary to prevent
949 // a reflection of POST requests to bypass CSRF protections. If the header was
950 // not set to "null", a POST request from origin A to a malicious origin M
951 // could be redirected by M back to A.
953 // In the Section 4.2, Step 4.10 of the Fetch spec
954 // (https://fetch.spec.whatwg.org/#concept-http-fetch), it states that on
955 // cross-origin 301, 302, 303, 307, and 308 redirects, the user agent should
956 // set the request's origin to an "opaque identifier," which serializes to
957 // "null." This matches Firefox and IE behavior, although it supercedes the
958 // suggested behavior in RFC 6454, "The Web Origin Concept."
960 // See also https://crbug.com/465517.
962 // TODO(jww): This is probably layering violation and should be refactored
963 // into //content. See https://crbug.com/471397.
964 if (redirect_info.new_url.GetOrigin() != url().GetOrigin() &&
965 extra_request_headers_.HasHeader(HttpRequestHeaders::kOrigin)) {
966 extra_request_headers_.SetHeader(HttpRequestHeaders::kOrigin,
967 url::Origin().string());
970 referrer_ = redirect_info.new_referrer;
971 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies;
973 url_chain_.push_back(redirect_info.new_url);
974 --redirect_limit_;
976 Start();
977 return OK;
980 const URLRequestContext* URLRequest::context() const {
981 return context_;
984 int64 URLRequest::GetExpectedContentSize() const {
985 int64 expected_content_size = -1;
986 if (job_.get())
987 expected_content_size = job_->expected_content_size();
989 return expected_content_size;
992 void URLRequest::SetPriority(RequestPriority priority) {
993 DCHECK_GE(priority, MINIMUM_PRIORITY);
994 DCHECK_LE(priority, MAXIMUM_PRIORITY);
996 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
997 NOTREACHED();
998 // Maintain the invariant that requests with IGNORE_LIMITS set
999 // have MAXIMUM_PRIORITY for release mode.
1000 return;
1003 if (priority_ == priority)
1004 return;
1006 priority_ = priority;
1007 if (job_.get()) {
1008 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
1009 NetLog::IntegerCallback("priority", priority_));
1010 job_->SetPriority(priority_);
1014 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
1015 const GURL& url = this->url();
1016 bool scheme_is_http = url.SchemeIs("http");
1017 if (!scheme_is_http && !url.SchemeIs("ws"))
1018 return false;
1019 TransportSecurityState* state = context()->transport_security_state();
1020 if (state && state->ShouldUpgradeToSSL(url.host())) {
1021 GURL::Replacements replacements;
1022 const char* new_scheme = scheme_is_http ? "https" : "wss";
1023 replacements.SetSchemeStr(new_scheme);
1024 *redirect_url = url.ReplaceComponents(replacements);
1025 return true;
1027 return false;
1030 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1031 NetworkDelegate::AuthRequiredResponse rv =
1032 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1033 auth_info_ = auth_info;
1034 if (network_delegate_) {
1035 OnCallToDelegate();
1036 rv = network_delegate_->NotifyAuthRequired(
1037 this,
1038 *auth_info,
1039 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1040 base::Unretained(this)),
1041 &auth_credentials_);
1042 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1043 return;
1046 NotifyAuthRequiredComplete(rv);
1049 void URLRequest::NotifyAuthRequiredComplete(
1050 NetworkDelegate::AuthRequiredResponse result) {
1051 OnCallToDelegateComplete();
1053 // Check that there are no callbacks to already canceled requests.
1054 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1056 // NotifyAuthRequired may be called multiple times, such as
1057 // when an authentication attempt fails. Clear out the data
1058 // so it can be reset on another round.
1059 AuthCredentials credentials = auth_credentials_;
1060 auth_credentials_ = AuthCredentials();
1061 scoped_refptr<AuthChallengeInfo> auth_info;
1062 auth_info.swap(auth_info_);
1064 switch (result) {
1065 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1066 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1067 // didn't take an action.
1068 if (delegate_) {
1069 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
1070 // fixed.
1071 tracked_objects::ScopedTracker tracking_profile(
1072 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1073 "423948 URLRequest::Delegate::OnAuthRequired"));
1074 delegate_->OnAuthRequired(this, auth_info.get());
1076 break;
1078 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1079 SetAuth(credentials);
1080 break;
1082 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1083 CancelAuth();
1084 break;
1086 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1087 NOTREACHED();
1088 break;
1092 void URLRequest::NotifyCertificateRequested(
1093 SSLCertRequestInfo* cert_request_info) {
1094 if (delegate_) {
1095 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1096 tracked_objects::ScopedTracker tracking_profile(
1097 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1098 "423948 URLRequest::Delegate::OnCertificateRequested"));
1099 delegate_->OnCertificateRequested(this, cert_request_info);
1103 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1104 bool fatal) {
1105 if (delegate_) {
1106 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1107 tracked_objects::ScopedTracker tracking_profile(
1108 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1109 "423948 URLRequest::Delegate::OnSSLCertificateError"));
1110 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1114 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1115 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1116 if (network_delegate_) {
1117 return network_delegate_->CanGetCookies(*this, cookie_list);
1119 return g_default_can_use_cookies;
1122 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1123 CookieOptions* options) const {
1124 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1125 if (network_delegate_) {
1126 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1128 return g_default_can_use_cookies;
1131 bool URLRequest::CanEnablePrivacyMode() const {
1132 if (network_delegate_) {
1133 return network_delegate_->CanEnablePrivacyMode(url(),
1134 first_party_for_cookies_);
1136 return !g_default_can_use_cookies;
1140 void URLRequest::NotifyReadCompleted(int bytes_read) {
1141 // Notify in case the entire URL Request has been finished.
1142 if (bytes_read <= 0)
1143 NotifyRequestCompleted();
1145 // Notify NetworkChangeNotifier that we just received network data.
1146 // This is to identify cases where the NetworkChangeNotifier thinks we
1147 // are off-line but we are still receiving network data (crbug.com/124069),
1148 // and to get rough network connection measurements.
1149 if (bytes_read > 0 && !was_cached())
1150 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1152 if (delegate_) {
1153 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1154 tracked_objects::ScopedTracker tracking_profile(
1155 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1156 "423948 URLRequest::Delegate::OnReadCompleted"));
1157 delegate_->OnReadCompleted(this, bytes_read);
1160 // Nothing below this line as OnReadCompleted may delete |this|.
1163 void URLRequest::OnHeadersComplete() {
1164 // Cache load timing information now, as information will be lost once the
1165 // socket is closed and the ClientSocketHandle is Reset, which will happen
1166 // once the body is complete. The start times should already be populated.
1167 if (job_.get()) {
1168 // Keep a copy of the two times the URLRequest sets.
1169 base::TimeTicks request_start = load_timing_info_.request_start;
1170 base::Time request_start_time = load_timing_info_.request_start_time;
1172 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1173 // consistent place to start from.
1174 load_timing_info_ = LoadTimingInfo();
1175 job_->GetLoadTimingInfo(&load_timing_info_);
1177 load_timing_info_.request_start = request_start;
1178 load_timing_info_.request_start_time = request_start_time;
1180 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1184 void URLRequest::NotifyRequestCompleted() {
1185 // TODO(battre): Get rid of this check, according to willchan it should
1186 // not be needed.
1187 if (has_notified_completion_)
1188 return;
1190 is_pending_ = false;
1191 is_redirecting_ = false;
1192 has_notified_completion_ = true;
1193 if (network_delegate_)
1194 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1197 void URLRequest::OnCallToDelegate() {
1198 DCHECK(!calling_delegate_);
1199 DCHECK(blocked_by_.empty());
1200 calling_delegate_ = true;
1201 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1204 void URLRequest::OnCallToDelegateComplete() {
1205 // This should have been cleared before resuming the request.
1206 DCHECK(blocked_by_.empty());
1207 if (!calling_delegate_)
1208 return;
1209 calling_delegate_ = false;
1210 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1213 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1214 base::debug::StackTrace* stack_trace_copy =
1215 new base::debug::StackTrace(NULL, 0);
1216 *stack_trace_copy = stack_trace;
1217 stack_trace_.reset(stack_trace_copy);
1220 const base::debug::StackTrace* URLRequest::stack_trace() const {
1221 return stack_trace_.get();
1224 } // namespace net