blob: 71904a52258023a5f9d7663439604c4d21fe99b9 [file] [log] [blame]
[email protected]fecef222012-01-05 02:26:151// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]175adac2008-07-30 17:28:045#include "net/url_request/url_request_http_job.h"
initial.commit586acc5fe2008-07-26 22:42:526
[email protected]4ed2755f2008-12-15 09:01:337#include "base/base_switches.h"
[email protected]4f9e5c82011-11-17 16:04:568#include "base/bind.h"
[email protected]084262c2011-12-01 21:12:479#include "base/bind_helpers.h"
[email protected]4ed2755f2008-12-15 09:01:3310#include "base/command_line.h"
[email protected]39ce5c02008-08-22 04:03:4411#include "base/compiler_specific.h"
[email protected]60889422008-09-23 01:18:1612#include "base/file_util.h"
13#include "base/file_version_info.h"
initial.commit586acc5fe2008-07-26 22:42:5214#include "base/message_loop.h"
[email protected]8684a8812011-03-22 13:59:3815#include "base/metrics/field_trial.h"
[email protected]ec23f522011-02-22 21:01:3816#include "base/metrics/histogram.h"
[email protected]5b90b5d2009-04-30 23:06:0117#include "base/rand_util.h"
initial.commit586acc5fe2008-07-26 22:42:5218#include "base/string_util.h"
[email protected]dd29bcd72011-03-24 00:03:4419#include "base/time.h"
[email protected]423041b2008-10-27 17:39:2820#include "net/base/filter.h"
[email protected]6d81b482011-02-22 19:47:1921#include "net/base/host_port_pair.h"
[email protected]b8430722008-09-17 20:05:4422#include "net/base/load_flags.h"
[email protected]dd29bcd72011-03-24 00:03:4423#include "net/base/mime_util.h"
initial.commit586acc5fe2008-07-26 22:42:5224#include "net/base/net_errors.h"
25#include "net/base/net_util.h"
[email protected]636eccd2011-06-28 12:28:0126#include "net/base/network_delegate.h"
[email protected]60889422008-09-23 01:18:1627#include "net/base/sdch_manager.h"
[email protected]6e7845ae2013-03-29 21:48:1128#include "net/cert/cert_status_flags.h"
[email protected]aa84a7e2012-03-15 21:29:0629#include "net/cookies/cookie_monster.h"
[email protected]7a299a92012-10-24 23:54:5030#include "net/http/http_network_session.h"
[email protected]8c76ae22010-04-20 22:15:4331#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2132#include "net/http/http_response_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/http/http_response_info.h"
[email protected]9094b602012-02-27 21:44:5834#include "net/http/http_status_code.h"
initial.commit586acc5fe2008-07-26 22:42:5235#include "net/http/http_transaction.h"
[email protected]5a07c192012-07-30 20:18:2236#include "net/http/http_transaction_delegate.h"
initial.commit586acc5fe2008-07-26 22:42:5237#include "net/http/http_transaction_factory.h"
[email protected]0757e7702009-03-27 04:00:2238#include "net/http/http_util.h"
[email protected]536fd0b2013-03-14 17:41:5739#include "net/ssl/ssl_cert_request_info.h"
40#include "net/ssl/ssl_config_service.h"
[email protected]7f269182011-10-16 01:58:3441#include "net/url_request/fraudulent_certificate_reporter.h"
[email protected]ee4c30d2012-11-07 15:08:4342#include "net/url_request/http_user_agent_settings.h"
initial.commit586acc5fe2008-07-26 22:42:5243#include "net/url_request/url_request.h"
[email protected]319d9e6f2009-02-18 19:47:2144#include "net/url_request/url_request_context.h"
initial.commit586acc5fe2008-07-26 22:42:5245#include "net/url_request/url_request_error_job.h"
[email protected]e0f35c92013-05-08 16:04:3446#include "net/url_request/url_request_job_factory.h"
[email protected]06965e02009-09-04 21:36:4247#include "net/url_request/url_request_redirect_job.h"
[email protected]6b3f9642010-11-25 02:29:0648#include "net/url_request/url_request_throttler_header_adapter.h"
49#include "net/url_request/url_request_throttler_manager.h"
initial.commit586acc5fe2008-07-26 22:42:5250
[email protected]8c76ae22010-04-20 22:15:4351static const char kAvailDictionaryHeader[] = "Avail-Dictionary";
52
[email protected]4f5656c62010-12-13 10:47:0953namespace net {
54
[email protected]2e92354c2011-03-25 20:49:5355class URLRequestHttpJob::HttpFilterContext : public FilterContext {
56 public:
57 explicit HttpFilterContext(URLRequestHttpJob* job);
58 virtual ~HttpFilterContext();
59
60 // FilterContext implementation.
[email protected]1e43b6812012-08-19 19:14:0061 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]2e92354c2011-03-25 20:49:5370
[email protected]46668fe52011-05-04 19:03:2371 // 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]2e92354c2011-03-25 20:49:5375 private:
76 URLRequestHttpJob* job_;
77
78 DISALLOW_COPY_AND_ASSIGN(HttpFilterContext);
79};
80
[email protected]5a07c192012-07-30 20:18:2281class URLRequestHttpJob::HttpTransactionDelegateImpl
82 : public HttpTransactionDelegate {
83 public:
[email protected]cc05edc2013-03-08 18:04:4184 HttpTransactionDelegateImpl(
85 URLRequest* request, NetworkDelegate* network_delegate)
[email protected]5a07c192012-07-30 20:18:2286 : request_(request),
[email protected]cc05edc2013-03-08 18:04:4187 network_delegate_(network_delegate),
[email protected]a9e0d1412012-08-20 22:13:0188 cache_active_(false),
89 network_active_(false) {
[email protected]5a07c192012-07-30 20:18:2290 }
91 virtual ~HttpTransactionDelegateImpl() {
92 OnDetachRequest();
93 }
94 void OnDetachRequest() {
95 if (request_ == NULL || network_delegate_ == NULL)
96 return;
[email protected]a9e0d1412012-08-20 22:13:0197 network_delegate_->NotifyRequestWaitStateChange(
[email protected]5a07c192012-07-30 20:18:2298 *request_,
[email protected]a9e0d1412012-08-20 22:13:0199 NetworkDelegate::REQUEST_WAIT_STATE_RESET);
100 cache_active_ = false;
101 network_active_ = false;
[email protected]5a07c192012-07-30 20:18:22102 request_ = NULL;
103 }
104 virtual void OnCacheActionStart() OVERRIDE {
105 if (request_ == NULL || network_delegate_ == NULL)
106 return;
[email protected]a9e0d1412012-08-20 22:13:01107 DCHECK(!cache_active_ && !network_active_);
108 cache_active_ = true;
109 network_delegate_->NotifyRequestWaitStateChange(
[email protected]5a07c192012-07-30 20:18:22110 *request_,
[email protected]a9e0d1412012-08-20 22:13:01111 NetworkDelegate::REQUEST_WAIT_STATE_CACHE_START);
[email protected]5a07c192012-07-30 20:18:22112 }
113 virtual void OnCacheActionFinish() OVERRIDE {
114 if (request_ == NULL || network_delegate_ == NULL)
115 return;
[email protected]a9e0d1412012-08-20 22:13:01116 DCHECK(cache_active_ && !network_active_);
117 cache_active_ = false;
118 network_delegate_->NotifyRequestWaitStateChange(
[email protected]5a07c192012-07-30 20:18:22119 *request_,
[email protected]a9e0d1412012-08-20 22:13:01120 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]5a07c192012-07-30 20:18:22139 }
140 private:
141 URLRequest* request_;
142 NetworkDelegate* network_delegate_;
[email protected]a9e0d1412012-08-20 22:13:01143 bool cache_active_;
144 bool network_active_;
[email protected]5a07c192012-07-30 20:18:22145};
146
[email protected]fc01f232011-03-17 19:06:01147URLRequestHttpJob::HttpFilterContext::HttpFilterContext(URLRequestHttpJob* job)
148 : job_(job) {
149 DCHECK(job_);
150}
151
152URLRequestHttpJob::HttpFilterContext::~HttpFilterContext() {
153}
154
155bool URLRequestHttpJob::HttpFilterContext::GetMimeType(
156 std::string* mime_type) const {
157 return job_->GetMimeType(mime_type);
158}
159
160bool URLRequestHttpJob::HttpFilterContext::GetURL(GURL* gurl) const {
161 if (!job_->request())
162 return false;
163 *gurl = job_->request()->url();
164 return true;
165}
166
167base::Time URLRequestHttpJob::HttpFilterContext::GetRequestTime() const {
168 return job_->request() ? job_->request()->request_time() : base::Time();
169}
170
171bool URLRequestHttpJob::HttpFilterContext::IsCachedContent() const {
[email protected]dd29bcd72011-03-24 00:03:44172 return job_->is_cached_content_;
[email protected]fc01f232011-03-17 19:06:01173}
174
175bool URLRequestHttpJob::HttpFilterContext::IsDownload() const {
176 return (job_->request_info_.load_flags & LOAD_IS_DOWNLOAD) != 0;
177}
178
[email protected]46668fe52011-05-04 19:03:23179void URLRequestHttpJob::HttpFilterContext::ResetSdchResponseToFalse() {
180 DCHECK(job_->sdch_dictionary_advertised_);
181 job_->sdch_dictionary_advertised_ = false;
182}
183
[email protected]fc01f232011-03-17 19:06:01184bool URLRequestHttpJob::HttpFilterContext::IsSdchResponse() const {
185 return job_->sdch_dictionary_advertised_;
186}
187
188int64 URLRequestHttpJob::HttpFilterContext::GetByteReadCount() const {
189 return job_->filter_input_byte_count();
190}
191
192int URLRequestHttpJob::HttpFilterContext::GetResponseCode() const {
193 return job_->GetResponseCode();
194}
195
196void URLRequestHttpJob::HttpFilterContext::RecordPacketStats(
197 StatisticSelector statistic) const {
198 job_->RecordPacketStats(statistic);
199}
200
initial.commit586acc5fe2008-07-26 22:42:52201// TODO(darin): make sure the port blocking code is not lost
initial.commit586acc5fe2008-07-26 22:42:52202// static
[email protected]4f5656c62010-12-13 10:47:09203URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request,
[email protected]9f170462012-08-24 01:06:58204 NetworkDelegate* network_delegate,
[email protected]4f5656c62010-12-13 10:47:09205 const std::string& scheme) {
initial.commit586acc5fe2008-07-26 22:42:52206 DCHECK(scheme == "http" || scheme == "https");
207
[email protected]81293f482012-08-13 19:35:45208 if (!request->context()->http_transaction_factory()) {
initial.commit586acc5fe2008-07-26 22:42:52209 NOTREACHED() << "requires a valid context";
[email protected]9f170462012-08-24 01:06:58210 return new URLRequestErrorJob(
211 request, network_delegate, ERR_INVALID_ARGUMENT);
initial.commit586acc5fe2008-07-26 22:42:52212 }
213
[email protected]ce572df2012-05-04 19:47:17214 GURL redirect_url;
[email protected]8ccc69f2012-11-28 19:52:14215 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]ee4c30d2012-11-07 15:08:43221 return new URLRequestHttpJob(request,
222 network_delegate,
223 request->context()->http_user_agent_settings());
initial.commit586acc5fe2008-07-26 22:42:52224}
225
[email protected]5394e422011-01-20 22:07:43226
[email protected]ee4c30d2012-11-07 15:08:43227URLRequestHttpJob::URLRequestHttpJob(
228 URLRequest* request,
229 NetworkDelegate* network_delegate,
230 const HttpUserAgentSettings* http_user_agent_settings)
[email protected]9f170462012-08-24 01:06:58231 : URLRequestJob(request, network_delegate),
[email protected]5033ab82013-03-22 20:17:46232 priority_(DEFAULT_PRIORITY),
initial.commit586acc5fe2008-07-26 22:42:52233 response_info_(NULL),
[email protected]34602282010-02-03 22:14:15234 response_cookies_save_index_(0),
[email protected]4f5656c62010-12-13 10:47:09235 proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
236 server_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
[email protected]aa249b52013-04-30 01:04:32237 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]3589e552008-08-20 23:11:34242 read_in_progress_(false),
[email protected]2aecf7382009-06-17 04:14:27243 transaction_(NULL),
[email protected]a73a2802012-05-02 19:20:15244 throttling_entry_(NULL),
[email protected]5b90b5d2009-04-30 23:06:01245 sdch_dictionary_advertised_(false),
246 sdch_test_activated_(false),
[email protected]d8fd5132009-05-15 01:06:53247 sdch_test_control_(false),
[email protected]00e48bf2010-12-03 06:15:42248 is_cached_content_(false),
[email protected]ec23f522011-02-22 21:01:38249 request_creation_time_(),
[email protected]dd29bcd72011-03-24 00:03:44250 packet_timing_enabled_(false),
[email protected]bbaea8f2011-06-24 00:11:01251 done_(false),
[email protected]dd29bcd72011-03-24 00:03:44252 bytes_observed_in_packets_(0),
[email protected]dd29bcd72011-03-24 00:03:44253 request_time_snapshot_(),
254 final_packet_time_(),
[email protected]aa249b52013-04-30 01:04:32255 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]5a07c192012-07-30 20:18:22260 awaiting_callback_(false),
[email protected]cc05edc2013-03-08 18:04:41261 http_transaction_delegate_(new HttpTransactionDelegateImpl(
262 request, network_delegate)),
[email protected]ee4c30d2012-11-07 15:08:43263 http_user_agent_settings_(http_user_agent_settings) {
[email protected]a73a2802012-05-02 19:20:15264 URLRequestThrottlerManager* manager = request->context()->throttler_manager();
265 if (manager)
266 throttling_entry_ = manager->RegisterRequestUrl(request->url());
267
[email protected]ec23f522011-02-22 21:01:38268 ResetTimer();
initial.commit586acc5fe2008-07-26 22:42:52269}
270
[email protected]5033ab82013-03-22 20:17:46271URLRequestHttpJob::~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
302void URLRequestHttpJob::SetPriority(RequestPriority priority) {
303 priority_ = priority;
304 if (transaction_)
305 transaction_->SetPriority(priority_);
306}
307
308void URLRequestHttpJob::Start() {
309 DCHECK(!transaction_.get());
310
[email protected]99ecf6e2013-04-10 22:46:13311 // URLRequest::SetReferrer ensures that we do not send username and password
312 // fields in the referrer.
313 GURL referrer(request_->referrer());
[email protected]5033ab82013-03-22 20:17:46314
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
342void 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]175adac2008-07-30 17:28:04353void URLRequestHttpJob::NotifyHeadersComplete() {
initial.commit586acc5fe2008-07-26 22:42:52354 DCHECK(!response_info_);
355
356 response_info_ = transaction_->GetResponseInfo();
357
[email protected]d8fd5132009-05-15 01:06:53358 // 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]a73a2802012-05-02 19:20:15362 if (!is_cached_content_ && throttling_entry_) {
[email protected]ea8141e2011-10-05 13:12:51363 URLRequestThrottlerHeaderAdapter response_adapter(GetResponseHeaders());
[email protected]2fd33ee92011-03-25 22:30:21364 throttling_entry_->UpdateWithResponse(request_info_.url.host(),
365 &response_adapter);
[email protected]6b3f9642010-11-25 02:29:06366 }
367
[email protected]fecef222012-01-05 02:26:15368 // The ordering of these calls is not important.
[email protected]77f6fb432009-09-05 14:21:09369 ProcessStrictTransportSecurityHeader();
[email protected]fecef222012-01-05 02:26:15370 ProcessPublicKeyPinsHeader();
[email protected]a9cea7542009-05-20 04:30:23371
[email protected]fe219872008-09-23 02:17:00372 if (SdchManager::Global() &&
373 SdchManager::Global()->IsInSupportedDomain(request_->url())) {
[email protected]264300242011-11-07 06:03:30374 const std::string name = "Get-Dictionary";
[email protected]60889422008-09-23 01:18:16375 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]ea8141e2011-10-05 13:12:51383 if (GetResponseHeaders()->EnumerateHeader(&iter, name, &url_text)) {
[email protected]d55ad15d2009-02-17 19:40:50384 // request_->url() won't be valid in the destructor, so we use an
385 // alternate copy.
[email protected]dd29bcd72011-03-24 00:03:44386 DCHECK_EQ(request_->url(), request_info_.url);
[email protected]d55ad15d2009-02-17 19:40:50387 // Resolve suggested URL relative to request url.
388 sdch_dictionary_url_ = request_info_.url.Resolve(url_text);
[email protected]60889422008-09-23 01:18:16389 }
390 }
391
[email protected]0757e7702009-03-27 04:00:22392 // 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]87a09a92011-07-14 15:50:50397 // TODO(battre): This breaks the webrequest API for
398 // URLRequestTestHTTP.BasicAuthWithCookies
[email protected]5796dc942011-07-14 19:26:10399 // where OnBeforeSendHeaders -> OnSendHeaders -> OnBeforeSendHeaders
[email protected]87a09a92011-07-14 15:50:50400 // occurs.
[email protected]f3cf9802011-10-28 18:44:58401 RestartTransactionWithAuth(AuthCredentials());
[email protected]0757e7702009-03-27 04:00:22402 return;
403 }
404
[email protected]4f5656c62010-12-13 10:47:09405 URLRequestJob::NotifyHeadersComplete();
initial.commit586acc5fe2008-07-26 22:42:52406}
407
[email protected]85c1dce2011-07-06 12:01:29408void URLRequestHttpJob::NotifyDone(const URLRequestStatus& status) {
[email protected]bbaea8f2011-06-24 00:11:01409 DoneWithRequest(FINISHED);
[email protected]dd29bcd72011-03-24 00:03:44410 URLRequestJob::NotifyDone(status);
411}
412
[email protected]175adac2008-07-30 17:28:04413void URLRequestHttpJob::DestroyTransaction() {
[email protected]c6a4eb92010-03-03 23:51:19414 DCHECK(transaction_.get());
initial.commit586acc5fe2008-07-26 22:42:52415
[email protected]bbaea8f2011-06-24 00:11:01416 DoneWithRequest(ABORTED);
[email protected]af4876d2008-10-21 23:10:57417 transaction_.reset();
initial.commit586acc5fe2008-07-26 22:42:52418 response_info_ = NULL;
419}
420
[email protected]175adac2008-07-30 17:28:04421void URLRequestHttpJob::StartTransaction() {
[email protected]cc05edc2013-03-08 18:04:41422 if (network_delegate()) {
423 int rv = network_delegate()->NotifyBeforeSendHeaders(
[email protected]084262c2011-12-01 21:12:47424 request_, notify_before_headers_sent_callback_,
[email protected]636eccd2011-06-28 12:28:01425 &request_info_.extra_headers);
426 // If an extension blocks the request, we rely on the callback to
[email protected]b4438d32012-09-27 06:15:30427 // MaybeStartTransactionInternal().
[email protected]636eccd2011-06-28 12:28:01428 if (rv == ERR_IO_PENDING) {
[email protected]9c235f042011-08-10 22:28:21429 SetBlockedOnDelegate();
[email protected]636eccd2011-06-28 12:28:01430 return;
431 }
[email protected]b4438d32012-09-27 06:15:30432 MaybeStartTransactionInternal(rv);
433 return;
[email protected]636eccd2011-06-28 12:28:01434 }
435 StartTransactionInternal();
436}
437
438void URLRequestHttpJob::NotifyBeforeSendHeadersCallback(int result) {
[email protected]9c235f042011-08-10 22:28:21439 SetUnblockedOnDelegate();
[email protected]636eccd2011-06-28 12:28:01440
[email protected]9045b8822012-01-13 20:35:35441 // Check that there are no callbacks to already canceled requests.
442 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
443
[email protected]b4438d32012-09-27 06:15:30444 MaybeStartTransactionInternal(result);
445}
446
447void URLRequestHttpJob::MaybeStartTransactionInternal(int result) {
[email protected]636eccd2011-06-28 12:28:01448 if (result == OK) {
449 StartTransactionInternal();
450 } else {
[email protected]55b8a6c12012-06-13 22:03:42451 std::string source("delegate");
[email protected]636eccd2011-06-28 12:28:01452 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42453 NetLog::StringCallback("source", &source));
[email protected]636eccd2011-06-28 12:28:01454 NotifyCanceled();
[email protected]b4438d32012-09-27 06:15:30455 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
[email protected]636eccd2011-06-28 12:28:01456 }
457}
458
459void URLRequestHttpJob::StartTransactionInternal() {
initial.commit586acc5fe2008-07-26 22:42:52460 // NOTE: This method assumes that request_info_ is already setup properly.
461
[email protected]34602282010-02-03 22:14:15462 // If we already have a transaction, then we should restart the transaction
[email protected]f3cf9802011-10-28 18:44:58463 // with auth provided by auth_credentials_.
initial.commit586acc5fe2008-07-26 22:42:52464
[email protected]99c07902010-08-17 18:59:52465 int rv;
[email protected]6b3f9642010-11-25 02:29:06466
[email protected]cc05edc2013-03-08 18:04:41467 if (network_delegate()) {
468 network_delegate()->NotifySendHeaders(
[email protected]5796dc942011-07-14 19:26:10469 request_, request_info_.extra_headers);
470 }
471
[email protected]34602282010-02-03 22:14:15472 if (transaction_.get()) {
[email protected]49639fa2011-12-20 23:22:41473 rv = transaction_->RestartWithAuth(auth_credentials_, start_callback_);
[email protected]f3cf9802011-10-28 18:44:58474 auth_credentials_ = AuthCredentials();
[email protected]34602282010-02-03 22:14:15475 } else {
[email protected]34602282010-02-03 22:14:15476 DCHECK(request_->context()->http_transaction_factory());
initial.commit586acc5fe2008-07-26 22:42:52477
[email protected]99c07902010-08-17 18:59:52478 rv = request_->context()->http_transaction_factory()->CreateTransaction(
[email protected]5033ab82013-03-22 20:17:46479 priority_, &transaction_, http_transaction_delegate_.get());
[email protected]4f5656c62010-12-13 10:47:09480 if (rv == OK) {
[email protected]a1d4ab072012-06-07 13:21:15481 if (!throttling_entry_ ||
482 !throttling_entry_->ShouldRejectRequest(*request_)) {
[email protected]227b0e82011-03-25 21:11:53483 rv = transaction_->Start(
[email protected]49639fa2011-12-20 23:22:41484 &request_info_, start_callback_, request_->net_log());
[email protected]bbaea8f2011-06-24 00:11:01485 start_time_ = base::TimeTicks::Now();
[email protected]227b0e82011-03-25 21:11:53486 } else {
487 // Special error code for the exponential back-off module.
488 rv = ERR_TEMPORARILY_THROTTLED;
489 }
[email protected]34602282010-02-03 22:14:15490 }
initial.commit586acc5fe2008-07-26 22:42:52491 }
492
[email protected]4f5656c62010-12-13 10:47:09493 if (rv == ERR_IO_PENDING)
[email protected]34602282010-02-03 22:14:15494 return;
495
initial.commit586acc5fe2008-07-26 22:42:52496 // The transaction started synchronously, but we need to notify the
[email protected]4f5656c62010-12-13 10:47:09497 // URLRequest delegate via the message loop.
[email protected]00e48bf2010-12-03 06:15:42498 MessageLoop::current()->PostTask(
499 FROM_HERE,
[email protected]098b29112011-12-20 21:12:34500 base::Bind(&URLRequestHttpJob::OnStartCompleted,
501 weak_factory_.GetWeakPtr(), rv));
initial.commit586acc5fe2008-07-26 22:42:52502}
503
[email protected]175adac2008-07-30 17:28:04504void URLRequestHttpJob::AddExtraHeaders() {
[email protected]c7bef94c2011-06-21 18:05:51505 // 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]5b90b5d2009-04-30 23:06:01520
[email protected]c7bef94c2011-06-21 18:05:51521 // 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]5b90b5d2009-04-30 23:06:01537 }
538 }
[email protected]5b90b5d2009-04-30 23:06:01539
[email protected]c7bef94c2011-06-21 18:05:51540 // 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]8c76ae22010-04-20 22:15:43547 request_info_.extra_headers.SetHeader(
[email protected]c7bef94c2011-06-21 18:05:51548 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]423041b2008-10-27 17:39:28565 }
[email protected]423041b2008-10-27 17:39:28566 }
567
[email protected]ee4c30d2012-11-07 15:08:43568 if (http_user_agent_settings_) {
[email protected]84f05432013-03-15 01:00:12569 // Only add default Accept-Language if the request didn't have it
570 // specified.
[email protected]ee4c30d2012-11-07 15:08:43571 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.commit586acc5fe2008-07-26 22:42:52578 }
initial.commit586acc5fe2008-07-26 22:42:52579}
580
[email protected]34602282010-02-03 22:14:15581void 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]861fcd52009-08-26 02:33:46585
[email protected]ed24fad2011-05-10 22:44:01586 // If the request was destroyed, then there is no more work to do.
587 if (!request_)
588 return;
[email protected]34602282010-02-03 22:14:15589
[email protected]81293f482012-08-13 19:35:45590 CookieStore* cookie_store = request_->context()->cookie_store();
[email protected]1a6fff52011-10-20 21:00:16591 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]098b29112011-12-20 21:12:34597 weak_factory_.GetWeakPtr()));
[email protected]1a6fff52011-10-20 21:00:16598 } else {
[email protected]cec80c3d2012-12-04 12:17:41599 CheckCookiePolicyAndLoad(CookieList());
[email protected]1a6fff52011-10-20 21:00:16600 }
[email protected]03d845f2011-07-29 19:06:26601 } else {
602 DoStartTransaction();
603 }
604}
605
[email protected]1a6fff52011-10-20 21:00:16606void URLRequestHttpJob::DoLoadCookies() {
607 CookieOptions options;
608 options.set_include_httponly();
[email protected]dedec0b2013-02-28 04:50:10609 request_->context()->cookie_store()->GetCookiesWithOptionsAsync(
[email protected]1a6fff52011-10-20 21:00:16610 request_->url(), options,
611 base::Bind(&URLRequestHttpJob::OnCookiesLoaded,
[email protected]098b29112011-12-20 21:12:34612 weak_factory_.GetWeakPtr()));
[email protected]1a6fff52011-10-20 21:00:16613}
614
[email protected]03d845f2011-07-29 19:06:26615void URLRequestHttpJob::CheckCookiePolicyAndLoad(
616 const CookieList& cookie_list) {
[email protected]1a6fff52011-10-20 21:00:16617 if (CanGetCookies(cookie_list))
618 DoLoadCookies();
619 else
[email protected]54f4c9362011-07-25 21:54:46620 DoStartTransaction();
[email protected]54f4c9362011-07-25 21:54:46621}
622
[email protected]dedec0b2013-02-28 04:50:10623void URLRequestHttpJob::OnCookiesLoaded(const std::string& cookie_line) {
[email protected]218aa6a12011-09-13 17:38:38624 if (!cookie_line.empty()) {
[email protected]54f4c9362011-07-25 21:54:46625 request_info_.extra_headers.SetHeader(
[email protected]218aa6a12011-09-13 17:38:38626 HttpRequestHeaders::kCookie, cookie_line);
[email protected]54f4c9362011-07-25 21:54:46627 }
[email protected]54f4c9362011-07-25 21:54:46628 DoStartTransaction();
629}
630
631void URLRequestHttpJob::DoStartTransaction() {
[email protected]03d845f2011-07-29 19:06:26632 // We may have been canceled while retrieving cookies.
[email protected]9025016c2011-05-12 15:51:23633 if (GetStatus().is_success()) {
634 StartTransaction();
635 } else {
636 NotifyCanceled();
637 }
[email protected]0757e7702009-03-27 04:00:22638}
639
[email protected]ea8141e2011-10-05 13:12:51640void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
641 if (result != net::OK) {
[email protected]55b8a6c12012-06-13 22:03:42642 std::string source("delegate");
[email protected]ea8141e2011-10-05 13:12:51643 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42644 NetLog::StringCallback("source", &source));
[email protected]ea8141e2011-10-05 13:12:51645 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
646 return;
647 }
648
[email protected]34602282010-02-03 22:14:15649 DCHECK(transaction_.get());
initial.commit586acc5fe2008-07-26 22:42:52650
[email protected]4f5656c62010-12-13 10:47:09651 const HttpResponseInfo* response_info = transaction_->GetResponseInfo();
[email protected]34602282010-02-03 22:14:15652 DCHECK(response_info);
653
654 response_cookies_.clear();
655 response_cookies_save_index_ = 0;
656
[email protected]ea8141e2011-10-05 13:12:51657 FetchResponseCookies(&response_cookies_);
[email protected]34602282010-02-03 22:14:15658
[email protected]5095cd72012-11-01 10:29:16659 if (!GetResponseHeaders()->GetDateValue(&response_date_))
660 response_date_ = base::Time();
661
[email protected]34602282010-02-03 22:14:15662 // Now, loop over the response cookies, and attempt to persist each.
663 SaveNextCookie();
664}
665
[email protected]263163f2012-06-14 22:40:34666// 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]34602282010-02-03 22:14:15672void URLRequestHttpJob::SaveNextCookie() {
[email protected]263163f2012-06-14 22:40:34673 // 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]5095cd72012-11-01 10:29:16688 options.set_server_time(response_date_);
[email protected]263163f2012-06-14 22:40:34689
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]34602282010-02-03 22:14:15714 response_cookies_.clear();
715 response_cookies_save_index_ = 0;
716 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
717 NotifyHeadersComplete();
718 return;
719 }
[email protected]263163f2012-06-14 22:40:34720}
[email protected]34602282010-02-03 22:14:15721
[email protected]263163f2012-06-14 22:40:34722// |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.
729void 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]34602282010-02-03 22:14:15734
[email protected]263163f2012-06-14 22:40:34735 // If we were called synchronously, return.
736 if (save_next_cookie_running->data) {
737 return;
[email protected]34602282010-02-03 22:14:15738 }
739
[email protected]263163f2012-06-14 22:40:34740 // We were called asynchronously, so trigger the next save.
[email protected]9025016c2011-05-12 15:51:23741 // We may have been canceled within OnSetCookie.
742 if (GetStatus().is_success()) {
743 SaveNextCookie();
744 } else {
745 NotifyCanceled();
746 }
[email protected]34602282010-02-03 22:14:15747}
748
749void URLRequestHttpJob::FetchResponseCookies(
[email protected]34602282010-02-03 22:14:15750 std::vector<std::string>* cookies) {
[email protected]264300242011-11-07 06:03:30751 const std::string name = "Set-Cookie";
initial.commit586acc5fe2008-07-26 22:42:52752 std::string value;
753
754 void* iter = NULL;
[email protected]ea8141e2011-10-05 13:12:51755 HttpResponseHeaders* headers = GetResponseHeaders();
756 while (headers->EnumerateHeader(&iter, name, &value)) {
[email protected]2adf2882010-09-27 08:30:37757 if (!value.empty())
758 cookies->push_back(value);
759 }
initial.commit586acc5fe2008-07-26 22:42:52760}
[email protected]a9cea7542009-05-20 04:30:23761
[email protected]000d9df02012-01-18 20:01:46762// NOTE: |ProcessStrictTransportSecurityHeader| and
763// |ProcessPublicKeyPinsHeader| have very similar structures, by design.
[email protected]77f6fb432009-09-05 14:21:09764void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() {
[email protected]a9cea7542009-05-20 04:30:23765 DCHECK(response_info_);
[email protected]6ed72be2013-01-08 22:07:33766 TransportSecurityState* security_state =
767 request_->context()->transport_security_state();
[email protected]e88006f2012-01-11 06:15:07768 const SSLInfo& ssl_info = response_info_->ssl_info;
[email protected]a9cea7542009-05-20 04:30:23769
[email protected]6ed72be2013-01-08 22:07:33770 // Only accept HSTS headers on HTTPS connections that have no
771 // certificate errors.
[email protected]e88006f2012-01-11 06:15:07772 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
[email protected]6ed72be2013-01-08 22:07:33773 !security_state)
[email protected]e88006f2012-01-11 06:15:07774 return;
[email protected]326e6792009-12-11 21:04:42775
[email protected]242d8562012-10-30 21:20:46776 // 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]6ed72be2013-01-08 22:07:33781 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]a9cea7542009-05-20 04:30:23785}
[email protected]4f5656c62010-12-13 10:47:09786
[email protected]fecef222012-01-05 02:26:15787void URLRequestHttpJob::ProcessPublicKeyPinsHeader() {
788 DCHECK(response_info_);
[email protected]6ed72be2013-01-08 22:07:33789 TransportSecurityState* security_state =
790 request_->context()->transport_security_state();
[email protected]fecef222012-01-05 02:26:15791 const SSLInfo& ssl_info = response_info_->ssl_info;
792
[email protected]6ed72be2013-01-08 22:07:33793 // Only accept HPKP headers on HTTPS connections that have no
[email protected]e88006f2012-01-11 06:15:07794 // certificate errors.
[email protected]fecef222012-01-05 02:26:15795 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
[email protected]6ed72be2013-01-08 22:07:33796 !security_state)
[email protected]fecef222012-01-05 02:26:15797 return;
[email protected]fecef222012-01-05 02:26:15798
[email protected]6ed72be2013-01-08 22:07:33799 // 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]fecef222012-01-05 02:26:15804 HttpResponseHeaders* headers = GetResponseHeaders();
[email protected]fecef222012-01-05 02:26:15805 std::string value;
[email protected]6ed72be2013-01-08 22:07:33806 if (headers->EnumerateHeader(NULL, "Public-Key-Pins", &value))
807 security_state->AddHPKPHeader(request_info_.url.host(), value, ssl_info);
[email protected]fecef222012-01-05 02:26:15808}
809
[email protected]5394e422011-01-20 22:07:43810void URLRequestHttpJob::OnStartCompleted(int result) {
[email protected]ec23f522011-02-22 21:01:38811 RecordTimer();
812
[email protected]5394e422011-01-20 22:07:43813 // If the request was destroyed, then there is no more work to do.
[email protected]a83dd332011-07-13 10:41:01814 if (!request_)
[email protected]5394e422011-01-20 22:07:43815 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]ef2bf422012-05-11 03:27:09825 const URLRequestContext* context = request_->context();
826
[email protected]f3572592011-12-12 21:36:31827 if (result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN &&
828 transaction_->GetResponseInfo() != NULL) {
829 FraudulentCertificateReporter* reporter =
[email protected]ef2bf422012-05-11 03:27:09830 context->fraudulent_certificate_reporter();
[email protected]f3572592011-12-12 21:36:31831 if (reporter != NULL) {
832 const SSLInfo& ssl_info = transaction_->GetResponseInfo()->ssl_info;
[email protected]551ff5172011-11-10 18:35:31833 bool sni_available = SSLConfigService::IsSNIAvailable(
[email protected]ef2bf422012-05-11 03:27:09834 context->ssl_config_service());
[email protected]f3572592011-12-12 21:36:31835 const std::string& host = request_->url().host();
[email protected]b7996452011-10-31 19:30:56836
[email protected]f3572592011-12-12 21:36:31837 reporter->SendReport(host, ssl_info, sni_available);
[email protected]381e8852011-04-14 14:30:58838 }
839 }
[email protected]551ff5172011-11-10 18:35:31840
[email protected]5394e422011-01-20 22:07:43841 if (result == OK) {
[email protected]ea8141e2011-10-05 13:12:51842 scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders();
[email protected]cc05edc2013-03-08 18:04:41843 if (network_delegate()) {
[email protected]ea8141e2011-10-05 13:12:51844 // Note that |this| may not be deleted until
845 // |on_headers_received_callback_| or
846 // |NetworkDelegate::URLRequestDestroyed()| has been called.
[email protected]cc05edc2013-03-08 18:04:41847 int error = network_delegate()->NotifyHeadersReceived(
848 request_, on_headers_received_callback_,
849 headers, &override_response_headers_);
[email protected]ea8141e2011-10-05 13:12:51850 if (error != net::OK) {
851 if (error == net::ERR_IO_PENDING) {
852 awaiting_callback_ = true;
[email protected]0a12e452012-11-14 05:04:30853 SetBlockedOnDelegate();
[email protected]ea8141e2011-10-05 13:12:51854 } else {
[email protected]55b8a6c12012-06-13 22:03:42855 std::string source("delegate");
[email protected]ea8141e2011-10-05 13:12:51856 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42857 NetLog::StringCallback("source",
858 &source));
[email protected]ea8141e2011-10-05 13:12:51859 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, error));
860 }
861 return;
862 }
863 }
864
865 SaveCookiesAndNotifyHeadersComplete(net::OK);
[email protected]e5624f02011-09-27 19:43:53866 } else if (IsCertificateError(result)) {
[email protected]5394e422011-01-20 22:07:43867 // We encountered an SSL certificate error. Ask our delegate to decide
868 // what we should do.
[email protected]e5624f02011-09-27 19:43:53869
870 TransportSecurityState::DomainState domain_state;
[email protected]ef2bf422012-05-11 03:27:09871 const URLRequestContext* context = request_->context();
[email protected]843329e2013-01-22 22:06:09872 const bool fatal = context->transport_security_state() &&
[email protected]ef2bf422012-05-11 03:27:09873 context->transport_security_state()->GetDomainState(
[email protected]f43b89f32012-05-01 19:39:48874 request_info_.url.host(),
[email protected]ef2bf422012-05-11 03:27:09875 SSLConfigService::IsSNIAvailable(context->ssl_config_service()),
[email protected]843329e2013-01-22 22:06:09876 &domain_state) &&
877 domain_state.ShouldSSLErrorsBeFatal();
[email protected]46d117e2012-01-18 01:53:14878 NotifySSLCertificateError(transaction_->GetResponseInfo()->ssl_info, fatal);
[email protected]5394e422011-01-20 22:07:43879 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]a83dd332011-07-13 10:41:01880 NotifyCertificateRequested(
881 transaction_->GetResponseInfo()->cert_request_info);
[email protected]5394e422011-01-20 22:07:43882 } else {
883 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
884 }
885}
886
[email protected]ea8141e2011-10-05 13:12:51887void URLRequestHttpJob::OnHeadersReceivedCallback(int result) {
[email protected]0a12e452012-11-14 05:04:30888 SetUnblockedOnDelegate();
[email protected]ea8141e2011-10-05 13:12:51889 awaiting_callback_ = false;
[email protected]9045b8822012-01-13 20:35:35890
891 // Check that there are no callbacks to already canceled requests.
892 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
893
[email protected]ea8141e2011-10-05 13:12:51894 SaveCookiesAndNotifyHeadersComplete(result);
895}
896
[email protected]5394e422011-01-20 22:07:43897void URLRequestHttpJob::OnReadCompleted(int result) {
898 read_in_progress_ = false;
899
[email protected]f001bd6a2011-12-08 04:31:37900 if (ShouldFixMismatchedContentLength(result))
[email protected]5543cbb2012-04-20 16:35:23901 result = OK;
[email protected]f001bd6a2011-12-08 04:31:37902
[email protected]5543cbb2012-04-20 16:35:23903 if (result == OK) {
[email protected]5394e422011-01-20 22:07:43904 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]5394e422011-01-20 22:07:43915void URLRequestHttpJob::RestartTransactionWithAuth(
[email protected]f3cf9802011-10-28 18:44:58916 const AuthCredentials& credentials) {
917 auth_credentials_ = credentials;
[email protected]5394e422011-01-20 22:07:43918
919 // These will be reset in OnStartCompleted.
920 response_info_ = NULL;
921 response_cookies_.clear();
922
[email protected]ec23f522011-02-22 21:01:38923 ResetTimer();
924
[email protected]5394e422011-01-20 22:07:43925 // 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]ea8141e2011-10-05 13:12:51928 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kCookie);
[email protected]5394e422011-01-20 22:07:43929
930 AddCookieHeaderAndStart();
931}
932
[email protected]0736d9e2012-11-28 19:50:40933void URLRequestHttpJob::SetUpload(UploadDataStream* upload) {
[email protected]5394e422011-01-20 22:07:43934 DCHECK(!transaction_.get()) << "cannot change once started";
[email protected]0736d9e2012-11-28 19:50:40935 request_info_.upload_data_stream = upload;
[email protected]5394e422011-01-20 22:07:43936}
937
938void 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]5394e422011-01-20 22:07:43944LoadState URLRequestHttpJob::GetLoadState() const {
945 return transaction_.get() ?
946 transaction_->GetLoadState() : LOAD_STATE_IDLE;
947}
948
[email protected]7335ab02012-08-30 22:30:42949UploadProgress URLRequestHttpJob::GetUploadProgress() const {
950 return transaction_.get() ?
951 transaction_->GetUploadProgress() : UploadProgress();
[email protected]5394e422011-01-20 22:07:43952}
953
954bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const {
955 DCHECK(transaction_.get());
956
957 if (!response_info_)
958 return false;
959
[email protected]ea8141e2011-10-05 13:12:51960 return GetResponseHeaders()->GetMimeType(mime_type);
[email protected]5394e422011-01-20 22:07:43961}
962
963bool URLRequestHttpJob::GetCharset(std::string* charset) {
964 DCHECK(transaction_.get());
965
966 if (!response_info_)
967 return false;
968
[email protected]ea8141e2011-10-05 13:12:51969 return GetResponseHeaders()->GetCharset(charset);
[email protected]5394e422011-01-20 22:07:43970}
971
972void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) {
973 DCHECK(request_);
974 DCHECK(transaction_.get());
975
[email protected]ea8141e2011-10-05 13:12:51976 if (response_info_) {
[email protected]5394e422011-01-20 22:07:43977 *info = *response_info_;
[email protected]ea8141e2011-10-05 13:12:51978 if (override_response_headers_)
979 info->headers = override_response_headers_;
980 }
[email protected]5394e422011-01-20 22:07:43981}
982
[email protected]58e32bb2013-01-21 18:23:25983void URLRequestHttpJob::GetLoadTimingInfo(
984 LoadTimingInfo* load_timing_info) const {
985 if (transaction_)
986 transaction_->GetLoadTimingInfo(load_timing_info);
987}
988
[email protected]ea8141e2011-10-05 13:12:51989bool URLRequestHttpJob::GetResponseCookies(std::vector<std::string>* cookies) {
[email protected]5394e422011-01-20 22:07:43990 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]ea8141e2011-10-05 13:12:51999 FetchResponseCookies(cookies);
[email protected]5394e422011-01-20 22:07:431000 return true;
1001}
1002
1003int URLRequestHttpJob::GetResponseCode() const {
1004 DCHECK(transaction_.get());
1005
1006 if (!response_info_)
1007 return -1;
1008
[email protected]ea8141e2011-10-05 13:12:511009 return GetResponseHeaders()->response_code();
[email protected]5394e422011-01-20 22:07:431010}
1011
[email protected]5a3b4d32011-03-17 01:24:051012Filter* URLRequestHttpJob::SetupFilter() const {
[email protected]5394e422011-01-20 22:07:431013 DCHECK(transaction_.get());
1014 if (!response_info_)
[email protected]5a3b4d32011-03-17 01:24:051015 return NULL;
[email protected]5394e422011-01-20 22:07:431016
[email protected]5a3b4d32011-03-17 01:24:051017 std::vector<Filter::FilterType> encoding_types;
[email protected]5394e422011-01-20 22:07:431018 std::string encoding_type;
[email protected]ea8141e2011-10-05 13:12:511019 HttpResponseHeaders* headers = GetResponseHeaders();
[email protected]5394e422011-01-20 22:07:431020 void* iter = NULL;
[email protected]ea8141e2011-10-05 13:12:511021 while (headers->EnumerateHeader(&iter, "Content-Encoding", &encoding_type)) {
[email protected]5a3b4d32011-03-17 01:24:051022 encoding_types.push_back(Filter::ConvertEncodingToType(encoding_type));
[email protected]5394e422011-01-20 22:07:431023 }
1024
[email protected]46668fe52011-05-04 19:03:231025 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]ea8141e2011-10-05 13:12:511033 while (headers->EnumerateHeader(&iter, "X-Sdch-Encode",
1034 &sdch_response_status)) {
[email protected]46668fe52011-05-04 19:03:231035 if (sdch_response_status == "0") {
1036 filter_context_->ResetSdchResponseToFalse();
1037 break;
1038 }
1039 }
1040 }
1041
[email protected]5394e422011-01-20 22:07:431042 // 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]2e92354c2011-03-25 20:49:531046 Filter::FixupEncodingTypes(*filter_context_, &encoding_types);
[email protected]5394e422011-01-20 22:07:431047
[email protected]5a3b4d32011-03-17 01:24:051048 return !encoding_types.empty()
[email protected]2e92354c2011-03-25 20:49:531049 ? Filter::Factory(encoding_types, *filter_context_) : NULL;
[email protected]5394e422011-01-20 22:07:431050}
1051
[email protected]5394e422011-01-20 22:07:431052bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) {
[email protected]e0f35c92013-05-08 16:04:341053 // 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]5394e422011-01-20 22:07:431057 return true;
[email protected]5394e422011-01-20 22:07:431058 }
[email protected]e0f35c92013-05-08 16:04:341059 // 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]5394e422011-01-20 22:07:431063}
1064
1065bool 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
1087void 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]9094b602012-02-27 21:44:581095 DCHECK((GetResponseHeaders()->response_code() == HTTP_UNAUTHORIZED) ||
1096 (GetResponseHeaders()->response_code() ==
1097 HTTP_PROXY_AUTHENTICATION_REQUIRED));
[email protected]5394e422011-01-20 22:07:431098
1099 *result = response_info_->auth_challenge;
1100}
1101
[email protected]f3cf9802011-10-28 18:44:581102void URLRequestHttpJob::SetAuth(const AuthCredentials& credentials) {
[email protected]5394e422011-01-20 22:07:431103 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]dd29bcd72011-03-24 00:03:441109 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
[email protected]5394e422011-01-20 22:07:431110 server_auth_state_ = AUTH_STATE_HAVE_AUTH;
1111 }
1112
[email protected]f3cf9802011-10-28 18:44:581113 RestartTransactionWithAuth(credentials);
[email protected]5394e422011-01-20 22:07:431114}
1115
1116void 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]dd29bcd72011-03-24 00:03:441121 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
[email protected]5394e422011-01-20 22:07:431122 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]ec23f522011-02-22 21:01:381129 ResetTimer();
1130
[email protected]5394e422011-01-20 22:07:431131 // 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]098b29112011-12-20 21:12:341141 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1142 weak_factory_.GetWeakPtr(), OK));
[email protected]5394e422011-01-20 22:07:431143}
1144
1145void URLRequestHttpJob::ContinueWithCertificate(
1146 X509Certificate* client_cert) {
1147 DCHECK(transaction_.get());
1148
1149 DCHECK(!response_info_) << "should not have a response yet";
1150
[email protected]ec23f522011-02-22 21:01:381151 ResetTimer();
1152
[email protected]5394e422011-01-20 22:07:431153 // 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]49639fa2011-12-20 23:22:411157 int rv = transaction_->RestartWithCertificate(client_cert, start_callback_);
[email protected]5394e422011-01-20 22:07:431158 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]098b29112011-12-20 21:12:341165 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1166 weak_factory_.GetWeakPtr(), rv));
[email protected]5394e422011-01-20 22:07:431167}
1168
1169void 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]ec23f522011-02-22 21:01:381176 ResetTimer();
1177
[email protected]5394e422011-01-20 22:07:431178 // 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]49639fa2011-12-20 23:22:411182 int rv = transaction_->RestartIgnoringLastError(start_callback_);
[email protected]5394e422011-01-20 22:07:431183 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]098b29112011-12-20 21:12:341190 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1191 weak_factory_.GetWeakPtr(), rv));
[email protected]5394e422011-01-20 22:07:431192}
1193
[email protected]f001bd6a2011-12-08 04:31:371194bool 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]5543cbb2012-04-20 16:35:231199 if (rv == net::ERR_CONTENT_LENGTH_MISMATCH ||
1200 rv == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
[email protected]f001bd6a2011-12-08 04:31:371201 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]5394e422011-01-20 22:07:431217bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size,
[email protected]b7996452011-10-31 19:30:561218 int* bytes_read) {
[email protected]5394e422011-01-20 22:07:431219 DCHECK_NE(buf_size, 0);
1220 DCHECK(bytes_read);
1221 DCHECK(!read_in_progress_);
1222
[email protected]49639fa2011-12-20 23:22:411223 int rv = transaction_->Read(
1224 buf, buf_size,
1225 base::Bind(&URLRequestHttpJob::OnReadCompleted, base::Unretained(this)));
[email protected]85c1dce2011-07-06 12:01:291226
[email protected]f001bd6a2011-12-08 04:31:371227 if (ShouldFixMismatchedContentLength(rv))
1228 rv = 0;
1229
[email protected]5394e422011-01-20 22:07:431230 if (rv >= 0) {
1231 *bytes_read = rv;
[email protected]bbaea8f2011-06-24 00:11:011232 if (!rv)
1233 DoneWithRequest(FINISHED);
[email protected]5394e422011-01-20 22:07:431234 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
1247void URLRequestHttpJob::StopCaching() {
1248 if (transaction_.get())
1249 transaction_->StopCaching();
1250}
1251
[email protected]5c04f722011-08-12 17:52:471252void URLRequestHttpJob::DoneReading() {
1253 if (transaction_.get())
1254 transaction_->DoneReading();
1255 DoneWithRequest(FINISHED);
1256}
1257
[email protected]6d81b482011-02-22 19:47:191258HostPortPair URLRequestHttpJob::GetSocketAddress() const {
1259 return response_info_ ? response_info_->socket_address : HostPortPair();
1260}
1261
[email protected]ec23f522011-02-22 21:01:381262void 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]320a29f12011-03-21 14:47:411269 base::TimeDelta to_start = base::Time::Now() - request_creation_time_;
[email protected]ec23f522011-02-22 21:01:381270 request_creation_time_ = base::Time();
[email protected]8684a8812011-03-22 13:59:381271
[email protected]5c68d692011-08-24 04:59:411272 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start);
[email protected]ec23f522011-02-22 21:01:381273}
1274
1275void 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]dd29bcd72011-03-24 00:03:441284void 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]d6b55392011-08-05 04:04:351293 final_packet_time_ = base::Time::Now();
[email protected]dd29bcd72011-03-24 00:03:441294 if (!bytes_observed_in_packets_)
1295 request_time_snapshot_ = request_ ? request_->request_time() : base::Time();
1296
[email protected]dd29bcd72011-03-24 00:03:441297 bytes_observed_in_packets_ = filter_input_byte_count();
1298}
1299
1300void 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]dd29bcd72011-03-24 00:03:441308 UMA_HISTOGRAM_CUSTOM_COUNTS("Sdch3.Network_Decode_Bytes_Processed_b",
1309 static_cast<int>(bytes_observed_in_packets_), 500, 100000, 100);
[email protected]dd29bcd72011-03-24 00:03:441310 return;
1311 }
1312 case FilterContext::SDCH_PASSTHROUGH: {
1313 // Despite advertising a dictionary, we handled non-sdch compressed
1314 // content.
[email protected]dd29bcd72011-03-24 00:03:441315 return;
1316 }
1317
1318 case FilterContext::SDCH_EXPERIMENT_DECODE: {
[email protected]d6b55392011-08-05 04:04:351319 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment2_Decode",
[email protected]dd29bcd72011-03-24 00:03:441320 duration,
1321 base::TimeDelta::FromMilliseconds(20),
1322 base::TimeDelta::FromMinutes(10), 100);
[email protected]dd29bcd72011-03-24 00:03:441323 return;
1324 }
1325 case FilterContext::SDCH_EXPERIMENT_HOLDBACK: {
[email protected]d6b55392011-08-05 04:04:351326 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment2_Holdback",
[email protected]dd29bcd72011-03-24 00:03:441327 duration,
1328 base::TimeDelta::FromMilliseconds(20),
1329 base::TimeDelta::FromMinutes(10), 100);
[email protected]dd29bcd72011-03-24 00:03:441330 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]b7996452011-10-31 19:30:561343 } while (0)
[email protected]dd29bcd72011-03-24 00:03:441344
1345void 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
1413bool 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]bbaea8f2011-06-24 00:11:011420void 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]4b4d20242012-02-23 18:27:461433 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]b73656ca2011-07-22 17:42:171438 }
[email protected]bbaea8f2011-06-24 00:11:011439 }
1440
1441 start_time_ = base::TimeTicks();
1442}
1443
1444void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) {
1445 if (done_)
1446 return;
1447 done_ = true;
[email protected]bbaea8f2011-06-24 00:11:011448 RecordPerfHistograms(reason);
[email protected]7a299a92012-10-24 23:54:501449 if (reason == FINISHED) {
1450 request_->set_received_response_content_length(prefilter_bytes_read());
[email protected]bbaea8f2011-06-24 00:11:011451 RecordCompressionHistograms();
[email protected]7a299a92012-10-24 23:54:501452 }
[email protected]bbaea8f2011-06-24 00:11:011453}
1454
[email protected]ea8141e2011-10-05 13:12:511455HttpResponseHeaders* 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
1463void URLRequestHttpJob::NotifyURLRequestDestroyed() {
1464 awaiting_callback_ = false;
1465}
1466
[email protected]5a07c192012-07-30 20:18:221467void URLRequestHttpJob::OnDetachRequest() {
1468 http_transaction_delegate_->OnDetachRequest();
1469}
1470
[email protected]4f5656c62010-12-13 10:47:091471} // namespace net