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