Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / net / url_request / url_request.cc
blob46f3a18c219cb7e594f9eeb7a228694dc87f1a54
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/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/host_port_pair.h"
23 #include "net/base/load_flags.h"
24 #include "net/base/load_timing_info.h"
25 #include "net/base/net_errors.h"
26 #include "net/base/net_log.h"
27 #include "net/base/network_change_notifier.h"
28 #include "net/base/network_delegate.h"
29 #include "net/base/upload_data_stream.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_util.h"
32 #include "net/ssl/ssl_cert_request_info.h"
33 #include "net/url_request/url_request_context.h"
34 #include "net/url_request/url_request_error_job.h"
35 #include "net/url_request/url_request_job.h"
36 #include "net/url_request/url_request_job_manager.h"
37 #include "net/url_request/url_request_netlog_params.h"
38 #include "net/url_request/url_request_redirect_job.h"
40 using base::Time;
41 using std::string;
43 namespace net {
45 namespace {
47 // Max number of http redirects to follow. Same number as gecko.
48 const int kMaxRedirects = 20;
50 // Discard headers which have meaning in POST (Content-Length, Content-Type,
51 // Origin).
52 void StripPostSpecificHeaders(HttpRequestHeaders* headers) {
53 // These are headers that may be attached to a POST.
54 headers->RemoveHeader(HttpRequestHeaders::kContentLength);
55 headers->RemoveHeader(HttpRequestHeaders::kContentType);
56 headers->RemoveHeader(HttpRequestHeaders::kOrigin);
59 // TODO(battre): Delete this, see http://crbug.com/89321:
60 // This counter keeps track of the identifiers used for URL requests so far.
61 // 0 is reserved to represent an invalid ID.
62 uint64 g_next_url_request_identifier = 1;
64 // This lock protects g_next_url_request_identifier.
65 base::LazyInstance<base::Lock>::Leaky
66 g_next_url_request_identifier_lock = LAZY_INSTANCE_INITIALIZER;
68 // Returns an prior unused identifier for URL requests.
69 uint64 GenerateURLRequestIdentifier() {
70 base::AutoLock lock(g_next_url_request_identifier_lock.Get());
71 return g_next_url_request_identifier++;
74 // True once the first URLRequest was started.
75 bool g_url_requests_started = false;
77 // True if cookies are accepted by default.
78 bool g_default_can_use_cookies = true;
80 // When the URLRequest first assempts load timing information, it has the times
81 // at which each event occurred. The API requires the time which the request
82 // was blocked on each phase. This function handles the conversion.
84 // In the case of reusing a SPDY session or HTTP pipeline, old proxy results may
85 // have been reused, so proxy resolution times may be before the request was
86 // started.
88 // Due to preconnect and late binding, it is also possible for the connection
89 // attempt to start before a request has been started, or proxy resolution
90 // completed.
92 // This functions fixes both those cases.
93 void ConvertRealLoadTimesToBlockingTimes(
94 net::LoadTimingInfo* load_timing_info) {
95 DCHECK(!load_timing_info->request_start.is_null());
97 // Earliest time possible for the request to be blocking on connect events.
98 base::TimeTicks block_on_connect = load_timing_info->request_start;
100 if (!load_timing_info->proxy_resolve_start.is_null()) {
101 DCHECK(!load_timing_info->proxy_resolve_end.is_null());
103 // Make sure the proxy times are after request start.
104 if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
105 load_timing_info->proxy_resolve_start = load_timing_info->request_start;
106 if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
107 load_timing_info->proxy_resolve_end = load_timing_info->request_start;
109 // Connect times must also be after the proxy times.
110 block_on_connect = load_timing_info->proxy_resolve_end;
113 // Make sure connection times are after start and proxy times.
115 net::LoadTimingInfo::ConnectTiming* connect_timing =
116 &load_timing_info->connect_timing;
117 if (!connect_timing->dns_start.is_null()) {
118 DCHECK(!connect_timing->dns_end.is_null());
119 if (connect_timing->dns_start < block_on_connect)
120 connect_timing->dns_start = block_on_connect;
121 if (connect_timing->dns_end < block_on_connect)
122 connect_timing->dns_end = block_on_connect;
125 if (!connect_timing->connect_start.is_null()) {
126 DCHECK(!connect_timing->connect_end.is_null());
127 if (connect_timing->connect_start < block_on_connect)
128 connect_timing->connect_start = block_on_connect;
129 if (connect_timing->connect_end < block_on_connect)
130 connect_timing->connect_end = block_on_connect;
133 if (!connect_timing->ssl_start.is_null()) {
134 DCHECK(!connect_timing->ssl_end.is_null());
135 if (connect_timing->ssl_start < block_on_connect)
136 connect_timing->ssl_start = block_on_connect;
137 if (connect_timing->ssl_end < block_on_connect)
138 connect_timing->ssl_end = block_on_connect;
142 } // namespace
144 URLRequest::ProtocolFactory*
145 URLRequest::Deprecated::RegisterProtocolFactory(const std::string& scheme,
146 ProtocolFactory* factory) {
147 return URLRequest::RegisterProtocolFactory(scheme, factory);
150 void URLRequest::Deprecated::RegisterRequestInterceptor(
151 Interceptor* interceptor) {
152 URLRequest::RegisterRequestInterceptor(interceptor);
155 void URLRequest::Deprecated::UnregisterRequestInterceptor(
156 Interceptor* interceptor) {
157 URLRequest::UnregisterRequestInterceptor(interceptor);
160 ///////////////////////////////////////////////////////////////////////////////
161 // URLRequest::Interceptor
163 URLRequestJob* URLRequest::Interceptor::MaybeInterceptRedirect(
164 URLRequest* request,
165 NetworkDelegate* network_delegate,
166 const GURL& location) {
167 return NULL;
170 URLRequestJob* URLRequest::Interceptor::MaybeInterceptResponse(
171 URLRequest* request, NetworkDelegate* network_delegate) {
172 return NULL;
175 ///////////////////////////////////////////////////////////////////////////////
176 // URLRequest::Delegate
178 void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
179 const GURL& new_url,
180 bool* defer_redirect) {
183 void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
184 AuthChallengeInfo* auth_info) {
185 request->CancelAuth();
188 void URLRequest::Delegate::OnCertificateRequested(
189 URLRequest* request,
190 SSLCertRequestInfo* cert_request_info) {
191 request->Cancel();
194 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
195 const SSLInfo& ssl_info,
196 bool is_hsts_ok) {
197 request->Cancel();
200 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request,
201 bool* defer) {
204 ///////////////////////////////////////////////////////////////////////////////
205 // URLRequest
207 URLRequest::URLRequest(const GURL& url,
208 RequestPriority priority,
209 Delegate* delegate,
210 const URLRequestContext* context)
211 : context_(context),
212 network_delegate_(context->network_delegate()),
213 net_log_(BoundNetLog::Make(context->net_log(),
214 NetLog::SOURCE_URL_REQUEST)),
215 url_chain_(1, url),
216 method_("GET"),
217 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
218 load_flags_(LOAD_NORMAL),
219 delegate_(delegate),
220 is_pending_(false),
221 is_redirecting_(false),
222 redirect_limit_(kMaxRedirects),
223 priority_(priority),
224 identifier_(GenerateURLRequestIdentifier()),
225 calling_delegate_(false),
226 use_blocked_by_as_load_param_(false),
227 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
228 base::Unretained(this))),
229 has_notified_completion_(false),
230 received_response_content_length_(0),
231 creation_time_(base::TimeTicks::Now()),
232 notified_before_network_start_(false) {
233 SIMPLE_STATS_COUNTER("URLRequestCount");
235 // Sanity check out environment.
236 DCHECK(base::MessageLoop::current())
237 << "The current base::MessageLoop must exist";
239 CHECK(context);
240 context->url_requests()->insert(this);
242 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
245 URLRequest::~URLRequest() {
246 Cancel();
248 if (network_delegate_) {
249 network_delegate_->NotifyURLRequestDestroyed(this);
250 if (job_.get())
251 job_->NotifyURLRequestDestroyed();
254 if (job_.get())
255 OrphanJob();
257 int deleted = context_->url_requests()->erase(this);
258 CHECK_EQ(1, deleted);
260 int net_error = OK;
261 // Log error only on failure, not cancellation, as even successful requests
262 // are "cancelled" on destruction.
263 if (status_.status() == URLRequestStatus::FAILED)
264 net_error = status_.error();
265 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
268 // static
269 URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory(
270 const string& scheme, ProtocolFactory* factory) {
271 return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme,
272 factory);
275 // static
276 void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) {
277 URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor);
280 // static
281 void URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) {
282 URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor(
283 interceptor);
286 void URLRequest::EnableChunkedUpload() {
287 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked());
288 if (!upload_data_stream_) {
289 upload_data_stream_.reset(
290 new UploadDataStream(UploadDataStream::CHUNKED, 0));
294 void URLRequest::AppendChunkToUpload(const char* bytes,
295 int bytes_len,
296 bool is_last_chunk) {
297 DCHECK(upload_data_stream_);
298 DCHECK(upload_data_stream_->is_chunked());
299 DCHECK_GT(bytes_len, 0);
300 upload_data_stream_->AppendChunk(bytes, bytes_len, is_last_chunk);
303 void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) {
304 DCHECK(!upload->is_chunked());
305 upload_data_stream_ = upload.Pass();
308 const UploadDataStream* URLRequest::get_upload() const {
309 return upload_data_stream_.get();
312 bool URLRequest::has_upload() const {
313 return upload_data_stream_.get() != NULL;
316 void URLRequest::SetExtraRequestHeaderById(int id, const string& value,
317 bool overwrite) {
318 DCHECK(!is_pending_ || is_redirecting_);
319 NOTREACHED() << "implement me!";
322 void URLRequest::SetExtraRequestHeaderByName(const string& name,
323 const string& value,
324 bool overwrite) {
325 DCHECK(!is_pending_ || is_redirecting_);
326 if (overwrite) {
327 extra_request_headers_.SetHeader(name, value);
328 } else {
329 extra_request_headers_.SetHeaderIfMissing(name, value);
333 void URLRequest::RemoveRequestHeaderByName(const string& name) {
334 DCHECK(!is_pending_ || is_redirecting_);
335 extra_request_headers_.RemoveHeader(name);
338 void URLRequest::SetExtraRequestHeaders(
339 const HttpRequestHeaders& headers) {
340 DCHECK(!is_pending_);
341 extra_request_headers_ = headers;
343 // NOTE: This method will likely become non-trivial once the other setters
344 // for request headers are implemented.
347 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
348 if (!job_.get())
349 return false;
351 return job_->GetFullRequestHeaders(headers);
354 int64 URLRequest::GetTotalReceivedBytes() const {
355 if (!job_.get())
356 return 0;
358 return job_->GetTotalReceivedBytes();
361 LoadStateWithParam URLRequest::GetLoadState() const {
362 // The !blocked_by_.empty() check allows |this| to report it's blocked on a
363 // delegate before it has been started.
364 if (calling_delegate_ || !blocked_by_.empty()) {
365 return LoadStateWithParam(
366 LOAD_STATE_WAITING_FOR_DELEGATE,
367 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
368 base::string16());
370 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
371 base::string16());
374 base::Value* URLRequest::GetStateAsValue() const {
375 base::DictionaryValue* dict = new base::DictionaryValue();
376 dict->SetString("url", original_url().possibly_invalid_spec());
378 if (url_chain_.size() > 1) {
379 base::ListValue* list = new base::ListValue();
380 for (std::vector<GURL>::const_iterator url = url_chain_.begin();
381 url != url_chain_.end(); ++url) {
382 list->AppendString(url->possibly_invalid_spec());
384 dict->Set("url_chain", list);
387 dict->SetInteger("load_flags", load_flags_);
389 LoadStateWithParam load_state = GetLoadState();
390 dict->SetInteger("load_state", load_state.state);
391 if (!load_state.param.empty())
392 dict->SetString("load_state_param", load_state.param);
393 if (!blocked_by_.empty())
394 dict->SetString("delegate_info", blocked_by_);
396 dict->SetString("method", method_);
397 dict->SetBoolean("has_upload", has_upload());
398 dict->SetBoolean("is_pending", is_pending_);
400 // Add the status of the request. The status should always be IO_PENDING, and
401 // the error should always be OK, unless something is holding onto a request
402 // that has finished or a request was leaked. Neither of these should happen.
403 switch (status_.status()) {
404 case URLRequestStatus::SUCCESS:
405 dict->SetString("status", "SUCCESS");
406 break;
407 case URLRequestStatus::IO_PENDING:
408 dict->SetString("status", "IO_PENDING");
409 break;
410 case URLRequestStatus::CANCELED:
411 dict->SetString("status", "CANCELED");
412 break;
413 case URLRequestStatus::FAILED:
414 dict->SetString("status", "FAILED");
415 break;
417 if (status_.error() != OK)
418 dict->SetInteger("net_error", status_.error());
419 return dict;
422 void URLRequest::LogBlockedBy(const char* blocked_by) {
423 DCHECK(blocked_by);
424 DCHECK_GT(strlen(blocked_by), 0u);
426 // Only log information to NetLog during startup and certain deferring calls
427 // to delegates. For all reads but the first, do nothing.
428 if (!calling_delegate_ && !response_info_.request_time.is_null())
429 return;
431 LogUnblocked();
432 blocked_by_ = blocked_by;
433 use_blocked_by_as_load_param_ = false;
435 net_log_.BeginEvent(
436 NetLog::TYPE_DELEGATE_INFO,
437 NetLog::StringCallback("delegate_info", &blocked_by_));
440 void URLRequest::LogAndReportBlockedBy(const char* source) {
441 LogBlockedBy(source);
442 use_blocked_by_as_load_param_ = true;
445 void URLRequest::LogUnblocked() {
446 if (blocked_by_.empty())
447 return;
449 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
450 blocked_by_.clear();
453 UploadProgress URLRequest::GetUploadProgress() const {
454 if (!job_.get()) {
455 // We haven't started or the request was cancelled
456 return UploadProgress();
458 if (final_upload_progress_.position()) {
459 // The first job completed and none of the subsequent series of
460 // GETs when following redirects will upload anything, so we return the
461 // cached results from the initial job, the POST.
462 return final_upload_progress_;
464 return job_->GetUploadProgress();
467 void URLRequest::GetResponseHeaderById(int id, string* value) {
468 DCHECK(job_.get());
469 NOTREACHED() << "implement me!";
472 void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
473 DCHECK(value);
474 if (response_info_.headers.get()) {
475 response_info_.headers->GetNormalizedHeader(name, value);
476 } else {
477 value->clear();
481 void URLRequest::GetAllResponseHeaders(string* headers) {
482 DCHECK(headers);
483 if (response_info_.headers.get()) {
484 response_info_.headers->GetNormalizedHeaders(headers);
485 } else {
486 headers->clear();
490 HostPortPair URLRequest::GetSocketAddress() const {
491 DCHECK(job_.get());
492 return job_->GetSocketAddress();
495 HttpResponseHeaders* URLRequest::response_headers() const {
496 return response_info_.headers.get();
499 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
500 *load_timing_info = load_timing_info_;
503 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
504 DCHECK(job_.get());
505 return job_->GetResponseCookies(cookies);
508 void URLRequest::GetMimeType(string* mime_type) {
509 DCHECK(job_.get());
510 job_->GetMimeType(mime_type);
513 void URLRequest::GetCharset(string* charset) {
514 DCHECK(job_.get());
515 job_->GetCharset(charset);
518 int URLRequest::GetResponseCode() const {
519 DCHECK(job_.get());
520 return job_->GetResponseCode();
523 void URLRequest::SetLoadFlags(int flags) {
524 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
525 DCHECK(!job_);
526 DCHECK(flags & LOAD_IGNORE_LIMITS);
527 DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
529 load_flags_ = flags;
531 // This should be a no-op given the above DCHECKs, but do this
532 // anyway for release mode.
533 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
534 SetPriority(MAXIMUM_PRIORITY);
537 // static
538 void URLRequest::SetDefaultCookiePolicyToBlock() {
539 CHECK(!g_url_requests_started);
540 g_default_can_use_cookies = false;
543 // static
544 bool URLRequest::IsHandledProtocol(const std::string& scheme) {
545 return URLRequestJobManager::GetInstance()->SupportsScheme(scheme);
548 // static
549 bool URLRequest::IsHandledURL(const GURL& url) {
550 if (!url.is_valid()) {
551 // We handle error cases.
552 return true;
555 return IsHandledProtocol(url.scheme());
558 void URLRequest::set_first_party_for_cookies(
559 const GURL& first_party_for_cookies) {
560 first_party_for_cookies_ = first_party_for_cookies;
563 void URLRequest::set_method(const std::string& method) {
564 DCHECK(!is_pending_);
565 method_ = method;
568 // static
569 std::string URLRequest::ComputeMethodForRedirect(
570 const std::string& method,
571 int http_status_code) {
572 // For 303 redirects, all request methods except HEAD are converted to GET,
573 // as per the latest httpbis draft. The draft also allows POST requests to
574 // be converted to GETs when following 301/302 redirects, for historical
575 // reasons. Most major browsers do this and so shall we. Both RFC 2616 and
576 // the httpbis draft say to prompt the user to confirm the generation of new
577 // requests, other than GET and HEAD requests, but IE omits these prompts and
578 // so shall we.
579 // See: https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
580 if ((http_status_code == 303 && method != "HEAD") ||
581 ((http_status_code == 301 || http_status_code == 302) &&
582 method == "POST")) {
583 return "GET";
585 return method;
588 void URLRequest::SetReferrer(const std::string& referrer) {
589 DCHECK(!is_pending_);
590 referrer_ = referrer;
591 // Ensure that we do not send URL fragment, username and password
592 // fields in the referrer.
593 GURL referrer_url(referrer);
594 UMA_HISTOGRAM_BOOLEAN("Net.URLRequest_SetReferrer_IsEmptyOrValid",
595 referrer_url.is_empty() || referrer_url.is_valid());
596 if (referrer_url.is_valid() && (referrer_url.has_ref() ||
597 referrer_url.has_username() || referrer_url.has_password())) {
598 GURL::Replacements referrer_mods;
599 referrer_mods.ClearRef();
600 referrer_mods.ClearUsername();
601 referrer_mods.ClearPassword();
602 referrer_url = referrer_url.ReplaceComponents(referrer_mods);
603 referrer_ = referrer_url.spec();
607 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
608 DCHECK(!is_pending_);
609 referrer_policy_ = referrer_policy;
612 void URLRequest::set_delegate(Delegate* delegate) {
613 delegate_ = delegate;
616 void URLRequest::Start() {
617 DCHECK_EQ(network_delegate_, context_->network_delegate());
618 // Anything that sets |blocked_by_| before start should have cleaned up after
619 // itself.
620 DCHECK(blocked_by_.empty());
622 g_url_requests_started = true;
623 response_info_.request_time = base::Time::Now();
625 load_timing_info_ = LoadTimingInfo();
626 load_timing_info_.request_start_time = response_info_.request_time;
627 load_timing_info_.request_start = base::TimeTicks::Now();
629 // Only notify the delegate for the initial request.
630 if (network_delegate_) {
631 OnCallToDelegate();
632 int error = network_delegate_->NotifyBeforeURLRequest(
633 this, before_request_callback_, &delegate_redirect_url_);
634 // If ERR_IO_PENDING is returned, the delegate will invoke
635 // |before_request_callback_| later.
636 if (error != ERR_IO_PENDING)
637 BeforeRequestComplete(error);
638 return;
641 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
642 this, network_delegate_));
645 ///////////////////////////////////////////////////////////////////////////////
647 void URLRequest::BeforeRequestComplete(int error) {
648 DCHECK(!job_.get());
649 DCHECK_NE(ERR_IO_PENDING, error);
650 DCHECK_EQ(network_delegate_, context_->network_delegate());
652 // Check that there are no callbacks to already canceled requests.
653 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
655 OnCallToDelegateComplete();
657 if (error != OK) {
658 std::string source("delegate");
659 net_log_.AddEvent(NetLog::TYPE_CANCELLED,
660 NetLog::StringCallback("source", &source));
661 StartJob(new URLRequestErrorJob(this, network_delegate_, error));
662 } else if (!delegate_redirect_url_.is_empty()) {
663 GURL new_url;
664 new_url.Swap(&delegate_redirect_url_);
666 URLRequestRedirectJob* job = new URLRequestRedirectJob(
667 this, network_delegate_, new_url,
668 // Use status code 307 to preserve the method, so POST requests work.
669 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT);
670 StartJob(job);
671 } else {
672 StartJob(URLRequestJobManager::GetInstance()->CreateJob(
673 this, network_delegate_));
677 void URLRequest::StartJob(URLRequestJob* job) {
678 DCHECK(!is_pending_);
679 DCHECK(!job_.get());
681 net_log_.BeginEvent(
682 NetLog::TYPE_URL_REQUEST_START_JOB,
683 base::Bind(&NetLogURLRequestStartCallback,
684 &url(), &method_, load_flags_, priority_,
685 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
687 job_ = job;
688 job_->SetExtraRequestHeaders(extra_request_headers_);
689 job_->SetPriority(priority_);
691 if (upload_data_stream_.get())
692 job_->SetUpload(upload_data_stream_.get());
694 is_pending_ = true;
695 is_redirecting_ = false;
697 response_info_.was_cached = false;
699 // If the referrer is secure, but the requested URL is not, the referrer
700 // policy should be something non-default. If you hit this, please file a
701 // bug.
702 if (referrer_policy_ ==
703 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE &&
704 GURL(referrer_).SchemeIsSecure() && !url().SchemeIsSecure()) {
705 #if !defined(OFFICIAL_BUILD)
706 LOG(FATAL) << "Trying to send secure referrer for insecure load";
707 #endif
708 referrer_.clear();
711 // Don't allow errors to be sent from within Start().
712 // TODO(brettw) this may cause NotifyDone to be sent synchronously,
713 // we probably don't want this: they should be sent asynchronously so
714 // the caller does not get reentered.
715 job_->Start();
718 void URLRequest::Restart() {
719 // Should only be called if the original job didn't make any progress.
720 DCHECK(job_.get() && !job_->has_response_started());
721 RestartWithJob(
722 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
725 void URLRequest::RestartWithJob(URLRequestJob *job) {
726 DCHECK(job->request() == this);
727 PrepareToRestart();
728 StartJob(job);
731 void URLRequest::Cancel() {
732 DoCancel(ERR_ABORTED, SSLInfo());
735 void URLRequest::CancelWithError(int error) {
736 DoCancel(error, SSLInfo());
739 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
740 // This should only be called on a started request.
741 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
742 NOTREACHED();
743 return;
745 DoCancel(error, ssl_info);
748 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
749 DCHECK(error < 0);
750 // If cancelled while calling a delegate, clear delegate info.
751 if (calling_delegate_) {
752 LogUnblocked();
753 OnCallToDelegateComplete();
756 // If the URL request already has an error status, then canceling is a no-op.
757 // Plus, we don't want to change the error status once it has been set.
758 if (status_.is_success()) {
759 status_.set_status(URLRequestStatus::CANCELED);
760 status_.set_error(error);
761 response_info_.ssl_info = ssl_info;
763 // If the request hasn't already been completed, log a cancellation event.
764 if (!has_notified_completion_) {
765 // Don't log an error code on ERR_ABORTED, since that's redundant.
766 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
767 error == ERR_ABORTED ? OK : error);
771 if (is_pending_ && job_.get())
772 job_->Kill();
774 // We need to notify about the end of this job here synchronously. The
775 // Job sends an asynchronous notification but by the time this is processed,
776 // our |context_| is NULL.
777 NotifyRequestCompleted();
779 // The Job will call our NotifyDone method asynchronously. This is done so
780 // that the Delegate implementation can call Cancel without having to worry
781 // about being called recursively.
784 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
785 DCHECK(job_.get());
786 DCHECK(bytes_read);
787 *bytes_read = 0;
789 // If this is the first read, end the delegate call that may have started in
790 // OnResponseStarted.
791 OnCallToDelegateComplete();
793 // This handles a cancel that happens while paused.
794 // TODO(ahendrickson): DCHECK() that it is not done after
795 // http://crbug.com/115705 is fixed.
796 if (job_->is_done())
797 return false;
799 if (dest_size == 0) {
800 // Caller is not too bright. I guess we've done what they asked.
801 return true;
804 // Once the request fails or is cancelled, read will just return 0 bytes
805 // to indicate end of stream.
806 if (!status_.is_success()) {
807 return true;
810 bool rv = job_->Read(dest, dest_size, bytes_read);
811 // If rv is false, the status cannot be success.
812 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
813 if (rv && *bytes_read <= 0 && status_.is_success())
814 NotifyRequestCompleted();
815 return rv;
818 void URLRequest::StopCaching() {
819 DCHECK(job_.get());
820 job_->StopCaching();
823 void URLRequest::NotifyReceivedRedirect(const GURL& location,
824 bool* defer_redirect) {
825 is_redirecting_ = true;
827 URLRequestJob* job =
828 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
829 this, network_delegate_, location);
830 if (job) {
831 RestartWithJob(job);
832 } else if (delegate_) {
833 OnCallToDelegate();
834 delegate_->OnReceivedRedirect(this, location, defer_redirect);
835 // |this| may be have been destroyed here.
839 void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
840 if (delegate_ && !notified_before_network_start_) {
841 OnCallToDelegate();
842 delegate_->OnBeforeNetworkStart(this, defer);
843 if (!*defer)
844 OnCallToDelegateComplete();
845 notified_before_network_start_ = true;
849 void URLRequest::ResumeNetworkStart() {
850 DCHECK(job_);
851 DCHECK(notified_before_network_start_);
853 OnCallToDelegateComplete();
854 job_->ResumeNetworkStart();
857 void URLRequest::NotifyResponseStarted() {
858 int net_error = OK;
859 if (!status_.is_success())
860 net_error = status_.error();
861 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
862 net_error);
864 URLRequestJob* job =
865 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
866 this, network_delegate_);
867 if (job) {
868 RestartWithJob(job);
869 } else {
870 if (delegate_) {
871 // In some cases (e.g. an event was canceled), we might have sent the
872 // completion event and receive a NotifyResponseStarted() later.
873 if (!has_notified_completion_ && status_.is_success()) {
874 if (network_delegate_)
875 network_delegate_->NotifyResponseStarted(this);
878 // Notify in case the entire URL Request has been finished.
879 if (!has_notified_completion_ && !status_.is_success())
880 NotifyRequestCompleted();
882 OnCallToDelegate();
883 delegate_->OnResponseStarted(this);
884 // Nothing may appear below this line as OnResponseStarted may delete
885 // |this|.
890 void URLRequest::FollowDeferredRedirect() {
891 CHECK(job_.get());
892 CHECK(status_.is_success());
894 job_->FollowDeferredRedirect();
897 void URLRequest::SetAuth(const AuthCredentials& credentials) {
898 DCHECK(job_.get());
899 DCHECK(job_->NeedsAuth());
901 job_->SetAuth(credentials);
904 void URLRequest::CancelAuth() {
905 DCHECK(job_.get());
906 DCHECK(job_->NeedsAuth());
908 job_->CancelAuth();
911 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
912 DCHECK(job_.get());
914 job_->ContinueWithCertificate(client_cert);
917 void URLRequest::ContinueDespiteLastError() {
918 DCHECK(job_.get());
920 job_->ContinueDespiteLastError();
923 void URLRequest::PrepareToRestart() {
924 DCHECK(job_.get());
926 // Close the current URL_REQUEST_START_JOB, since we will be starting a new
927 // one.
928 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
930 OrphanJob();
932 response_info_ = HttpResponseInfo();
933 response_info_.request_time = base::Time::Now();
935 load_timing_info_ = LoadTimingInfo();
936 load_timing_info_.request_start_time = response_info_.request_time;
937 load_timing_info_.request_start = base::TimeTicks::Now();
939 status_ = URLRequestStatus();
940 is_pending_ = false;
943 void URLRequest::OrphanJob() {
944 // When calling this function, please check that URLRequestHttpJob is
945 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
946 // the call back. This is currently guaranteed by the following strategies:
947 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
948 // be receiving any headers at that time.
949 // - OrphanJob is called in ~URLRequest, in this case
950 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
951 // that the callback becomes invalid.
952 job_->Kill();
953 job_->DetachRequest(); // ensures that the job will not call us again
954 job_ = NULL;
957 int URLRequest::Redirect(const GURL& location, int http_status_code) {
958 // Matches call in NotifyReceivedRedirect.
959 OnCallToDelegateComplete();
960 if (net_log_.IsLoggingAllEvents()) {
961 net_log_.AddEvent(
962 NetLog::TYPE_URL_REQUEST_REDIRECTED,
963 NetLog::StringCallback("location", &location.possibly_invalid_spec()));
966 if (network_delegate_)
967 network_delegate_->NotifyBeforeRedirect(this, location);
969 if (redirect_limit_ <= 0) {
970 DVLOG(1) << "disallowing redirect: exceeds limit";
971 return ERR_TOO_MANY_REDIRECTS;
974 if (!location.is_valid())
975 return ERR_INVALID_URL;
977 if (!job_->IsSafeRedirect(location)) {
978 DVLOG(1) << "disallowing redirect: unsafe protocol";
979 return ERR_UNSAFE_REDIRECT;
982 if (!final_upload_progress_.position())
983 final_upload_progress_ = job_->GetUploadProgress();
984 PrepareToRestart();
986 std::string new_method(ComputeMethodForRedirect(method_, http_status_code));
987 if (new_method != method_) {
988 if (method_ == "POST") {
989 // If being switched from POST, must remove headers that were specific to
990 // the POST and don't have meaning in other methods. For example the
991 // inclusion of a multipart Content-Type header in GET can cause problems
992 // with some servers:
993 // http://code.google.com/p/chromium/issues/detail?id=843
994 StripPostSpecificHeaders(&extra_request_headers_);
996 upload_data_stream_.reset();
997 method_.swap(new_method);
1000 // Suppress the referrer if we're redirecting out of https.
1001 if (referrer_policy_ ==
1002 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE &&
1003 GURL(referrer_).SchemeIsSecure() && !location.SchemeIsSecure()) {
1004 referrer_.clear();
1007 url_chain_.push_back(location);
1008 --redirect_limit_;
1010 Start();
1011 return OK;
1014 const URLRequestContext* URLRequest::context() const {
1015 return context_;
1018 int64 URLRequest::GetExpectedContentSize() const {
1019 int64 expected_content_size = -1;
1020 if (job_.get())
1021 expected_content_size = job_->expected_content_size();
1023 return expected_content_size;
1026 void URLRequest::SetPriority(RequestPriority priority) {
1027 DCHECK_GE(priority, MINIMUM_PRIORITY);
1028 DCHECK_LE(priority, MAXIMUM_PRIORITY);
1030 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) {
1031 NOTREACHED();
1032 // Maintain the invariant that requests with IGNORE_LIMITS set
1033 // have MAXIMUM_PRIORITY for release mode.
1034 return;
1037 if (priority_ == priority)
1038 return;
1040 priority_ = priority;
1041 if (job_.get()) {
1042 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY,
1043 NetLog::IntegerCallback("priority", priority_));
1044 job_->SetPriority(priority_);
1048 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
1049 const GURL& url = this->url();
1050 if (!url.SchemeIs("http"))
1051 return false;
1052 TransportSecurityState::DomainState domain_state;
1053 if (context()->transport_security_state() &&
1054 context()->transport_security_state()->GetDomainState(
1055 url.host(),
1056 SSLConfigService::IsSNIAvailable(context()->ssl_config_service()),
1057 &domain_state) &&
1058 domain_state.ShouldUpgradeToSSL()) {
1059 url_canon::Replacements<char> replacements;
1060 const char kNewScheme[] = "https";
1061 replacements.SetScheme(kNewScheme,
1062 url_parse::Component(0, strlen(kNewScheme)));
1063 *redirect_url = url.ReplaceComponents(replacements);
1064 return true;
1066 return false;
1069 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) {
1070 NetworkDelegate::AuthRequiredResponse rv =
1071 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
1072 auth_info_ = auth_info;
1073 if (network_delegate_) {
1074 OnCallToDelegate();
1075 rv = network_delegate_->NotifyAuthRequired(
1076 this,
1077 *auth_info,
1078 base::Bind(&URLRequest::NotifyAuthRequiredComplete,
1079 base::Unretained(this)),
1080 &auth_credentials_);
1081 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING)
1082 return;
1085 NotifyAuthRequiredComplete(rv);
1088 void URLRequest::NotifyAuthRequiredComplete(
1089 NetworkDelegate::AuthRequiredResponse result) {
1090 OnCallToDelegateComplete();
1092 // Check that there are no callbacks to already canceled requests.
1093 DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
1095 // NotifyAuthRequired may be called multiple times, such as
1096 // when an authentication attempt fails. Clear out the data
1097 // so it can be reset on another round.
1098 AuthCredentials credentials = auth_credentials_;
1099 auth_credentials_ = AuthCredentials();
1100 scoped_refptr<AuthChallengeInfo> auth_info;
1101 auth_info.swap(auth_info_);
1103 switch (result) {
1104 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
1105 // Defer to the URLRequest::Delegate, since the NetworkDelegate
1106 // didn't take an action.
1107 if (delegate_)
1108 delegate_->OnAuthRequired(this, auth_info.get());
1109 break;
1111 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
1112 SetAuth(credentials);
1113 break;
1115 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
1116 CancelAuth();
1117 break;
1119 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
1120 NOTREACHED();
1121 break;
1125 void URLRequest::NotifyCertificateRequested(
1126 SSLCertRequestInfo* cert_request_info) {
1127 if (delegate_)
1128 delegate_->OnCertificateRequested(this, cert_request_info);
1131 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info,
1132 bool fatal) {
1133 if (delegate_)
1134 delegate_->OnSSLCertificateError(this, ssl_info, fatal);
1137 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const {
1138 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES));
1139 if (network_delegate_) {
1140 return network_delegate_->CanGetCookies(*this, cookie_list);
1142 return g_default_can_use_cookies;
1145 bool URLRequest::CanSetCookie(const std::string& cookie_line,
1146 CookieOptions* options) const {
1147 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES));
1148 if (network_delegate_) {
1149 return network_delegate_->CanSetCookie(*this, cookie_line, options);
1151 return g_default_can_use_cookies;
1154 bool URLRequest::CanEnablePrivacyMode() const {
1155 if (network_delegate_) {
1156 return network_delegate_->CanEnablePrivacyMode(url(),
1157 first_party_for_cookies_);
1159 return !g_default_can_use_cookies;
1163 void URLRequest::NotifyReadCompleted(int bytes_read) {
1164 // Notify in case the entire URL Request has been finished.
1165 if (bytes_read <= 0)
1166 NotifyRequestCompleted();
1168 // Notify NetworkChangeNotifier that we just received network data.
1169 // This is to identify cases where the NetworkChangeNotifier thinks we
1170 // are off-line but we are still receiving network data (crbug.com/124069),
1171 // and to get rough network connection measurements.
1172 if (bytes_read > 0 && !was_cached())
1173 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1175 if (delegate_)
1176 delegate_->OnReadCompleted(this, bytes_read);
1178 // Nothing below this line as OnReadCompleted may delete |this|.
1181 void URLRequest::OnHeadersComplete() {
1182 // Cache load timing information now, as information will be lost once the
1183 // socket is closed and the ClientSocketHandle is Reset, which will happen
1184 // once the body is complete. The start times should already be populated.
1185 if (job_.get()) {
1186 // Keep a copy of the two times the URLRequest sets.
1187 base::TimeTicks request_start = load_timing_info_.request_start;
1188 base::Time request_start_time = load_timing_info_.request_start_time;
1190 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a
1191 // consistent place to start from.
1192 load_timing_info_ = LoadTimingInfo();
1193 job_->GetLoadTimingInfo(&load_timing_info_);
1195 load_timing_info_.request_start = request_start;
1196 load_timing_info_.request_start_time = request_start_time;
1198 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_);
1202 void URLRequest::NotifyRequestCompleted() {
1203 // TODO(battre): Get rid of this check, according to willchan it should
1204 // not be needed.
1205 if (has_notified_completion_)
1206 return;
1208 is_pending_ = false;
1209 is_redirecting_ = false;
1210 has_notified_completion_ = true;
1211 if (network_delegate_)
1212 network_delegate_->NotifyCompleted(this, job_.get() != NULL);
1215 void URLRequest::OnCallToDelegate() {
1216 DCHECK(!calling_delegate_);
1217 DCHECK(blocked_by_.empty());
1218 calling_delegate_ = true;
1219 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1222 void URLRequest::OnCallToDelegateComplete() {
1223 // This should have been cleared before resuming the request.
1224 DCHECK(blocked_by_.empty());
1225 if (!calling_delegate_)
1226 return;
1227 calling_delegate_ = false;
1228 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1231 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) {
1232 base::debug::StackTrace* stack_trace_copy =
1233 new base::debug::StackTrace(NULL, 0);
1234 *stack_trace_copy = stack_trace;
1235 stack_trace_.reset(stack_trace_copy);
1238 const base::debug::StackTrace* URLRequest::stack_trace() const {
1239 return stack_trace_.get();
1242 } // namespace net