Home | History | Annotate | Download | only in url_request
      1 // Copyright (c) 2011 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 #ifndef NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
      6 #define NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
      7 #pragma once
      8 
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/string16.h"
     14 #include "base/task.h"
     15 #include "base/time.h"
     16 #include "net/base/auth.h"
     17 #include "net/base/completion_callback.h"
     18 #include "net/http/http_request_info.h"
     19 #include "net/url_request/url_request_job.h"
     20 #include "net/url_request/url_request_throttler_entry_interface.h"
     21 
     22 namespace net {
     23 
     24 class HttpResponseInfo;
     25 class HttpTransaction;
     26 class URLRequestContext;
     27 
     28 // A URLRequestJob subclass that is built on top of HttpTransaction.  It
     29 // provides an implementation for both HTTP and HTTPS.
     30 class URLRequestHttpJob : public URLRequestJob {
     31  public:
     32   static URLRequestJob* Factory(URLRequest* request,
     33                                 const std::string& scheme);
     34 
     35  protected:
     36   explicit URLRequestHttpJob(URLRequest* request);
     37 
     38   // Shadows URLRequestJob's version of this method so we can grab cookies.
     39   void NotifyHeadersComplete();
     40 
     41   // Shadows URLRequestJob's method so we can record histograms.
     42   void NotifyDone(const URLRequestStatus& status);
     43 
     44   void DestroyTransaction();
     45   void StartTransaction();
     46   void AddExtraHeaders();
     47   void AddCookieHeaderAndStart();
     48   void SaveCookiesAndNotifyHeadersComplete();
     49   void SaveNextCookie();
     50   void FetchResponseCookies(const HttpResponseInfo* response_info,
     51                             std::vector<std::string>* cookies);
     52 
     53   // Process the Strict-Transport-Security header, if one exists.
     54   void ProcessStrictTransportSecurityHeader();
     55 
     56   void OnCanGetCookiesCompleted(int result);
     57   void OnCanSetCookieCompleted(int result);
     58   void OnStartCompleted(int result);
     59   void OnReadCompleted(int result);
     60 
     61   bool ShouldTreatAsCertificateError(int result);
     62 
     63   void RestartTransactionWithAuth(const string16& username,
     64                                   const string16& password);
     65 
     66   // Overridden from URLRequestJob:
     67   virtual void SetUpload(UploadData* upload);
     68   virtual void SetExtraRequestHeaders(const HttpRequestHeaders& headers);
     69   virtual void Start();
     70   virtual void Kill();
     71   virtual LoadState GetLoadState() const;
     72   virtual uint64 GetUploadProgress() const;
     73   virtual bool GetMimeType(std::string* mime_type) const;
     74   virtual bool GetCharset(std::string* charset);
     75   virtual void GetResponseInfo(HttpResponseInfo* info);
     76   virtual bool GetResponseCookies(std::vector<std::string>* cookies);
     77   virtual int GetResponseCode() const;
     78   virtual Filter* SetupFilter() const;
     79   virtual bool IsSafeRedirect(const GURL& location);
     80   virtual bool NeedsAuth();
     81   virtual void GetAuthChallengeInfo(scoped_refptr<AuthChallengeInfo>*);
     82   virtual void SetAuth(const string16& username,
     83                        const string16& password);
     84   virtual void CancelAuth();
     85   virtual void ContinueWithCertificate(X509Certificate* client_cert);
     86   virtual void ContinueDespiteLastError();
     87   virtual bool ReadRawData(IOBuffer* buf, int buf_size, int *bytes_read);
     88   virtual void StopCaching();
     89   virtual HostPortPair GetSocketAddress() const;
     90 
     91   // Keep a reference to the url request context to be sure it's not deleted
     92   // before us.
     93   scoped_refptr<URLRequestContext> context_;
     94 
     95   HttpRequestInfo request_info_;
     96   const HttpResponseInfo* response_info_;
     97 
     98   std::vector<std::string> response_cookies_;
     99   size_t response_cookies_save_index_;
    100 
    101   // Auth states for proxy and origin server.
    102   AuthState proxy_auth_state_;
    103   AuthState server_auth_state_;
    104 
    105   string16 username_;
    106   string16 password_;
    107 
    108   CompletionCallbackImpl<URLRequestHttpJob> start_callback_;
    109   CompletionCallbackImpl<URLRequestHttpJob> read_callback_;
    110 
    111   bool read_in_progress_;
    112 
    113   // An URL for an SDCH dictionary as suggested in a Get-Dictionary HTTP header.
    114   GURL sdch_dictionary_url_;
    115 
    116   scoped_ptr<HttpTransaction> transaction_;
    117 
    118   // This is used to supervise traffic and enforce exponential back-off.
    119   scoped_refptr<URLRequestThrottlerEntryInterface> throttling_entry_;
    120 
    121   // Indicated if an SDCH dictionary was advertised, and hence an SDCH
    122   // compressed response is expected.  We use this to help detect (accidental?)
    123   // proxy corruption of a response, which sometimes marks SDCH content as
    124   // having no content encoding <oops>.
    125   bool sdch_dictionary_advertised_;
    126 
    127   // For SDCH latency experiments, when we are able to do SDCH, we may enable
    128   // either an SDCH latency test xor a pass through test.  The following bools
    129   // indicate what we decided on for this instance.
    130   bool sdch_test_activated_;  // Advertising a dictionary for sdch.
    131   bool sdch_test_control_;    // Not even accepting-content sdch.
    132 
    133   // For recording of stats, we need to remember if this is cached content.
    134   bool is_cached_content_;
    135 
    136  private:
    137   class HttpFilterContext;
    138 
    139   virtual ~URLRequestHttpJob();
    140 
    141   void RecordTimer();
    142   void ResetTimer();
    143 
    144   virtual void UpdatePacketReadTimes();
    145   void RecordPacketStats(FilterContext::StatisticSelector statistic) const;
    146 
    147   void RecordCompressionHistograms();
    148   bool IsCompressibleContent() const;
    149 
    150   base::Time request_creation_time_;
    151 
    152   // Data used for statistics gathering. This data is only used for histograms
    153   // and is not required. It is only gathered if packet_timing_enabled_ == true.
    154   //
    155   // TODO(jar): improve the quality of the gathered info by gathering most times
    156   // at a lower point in the network stack, assuring we have actual packet
    157   // boundaries, rather than approximations.  Also note that input byte count
    158   // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
    159   // true packet arrival times in such cases.
    160 
    161   // Enable recording of packet arrival times for histogramming.
    162   bool packet_timing_enabled_;
    163 
    164   // The number of bytes that have been accounted for in packets (where some of
    165   // those packets may possibly have had their time of arrival recorded).
    166   int64 bytes_observed_in_packets_;
    167 
    168   // Arrival times for some of the first few packets.
    169   std::vector<base::Time> packet_times_;
    170 
    171   // The request time may not be available when we are being destroyed, so we
    172   // snapshot it early on.
    173   base::Time request_time_snapshot_;
    174 
    175   // Since we don't save all packet times in packet_times_, we save the
    176   // last time for use in histograms.
    177   base::Time final_packet_time_;
    178 
    179   // The count of the number of packets, some of which may not have been timed.
    180   // We're ignoring overflow, as 1430 x 2^31 is a LOT of bytes.
    181   int observed_packet_count_;
    182 
    183   scoped_ptr<HttpFilterContext> filter_context_;
    184   ScopedRunnableMethodFactory<URLRequestHttpJob> method_factory_;
    185 
    186   DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob);
    187 };
    188 
    189 }  // namespace net
    190 
    191 #endif  // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
    192