Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / url_request / url_request.cc
blob6032c4cb4885b6f6124939844a44d4c7e165bf79
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/url_request/url_request.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/debug/stack_trace.h"
12 #include "base/lazy_instance.h"
13 #include "base/memory/singleton.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/profiler/scoped_tracker.h"
16 #include "base/stl_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/synchronization/lock.h"
19 #include "base/values.h"
20 #include "net/base/auth.h"
21 #include "net/base/chunked_upload_data_stream.h"
22 #include "net/base/host_port_pair.h"
23 #include "net/base/load_flags.h"
24 #include "net/base/load_timing_info.h"
25 #include "net/base/net_errors.h"
26 #include "net/base/network_change_notifier.h"
27 #include "net/base/network_delegate.h"
28 #include "net/base/upload_data_stream.h"
29 #include "net/http/http_response_headers.h"
30 #include "net/http/http_util.h"
31 #include "net/log/net_log.h"
32 #include "net/ssl/ssl_cert_request_info.h"
33 #include "net/url_request/redirect_info.h"
34 #include "net/url_request/url_request_context.h"
35 #include "net/url_request/url_request_error_job.h"
36 #include "net/url_request/url_request_job.h"
37 #include "net/url_request/url_request_job_manager.h"
38 #include "net/url_request/url_request_netlog_params.h"
39 #include "net/url_request/url_request_redirect_job.h"
40 #include "url/gurl.h"
41 #include "url/origin.h"
43 using base::Time;
44 using std::string;
46 namespace net {
48 namespace {
50 // Max number of http redirects to follow. Same number as gecko.
51 const int kMaxRedirects = 20;
53 // Discard headers which have meaning in POST (Content-Length, Content-Type,
54 // Origin).
55 void StripPostSpecificHeaders(HttpRequestHeaders* headers) {
56 // These are headers that may be attached to a POST.
57 headers->RemoveHeader(HttpRequestHeaders::kContentLength);
58 headers->RemoveHeader(HttpRequestHeaders::kContentType);
59 // TODO(jww): This is Origin header removal is probably layering violation and
60 // should be refactored into //content. See https://crbug.com/471397.
61 headers->RemoveHeader(HttpRequestHeaders::kOrigin);
64 // TODO(battre): Delete this, see http://crbug.com/89321:
65 // This counter keeps track of the identifiers used for URL requests so far.
66 // 0 is reserved to represent an invalid ID.
67 uint64 g_next_url_request_identifier = 1;
69 // This lock protects g_next_url_request_identifier.
70 base::LazyInstance<base::Lock>::Leaky
71 g_next_url_request_identifier_lock = LAZY_INSTANCE_INITIALIZER;
73 // Returns an prior unused identifier for URL requests.
74 uint64 GenerateURLRequestIdentifier() {
75 base::AutoLock lock(g_next_url_request_identifier_lock.Get());
76 return g_next_url_request_identifier++;
79 // True once the first URLRequest was started.
80 bool g_url_requests_started = false;
82 // True if cookies are accepted by default.
83 bool g_default_can_use_cookies = true;
85 // When the URLRequest first assempts load timing information, it has the times
86 // at which each event occurred. The API requires the time which the request
87 // was blocked on each phase. This function handles the conversion.
89 // In the case of reusing a SPDY session, old proxy results may have been
90 // reused, so proxy resolution times may be before the request was started.
92 // Due to preconnect and late binding, it is also possible for the connection
93 // attempt to start before a request has been started, or proxy resolution
94 // completed.
96 // This functions fixes both those cases.
97 void ConvertRealLoadTimesToBlockingTimes(LoadTimingInfo* load_timing_info) {
98 DCHECK(!load_timing_info->request_start.is_null());
100 // Earliest time possible for the request to be blocking on connect events.
101 base::TimeTicks block_on_connect = load_timing_info->request_start;
103 if (!load_timing_info->proxy_resolve_start.is_null()) {
104 DCHECK(!load_timing_info->proxy_resolve_end.is_null());
106 // Make sure the proxy times are after request start.
107 if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
108 load_timing_info->proxy_resolve_start = load_timing_info->request_start;
109 if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
110 load_timing_info->proxy_resolve_end = load_timing_info->request_start;
112 // Connect times must also be after the proxy times.
113 block_on_connect = load_timing_info->proxy_resolve_end;
116 // Make sure connection times are after start and proxy times.
118 LoadTimingInfo::ConnectTiming* connect_timing =
119 &load_timing_info->connect_timing;
120 if (!connect_timing->dns_start.is_null()) {
121 DCHECK(!connect_timing->dns_end.is_null());
122 if (connect_timing->dns_start < block_on_connect)
123 connect_timing->dns_start = block_on_connect;
124 if (connect_timing->dns_end < block_on_connect)
125 connect_timing->dns_end = block_on_connect;
128 if (!connect_timing->connect_start.is_null()) {
129 DCHECK(!connect_timing->connect_end.is_null());
130 if (connect_timing->connect_start < block_on_connect)
131 connect_timing->connect_start = block_on_connect;
132 if (connect_timing->connect_end < block_on_connect)
133 connect_timing->connect_end = block_on_connect;
136 if (!connect_timing->ssl_start.is_null()) {
137 DCHECK(!connect_timing->ssl_end.is_null());
138 if (connect_timing->ssl_start < block_on_connect)
139 connect_timing->ssl_start = block_on_connect;
140 if (connect_timing->ssl_end < block_on_connect)
141 connect_timing->ssl_end = block_on_connect;
145 } // namespace
147 ///////////////////////////////////////////////////////////////////////////////
148 // URLRequest::Delegate
150 void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
151 const RedirectInfo& redirect_info,
152 bool* defer_redirect) {
155 void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
156 AuthChallengeInfo* auth_info) {
157 request->CancelAuth();
160 void URLRequest::Delegate::OnCertificateRequested(
161 URLRequest* request,
162 SSLCertRequestInfo* cert_request_info) {
163 request->CancelWithError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
166 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
167 const SSLInfo& ssl_info,
168 bool is_hsts_ok) {
169 request->Cancel();
172 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request,
173 bool* defer) {
176 ///////////////////////////////////////////////////////////////////////////////
177 // URLRequest
179 URLRequest::~URLRequest() {
180 Cancel();
182 if (network_delegate_) {
183 network_delegate_->NotifyURLRequestDestroyed(this);
184 if (job_.get())
185 job_->NotifyURLRequestDestroyed();
188 if (job_.get())
189 OrphanJob();
191 int deleted = context_->url_requests()->erase(this);
192 CHECK_EQ(1, deleted);
194 int net_error = OK;
195 // Log error only on failure, not cancellation, as even successful requests
196 // are "cancelled" on destruction.
197 if (status_.status() == URLRequestStatus::FAILED)
198 net_error = status_.error();
199 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
202 void URLRequest::EnableChunkedUpload() {
203 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked());
204 if (!upload_data_stream_) {
205 upload_chunked_data_stream_ = new ChunkedUploadDataStream(0);
206 upload_data_stream_.reset(upload_chunked_data_stream_);
210 void URLRequest::AppendChunkToUpload(const char* bytes,
211 int bytes_len,
212 bool is_last_chunk) {
213 DCHECK(upload_data_stream_);
214 DCHECK(upload_data_stream_->is_chunked());
215 upload_chunked_data_stream_->AppendData(bytes, bytes_len, is_last_chunk);
218 void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) {
219 upload_data_stream_ = upload.Pass();
222 const UploadDataStream* URLRequest::get_upload() const {
223 return upload_data_stream_.get();
226 bool URLRequest::has_upload() const {
227 return upload_data_stream_.get() != NULL;
230 void URLRequest::SetExtraRequestHeaderByName(const string& name,
231 const string& value,
232 bool overwrite) {
233 DCHECK(!is_pending_ || is_redirecting_);
234 if (overwrite) {
235 extra_request_headers_.SetHeader(name, value);
236 } else {
237 extra_request_headers_.SetHeaderIfMissing(name, value);
241 void URLRequest::RemoveRequestHeaderByName(const string& name) {
242 DCHECK(!is_pending_ || is_redirecting_);
243 extra_request_headers_.RemoveHeader(name);
246 void URLRequest::SetExtraRequestHeaders(
247 const HttpRequestHeaders& headers) {
248 DCHECK(!is_pending_);
249 extra_request_headers_ = headers;
251 // NOTE: This method will likely become non-trivial once the other setters
252 // for request headers are implemented.
255 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
256 if (!job_.get())
257 return false;
259 return job_->GetFullRequestHeaders(headers);
262 int64 URLRequest::GetTotalReceivedBytes() const {
263 if (!job_.get())
264 return 0;
266 return job_->GetTotalReceivedBytes();
269 LoadStateWithParam URLRequest::GetLoadState() const {
270 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
271 // delegate before it has been started.
272 if (calling_delegate_ || !blocked_by_.empty()) {
273 return LoadStateWithParam(
274 LOAD_STATE_WAITING_FOR_DELEGATE,
275 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
276 base::string16());
278 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
279 base::string16());
282 base::Value* URLRequest::GetStateAsValue() const {
283 base::DictionaryValue* dict = new base::DictionaryValue();
284 dict->SetString("url", original_url().possibly_invalid_spec());
286 if (url_chain_.size() > 1) {
287 base::ListValue* list = new base::ListValue();
288 for (std::vector<GURL>::const_iterator url = url_chain_.begin();
289 url != url_chain_.end(); ++url) {
290 list->AppendString(url->possibly_invalid_spec());
292 dict->Set("url_chain", list);
295 dict->SetInteger("load_flags", load_flags_);
297 LoadStateWithParam load_state = GetLoadState();
298 dict->SetInteger("load_state", load_state.state);
299 if (!load_state.param.empty())
300 dict->SetString("load_state_param", load_state.param);
301 if (!blocked_by_.empty())
302 dict->SetString("delegate_info", blocked_by_);
304 dict->SetString("method", method_);
305 dict->SetBoolean("has_upload", has_upload());
306 dict->SetBoolean("is_pending", is_pending_);
308 // Add the status of the request. The status should always be IO_PENDING, and
309 // the error should always be OK, unless something is holding onto a request
310 // that has finished or a request was leaked. Neither of these should happen.
311 switch (status_.status()) {
312 case URLRequestStatus::SUCCESS:
313 dict->SetString("status", "SUCCESS");
314 break;
315 case URLRequestStatus::IO_PENDING:
316 dict->SetString("status", "IO_PENDING");
317 break;
318 case URLRequestStatus::CANCELED:
319 dict->SetString("status", "CANCELED");
320 break;
321 case URLRequestStatus::FAILED:
322 dict->SetString("status", "FAILED");
323 break;
325 if (status_.error() != OK)
326 dict->SetInteger("net_error", status_.error());
327 return dict;
330 void URLRequest::LogBlockedBy(const char* blocked_by) {
331 DCHECK(blocked_by);
332 DCHECK_GT(strlen(blocked_by), 0u);
334 // Only log information to NetLog during startup and certain deferring calls
335 // to delegates. For all reads but the first, do nothing.
336 if (!calling_delegate_ && !response_info_.request_time.is_null())
337 return;
339 LogUnblocked();
340 blocked_by_ = blocked_by;
341 use_blocked_by_as_load_param_ = false;
343 net_log_.BeginEvent(
344 NetLog::TYPE_DELEGATE_INFO,
345 NetLog::StringCallback("delegate_info", &blocked_by_));
348 void URLRequest::LogAndReportBlockedBy(const char* source) {
349 LogBlockedBy(source);
350 use_blocked_by_as_load_param_ = true;
353 void URLRequest::LogUnblocked() {
354 if (blocked_by_.empty())
355 return;
357 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
358 blocked_by_.clear();
361 UploadProgress URLRequest::GetUploadProgress() const {
362 if (!job_.get()) {
363 // We haven't started or the request was cancelled
364 return UploadProgress();
366 if (final_upload_progress_.position()) {
367 // The first job completed and none of the subsequent series of
368 // GETs when following redirects will upload anything, so we return the
369 // cached results from the initial job, the POST.
370 return final_upload_progress_;
372 return job_->GetUploadProgress();
375 void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
376 DCHECK(value);
377 if (response_info_.headers.get()) {
378 response_info_.headers->GetNormalizedHeader(name, value);
379 } else {
380 value->clear();
384 HostPortPair URLRequest::GetSocketAddress() const {
385 DCHECK(job_.get());
386 return job_->GetSocketAddress();
389 HttpResponseHeaders* URLRequest::response_headers() const {
390 return response_info_.headers.get();
393 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
394 *load_timing_info = load_timing_info_;
397 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
398 DCHECK(job_.get());
399 return job_->GetResponseCookies(cookies);
402 void URLRequest::GetMimeType(string* mime_type) const {
403 DCHECK(job_.get());
404 job_->GetMimeType(mime_type);
407 void URLRequest::GetCharset(string* charset) const {
408 DCHECK(job_.get());
409 job_->GetCharset(charset);
412 int URLRequest::GetResponseCode() const {
413 DCHECK(job_.get());
414 return job_->GetResponseCode();
417 void URLRequest::SetLoadFlags(int flags) {
418 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
419 DCHECK(!job_.get());
420 DCHECK(flags & LOAD_IGNORE_LIMITS);
421 DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
423 load_flags_ = flags;
425 // This should be a no-op given the above DCHECKs, but do this
426 // anyway for release mode.
427 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
428 SetPriority(MAXIMUM_PRIORITY);
431 // static
432 void URLRequest::SetDefaultCookiePolicyToBlock() {
433 CHECK(!g_url_requests_started);
434 g_default_can_use_cookies = false;
437 // static
438 bool URLRequest::IsHandledProtocol(const std::string& scheme) {
439 return URLRequestJobManager::SupportsScheme(scheme);
442 // static
443 bool URLRequest::IsHandledURL(const GURL& url) {
444 if (!url.is_valid()) {
445 // We handle error cases.
446 return true;
449 return IsHandledProtocol(url.scheme());
452 void URLRequest::set_first_party_for_cookies(
453 const GURL& first_party_for_cookies) {
454 DCHECK(!is_pending_);
455 first_party_for_cookies_ = first_party_for_cookies;
458 void URLRequest::set_first_party_url_policy(
459 FirstPartyURLPolicy first_party_url_policy) {
460 DCHECK(!is_pending_);
461 first_party_url_policy_ = first_party_url_policy;
464 void URLRequest::set_method(const std::string& method) {
465 DCHECK(!is_pending_);
466 method_ = method;
469 void URLRequest::SetReferrer(const std::string& referrer) {
470 DCHECK(!is_pending_);
471 GURL referrer_url(referrer);
472 if (referrer_url.is_valid()) {
473 referrer_ = referrer_url.GetAsReferrer().spec();
474 } else {
475 referrer_ = referrer;
479 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
480 DCHECK(!is_pending_);
481 referrer_policy_ = referrer_policy;
484 void URLRequest::set_delegate(Delegate* delegate) {
485 delegate_ = delegate;
488 void URLRequest::Start() {
489 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456327 is fixed.
490 tracked_objects::ScopedTracker tracking_profile(
491 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start"));
493 // Some values can be NULL, but the job factory must not be.
494 DCHECK(context_->job_factory());
496 // Anything that sets |blocked_by_| before start should have cleaned up after
497 // itself.
498 DCHECK(blocked_by_.empty());
500 g_url_requests_started = true;
501 response_info_.request_time = base::Time::Now();
503 load_timing_info_ = LoadTimingInfo();
504 load_timing_info_.request_start_time = response_info_.request_time;
505 load_timing_info_.request_start = base::TimeTicks::Now();
507 // Only notify the delegate for the initial request.
508 if (network_delegate_) {
509 OnCallToDelegate();
510 int error = network_delegate_->NotifyBeforeURLRequest(
511 this, before_request_callback_, &delegate_redirect_url_);
512 // If ERR_IO_PENDING is returned, the delegate will invoke
513 // |before_request_callback_| later.
514 if (error != ERR_IO_PENDING)
515 BeforeRequestComplete(error);
516 return;
519 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
520 tracked_objects::ScopedTracker tracking_profile2(
521 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start 2"));
523 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
524 this, network_delegate_));
527 ///////////////////////////////////////////////////////////////////////////////
529 URLRequest::URLRequest(const GURL& url,
530 RequestPriority priority,
531 Delegate* delegate,
532 const URLRequestContext* context,
533 NetworkDelegate* network_delegate)
534 : context_(context),
535 network_delegate_(network_delegate ? network_delegate
536 : context->network_delegate()),
537 net_log_(
538 BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST)),
539 url_chain_(1, url),
540 method_("GET"),
541 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
542 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
543 load_flags_(LOAD_NORMAL),
544 delegate_(delegate),
545 is_pending_(false),
546 is_redirecting_(false),
547 redirect_limit_(kMaxRedirects),
548 priority_(priority),
549 identifier_(GenerateURLRequestIdentifier()),
550 calling_delegate_(false),
551 use_blocked_by_as_load_param_(false),
552 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
553 base::Unretained(this))),
554 has_notified_completion_(false),
555 received_response_content_length_(0),
556 creation_time_(base::TimeTicks::Now()),
557 notified_before_network_start_(false) {
558 // Sanity check out environment.
559 DCHECK(base::MessageLoop::current())
560 << "The current base::MessageLoop must exist";
562 context->url_requests()->insert(this);
563 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
566 void URLRequest::BeforeRequestComplete(int error) {
567 DCHECK(!job_.get());
568 DCHECK_NE(ERR_IO_PENDING, error);
570 // Check that there are no callbacks to already canceled requests.
571 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
573 OnCallToDelegateComplete();
575 if (error != OK) {
576 std::string source("delegate");
577 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
578 NetLog::StringCallback("source", &source));
579 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
580 } else if (!delegate_redirect_url_.is_empty()) {
581 GURL new_url;
582 new_url.Swap(&delegate_redirect_url_);
584 URLRequestRedirectJob* job = new URLRequestRedirectJob(
585 this, network_delegate_, new_url,
586 // Use status code 307 to preserve the method, so POST requests work.
587 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
588 StartJob(job);
589 } else {
590 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
591 this, network_delegate_));
595 void URLRequest::StartJob(URLRequestJob* job) {
596 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
597 tracked_objects::ScopedTracker tracking_profile(
598 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::StartJob"));
600 DCHECK(!is_pending_);
601 DCHECK(!job_.get());
603 net_log_.BeginEvent(
604 NetLog::TYPE_URL_REQUEST_START_JOB,
605 base::Bind(&NetLogURLRequestStartCallback,
606 &url(), &method_, load_flags_, priority_,
607 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
609 job_ = job;
610 job_->SetExtraRequestHeaders(extra_request_headers_);
611 job_->SetPriority(priority_);
613 if (upload_data_stream_.get())
614 job_->SetUpload(upload_data_stream_.get());
616 is_pending_ = true;
617 is_redirecting_ = false;
619 response_info_.was_cached = false;
621 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect(
622 referrer_policy_, referrer_, url())) {
623 if (!network_delegate_ ||
624 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
625 *this, url(), GURL(referrer_))) {
626 referrer_.clear();
627 } else {
628 // We need to clear the referrer anyway to avoid an infinite recursion
629 // when starting the error job.
630 referrer_.clear();
631 std::string source("delegate");
632 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
633 NetLog::StringCallback("source", &source));
634 RestartWithJob(new URLRequestErrorJob(
635 this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
636 return;
640 // Don't allow errors to be sent from within Start().
641 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
642 // we probably don't want this: they should be sent asynchronously so
643 // the caller does not get reentered.
644 job_->Start();
647 void URLRequest::Restart() {
648 // Should only be called if the original job didn't make any progress.
649 DCHECK(job_.get() && !job_->has_response_started());
650 RestartWithJob(
651 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
654 void URLRequest::RestartWithJob(URLRequestJob *job) {
655 DCHECK(job->request() == this);
656 PrepareToRestart();
657 StartJob(job);
660 void URLRequest::Cancel() {
661 DoCancel(ERR_ABORTED, SSLInfo());
664 void URLRequest::CancelWithError(int error) {
665 DoCancel(error, SSLInfo());
668 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
669 // This should only be called on a started request.
670 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
671 NOTREACHED();
672 return;
674 DoCancel(error, ssl_info);
677 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
678 DCHECK(error < 0);
679 // If cancelled while calling a delegate, clear delegate info.
680 if (calling_delegate_) {
681 LogUnblocked();
682 OnCallToDelegateComplete();
685 // If the URL request already has an error status, then canceling is a no-op.
686 // Plus, we don't want to change the error status once it has been set.
687 if (status_.is_success()) {
688 status_.set_status(URLRequestStatus::CANCELED);
689 status_.set_error(error);
690 response_info_.ssl_info = ssl_info;
692 // If the request hasn't already been completed, log a cancellation event.
693 if (!has_notified_completion_) {
694 // Don't log an error code on ERR_ABORTED, since that's redundant.
695 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
696 error == ERR_ABORTED ? OK : error);
700 if (is_pending_ && job_.get())
701 job_->Kill();
703 // We need to notify about the end of this job here synchronously. The
704 // Job sends an asynchronous notification but by the time this is processed,
705 // our |context_| is NULL.
706 NotifyRequestCompleted();
708 // The Job will call our NotifyDone method asynchronously. This is done so
709 // that the Delegate implementation can call Cancel without having to worry
710 // about being called recursively.
713 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
714 DCHECK(job_.get());
715 DCHECK(bytes_read);
716 *bytes_read = 0;
718 // If this is the first read, end the delegate call that may have started in
719 // OnResponseStarted.
720 OnCallToDelegateComplete();
722 // This handles a cancel that happens while paused.
723 // TODO(ahendrickson): DCHECK() that it is not done after
724 // http://crbug.com/115705 is fixed.
725 if (job_->is_done())
726 return false;
728 if (dest_size == 0) {
729 // Caller is not too bright. I guess we've done what they asked.
730 return true;
733 // Once the request fails or is cancelled, read will just return 0 bytes
734 // to indicate end of stream.
735 if (!status_.is_success()) {
736 return true;
739 bool rv = job_->Read(dest, dest_size, bytes_read);
740 // If rv is false, the status cannot be success.
741 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
743 if (rv && *bytes_read <= 0 && status_.is_success())
744 NotifyRequestCompleted();
745 return rv;
748 void URLRequest::StopCaching() {
749 DCHECK(job_.get());
750 job_->StopCaching();
753 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
754 bool* defer_redirect) {
755 is_redirecting_ = true;
757 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
758 URLRequestJob* job =
759 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
760 this, network_delegate_, redirect_info.new_url);
761 if (job) {
762 RestartWithJob(job);
763 } else if (delegate_) {
764 OnCallToDelegate();
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();
773 delegate_->OnBeforeNetworkStart(this, defer);
774 if (!*defer)
775 OnCallToDelegateComplete();
776 notified_before_network_start_ = true;
780 void URLRequest::ResumeNetworkStart() {
781 DCHECK(job_.get());
782 DCHECK(notified_before_network_start_);
784 OnCallToDelegateComplete();
785 job_->ResumeNetworkStart();
788 void URLRequest::NotifyResponseStarted() {
789 int net_error = OK;
790 if (!status_.is_success())
791 net_error = status_.error();
792 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
793 net_error);
795 URLRequestJob* job =
796 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
797 this, network_delegate_);
798 if (job) {
799 RestartWithJob(job);
800 } else {
801 if (delegate_) {
802 // In some cases (e.g. an event was canceled), we might have sent the
803 // completion event and receive a NotifyResponseStarted() later.
804 if (!has_notified_completion_ && status_.is_success()) {
805 if (network_delegate_)
806 network_delegate_->NotifyResponseStarted(this);
809 // Notify in case the entire URL Request has been finished.
810 if (!has_notified_completion_ && !status_.is_success())
811 NotifyRequestCompleted();
813 OnCallToDelegate();
814 delegate_->OnResponseStarted(this);
815 // Nothing may appear below this line as OnResponseStarted may delete
816 // |this|.
821 void URLRequest::FollowDeferredRedirect() {
822 CHECK(job_.get());
823 CHECK(status_.is_success());
825 job_->FollowDeferredRedirect();
828 void URLRequest::SetAuth(const AuthCredentials& credentials) {
829 DCHECK(job_.get());
830 DCHECK(job_->NeedsAuth());
832 job_->SetAuth(credentials);
835 void URLRequest::CancelAuth() {
836 DCHECK(job_.get());
837 DCHECK(job_->NeedsAuth());
839 job_->CancelAuth();
842 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
843 DCHECK(job_.get());
845 job_->ContinueWithCertificate(client_cert);
848 void URLRequest::ContinueDespiteLastError() {
849 DCHECK(job_.get());
851 job_->ContinueDespiteLastError();
854 void URLRequest::PrepareToRestart() {
855 DCHECK(job_.get());
857 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
858 // one.
859 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
861 OrphanJob();
863 response_info_ = HttpResponseInfo();
864 response_info_.request_time = base::Time::Now();
866 load_timing_info_ = LoadTimingInfo();
867 load_timing_info_.request_start_time = response_info_.request_time;
868 load_timing_info_.request_start = base::TimeTicks::Now();
870 status_ = URLRequestStatus();
871 is_pending_ = false;
874 void URLRequest::OrphanJob() {
875 // When calling this function, please check that URLRequestHttpJob is
876 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
877 // the call back. This is currently guaranteed by the following strategies:
878 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
879 // be receiving any headers at that time.
880 // - OrphanJob is called in ~URLRequest, in this case
881 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
882 // that the callback becomes invalid.
883 job_->Kill();
884 job_->DetachRequest(); // ensures that the job will not call us again
885 job_ = NULL;
888 int URLRequest::Redirect(const RedirectInfo& redirect_info) {
889 // Matches call in NotifyReceivedRedirect.
890 OnCallToDelegateComplete();
891 if (net_log_.GetCaptureMode().enabled()) {
892 net_log_.AddEvent(
893 NetLog::TYPE_URL_REQUEST_REDIRECTED,
894 NetLog::StringCallback("location",
895 &redirect_info.new_url.possibly_invalid_spec()));
898 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
899 if (network_delegate_)
900 network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url);
902 if (redirect_limit_ <= 0) {
903 DVLOG(1) << "disallowing redirect: exceeds limit";
904 return ERR_TOO_MANY_REDIRECTS;
907 if (!redirect_info.new_url.is_valid())
908 return ERR_INVALID_URL;
910 if (!job_->IsSafeRedirect(redirect_info.new_url)) {
911 DVLOG(1) << "disallowing redirect: unsafe protocol";
912 return ERR_UNSAFE_REDIRECT;
915 if (!final_upload_progress_.position())
916 final_upload_progress_ = job_->GetUploadProgress();
917 PrepareToRestart();
919 if (redirect_info.new_method != method_) {
920 // TODO(davidben): This logic still needs to be replicated at the consumers.
921 if (method_ == "POST") {
922 // If being switched from POST, must remove headers that were specific to
923 // the POST and don't have meaning in other methods. For example the
924 // inclusion of a multipart Content-Type header in GET can cause problems
925 // with some servers:
926 // http://code.google.com/p/chromium/issues/detail?id=843
927 StripPostSpecificHeaders(&extra_request_headers_);
929 upload_data_stream_.reset();
930 method_ = redirect_info.new_method;
933 // Cross-origin redirects should not result in an Origin header value that is
934 // equal to the original request's Origin header. This is necessary to prevent
935 // a reflection of POST requests to bypass CSRF protections. If the header was
936 // not set to "null", a POST request from origin A to a malicious origin M
937 // could be redirected by M back to A.
939 // In the Section 4.2, Step 4.10 of the Fetch spec
940 // (https://fetch.spec.whatwg.org/#concept-http-fetch), it states that on
941 // cross-origin 301, 302, 303, 307, and 308 redirects, the user agent should
942 // set the request's origin to an "opaque identifier," which serializes to
943 // "null." This matches Firefox and IE behavior, although it supercedes the
944 // suggested behavior in RFC 6454, "The Web Origin Concept."
946 // See also https://crbug.com/465517.
948 // TODO(jww): This is probably layering violation and should be refactored
949 // into //content. See https://crbug.com/471397.
950 if (redirect_info.new_url.GetOrigin() != url().GetOrigin() &&
951 extra_request_headers_.HasHeader(HttpRequestHeaders::kOrigin)) {
952 extra_request_headers_.SetHeader(HttpRequestHeaders::kOrigin,
953 url::Origin().string());
956 referrer_ = redirect_info.new_referrer;
957 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies;
959 url_chain_.push_back(redirect_info.new_url);
960 --redirect_limit_;
962 Start();
963 return OK;
966 const URLRequestContext* URLRequest::context() const {
967 return context_;
970 int64 URLRequest::GetExpectedContentSize() const {
971 int64 expected_content_size = -1;
972 if (job_.get())
973 expected_content_size = job_->expected_content_size();
975 return expected_content_size;
978 void URLRequest::SetPriority(RequestPriority priority) {
979 DCHECK_GE(priority, MINIMUM_PRIORITY);
980 DCHECK_LE(priority, MAXIMUM_PRIORITY);
982 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
983 NOTREACHED();
984 // Maintain the invariant that requests with IGNORE_LIMITS set
985 // have MAXIMUM_PRIORITY for release mode.
986 return;
989 if (priority_ == priority)
990 return;
992 priority_ = priority;
993 if (job_.get()) {
994 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
995 NetLog::IntegerCallback("priority", priority_));
996 job_->SetPriority(priority_);
1000 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
1001 const GURL& url = this->url();
1002 bool scheme_is_http = url.SchemeIs("http");
1003 if (!scheme_is_http && !url.SchemeIs("ws"))
1004 return false;
1005 TransportSecurityState* state = context()->transport_security_state();
1006 if (state && state->ShouldUpgradeToSSL(url.host())) {
1007 GURL::Replacements replacements;
1008 const char* new_scheme = scheme_is_http ? "https" : "wss";
1009 replacements.SetSchemeStr(new_scheme);
1010 *redirect_url = url.ReplaceComponents(replacements);
1011 return true;
1013 return false;
1016 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1017 NetworkDelegate::AuthRequiredResponse rv =
1018 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1019 auth_info_ = auth_info;
1020 if (network_delegate_) {
1021 OnCallToDelegate();
1022 rv = network_delegate_->NotifyAuthRequired(
1023 this,
1024 *auth_info,
1025 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1026 base::Unretained(this)),
1027 &auth_credentials_);
1028 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1029 return;
1032 NotifyAuthRequiredComplete(rv);
1035 void URLRequest::NotifyAuthRequiredComplete(
1036 NetworkDelegate::AuthRequiredResponse result) {
1037 OnCallToDelegateComplete();
1039 // Check that there are no callbacks to already canceled requests.
1040 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1042 // NotifyAuthRequired may be called multiple times, such as
1043 // when an authentication attempt fails. Clear out the data
1044 // so it can be reset on another round.
1045 AuthCredentials credentials = auth_credentials_;
1046 auth_credentials_ = AuthCredentials();
1047 scoped_refptr<AuthChallengeInfo> auth_info;
1048 auth_info.swap(auth_info_);
1050 switch (result) {
1051 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1052 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1053 // didn't take an action.
1054 if (delegate_)
1055 delegate_->OnAuthRequired(this, auth_info.get());
1056 break;
1058 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1059 SetAuth(credentials);
1060 break;
1062 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1063 CancelAuth();
1064 break;
1066 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1067 NOTREACHED();
1068 break;
1072 void URLRequest::NotifyCertificateRequested(
1073 SSLCertRequestInfo* cert_request_info) {
1074 if (delegate_)
1075 delegate_->OnCertificateRequested(this, cert_request_info);
1078 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1079 bool fatal) {
1080 if (delegate_)
1081 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1084 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1085 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1086 if (network_delegate_) {
1087 return network_delegate_->CanGetCookies(*this, cookie_list);
1089 return g_default_can_use_cookies;
1092 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1093 CookieOptions* options) const {
1094 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1095 if (network_delegate_) {
1096 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1098 return g_default_can_use_cookies;
1101 bool URLRequest::CanEnablePrivacyMode() const {
1102 if (network_delegate_) {
1103 return network_delegate_->CanEnablePrivacyMode(url(),
1104 first_party_for_cookies_);
1106 return !g_default_can_use_cookies;
1110 void URLRequest::NotifyReadCompleted(int bytes_read) {
1111 // Notify in case the entire URL Request has been finished.
1112 if (bytes_read <= 0)
1113 NotifyRequestCompleted();
1115 // Notify NetworkChangeNotifier that we just received network data.
1116 // This is to identify cases where the NetworkChangeNotifier thinks we
1117 // are off-line but we are still receiving network data (crbug.com/124069),
1118 // and to get rough network connection measurements.
1119 if (bytes_read > 0 && !was_cached())
1120 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1122 if (delegate_)
1123 delegate_->OnReadCompleted(this, bytes_read);
1125 // Nothing below this line as OnReadCompleted may delete |this|.
1128 void URLRequest::OnHeadersComplete() {
1129 // Cache load timing information now, as information will be lost once the
1130 // socket is closed and the ClientSocketHandle is Reset, which will happen
1131 // once the body is complete. The start times should already be populated.
1132 if (job_.get()) {
1133 // Keep a copy of the two times the URLRequest sets.
1134 base::TimeTicks request_start = load_timing_info_.request_start;
1135 base::Time request_start_time = load_timing_info_.request_start_time;
1137 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1138 // consistent place to start from.
1139 load_timing_info_ = LoadTimingInfo();
1140 job_->GetLoadTimingInfo(&load_timing_info_);
1142 load_timing_info_.request_start = request_start;
1143 load_timing_info_.request_start_time = request_start_time;
1145 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1149 void URLRequest::NotifyRequestCompleted() {
1150 // TODO(battre): Get rid of this check, according to willchan it should
1151 // not be needed.
1152 if (has_notified_completion_)
1153 return;
1155 is_pending_ = false;
1156 is_redirecting_ = false;
1157 has_notified_completion_ = true;
1158 if (network_delegate_)
1159 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1162 void URLRequest::OnCallToDelegate() {
1163 DCHECK(!calling_delegate_);
1164 DCHECK(blocked_by_.empty());
1165 calling_delegate_ = true;
1166 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1169 void URLRequest::OnCallToDelegateComplete() {
1170 // This should have been cleared before resuming the request.
1171 DCHECK(blocked_by_.empty());
1172 if (!calling_delegate_)
1173 return;
1174 calling_delegate_ = false;
1175 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1178 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1179 base::debug::StackTrace* stack_trace_copy =
1180 new base::debug::StackTrace(NULL, 0);
1181 *stack_trace_copy = stack_trace;
1182 stack_trace_.reset(stack_trace_copy);
1185 const base::debug::StackTrace* URLRequest::stack_trace() const {
1186 return stack_trace_.get();
1189 } // namespace net