blob: a870a2ca86b381a0444969620288137da2e5467d [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/url_request/url_request_http_job.h"
6
7#include "base/base_switches.h"
8#include "base/bind.h"
9#include "base/bind_helpers.h"
10#include "base/command_line.h"
11#include "base/compiler_specific.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012#include "base/file_version_info.h"
Ben Murdoch9ab55632013-07-18 11:57:30 +010013#include "base/message_loop/message_loop.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "base/metrics/field_trial.h"
15#include "base/metrics/histogram.h"
16#include "base/rand_util.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010017#include "base/strings/string_util.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010018#include "base/time/time.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#include "net/base/host_port_pair.h"
20#include "net/base/load_flags.h"
21#include "net/base/mime_util.h"
22#include "net/base/net_errors.h"
23#include "net/base/net_util.h"
24#include "net/base/network_delegate.h"
25#include "net/base/sdch_manager.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010026#include "net/cert/cert_status_flags.h"
Ben Murdocheffb81e2014-03-31 11:51:25 +010027#include "net/cookies/cookie_store.h"
28#include "net/http/http_content_disposition.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029#include "net/http/http_network_session.h"
30#include "net/http/http_request_headers.h"
31#include "net/http/http_response_headers.h"
32#include "net/http/http_response_info.h"
33#include "net/http/http_status_code.h"
34#include "net/http/http_transaction.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000035#include "net/http/http_transaction_factory.h"
36#include "net/http/http_util.h"
Ben Murdoch116680a2014-07-20 18:25:52 -070037#include "net/proxy/proxy_info.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000038#include "net/ssl/ssl_cert_request_info.h"
39#include "net/ssl/ssl_config_service.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000040#include "net/url_request/fraudulent_certificate_reporter.h"
41#include "net/url_request/http_user_agent_settings.h"
42#include "net/url_request/url_request.h"
43#include "net/url_request/url_request_context.h"
44#include "net/url_request/url_request_error_job.h"
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +010045#include "net/url_request/url_request_job_factory.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000046#include "net/url_request/url_request_redirect_job.h"
47#include "net/url_request/url_request_throttler_header_adapter.h"
48#include "net/url_request/url_request_throttler_manager.h"
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +000049#include "net/websockets/websocket_handshake_stream_base.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050
51static const char kAvailDictionaryHeader[] = "Avail-Dictionary";
52
53namespace net {
54
55class URLRequestHttpJob::HttpFilterContext : public FilterContext {
56 public:
57 explicit HttpFilterContext(URLRequestHttpJob* job);
58 virtual ~HttpFilterContext();
59
60 // FilterContext implementation.
61 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
62 virtual bool GetURL(GURL* gurl) const OVERRIDE;
Ben Murdocheffb81e2014-03-31 11:51:25 +010063 virtual bool GetContentDisposition(std::string* disposition) const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000064 virtual base::Time GetRequestTime() const OVERRIDE;
65 virtual bool IsCachedContent() const OVERRIDE;
66 virtual bool IsDownload() const OVERRIDE;
67 virtual bool IsSdchResponse() const OVERRIDE;
68 virtual int64 GetByteReadCount() const OVERRIDE;
69 virtual int GetResponseCode() const OVERRIDE;
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +010070 virtual const URLRequestContext* GetURLRequestContext() const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000071 virtual void RecordPacketStats(StatisticSelector statistic) const OVERRIDE;
72
73 // Method to allow us to reset filter context for a response that should have
74 // been SDCH encoded when there is an update due to an explicit HTTP header.
75 void ResetSdchResponseToFalse();
76
77 private:
78 URLRequestHttpJob* job_;
79
80 DISALLOW_COPY_AND_ASSIGN(HttpFilterContext);
81};
82
Torne (Richard Coles)58218062012-11-14 11:43:16 +000083URLRequestHttpJob::HttpFilterContext::HttpFilterContext(URLRequestHttpJob* job)
84 : job_(job) {
85 DCHECK(job_);
86}
87
88URLRequestHttpJob::HttpFilterContext::~HttpFilterContext() {
89}
90
91bool URLRequestHttpJob::HttpFilterContext::GetMimeType(
92 std::string* mime_type) const {
93 return job_->GetMimeType(mime_type);
94}
95
96bool URLRequestHttpJob::HttpFilterContext::GetURL(GURL* gurl) const {
97 if (!job_->request())
98 return false;
99 *gurl = job_->request()->url();
100 return true;
101}
102
Ben Murdocheffb81e2014-03-31 11:51:25 +0100103bool URLRequestHttpJob::HttpFilterContext::GetContentDisposition(
104 std::string* disposition) const {
105 HttpResponseHeaders* headers = job_->GetResponseHeaders();
106 void *iter = NULL;
107 return headers->EnumerateHeader(&iter, "Content-Disposition", disposition);
108}
109
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000110base::Time URLRequestHttpJob::HttpFilterContext::GetRequestTime() const {
111 return job_->request() ? job_->request()->request_time() : base::Time();
112}
113
114bool URLRequestHttpJob::HttpFilterContext::IsCachedContent() const {
115 return job_->is_cached_content_;
116}
117
118bool URLRequestHttpJob::HttpFilterContext::IsDownload() const {
119 return (job_->request_info_.load_flags & LOAD_IS_DOWNLOAD) != 0;
120}
121
122void URLRequestHttpJob::HttpFilterContext::ResetSdchResponseToFalse() {
123 DCHECK(job_->sdch_dictionary_advertised_);
124 job_->sdch_dictionary_advertised_ = false;
125}
126
127bool URLRequestHttpJob::HttpFilterContext::IsSdchResponse() const {
128 return job_->sdch_dictionary_advertised_;
129}
130
131int64 URLRequestHttpJob::HttpFilterContext::GetByteReadCount() const {
132 return job_->filter_input_byte_count();
133}
134
135int URLRequestHttpJob::HttpFilterContext::GetResponseCode() const {
136 return job_->GetResponseCode();
137}
138
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100139const URLRequestContext*
140URLRequestHttpJob::HttpFilterContext::GetURLRequestContext() const {
141 return job_->request() ? job_->request()->context() : NULL;
142}
143
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000144void URLRequestHttpJob::HttpFilterContext::RecordPacketStats(
145 StatisticSelector statistic) const {
146 job_->RecordPacketStats(statistic);
147}
148
149// TODO(darin): make sure the port blocking code is not lost
150// static
151URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request,
152 NetworkDelegate* network_delegate,
153 const std::string& scheme) {
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +0000154 DCHECK(scheme == "http" || scheme == "https" || scheme == "ws" ||
155 scheme == "wss");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000156
157 if (!request->context()->http_transaction_factory()) {
158 NOTREACHED() << "requires a valid context";
159 return new URLRequestErrorJob(
160 request, network_delegate, ERR_INVALID_ARGUMENT);
161 }
162
163 GURL redirect_url;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000164 if (request->GetHSTSRedirect(&redirect_url)) {
165 return new URLRequestRedirectJob(
166 request, network_delegate, redirect_url,
167 // Use status code 307 to preserve the method, so POST requests work.
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000168 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "HSTS");
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000169 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000170 return new URLRequestHttpJob(request,
171 network_delegate,
172 request->context()->http_user_agent_settings());
173}
174
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000175URLRequestHttpJob::URLRequestHttpJob(
176 URLRequest* request,
177 NetworkDelegate* network_delegate,
178 const HttpUserAgentSettings* http_user_agent_settings)
179 : URLRequestJob(request, network_delegate),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000180 priority_(DEFAULT_PRIORITY),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000181 response_info_(NULL),
182 response_cookies_save_index_(0),
183 proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
184 server_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100185 start_callback_(base::Bind(&URLRequestHttpJob::OnStartCompleted,
186 base::Unretained(this))),
187 notify_before_headers_sent_callback_(
188 base::Bind(&URLRequestHttpJob::NotifyBeforeSendHeadersCallback,
189 base::Unretained(this))),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000190 read_in_progress_(false),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000191 throttling_entry_(NULL),
192 sdch_dictionary_advertised_(false),
193 sdch_test_activated_(false),
194 sdch_test_control_(false),
195 is_cached_content_(false),
196 request_creation_time_(),
197 packet_timing_enabled_(false),
198 done_(false),
199 bytes_observed_in_packets_(0),
200 request_time_snapshot_(),
201 final_packet_time_(),
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100202 filter_context_(new HttpFilterContext(this)),
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100203 on_headers_received_callback_(
204 base::Bind(&URLRequestHttpJob::OnHeadersReceivedCallback,
205 base::Unretained(this))),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000206 awaiting_callback_(false),
Torne (Richard Coles)cedac222014-06-03 10:58:34 +0100207 http_user_agent_settings_(http_user_agent_settings),
208 weak_factory_(this) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000209 URLRequestThrottlerManager* manager = request->context()->throttler_manager();
210 if (manager)
211 throttling_entry_ = manager->RegisterRequestUrl(request->url());
212
213 ResetTimer();
214}
215
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000216URLRequestHttpJob::~URLRequestHttpJob() {
217 CHECK(!awaiting_callback_);
218
219 DCHECK(!sdch_test_control_ || !sdch_test_activated_);
220 if (!is_cached_content_) {
221 if (sdch_test_control_)
222 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_HOLDBACK);
223 if (sdch_test_activated_)
224 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_DECODE);
225 }
226 // Make sure SDCH filters are told to emit histogram data while
227 // filter_context_ is still alive.
228 DestroyFilters();
229
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000230 DoneWithRequest(ABORTED);
231}
232
233void URLRequestHttpJob::SetPriority(RequestPriority priority) {
234 priority_ = priority;
235 if (transaction_)
236 transaction_->SetPriority(priority_);
237}
238
239void URLRequestHttpJob::Start() {
240 DCHECK(!transaction_.get());
241
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100242 // URLRequest::SetReferrer ensures that we do not send username and password
243 // fields in the referrer.
244 GURL referrer(request_->referrer());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000245
246 request_info_.url = request_->url();
247 request_info_.method = request_->method();
248 request_info_.load_flags = request_->load_flags();
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100249 // Enable privacy mode if cookie settings or flags tell us not send or
250 // save cookies.
251 bool enable_privacy_mode =
252 (request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES) ||
253 (request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) ||
254 CanEnablePrivacyMode();
255 // Privacy mode could still be disabled in OnCookiesLoaded if we are going
256 // to send previously saved cookies.
257 request_info_.privacy_mode = enable_privacy_mode ?
Ben Murdoche5d81f52014-04-03 12:29:45 +0100258 PRIVACY_MODE_ENABLED : PRIVACY_MODE_DISABLED;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000259
260 // Strip Referer from request_info_.extra_headers to prevent, e.g., plugins
261 // from overriding headers that are controlled using other means. Otherwise a
262 // plugin could set a referrer although sending the referrer is inhibited.
263 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kReferer);
264
265 // Our consumer should have made sure that this is a safe referrer. See for
266 // instance WebCore::FrameLoader::HideReferrer.
267 if (referrer.is_valid()) {
268 request_info_.extra_headers.SetHeader(HttpRequestHeaders::kReferer,
269 referrer.spec());
270 }
271
272 request_info_.extra_headers.SetHeaderIfMissing(
273 HttpRequestHeaders::kUserAgent,
274 http_user_agent_settings_ ?
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000275 http_user_agent_settings_->GetUserAgent() : std::string());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000276
277 AddExtraHeaders();
278 AddCookieHeaderAndStart();
279}
280
281void URLRequestHttpJob::Kill() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000282 if (!transaction_.get())
283 return;
284
285 weak_factory_.InvalidateWeakPtrs();
286 DestroyTransaction();
287 URLRequestJob::Kill();
288}
289
Ben Murdoch116680a2014-07-20 18:25:52 -0700290void URLRequestHttpJob::NotifyBeforeSendProxyHeadersCallback(
291 const ProxyInfo& proxy_info,
292 HttpRequestHeaders* request_headers) {
293 DCHECK(request_headers);
294 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
295 if (network_delegate()) {
296 network_delegate()->NotifyBeforeSendProxyHeaders(
297 request_,
298 proxy_info,
299 request_headers);
300 }
301}
302
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000303void URLRequestHttpJob::NotifyHeadersComplete() {
304 DCHECK(!response_info_);
305
306 response_info_ = transaction_->GetResponseInfo();
307
308 // Save boolean, as we'll need this info at destruction time, and filters may
309 // also need this info.
310 is_cached_content_ = response_info_->was_cached;
311
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100312 if (!is_cached_content_ && throttling_entry_.get()) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000313 URLRequestThrottlerHeaderAdapter response_adapter(GetResponseHeaders());
314 throttling_entry_->UpdateWithResponse(request_info_.url.host(),
315 &response_adapter);
316 }
317
318 // The ordering of these calls is not important.
319 ProcessStrictTransportSecurityHeader();
320 ProcessPublicKeyPinsHeader();
321
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100322 SdchManager* sdch_manager(request()->context()->sdch_manager());
323 if (sdch_manager && sdch_manager->IsInSupportedDomain(request_->url())) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000324 const std::string name = "Get-Dictionary";
325 std::string url_text;
326 void* iter = NULL;
327 // TODO(jar): We need to not fetch dictionaries the first time they are
328 // seen, but rather wait until we can justify their usefulness.
329 // For now, we will only fetch the first dictionary, which will at least
330 // require multiple suggestions before we get additional ones for this site.
331 // Eventually we should wait until a dictionary is requested several times
332 // before we even download it (so that we don't waste memory or bandwidth).
333 if (GetResponseHeaders()->EnumerateHeader(&iter, name, &url_text)) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000334 // Resolve suggested URL relative to request url.
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100335 GURL sdch_dictionary_url = request_->url().Resolve(url_text);
336 if (sdch_dictionary_url.is_valid()) {
337 sdch_manager->FetchDictionary(request_->url(), sdch_dictionary_url);
338 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000339 }
340 }
341
342 // The HTTP transaction may be restarted several times for the purposes
343 // of sending authorization information. Each time it restarts, we get
344 // notified of the headers completion so that we can update the cookie store.
345 if (transaction_->IsReadyToRestartForAuth()) {
346 DCHECK(!response_info_->auth_challenge.get());
347 // TODO(battre): This breaks the webrequest API for
348 // URLRequestTestHTTP.BasicAuthWithCookies
349 // where OnBeforeSendHeaders -> OnSendHeaders -> OnBeforeSendHeaders
350 // occurs.
351 RestartTransactionWithAuth(AuthCredentials());
352 return;
353 }
354
355 URLRequestJob::NotifyHeadersComplete();
356}
357
358void URLRequestHttpJob::NotifyDone(const URLRequestStatus& status) {
359 DoneWithRequest(FINISHED);
360 URLRequestJob::NotifyDone(status);
361}
362
363void URLRequestHttpJob::DestroyTransaction() {
364 DCHECK(transaction_.get());
365
366 DoneWithRequest(ABORTED);
367 transaction_.reset();
368 response_info_ = NULL;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100369 receive_headers_end_ = base::TimeTicks();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000370}
371
372void URLRequestHttpJob::StartTransaction() {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000373 if (network_delegate()) {
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000374 OnCallToDelegate();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000375 int rv = network_delegate()->NotifyBeforeSendHeaders(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000376 request_, notify_before_headers_sent_callback_,
377 &request_info_.extra_headers);
378 // If an extension blocks the request, we rely on the callback to
379 // MaybeStartTransactionInternal().
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000380 if (rv == ERR_IO_PENDING)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000381 return;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000382 MaybeStartTransactionInternal(rv);
383 return;
384 }
385 StartTransactionInternal();
386}
387
388void URLRequestHttpJob::NotifyBeforeSendHeadersCallback(int result) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000389 // Check that there are no callbacks to already canceled requests.
390 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
391
392 MaybeStartTransactionInternal(result);
393}
394
395void URLRequestHttpJob::MaybeStartTransactionInternal(int result) {
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000396 OnCallToDelegateComplete();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000397 if (result == OK) {
398 StartTransactionInternal();
399 } else {
400 std::string source("delegate");
401 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
402 NetLog::StringCallback("source", &source));
403 NotifyCanceled();
404 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
405 }
406}
407
408void URLRequestHttpJob::StartTransactionInternal() {
409 // NOTE: This method assumes that request_info_ is already setup properly.
410
411 // If we already have a transaction, then we should restart the transaction
412 // with auth provided by auth_credentials_.
413
414 int rv;
415
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000416 if (network_delegate()) {
417 network_delegate()->NotifySendHeaders(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000418 request_, request_info_.extra_headers);
419 }
420
421 if (transaction_.get()) {
422 rv = transaction_->RestartWithAuth(auth_credentials_, start_callback_);
423 auth_credentials_ = AuthCredentials();
424 } else {
425 DCHECK(request_->context()->http_transaction_factory());
426
427 rv = request_->context()->http_transaction_factory()->CreateTransaction(
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000428 priority_, &transaction_);
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +0000429
430 if (rv == OK && request_info_.url.SchemeIsWSOrWSS()) {
Torne (Richard Coles)a3f6a492013-12-18 16:25:09 +0000431 base::SupportsUserData::Data* data = request_->GetUserData(
432 WebSocketHandshakeStreamBase::CreateHelper::DataKey());
433 if (data) {
434 transaction_->SetWebSocketHandshakeStreamCreateHelper(
435 static_cast<WebSocketHandshakeStreamBase::CreateHelper*>(data));
436 } else {
437 rv = ERR_DISALLOWED_URL_SCHEME;
438 }
439 }
440
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000441 if (rv == OK) {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000442 transaction_->SetBeforeNetworkStartCallback(
443 base::Bind(&URLRequestHttpJob::NotifyBeforeNetworkStart,
444 base::Unretained(this)));
Ben Murdoch116680a2014-07-20 18:25:52 -0700445 transaction_->SetBeforeProxyHeadersSentCallback(
446 base::Bind(&URLRequestHttpJob::NotifyBeforeSendProxyHeadersCallback,
447 base::Unretained(this)));
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000448
Torne (Richard Coles)03b57e02014-08-28 12:05:23 +0100449 if (!throttling_entry_ ||
450 !throttling_entry_->ShouldRejectRequest(
451 *request_, network_delegate())) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000452 rv = transaction_->Start(
453 &request_info_, start_callback_, request_->net_log());
454 start_time_ = base::TimeTicks::Now();
455 } else {
456 // Special error code for the exponential back-off module.
457 rv = ERR_TEMPORARILY_THROTTLED;
458 }
459 }
460 }
461
462 if (rv == ERR_IO_PENDING)
463 return;
464
465 // The transaction started synchronously, but we need to notify the
466 // URLRequest delegate via the message loop.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100467 base::MessageLoop::current()->PostTask(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000468 FROM_HERE,
469 base::Bind(&URLRequestHttpJob::OnStartCompleted,
470 weak_factory_.GetWeakPtr(), rv));
471}
472
473void URLRequestHttpJob::AddExtraHeaders() {
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100474 SdchManager* sdch_manager = request()->context()->sdch_manager();
475
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000476 // Supply Accept-Encoding field only if it is not already provided.
477 // It should be provided IF the content is known to have restrictions on
478 // potential encoding, such as streaming multi-media.
479 // For details see bug 47381.
480 // TODO(jar, enal): jpeg files etc. should set up a request header if
481 // possible. Right now it is done only by buffered_resource_loader and
482 // simple_data_source.
483 if (!request_info_.extra_headers.HasHeader(
484 HttpRequestHeaders::kAcceptEncoding)) {
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100485 bool advertise_sdch = sdch_manager &&
486 // We don't support SDCH responses to POST as there is a possibility
487 // of having SDCH encoded responses returned (e.g. by the cache)
488 // which we cannot decode, and in those situations, we will need
489 // to retransmit the request without SDCH, which is illegal for a POST.
490 request()->method() != "POST" &&
491 sdch_manager->IsInSupportedDomain(request_->url());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000492 std::string avail_dictionaries;
493 if (advertise_sdch) {
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100494 sdch_manager->GetAvailDictionaryList(request_->url(),
495 &avail_dictionaries);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000496
497 // The AllowLatencyExperiment() is only true if we've successfully done a
498 // full SDCH compression recently in this browser session for this host.
499 // Note that for this path, there might be no applicable dictionaries,
500 // and hence we can't participate in the experiment.
501 if (!avail_dictionaries.empty() &&
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100502 sdch_manager->AllowLatencyExperiment(request_->url())) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000503 // We are participating in the test (or control), and hence we'll
504 // eventually record statistics via either SDCH_EXPERIMENT_DECODE or
505 // SDCH_EXPERIMENT_HOLDBACK, and we'll need some packet timing data.
506 packet_timing_enabled_ = true;
507 if (base::RandDouble() < .01) {
508 sdch_test_control_ = true; // 1% probability.
509 advertise_sdch = false;
510 } else {
511 sdch_test_activated_ = true;
512 }
513 }
514 }
515
516 // Supply Accept-Encoding headers first so that it is more likely that they
517 // will be in the first transmitted packet. This can sometimes make it
518 // easier to filter and analyze the streams to assure that a proxy has not
519 // damaged these headers. Some proxies deliberately corrupt Accept-Encoding
520 // headers.
521 if (!advertise_sdch) {
522 // Tell the server what compression formats we support (other than SDCH).
523 request_info_.extra_headers.SetHeader(
524 HttpRequestHeaders::kAcceptEncoding, "gzip,deflate");
525 } else {
526 // Include SDCH in acceptable list.
527 request_info_.extra_headers.SetHeader(
528 HttpRequestHeaders::kAcceptEncoding, "gzip,deflate,sdch");
529 if (!avail_dictionaries.empty()) {
530 request_info_.extra_headers.SetHeader(
531 kAvailDictionaryHeader,
532 avail_dictionaries);
533 sdch_dictionary_advertised_ = true;
534 // Since we're tagging this transaction as advertising a dictionary,
535 // we'll definitely employ an SDCH filter (or tentative sdch filter)
536 // when we get a response. When done, we'll record histograms via
537 // SDCH_DECODE or SDCH_PASSTHROUGH. Hence we need to record packet
538 // arrival times.
539 packet_timing_enabled_ = true;
540 }
541 }
542 }
543
544 if (http_user_agent_settings_) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000545 // Only add default Accept-Language if the request didn't have it
546 // specified.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000547 std::string accept_language =
548 http_user_agent_settings_->GetAcceptLanguage();
549 if (!accept_language.empty()) {
550 request_info_.extra_headers.SetHeaderIfMissing(
551 HttpRequestHeaders::kAcceptLanguage,
552 accept_language);
553 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000554 }
555}
556
557void URLRequestHttpJob::AddCookieHeaderAndStart() {
558 // No matter what, we want to report our status as IO pending since we will
559 // be notifying our consumer asynchronously via OnStartCompleted.
560 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
561
562 // If the request was destroyed, then there is no more work to do.
563 if (!request_)
564 return;
565
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000566 CookieStore* cookie_store = GetCookieStore();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000567 if (cookie_store && !(request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES)) {
Ben Murdocheffb81e2014-03-31 11:51:25 +0100568 cookie_store->GetAllCookiesForURLAsync(
569 request_->url(),
570 base::Bind(&URLRequestHttpJob::CheckCookiePolicyAndLoad,
571 weak_factory_.GetWeakPtr()));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000572 } else {
573 DoStartTransaction();
574 }
575}
576
577void URLRequestHttpJob::DoLoadCookies() {
578 CookieOptions options;
579 options.set_include_httponly();
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000580 GetCookieStore()->GetCookiesWithOptionsAsync(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000581 request_->url(), options,
582 base::Bind(&URLRequestHttpJob::OnCookiesLoaded,
583 weak_factory_.GetWeakPtr()));
584}
585
586void URLRequestHttpJob::CheckCookiePolicyAndLoad(
587 const CookieList& cookie_list) {
588 if (CanGetCookies(cookie_list))
589 DoLoadCookies();
590 else
591 DoStartTransaction();
592}
593
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000594void URLRequestHttpJob::OnCookiesLoaded(const std::string& cookie_line) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000595 if (!cookie_line.empty()) {
596 request_info_.extra_headers.SetHeader(
597 HttpRequestHeaders::kCookie, cookie_line);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100598 // Disable privacy mode as we are sending cookies anyway.
Ben Murdoche5d81f52014-04-03 12:29:45 +0100599 request_info_.privacy_mode = PRIVACY_MODE_DISABLED;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000600 }
601 DoStartTransaction();
602}
603
604void URLRequestHttpJob::DoStartTransaction() {
605 // We may have been canceled while retrieving cookies.
606 if (GetStatus().is_success()) {
607 StartTransaction();
608 } else {
609 NotifyCanceled();
610 }
611}
612
613void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000614 // End of the call started in OnStartCompleted.
615 OnCallToDelegateComplete();
616
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000617 if (result != net::OK) {
618 std::string source("delegate");
619 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
620 NetLog::StringCallback("source", &source));
621 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
622 return;
623 }
624
625 DCHECK(transaction_.get());
626
627 const HttpResponseInfo* response_info = transaction_->GetResponseInfo();
628 DCHECK(response_info);
629
630 response_cookies_.clear();
631 response_cookies_save_index_ = 0;
632
633 FetchResponseCookies(&response_cookies_);
634
635 if (!GetResponseHeaders()->GetDateValue(&response_date_))
636 response_date_ = base::Time();
637
638 // Now, loop over the response cookies, and attempt to persist each.
639 SaveNextCookie();
640}
641
642// If the save occurs synchronously, SaveNextCookie will loop and save the next
643// cookie. If the save is deferred, the callback is responsible for continuing
644// to iterate through the cookies.
645// TODO(erikwright): Modify the CookieStore API to indicate via return value
646// whether it completed synchronously or asynchronously.
647// See http://crbug.com/131066.
648void URLRequestHttpJob::SaveNextCookie() {
649 // No matter what, we want to report our status as IO pending since we will
650 // be notifying our consumer asynchronously via OnStartCompleted.
651 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
652
653 // Used to communicate with the callback. See the implementation of
654 // OnCookieSaved.
655 scoped_refptr<SharedBoolean> callback_pending = new SharedBoolean(false);
656 scoped_refptr<SharedBoolean> save_next_cookie_running =
657 new SharedBoolean(true);
658
659 if (!(request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) &&
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000660 GetCookieStore() && response_cookies_.size() > 0) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000661 CookieOptions options;
662 options.set_include_httponly();
663 options.set_server_time(response_date_);
664
665 net::CookieStore::SetCookiesCallback callback(
666 base::Bind(&URLRequestHttpJob::OnCookieSaved,
667 weak_factory_.GetWeakPtr(),
668 save_next_cookie_running,
669 callback_pending));
670
671 // Loop through the cookies as long as SetCookieWithOptionsAsync completes
672 // synchronously.
673 while (!callback_pending->data &&
674 response_cookies_save_index_ < response_cookies_.size()) {
675 if (CanSetCookie(
676 response_cookies_[response_cookies_save_index_], &options)) {
677 callback_pending->data = true;
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000678 GetCookieStore()->SetCookieWithOptionsAsync(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000679 request_->url(), response_cookies_[response_cookies_save_index_],
680 options, callback);
681 }
682 ++response_cookies_save_index_;
683 }
684 }
685
686 save_next_cookie_running->data = false;
687
688 if (!callback_pending->data) {
689 response_cookies_.clear();
690 response_cookies_save_index_ = 0;
691 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
692 NotifyHeadersComplete();
693 return;
694 }
695}
696
697// |save_next_cookie_running| is true when the callback is bound and set to
698// false when SaveNextCookie exits, allowing the callback to determine if the
699// save occurred synchronously or asynchronously.
700// |callback_pending| is false when the callback is invoked and will be set to
701// true by the callback, allowing SaveNextCookie to detect whether the save
702// occurred synchronously.
703// See SaveNextCookie() for more information.
704void URLRequestHttpJob::OnCookieSaved(
705 scoped_refptr<SharedBoolean> save_next_cookie_running,
706 scoped_refptr<SharedBoolean> callback_pending,
707 bool cookie_status) {
708 callback_pending->data = false;
709
710 // If we were called synchronously, return.
711 if (save_next_cookie_running->data) {
712 return;
713 }
714
715 // We were called asynchronously, so trigger the next save.
716 // We may have been canceled within OnSetCookie.
717 if (GetStatus().is_success()) {
718 SaveNextCookie();
719 } else {
720 NotifyCanceled();
721 }
722}
723
724void URLRequestHttpJob::FetchResponseCookies(
725 std::vector<std::string>* cookies) {
726 const std::string name = "Set-Cookie";
727 std::string value;
728
729 void* iter = NULL;
730 HttpResponseHeaders* headers = GetResponseHeaders();
731 while (headers->EnumerateHeader(&iter, name, &value)) {
732 if (!value.empty())
733 cookies->push_back(value);
734 }
735}
736
737// NOTE: |ProcessStrictTransportSecurityHeader| and
738// |ProcessPublicKeyPinsHeader| have very similar structures, by design.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000739void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() {
740 DCHECK(response_info_);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000741 TransportSecurityState* security_state =
742 request_->context()->transport_security_state();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000743 const SSLInfo& ssl_info = response_info_->ssl_info;
744
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000745 // Only accept HSTS headers on HTTPS connections that have no
746 // certificate errors.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000747 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000748 !security_state)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000749 return;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000750
751 // http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec:
752 //
753 // If a UA receives more than one STS header field in a HTTP response
754 // message over secure transport, then the UA MUST process only the
755 // first such header field.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000756 HttpResponseHeaders* headers = GetResponseHeaders();
757 std::string value;
758 if (headers->EnumerateHeader(NULL, "Strict-Transport-Security", &value))
759 security_state->AddHSTSHeader(request_info_.url.host(), value);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000760}
761
762void URLRequestHttpJob::ProcessPublicKeyPinsHeader() {
763 DCHECK(response_info_);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000764 TransportSecurityState* security_state =
765 request_->context()->transport_security_state();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000766 const SSLInfo& ssl_info = response_info_->ssl_info;
767
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000768 // Only accept HPKP headers on HTTPS connections that have no
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000769 // certificate errors.
770 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000771 !security_state)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000772 return;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000773
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000774 // http://tools.ietf.org/html/draft-ietf-websec-key-pinning:
775 //
776 // If a UA receives more than one PKP header field in an HTTP
777 // response message over secure transport, then the UA MUST process
778 // only the first such header field.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000779 HttpResponseHeaders* headers = GetResponseHeaders();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000780 std::string value;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000781 if (headers->EnumerateHeader(NULL, "Public-Key-Pins", &value))
782 security_state->AddHPKPHeader(request_info_.url.host(), value, ssl_info);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000783}
784
785void URLRequestHttpJob::OnStartCompleted(int result) {
786 RecordTimer();
787
788 // If the request was destroyed, then there is no more work to do.
789 if (!request_)
790 return;
791
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000792 // If the job is done (due to cancellation), can just ignore this
793 // notification.
794 if (done_)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000795 return;
796
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100797 receive_headers_end_ = base::TimeTicks::Now();
798
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000799 // Clear the IO_PENDING status
800 SetStatus(URLRequestStatus());
801
802 const URLRequestContext* context = request_->context();
803
804 if (result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN &&
805 transaction_->GetResponseInfo() != NULL) {
806 FraudulentCertificateReporter* reporter =
807 context->fraudulent_certificate_reporter();
808 if (reporter != NULL) {
809 const SSLInfo& ssl_info = transaction_->GetResponseInfo()->ssl_info;
810 bool sni_available = SSLConfigService::IsSNIAvailable(
811 context->ssl_config_service());
812 const std::string& host = request_->url().host();
813
814 reporter->SendReport(host, ssl_info, sni_available);
815 }
816 }
817
818 if (result == OK) {
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +0100819 if (transaction_ && transaction_->GetResponseInfo()) {
820 SetProxyServer(transaction_->GetResponseInfo()->proxy_server);
821 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000822 scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000823 if (network_delegate()) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000824 // Note that |this| may not be deleted until
825 // |on_headers_received_callback_| or
826 // |NetworkDelegate::URLRequestDestroyed()| has been called.
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000827 OnCallToDelegate();
Ben Murdocheffb81e2014-03-31 11:51:25 +0100828 allowed_unsafe_redirect_url_ = GURL();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000829 int error = network_delegate()->NotifyHeadersReceived(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100830 request_,
831 on_headers_received_callback_,
832 headers.get(),
Ben Murdocheffb81e2014-03-31 11:51:25 +0100833 &override_response_headers_,
834 &allowed_unsafe_redirect_url_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000835 if (error != net::OK) {
836 if (error == net::ERR_IO_PENDING) {
837 awaiting_callback_ = true;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000838 } else {
839 std::string source("delegate");
840 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
841 NetLog::StringCallback("source",
842 &source));
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +0000843 OnCallToDelegateComplete();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000844 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, error));
845 }
846 return;
847 }
848 }
849
850 SaveCookiesAndNotifyHeadersComplete(net::OK);
851 } else if (IsCertificateError(result)) {
Torne (Richard Coles)8bcbed82013-10-22 16:41:35 +0100852 // We encountered an SSL certificate error.
853 if (result == ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY ||
854 result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN) {
855 // These are hard failures. They're handled separately and don't have
856 // the correct cert status, so set it here.
857 SSLInfo info(transaction_->GetResponseInfo()->ssl_info);
858 info.cert_status = MapNetErrorToCertStatus(result);
859 NotifySSLCertificateError(info, true);
860 } else {
861 // Maybe overridable, maybe not. Ask the delegate to decide.
Torne (Richard Coles)8bcbed82013-10-22 16:41:35 +0100862 const URLRequestContext* context = request_->context();
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100863 TransportSecurityState* state = context->transport_security_state();
864 const bool fatal =
865 state &&
866 state->ShouldSSLErrorsBeFatal(
Torne (Richard Coles)8bcbed82013-10-22 16:41:35 +0100867 request_info_.url.host(),
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100868 SSLConfigService::IsSNIAvailable(context->ssl_config_service()));
Torne (Richard Coles)8bcbed82013-10-22 16:41:35 +0100869 NotifySSLCertificateError(
870 transaction_->GetResponseInfo()->ssl_info, fatal);
871 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000872 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
873 NotifyCertificateRequested(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100874 transaction_->GetResponseInfo()->cert_request_info.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000875 } else {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000876 // Even on an error, there may be useful information in the response
877 // info (e.g. whether there's a cached copy).
878 if (transaction_.get())
879 response_info_ = transaction_->GetResponseInfo();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000880 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
881 }
882}
883
884void URLRequestHttpJob::OnHeadersReceivedCallback(int result) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000885 awaiting_callback_ = false;
886
887 // Check that there are no callbacks to already canceled requests.
888 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
889
890 SaveCookiesAndNotifyHeadersComplete(result);
891}
892
893void URLRequestHttpJob::OnReadCompleted(int result) {
894 read_in_progress_ = false;
895
896 if (ShouldFixMismatchedContentLength(result))
897 result = OK;
898
899 if (result == OK) {
900 NotifyDone(URLRequestStatus());
901 } else if (result < 0) {
902 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
903 } else {
904 // Clear the IO_PENDING status
905 SetStatus(URLRequestStatus());
906 }
907
908 NotifyReadComplete(result);
909}
910
911void URLRequestHttpJob::RestartTransactionWithAuth(
912 const AuthCredentials& credentials) {
913 auth_credentials_ = credentials;
914
915 // These will be reset in OnStartCompleted.
916 response_info_ = NULL;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100917 receive_headers_end_ = base::TimeTicks();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000918 response_cookies_.clear();
919
920 ResetTimer();
921
922 // Update the cookies, since the cookie store may have been updated from the
923 // headers in the 401/407. Since cookies were already appended to
924 // extra_headers, we need to strip them out before adding them again.
925 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kCookie);
926
927 AddCookieHeaderAndStart();
928}
929
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000930void URLRequestHttpJob::SetUpload(UploadDataStream* upload) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000931 DCHECK(!transaction_.get()) << "cannot change once started";
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000932 request_info_.upload_data_stream = upload;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000933}
934
935void URLRequestHttpJob::SetExtraRequestHeaders(
936 const HttpRequestHeaders& headers) {
937 DCHECK(!transaction_.get()) << "cannot change once started";
938 request_info_.extra_headers.CopyFrom(headers);
939}
940
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000941LoadState URLRequestHttpJob::GetLoadState() const {
942 return transaction_.get() ?
943 transaction_->GetLoadState() : LOAD_STATE_IDLE;
944}
945
946UploadProgress URLRequestHttpJob::GetUploadProgress() const {
947 return transaction_.get() ?
948 transaction_->GetUploadProgress() : UploadProgress();
949}
950
951bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const {
952 DCHECK(transaction_.get());
953
954 if (!response_info_)
955 return false;
956
957 return GetResponseHeaders()->GetMimeType(mime_type);
958}
959
960bool URLRequestHttpJob::GetCharset(std::string* charset) {
961 DCHECK(transaction_.get());
962
963 if (!response_info_)
964 return false;
965
966 return GetResponseHeaders()->GetCharset(charset);
967}
968
969void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) {
970 DCHECK(request_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000971
972 if (response_info_) {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000973 DCHECK(transaction_.get());
974
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000975 *info = *response_info_;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100976 if (override_response_headers_.get())
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000977 info->headers = override_response_headers_;
978 }
979}
980
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000981void URLRequestHttpJob::GetLoadTimingInfo(
982 LoadTimingInfo* load_timing_info) const {
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100983 // If haven't made it far enough to receive any headers, don't return
984 // anything. This makes for more consistent behavior in the case of errors.
985 if (!transaction_ || receive_headers_end_.is_null())
986 return;
987 if (transaction_->GetLoadTimingInfo(load_timing_info))
988 load_timing_info->receive_headers_end = receive_headers_end_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000989}
990
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000991bool URLRequestHttpJob::GetResponseCookies(std::vector<std::string>* cookies) {
992 DCHECK(transaction_.get());
993
994 if (!response_info_)
995 return false;
996
997 // TODO(darin): Why are we extracting response cookies again? Perhaps we
998 // should just leverage response_cookies_.
999
1000 cookies->clear();
1001 FetchResponseCookies(cookies);
1002 return true;
1003}
1004
1005int URLRequestHttpJob::GetResponseCode() const {
1006 DCHECK(transaction_.get());
1007
1008 if (!response_info_)
1009 return -1;
1010
1011 return GetResponseHeaders()->response_code();
1012}
1013
1014Filter* URLRequestHttpJob::SetupFilter() const {
1015 DCHECK(transaction_.get());
1016 if (!response_info_)
1017 return NULL;
1018
1019 std::vector<Filter::FilterType> encoding_types;
1020 std::string encoding_type;
1021 HttpResponseHeaders* headers = GetResponseHeaders();
1022 void* iter = NULL;
1023 while (headers->EnumerateHeader(&iter, "Content-Encoding", &encoding_type)) {
1024 encoding_types.push_back(Filter::ConvertEncodingToType(encoding_type));
1025 }
1026
1027 if (filter_context_->IsSdchResponse()) {
1028 // We are wary of proxies that discard or damage SDCH encoding. If a server
1029 // explicitly states that this is not SDCH content, then we can correct our
1030 // assumption that this is an SDCH response, and avoid the need to recover
1031 // as though the content is corrupted (when we discover it is not SDCH
1032 // encoded).
1033 std::string sdch_response_status;
1034 iter = NULL;
1035 while (headers->EnumerateHeader(&iter, "X-Sdch-Encode",
1036 &sdch_response_status)) {
1037 if (sdch_response_status == "0") {
1038 filter_context_->ResetSdchResponseToFalse();
1039 break;
1040 }
1041 }
1042 }
1043
1044 // Even if encoding types are empty, there is a chance that we need to add
1045 // some decoding, as some proxies strip encoding completely. In such cases,
1046 // we may need to add (for example) SDCH filtering (when the context suggests
1047 // it is appropriate).
1048 Filter::FixupEncodingTypes(*filter_context_, &encoding_types);
1049
1050 return !encoding_types.empty()
1051 ? Filter::Factory(encoding_types, *filter_context_) : NULL;
1052}
1053
Ben Murdochc5cede92014-04-10 11:22:14 +01001054bool URLRequestHttpJob::CopyFragmentOnRedirect(const GURL& location) const {
1055 // Allow modification of reference fragments by default, unless
1056 // |allowed_unsafe_redirect_url_| is set and equal to the redirect URL.
1057 // When this is the case, we assume that the network delegate has set the
1058 // desired redirect URL (with or without fragment), so it must not be changed
1059 // any more.
1060 return !allowed_unsafe_redirect_url_.is_valid() ||
1061 allowed_unsafe_redirect_url_ != location;
1062}
1063
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001064bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) {
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +01001065 // HTTP is always safe.
1066 // TODO(pauljensen): Remove once crbug.com/146591 is fixed.
1067 if (location.is_valid() &&
1068 (location.scheme() == "http" || location.scheme() == "https")) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001069 return true;
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001070 }
Ben Murdochc5cede92014-04-10 11:22:14 +01001071 // Delegates may mark a URL as safe for redirection.
1072 if (allowed_unsafe_redirect_url_.is_valid() &&
1073 allowed_unsafe_redirect_url_ == location) {
1074 return true;
Ben Murdocheffb81e2014-03-31 11:51:25 +01001075 }
Torne (Richard Coles)b2df76e2013-05-13 16:52:09 +01001076 // Query URLRequestJobFactory as to whether |location| would be safe to
1077 // redirect to.
1078 return request_->context()->job_factory() &&
1079 request_->context()->job_factory()->IsSafeRedirectTarget(location);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001080}
1081
1082bool URLRequestHttpJob::NeedsAuth() {
1083 int code = GetResponseCode();
1084 if (code == -1)
1085 return false;
1086
1087 // Check if we need either Proxy or WWW Authentication. This could happen
1088 // because we either provided no auth info, or provided incorrect info.
1089 switch (code) {
1090 case 407:
1091 if (proxy_auth_state_ == AUTH_STATE_CANCELED)
1092 return false;
1093 proxy_auth_state_ = AUTH_STATE_NEED_AUTH;
1094 return true;
1095 case 401:
1096 if (server_auth_state_ == AUTH_STATE_CANCELED)
1097 return false;
1098 server_auth_state_ = AUTH_STATE_NEED_AUTH;
1099 return true;
1100 }
1101 return false;
1102}
1103
1104void URLRequestHttpJob::GetAuthChallengeInfo(
1105 scoped_refptr<AuthChallengeInfo>* result) {
1106 DCHECK(transaction_.get());
1107 DCHECK(response_info_);
1108
1109 // sanity checks:
1110 DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH ||
1111 server_auth_state_ == AUTH_STATE_NEED_AUTH);
1112 DCHECK((GetResponseHeaders()->response_code() == HTTP_UNAUTHORIZED) ||
1113 (GetResponseHeaders()->response_code() ==
1114 HTTP_PROXY_AUTHENTICATION_REQUIRED));
1115
1116 *result = response_info_->auth_challenge;
1117}
1118
1119void URLRequestHttpJob::SetAuth(const AuthCredentials& credentials) {
1120 DCHECK(transaction_.get());
1121
1122 // Proxy gets set first, then WWW.
1123 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1124 proxy_auth_state_ = AUTH_STATE_HAVE_AUTH;
1125 } else {
1126 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
1127 server_auth_state_ = AUTH_STATE_HAVE_AUTH;
1128 }
1129
1130 RestartTransactionWithAuth(credentials);
1131}
1132
1133void URLRequestHttpJob::CancelAuth() {
1134 // Proxy gets set first, then WWW.
1135 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1136 proxy_auth_state_ = AUTH_STATE_CANCELED;
1137 } else {
1138 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
1139 server_auth_state_ = AUTH_STATE_CANCELED;
1140 }
1141
1142 // These will be reset in OnStartCompleted.
1143 response_info_ = NULL;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001144 receive_headers_end_ = base::TimeTicks::Now();
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001145 response_cookies_.clear();
1146
1147 ResetTimer();
1148
1149 // OK, let the consumer read the error page...
1150 //
1151 // Because we set the AUTH_STATE_CANCELED flag, NeedsAuth will return false,
1152 // which will cause the consumer to receive OnResponseStarted instead of
1153 // OnAuthRequired.
1154 //
1155 // We have to do this via InvokeLater to avoid "recursing" the consumer.
1156 //
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001157 base::MessageLoop::current()->PostTask(
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001158 FROM_HERE,
1159 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1160 weak_factory_.GetWeakPtr(), OK));
1161}
1162
1163void URLRequestHttpJob::ContinueWithCertificate(
1164 X509Certificate* client_cert) {
1165 DCHECK(transaction_.get());
1166
1167 DCHECK(!response_info_) << "should not have a response yet";
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001168 receive_headers_end_ = base::TimeTicks();
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001169
1170 ResetTimer();
1171
1172 // No matter what, we want to report our status as IO pending since we will
1173 // be notifying our consumer asynchronously via OnStartCompleted.
1174 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1175
1176 int rv = transaction_->RestartWithCertificate(client_cert, start_callback_);
1177 if (rv == ERR_IO_PENDING)
1178 return;
1179
1180 // The transaction started synchronously, but we need to notify the
1181 // URLRequest delegate via the message loop.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001182 base::MessageLoop::current()->PostTask(
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001183 FROM_HERE,
1184 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1185 weak_factory_.GetWeakPtr(), rv));
1186}
1187
1188void URLRequestHttpJob::ContinueDespiteLastError() {
1189 // If the transaction was destroyed, then the job was cancelled.
1190 if (!transaction_.get())
1191 return;
1192
1193 DCHECK(!response_info_) << "should not have a response yet";
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001194 receive_headers_end_ = base::TimeTicks();
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001195
1196 ResetTimer();
1197
1198 // No matter what, we want to report our status as IO pending since we will
1199 // be notifying our consumer asynchronously via OnStartCompleted.
1200 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1201
1202 int rv = transaction_->RestartIgnoringLastError(start_callback_);
1203 if (rv == ERR_IO_PENDING)
1204 return;
1205
1206 // The transaction started synchronously, but we need to notify the
1207 // URLRequest delegate via the message loop.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001208 base::MessageLoop::current()->PostTask(
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001209 FROM_HERE,
1210 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1211 weak_factory_.GetWeakPtr(), rv));
1212}
1213
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +00001214void URLRequestHttpJob::ResumeNetworkStart() {
1215 DCHECK(transaction_.get());
1216 transaction_->ResumeNetworkStart();
1217}
1218
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001219bool URLRequestHttpJob::ShouldFixMismatchedContentLength(int rv) const {
1220 // Some servers send the body compressed, but specify the content length as
1221 // the uncompressed size. Although this violates the HTTP spec we want to
1222 // support it (as IE and FireFox do), but *only* for an exact match.
1223 // See http://crbug.com/79694.
1224 if (rv == net::ERR_CONTENT_LENGTH_MISMATCH ||
1225 rv == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
1226 if (request_ && request_->response_headers()) {
1227 int64 expected_length = request_->response_headers()->GetContentLength();
1228 VLOG(1) << __FUNCTION__ << "() "
1229 << "\"" << request_->url().spec() << "\""
1230 << " content-length = " << expected_length
1231 << " pre total = " << prefilter_bytes_read()
1232 << " post total = " << postfilter_bytes_read();
1233 if (postfilter_bytes_read() == expected_length) {
1234 // Clear the error.
1235 return true;
1236 }
1237 }
1238 }
1239 return false;
1240}
1241
1242bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size,
1243 int* bytes_read) {
1244 DCHECK_NE(buf_size, 0);
1245 DCHECK(bytes_read);
1246 DCHECK(!read_in_progress_);
1247
1248 int rv = transaction_->Read(
1249 buf, buf_size,
1250 base::Bind(&URLRequestHttpJob::OnReadCompleted, base::Unretained(this)));
1251
1252 if (ShouldFixMismatchedContentLength(rv))
1253 rv = 0;
1254
1255 if (rv >= 0) {
1256 *bytes_read = rv;
1257 if (!rv)
1258 DoneWithRequest(FINISHED);
1259 return true;
1260 }
1261
1262 if (rv == ERR_IO_PENDING) {
1263 read_in_progress_ = true;
1264 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1265 } else {
1266 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
1267 }
1268
1269 return false;
1270}
1271
1272void URLRequestHttpJob::StopCaching() {
1273 if (transaction_.get())
1274 transaction_->StopCaching();
1275}
1276
Ben Murdocheb525c52013-07-10 11:40:50 +01001277bool URLRequestHttpJob::GetFullRequestHeaders(
1278 HttpRequestHeaders* headers) const {
1279 if (!transaction_)
1280 return false;
1281
1282 return transaction_->GetFullRequestHeaders(headers);
1283}
1284
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +00001285int64 URLRequestHttpJob::GetTotalReceivedBytes() const {
1286 if (!transaction_)
1287 return 0;
1288
1289 return transaction_->GetTotalReceivedBytes();
1290}
1291
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001292void URLRequestHttpJob::DoneReading() {
Ben Murdocheffb81e2014-03-31 11:51:25 +01001293 if (transaction_) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001294 transaction_->DoneReading();
Ben Murdocheffb81e2014-03-31 11:51:25 +01001295 }
1296 DoneWithRequest(FINISHED);
1297}
1298
1299void URLRequestHttpJob::DoneReadingRedirectResponse() {
1300 if (transaction_) {
1301 if (transaction_->GetResponseInfo()->headers->IsRedirect(NULL)) {
1302 // If the original headers indicate a redirect, go ahead and cache the
1303 // response, even if the |override_response_headers_| are a redirect to
1304 // another location.
1305 transaction_->DoneReading();
1306 } else {
1307 // Otherwise, |override_response_headers_| must be non-NULL and contain
1308 // bogus headers indicating a redirect.
1309 DCHECK(override_response_headers_);
1310 DCHECK(override_response_headers_->IsRedirect(NULL));
1311 transaction_->StopCaching();
1312 }
1313 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001314 DoneWithRequest(FINISHED);
1315}
1316
1317HostPortPair URLRequestHttpJob::GetSocketAddress() const {
1318 return response_info_ ? response_info_->socket_address : HostPortPair();
1319}
1320
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001321void URLRequestHttpJob::RecordTimer() {
1322 if (request_creation_time_.is_null()) {
1323 NOTREACHED()
1324 << "The same transaction shouldn't start twice without new timing.";
1325 return;
1326 }
1327
1328 base::TimeDelta to_start = base::Time::Now() - request_creation_time_;
1329 request_creation_time_ = base::Time();
1330
1331 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001332}
1333
1334void URLRequestHttpJob::ResetTimer() {
1335 if (!request_creation_time_.is_null()) {
1336 NOTREACHED()
1337 << "The timer was reset before it was recorded.";
1338 return;
1339 }
1340 request_creation_time_ = base::Time::Now();
1341}
1342
1343void URLRequestHttpJob::UpdatePacketReadTimes() {
1344 if (!packet_timing_enabled_)
1345 return;
1346
1347 if (filter_input_byte_count() <= bytes_observed_in_packets_) {
1348 DCHECK_EQ(filter_input_byte_count(), bytes_observed_in_packets_);
1349 return; // No new bytes have arrived.
1350 }
1351
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +01001352 base::Time now(base::Time::Now());
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001353 if (!bytes_observed_in_packets_)
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +01001354 request_time_snapshot_ = now;
1355 final_packet_time_ = now;
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001356
1357 bytes_observed_in_packets_ = filter_input_byte_count();
1358}
1359
1360void URLRequestHttpJob::RecordPacketStats(
1361 FilterContext::StatisticSelector statistic) const {
1362 if (!packet_timing_enabled_ || (final_packet_time_ == base::Time()))
1363 return;
1364
1365 base::TimeDelta duration = final_packet_time_ - request_time_snapshot_;
1366 switch (statistic) {
1367 case FilterContext::SDCH_DECODE: {
1368 UMA_HISTOGRAM_CUSTOM_COUNTS("Sdch3.Network_Decode_Bytes_Processed_b",
1369 static_cast<int>(bytes_observed_in_packets_), 500, 100000, 100);
1370 return;
1371 }
1372 case FilterContext::SDCH_PASSTHROUGH: {
1373 // Despite advertising a dictionary, we handled non-sdch compressed
1374 // content.
1375 return;
1376 }
1377
1378 case FilterContext::SDCH_EXPERIMENT_DECODE: {
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +01001379 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment3_Decode",
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001380 duration,
1381 base::TimeDelta::FromMilliseconds(20),
1382 base::TimeDelta::FromMinutes(10), 100);
1383 return;
1384 }
1385 case FilterContext::SDCH_EXPERIMENT_HOLDBACK: {
Torne (Richard Coles)5f1c9432014-08-12 13:47:38 +01001386 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment3_Holdback",
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001387 duration,
1388 base::TimeDelta::FromMilliseconds(20),
1389 base::TimeDelta::FromMinutes(10), 100);
1390 return;
1391 }
1392 default:
1393 NOTREACHED();
1394 return;
1395 }
1396}
1397
1398// The common type of histogram we use for all compression-tracking histograms.
1399#define COMPRESSION_HISTOGRAM(name, sample) \
1400 do { \
1401 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Compress." name, sample, \
1402 500, 1000000, 100); \
1403 } while (0)
1404
1405void URLRequestHttpJob::RecordCompressionHistograms() {
1406 DCHECK(request_);
1407 if (!request_)
1408 return;
1409
1410 if (is_cached_content_ || // Don't record cached content
1411 !GetStatus().is_success() || // Don't record failed content
1412 !IsCompressibleContent() || // Only record compressible content
1413 !prefilter_bytes_read()) // Zero-byte responses aren't useful.
1414 return;
1415
1416 // Miniature requests aren't really compressible. Don't count them.
1417 const int kMinSize = 16;
1418 if (prefilter_bytes_read() < kMinSize)
1419 return;
1420
1421 // Only record for http or https urls.
1422 bool is_http = request_->url().SchemeIs("http");
1423 bool is_https = request_->url().SchemeIs("https");
1424 if (!is_http && !is_https)
1425 return;
1426
1427 int compressed_B = prefilter_bytes_read();
1428 int decompressed_B = postfilter_bytes_read();
1429 bool was_filtered = HasFilter();
1430
1431 // We want to record how often downloaded resources are compressed.
1432 // But, we recognize that different protocols may have different
1433 // properties. So, for each request, we'll put it into one of 3
1434 // groups:
1435 // a) SSL resources
1436 // Proxies cannot tamper with compression headers with SSL.
1437 // b) Non-SSL, loaded-via-proxy resources
1438 // In this case, we know a proxy might have interfered.
1439 // c) Non-SSL, loaded-without-proxy resources
1440 // In this case, we know there was no explicit proxy. However,
1441 // it is possible that a transparent proxy was still interfering.
1442 //
1443 // For each group, we record the same 3 histograms.
1444
1445 if (is_https) {
1446 if (was_filtered) {
1447 COMPRESSION_HISTOGRAM("SSL.BytesBeforeCompression", compressed_B);
1448 COMPRESSION_HISTOGRAM("SSL.BytesAfterCompression", decompressed_B);
1449 } else {
1450 COMPRESSION_HISTOGRAM("SSL.ShouldHaveBeenCompressed", decompressed_B);
1451 }
1452 return;
1453 }
1454
1455 if (request_->was_fetched_via_proxy()) {
1456 if (was_filtered) {
1457 COMPRESSION_HISTOGRAM("Proxy.BytesBeforeCompression", compressed_B);
1458 COMPRESSION_HISTOGRAM("Proxy.BytesAfterCompression", decompressed_B);
1459 } else {
1460 COMPRESSION_HISTOGRAM("Proxy.ShouldHaveBeenCompressed", decompressed_B);
1461 }
1462 return;
1463 }
1464
1465 if (was_filtered) {
1466 COMPRESSION_HISTOGRAM("NoProxy.BytesBeforeCompression", compressed_B);
1467 COMPRESSION_HISTOGRAM("NoProxy.BytesAfterCompression", decompressed_B);
1468 } else {
1469 COMPRESSION_HISTOGRAM("NoProxy.ShouldHaveBeenCompressed", decompressed_B);
1470 }
1471}
1472
1473bool URLRequestHttpJob::IsCompressibleContent() const {
1474 std::string mime_type;
1475 return GetMimeType(&mime_type) &&
1476 (IsSupportedJavascriptMimeType(mime_type.c_str()) ||
1477 IsSupportedNonImageMimeType(mime_type.c_str()));
1478}
1479
1480void URLRequestHttpJob::RecordPerfHistograms(CompletionCause reason) {
1481 if (start_time_.is_null())
1482 return;
1483
1484 base::TimeDelta total_time = base::TimeTicks::Now() - start_time_;
1485 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTime", total_time);
1486
1487 if (reason == FINISHED) {
1488 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeSuccess", total_time);
1489 } else {
1490 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCancel", total_time);
1491 }
1492
1493 if (response_info_) {
1494 if (response_info_->was_cached) {
1495 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCached", total_time);
1496 } else {
1497 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeNotCached", total_time);
1498 }
1499 }
1500
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +00001501 if (request_info_.load_flags & LOAD_PREFETCH && !request_->was_cached())
1502 UMA_HISTOGRAM_COUNTS("Net.Prefetch.PrefilterBytesReadFromNetwork",
1503 prefilter_bytes_read());
1504
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001505 start_time_ = base::TimeTicks();
1506}
1507
1508void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) {
1509 if (done_)
1510 return;
1511 done_ = true;
1512 RecordPerfHistograms(reason);
1513 if (reason == FINISHED) {
1514 request_->set_received_response_content_length(prefilter_bytes_read());
1515 RecordCompressionHistograms();
1516 }
1517}
1518
1519HttpResponseHeaders* URLRequestHttpJob::GetResponseHeaders() const {
1520 DCHECK(transaction_.get());
1521 DCHECK(transaction_->GetResponseInfo());
1522 return override_response_headers_.get() ?
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001523 override_response_headers_.get() :
1524 transaction_->GetResponseInfo()->headers.get();
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001525}
1526
1527void URLRequestHttpJob::NotifyURLRequestDestroyed() {
1528 awaiting_callback_ = false;
1529}
1530
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001531} // namespace net