[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 5 | #include "net/url_request/url_request_http_job.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 6 | |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 7 | #include "base/base_switches.h" |
[email protected] | 4f9e5c8 | 2011-11-17 16:04:56 | [diff] [blame] | 8 | #include "base/bind.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 9 | #include "base/bind_helpers.h" |
[email protected] | 4ed2755f | 2008-12-15 09:01:33 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 39ce5c0 | 2008-08-22 04:03:44 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 12 | #include "base/file_util.h" |
| 13 | #include "base/file_version_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | #include "base/message_loop.h" |
[email protected] | 8684a881 | 2011-03-22 13:59:38 | [diff] [blame] | 15 | #include "base/metrics/field_trial.h" |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 16 | #include "base/metrics/histogram.h" |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 17 | #include "base/rand_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 18 | #include "base/string_util.h" |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 19 | #include "base/time.h" |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 20 | #include "net/base/filter.h" |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 21 | #include "net/base/host_port_pair.h" |
[email protected] | b843072 | 2008-09-17 20:05:44 | [diff] [blame] | 22 | #include "net/base/load_flags.h" |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 23 | #include "net/base/mime_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 24 | #include "net/base/net_errors.h" |
| 25 | #include "net/base/net_util.h" |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 26 | #include "net/base/network_delegate.h" |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 27 | #include "net/base/sdch_manager.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 28 | #include "net/cert/cert_status_flags.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 29 | #include "net/cookies/cookie_monster.h" |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 30 | #include "net/http/http_network_session.h" |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 31 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 32 | #include "net/http/http_response_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include "net/http/http_response_info.h" |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 34 | #include "net/http/http_status_code.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | #include "net/http/http_transaction.h" |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 36 | #include "net/http/http_transaction_delegate.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | #include "net/http/http_transaction_factory.h" |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 38 | #include "net/http/http_util.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 39 | #include "net/ssl/ssl_cert_request_info.h" |
| 40 | #include "net/ssl/ssl_config_service.h" |
[email protected] | 7f26918 | 2011-10-16 01:58:34 | [diff] [blame] | 41 | #include "net/url_request/fraudulent_certificate_reporter.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 42 | #include "net/url_request/http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | #include "net/url_request/url_request.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 44 | #include "net/url_request/url_request_context.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | #include "net/url_request/url_request_error_job.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame^] | 46 | #include "net/url_request/url_request_job_factory.h" |
[email protected] | 06965e0 | 2009-09-04 21:36:42 | [diff] [blame] | 47 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 48 | #include "net/url_request/url_request_throttler_header_adapter.h" |
| 49 | #include "net/url_request/url_request_throttler_manager.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 51 | static const char kAvailDictionaryHeader[] = "Avail-Dictionary"; |
| 52 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 53 | namespace net { |
| 54 | |
[email protected] | 2e92354c | 2011-03-25 20:49:53 | [diff] [blame] | 55 | class URLRequestHttpJob::HttpFilterContext : public FilterContext { |
| 56 | public: |
| 57 | explicit HttpFilterContext(URLRequestHttpJob* job); |
| 58 | virtual ~HttpFilterContext(); |
| 59 | |
| 60 | // FilterContext implementation. |
[email protected] | 1e43b681 | 2012-08-19 19:14:00 | [diff] [blame] | 61 | virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; |
| 62 | virtual bool GetURL(GURL* gurl) const OVERRIDE; |
| 63 | virtual base::Time GetRequestTime() const OVERRIDE; |
| 64 | virtual bool IsCachedContent() const OVERRIDE; |
| 65 | virtual bool IsDownload() const OVERRIDE; |
| 66 | virtual bool IsSdchResponse() const OVERRIDE; |
| 67 | virtual int64 GetByteReadCount() const OVERRIDE; |
| 68 | virtual int GetResponseCode() const OVERRIDE; |
| 69 | virtual void RecordPacketStats(StatisticSelector statistic) const OVERRIDE; |
[email protected] | 2e92354c | 2011-03-25 20:49:53 | [diff] [blame] | 70 | |
[email protected] | 46668fe5 | 2011-05-04 19:03:23 | [diff] [blame] | 71 | // Method to allow us to reset filter context for a response that should have |
| 72 | // been SDCH encoded when there is an update due to an explicit HTTP header. |
| 73 | void ResetSdchResponseToFalse(); |
| 74 | |
[email protected] | 2e92354c | 2011-03-25 20:49:53 | [diff] [blame] | 75 | private: |
| 76 | URLRequestHttpJob* job_; |
| 77 | |
| 78 | DISALLOW_COPY_AND_ASSIGN(HttpFilterContext); |
| 79 | }; |
| 80 | |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 81 | class URLRequestHttpJob::HttpTransactionDelegateImpl |
| 82 | : public HttpTransactionDelegate { |
| 83 | public: |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 84 | HttpTransactionDelegateImpl( |
| 85 | URLRequest* request, NetworkDelegate* network_delegate) |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 86 | : request_(request), |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 87 | network_delegate_(network_delegate), |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 88 | cache_active_(false), |
| 89 | network_active_(false) { |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 90 | } |
| 91 | virtual ~HttpTransactionDelegateImpl() { |
| 92 | OnDetachRequest(); |
| 93 | } |
| 94 | void OnDetachRequest() { |
| 95 | if (request_ == NULL || network_delegate_ == NULL) |
| 96 | return; |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 97 | network_delegate_->NotifyRequestWaitStateChange( |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 98 | *request_, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 99 | NetworkDelegate::REQUEST_WAIT_STATE_RESET); |
| 100 | cache_active_ = false; |
| 101 | network_active_ = false; |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 102 | request_ = NULL; |
| 103 | } |
| 104 | virtual void OnCacheActionStart() OVERRIDE { |
| 105 | if (request_ == NULL || network_delegate_ == NULL) |
| 106 | return; |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 107 | DCHECK(!cache_active_ && !network_active_); |
| 108 | cache_active_ = true; |
| 109 | network_delegate_->NotifyRequestWaitStateChange( |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 110 | *request_, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 111 | NetworkDelegate::REQUEST_WAIT_STATE_CACHE_START); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 112 | } |
| 113 | virtual void OnCacheActionFinish() OVERRIDE { |
| 114 | if (request_ == NULL || network_delegate_ == NULL) |
| 115 | return; |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 116 | DCHECK(cache_active_ && !network_active_); |
| 117 | cache_active_ = false; |
| 118 | network_delegate_->NotifyRequestWaitStateChange( |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 119 | *request_, |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 120 | NetworkDelegate::REQUEST_WAIT_STATE_CACHE_FINISH); |
| 121 | } |
| 122 | virtual void OnNetworkActionStart() OVERRIDE { |
| 123 | if (request_ == NULL || network_delegate_ == NULL) |
| 124 | return; |
| 125 | DCHECK(!cache_active_ && !network_active_); |
| 126 | network_active_ = true; |
| 127 | network_delegate_->NotifyRequestWaitStateChange( |
| 128 | *request_, |
| 129 | NetworkDelegate::REQUEST_WAIT_STATE_NETWORK_START); |
| 130 | } |
| 131 | virtual void OnNetworkActionFinish() OVERRIDE { |
| 132 | if (request_ == NULL || network_delegate_ == NULL) |
| 133 | return; |
| 134 | DCHECK(!cache_active_ && network_active_); |
| 135 | network_active_ = false; |
| 136 | network_delegate_->NotifyRequestWaitStateChange( |
| 137 | *request_, |
| 138 | NetworkDelegate::REQUEST_WAIT_STATE_NETWORK_FINISH); |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 139 | } |
| 140 | private: |
| 141 | URLRequest* request_; |
| 142 | NetworkDelegate* network_delegate_; |
[email protected] | a9e0d141 | 2012-08-20 22:13:01 | [diff] [blame] | 143 | bool cache_active_; |
| 144 | bool network_active_; |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 145 | }; |
| 146 | |
[email protected] | fc01f23 | 2011-03-17 19:06:01 | [diff] [blame] | 147 | URLRequestHttpJob::HttpFilterContext::HttpFilterContext(URLRequestHttpJob* job) |
| 148 | : job_(job) { |
| 149 | DCHECK(job_); |
| 150 | } |
| 151 | |
| 152 | URLRequestHttpJob::HttpFilterContext::~HttpFilterContext() { |
| 153 | } |
| 154 | |
| 155 | bool URLRequestHttpJob::HttpFilterContext::GetMimeType( |
| 156 | std::string* mime_type) const { |
| 157 | return job_->GetMimeType(mime_type); |
| 158 | } |
| 159 | |
| 160 | bool URLRequestHttpJob::HttpFilterContext::GetURL(GURL* gurl) const { |
| 161 | if (!job_->request()) |
| 162 | return false; |
| 163 | *gurl = job_->request()->url(); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | base::Time URLRequestHttpJob::HttpFilterContext::GetRequestTime() const { |
| 168 | return job_->request() ? job_->request()->request_time() : base::Time(); |
| 169 | } |
| 170 | |
| 171 | bool URLRequestHttpJob::HttpFilterContext::IsCachedContent() const { |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 172 | return job_->is_cached_content_; |
[email protected] | fc01f23 | 2011-03-17 19:06:01 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | bool URLRequestHttpJob::HttpFilterContext::IsDownload() const { |
| 176 | return (job_->request_info_.load_flags & LOAD_IS_DOWNLOAD) != 0; |
| 177 | } |
| 178 | |
[email protected] | 46668fe5 | 2011-05-04 19:03:23 | [diff] [blame] | 179 | void URLRequestHttpJob::HttpFilterContext::ResetSdchResponseToFalse() { |
| 180 | DCHECK(job_->sdch_dictionary_advertised_); |
| 181 | job_->sdch_dictionary_advertised_ = false; |
| 182 | } |
| 183 | |
[email protected] | fc01f23 | 2011-03-17 19:06:01 | [diff] [blame] | 184 | bool URLRequestHttpJob::HttpFilterContext::IsSdchResponse() const { |
| 185 | return job_->sdch_dictionary_advertised_; |
| 186 | } |
| 187 | |
| 188 | int64 URLRequestHttpJob::HttpFilterContext::GetByteReadCount() const { |
| 189 | return job_->filter_input_byte_count(); |
| 190 | } |
| 191 | |
| 192 | int URLRequestHttpJob::HttpFilterContext::GetResponseCode() const { |
| 193 | return job_->GetResponseCode(); |
| 194 | } |
| 195 | |
| 196 | void URLRequestHttpJob::HttpFilterContext::RecordPacketStats( |
| 197 | StatisticSelector statistic) const { |
| 198 | job_->RecordPacketStats(statistic); |
| 199 | } |
| 200 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 201 | // TODO(darin): make sure the port blocking code is not lost |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 202 | // static |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 203 | URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request, |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 204 | NetworkDelegate* network_delegate, |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 205 | const std::string& scheme) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 206 | DCHECK(scheme == "http" || scheme == "https"); |
| 207 | |
[email protected] | 81293f48 | 2012-08-13 19:35:45 | [diff] [blame] | 208 | if (!request->context()->http_transaction_factory()) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 209 | NOTREACHED() << "requires a valid context"; |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 210 | return new URLRequestErrorJob( |
| 211 | request, network_delegate, ERR_INVALID_ARGUMENT); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | ce572df | 2012-05-04 19:47:17 | [diff] [blame] | 214 | GURL redirect_url; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 215 | if (request->GetHSTSRedirect(&redirect_url)) { |
| 216 | return new URLRequestRedirectJob( |
| 217 | request, network_delegate, redirect_url, |
| 218 | // Use status code 307 to preserve the method, so POST requests work. |
| 219 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); |
| 220 | } |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 221 | return new URLRequestHttpJob(request, |
| 222 | network_delegate, |
| 223 | request->context()->http_user_agent_settings()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 224 | } |
| 225 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 226 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 227 | URLRequestHttpJob::URLRequestHttpJob( |
| 228 | URLRequest* request, |
| 229 | NetworkDelegate* network_delegate, |
| 230 | const HttpUserAgentSettings* http_user_agent_settings) |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 231 | : URLRequestJob(request, network_delegate), |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 232 | priority_(DEFAULT_PRIORITY), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 233 | response_info_(NULL), |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 234 | response_cookies_save_index_(0), |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 235 | proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH), |
| 236 | server_auth_state_(AUTH_STATE_DONT_NEED_AUTH), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 237 | start_callback_(base::Bind( |
| 238 | &URLRequestHttpJob::OnStartCompleted, base::Unretained(this))), |
| 239 | notify_before_headers_sent_callback_(base::Bind( |
| 240 | &URLRequestHttpJob::NotifyBeforeSendHeadersCallback, |
| 241 | base::Unretained(this))), |
[email protected] | 3589e55 | 2008-08-20 23:11:34 | [diff] [blame] | 242 | read_in_progress_(false), |
[email protected] | 2aecf738 | 2009-06-17 04:14:27 | [diff] [blame] | 243 | transaction_(NULL), |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 244 | throttling_entry_(NULL), |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 245 | sdch_dictionary_advertised_(false), |
| 246 | sdch_test_activated_(false), |
[email protected] | d8fd513 | 2009-05-15 01:06:53 | [diff] [blame] | 247 | sdch_test_control_(false), |
[email protected] | 00e48bf | 2010-12-03 06:15:42 | [diff] [blame] | 248 | is_cached_content_(false), |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 249 | request_creation_time_(), |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 250 | packet_timing_enabled_(false), |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 251 | done_(false), |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 252 | bytes_observed_in_packets_(0), |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 253 | request_time_snapshot_(), |
| 254 | final_packet_time_(), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 255 | filter_context_(new HttpFilterContext(this)), |
| 256 | weak_factory_(this), |
| 257 | on_headers_received_callback_(base::Bind( |
| 258 | &URLRequestHttpJob::OnHeadersReceivedCallback, |
| 259 | base::Unretained(this))), |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 260 | awaiting_callback_(false), |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 261 | http_transaction_delegate_(new HttpTransactionDelegateImpl( |
| 262 | request, network_delegate)), |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 263 | http_user_agent_settings_(http_user_agent_settings) { |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 264 | URLRequestThrottlerManager* manager = request->context()->throttler_manager(); |
| 265 | if (manager) |
| 266 | throttling_entry_ = manager->RegisterRequestUrl(request->url()); |
| 267 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 268 | ResetTimer(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 271 | URLRequestHttpJob::~URLRequestHttpJob() { |
| 272 | CHECK(!awaiting_callback_); |
| 273 | |
| 274 | DCHECK(!sdch_test_control_ || !sdch_test_activated_); |
| 275 | if (!is_cached_content_) { |
| 276 | if (sdch_test_control_) |
| 277 | RecordPacketStats(FilterContext::SDCH_EXPERIMENT_HOLDBACK); |
| 278 | if (sdch_test_activated_) |
| 279 | RecordPacketStats(FilterContext::SDCH_EXPERIMENT_DECODE); |
| 280 | } |
| 281 | // Make sure SDCH filters are told to emit histogram data while |
| 282 | // filter_context_ is still alive. |
| 283 | DestroyFilters(); |
| 284 | |
| 285 | if (sdch_dictionary_url_.is_valid()) { |
| 286 | // Prior to reaching the destructor, request_ has been set to a NULL |
| 287 | // pointer, so request_->url() is no longer valid in the destructor, and we |
| 288 | // use an alternate copy |request_info_.url|. |
| 289 | SdchManager* manager = SdchManager::Global(); |
| 290 | // To be extra safe, since this is a "different time" from when we decided |
| 291 | // to get the dictionary, we'll validate that an SdchManager is available. |
| 292 | // At shutdown time, care is taken to be sure that we don't delete this |
| 293 | // globally useful instance "too soon," so this check is just defensive |
| 294 | // coding to assure that IF the system is shutting down, we don't have any |
| 295 | // problem if the manager was deleted ahead of time. |
| 296 | if (manager) // Defensive programming. |
| 297 | manager->FetchDictionary(request_info_.url, sdch_dictionary_url_); |
| 298 | } |
| 299 | DoneWithRequest(ABORTED); |
| 300 | } |
| 301 | |
| 302 | void URLRequestHttpJob::SetPriority(RequestPriority priority) { |
| 303 | priority_ = priority; |
| 304 | if (transaction_) |
| 305 | transaction_->SetPriority(priority_); |
| 306 | } |
| 307 | |
| 308 | void URLRequestHttpJob::Start() { |
| 309 | DCHECK(!transaction_.get()); |
| 310 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 311 | // URLRequest::SetReferrer ensures that we do not send username and password |
| 312 | // fields in the referrer. |
| 313 | GURL referrer(request_->referrer()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 314 | |
| 315 | request_info_.url = request_->url(); |
| 316 | request_info_.method = request_->method(); |
| 317 | request_info_.load_flags = request_->load_flags(); |
| 318 | request_info_.request_id = request_->identifier(); |
| 319 | |
| 320 | // Strip Referer from request_info_.extra_headers to prevent, e.g., plugins |
| 321 | // from overriding headers that are controlled using other means. Otherwise a |
| 322 | // plugin could set a referrer although sending the referrer is inhibited. |
| 323 | request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kReferer); |
| 324 | |
| 325 | // Our consumer should have made sure that this is a safe referrer. See for |
| 326 | // instance WebCore::FrameLoader::HideReferrer. |
| 327 | if (referrer.is_valid()) { |
| 328 | request_info_.extra_headers.SetHeader(HttpRequestHeaders::kReferer, |
| 329 | referrer.spec()); |
| 330 | } |
| 331 | |
| 332 | request_info_.extra_headers.SetHeaderIfMissing( |
| 333 | HttpRequestHeaders::kUserAgent, |
| 334 | http_user_agent_settings_ ? |
| 335 | http_user_agent_settings_->GetUserAgent(request_->url()) : |
| 336 | EmptyString()); |
| 337 | |
| 338 | AddExtraHeaders(); |
| 339 | AddCookieHeaderAndStart(); |
| 340 | } |
| 341 | |
| 342 | void URLRequestHttpJob::Kill() { |
| 343 | http_transaction_delegate_->OnDetachRequest(); |
| 344 | |
| 345 | if (!transaction_.get()) |
| 346 | return; |
| 347 | |
| 348 | weak_factory_.InvalidateWeakPtrs(); |
| 349 | DestroyTransaction(); |
| 350 | URLRequestJob::Kill(); |
| 351 | } |
| 352 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 353 | void URLRequestHttpJob::NotifyHeadersComplete() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 354 | DCHECK(!response_info_); |
| 355 | |
| 356 | response_info_ = transaction_->GetResponseInfo(); |
| 357 | |
[email protected] | d8fd513 | 2009-05-15 01:06:53 | [diff] [blame] | 358 | // Save boolean, as we'll need this info at destruction time, and filters may |
| 359 | // also need this info. |
| 360 | is_cached_content_ = response_info_->was_cached; |
| 361 | |
[email protected] | a73a280 | 2012-05-02 19:20:15 | [diff] [blame] | 362 | if (!is_cached_content_ && throttling_entry_) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 363 | URLRequestThrottlerHeaderAdapter response_adapter(GetResponseHeaders()); |
[email protected] | 2fd33ee9 | 2011-03-25 22:30:21 | [diff] [blame] | 364 | throttling_entry_->UpdateWithResponse(request_info_.url.host(), |
| 365 | &response_adapter); |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 366 | } |
| 367 | |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 368 | // The ordering of these calls is not important. |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 369 | ProcessStrictTransportSecurityHeader(); |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 370 | ProcessPublicKeyPinsHeader(); |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 371 | |
[email protected] | fe21987 | 2008-09-23 02:17:00 | [diff] [blame] | 372 | if (SdchManager::Global() && |
| 373 | SdchManager::Global()->IsInSupportedDomain(request_->url())) { |
[email protected] | 26430024 | 2011-11-07 06:03:30 | [diff] [blame] | 374 | const std::string name = "Get-Dictionary"; |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 375 | std::string url_text; |
| 376 | void* iter = NULL; |
| 377 | // TODO(jar): We need to not fetch dictionaries the first time they are |
| 378 | // seen, but rather wait until we can justify their usefulness. |
| 379 | // For now, we will only fetch the first dictionary, which will at least |
| 380 | // require multiple suggestions before we get additional ones for this site. |
| 381 | // Eventually we should wait until a dictionary is requested several times |
| 382 | // before we even download it (so that we don't waste memory or bandwidth). |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 383 | if (GetResponseHeaders()->EnumerateHeader(&iter, name, &url_text)) { |
[email protected] | d55ad15d | 2009-02-17 19:40:50 | [diff] [blame] | 384 | // request_->url() won't be valid in the destructor, so we use an |
| 385 | // alternate copy. |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 386 | DCHECK_EQ(request_->url(), request_info_.url); |
[email protected] | d55ad15d | 2009-02-17 19:40:50 | [diff] [blame] | 387 | // Resolve suggested URL relative to request url. |
| 388 | sdch_dictionary_url_ = request_info_.url.Resolve(url_text); |
[email protected] | 6088942 | 2008-09-23 01:18:16 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 392 | // The HTTP transaction may be restarted several times for the purposes |
| 393 | // of sending authorization information. Each time it restarts, we get |
| 394 | // notified of the headers completion so that we can update the cookie store. |
| 395 | if (transaction_->IsReadyToRestartForAuth()) { |
| 396 | DCHECK(!response_info_->auth_challenge.get()); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 397 | // TODO(battre): This breaks the webrequest API for |
| 398 | // URLRequestTestHTTP.BasicAuthWithCookies |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 399 | // where OnBeforeSendHeaders -> OnSendHeaders -> OnBeforeSendHeaders |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 400 | // occurs. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 401 | RestartTransactionWithAuth(AuthCredentials()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 405 | URLRequestJob::NotifyHeadersComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 406 | } |
| 407 | |
[email protected] | 85c1dce | 2011-07-06 12:01:29 | [diff] [blame] | 408 | void URLRequestHttpJob::NotifyDone(const URLRequestStatus& status) { |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 409 | DoneWithRequest(FINISHED); |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 410 | URLRequestJob::NotifyDone(status); |
| 411 | } |
| 412 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 413 | void URLRequestHttpJob::DestroyTransaction() { |
[email protected] | c6a4eb9 | 2010-03-03 23:51:19 | [diff] [blame] | 414 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 415 | |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 416 | DoneWithRequest(ABORTED); |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 417 | transaction_.reset(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 418 | response_info_ = NULL; |
| 419 | } |
| 420 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 421 | void URLRequestHttpJob::StartTransaction() { |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 422 | if (network_delegate()) { |
| 423 | int rv = network_delegate()->NotifyBeforeSendHeaders( |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 424 | request_, notify_before_headers_sent_callback_, |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 425 | &request_info_.extra_headers); |
| 426 | // If an extension blocks the request, we rely on the callback to |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 427 | // MaybeStartTransactionInternal(). |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 428 | if (rv == ERR_IO_PENDING) { |
[email protected] | 9c235f04 | 2011-08-10 22:28:21 | [diff] [blame] | 429 | SetBlockedOnDelegate(); |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 430 | return; |
| 431 | } |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 432 | MaybeStartTransactionInternal(rv); |
| 433 | return; |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 434 | } |
| 435 | StartTransactionInternal(); |
| 436 | } |
| 437 | |
| 438 | void URLRequestHttpJob::NotifyBeforeSendHeadersCallback(int result) { |
[email protected] | 9c235f04 | 2011-08-10 22:28:21 | [diff] [blame] | 439 | SetUnblockedOnDelegate(); |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 440 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 441 | // Check that there are no callbacks to already canceled requests. |
| 442 | DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status()); |
| 443 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 444 | MaybeStartTransactionInternal(result); |
| 445 | } |
| 446 | |
| 447 | void URLRequestHttpJob::MaybeStartTransactionInternal(int result) { |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 448 | if (result == OK) { |
| 449 | StartTransactionInternal(); |
| 450 | } else { |
[email protected] | 55b8a6c1 | 2012-06-13 22:03:42 | [diff] [blame] | 451 | std::string source("delegate"); |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 452 | request_->net_log().AddEvent(NetLog::TYPE_CANCELLED, |
[email protected] | 55b8a6c1 | 2012-06-13 22:03:42 | [diff] [blame] | 453 | NetLog::StringCallback("source", &source)); |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 454 | NotifyCanceled(); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 455 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
[email protected] | 636eccd | 2011-06-28 12:28:01 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | void URLRequestHttpJob::StartTransactionInternal() { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 460 | // NOTE: This method assumes that request_info_ is already setup properly. |
| 461 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 462 | // If we already have a transaction, then we should restart the transaction |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 463 | // with auth provided by auth_credentials_. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 464 | |
[email protected] | 99c0790 | 2010-08-17 18:59:52 | [diff] [blame] | 465 | int rv; |
[email protected] | 6b3f964 | 2010-11-25 02:29:06 | [diff] [blame] | 466 | |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 467 | if (network_delegate()) { |
| 468 | network_delegate()->NotifySendHeaders( |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 469 | request_, request_info_.extra_headers); |
| 470 | } |
| 471 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 472 | if (transaction_.get()) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 473 | rv = transaction_->RestartWithAuth(auth_credentials_, start_callback_); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 474 | auth_credentials_ = AuthCredentials(); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 475 | } else { |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 476 | DCHECK(request_->context()->http_transaction_factory()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 477 | |
[email protected] | 99c0790 | 2010-08-17 18:59:52 | [diff] [blame] | 478 | rv = request_->context()->http_transaction_factory()->CreateTransaction( |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 479 | priority_, &transaction_, http_transaction_delegate_.get()); |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 480 | if (rv == OK) { |
[email protected] | a1d4ab07 | 2012-06-07 13:21:15 | [diff] [blame] | 481 | if (!throttling_entry_ || |
| 482 | !throttling_entry_->ShouldRejectRequest(*request_)) { |
[email protected] | 227b0e8 | 2011-03-25 21:11:53 | [diff] [blame] | 483 | rv = transaction_->Start( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 484 | &request_info_, start_callback_, request_->net_log()); |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 485 | start_time_ = base::TimeTicks::Now(); |
[email protected] | 227b0e8 | 2011-03-25 21:11:53 | [diff] [blame] | 486 | } else { |
| 487 | // Special error code for the exponential back-off module. |
| 488 | rv = ERR_TEMPORARILY_THROTTLED; |
| 489 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 490 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 491 | } |
| 492 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 493 | if (rv == ERR_IO_PENDING) |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 494 | return; |
| 495 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 496 | // The transaction started synchronously, but we need to notify the |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 497 | // URLRequest delegate via the message loop. |
[email protected] | 00e48bf | 2010-12-03 06:15:42 | [diff] [blame] | 498 | MessageLoop::current()->PostTask( |
| 499 | FROM_HERE, |
[email protected] | 098b2911 | 2011-12-20 21:12:34 | [diff] [blame] | 500 | base::Bind(&URLRequestHttpJob::OnStartCompleted, |
| 501 | weak_factory_.GetWeakPtr(), rv)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 502 | } |
| 503 | |
[email protected] | 175adac | 2008-07-30 17:28:04 | [diff] [blame] | 504 | void URLRequestHttpJob::AddExtraHeaders() { |
[email protected] | c7bef94c | 2011-06-21 18:05:51 | [diff] [blame] | 505 | // Supply Accept-Encoding field only if it is not already provided. |
| 506 | // It should be provided IF the content is known to have restrictions on |
| 507 | // potential encoding, such as streaming multi-media. |
| 508 | // For details see bug 47381. |
| 509 | // TODO(jar, enal): jpeg files etc. should set up a request header if |
| 510 | // possible. Right now it is done only by buffered_resource_loader and |
| 511 | // simple_data_source. |
| 512 | if (!request_info_.extra_headers.HasHeader( |
| 513 | HttpRequestHeaders::kAcceptEncoding)) { |
| 514 | bool advertise_sdch = SdchManager::Global() && |
| 515 | SdchManager::Global()->IsInSupportedDomain(request_->url()); |
| 516 | std::string avail_dictionaries; |
| 517 | if (advertise_sdch) { |
| 518 | SdchManager::Global()->GetAvailDictionaryList(request_->url(), |
| 519 | &avail_dictionaries); |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 520 | |
[email protected] | c7bef94c | 2011-06-21 18:05:51 | [diff] [blame] | 521 | // The AllowLatencyExperiment() is only true if we've successfully done a |
| 522 | // full SDCH compression recently in this browser session for this host. |
| 523 | // Note that for this path, there might be no applicable dictionaries, |
| 524 | // and hence we can't participate in the experiment. |
| 525 | if (!avail_dictionaries.empty() && |
| 526 | SdchManager::Global()->AllowLatencyExperiment(request_->url())) { |
| 527 | // We are participating in the test (or control), and hence we'll |
| 528 | // eventually record statistics via either SDCH_EXPERIMENT_DECODE or |
| 529 | // SDCH_EXPERIMENT_HOLDBACK, and we'll need some packet timing data. |
| 530 | packet_timing_enabled_ = true; |
| 531 | if (base::RandDouble() < .01) { |
| 532 | sdch_test_control_ = true; // 1% probability. |
| 533 | advertise_sdch = false; |
| 534 | } else { |
| 535 | sdch_test_activated_ = true; |
| 536 | } |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 537 | } |
| 538 | } |
[email protected] | 5b90b5d | 2009-04-30 23:06:01 | [diff] [blame] | 539 | |
[email protected] | c7bef94c | 2011-06-21 18:05:51 | [diff] [blame] | 540 | // Supply Accept-Encoding headers first so that it is more likely that they |
| 541 | // will be in the first transmitted packet. This can sometimes make it |
| 542 | // easier to filter and analyze the streams to assure that a proxy has not |
| 543 | // damaged these headers. Some proxies deliberately corrupt Accept-Encoding |
| 544 | // headers. |
| 545 | if (!advertise_sdch) { |
| 546 | // Tell the server what compression formats we support (other than SDCH). |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 547 | request_info_.extra_headers.SetHeader( |
[email protected] | c7bef94c | 2011-06-21 18:05:51 | [diff] [blame] | 548 | HttpRequestHeaders::kAcceptEncoding, "gzip,deflate"); |
| 549 | } else { |
| 550 | // Include SDCH in acceptable list. |
| 551 | request_info_.extra_headers.SetHeader( |
| 552 | HttpRequestHeaders::kAcceptEncoding, "gzip,deflate,sdch"); |
| 553 | if (!avail_dictionaries.empty()) { |
| 554 | request_info_.extra_headers.SetHeader( |
| 555 | kAvailDictionaryHeader, |
| 556 | avail_dictionaries); |
| 557 | sdch_dictionary_advertised_ = true; |
| 558 | // Since we're tagging this transaction as advertising a dictionary, |
| 559 | // we'll definitely employ an SDCH filter (or tentative sdch filter) |
| 560 | // when we get a response. When done, we'll record histograms via |
| 561 | // SDCH_DECODE or SDCH_PASSTHROUGH. Hence we need to record packet |
| 562 | // arrival times. |
| 563 | packet_timing_enabled_ = true; |
| 564 | } |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 565 | } |
[email protected] | 423041b | 2008-10-27 17:39:28 | [diff] [blame] | 566 | } |
| 567 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 568 | if (http_user_agent_settings_) { |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 569 | // Only add default Accept-Language if the request didn't have it |
| 570 | // specified. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 571 | std::string accept_language = |
| 572 | http_user_agent_settings_->GetAcceptLanguage(); |
| 573 | if (!accept_language.empty()) { |
| 574 | request_info_.extra_headers.SetHeaderIfMissing( |
| 575 | HttpRequestHeaders::kAcceptLanguage, |
| 576 | accept_language); |
| 577 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 578 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 579 | } |
| 580 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 581 | void URLRequestHttpJob::AddCookieHeaderAndStart() { |
| 582 | // No matter what, we want to report our status as IO pending since we will |
| 583 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 584 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 585 | |
[email protected] | ed24fad | 2011-05-10 22:44:01 | [diff] [blame] | 586 | // If the request was destroyed, then there is no more work to do. |
| 587 | if (!request_) |
| 588 | return; |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 589 | |
[email protected] | 81293f48 | 2012-08-13 19:35:45 | [diff] [blame] | 590 | CookieStore* cookie_store = request_->context()->cookie_store(); |
[email protected] | 1a6fff5 | 2011-10-20 21:00:16 | [diff] [blame] | 591 | if (cookie_store && !(request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES)) { |
| 592 | net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster(); |
| 593 | if (cookie_monster) { |
| 594 | cookie_monster->GetAllCookiesForURLAsync( |
| 595 | request_->url(), |
| 596 | base::Bind(&URLRequestHttpJob::CheckCookiePolicyAndLoad, |
[email protected] | 098b2911 | 2011-12-20 21:12:34 | [diff] [blame] | 597 | weak_factory_.GetWeakPtr())); |
[email protected] | 1a6fff5 | 2011-10-20 21:00:16 | [diff] [blame] | 598 | } else { |
[email protected] | cec80c3d | 2012-12-04 12:17:41 | [diff] [blame] | 599 | CheckCookiePolicyAndLoad(CookieList()); |
[email protected] | 1a6fff5 | 2011-10-20 21:00:16 | [diff] [blame] | 600 | } |
[email protected] | 03d845f | 2011-07-29 19:06:26 | [diff] [blame] | 601 | } else { |
| 602 | DoStartTransaction(); |
| 603 | } |
| 604 | } |
| 605 | |
[email protected] | 1a6fff5 | 2011-10-20 21:00:16 | [diff] [blame] | 606 | void URLRequestHttpJob::DoLoadCookies() { |
| 607 | CookieOptions options; |
| 608 | options.set_include_httponly(); |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 609 | request_->context()->cookie_store()->GetCookiesWithOptionsAsync( |
[email protected] | 1a6fff5 | 2011-10-20 21:00:16 | [diff] [blame] | 610 | request_->url(), options, |
| 611 | base::Bind(&URLRequestHttpJob::OnCookiesLoaded, |
[email protected] | 098b2911 | 2011-12-20 21:12:34 | [diff] [blame] | 612 | weak_factory_.GetWeakPtr())); |
[email protected] | 1a6fff5 | 2011-10-20 21:00:16 | [diff] [blame] | 613 | } |
| 614 | |
[email protected] | 03d845f | 2011-07-29 19:06:26 | [diff] [blame] | 615 | void URLRequestHttpJob::CheckCookiePolicyAndLoad( |
| 616 | const CookieList& cookie_list) { |
[email protected] | 1a6fff5 | 2011-10-20 21:00:16 | [diff] [blame] | 617 | if (CanGetCookies(cookie_list)) |
| 618 | DoLoadCookies(); |
| 619 | else |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 620 | DoStartTransaction(); |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 621 | } |
| 622 | |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 623 | void URLRequestHttpJob::OnCookiesLoaded(const std::string& cookie_line) { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 624 | if (!cookie_line.empty()) { |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 625 | request_info_.extra_headers.SetHeader( |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 626 | HttpRequestHeaders::kCookie, cookie_line); |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 627 | } |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 628 | DoStartTransaction(); |
| 629 | } |
| 630 | |
| 631 | void URLRequestHttpJob::DoStartTransaction() { |
[email protected] | 03d845f | 2011-07-29 19:06:26 | [diff] [blame] | 632 | // We may have been canceled while retrieving cookies. |
[email protected] | 9025016c | 2011-05-12 15:51:23 | [diff] [blame] | 633 | if (GetStatus().is_success()) { |
| 634 | StartTransaction(); |
| 635 | } else { |
| 636 | NotifyCanceled(); |
| 637 | } |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 638 | } |
| 639 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 640 | void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) { |
| 641 | if (result != net::OK) { |
[email protected] | 55b8a6c1 | 2012-06-13 22:03:42 | [diff] [blame] | 642 | std::string source("delegate"); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 643 | request_->net_log().AddEvent(NetLog::TYPE_CANCELLED, |
[email protected] | 55b8a6c1 | 2012-06-13 22:03:42 | [diff] [blame] | 644 | NetLog::StringCallback("source", &source)); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 645 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 646 | return; |
| 647 | } |
| 648 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 649 | DCHECK(transaction_.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 650 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 651 | const HttpResponseInfo* response_info = transaction_->GetResponseInfo(); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 652 | DCHECK(response_info); |
| 653 | |
| 654 | response_cookies_.clear(); |
| 655 | response_cookies_save_index_ = 0; |
| 656 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 657 | FetchResponseCookies(&response_cookies_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 658 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 659 | if (!GetResponseHeaders()->GetDateValue(&response_date_)) |
| 660 | response_date_ = base::Time(); |
| 661 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 662 | // Now, loop over the response cookies, and attempt to persist each. |
| 663 | SaveNextCookie(); |
| 664 | } |
| 665 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 666 | // If the save occurs synchronously, SaveNextCookie will loop and save the next |
| 667 | // cookie. If the save is deferred, the callback is responsible for continuing |
| 668 | // to iterate through the cookies. |
| 669 | // TODO(erikwright): Modify the CookieStore API to indicate via return value |
| 670 | // whether it completed synchronously or asynchronously. |
| 671 | // See http://crbug.com/131066. |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 672 | void URLRequestHttpJob::SaveNextCookie() { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 673 | // No matter what, we want to report our status as IO pending since we will |
| 674 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 675 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 676 | |
| 677 | // Used to communicate with the callback. See the implementation of |
| 678 | // OnCookieSaved. |
| 679 | scoped_refptr<SharedBoolean> callback_pending = new SharedBoolean(false); |
| 680 | scoped_refptr<SharedBoolean> save_next_cookie_running = |
| 681 | new SharedBoolean(true); |
| 682 | |
| 683 | if (!(request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) && |
| 684 | request_->context()->cookie_store() && |
| 685 | response_cookies_.size() > 0) { |
| 686 | CookieOptions options; |
| 687 | options.set_include_httponly(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 688 | options.set_server_time(response_date_); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 689 | |
| 690 | net::CookieStore::SetCookiesCallback callback( |
| 691 | base::Bind(&URLRequestHttpJob::OnCookieSaved, |
| 692 | weak_factory_.GetWeakPtr(), |
| 693 | save_next_cookie_running, |
| 694 | callback_pending)); |
| 695 | |
| 696 | // Loop through the cookies as long as SetCookieWithOptionsAsync completes |
| 697 | // synchronously. |
| 698 | while (!callback_pending->data && |
| 699 | response_cookies_save_index_ < response_cookies_.size()) { |
| 700 | if (CanSetCookie( |
| 701 | response_cookies_[response_cookies_save_index_], &options)) { |
| 702 | callback_pending->data = true; |
| 703 | request_->context()->cookie_store()->SetCookieWithOptionsAsync( |
| 704 | request_->url(), response_cookies_[response_cookies_save_index_], |
| 705 | options, callback); |
| 706 | } |
| 707 | ++response_cookies_save_index_; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | save_next_cookie_running->data = false; |
| 712 | |
| 713 | if (!callback_pending->data) { |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 714 | response_cookies_.clear(); |
| 715 | response_cookies_save_index_ = 0; |
| 716 | SetStatus(URLRequestStatus()); // Clear the IO_PENDING status |
| 717 | NotifyHeadersComplete(); |
| 718 | return; |
| 719 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 720 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 721 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 722 | // |save_next_cookie_running| is true when the callback is bound and set to |
| 723 | // false when SaveNextCookie exits, allowing the callback to determine if the |
| 724 | // save occurred synchronously or asynchronously. |
| 725 | // |callback_pending| is false when the callback is invoked and will be set to |
| 726 | // true by the callback, allowing SaveNextCookie to detect whether the save |
| 727 | // occurred synchronously. |
| 728 | // See SaveNextCookie() for more information. |
| 729 | void URLRequestHttpJob::OnCookieSaved( |
| 730 | scoped_refptr<SharedBoolean> save_next_cookie_running, |
| 731 | scoped_refptr<SharedBoolean> callback_pending, |
| 732 | bool cookie_status) { |
| 733 | callback_pending->data = false; |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 734 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 735 | // If we were called synchronously, return. |
| 736 | if (save_next_cookie_running->data) { |
| 737 | return; |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 738 | } |
| 739 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 740 | // We were called asynchronously, so trigger the next save. |
[email protected] | 9025016c | 2011-05-12 15:51:23 | [diff] [blame] | 741 | // We may have been canceled within OnSetCookie. |
| 742 | if (GetStatus().is_success()) { |
| 743 | SaveNextCookie(); |
| 744 | } else { |
| 745 | NotifyCanceled(); |
| 746 | } |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | void URLRequestHttpJob::FetchResponseCookies( |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 750 | std::vector<std::string>* cookies) { |
[email protected] | 26430024 | 2011-11-07 06:03:30 | [diff] [blame] | 751 | const std::string name = "Set-Cookie"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 752 | std::string value; |
| 753 | |
| 754 | void* iter = NULL; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 755 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 756 | while (headers->EnumerateHeader(&iter, name, &value)) { |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 757 | if (!value.empty()) |
| 758 | cookies->push_back(value); |
| 759 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 760 | } |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 761 | |
[email protected] | 000d9df0 | 2012-01-18 20:01:46 | [diff] [blame] | 762 | // NOTE: |ProcessStrictTransportSecurityHeader| and |
| 763 | // |ProcessPublicKeyPinsHeader| have very similar structures, by design. |
[email protected] | 77f6fb43 | 2009-09-05 14:21:09 | [diff] [blame] | 764 | void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() { |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 765 | DCHECK(response_info_); |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 766 | TransportSecurityState* security_state = |
| 767 | request_->context()->transport_security_state(); |
[email protected] | e88006f | 2012-01-11 06:15:07 | [diff] [blame] | 768 | const SSLInfo& ssl_info = response_info_->ssl_info; |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 769 | |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 770 | // Only accept HSTS headers on HTTPS connections that have no |
| 771 | // certificate errors. |
[email protected] | e88006f | 2012-01-11 06:15:07 | [diff] [blame] | 772 | if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) || |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 773 | !security_state) |
[email protected] | e88006f | 2012-01-11 06:15:07 | [diff] [blame] | 774 | return; |
[email protected] | 326e679 | 2009-12-11 21:04:42 | [diff] [blame] | 775 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 776 | // http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec: |
| 777 | // |
| 778 | // If a UA receives more than one STS header field in a HTTP response |
| 779 | // message over secure transport, then the UA MUST process only the |
| 780 | // first such header field. |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 781 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 782 | std::string value; |
| 783 | if (headers->EnumerateHeader(NULL, "Strict-Transport-Security", &value)) |
| 784 | security_state->AddHSTSHeader(request_info_.url.host(), value); |
[email protected] | a9cea754 | 2009-05-20 04:30:23 | [diff] [blame] | 785 | } |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 786 | |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 787 | void URLRequestHttpJob::ProcessPublicKeyPinsHeader() { |
| 788 | DCHECK(response_info_); |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 789 | TransportSecurityState* security_state = |
| 790 | request_->context()->transport_security_state(); |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 791 | const SSLInfo& ssl_info = response_info_->ssl_info; |
| 792 | |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 793 | // Only accept HPKP headers on HTTPS connections that have no |
[email protected] | e88006f | 2012-01-11 06:15:07 | [diff] [blame] | 794 | // certificate errors. |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 795 | if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) || |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 796 | !security_state) |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 797 | return; |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 798 | |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 799 | // http://tools.ietf.org/html/draft-ietf-websec-key-pinning: |
| 800 | // |
| 801 | // If a UA receives more than one PKP header field in an HTTP |
| 802 | // response message over secure transport, then the UA MUST process |
| 803 | // only the first such header field. |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 804 | HttpResponseHeaders* headers = GetResponseHeaders(); |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 805 | std::string value; |
[email protected] | 6ed72be | 2013-01-08 22:07:33 | [diff] [blame] | 806 | if (headers->EnumerateHeader(NULL, "Public-Key-Pins", &value)) |
| 807 | security_state->AddHPKPHeader(request_info_.url.host(), value, ssl_info); |
[email protected] | fecef22 | 2012-01-05 02:26:15 | [diff] [blame] | 808 | } |
| 809 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 810 | void URLRequestHttpJob::OnStartCompleted(int result) { |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 811 | RecordTimer(); |
| 812 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 813 | // If the request was destroyed, then there is no more work to do. |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 814 | if (!request_) |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 815 | return; |
| 816 | |
| 817 | // If the transaction was destroyed, then the job was cancelled, and |
| 818 | // we can just ignore this notification. |
| 819 | if (!transaction_.get()) |
| 820 | return; |
| 821 | |
| 822 | // Clear the IO_PENDING status |
| 823 | SetStatus(URLRequestStatus()); |
| 824 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 825 | const URLRequestContext* context = request_->context(); |
| 826 | |
[email protected] | f357259 | 2011-12-12 21:36:31 | [diff] [blame] | 827 | if (result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN && |
| 828 | transaction_->GetResponseInfo() != NULL) { |
| 829 | FraudulentCertificateReporter* reporter = |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 830 | context->fraudulent_certificate_reporter(); |
[email protected] | f357259 | 2011-12-12 21:36:31 | [diff] [blame] | 831 | if (reporter != NULL) { |
| 832 | const SSLInfo& ssl_info = transaction_->GetResponseInfo()->ssl_info; |
[email protected] | 551ff517 | 2011-11-10 18:35:31 | [diff] [blame] | 833 | bool sni_available = SSLConfigService::IsSNIAvailable( |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 834 | context->ssl_config_service()); |
[email protected] | f357259 | 2011-12-12 21:36:31 | [diff] [blame] | 835 | const std::string& host = request_->url().host(); |
[email protected] | b799645 | 2011-10-31 19:30:56 | [diff] [blame] | 836 | |
[email protected] | f357259 | 2011-12-12 21:36:31 | [diff] [blame] | 837 | reporter->SendReport(host, ssl_info, sni_available); |
[email protected] | 381e885 | 2011-04-14 14:30:58 | [diff] [blame] | 838 | } |
| 839 | } |
[email protected] | 551ff517 | 2011-11-10 18:35:31 | [diff] [blame] | 840 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 841 | if (result == OK) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 842 | scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders(); |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 843 | if (network_delegate()) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 844 | // Note that |this| may not be deleted until |
| 845 | // |on_headers_received_callback_| or |
| 846 | // |NetworkDelegate::URLRequestDestroyed()| has been called. |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 847 | int error = network_delegate()->NotifyHeadersReceived( |
| 848 | request_, on_headers_received_callback_, |
| 849 | headers, &override_response_headers_); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 850 | if (error != net::OK) { |
| 851 | if (error == net::ERR_IO_PENDING) { |
| 852 | awaiting_callback_ = true; |
[email protected] | 0a12e45 | 2012-11-14 05:04:30 | [diff] [blame] | 853 | SetBlockedOnDelegate(); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 854 | } else { |
[email protected] | 55b8a6c1 | 2012-06-13 22:03:42 | [diff] [blame] | 855 | std::string source("delegate"); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 856 | request_->net_log().AddEvent(NetLog::TYPE_CANCELLED, |
[email protected] | 55b8a6c1 | 2012-06-13 22:03:42 | [diff] [blame] | 857 | NetLog::StringCallback("source", |
| 858 | &source)); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 859 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, error)); |
| 860 | } |
| 861 | return; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | SaveCookiesAndNotifyHeadersComplete(net::OK); |
[email protected] | e5624f0 | 2011-09-27 19:43:53 | [diff] [blame] | 866 | } else if (IsCertificateError(result)) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 867 | // We encountered an SSL certificate error. Ask our delegate to decide |
| 868 | // what we should do. |
[email protected] | e5624f0 | 2011-09-27 19:43:53 | [diff] [blame] | 869 | |
| 870 | TransportSecurityState::DomainState domain_state; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 871 | const URLRequestContext* context = request_->context(); |
[email protected] | 843329e | 2013-01-22 22:06:09 | [diff] [blame] | 872 | const bool fatal = context->transport_security_state() && |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 873 | context->transport_security_state()->GetDomainState( |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 874 | request_info_.url.host(), |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 875 | SSLConfigService::IsSNIAvailable(context->ssl_config_service()), |
[email protected] | 843329e | 2013-01-22 22:06:09 | [diff] [blame] | 876 | &domain_state) && |
| 877 | domain_state.ShouldSSLErrorsBeFatal(); |
[email protected] | 46d117e | 2012-01-18 01:53:14 | [diff] [blame] | 878 | NotifySSLCertificateError(transaction_->GetResponseInfo()->ssl_info, fatal); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 879 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 880 | NotifyCertificateRequested( |
| 881 | transaction_->GetResponseInfo()->cert_request_info); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 882 | } else { |
| 883 | NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 884 | } |
| 885 | } |
| 886 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 887 | void URLRequestHttpJob::OnHeadersReceivedCallback(int result) { |
[email protected] | 0a12e45 | 2012-11-14 05:04:30 | [diff] [blame] | 888 | SetUnblockedOnDelegate(); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 889 | awaiting_callback_ = false; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 890 | |
| 891 | // Check that there are no callbacks to already canceled requests. |
| 892 | DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status()); |
| 893 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 894 | SaveCookiesAndNotifyHeadersComplete(result); |
| 895 | } |
| 896 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 897 | void URLRequestHttpJob::OnReadCompleted(int result) { |
| 898 | read_in_progress_ = false; |
| 899 | |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 900 | if (ShouldFixMismatchedContentLength(result)) |
[email protected] | 5543cbb | 2012-04-20 16:35:23 | [diff] [blame] | 901 | result = OK; |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 902 | |
[email protected] | 5543cbb | 2012-04-20 16:35:23 | [diff] [blame] | 903 | if (result == OK) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 904 | NotifyDone(URLRequestStatus()); |
| 905 | } else if (result < 0) { |
| 906 | NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 907 | } else { |
| 908 | // Clear the IO_PENDING status |
| 909 | SetStatus(URLRequestStatus()); |
| 910 | } |
| 911 | |
| 912 | NotifyReadComplete(result); |
| 913 | } |
| 914 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 915 | void URLRequestHttpJob::RestartTransactionWithAuth( |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 916 | const AuthCredentials& credentials) { |
| 917 | auth_credentials_ = credentials; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 918 | |
| 919 | // These will be reset in OnStartCompleted. |
| 920 | response_info_ = NULL; |
| 921 | response_cookies_.clear(); |
| 922 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 923 | ResetTimer(); |
| 924 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 925 | // Update the cookies, since the cookie store may have been updated from the |
| 926 | // headers in the 401/407. Since cookies were already appended to |
| 927 | // extra_headers, we need to strip them out before adding them again. |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 928 | request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kCookie); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 929 | |
| 930 | AddCookieHeaderAndStart(); |
| 931 | } |
| 932 | |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 933 | void URLRequestHttpJob::SetUpload(UploadDataStream* upload) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 934 | DCHECK(!transaction_.get()) << "cannot change once started"; |
[email protected] | 0736d9e | 2012-11-28 19:50:40 | [diff] [blame] | 935 | request_info_.upload_data_stream = upload; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void URLRequestHttpJob::SetExtraRequestHeaders( |
| 939 | const HttpRequestHeaders& headers) { |
| 940 | DCHECK(!transaction_.get()) << "cannot change once started"; |
| 941 | request_info_.extra_headers.CopyFrom(headers); |
| 942 | } |
| 943 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 944 | LoadState URLRequestHttpJob::GetLoadState() const { |
| 945 | return transaction_.get() ? |
| 946 | transaction_->GetLoadState() : LOAD_STATE_IDLE; |
| 947 | } |
| 948 | |
[email protected] | 7335ab0 | 2012-08-30 22:30:42 | [diff] [blame] | 949 | UploadProgress URLRequestHttpJob::GetUploadProgress() const { |
| 950 | return transaction_.get() ? |
| 951 | transaction_->GetUploadProgress() : UploadProgress(); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const { |
| 955 | DCHECK(transaction_.get()); |
| 956 | |
| 957 | if (!response_info_) |
| 958 | return false; |
| 959 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 960 | return GetResponseHeaders()->GetMimeType(mime_type); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | bool URLRequestHttpJob::GetCharset(std::string* charset) { |
| 964 | DCHECK(transaction_.get()); |
| 965 | |
| 966 | if (!response_info_) |
| 967 | return false; |
| 968 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 969 | return GetResponseHeaders()->GetCharset(charset); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) { |
| 973 | DCHECK(request_); |
| 974 | DCHECK(transaction_.get()); |
| 975 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 976 | if (response_info_) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 977 | *info = *response_info_; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 978 | if (override_response_headers_) |
| 979 | info->headers = override_response_headers_; |
| 980 | } |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 981 | } |
| 982 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 983 | void URLRequestHttpJob::GetLoadTimingInfo( |
| 984 | LoadTimingInfo* load_timing_info) const { |
| 985 | if (transaction_) |
| 986 | transaction_->GetLoadTimingInfo(load_timing_info); |
| 987 | } |
| 988 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 989 | bool URLRequestHttpJob::GetResponseCookies(std::vector<std::string>* cookies) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 990 | DCHECK(transaction_.get()); |
| 991 | |
| 992 | if (!response_info_) |
| 993 | return false; |
| 994 | |
| 995 | // TODO(darin): Why are we extracting response cookies again? Perhaps we |
| 996 | // should just leverage response_cookies_. |
| 997 | |
| 998 | cookies->clear(); |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 999 | FetchResponseCookies(cookies); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1000 | return true; |
| 1001 | } |
| 1002 | |
| 1003 | int URLRequestHttpJob::GetResponseCode() const { |
| 1004 | DCHECK(transaction_.get()); |
| 1005 | |
| 1006 | if (!response_info_) |
| 1007 | return -1; |
| 1008 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1009 | return GetResponseHeaders()->response_code(); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1010 | } |
| 1011 | |
[email protected] | 5a3b4d3 | 2011-03-17 01:24:05 | [diff] [blame] | 1012 | Filter* URLRequestHttpJob::SetupFilter() const { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1013 | DCHECK(transaction_.get()); |
| 1014 | if (!response_info_) |
[email protected] | 5a3b4d3 | 2011-03-17 01:24:05 | [diff] [blame] | 1015 | return NULL; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1016 | |
[email protected] | 5a3b4d3 | 2011-03-17 01:24:05 | [diff] [blame] | 1017 | std::vector<Filter::FilterType> encoding_types; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1018 | std::string encoding_type; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1019 | HttpResponseHeaders* headers = GetResponseHeaders(); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1020 | void* iter = NULL; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1021 | while (headers->EnumerateHeader(&iter, "Content-Encoding", &encoding_type)) { |
[email protected] | 5a3b4d3 | 2011-03-17 01:24:05 | [diff] [blame] | 1022 | encoding_types.push_back(Filter::ConvertEncodingToType(encoding_type)); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1023 | } |
| 1024 | |
[email protected] | 46668fe5 | 2011-05-04 19:03:23 | [diff] [blame] | 1025 | if (filter_context_->IsSdchResponse()) { |
| 1026 | // We are wary of proxies that discard or damage SDCH encoding. If a server |
| 1027 | // explicitly states that this is not SDCH content, then we can correct our |
| 1028 | // assumption that this is an SDCH response, and avoid the need to recover |
| 1029 | // as though the content is corrupted (when we discover it is not SDCH |
| 1030 | // encoded). |
| 1031 | std::string sdch_response_status; |
| 1032 | iter = NULL; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1033 | while (headers->EnumerateHeader(&iter, "X-Sdch-Encode", |
| 1034 | &sdch_response_status)) { |
[email protected] | 46668fe5 | 2011-05-04 19:03:23 | [diff] [blame] | 1035 | if (sdch_response_status == "0") { |
| 1036 | filter_context_->ResetSdchResponseToFalse(); |
| 1037 | break; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1042 | // Even if encoding types are empty, there is a chance that we need to add |
| 1043 | // some decoding, as some proxies strip encoding completely. In such cases, |
| 1044 | // we may need to add (for example) SDCH filtering (when the context suggests |
| 1045 | // it is appropriate). |
[email protected] | 2e92354c | 2011-03-25 20:49:53 | [diff] [blame] | 1046 | Filter::FixupEncodingTypes(*filter_context_, &encoding_types); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1047 | |
[email protected] | 5a3b4d3 | 2011-03-17 01:24:05 | [diff] [blame] | 1048 | return !encoding_types.empty() |
[email protected] | 2e92354c | 2011-03-25 20:49:53 | [diff] [blame] | 1049 | ? Filter::Factory(encoding_types, *filter_context_) : NULL; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1050 | } |
| 1051 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1052 | bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame^] | 1053 | // HTTP is always safe. |
| 1054 | // TODO(pauljensen): Remove once crbug.com/146591 is fixed. |
| 1055 | if (location.is_valid() && |
| 1056 | (location.scheme() == "http" || location.scheme() == "https")) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1057 | return true; |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1058 | } |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame^] | 1059 | // Query URLRequestJobFactory as to whether |location| would be safe to |
| 1060 | // redirect to. |
| 1061 | return request_->context()->job_factory() && |
| 1062 | request_->context()->job_factory()->IsSafeRedirectTarget(location); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | bool URLRequestHttpJob::NeedsAuth() { |
| 1066 | int code = GetResponseCode(); |
| 1067 | if (code == -1) |
| 1068 | return false; |
| 1069 | |
| 1070 | // Check if we need either Proxy or WWW Authentication. This could happen |
| 1071 | // because we either provided no auth info, or provided incorrect info. |
| 1072 | switch (code) { |
| 1073 | case 407: |
| 1074 | if (proxy_auth_state_ == AUTH_STATE_CANCELED) |
| 1075 | return false; |
| 1076 | proxy_auth_state_ = AUTH_STATE_NEED_AUTH; |
| 1077 | return true; |
| 1078 | case 401: |
| 1079 | if (server_auth_state_ == AUTH_STATE_CANCELED) |
| 1080 | return false; |
| 1081 | server_auth_state_ = AUTH_STATE_NEED_AUTH; |
| 1082 | return true; |
| 1083 | } |
| 1084 | return false; |
| 1085 | } |
| 1086 | |
| 1087 | void URLRequestHttpJob::GetAuthChallengeInfo( |
| 1088 | scoped_refptr<AuthChallengeInfo>* result) { |
| 1089 | DCHECK(transaction_.get()); |
| 1090 | DCHECK(response_info_); |
| 1091 | |
| 1092 | // sanity checks: |
| 1093 | DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH || |
| 1094 | server_auth_state_ == AUTH_STATE_NEED_AUTH); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1095 | DCHECK((GetResponseHeaders()->response_code() == HTTP_UNAUTHORIZED) || |
| 1096 | (GetResponseHeaders()->response_code() == |
| 1097 | HTTP_PROXY_AUTHENTICATION_REQUIRED)); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1098 | |
| 1099 | *result = response_info_->auth_challenge; |
| 1100 | } |
| 1101 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1102 | void URLRequestHttpJob::SetAuth(const AuthCredentials& credentials) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1103 | DCHECK(transaction_.get()); |
| 1104 | |
| 1105 | // Proxy gets set first, then WWW. |
| 1106 | if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) { |
| 1107 | proxy_auth_state_ = AUTH_STATE_HAVE_AUTH; |
| 1108 | } else { |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1109 | DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1110 | server_auth_state_ = AUTH_STATE_HAVE_AUTH; |
| 1111 | } |
| 1112 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 1113 | RestartTransactionWithAuth(credentials); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | void URLRequestHttpJob::CancelAuth() { |
| 1117 | // Proxy gets set first, then WWW. |
| 1118 | if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) { |
| 1119 | proxy_auth_state_ = AUTH_STATE_CANCELED; |
| 1120 | } else { |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1121 | DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1122 | server_auth_state_ = AUTH_STATE_CANCELED; |
| 1123 | } |
| 1124 | |
| 1125 | // These will be reset in OnStartCompleted. |
| 1126 | response_info_ = NULL; |
| 1127 | response_cookies_.clear(); |
| 1128 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1129 | ResetTimer(); |
| 1130 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1131 | // OK, let the consumer read the error page... |
| 1132 | // |
| 1133 | // Because we set the AUTH_STATE_CANCELED flag, NeedsAuth will return false, |
| 1134 | // which will cause the consumer to receive OnResponseStarted instead of |
| 1135 | // OnAuthRequired. |
| 1136 | // |
| 1137 | // We have to do this via InvokeLater to avoid "recursing" the consumer. |
| 1138 | // |
| 1139 | MessageLoop::current()->PostTask( |
| 1140 | FROM_HERE, |
[email protected] | 098b2911 | 2011-12-20 21:12:34 | [diff] [blame] | 1141 | base::Bind(&URLRequestHttpJob::OnStartCompleted, |
| 1142 | weak_factory_.GetWeakPtr(), OK)); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | void URLRequestHttpJob::ContinueWithCertificate( |
| 1146 | X509Certificate* client_cert) { |
| 1147 | DCHECK(transaction_.get()); |
| 1148 | |
| 1149 | DCHECK(!response_info_) << "should not have a response yet"; |
| 1150 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1151 | ResetTimer(); |
| 1152 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1153 | // No matter what, we want to report our status as IO pending since we will |
| 1154 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 1155 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 1156 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1157 | int rv = transaction_->RestartWithCertificate(client_cert, start_callback_); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1158 | if (rv == ERR_IO_PENDING) |
| 1159 | return; |
| 1160 | |
| 1161 | // The transaction started synchronously, but we need to notify the |
| 1162 | // URLRequest delegate via the message loop. |
| 1163 | MessageLoop::current()->PostTask( |
| 1164 | FROM_HERE, |
[email protected] | 098b2911 | 2011-12-20 21:12:34 | [diff] [blame] | 1165 | base::Bind(&URLRequestHttpJob::OnStartCompleted, |
| 1166 | weak_factory_.GetWeakPtr(), rv)); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | void URLRequestHttpJob::ContinueDespiteLastError() { |
| 1170 | // If the transaction was destroyed, then the job was cancelled. |
| 1171 | if (!transaction_.get()) |
| 1172 | return; |
| 1173 | |
| 1174 | DCHECK(!response_info_) << "should not have a response yet"; |
| 1175 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1176 | ResetTimer(); |
| 1177 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1178 | // No matter what, we want to report our status as IO pending since we will |
| 1179 | // be notifying our consumer asynchronously via OnStartCompleted. |
| 1180 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 1181 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1182 | int rv = transaction_->RestartIgnoringLastError(start_callback_); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1183 | if (rv == ERR_IO_PENDING) |
| 1184 | return; |
| 1185 | |
| 1186 | // The transaction started synchronously, but we need to notify the |
| 1187 | // URLRequest delegate via the message loop. |
| 1188 | MessageLoop::current()->PostTask( |
| 1189 | FROM_HERE, |
[email protected] | 098b2911 | 2011-12-20 21:12:34 | [diff] [blame] | 1190 | base::Bind(&URLRequestHttpJob::OnStartCompleted, |
| 1191 | weak_factory_.GetWeakPtr(), rv)); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1192 | } |
| 1193 | |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1194 | bool URLRequestHttpJob::ShouldFixMismatchedContentLength(int rv) const { |
| 1195 | // Some servers send the body compressed, but specify the content length as |
| 1196 | // the uncompressed size. Although this violates the HTTP spec we want to |
| 1197 | // support it (as IE and FireFox do), but *only* for an exact match. |
| 1198 | // See http://crbug.com/79694. |
[email protected] | 5543cbb | 2012-04-20 16:35:23 | [diff] [blame] | 1199 | if (rv == net::ERR_CONTENT_LENGTH_MISMATCH || |
| 1200 | rv == net::ERR_INCOMPLETE_CHUNKED_ENCODING) { |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1201 | if (request_ && request_->response_headers()) { |
| 1202 | int64 expected_length = request_->response_headers()->GetContentLength(); |
| 1203 | VLOG(1) << __FUNCTION__ << "() " |
| 1204 | << "\"" << request_->url().spec() << "\"" |
| 1205 | << " content-length = " << expected_length |
| 1206 | << " pre total = " << prefilter_bytes_read() |
| 1207 | << " post total = " << postfilter_bytes_read(); |
| 1208 | if (postfilter_bytes_read() == expected_length) { |
| 1209 | // Clear the error. |
| 1210 | return true; |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | return false; |
| 1215 | } |
| 1216 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1217 | bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size, |
[email protected] | b799645 | 2011-10-31 19:30:56 | [diff] [blame] | 1218 | int* bytes_read) { |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1219 | DCHECK_NE(buf_size, 0); |
| 1220 | DCHECK(bytes_read); |
| 1221 | DCHECK(!read_in_progress_); |
| 1222 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1223 | int rv = transaction_->Read( |
| 1224 | buf, buf_size, |
| 1225 | base::Bind(&URLRequestHttpJob::OnReadCompleted, base::Unretained(this))); |
[email protected] | 85c1dce | 2011-07-06 12:01:29 | [diff] [blame] | 1226 | |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1227 | if (ShouldFixMismatchedContentLength(rv)) |
| 1228 | rv = 0; |
| 1229 | |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1230 | if (rv >= 0) { |
| 1231 | *bytes_read = rv; |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1232 | if (!rv) |
| 1233 | DoneWithRequest(FINISHED); |
[email protected] | 5394e42 | 2011-01-20 22:07:43 | [diff] [blame] | 1234 | return true; |
| 1235 | } |
| 1236 | |
| 1237 | if (rv == ERR_IO_PENDING) { |
| 1238 | read_in_progress_ = true; |
| 1239 | SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 1240 | } else { |
| 1241 | NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
| 1242 | } |
| 1243 | |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | void URLRequestHttpJob::StopCaching() { |
| 1248 | if (transaction_.get()) |
| 1249 | transaction_->StopCaching(); |
| 1250 | } |
| 1251 | |
[email protected] | 5c04f72 | 2011-08-12 17:52:47 | [diff] [blame] | 1252 | void URLRequestHttpJob::DoneReading() { |
| 1253 | if (transaction_.get()) |
| 1254 | transaction_->DoneReading(); |
| 1255 | DoneWithRequest(FINISHED); |
| 1256 | } |
| 1257 | |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 1258 | HostPortPair URLRequestHttpJob::GetSocketAddress() const { |
| 1259 | return response_info_ ? response_info_->socket_address : HostPortPair(); |
| 1260 | } |
| 1261 | |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1262 | void URLRequestHttpJob::RecordTimer() { |
| 1263 | if (request_creation_time_.is_null()) { |
| 1264 | NOTREACHED() |
| 1265 | << "The same transaction shouldn't start twice without new timing."; |
| 1266 | return; |
| 1267 | } |
| 1268 | |
[email protected] | 320a29f1 | 2011-03-21 14:47:41 | [diff] [blame] | 1269 | base::TimeDelta to_start = base::Time::Now() - request_creation_time_; |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1270 | request_creation_time_ = base::Time(); |
[email protected] | 8684a881 | 2011-03-22 13:59:38 | [diff] [blame] | 1271 | |
[email protected] | 5c68d69 | 2011-08-24 04:59:41 | [diff] [blame] | 1272 | UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start); |
[email protected] | ec23f52 | 2011-02-22 21:01:38 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | void URLRequestHttpJob::ResetTimer() { |
| 1276 | if (!request_creation_time_.is_null()) { |
| 1277 | NOTREACHED() |
| 1278 | << "The timer was reset before it was recorded."; |
| 1279 | return; |
| 1280 | } |
| 1281 | request_creation_time_ = base::Time::Now(); |
| 1282 | } |
| 1283 | |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1284 | void URLRequestHttpJob::UpdatePacketReadTimes() { |
| 1285 | if (!packet_timing_enabled_) |
| 1286 | return; |
| 1287 | |
| 1288 | if (filter_input_byte_count() <= bytes_observed_in_packets_) { |
| 1289 | DCHECK_EQ(filter_input_byte_count(), bytes_observed_in_packets_); |
| 1290 | return; // No new bytes have arrived. |
| 1291 | } |
| 1292 | |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1293 | final_packet_time_ = base::Time::Now(); |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1294 | if (!bytes_observed_in_packets_) |
| 1295 | request_time_snapshot_ = request_ ? request_->request_time() : base::Time(); |
| 1296 | |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1297 | bytes_observed_in_packets_ = filter_input_byte_count(); |
| 1298 | } |
| 1299 | |
| 1300 | void URLRequestHttpJob::RecordPacketStats( |
| 1301 | FilterContext::StatisticSelector statistic) const { |
| 1302 | if (!packet_timing_enabled_ || (final_packet_time_ == base::Time())) |
| 1303 | return; |
| 1304 | |
| 1305 | base::TimeDelta duration = final_packet_time_ - request_time_snapshot_; |
| 1306 | switch (statistic) { |
| 1307 | case FilterContext::SDCH_DECODE: { |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1308 | UMA_HISTOGRAM_CUSTOM_COUNTS("Sdch3.Network_Decode_Bytes_Processed_b", |
| 1309 | static_cast<int>(bytes_observed_in_packets_), 500, 100000, 100); |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1310 | return; |
| 1311 | } |
| 1312 | case FilterContext::SDCH_PASSTHROUGH: { |
| 1313 | // Despite advertising a dictionary, we handled non-sdch compressed |
| 1314 | // content. |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1315 | return; |
| 1316 | } |
| 1317 | |
| 1318 | case FilterContext::SDCH_EXPERIMENT_DECODE: { |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1319 | UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment2_Decode", |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1320 | duration, |
| 1321 | base::TimeDelta::FromMilliseconds(20), |
| 1322 | base::TimeDelta::FromMinutes(10), 100); |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1323 | return; |
| 1324 | } |
| 1325 | case FilterContext::SDCH_EXPERIMENT_HOLDBACK: { |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1326 | UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment2_Holdback", |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1327 | duration, |
| 1328 | base::TimeDelta::FromMilliseconds(20), |
| 1329 | base::TimeDelta::FromMinutes(10), 100); |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1330 | return; |
| 1331 | } |
| 1332 | default: |
| 1333 | NOTREACHED(); |
| 1334 | return; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | // The common type of histogram we use for all compression-tracking histograms. |
| 1339 | #define COMPRESSION_HISTOGRAM(name, sample) \ |
| 1340 | do { \ |
| 1341 | UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Compress." name, sample, \ |
| 1342 | 500, 1000000, 100); \ |
[email protected] | b799645 | 2011-10-31 19:30:56 | [diff] [blame] | 1343 | } while (0) |
[email protected] | dd29bcd7 | 2011-03-24 00:03:44 | [diff] [blame] | 1344 | |
| 1345 | void URLRequestHttpJob::RecordCompressionHistograms() { |
| 1346 | DCHECK(request_); |
| 1347 | if (!request_) |
| 1348 | return; |
| 1349 | |
| 1350 | if (is_cached_content_ || // Don't record cached content |
| 1351 | !GetStatus().is_success() || // Don't record failed content |
| 1352 | !IsCompressibleContent() || // Only record compressible content |
| 1353 | !prefilter_bytes_read()) // Zero-byte responses aren't useful. |
| 1354 | return; |
| 1355 | |
| 1356 | // Miniature requests aren't really compressible. Don't count them. |
| 1357 | const int kMinSize = 16; |
| 1358 | if (prefilter_bytes_read() < kMinSize) |
| 1359 | return; |
| 1360 | |
| 1361 | // Only record for http or https urls. |
| 1362 | bool is_http = request_->url().SchemeIs("http"); |
| 1363 | bool is_https = request_->url().SchemeIs("https"); |
| 1364 | if (!is_http && !is_https) |
| 1365 | return; |
| 1366 | |
| 1367 | int compressed_B = prefilter_bytes_read(); |
| 1368 | int decompressed_B = postfilter_bytes_read(); |
| 1369 | bool was_filtered = HasFilter(); |
| 1370 | |
| 1371 | // We want to record how often downloaded resources are compressed. |
| 1372 | // But, we recognize that different protocols may have different |
| 1373 | // properties. So, for each request, we'll put it into one of 3 |
| 1374 | // groups: |
| 1375 | // a) SSL resources |
| 1376 | // Proxies cannot tamper with compression headers with SSL. |
| 1377 | // b) Non-SSL, loaded-via-proxy resources |
| 1378 | // In this case, we know a proxy might have interfered. |
| 1379 | // c) Non-SSL, loaded-without-proxy resources |
| 1380 | // In this case, we know there was no explicit proxy. However, |
| 1381 | // it is possible that a transparent proxy was still interfering. |
| 1382 | // |
| 1383 | // For each group, we record the same 3 histograms. |
| 1384 | |
| 1385 | if (is_https) { |
| 1386 | if (was_filtered) { |
| 1387 | COMPRESSION_HISTOGRAM("SSL.BytesBeforeCompression", compressed_B); |
| 1388 | COMPRESSION_HISTOGRAM("SSL.BytesAfterCompression", decompressed_B); |
| 1389 | } else { |
| 1390 | COMPRESSION_HISTOGRAM("SSL.ShouldHaveBeenCompressed", decompressed_B); |
| 1391 | } |
| 1392 | return; |
| 1393 | } |
| 1394 | |
| 1395 | if (request_->was_fetched_via_proxy()) { |
| 1396 | if (was_filtered) { |
| 1397 | COMPRESSION_HISTOGRAM("Proxy.BytesBeforeCompression", compressed_B); |
| 1398 | COMPRESSION_HISTOGRAM("Proxy.BytesAfterCompression", decompressed_B); |
| 1399 | } else { |
| 1400 | COMPRESSION_HISTOGRAM("Proxy.ShouldHaveBeenCompressed", decompressed_B); |
| 1401 | } |
| 1402 | return; |
| 1403 | } |
| 1404 | |
| 1405 | if (was_filtered) { |
| 1406 | COMPRESSION_HISTOGRAM("NoProxy.BytesBeforeCompression", compressed_B); |
| 1407 | COMPRESSION_HISTOGRAM("NoProxy.BytesAfterCompression", decompressed_B); |
| 1408 | } else { |
| 1409 | COMPRESSION_HISTOGRAM("NoProxy.ShouldHaveBeenCompressed", decompressed_B); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | bool URLRequestHttpJob::IsCompressibleContent() const { |
| 1414 | std::string mime_type; |
| 1415 | return GetMimeType(&mime_type) && |
| 1416 | (IsSupportedJavascriptMimeType(mime_type.c_str()) || |
| 1417 | IsSupportedNonImageMimeType(mime_type.c_str())); |
| 1418 | } |
| 1419 | |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1420 | void URLRequestHttpJob::RecordPerfHistograms(CompletionCause reason) { |
| 1421 | if (start_time_.is_null()) |
| 1422 | return; |
| 1423 | |
| 1424 | base::TimeDelta total_time = base::TimeTicks::Now() - start_time_; |
| 1425 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTime", total_time); |
| 1426 | |
| 1427 | if (reason == FINISHED) { |
| 1428 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeSuccess", total_time); |
| 1429 | } else { |
| 1430 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCancel", total_time); |
| 1431 | } |
| 1432 | |
[email protected] | 4b4d2024 | 2012-02-23 18:27:46 | [diff] [blame] | 1433 | if (response_info_) { |
| 1434 | if (response_info_->was_cached) { |
| 1435 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCached", total_time); |
| 1436 | } else { |
| 1437 | UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeNotCached", total_time); |
[email protected] | b73656ca | 2011-07-22 17:42:17 | [diff] [blame] | 1438 | } |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | start_time_ = base::TimeTicks(); |
| 1442 | } |
| 1443 | |
| 1444 | void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) { |
| 1445 | if (done_) |
| 1446 | return; |
| 1447 | done_ = true; |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1448 | RecordPerfHistograms(reason); |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 1449 | if (reason == FINISHED) { |
| 1450 | request_->set_received_response_content_length(prefilter_bytes_read()); |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1451 | RecordCompressionHistograms(); |
[email protected] | 7a299a9 | 2012-10-24 23:54:50 | [diff] [blame] | 1452 | } |
[email protected] | bbaea8f | 2011-06-24 00:11:01 | [diff] [blame] | 1453 | } |
| 1454 | |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 1455 | HttpResponseHeaders* URLRequestHttpJob::GetResponseHeaders() const { |
| 1456 | DCHECK(transaction_.get()); |
| 1457 | DCHECK(transaction_->GetResponseInfo()); |
| 1458 | return override_response_headers_.get() ? |
| 1459 | override_response_headers_ : |
| 1460 | transaction_->GetResponseInfo()->headers; |
| 1461 | } |
| 1462 | |
| 1463 | void URLRequestHttpJob::NotifyURLRequestDestroyed() { |
| 1464 | awaiting_callback_ = false; |
| 1465 | } |
| 1466 | |
[email protected] | 5a07c19 | 2012-07-30 20:18:22 | [diff] [blame] | 1467 | void URLRequestHttpJob::OnDetachRequest() { |
| 1468 | http_transaction_delegate_->OnDetachRequest(); |
| 1469 | } |
| 1470 | |
[email protected] | 4f5656c6 | 2010-12-13 10:47:09 | [diff] [blame] | 1471 | } // namespace net |