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.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/string16.h"
15 #include "base/task.h"
16 #include "base/time.h"
17 #include "net/base/auth.h"
18 #include "net/base/completion_callback.h"
19 #include "net/base/cookie_store.h"
20 #include "net/http/http_request_info.h"
21 #include "net/url_request/url_request_job.h"
22 #include "net/url_request/url_request_throttler_entry_interface.h"
26 class HttpResponseInfo
;
27 class HttpTransaction
;
28 class URLRequestContext
;
30 // A URLRequestJob subclass that is built on top of HttpTransaction. It
31 // provides an implementation for both HTTP and HTTPS.
32 class URLRequestHttpJob
: public URLRequestJob
{
34 static URLRequestJob
* Factory(URLRequest
* request
,
35 const std::string
& scheme
);
38 explicit URLRequestHttpJob(URLRequest
* request
);
40 // Shadows URLRequestJob's version of this method so we can grab cookies.
41 void NotifyHeadersComplete();
43 // Shadows URLRequestJob's method so we can record histograms.
44 void NotifyDone(const URLRequestStatus
& status
);
46 void DestroyTransaction();
48 void AddExtraHeaders();
49 void AddCookieHeaderAndStart();
50 void SaveCookiesAndNotifyHeadersComplete();
51 void SaveNextCookie();
52 void FetchResponseCookies(const HttpResponseInfo
* response_info
,
53 std::vector
<std::string
>* cookies
);
55 // Process the Strict-Transport-Security header, if one exists.
56 void ProcessStrictTransportSecurityHeader();
58 void OnStartCompleted(int result
);
59 void OnReadCompleted(int result
);
60 void NotifyBeforeSendHeadersCallback(int result
);
62 bool ShouldTreatAsCertificateError(int result
);
64 void RestartTransactionWithAuth(const string16
& username
,
65 const string16
& password
);
67 // Overridden from URLRequestJob:
68 virtual void SetUpload(UploadData
* upload
) OVERRIDE
;
69 virtual void SetExtraRequestHeaders(
70 const HttpRequestHeaders
& headers
) OVERRIDE
;
71 virtual void Start() OVERRIDE
;
72 virtual void Kill() OVERRIDE
;
73 virtual LoadState
GetLoadState() const OVERRIDE
;
74 virtual uint64
GetUploadProgress() const OVERRIDE
;
75 virtual bool GetMimeType(std::string
* mime_type
) const OVERRIDE
;
76 virtual bool GetCharset(std::string
* charset
) OVERRIDE
;
77 virtual void GetResponseInfo(HttpResponseInfo
* info
) OVERRIDE
;
78 virtual bool GetResponseCookies(std::vector
<std::string
>* cookies
) OVERRIDE
;
79 virtual int GetResponseCode() const OVERRIDE
;
80 virtual Filter
* SetupFilter() const OVERRIDE
;
81 virtual bool IsSafeRedirect(const GURL
& location
) OVERRIDE
;
82 virtual bool NeedsAuth() OVERRIDE
;
83 virtual void GetAuthChallengeInfo(scoped_refptr
<AuthChallengeInfo
>*) OVERRIDE
;
84 virtual void SetAuth(const string16
& username
,
85 const string16
& password
) OVERRIDE
;
86 virtual void CancelAuth() OVERRIDE
;
87 virtual void ContinueWithCertificate(X509Certificate
* client_cert
) OVERRIDE
;
88 virtual void ContinueDespiteLastError() OVERRIDE
;
89 virtual bool ReadRawData(IOBuffer
* buf
, int buf_size
,
90 int *bytes_read
) OVERRIDE
;
91 virtual void StopCaching() OVERRIDE
;
92 virtual void DoneReading() OVERRIDE
;
93 virtual HostPortPair
GetSocketAddress() const OVERRIDE
;
95 // Keep a reference to the url request context to be sure it's not deleted
97 scoped_refptr
<const URLRequestContext
> context_
;
99 HttpRequestInfo request_info_
;
100 const HttpResponseInfo
* response_info_
;
102 std::vector
<std::string
> response_cookies_
;
103 size_t response_cookies_save_index_
;
105 // Auth states for proxy and origin server.
106 AuthState proxy_auth_state_
;
107 AuthState server_auth_state_
;
112 CompletionCallbackImpl
<URLRequestHttpJob
> start_callback_
;
113 CompletionCallbackImpl
<URLRequestHttpJob
> read_callback_
;
114 CompletionCallbackImpl
<URLRequestHttpJob
>
115 notify_before_headers_sent_callback_
;
117 bool read_in_progress_
;
119 // An URL for an SDCH dictionary as suggested in a Get-Dictionary HTTP header.
120 GURL sdch_dictionary_url_
;
122 scoped_ptr
<HttpTransaction
> transaction_
;
124 // This is used to supervise traffic and enforce exponential back-off.
125 scoped_refptr
<URLRequestThrottlerEntryInterface
> throttling_entry_
;
127 // Indicated if an SDCH dictionary was advertised, and hence an SDCH
128 // compressed response is expected. We use this to help detect (accidental?)
129 // proxy corruption of a response, which sometimes marks SDCH content as
130 // having no content encoding <oops>.
131 bool sdch_dictionary_advertised_
;
133 // For SDCH latency experiments, when we are able to do SDCH, we may enable
134 // either an SDCH latency test xor a pass through test. The following bools
135 // indicate what we decided on for this instance.
136 bool sdch_test_activated_
; // Advertising a dictionary for sdch.
137 bool sdch_test_control_
; // Not even accepting-content sdch.
139 // For recording of stats, we need to remember if this is cached content.
140 bool is_cached_content_
;
143 enum CompletionCause
{
148 class HttpFilterContext
;
150 virtual ~URLRequestHttpJob();
155 virtual void UpdatePacketReadTimes();
156 void RecordPacketStats(FilterContext::StatisticSelector statistic
) const;
158 void RecordCompressionHistograms();
159 bool IsCompressibleContent() const;
161 // Starts the transaction if extensions using the webrequest API do not
163 void StartTransaction();
164 void StartTransactionInternal();
166 void RecordPerfHistograms(CompletionCause reason
);
167 void DoneWithRequest(CompletionCause reason
);
169 // Callback functions for Cookie Monster
170 void CheckCookiePolicyAndLoad(const CookieList
& cookie_list
);
171 void OnCookiesLoaded(
172 std::string
* cookie_line
,
173 std::vector
<CookieStore::CookieInfo
>* cookie_infos
);
174 void DoStartTransaction();
175 void OnCookieSaved(bool cookie_status
);
176 void CookieHandled();
178 // Some servers send the body compressed, but specify the content length as
179 // the uncompressed size. If this is the case, we return true in order
180 // to request to work around this non-adherence to the HTTP standard.
181 // |rv| is the standard return value of a read function indicating the number
182 // of bytes read or, if negative, an error code.
183 bool ShouldFixMismatchedContentLength(int rv
) const;
185 base::Time request_creation_time_
;
187 // Data used for statistics gathering. This data is only used for histograms
188 // and is not required. It is only gathered if packet_timing_enabled_ == true.
190 // TODO(jar): improve the quality of the gathered info by gathering most times
191 // at a lower point in the network stack, assuring we have actual packet
192 // boundaries, rather than approximations. Also note that input byte count
193 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
194 // true packet arrival times in such cases.
196 // Enable recording of packet arrival times for histogramming.
197 bool packet_timing_enabled_
;
198 bool done_
; // True when we are done doing work.
200 // The number of bytes that have been accounted for in packets (where some of
201 // those packets may possibly have had their time of arrival recorded).
202 int64 bytes_observed_in_packets_
;
204 // The request time may not be available when we are being destroyed, so we
205 // snapshot it early on.
206 base::Time request_time_snapshot_
;
208 // Since we don't save all packet times in packet_times_, we save the
209 // last time for use in histograms.
210 base::Time final_packet_time_
;
212 // The start time for the job, ignoring re-starts.
213 base::TimeTicks start_time_
;
215 scoped_ptr
<HttpFilterContext
> filter_context_
;
216 ScopedRunnableMethodFactory
<URLRequestHttpJob
> method_factory_
;
217 base::WeakPtrFactory
<URLRequestHttpJob
> weak_ptr_factory_
;
219 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob
);
224 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_