15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/debug/stack_trace.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/lazy_instance.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/singleton.h"
149ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/stats_counters.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stl_util.h"
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/synchronization/lock.h"
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/values.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/auth.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/host_port_pair.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/load_flags.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/load_timing_info.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/network_change_notifier.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/network_delegate.h"
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/upload_data_stream.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_response_headers.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_util.h"
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/ssl/ssl_cert_request_info.h"
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "net/url_request/redirect_info.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_context.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_error_job.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job_manager.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_netlog_params.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_redirect_job.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::Time;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using std::string;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Max number of http redirects to follow.  Same number as gecko.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int kMaxRedirects = 20;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Discard headers which have meaning in POST (Content-Length, Content-Type,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Origin).
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void StripPostSpecificHeaders(HttpRequestHeaders* headers) {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These are headers that may be attached to a POST.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  headers->RemoveHeader(HttpRequestHeaders::kContentLength);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  headers->RemoveHeader(HttpRequestHeaders::kContentType);
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  headers->RemoveHeader(HttpRequestHeaders::kOrigin);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(battre): Delete this, see http://crbug.com/89321:
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This counter keeps track of the identifiers used for URL requests so far.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 0 is reserved to represent an invalid ID.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uint64 g_next_url_request_identifier = 1;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This lock protects g_next_url_request_identifier.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::LazyInstance<base::Lock>::Leaky
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    g_next_url_request_identifier_lock = LAZY_INSTANCE_INITIALIZER;
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns an prior unused identifier for URL requests.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uint64 GenerateURLRequestIdentifier() {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::AutoLock lock(g_next_url_request_identifier_lock.Get());
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return g_next_url_request_identifier++;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// True once the first URLRequest was started.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool g_url_requests_started = false;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// True if cookies are accepted by default.
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool g_default_can_use_cookies = true;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// When the URLRequest first assempts load timing information, it has the times
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// at which each event occurred.  The API requires the time which the request
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// was blocked on each phase.  This function handles the conversion.
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// In the case of reusing a SPDY session, old proxy results may have been
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// reused, so proxy resolution times may be before the request was started.
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Due to preconnect and late binding, it is also possible for the connection
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// attempt to start before a request has been started, or proxy resolution
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// completed.
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// This functions fixes both those cases.
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void ConvertRealLoadTimesToBlockingTimes(
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    net::LoadTimingInfo* load_timing_info) {
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(!load_timing_info->request_start.is_null());
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Earliest time possible for the request to be blocking on connect events.
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::TimeTicks block_on_connect = load_timing_info->request_start;
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!load_timing_info->proxy_resolve_start.is_null()) {
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!load_timing_info->proxy_resolve_end.is_null());
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Make sure the proxy times are after request start.
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (load_timing_info->proxy_resolve_start < load_timing_info->request_start)
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      load_timing_info->proxy_resolve_start = load_timing_info->request_start;
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (load_timing_info->proxy_resolve_end < load_timing_info->request_start)
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      load_timing_info->proxy_resolve_end = load_timing_info->request_start;
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Connect times must also be after the proxy times.
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    block_on_connect = load_timing_info->proxy_resolve_end;
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Make sure connection times are after start and proxy times.
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  net::LoadTimingInfo::ConnectTiming* connect_timing =
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      &load_timing_info->connect_timing;
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!connect_timing->dns_start.is_null()) {
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!connect_timing->dns_end.is_null());
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (connect_timing->dns_start < block_on_connect)
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      connect_timing->dns_start = block_on_connect;
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (connect_timing->dns_end < block_on_connect)
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      connect_timing->dns_end = block_on_connect;
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!connect_timing->connect_start.is_null()) {
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!connect_timing->connect_end.is_null());
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (connect_timing->connect_start < block_on_connect)
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      connect_timing->connect_start = block_on_connect;
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (connect_timing->connect_end < block_on_connect)
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      connect_timing->connect_end = block_on_connect;
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!connect_timing->ssl_start.is_null()) {
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    DCHECK(!connect_timing->ssl_end.is_null());
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (connect_timing->ssl_start < block_on_connect)
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      connect_timing->ssl_start = block_on_connect;
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (connect_timing->ssl_end < block_on_connect)
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      connect_timing->ssl_end = block_on_connect;
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Deprecated::RegisterRequestInterceptor(
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Interceptor* interceptor) {
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLRequest::RegisterRequestInterceptor(interceptor);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Deprecated::UnregisterRequestInterceptor(
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Interceptor* interceptor) {
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLRequest::UnregisterRequestInterceptor(interceptor);
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// URLRequest::Interceptor
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJob* URLRequest::Interceptor::MaybeInterceptRedirect(
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequest* request,
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NetworkDelegate* network_delegate,
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& location) {
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestJob* URLRequest::Interceptor::MaybeInterceptResponse(
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequest* request, NetworkDelegate* network_delegate) {
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// URLRequest::Delegate
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request,
1726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                              const RedirectInfo& redirect_info,
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                              bool* defer_redirect) {
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Delegate::OnAuthRequired(URLRequest* request,
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          AuthChallengeInfo* auth_info) {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  request->CancelAuth();
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Delegate::OnCertificateRequested(
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequest* request,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SSLCertRequestInfo* cert_request_info) {
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  request->Cancel();
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 const SSLInfo& ssl_info,
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 bool is_hsts_ok) {
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  request->Cancel();
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request,
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                                bool* defer) {
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// URLRequest
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequest::~URLRequest() {
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Cancel();
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (network_delegate_) {
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    network_delegate_->NotifyURLRequestDestroyed(this);
205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (job_.get())
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      job_->NotifyURLRequestDestroyed();
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
209868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (job_.get())
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    OrphanJob();
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int deleted = context_->url_requests()->erase(this);
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK_EQ(1, deleted);
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int net_error = OK;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Log error only on failure, not cancellation, as even successful requests
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are "cancelled" on destruction.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (status_.status() == URLRequestStatus::FAILED)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net_error = status_.error();
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::EnableChunkedUpload() {
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked());
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!upload_data_stream_) {
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    upload_data_stream_.reset(
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        new UploadDataStream(UploadDataStream::CHUNKED, 0));
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::AppendChunkToUpload(const char* bytes,
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     int bytes_len,
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     bool is_last_chunk) {
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(upload_data_stream_);
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(upload_data_stream_->is_chunked());
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_GT(bytes_len, 0);
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  upload_data_stream_->AppendChunk(bytes, bytes_len, is_last_chunk);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) {
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!upload->is_chunked());
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  upload_data_stream_ = upload.Pass();
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const UploadDataStream* URLRequest::get_upload() const {
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return upload_data_stream_.get();
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequest::has_upload() const {
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return upload_data_stream_.get() != NULL;
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::SetExtraRequestHeaderById(int id, const string& value,
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           bool overwrite) {
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!is_pending_ || is_redirecting_);
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTREACHED() << "implement me!";
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::SetExtraRequestHeaderByName(const string& name,
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             const string& value,
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             bool overwrite) {
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!is_pending_ || is_redirecting_);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (overwrite) {
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extra_request_headers_.SetHeader(name, value);
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extra_request_headers_.SetHeaderIfMissing(name, value);
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void URLRequest::RemoveRequestHeaderByName(const string& name) {
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(!is_pending_ || is_redirecting_);
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  extra_request_headers_.RemoveHeader(name);
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::SetExtraRequestHeaders(
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const HttpRequestHeaders& headers) {
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!is_pending_);
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extra_request_headers_ = headers;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NOTE: This method will likely become non-trivial once the other setters
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for request headers are implemented.
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
284eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
285eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!job_.get())
286eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return false;
287eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
288eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return job_->GetFullRequestHeaders(headers);
289eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
290eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int64 URLRequest::GetTotalReceivedBytes() const {
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!job_.get())
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return 0;
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return job_->GetTotalReceivedBytes();
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LoadStateWithParam URLRequest::GetLoadState() const {
299a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The !blocked_by_.empty() check allows |this| to report it's blocked on a
300a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // delegate before it has been started.
301a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (calling_delegate_ || !blocked_by_.empty()) {
3021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return LoadStateWithParam(
3031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        LOAD_STATE_WAITING_FOR_DELEGATE,
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) :
305a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                        base::string16());
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
307868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE,
308c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                            base::string16());
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)base::Value* URLRequest::GetStateAsValue() const {
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::DictionaryValue* dict = new base::DictionaryValue();
3131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dict->SetString("url", original_url().possibly_invalid_spec());
3141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (url_chain_.size() > 1) {
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue* list = new base::ListValue();
3171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    for (std::vector<GURL>::const_iterator url = url_chain_.begin();
3181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)         url != url_chain_.end(); ++url) {
3191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      list->AppendString(url->possibly_invalid_spec());
3201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
3211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    dict->Set("url_chain", list);
3221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
3231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dict->SetInteger("load_flags", load_flags_);
3251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  LoadStateWithParam load_state = GetLoadState();
3271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dict->SetInteger("load_state", load_state.state);
3281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!load_state.param.empty())
3291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    dict->SetString("load_state_param", load_state.param);
330a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!blocked_by_.empty())
331a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    dict->SetString("delegate_info", blocked_by_);
3321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dict->SetString("method", method_);
3341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dict->SetBoolean("has_upload", has_upload());
3351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dict->SetBoolean("is_pending", is_pending_);
3361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Add the status of the request.  The status should always be IO_PENDING, and
3381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // the error should always be OK, unless something is holding onto a request
3391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // that has finished or a request was leaked.  Neither of these should happen.
3401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (status_.status()) {
3411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case URLRequestStatus::SUCCESS:
3421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dict->SetString("status", "SUCCESS");
3431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
3441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case URLRequestStatus::IO_PENDING:
3451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dict->SetString("status", "IO_PENDING");
3461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
3471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case URLRequestStatus::CANCELED:
3481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dict->SetString("status", "CANCELED");
3491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
3501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case URLRequestStatus::FAILED:
3511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dict->SetString("status", "FAILED");
3521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
3531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
3541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (status_.error() != OK)
3551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    dict->SetInteger("net_error", status_.error());
3561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return dict;
3571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
359a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void URLRequest::LogBlockedBy(const char* blocked_by) {
360a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK(blocked_by);
361a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK_GT(strlen(blocked_by), 0u);
362a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
363a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Only log information to NetLog during startup and certain deferring calls
364a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // to delegates.  For all reads but the first, do nothing.
3651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!calling_delegate_ && !response_info_.request_time.is_null())
3661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
3671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
368a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  LogUnblocked();
369a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  blocked_by_ = blocked_by;
370a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  use_blocked_by_as_load_param_ = false;
371a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
372a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  net_log_.BeginEvent(
373a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NetLog::TYPE_DELEGATE_INFO,
374a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      NetLog::StringCallback("delegate_info", &blocked_by_));
375a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
376a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
377a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void URLRequest::LogAndReportBlockedBy(const char* source) {
378a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  LogBlockedBy(source);
379a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  use_blocked_by_as_load_param_ = true;
380a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
381a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
382a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void URLRequest::LogUnblocked() {
383a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (blocked_by_.empty())
384a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return;
385a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
386a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO);
387a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  blocked_by_.clear();
3881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)UploadProgress URLRequest::GetUploadProgress() const {
391868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!job_.get()) {
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We haven't started or the request was cancelled
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return UploadProgress();
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (final_upload_progress_.position()) {
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The first job completed and none of the subsequent series of
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // GETs when following redirects will upload anything, so we return the
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // cached results from the initial job, the POST.
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return final_upload_progress_;
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return job_->GetUploadProgress();
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::GetResponseHeaderById(int id, string* value) {
405868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTREACHED() << "implement me!";
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::GetResponseHeaderByName(const string& name, string* value) {
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(value);
411868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (response_info_.headers.get()) {
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    response_info_.headers->GetNormalizedHeader(name, value);
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    value->clear();
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::GetAllResponseHeaders(string* headers) {
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(headers);
420868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (response_info_.headers.get()) {
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    response_info_.headers->GetNormalizedHeaders(headers);
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    headers->clear();
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HostPortPair URLRequest::GetSocketAddress() const {
428868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return job_->GetSocketAddress();
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HttpResponseHeaders* URLRequest::response_headers() const {
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return response_info_.headers.get();
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
4372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *load_timing_info = load_timing_info_;
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequest::GetResponseCookies(ResponseCookies* cookies) {
441868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return job_->GetResponseCookies(cookies);
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid URLRequest::GetMimeType(string* mime_type) const {
446868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->GetMimeType(mime_type);
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
450116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid URLRequest::GetCharset(string* charset) const {
451868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->GetCharset(charset);
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)int URLRequest::GetResponseCode() const {
456868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return job_->GetResponseCode();
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
460f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void URLRequest::SetLoadFlags(int flags) {
461f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) {
4621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    DCHECK(!job_.get());
463f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DCHECK(flags & LOAD_IGNORE_LIMITS);
464f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DCHECK_EQ(priority_, MAXIMUM_PRIORITY);
465f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
466f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  load_flags_ = flags;
467f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
468f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // This should be a no-op given the above DCHECKs, but do this
469f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // anyway for release mode.
470f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0)
471f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    SetPriority(MAXIMUM_PRIORITY);
472f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
473f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::SetDefaultCookiePolicyToBlock() {
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(!g_url_requests_started);
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_default_can_use_cookies = false;
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequest::IsHandledProtocol(const std::string& scheme) {
482f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return URLRequestJobManager::SupportsScheme(scheme);
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequest::IsHandledURL(const GURL& url) {
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!url.is_valid()) {
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // We handle error cases.
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return IsHandledProtocol(url.scheme());
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::set_first_party_for_cookies(
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const GURL& first_party_for_cookies) {
4976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DCHECK(!is_pending_);
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  first_party_for_cookies_ = first_party_for_cookies;
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void URLRequest::set_first_party_url_policy(
5026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    FirstPartyURLPolicy first_party_url_policy) {
5036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DCHECK(!is_pending_);
5046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  first_party_url_policy_ = first_party_url_policy;
5056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
5066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::set_method(const std::string& method) {
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!is_pending_);
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  method_ = method;
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5129ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch// static
5139ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdochstd::string URLRequest::ComputeMethodForRedirect(
5149ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch    const std::string& method,
5159ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch    int http_status_code) {
5169ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // For 303 redirects, all request methods except HEAD are converted to GET,
5179ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // as per the latest httpbis draft.  The draft also allows POST requests to
5189ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // be converted to GETs when following 301/302 redirects, for historical
5199ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // reasons. Most major browsers do this and so shall we.  Both RFC 2616 and
5209ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // the httpbis draft say to prompt the user to confirm the generation of new
5219ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // requests, other than GET and HEAD requests, but IE omits these prompts and
5229ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // so shall we.
5239ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  // See:  https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
5249ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  if ((http_status_code == 303 && method != "HEAD") ||
5259ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch      ((http_status_code == 301 || http_status_code == 302) &&
5269ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch       method == "POST")) {
5279ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch    return "GET";
5289ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  }
5299ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  return method;
5309ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch}
5319ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch
532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void URLRequest::SetReferrer(const std::string& referrer) {
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!is_pending_);
534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GURL referrer_url(referrer);
53523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (referrer_url.is_valid()) {
53623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    referrer_ = referrer_url.GetAsReferrer().spec();
53723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  } else {
53823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    referrer_ = referrer;
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) {
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!is_pending_);
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  referrer_policy_ = referrer_policy;
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::set_delegate(Delegate* delegate) {
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  delegate_ = delegate;
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Start() {
552f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Some values can be NULL, but the job factory must not be.
553f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DCHECK(context_->job_factory());
554f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
555a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Anything that sets |blocked_by_| before start should have cleaned up after
556a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // itself.
557a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  DCHECK(blocked_by_.empty());
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  g_url_requests_started = true;
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  response_info_.request_time = base::Time::Now();
5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  load_timing_info_ = LoadTimingInfo();
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  load_timing_info_.request_start_time = response_info_.request_time;
5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  load_timing_info_.request_start = base::TimeTicks::Now();
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Only notify the delegate for the initial request.
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (network_delegate_) {
5681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    OnCallToDelegate();
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int error = network_delegate_->NotifyBeforeURLRequest(
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        this, before_request_callback_, &delegate_redirect_url_);
5711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // If ERR_IO_PENDING is returned, the delegate will invoke
5721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // |before_request_callback_| later.
5731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (error != ERR_IO_PENDING)
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BeforeRequestComplete(error);
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartJob(URLRequestJobManager::GetInstance()->CreateJob(
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this, network_delegate_));
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)///////////////////////////////////////////////////////////////////////////////
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciURLRequest::URLRequest(const GURL& url,
5851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                       RequestPriority priority,
5861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                       Delegate* delegate,
5871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                       const URLRequestContext* context,
5881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                       CookieStore* cookie_store,
5891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                       NetworkDelegate* network_delegate)
5901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : context_(context),
5911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      network_delegate_(network_delegate ? network_delegate
5921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         : context->network_delegate()),
5931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      net_log_(BoundNetLog::Make(context->net_log(),
5941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                 NetLog::SOURCE_URL_REQUEST)),
5951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      url_chain_(1, url),
5961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      method_("GET"),
5971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
5981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
5991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      load_flags_(LOAD_NORMAL),
6001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      delegate_(delegate),
6011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      is_pending_(false),
6021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      is_redirecting_(false),
6031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      redirect_limit_(kMaxRedirects),
6041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      priority_(priority),
6051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      identifier_(GenerateURLRequestIdentifier()),
6061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      calling_delegate_(false),
6071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      use_blocked_by_as_load_param_(false),
6081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
6091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                          base::Unretained(this))),
6101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      has_notified_completion_(false),
6111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      received_response_content_length_(0),
6121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      creation_time_(base::TimeTicks::Now()),
6131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      notified_before_network_start_(false),
6141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      cookie_store_(cookie_store ? cookie_store : context->cookie_store()) {
6151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SIMPLE_STATS_COUNTER("URLRequestCount");
6161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Sanity check out environment.
6181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(base::MessageLoop::current())
6191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      << "The current base::MessageLoop must exist";
6201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  context->url_requests()->insert(this);
6221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE);
6231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
6241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// static
6261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) {
6271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor);
6281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
6291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// static
6311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) {
6321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor(
6331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      interceptor);
6341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
6351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::BeforeRequestComplete(int error) {
637868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(!job_.get());
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_NE(ERR_IO_PENDING, error);
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check that there are no callbacks to already canceled requests.
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_NE(URLRequestStatus::CANCELED, status_.status());
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  OnCallToDelegateComplete();
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (error != OK) {
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string source("delegate");
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net_log_.AddEvent(NetLog::TYPE_CANCELLED,
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      NetLog::StringCallback("source", &source));
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StartJob(new URLRequestErrorJob(this, network_delegate_, error));
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (!delegate_redirect_url_.is_empty()) {
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL new_url;
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    new_url.Swap(&delegate_redirect_url_);
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    URLRequestRedirectJob* job = new URLRequestRedirectJob(
6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        this, network_delegate_, new_url,
6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // Use status code 307 to preserve the method, so POST requests work.
657a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate");
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StartJob(job);
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StartJob(URLRequestJobManager::GetInstance()->CreateJob(
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        this, network_delegate_));
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::StartJob(URLRequestJob* job) {
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!is_pending_);
667868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(!job_.get());
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net_log_.BeginEvent(
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NetLog::TYPE_URL_REQUEST_START_JOB,
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&NetLogURLRequestStartCallback,
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 &url(), &method_, load_flags_, priority_,
6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 upload_data_stream_ ? upload_data_stream_->identifier() : -1));
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_ = job;
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->SetExtraRequestHeaders(extra_request_headers_);
6772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  job_->SetPriority(priority_);
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (upload_data_stream_.get())
6802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    job_->SetUpload(upload_data_stream_.get());
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  is_pending_ = true;
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  is_redirecting_ = false;
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  response_info_.was_cached = false;
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If the referrer is secure, but the requested URL is not, the referrer
6885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // policy should be something non-default. If you hit this, please file a
6895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // bug.
6905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (referrer_policy_ ==
6915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE &&
6925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GURL(referrer_).SchemeIsSecure() && !url().SchemeIsSecure()) {
6931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (!network_delegate_ ||
6941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader(
6951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci            *this, url(), GURL(referrer_))) {
6961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      referrer_.clear();
6971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    } else {
6981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // We need to clear the referrer anyway to avoid an infinite recursion
6991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // when starting the error job.
7001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      referrer_.clear();
7011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      std::string source("delegate");
7021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      net_log_.AddEvent(NetLog::TYPE_CANCELLED,
7031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                        NetLog::StringCallback("source", &source));
7041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      RestartWithJob(new URLRequestErrorJob(
7051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          this, network_delegate_, ERR_BLOCKED_BY_CLIENT));
7061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return;
7071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
7085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
7095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't allow errors to be sent from within Start().
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(brettw) this may cause NotifyDone to be sent synchronously,
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we probably don't want this: they should be sent asynchronously so
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the caller does not get reentered.
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->Start();
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Restart() {
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should only be called if the original job didn't make any progress.
719868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get() && !job_->has_response_started());
720868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  RestartWithJob(
721868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::RestartWithJob(URLRequestJob *job) {
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(job->request() == this);
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrepareToRestart();
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StartJob(job);
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::Cancel() {
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DoCancel(ERR_ABORTED, SSLInfo());
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::CancelWithError(int error) {
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DoCancel(error, SSLInfo());
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should only be called on a started request.
740868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!is_pending_ || !job_.get() || job_->has_response_started()) {
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DoCancel(error, ssl_info);
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(error < 0);
7491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // If cancelled while calling a delegate, clear delegate info.
7501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (calling_delegate_) {
751a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    LogUnblocked();
7521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    OnCallToDelegateComplete();
7531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the URL request already has an error status, then canceling is a no-op.
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Plus, we don't want to change the error status once it has been set.
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (status_.is_success()) {
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_.set_status(URLRequestStatus::CANCELED);
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    status_.set_error(error);
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    response_info_.ssl_info = ssl_info;
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // If the request hasn't already been completed, log a cancellation event.
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!has_notified_completion_) {
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Don't log an error code on ERR_ABORTED, since that's redundant.
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED,
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        error == ERR_ABORTED ? OK : error);
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
770868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (is_pending_ && job_.get())
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    job_->Kill();
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We need to notify about the end of this job here synchronously. The
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Job sends an asynchronous notification but by the time this is processed,
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // our |context_| is NULL.
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotifyRequestCompleted();
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The Job will call our NotifyDone method asynchronously.  This is done so
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that the Delegate implementation can call Cancel without having to worry
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // about being called recursively.
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
784868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(bytes_read);
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *bytes_read = 0;
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // If this is the first read, end the delegate call that may have started in
7891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // OnResponseStarted.
7901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  OnCallToDelegateComplete();
7911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This handles a cancel that happens while paused.
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(ahendrickson): DCHECK() that it is not done after
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://crbug.com/115705 is fixed.
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (job_->is_done())
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (dest_size == 0) {
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Caller is not too bright.  I guess we've done what they asked.
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Once the request fails or is cancelled, read will just return 0 bytes
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to indicate end of stream.
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!status_.is_success()) {
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool rv = job_->Read(dest, dest_size, bytes_read);
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If rv is false, the status cannot be success.
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS);
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (rv && *bytes_read <= 0 && status_.is_success())
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NotifyRequestCompleted();
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return rv;
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::StopCaching() {
818868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->StopCaching();
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info,
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                        bool* defer_redirect) {
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  is_redirecting_ = true;
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect?
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLRequestJob* job =
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      URLRequestJobManager::GetInstance()->MaybeInterceptRedirect(
8296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          this, network_delegate_, redirect_info.new_url);
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (job) {
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RestartWithJob(job);
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else if (delegate_) {
8331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    OnCallToDelegate();
8346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect);
8352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // |this| may be have been destroyed here.
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void URLRequest::NotifyBeforeNetworkStart(bool* defer) {
8405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (delegate_ && !notified_before_network_start_) {
8415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OnCallToDelegate();
8425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    delegate_->OnBeforeNetworkStart(this, defer);
8435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!*defer)
8445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      OnCallToDelegateComplete();
8455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    notified_before_network_start_ = true;
8465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
8475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
8485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void URLRequest::ResumeNetworkStart() {
8501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(job_.get());
8515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(notified_before_network_start_);
8525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  OnCallToDelegateComplete();
8545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  job_->ResumeNetworkStart();
8555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
8565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::NotifyResponseStarted() {
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int net_error = OK;
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!status_.is_success())
8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net_error = status_.error();
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB,
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    net_error);
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  URLRequestJob* job =
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          this, network_delegate_);
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (job) {
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RestartWithJob(job);
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (delegate_) {
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // In some cases (e.g. an event was canceled), we might have sent the
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // completion event and receive a NotifyResponseStarted() later.
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!has_notified_completion_ && status_.is_success()) {
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (network_delegate_)
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          network_delegate_->NotifyResponseStarted(this);
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Notify in case the entire URL Request has been finished.
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!has_notified_completion_ && !status_.is_success())
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NotifyRequestCompleted();
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      OnCallToDelegate();
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delegate_->OnResponseStarted(this);
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Nothing may appear below this line as OnResponseStarted may delete
8855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // |this|.
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::FollowDeferredRedirect() {
891868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CHECK(job_.get());
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(status_.is_success());
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->FollowDeferredRedirect();
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::SetAuth(const AuthCredentials& credentials) {
898868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(job_->NeedsAuth());
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->SetAuth(credentials);
9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::CancelAuth() {
905868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(job_->NeedsAuth());
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->CancelAuth();
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) {
912868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->ContinueWithCertificate(client_cert);
9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::ContinueDespiteLastError() {
918868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
9195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->ContinueDespiteLastError();
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::PrepareToRestart() {
924868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(job_.get());
9255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Close the current URL_REQUEST_START_JOB, since we will be starting a new
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // one.
9285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
9295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  OrphanJob();
9315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  response_info_ = HttpResponseInfo();
9332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  response_info_.request_time = base::Time::Now();
9342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  load_timing_info_ = LoadTimingInfo();
9362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  load_timing_info_.request_start_time = response_info_.request_time;
9372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  load_timing_info_.request_start = base::TimeTicks::Now();
9382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  status_ = URLRequestStatus();
9405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  is_pending_ = false;
9415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequest::OrphanJob() {
9445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When calling this function, please check that URLRequestHttpJob is
9455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not in between calling NetworkDelegate::NotifyHeadersReceived receiving
9465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the call back. This is currently guaranteed by the following strategies:
9475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot
9485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   be receiving any headers at that time.
9495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - OrphanJob is called in ~URLRequest, in this case
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate
9515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   that the callback becomes invalid.
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->Kill();
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_->DetachRequest();  // ensures that the job will not call us again
9545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job_ = NULL;
9555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)int URLRequest::Redirect(const RedirectInfo& redirect_info) {
9581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Matches call in NotifyReceivedRedirect.
9591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  OnCallToDelegateComplete();
960a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (net_log_.IsLogging()) {
9615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net_log_.AddEvent(
9625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NetLog::TYPE_URL_REQUEST_REDIRECTED,
9636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        NetLog::StringCallback("location",
9646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                               &redirect_info.new_url.possibly_invalid_spec()));
9655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate.
9685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (network_delegate_)
9696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url);
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (redirect_limit_ <= 0) {
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DVLOG(1) << "disallowing redirect: exceeds limit";
9735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ERR_TOO_MANY_REDIRECTS;
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!redirect_info.new_url.is_valid())
9775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ERR_INVALID_URL;
9785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!job_->IsSafeRedirect(redirect_info.new_url)) {
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DVLOG(1) << "disallowing redirect: unsafe protocol";
9815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ERR_UNSAFE_REDIRECT;
9825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
9835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!final_upload_progress_.position())
9852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    final_upload_progress_ = job_->GetUploadProgress();
9862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PrepareToRestart();
9872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (redirect_info.new_method != method_) {
9896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // TODO(davidben): This logic still needs to be replicated at the consumers.
9909ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch    if (method_ == "POST") {
9919ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch      // If being switched from POST, must remove headers that were specific to
9929ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch      // the POST and don't have meaning in other methods. For example the
9939ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch      // inclusion of a multipart Content-Type header in GET can cause problems
9949ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch      // with some servers:
995