mac: Don't limit GL_MAX_TEXTURE_SIZE on 10.9.0+.
[chromium-blink-merge.git] / net / url_request / url_request.cc
blobc61a14b3bac939e6dc727fdeb7d4be18cd7dc43c
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/metrics/stats_counters.h"
16 #include "base/profiler/scoped_tracker.h"
17 #include "base/stl_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/synchronization/lock.h"
20 #include "base/values.h"
21 #include "net/base/auth.h"
22 #include "net/base/chunked_upload_data_stream.h"
23 #include "net/base/host_port_pair.h"
24 #include "net/base/load_flags.h"
25 #include "net/base/load_timing_info.h"
26 #include "net/base/net_errors.h"
27 #include "net/base/net_log.h"
28 #include "net/base/network_change_notifier.h"
29 #include "net/base/network_delegate.h"
30 #include "net/base/upload_data_stream.h"
31 #include "net/http/http_response_headers.h"
32 #include "net/http/http_util.h"
33 #include "net/ssl/ssl_cert_request_info.h"
34 #include "net/url_request/redirect_info.h"
35 #include "net/url_request/url_request_context.h"
36 #include "net/url_request/url_request_error_job.h"
37 #include "net/url_request/url_request_job.h"
38 #include "net/url_request/url_request_job_manager.h"
39 #include "net/url_request/url_request_netlog_params.h"
40 #include "net/url_request/url_request_redirect_job.h"
42 using base::Time;
43 using std::string;
45 namespace net {
47 namespace {
49 // Max number of http redirects to follow. Same number as gecko.
50 const int kMaxRedirects = 20;
52 // Discard headers which have meaning in POST (Content-Length, Content-Type,
53 // Origin).
54 void StripPostSpecificHeaders(HttpRequestHeaders* headers) {
55 // These are headers that may be attached to a POST.
56 headers->RemoveHeader(HttpRequestHeaders::kContentLength);
57 headers->RemoveHeader(HttpRequestHeaders::kContentType);
58 headers->RemoveHeader(HttpRequestHeaders::kOrigin);
61 // TODO(battre): Delete this, see http://crbug.com/89321:
62 // This counter keeps track of the identifiers used for URL requests so far.
63 // 0 is reserved to represent an invalid ID.
64 uint64 g_next_url_request_identifier = 1;
66 // This lock protects g_next_url_request_identifier.
67 base::LazyInstance<base::Lock>::Leaky
68 g_next_url_request_identifier_lock = LAZY_INSTANCE_INITIALIZER;
70 // Returns an prior unused identifier for URL requests.
71 uint64 GenerateURLRequestIdentifier() {
72 base::AutoLock lock(g_next_url_request_identifier_lock.Get());
73 return g_next_url_request_identifier++;
76 // True once the first URLRequest was started.
77 bool g_url_requests_started = false;
79 // True if cookies are accepted by default.
80 bool g_default_can_use_cookies = true;
82 // When the URLRequest first assempts load timing information, it has the times
83 // at which each event occurred. The API requires the time which the request
84 // was blocked on each phase. This function handles the conversion.
86 // In the case of reusing a SPDY session, old proxy results may have been
87 // reused, so proxy resolution times may be before the request was started.
89 // Due to preconnect and late binding, it is also possible for the connection
90 // attempt to start before a request has been started, or proxy resolution
91 // completed.
93 // This functions fixes both those cases.
94 void ConvertRealLoadTimesToBlockingTimes(
95 net::LoadTimingInfo* load_timing_info) {
96 DCHECK(!load_timing_info->request_start.is_null());
98 // Earliest time possible for the request to be blocking on connect events.
99 base::TimeTicks block_on_connect = load_timing_info->request_start;
101 if (!load_timing_info->proxy_resolve_start.is_null()) {
102 DCHECK(!load_timing_info->proxy_resolve_end.is_null());
104 // Make sure the proxy times are after request start.
105 if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
106 load_timing_info->proxy_resolve_start = load_timing_info->request_start;
107 if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
108 load_timing_info->proxy_resolve_end = load_timing_info->request_start;
110 // Connect times must also be after the proxy times.
111 block_on_connect = load_timing_info->proxy_resolve_end;
114 // Make sure connection times are after start and proxy times.
116 net::LoadTimingInfo::ConnectTiming* connect_timing =
117 &load_timing_info->connect_timing;
118 if (!connect_timing->dns_start.is_null()) {
119 DCHECK(!connect_timing->dns_end.is_null());
120 if (connect_timing->dns_start < block_on_connect)
121 connect_timing->dns_start = block_on_connect;
122 if (connect_timing->dns_end < block_on_connect)
123 connect_timing->dns_end = block_on_connect;
126 if (!connect_timing->connect_start.is_null()) {
127 DCHECK(!connect_timing->connect_end.is_null());
128 if (connect_timing->connect_start < block_on_connect)
129 connect_timing->connect_start = block_on_connect;
130 if (connect_timing->connect_end < block_on_connect)
131 connect_timing->connect_end = block_on_connect;
134 if (!connect_timing->ssl_start.is_null()) {
135 DCHECK(!connect_timing->ssl_end.is_null());
136 if (connect_timing->ssl_start < block_on_connect)
137 connect_timing->ssl_start = block_on_connect;
138 if (connect_timing->ssl_end < block_on_connect)
139 connect_timing->ssl_end = block_on_connect;
143 } // namespace
145 ///////////////////////////////////////////////////////////////////////////////
146 // URLRequest::Delegate
148 void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
149 const RedirectInfo& redirect_info,
150 bool* defer_redirect) {
153 void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
154 AuthChallengeInfo* auth_info) {
155 request->CancelAuth();
158 void URLRequest::Delegate::OnCertificateRequested(
159 URLRequest* request,
160 SSLCertRequestInfo* cert_request_info) {
161 request->Cancel();
164 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
165 const SSLInfo& ssl_info,
166 bool is_hsts_ok) {
167 request->Cancel();
170 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request,
171 bool* defer) {
174 ///////////////////////////////////////////////////////////////////////////////
175 // URLRequest
177 URLRequest::~URLRequest() {
178 Cancel();
180 if (network_delegate_) {
181 network_delegate_->NotifyURLRequestDestroyed(this);
182 if (job_.get())
183 job_->NotifyURLRequestDestroyed();
186 if (job_.get())
187 OrphanJob();
189 int deleted = context_->url_requests()->erase(this);
190 CHECK_EQ(1, deleted);
192 int net_error = OK;
193 // Log error only on failure, not cancellation, as even successful requests
194 // are "cancelled" on destruction.
195 if (status_.status() == URLRequestStatus::FAILED)
196 net_error = status_.error();
197 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
200 void URLRequest::EnableChunkedUpload() {
201 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked());
202 if (!upload_data_stream_) {
203 upload_chunked_data_stream_ = new ChunkedUploadDataStream(0);
204 upload_data_stream_.reset(upload_chunked_data_stream_);
208 void URLRequest::AppendChunkToUpload(const char* bytes,
209 int bytes_len,
210 bool is_last_chunk) {
211 DCHECK(upload_data_stream_);
212 DCHECK(upload_data_stream_->is_chunked());
213 DCHECK_GT(bytes_len, 0);
214 upload_chunked_data_stream_->AppendData(bytes, bytes_len, is_last_chunk);
217 void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) {
218 DCHECK(!upload->is_chunked());
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::SetExtraRequestHeaderById(int id, const string& value,
231 bool overwrite) {
232 DCHECK(!is_pending_ || is_redirecting_);
233 NOTREACHED() << "implement me!";
236 void URLRequest::SetExtraRequestHeaderByName(const string& name,
237 const string& value,
238 bool overwrite) {
239 DCHECK(!is_pending_ || is_redirecting_);
240 if (overwrite) {
241 extra_request_headers_.SetHeader(name, value);
242 } else {
243 extra_request_headers_.SetHeaderIfMissing(name, value);
247 void URLRequest::RemoveRequestHeaderByName(const string& name) {
248 DCHECK(!is_pending_ || is_redirecting_);
249 extra_request_headers_.RemoveHeader(name);
252 void URLRequest::SetExtraRequestHeaders(
253 const HttpRequestHeaders& headers) {
254 DCHECK(!is_pending_);
255 extra_request_headers_ = headers;
257 // NOTE: This method will likely become non-trivial once the other setters
258 // for request headers are implemented.
261 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
262 if (!job_.get())
263 return false;
265 return job_->GetFullRequestHeaders(headers);
268 int64 URLRequest::GetTotalReceivedBytes() const {
269 if (!job_.get())
270 return 0;
272 return job_->GetTotalReceivedBytes();
275 LoadStateWithParam URLRequest::GetLoadState() const {
276 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
277 // delegate before it has been started.
278 if (calling_delegate_ || !blocked_by_.empty()) {
279 return LoadStateWithParam(
280 LOAD_STATE_WAITING_FOR_DELEGATE,
281 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
282 base::string16());
284 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
285 base::string16());
288 base::Value* URLRequest::GetStateAsValue() const {
289 base::DictionaryValue* dict = new base::DictionaryValue();
290 dict->SetString("url", original_url().possibly_invalid_spec());
292 if (url_chain_.size() > 1) {
293 base::ListValue* list = new base::ListValue();
294 for (std::vector<GURL>::const_iterator url = url_chain_.begin();
295 url != url_chain_.end(); ++url) {
296 list->AppendString(url->possibly_invalid_spec());
298 dict->Set("url_chain", list);
301 dict->SetInteger("load_flags", load_flags_);
303 LoadStateWithParam load_state = GetLoadState();
304 dict->SetInteger("load_state", load_state.state);
305 if (!load_state.param.empty())
306 dict->SetString("load_state_param", load_state.param);
307 if (!blocked_by_.empty())
308 dict->SetString("delegate_info", blocked_by_);
310 dict->SetString("method", method_);
311 dict->SetBoolean("has_upload", has_upload());
312 dict->SetBoolean("is_pending", is_pending_);
314 // Add the status of the request. The status should always be IO_PENDING, and
315 // the error should always be OK, unless something is holding onto a request
316 // that has finished or a request was leaked. Neither of these should happen.
317 switch (status_.status()) {
318 case URLRequestStatus::SUCCESS:
319 dict->SetString("status", "SUCCESS");
320 break;
321 case URLRequestStatus::IO_PENDING:
322 dict->SetString("status", "IO_PENDING");
323 break;
324 case URLRequestStatus::CANCELED:
325 dict->SetString("status", "CANCELED");
326 break;
327 case URLRequestStatus::FAILED:
328 dict->SetString("status", "FAILED");
329 break;
331 if (status_.error() != OK)
332 dict->SetInteger("net_error", status_.error());
333 return dict;
336 void URLRequest::LogBlockedBy(const char* blocked_by) {
337 DCHECK(blocked_by);
338 DCHECK_GT(strlen(blocked_by), 0u);
340 // Only log information to NetLog during startup and certain deferring calls
341 // to delegates. For all reads but the first, do nothing.
342 if (!calling_delegate_ && !response_info_.request_time.is_null())
343 return;
345 LogUnblocked();
346 blocked_by_ = blocked_by;
347 use_blocked_by_as_load_param_ = false;
349 net_log_.BeginEvent(
350 NetLog::TYPE_DELEGATE_INFO,
351 NetLog::StringCallback("delegate_info", &blocked_by_));
354 void URLRequest::LogAndReportBlockedBy(const char* source) {
355 LogBlockedBy(source);
356 use_blocked_by_as_load_param_ = true;
359 void URLRequest::LogUnblocked() {
360 if (blocked_by_.empty())
361 return;
363 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
364 blocked_by_.clear();
367 UploadProgress URLRequest::GetUploadProgress() const {
368 if (!job_.get()) {
369 // We haven't started or the request was cancelled
370 return UploadProgress();
372 if (final_upload_progress_.position()) {
373 // The first job completed and none of the subsequent series of
374 // GETs when following redirects will upload anything, so we return the
375 // cached results from the initial job, the POST.
376 return final_upload_progress_;
378 return job_->GetUploadProgress();
381 void URLRequest::GetResponseHeaderById(int id, string* value) {
382 DCHECK(job_.get());
383 NOTREACHED() << "implement me!";
386 void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
387 DCHECK(value);
388 if (response_info_.headers.get()) {
389 response_info_.headers->GetNormalizedHeader(name, value);
390 } else {
391 value->clear();
395 void URLRequest::GetAllResponseHeaders(string* headers) {
396 DCHECK(headers);
397 if (response_info_.headers.get()) {
398 response_info_.headers->GetNormalizedHeaders(headers);
399 } else {
400 headers->clear();
404 HostPortPair URLRequest::GetSocketAddress() const {
405 DCHECK(job_.get());
406 return job_->GetSocketAddress();
409 HttpResponseHeaders* URLRequest::response_headers() const {
410 return response_info_.headers.get();
413 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
414 *load_timing_info = load_timing_info_;
417 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
418 DCHECK(job_.get());
419 return job_->GetResponseCookies(cookies);
422 void URLRequest::GetMimeType(string* mime_type) const {
423 DCHECK(job_.get());
424 job_->GetMimeType(mime_type);
427 void URLRequest::GetCharset(string* charset) const {
428 DCHECK(job_.get());
429 job_->GetCharset(charset);
432 int URLRequest::GetResponseCode() const {
433 DCHECK(job_.get());
434 return job_->GetResponseCode();
437 void URLRequest::SetLoadFlags(int flags) {
438 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
439 DCHECK(!job_.get());
440 DCHECK(flags & LOAD_IGNORE_LIMITS);
441 DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
443 load_flags_ = flags;
445 // This should be a no-op given the above DCHECKs, but do this
446 // anyway for release mode.
447 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
448 SetPriority(MAXIMUM_PRIORITY);
451 // static
452 void URLRequest::SetDefaultCookiePolicyToBlock() {
453 CHECK(!g_url_requests_started);
454 g_default_can_use_cookies = false;
457 // static
458 bool URLRequest::IsHandledProtocol(const std::string& scheme) {
459 return URLRequestJobManager::SupportsScheme(scheme);
462 // static
463 bool URLRequest::IsHandledURL(const GURL& url) {
464 if (!url.is_valid()) {
465 // We handle error cases.
466 return true;
469 return IsHandledProtocol(url.scheme());
472 void URLRequest::set_first_party_for_cookies(
473 const GURL& first_party_for_cookies) {
474 DCHECK(!is_pending_);
475 first_party_for_cookies_ = first_party_for_cookies;
478 void URLRequest::set_first_party_url_policy(
479 FirstPartyURLPolicy first_party_url_policy) {
480 DCHECK(!is_pending_);
481 first_party_url_policy_ = first_party_url_policy;
484 void URLRequest::set_method(const std::string& method) {
485 DCHECK(!is_pending_);
486 method_ = method;
489 // static
490 std::string URLRequest::ComputeMethodForRedirect(
491 const std::string& method,
492 int http_status_code) {
493 // For 303 redirects, all request methods except HEAD are converted to GET,
494 // as per the latest httpbis draft. The draft also allows POST requests to
495 // be converted to GETs when following 301/302 redirects, for historical
496 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and
497 // the httpbis draft say to prompt the user to confirm the generation of new
498 // requests, other than GET and HEAD requests, but IE omits these prompts and
499 // so shall we.
500 // See: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
501 if ((http_status_code == 303 && method != "HEAD") ||
502 ((http_status_code == 301 || http_status_code == 302) &&
503 method == "POST")) {
504 return "GET";
506 return method;
509 void URLRequest::SetReferrer(const std::string& referrer) {
510 DCHECK(!is_pending_);
511 GURL referrer_url(referrer);
512 if (referrer_url.is_valid()) {
513 referrer_ = referrer_url.GetAsReferrer().spec();
514 } else {
515 referrer_ = referrer;
519 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
520 DCHECK(!is_pending_);
521 referrer_policy_ = referrer_policy;
524 void URLRequest::set_delegate(Delegate* delegate) {
525 delegate_ = delegate;
528 void URLRequest::Start() {
529 // Some values can be NULL, but the job factory must not be.
530 DCHECK(context_->job_factory());
532 // Anything that sets |blocked_by_| before start should have cleaned up after
533 // itself.
534 DCHECK(blocked_by_.empty());
536 g_url_requests_started = true;
537 response_info_.request_time = base::Time::Now();
539 load_timing_info_ = LoadTimingInfo();
540 load_timing_info_.request_start_time = response_info_.request_time;
541 load_timing_info_.request_start = base::TimeTicks::Now();
543 // Only notify the delegate for the initial request.
544 if (network_delegate_) {
545 OnCallToDelegate();
546 int error = network_delegate_->NotifyBeforeURLRequest(
547 this, before_request_callback_, &delegate_redirect_url_);
548 // If ERR_IO_PENDING is returned, the delegate will invoke
549 // |before_request_callback_| later.
550 if (error != ERR_IO_PENDING)
551 BeforeRequestComplete(error);
552 return;
555 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
556 this, network_delegate_));
559 ///////////////////////////////////////////////////////////////////////////////
561 URLRequest::URLRequest(const GURL& url,
562 RequestPriority priority,
563 Delegate* delegate,
564 const URLRequestContext* context,
565 CookieStore* cookie_store,
566 NetworkDelegate* network_delegate)
567 : context_(context),
568 network_delegate_(network_delegate ? network_delegate
569 : context->network_delegate()),
570 net_log_(BoundNetLog::Make(context->net_log(),
571 NetLog::SOURCE_URL_REQUEST)),
572 url_chain_(1, url),
573 method_("GET"),
574 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
575 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
576 load_flags_(LOAD_NORMAL),
577 delegate_(delegate),
578 is_pending_(false),
579 is_redirecting_(false),
580 redirect_limit_(kMaxRedirects),
581 priority_(priority),
582 identifier_(GenerateURLRequestIdentifier()),
583 calling_delegate_(false),
584 use_blocked_by_as_load_param_(false),
585 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
586 base::Unretained(this))),
587 has_notified_completion_(false),
588 received_response_content_length_(0),
589 creation_time_(base::TimeTicks::Now()),
590 notified_before_network_start_(false),
591 cookie_store_(cookie_store ? cookie_store : context->cookie_store()) {
592 SIMPLE_STATS_COUNTER("URLRequestCount");
594 // Sanity check out environment.
595 DCHECK(base::MessageLoop::current())
596 << "The current base::MessageLoop must exist";
598 context->url_requests()->insert(this);
599 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
602 void URLRequest::BeforeRequestComplete(int error) {
603 DCHECK(!job_.get());
604 DCHECK_NE(ERR_IO_PENDING, error);
606 // Check that there are no callbacks to already canceled requests.
607 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
609 OnCallToDelegateComplete();
611 if (error != OK) {
612 std::string source("delegate");
613 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
614 NetLog::StringCallback("source", &source));
615 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
616 } else if (!delegate_redirect_url_.is_empty()) {
617 GURL new_url;
618 new_url.Swap(&delegate_redirect_url_);
620 URLRequestRedirectJob* job = new URLRequestRedirectJob(
621 this, network_delegate_, new_url,
622 // Use status code 307 to preserve the method, so POST requests work.
623 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
624 StartJob(job);
625 } else {
626 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
627 this, network_delegate_));
631 void URLRequest::StartJob(URLRequestJob* job) {
632 DCHECK(!is_pending_);
633 DCHECK(!job_.get());
635 net_log_.BeginEvent(
636 NetLog::TYPE_URL_REQUEST_START_JOB,
637 base::Bind(&NetLogURLRequestStartCallback,
638 &url(), &method_, load_flags_, priority_,
639 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
641 job_ = job;
642 job_->SetExtraRequestHeaders(extra_request_headers_);
643 job_->SetPriority(priority_);
645 if (upload_data_stream_.get())
646 job_->SetUpload(upload_data_stream_.get());
648 is_pending_ = true;
649 is_redirecting_ = false;
651 response_info_.was_cached = false;
653 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect(
654 referrer_policy_, referrer_, url())) {
655 if (!network_delegate_ ||
656 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
657 *this, url(), GURL(referrer_))) {
658 referrer_.clear();
659 } else {
660 // We need to clear the referrer anyway to avoid an infinite recursion
661 // when starting the error job.
662 referrer_.clear();
663 std::string source("delegate");
664 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
665 NetLog::StringCallback("source", &source));
666 RestartWithJob(new URLRequestErrorJob(
667 this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
668 return;
672 // Don't allow errors to be sent from within Start().
673 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
674 // we probably don't want this: they should be sent asynchronously so
675 // the caller does not get reentered.
676 job_->Start();
679 void URLRequest::Restart() {
680 // Should only be called if the original job didn't make any progress.
681 DCHECK(job_.get() && !job_->has_response_started());
682 RestartWithJob(
683 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
686 void URLRequest::RestartWithJob(URLRequestJob *job) {
687 DCHECK(job->request() == this);
688 PrepareToRestart();
689 StartJob(job);
692 void URLRequest::Cancel() {
693 DoCancel(ERR_ABORTED, SSLInfo());
696 void URLRequest::CancelWithError(int error) {
697 DoCancel(error, SSLInfo());
700 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
701 // This should only be called on a started request.
702 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
703 NOTREACHED();
704 return;
706 DoCancel(error, ssl_info);
709 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
710 DCHECK(error < 0);
711 // If cancelled while calling a delegate, clear delegate info.
712 if (calling_delegate_) {
713 LogUnblocked();
714 OnCallToDelegateComplete();
717 // If the URL request already has an error status, then canceling is a no-op.
718 // Plus, we don't want to change the error status once it has been set.
719 if (status_.is_success()) {
720 status_.set_status(URLRequestStatus::CANCELED);
721 status_.set_error(error);
722 response_info_.ssl_info = ssl_info;
724 // If the request hasn't already been completed, log a cancellation event.
725 if (!has_notified_completion_) {
726 // Don't log an error code on ERR_ABORTED, since that's redundant.
727 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
728 error == ERR_ABORTED ? OK : error);
732 if (is_pending_ && job_.get())
733 job_->Kill();
735 // We need to notify about the end of this job here synchronously. The
736 // Job sends an asynchronous notification but by the time this is processed,
737 // our |context_| is NULL.
738 NotifyRequestCompleted();
740 // The Job will call our NotifyDone method asynchronously. This is done so
741 // that the Delegate implementation can call Cancel without having to worry
742 // about being called recursively.
745 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
746 DCHECK(job_.get());
747 DCHECK(bytes_read);
748 *bytes_read = 0;
750 // If this is the first read, end the delegate call that may have started in
751 // OnResponseStarted.
752 OnCallToDelegateComplete();
754 // This handles a cancel that happens while paused.
755 // TODO(ahendrickson): DCHECK() that it is not done after
756 // http://crbug.com/115705 is fixed.
757 if (job_->is_done())
758 return false;
760 if (dest_size == 0) {
761 // Caller is not too bright. I guess we've done what they asked.
762 return true;
765 // Once the request fails or is cancelled, read will just return 0 bytes
766 // to indicate end of stream.
767 if (!status_.is_success()) {
768 return true;
771 bool rv = job_->Read(dest, dest_size, bytes_read);
772 // If rv is false, the status cannot be success.
773 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
774 if (rv && *bytes_read <= 0 && status_.is_success())
775 NotifyRequestCompleted();
776 return rv;
779 void URLRequest::StopCaching() {
780 DCHECK(job_.get());
781 job_->StopCaching();
784 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
785 bool* defer_redirect) {
786 is_redirecting_ = true;
788 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
789 URLRequestJob* job =
790 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
791 this, network_delegate_, redirect_info.new_url);
792 if (job) {
793 RestartWithJob(job);
794 } else if (delegate_) {
795 OnCallToDelegate();
797 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
798 tracked_objects::ScopedTracker tracking_profile(
799 FROM_HERE_WITH_EXPLICIT_FUNCTION(
800 "423948 URLRequest::Delegate::OnReceivedRedirect"));
801 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
802 // |this| may be have been destroyed here.
806 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
807 if (delegate_ && !notified_before_network_start_) {
808 OnCallToDelegate();
810 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
811 // fixed.
812 tracked_objects::ScopedTracker tracking_profile(
813 FROM_HERE_WITH_EXPLICIT_FUNCTION(
814 "423948 URLRequest::Delegate::OnBeforeNetworkStart"));
815 delegate_->OnBeforeNetworkStart(this, defer);
817 if (!*defer)
818 OnCallToDelegateComplete();
819 notified_before_network_start_ = true;
823 void URLRequest::ResumeNetworkStart() {
824 DCHECK(job_.get());
825 DCHECK(notified_before_network_start_);
827 OnCallToDelegateComplete();
828 job_->ResumeNetworkStart();
831 void URLRequest::NotifyResponseStarted() {
832 int net_error = OK;
833 if (!status_.is_success())
834 net_error = status_.error();
835 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
836 net_error);
838 URLRequestJob* job =
839 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
840 this, network_delegate_);
841 if (job) {
842 RestartWithJob(job);
843 } else {
844 if (delegate_) {
845 // In some cases (e.g. an event was canceled), we might have sent the
846 // completion event and receive a NotifyResponseStarted() later.
847 if (!has_notified_completion_ && status_.is_success()) {
848 if (network_delegate_)
849 network_delegate_->NotifyResponseStarted(this);
852 // Notify in case the entire URL Request has been finished.
853 if (!has_notified_completion_ && !status_.is_success())
854 NotifyRequestCompleted();
856 OnCallToDelegate();
857 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
858 // fixed.
859 tracked_objects::ScopedTracker tracking_profile(
860 FROM_HERE_WITH_EXPLICIT_FUNCTION(
861 "423948 URLRequest::Delegate::OnResponseStarted"));
862 delegate_->OnResponseStarted(this);
863 // Nothing may appear below this line as OnResponseStarted may delete
864 // |this|.
869 void URLRequest::FollowDeferredRedirect() {
870 CHECK(job_.get());
871 CHECK(status_.is_success());
873 job_->FollowDeferredRedirect();
876 void URLRequest::SetAuth(const AuthCredentials& credentials) {
877 DCHECK(job_.get());
878 DCHECK(job_->NeedsAuth());
880 job_->SetAuth(credentials);
883 void URLRequest::CancelAuth() {
884 DCHECK(job_.get());
885 DCHECK(job_->NeedsAuth());
887 job_->CancelAuth();
890 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
891 DCHECK(job_.get());
893 job_->ContinueWithCertificate(client_cert);
896 void URLRequest::ContinueDespiteLastError() {
897 DCHECK(job_.get());
899 job_->ContinueDespiteLastError();
902 void URLRequest::PrepareToRestart() {
903 DCHECK(job_.get());
905 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
906 // one.
907 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
909 OrphanJob();
911 response_info_ = HttpResponseInfo();
912 response_info_.request_time = base::Time::Now();
914 load_timing_info_ = LoadTimingInfo();
915 load_timing_info_.request_start_time = response_info_.request_time;
916 load_timing_info_.request_start = base::TimeTicks::Now();
918 status_ = URLRequestStatus();
919 is_pending_ = false;
922 void URLRequest::OrphanJob() {
923 // When calling this function, please check that URLRequestHttpJob is
924 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
925 // the call back. This is currently guaranteed by the following strategies:
926 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
927 // be receiving any headers at that time.
928 // - OrphanJob is called in ~URLRequest, in this case
929 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
930 // that the callback becomes invalid.
931 job_->Kill();
932 job_->DetachRequest(); // ensures that the job will not call us again
933 job_ = NULL;
936 int URLRequest::Redirect(const RedirectInfo& redirect_info) {
937 // Matches call in NotifyReceivedRedirect.
938 OnCallToDelegateComplete();
939 if (net_log_.IsLogging()) {
940 net_log_.AddEvent(
941 NetLog::TYPE_URL_REQUEST_REDIRECTED,
942 NetLog::StringCallback("location",
943 &redirect_info.new_url.possibly_invalid_spec()));
946 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
947 if (network_delegate_)
948 network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url);
950 if (redirect_limit_ <= 0) {
951 DVLOG(1) << "disallowing redirect: exceeds limit";
952 return ERR_TOO_MANY_REDIRECTS;
955 if (!redirect_info.new_url.is_valid())
956 return ERR_INVALID_URL;
958 if (!job_->IsSafeRedirect(redirect_info.new_url)) {
959 DVLOG(1) << "disallowing redirect: unsafe protocol";
960 return ERR_UNSAFE_REDIRECT;
963 if (!final_upload_progress_.position())
964 final_upload_progress_ = job_->GetUploadProgress();
965 PrepareToRestart();
967 if (redirect_info.new_method != method_) {
968 // TODO(davidben): This logic still needs to be replicated at the consumers.
969 if (method_ == "POST") {
970 // If being switched from POST, must remove headers that were specific to
971 // the POST and don't have meaning in other methods. For example the
972 // inclusion of a multipart Content-Type header in GET can cause problems
973 // with some servers:
974 // http://code.google.com/p/chromium/issues/detail?id=843
975 StripPostSpecificHeaders(&extra_request_headers_);
977 upload_data_stream_.reset();
978 method_ = redirect_info.new_method;
981 referrer_ = redirect_info.new_referrer;
982 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies;
984 url_chain_.push_back(redirect_info.new_url);
985 --redirect_limit_;
987 Start();
988 return OK;
991 const URLRequestContext* URLRequest::context() const {
992 return context_;
995 int64 URLRequest::GetExpectedContentSize() const {
996 int64 expected_content_size = -1;
997 if (job_.get())
998 expected_content_size = job_->expected_content_size();
1000 return expected_content_size;
1003 void URLRequest::SetPriority(RequestPriority priority) {
1004 DCHECK_GE(priority, MINIMUM_PRIORITY);
1005 DCHECK_LE(priority, MAXIMUM_PRIORITY);
1007 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
1008 NOTREACHED();
1009 // Maintain the invariant that requests with IGNORE_LIMITS set
1010 // have MAXIMUM_PRIORITY for release mode.
1011 return;
1014 if (priority_ == priority)
1015 return;
1017 priority_ = priority;
1018 if (job_.get()) {
1019 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
1020 NetLog::IntegerCallback("priority", priority_));
1021 job_->SetPriority(priority_);
1025 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
1026 const GURL& url = this->url();
1027 if (!url.SchemeIs("http"))
1028 return false;
1029 TransportSecurityState* state = context()->transport_security_state();
1030 if (state && state->ShouldUpgradeToSSL(url.host())) {
1031 url::Replacements<char> replacements;
1032 const char kNewScheme[] = "https";
1033 replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme)));
1034 *redirect_url = url.ReplaceComponents(replacements);
1035 return true;
1037 return false;
1040 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1041 NetworkDelegate::AuthRequiredResponse rv =
1042 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1043 auth_info_ = auth_info;
1044 if (network_delegate_) {
1045 OnCallToDelegate();
1046 rv = network_delegate_->NotifyAuthRequired(
1047 this,
1048 *auth_info,
1049 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1050 base::Unretained(this)),
1051 &auth_credentials_);
1052 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1053 return;
1056 NotifyAuthRequiredComplete(rv);
1059 void URLRequest::NotifyAuthRequiredComplete(
1060 NetworkDelegate::AuthRequiredResponse result) {
1061 OnCallToDelegateComplete();
1063 // Check that there are no callbacks to already canceled requests.
1064 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1066 // NotifyAuthRequired may be called multiple times, such as
1067 // when an authentication attempt fails. Clear out the data
1068 // so it can be reset on another round.
1069 AuthCredentials credentials = auth_credentials_;
1070 auth_credentials_ = AuthCredentials();
1071 scoped_refptr<AuthChallengeInfo> auth_info;
1072 auth_info.swap(auth_info_);
1074 switch (result) {
1075 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1076 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1077 // didn't take an action.
1078 if (delegate_) {
1079 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is
1080 // fixed.
1081 tracked_objects::ScopedTracker tracking_profile(
1082 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1083 "423948 URLRequest::Delegate::OnAuthRequired"));
1084 delegate_->OnAuthRequired(this, auth_info.get());
1086 break;
1088 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1089 SetAuth(credentials);
1090 break;
1092 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1093 CancelAuth();
1094 break;
1096 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1097 NOTREACHED();
1098 break;
1102 void URLRequest::NotifyCertificateRequested(
1103 SSLCertRequestInfo* cert_request_info) {
1104 if (delegate_) {
1105 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1106 tracked_objects::ScopedTracker tracking_profile(
1107 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1108 "423948 URLRequest::Delegate::OnCertificateRequested"));
1109 delegate_->OnCertificateRequested(this, cert_request_info);
1113 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1114 bool fatal) {
1115 if (delegate_) {
1116 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1117 tracked_objects::ScopedTracker tracking_profile(
1118 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1119 "423948 URLRequest::Delegate::OnSSLCertificateError"));
1120 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1124 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1125 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1126 if (network_delegate_) {
1127 return network_delegate_->CanGetCookies(*this, cookie_list);
1129 return g_default_can_use_cookies;
1132 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1133 CookieOptions* options) const {
1134 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1135 if (network_delegate_) {
1136 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1138 return g_default_can_use_cookies;
1141 bool URLRequest::CanEnablePrivacyMode() const {
1142 if (network_delegate_) {
1143 return network_delegate_->CanEnablePrivacyMode(url(),
1144 first_party_for_cookies_);
1146 return !g_default_can_use_cookies;
1150 void URLRequest::NotifyReadCompleted(int bytes_read) {
1151 // Notify in case the entire URL Request has been finished.
1152 if (bytes_read <= 0)
1153 NotifyRequestCompleted();
1155 // Notify NetworkChangeNotifier that we just received network data.
1156 // This is to identify cases where the NetworkChangeNotifier thinks we
1157 // are off-line but we are still receiving network data (crbug.com/124069),
1158 // and to get rough network connection measurements.
1159 if (bytes_read > 0 && !was_cached())
1160 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1162 if (delegate_) {
1163 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
1164 tracked_objects::ScopedTracker tracking_profile(
1165 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1166 "423948 URLRequest::Delegate::OnReadCompleted"));
1167 delegate_->OnReadCompleted(this, bytes_read);
1170 // Nothing below this line as OnReadCompleted may delete |this|.
1173 void URLRequest::OnHeadersComplete() {
1174 // Cache load timing information now, as information will be lost once the
1175 // socket is closed and the ClientSocketHandle is Reset, which will happen
1176 // once the body is complete. The start times should already be populated.
1177 if (job_.get()) {
1178 // Keep a copy of the two times the URLRequest sets.
1179 base::TimeTicks request_start = load_timing_info_.request_start;
1180 base::Time request_start_time = load_timing_info_.request_start_time;
1182 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1183 // consistent place to start from.
1184 load_timing_info_ = LoadTimingInfo();
1185 job_->GetLoadTimingInfo(&load_timing_info_);
1187 load_timing_info_.request_start = request_start;
1188 load_timing_info_.request_start_time = request_start_time;
1190 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1194 void URLRequest::NotifyRequestCompleted() {
1195 // TODO(battre): Get rid of this check, according to willchan it should
1196 // not be needed.
1197 if (has_notified_completion_)
1198 return;
1200 is_pending_ = false;
1201 is_redirecting_ = false;
1202 has_notified_completion_ = true;
1203 if (network_delegate_)
1204 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1207 void URLRequest::OnCallToDelegate() {
1208 DCHECK(!calling_delegate_);
1209 DCHECK(blocked_by_.empty());
1210 calling_delegate_ = true;
1211 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1214 void URLRequest::OnCallToDelegateComplete() {
1215 // This should have been cleared before resuming the request.
1216 DCHECK(blocked_by_.empty());
1217 if (!calling_delegate_)
1218 return;
1219 calling_delegate_ = false;
1220 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1223 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1224 base::debug::StackTrace* stack_trace_copy =
1225 new base::debug::StackTrace(NULL, 0);
1226 *stack_trace_copy = stack_trace;
1227 stack_trace_.reset(stack_trace_copy);
1230 const base::debug::StackTrace* URLRequest::stack_trace() const {
1231 return stack_trace_.get();
1234 } // namespace net