isolate_driver: Enable ninja parsing code all the time.
[chromium-blink-merge.git] / net / url_request / url_request.cc
blobe2784eed48366cb7e34ee1f03bd0229381a42135
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/histogram.h"
16 #include "base/metrics/stats_counters.h"
17 #include "base/metrics/user_metrics.h"
18 #include "base/stl_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/synchronization/lock.h"
21 #include "base/values.h"
22 #include "net/base/auth.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/url_request_context.h"
35 #include "net/url_request/url_request_error_job.h"
36 #include "net/url_request/url_request_job.h"
37 #include "net/url_request/url_request_job_manager.h"
38 #include "net/url_request/url_request_netlog_params.h"
39 #include "net/url_request/url_request_redirect_job.h"
41 using base::Time;
42 using std::string;
44 namespace net {
46 namespace {
48 // Max number of http redirects to follow. Same number as gecko.
49 const int kMaxRedirects = 20;
51 // Discard headers which have meaning in POST (Content-Length, Content-Type,
52 // Origin).
53 void StripPostSpecificHeaders(HttpRequestHeaders* headers) {
54 // These are headers that may be attached to a POST.
55 headers->RemoveHeader(HttpRequestHeaders::kContentLength);
56 headers->RemoveHeader(HttpRequestHeaders::kContentType);
57 headers->RemoveHeader(HttpRequestHeaders::kOrigin);
60 // TODO(battre): Delete this, see http://crbug.com/89321:
61 // This counter keeps track of the identifiers used for URL requests so far.
62 // 0 is reserved to represent an invalid ID.
63 uint64 g_next_url_request_identifier = 1;
65 // This lock protects g_next_url_request_identifier.
66 base::LazyInstance<base::Lock>::Leaky
67 g_next_url_request_identifier_lock = LAZY_INSTANCE_INITIALIZER;
69 // Returns an prior unused identifier for URL requests.
70 uint64 GenerateURLRequestIdentifier() {
71 base::AutoLock lock(g_next_url_request_identifier_lock.Get());
72 return g_next_url_request_identifier++;
75 // True once the first URLRequest was started.
76 bool g_url_requests_started = false;
78 // True if cookies are accepted by default.
79 bool g_default_can_use_cookies = true;
81 // When the URLRequest first assempts load timing information, it has the times
82 // at which each event occurred. The API requires the time which the request
83 // was blocked on each phase. This function handles the conversion.
85 // In the case of reusing a SPDY session or HTTP pipeline, old proxy results may
86 // have been reused, so proxy resolution times may be before the request was
87 // 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 URLRequest::ProtocolFactory*
146 URLRequest::Deprecated::RegisterProtocolFactory(const std::string& scheme,
147 ProtocolFactory* factory) {
148 return URLRequest::RegisterProtocolFactory(scheme, factory);
151 void URLRequest::Deprecated::RegisterRequestInterceptor(
152 Interceptor* interceptor) {
153 URLRequest::RegisterRequestInterceptor(interceptor);
156 void URLRequest::Deprecated::UnregisterRequestInterceptor(
157 Interceptor* interceptor) {
158 URLRequest::UnregisterRequestInterceptor(interceptor);
161 ///////////////////////////////////////////////////////////////////////////////
162 // URLRequest::Interceptor
164 URLRequestJob* URLRequest::Interceptor::MaybeInterceptRedirect(
165 URLRequest* request,
166 NetworkDelegate* network_delegate,
167 const GURL& location) {
168 return NULL;
171 URLRequestJob* URLRequest::Interceptor::MaybeInterceptResponse(
172 URLRequest* request, NetworkDelegate* network_delegate) {
173 return NULL;
176 ///////////////////////////////////////////////////////////////////////////////
177 // URLRequest::Delegate
179 void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
180 const GURL& new_url,
181 bool* defer_redirect) {
184 void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
185 AuthChallengeInfo* auth_info) {
186 request->CancelAuth();
189 void URLRequest::Delegate::OnCertificateRequested(
190 URLRequest* request,
191 SSLCertRequestInfo* cert_request_info) {
192 request->Cancel();
195 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
196 const SSLInfo& ssl_info,
197 bool is_hsts_ok) {
198 request->Cancel();
201 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request,
202 bool* defer) {
205 ///////////////////////////////////////////////////////////////////////////////
206 // URLRequest
208 URLRequest::URLRequest(const GURL& url,
209 RequestPriority priority,
210 Delegate* delegate,
211 const URLRequestContext* context)
212 : identifier_(GenerateURLRequestIdentifier()) {
213 Init(url, priority, delegate, context, NULL);
216 URLRequest::URLRequest(const GURL& url,
217 RequestPriority priority,
218 Delegate* delegate,
219 const URLRequestContext* context,
220 CookieStore* cookie_store)
221 : identifier_(GenerateURLRequestIdentifier()) {
222 Init(url, priority, delegate, context, cookie_store);
225 URLRequest::~URLRequest() {
226 Cancel();
228 if (network_delegate_) {
229 network_delegate_->NotifyURLRequestDestroyed(this);
230 if (job_.get())
231 job_->NotifyURLRequestDestroyed();
234 if (job_.get())
235 OrphanJob();
237 int deleted = context_->url_requests()->erase(this);
238 CHECK_EQ(1, deleted);
240 int net_error = OK;
241 // Log error only on failure, not cancellation, as even successful requests
242 // are "cancelled" on destruction.
243 if (status_.status() == URLRequestStatus::FAILED)
244 net_error = status_.error();
245 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
248 // static
249 URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory(
250 const string& scheme, ProtocolFactory* factory) {
251 return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme,
252 factory);
255 // static
256 void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) {
257 URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor);
260 // static
261 void URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) {
262 URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor(
263 interceptor);
266 void URLRequest::Init(const GURL& url,
267 RequestPriority priority,
268 Delegate* delegate,
269 const URLRequestContext* context,
270 CookieStore* cookie_store) {
271 context_ = context;
272 network_delegate_ = context->network_delegate();
273 net_log_ = BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST);
274 url_chain_.push_back(url);
275 method_ = "GET";
276 referrer_policy_ = CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE;
277 load_flags_ = LOAD_NORMAL;
278 delegate_ = delegate;
279 is_pending_ = false;
280 is_redirecting_ = false;
281 redirect_limit_ = kMaxRedirects;
282 priority_ = priority;
283 calling_delegate_ = false;
284 use_blocked_by_as_load_param_ =false;
285 before_request_callback_ = base::Bind(&URLRequest::BeforeRequestComplete,
286 base::Unretained(this));
287 has_notified_completion_ = false;
288 received_response_content_length_ = 0;
289 creation_time_ = base::TimeTicks::Now();
290 notified_before_network_start_ = false;
292 SIMPLE_STATS_COUNTER("URLRequestCount");
294 // Sanity check out environment.
295 DCHECK(base::MessageLoop::current())
296 << "The current base::MessageLoop must exist";
298 CHECK(context);
299 context->url_requests()->insert(this);
300 cookie_store_ = cookie_store;
301 if (cookie_store_ == NULL)
302 cookie_store_ = context->cookie_store();
304 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
307 void URLRequest::EnableChunkedUpload() {
308 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked());
309 if (!upload_data_stream_) {
310 upload_data_stream_.reset(
311 new UploadDataStream(UploadDataStream::CHUNKED, 0));
315 void URLRequest::AppendChunkToUpload(const char* bytes,
316 int bytes_len,
317 bool is_last_chunk) {
318 DCHECK(upload_data_stream_);
319 DCHECK(upload_data_stream_->is_chunked());
320 DCHECK_GT(bytes_len, 0);
321 upload_data_stream_->AppendChunk(bytes, bytes_len, is_last_chunk);
324 void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) {
325 DCHECK(!upload->is_chunked());
326 upload_data_stream_ = upload.Pass();
329 const UploadDataStream* URLRequest::get_upload() const {
330 return upload_data_stream_.get();
333 bool URLRequest::has_upload() const {
334 return upload_data_stream_.get() != NULL;
337 void URLRequest::SetExtraRequestHeaderById(int id, const string& value,
338 bool overwrite) {
339 DCHECK(!is_pending_ || is_redirecting_);
340 NOTREACHED() << "implement me!";
343 void URLRequest::SetExtraRequestHeaderByName(const string& name,
344 const string& value,
345 bool overwrite) {
346 DCHECK(!is_pending_ || is_redirecting_);
347 if (overwrite) {
348 extra_request_headers_.SetHeader(name, value);
349 } else {
350 extra_request_headers_.SetHeaderIfMissing(name, value);
354 void URLRequest::RemoveRequestHeaderByName(const string& name) {
355 DCHECK(!is_pending_ || is_redirecting_);
356 extra_request_headers_.RemoveHeader(name);
359 void URLRequest::SetExtraRequestHeaders(
360 const HttpRequestHeaders& headers) {
361 DCHECK(!is_pending_);
362 extra_request_headers_ = headers;
364 // NOTE: This method will likely become non-trivial once the other setters
365 // for request headers are implemented.
368 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
369 if (!job_.get())
370 return false;
372 return job_->GetFullRequestHeaders(headers);
375 int64 URLRequest::GetTotalReceivedBytes() const {
376 if (!job_.get())
377 return 0;
379 return job_->GetTotalReceivedBytes();
382 LoadStateWithParam URLRequest::GetLoadState() const {
383 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
384 // delegate before it has been started.
385 if (calling_delegate_ || !blocked_by_.empty()) {
386 return LoadStateWithParam(
387 LOAD_STATE_WAITING_FOR_DELEGATE,
388 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
389 base::string16());
391 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
392 base::string16());
395 base::Value* URLRequest::GetStateAsValue() const {
396 base::DictionaryValue* dict = new base::DictionaryValue();
397 dict->SetString("url", original_url().possibly_invalid_spec());
399 if (url_chain_.size() > 1) {
400 base::ListValue* list = new base::ListValue();
401 for (std::vector<GURL>::const_iterator url = url_chain_.begin();
402 url != url_chain_.end(); ++url) {
403 list->AppendString(url->possibly_invalid_spec());
405 dict->Set("url_chain", list);
408 dict->SetInteger("load_flags", load_flags_);
410 LoadStateWithParam load_state = GetLoadState();
411 dict->SetInteger("load_state", load_state.state);
412 if (!load_state.param.empty())
413 dict->SetString("load_state_param", load_state.param);
414 if (!blocked_by_.empty())
415 dict->SetString("delegate_info", blocked_by_);
417 dict->SetString("method", method_);
418 dict->SetBoolean("has_upload", has_upload());
419 dict->SetBoolean("is_pending", is_pending_);
421 // Add the status of the request. The status should always be IO_PENDING, and
422 // the error should always be OK, unless something is holding onto a request
423 // that has finished or a request was leaked. Neither of these should happen.
424 switch (status_.status()) {
425 case URLRequestStatus::SUCCESS:
426 dict->SetString("status", "SUCCESS");
427 break;
428 case URLRequestStatus::IO_PENDING:
429 dict->SetString("status", "IO_PENDING");
430 break;
431 case URLRequestStatus::CANCELED:
432 dict->SetString("status", "CANCELED");
433 break;
434 case URLRequestStatus::FAILED:
435 dict->SetString("status", "FAILED");
436 break;
438 if (status_.error() != OK)
439 dict->SetInteger("net_error", status_.error());
440 return dict;
443 void URLRequest::LogBlockedBy(const char* blocked_by) {
444 DCHECK(blocked_by);
445 DCHECK_GT(strlen(blocked_by), 0u);
447 // Only log information to NetLog during startup and certain deferring calls
448 // to delegates. For all reads but the first, do nothing.
449 if (!calling_delegate_ && !response_info_.request_time.is_null())
450 return;
452 LogUnblocked();
453 blocked_by_ = blocked_by;
454 use_blocked_by_as_load_param_ = false;
456 net_log_.BeginEvent(
457 NetLog::TYPE_DELEGATE_INFO,
458 NetLog::StringCallback("delegate_info", &blocked_by_));
461 void URLRequest::LogAndReportBlockedBy(const char* source) {
462 LogBlockedBy(source);
463 use_blocked_by_as_load_param_ = true;
466 void URLRequest::LogUnblocked() {
467 if (blocked_by_.empty())
468 return;
470 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
471 blocked_by_.clear();
474 UploadProgress URLRequest::GetUploadProgress() const {
475 if (!job_.get()) {
476 // We haven't started or the request was cancelled
477 return UploadProgress();
479 if (final_upload_progress_.position()) {
480 // The first job completed and none of the subsequent series of
481 // GETs when following redirects will upload anything, so we return the
482 // cached results from the initial job, the POST.
483 return final_upload_progress_;
485 return job_->GetUploadProgress();
488 void URLRequest::GetResponseHeaderById(int id, string* value) {
489 DCHECK(job_.get());
490 NOTREACHED() << "implement me!";
493 void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
494 DCHECK(value);
495 if (response_info_.headers.get()) {
496 response_info_.headers->GetNormalizedHeader(name, value);
497 } else {
498 value->clear();
502 void URLRequest::GetAllResponseHeaders(string* headers) {
503 DCHECK(headers);
504 if (response_info_.headers.get()) {
505 response_info_.headers->GetNormalizedHeaders(headers);
506 } else {
507 headers->clear();
511 HostPortPair URLRequest::GetSocketAddress() const {
512 DCHECK(job_.get());
513 return job_->GetSocketAddress();
516 HttpResponseHeaders* URLRequest::response_headers() const {
517 return response_info_.headers.get();
520 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
521 *load_timing_info = load_timing_info_;
524 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
525 DCHECK(job_.get());
526 return job_->GetResponseCookies(cookies);
529 void URLRequest::GetMimeType(string* mime_type) {
530 DCHECK(job_.get());
531 job_->GetMimeType(mime_type);
534 void URLRequest::GetCharset(string* charset) {
535 DCHECK(job_.get());
536 job_->GetCharset(charset);
539 int URLRequest::GetResponseCode() const {
540 DCHECK(job_.get());
541 return job_->GetResponseCode();
544 void URLRequest::SetLoadFlags(int flags) {
545 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
546 DCHECK(!job_);
547 DCHECK(flags & LOAD_IGNORE_LIMITS);
548 DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
550 load_flags_ = flags;
552 // This should be a no-op given the above DCHECKs, but do this
553 // anyway for release mode.
554 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
555 SetPriority(MAXIMUM_PRIORITY);
558 // static
559 void URLRequest::SetDefaultCookiePolicyToBlock() {
560 CHECK(!g_url_requests_started);
561 g_default_can_use_cookies = false;
564 // static
565 bool URLRequest::IsHandledProtocol(const std::string& scheme) {
566 return URLRequestJobManager::GetInstance()->SupportsScheme(scheme);
569 // static
570 bool URLRequest::IsHandledURL(const GURL& url) {
571 if (!url.is_valid()) {
572 // We handle error cases.
573 return true;
576 return IsHandledProtocol(url.scheme());
579 void URLRequest::set_first_party_for_cookies(
580 const GURL& first_party_for_cookies) {
581 first_party_for_cookies_ = first_party_for_cookies;
584 void URLRequest::set_method(const std::string& method) {
585 DCHECK(!is_pending_);
586 method_ = method;
589 // static
590 std::string URLRequest::ComputeMethodForRedirect(
591 const std::string& method,
592 int http_status_code) {
593 // For 303 redirects, all request methods except HEAD are converted to GET,
594 // as per the latest httpbis draft. The draft also allows POST requests to
595 // be converted to GETs when following 301/302 redirects, for historical
596 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and
597 // the httpbis draft say to prompt the user to confirm the generation of new
598 // requests, other than GET and HEAD requests, but IE omits these prompts and
599 // so shall we.
600 // See: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
601 if ((http_status_code == 303 && method != "HEAD") ||
602 ((http_status_code == 301 || http_status_code == 302) &&
603 method == "POST")) {
604 return "GET";
606 return method;
609 void URLRequest::SetReferrer(const std::string& referrer) {
610 DCHECK(!is_pending_);
611 GURL referrer_url(referrer);
612 UMA_HISTOGRAM_BOOLEAN("Net.URLRequest_SetReferrer_IsEmptyOrValid",
613 referrer_url.is_empty() || referrer_url.is_valid());
614 if (referrer_url.is_valid()) {
615 referrer_ = referrer_url.GetAsReferrer().spec();
616 } else {
617 referrer_ = referrer;
621 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
622 DCHECK(!is_pending_);
623 referrer_policy_ = referrer_policy;
626 void URLRequest::set_delegate(Delegate* delegate) {
627 delegate_ = delegate;
630 void URLRequest::Start() {
631 DCHECK_EQ(network_delegate_, context_->network_delegate());
632 // Anything that sets |blocked_by_| before start should have cleaned up after
633 // itself.
634 DCHECK(blocked_by_.empty());
636 g_url_requests_started = true;
637 response_info_.request_time = base::Time::Now();
639 load_timing_info_ = LoadTimingInfo();
640 load_timing_info_.request_start_time = response_info_.request_time;
641 load_timing_info_.request_start = base::TimeTicks::Now();
643 // Only notify the delegate for the initial request.
644 if (network_delegate_) {
645 OnCallToDelegate();
646 int error = network_delegate_->NotifyBeforeURLRequest(
647 this, before_request_callback_, &delegate_redirect_url_);
648 // If ERR_IO_PENDING is returned, the delegate will invoke
649 // |before_request_callback_| later.
650 if (error != ERR_IO_PENDING)
651 BeforeRequestComplete(error);
652 return;
655 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
656 this, network_delegate_));
659 ///////////////////////////////////////////////////////////////////////////////
661 void URLRequest::BeforeRequestComplete(int error) {
662 DCHECK(!job_.get());
663 DCHECK_NE(ERR_IO_PENDING, error);
664 DCHECK_EQ(network_delegate_, context_->network_delegate());
666 // Check that there are no callbacks to already canceled requests.
667 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
669 OnCallToDelegateComplete();
671 if (error != OK) {
672 std::string source("delegate");
673 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
674 NetLog::StringCallback("source", &source));
675 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
676 } else if (!delegate_redirect_url_.is_empty()) {
677 GURL new_url;
678 new_url.Swap(&delegate_redirect_url_);
680 URLRequestRedirectJob* job = new URLRequestRedirectJob(
681 this, network_delegate_, new_url,
682 // Use status code 307 to preserve the method, so POST requests work.
683 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
684 StartJob(job);
685 } else {
686 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
687 this, network_delegate_));
691 void URLRequest::StartJob(URLRequestJob* job) {
692 DCHECK(!is_pending_);
693 DCHECK(!job_.get());
695 net_log_.BeginEvent(
696 NetLog::TYPE_URL_REQUEST_START_JOB,
697 base::Bind(&NetLogURLRequestStartCallback,
698 &url(), &method_, load_flags_, priority_,
699 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
701 job_ = job;
702 job_->SetExtraRequestHeaders(extra_request_headers_);
703 job_->SetPriority(priority_);
705 if (upload_data_stream_.get())
706 job_->SetUpload(upload_data_stream_.get());
708 is_pending_ = true;
709 is_redirecting_ = false;
711 response_info_.was_cached = false;
713 // If the referrer is secure, but the requested URL is not, the referrer
714 // policy should be something non-default. If you hit this, please file a
715 // bug.
716 if (referrer_policy_ ==
717 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE &&
718 GURL(referrer_).SchemeIsSecure() && !url().SchemeIsSecure()) {
719 #if !defined(OFFICIAL_BUILD)
720 LOG(FATAL) << "Trying to send secure referrer for insecure load";
721 #endif
722 referrer_.clear();
723 base::RecordAction(
724 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
727 // Don't allow errors to be sent from within Start().
728 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
729 // we probably don't want this: they should be sent asynchronously so
730 // the caller does not get reentered.
731 job_->Start();
734 void URLRequest::Restart() {
735 // Should only be called if the original job didn't make any progress.
736 DCHECK(job_.get() && !job_->has_response_started());
737 RestartWithJob(
738 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
741 void URLRequest::RestartWithJob(URLRequestJob *job) {
742 DCHECK(job->request() == this);
743 PrepareToRestart();
744 StartJob(job);
747 void URLRequest::Cancel() {
748 DoCancel(ERR_ABORTED, SSLInfo());
751 void URLRequest::CancelWithError(int error) {
752 DoCancel(error, SSLInfo());
755 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
756 // This should only be called on a started request.
757 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
758 NOTREACHED();
759 return;
761 DoCancel(error, ssl_info);
764 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
765 DCHECK(error < 0);
766 // If cancelled while calling a delegate, clear delegate info.
767 if (calling_delegate_) {
768 LogUnblocked();
769 OnCallToDelegateComplete();
772 // If the URL request already has an error status, then canceling is a no-op.
773 // Plus, we don't want to change the error status once it has been set.
774 if (status_.is_success()) {
775 status_.set_status(URLRequestStatus::CANCELED);
776 status_.set_error(error);
777 response_info_.ssl_info = ssl_info;
779 // If the request hasn't already been completed, log a cancellation event.
780 if (!has_notified_completion_) {
781 // Don't log an error code on ERR_ABORTED, since that's redundant.
782 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
783 error == ERR_ABORTED ? OK : error);
787 if (is_pending_ && job_.get())
788 job_->Kill();
790 // We need to notify about the end of this job here synchronously. The
791 // Job sends an asynchronous notification but by the time this is processed,
792 // our |context_| is NULL.
793 NotifyRequestCompleted();
795 // The Job will call our NotifyDone method asynchronously. This is done so
796 // that the Delegate implementation can call Cancel without having to worry
797 // about being called recursively.
800 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
801 DCHECK(job_.get());
802 DCHECK(bytes_read);
803 *bytes_read = 0;
805 // If this is the first read, end the delegate call that may have started in
806 // OnResponseStarted.
807 OnCallToDelegateComplete();
809 // This handles a cancel that happens while paused.
810 // TODO(ahendrickson): DCHECK() that it is not done after
811 // http://crbug.com/115705 is fixed.
812 if (job_->is_done())
813 return false;
815 if (dest_size == 0) {
816 // Caller is not too bright. I guess we've done what they asked.
817 return true;
820 // Once the request fails or is cancelled, read will just return 0 bytes
821 // to indicate end of stream.
822 if (!status_.is_success()) {
823 return true;
826 bool rv = job_->Read(dest, dest_size, bytes_read);
827 // If rv is false, the status cannot be success.
828 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
829 if (rv && *bytes_read <= 0 && status_.is_success())
830 NotifyRequestCompleted();
831 return rv;
834 void URLRequest::StopCaching() {
835 DCHECK(job_.get());
836 job_->StopCaching();
839 void URLRequest::NotifyReceivedRedirect(const GURL& location,
840 bool* defer_redirect) {
841 is_redirecting_ = true;
843 URLRequestJob* job =
844 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
845 this, network_delegate_, location);
846 if (job) {
847 RestartWithJob(job);
848 } else if (delegate_) {
849 OnCallToDelegate();
850 delegate_->OnReceivedRedirect(this, location, defer_redirect);
851 // |this| may be have been destroyed here.
855 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
856 if (delegate_ && !notified_before_network_start_) {
857 OnCallToDelegate();
858 delegate_->OnBeforeNetworkStart(this, defer);
859 if (!*defer)
860 OnCallToDelegateComplete();
861 notified_before_network_start_ = true;
865 void URLRequest::ResumeNetworkStart() {
866 DCHECK(job_);
867 DCHECK(notified_before_network_start_);
869 OnCallToDelegateComplete();
870 job_->ResumeNetworkStart();
873 void URLRequest::NotifyResponseStarted() {
874 int net_error = OK;
875 if (!status_.is_success())
876 net_error = status_.error();
877 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
878 net_error);
880 URLRequestJob* job =
881 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
882 this, network_delegate_);
883 if (job) {
884 RestartWithJob(job);
885 } else {
886 if (delegate_) {
887 // In some cases (e.g. an event was canceled), we might have sent the
888 // completion event and receive a NotifyResponseStarted() later.
889 if (!has_notified_completion_ && status_.is_success()) {
890 if (network_delegate_)
891 network_delegate_->NotifyResponseStarted(this);
894 // Notify in case the entire URL Request has been finished.
895 if (!has_notified_completion_ && !status_.is_success())
896 NotifyRequestCompleted();
898 OnCallToDelegate();
899 delegate_->OnResponseStarted(this);
900 // Nothing may appear below this line as OnResponseStarted may delete
901 // |this|.
906 void URLRequest::FollowDeferredRedirect() {
907 CHECK(job_.get());
908 CHECK(status_.is_success());
910 job_->FollowDeferredRedirect();
913 void URLRequest::SetAuth(const AuthCredentials& credentials) {
914 DCHECK(job_.get());
915 DCHECK(job_->NeedsAuth());
917 job_->SetAuth(credentials);
920 void URLRequest::CancelAuth() {
921 DCHECK(job_.get());
922 DCHECK(job_->NeedsAuth());
924 job_->CancelAuth();
927 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
928 DCHECK(job_.get());
930 job_->ContinueWithCertificate(client_cert);
933 void URLRequest::ContinueDespiteLastError() {
934 DCHECK(job_.get());
936 job_->ContinueDespiteLastError();
939 void URLRequest::PrepareToRestart() {
940 DCHECK(job_.get());
942 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
943 // one.
944 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
946 OrphanJob();
948 response_info_ = HttpResponseInfo();
949 response_info_.request_time = base::Time::Now();
951 load_timing_info_ = LoadTimingInfo();
952 load_timing_info_.request_start_time = response_info_.request_time;
953 load_timing_info_.request_start = base::TimeTicks::Now();
955 status_ = URLRequestStatus();
956 is_pending_ = false;
959 void URLRequest::OrphanJob() {
960 // When calling this function, please check that URLRequestHttpJob is
961 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
962 // the call back. This is currently guaranteed by the following strategies:
963 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
964 // be receiving any headers at that time.
965 // - OrphanJob is called in ~URLRequest, in this case
966 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
967 // that the callback becomes invalid.
968 job_->Kill();
969 job_->DetachRequest(); // ensures that the job will not call us again
970 job_ = NULL;
973 int URLRequest::Redirect(const GURL& location, int http_status_code) {
974 // Matches call in NotifyReceivedRedirect.
975 OnCallToDelegateComplete();
976 if (net_log_.IsLogging()) {
977 net_log_.AddEvent(
978 NetLog::TYPE_URL_REQUEST_REDIRECTED,
979 NetLog::StringCallback("location", &location.possibly_invalid_spec()));
982 if (network_delegate_)
983 network_delegate_->NotifyBeforeRedirect(this, location);
985 if (redirect_limit_ <= 0) {
986 DVLOG(1) << "disallowing redirect: exceeds limit";
987 return ERR_TOO_MANY_REDIRECTS;
990 if (!location.is_valid())
991 return ERR_INVALID_URL;
993 if (!job_->IsSafeRedirect(location)) {
994 DVLOG(1) << "disallowing redirect: unsafe protocol";
995 return ERR_UNSAFE_REDIRECT;
998 if (!final_upload_progress_.position())
999 final_upload_progress_ = job_->GetUploadProgress();
1000 PrepareToRestart();
1002 std::string new_method(ComputeMethodForRedirect(method_, http_status_code));
1003 if (new_method != method_) {
1004 if (method_ == "POST") {
1005 // If being switched from POST, must remove headers that were specific to
1006 // the POST and don't have meaning in other methods. For example the
1007 // inclusion of a multipart Content-Type header in GET can cause problems
1008 // with some servers:
1009 // http://code.google.com/p/chromium/issues/detail?id=843
1010 StripPostSpecificHeaders(&extra_request_headers_);
1012 upload_data_stream_.reset();
1013 method_.swap(new_method);
1016 // Suppress the referrer if we're redirecting out of https.
1017 if (referrer_policy_ ==
1018 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE &&
1019 GURL(referrer_).SchemeIsSecure() && !location.SchemeIsSecure()) {
1020 referrer_.clear();
1023 url_chain_.push_back(location);
1024 --redirect_limit_;
1026 Start();
1027 return OK;
1030 const URLRequestContext* URLRequest::context() const {
1031 return context_;
1034 int64 URLRequest::GetExpectedContentSize() const {
1035 int64 expected_content_size = -1;
1036 if (job_.get())
1037 expected_content_size = job_->expected_content_size();
1039 return expected_content_size;
1042 void URLRequest::SetPriority(RequestPriority priority) {
1043 DCHECK_GE(priority, MINIMUM_PRIORITY);
1044 DCHECK_LE(priority, MAXIMUM_PRIORITY);
1046 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
1047 NOTREACHED();
1048 // Maintain the invariant that requests with IGNORE_LIMITS set
1049 // have MAXIMUM_PRIORITY for release mode.
1050 return;
1053 if (priority_ == priority)
1054 return;
1056 priority_ = priority;
1057 if (job_.get()) {
1058 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
1059 NetLog::IntegerCallback("priority", priority_));
1060 job_->SetPriority(priority_);
1064 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
1065 const GURL& url = this->url();
1066 if (!url.SchemeIs("http"))
1067 return false;
1068 TransportSecurityState* state = context()->transport_security_state();
1069 if (state &&
1070 state->ShouldUpgradeToSSL(
1071 url.host(),
1072 SSLConfigService::IsSNIAvailable(context()->ssl_config_service()))) {
1073 url::Replacements<char> replacements;
1074 const char kNewScheme[] = "https";
1075 replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme)));
1076 *redirect_url = url.ReplaceComponents(replacements);
1077 return true;
1079 return false;
1082 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1083 NetworkDelegate::AuthRequiredResponse rv =
1084 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1085 auth_info_ = auth_info;
1086 if (network_delegate_) {
1087 OnCallToDelegate();
1088 rv = network_delegate_->NotifyAuthRequired(
1089 this,
1090 *auth_info,
1091 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1092 base::Unretained(this)),
1093 &auth_credentials_);
1094 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1095 return;
1098 NotifyAuthRequiredComplete(rv);
1101 void URLRequest::NotifyAuthRequiredComplete(
1102 NetworkDelegate::AuthRequiredResponse result) {
1103 OnCallToDelegateComplete();
1105 // Check that there are no callbacks to already canceled requests.
1106 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1108 // NotifyAuthRequired may be called multiple times, such as
1109 // when an authentication attempt fails. Clear out the data
1110 // so it can be reset on another round.
1111 AuthCredentials credentials = auth_credentials_;
1112 auth_credentials_ = AuthCredentials();
1113 scoped_refptr<AuthChallengeInfo> auth_info;
1114 auth_info.swap(auth_info_);
1116 switch (result) {
1117 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1118 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1119 // didn't take an action.
1120 if (delegate_)
1121 delegate_->OnAuthRequired(this, auth_info.get());
1122 break;
1124 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1125 SetAuth(credentials);
1126 break;
1128 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1129 CancelAuth();
1130 break;
1132 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1133 NOTREACHED();
1134 break;
1138 void URLRequest::NotifyCertificateRequested(
1139 SSLCertRequestInfo* cert_request_info) {
1140 if (delegate_)
1141 delegate_->OnCertificateRequested(this, cert_request_info);
1144 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1145 bool fatal) {
1146 if (delegate_)
1147 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1150 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1151 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1152 if (network_delegate_) {
1153 return network_delegate_->CanGetCookies(*this, cookie_list);
1155 return g_default_can_use_cookies;
1158 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1159 CookieOptions* options) const {
1160 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1161 if (network_delegate_) {
1162 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1164 return g_default_can_use_cookies;
1167 bool URLRequest::CanEnablePrivacyMode() const {
1168 if (network_delegate_) {
1169 return network_delegate_->CanEnablePrivacyMode(url(),
1170 first_party_for_cookies_);
1172 return !g_default_can_use_cookies;
1176 void URLRequest::NotifyReadCompleted(int bytes_read) {
1177 // Notify in case the entire URL Request has been finished.
1178 if (bytes_read <= 0)
1179 NotifyRequestCompleted();
1181 // Notify NetworkChangeNotifier that we just received network data.
1182 // This is to identify cases where the NetworkChangeNotifier thinks we
1183 // are off-line but we are still receiving network data (crbug.com/124069),
1184 // and to get rough network connection measurements.
1185 if (bytes_read > 0 && !was_cached())
1186 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1188 if (delegate_)
1189 delegate_->OnReadCompleted(this, bytes_read);
1191 // Nothing below this line as OnReadCompleted may delete |this|.
1194 void URLRequest::OnHeadersComplete() {
1195 // Cache load timing information now, as information will be lost once the
1196 // socket is closed and the ClientSocketHandle is Reset, which will happen
1197 // once the body is complete. The start times should already be populated.
1198 if (job_.get()) {
1199 // Keep a copy of the two times the URLRequest sets.
1200 base::TimeTicks request_start = load_timing_info_.request_start;
1201 base::Time request_start_time = load_timing_info_.request_start_time;
1203 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1204 // consistent place to start from.
1205 load_timing_info_ = LoadTimingInfo();
1206 job_->GetLoadTimingInfo(&load_timing_info_);
1208 load_timing_info_.request_start = request_start;
1209 load_timing_info_.request_start_time = request_start_time;
1211 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1215 void URLRequest::NotifyRequestCompleted() {
1216 // TODO(battre): Get rid of this check, according to willchan it should
1217 // not be needed.
1218 if (has_notified_completion_)
1219 return;
1221 is_pending_ = false;
1222 is_redirecting_ = false;
1223 has_notified_completion_ = true;
1224 if (network_delegate_)
1225 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1228 void URLRequest::OnCallToDelegate() {
1229 DCHECK(!calling_delegate_);
1230 DCHECK(blocked_by_.empty());
1231 calling_delegate_ = true;
1232 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1235 void URLRequest::OnCallToDelegateComplete() {
1236 // This should have been cleared before resuming the request.
1237 DCHECK(blocked_by_.empty());
1238 if (!calling_delegate_)
1239 return;
1240 calling_delegate_ = false;
1241 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1244 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1245 base::debug::StackTrace* stack_trace_copy =
1246 new base::debug::StackTrace(NULL, 0);
1247 *stack_trace_copy = stack_trace;
1248 stack_trace_.reset(stack_trace_copy);
1251 const base::debug::StackTrace* URLRequest::stack_trace() const {
1252 return stack_trace_.get();
1255 } // namespace net