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.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "net/base/auth.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/net_export.h"
18 #include "net/cookies/cookie_store.h"
19 #include "net/filter/filter.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 HttpResponseHeaders
;
27 class HttpResponseInfo
;
28 class HttpTransaction
;
29 class HttpUserAgentSettings
;
30 class UploadDataStream
;
31 class URLRequestContext
;
33 // A URLRequestJob subclass that is built on top of HttpTransaction. It
34 // provides an implementation for both HTTP and HTTPS.
35 class NET_EXPORT_PRIVATE URLRequestHttpJob
: public URLRequestJob
{
37 static URLRequestJob
* Factory(URLRequest
* request
,
38 NetworkDelegate
* network_delegate
,
39 const std::string
& scheme
);
42 URLRequestHttpJob(URLRequest
* request
,
43 NetworkDelegate
* network_delegate
,
44 const HttpUserAgentSettings
* http_user_agent_settings
);
46 virtual ~URLRequestHttpJob();
48 // Overridden from URLRequestJob:
49 virtual void SetPriority(RequestPriority priority
) OVERRIDE
;
50 virtual void Start() OVERRIDE
;
51 virtual void Kill() OVERRIDE
;
53 RequestPriority
priority() const {
58 enum CompletionCause
{
63 typedef base::RefCountedData
<bool> SharedBoolean
;
65 class HttpFilterContext
;
66 class HttpTransactionDelegateImpl
;
68 // Shadows URLRequestJob's version of this method so we can grab cookies.
69 void NotifyHeadersComplete();
71 // Shadows URLRequestJob's method so we can record histograms.
72 void NotifyDone(const URLRequestStatus
& status
);
74 void DestroyTransaction();
76 void AddExtraHeaders();
77 void AddCookieHeaderAndStart();
78 void SaveCookiesAndNotifyHeadersComplete(int result
);
79 void SaveNextCookie();
80 void FetchResponseCookies(std::vector
<std::string
>* cookies
);
82 // Processes the Strict-Transport-Security header, if one exists.
83 void ProcessStrictTransportSecurityHeader();
85 // Processes the Public-Key-Pins header, if one exists.
86 void ProcessPublicKeyPinsHeader();
88 // |result| should be net::OK, or the request is canceled.
89 void OnHeadersReceivedCallback(int result
);
90 void OnStartCompleted(int result
);
91 void OnReadCompleted(int result
);
92 void NotifyBeforeSendHeadersCallback(int result
);
94 void RestartTransactionWithAuth(const AuthCredentials
& credentials
);
96 // Overridden from URLRequestJob:
97 virtual void SetUpload(UploadDataStream
* upload
) OVERRIDE
;
98 virtual void SetExtraRequestHeaders(
99 const HttpRequestHeaders
& headers
) OVERRIDE
;
100 virtual LoadState
GetLoadState() const OVERRIDE
;
101 virtual UploadProgress
GetUploadProgress() const OVERRIDE
;
102 virtual bool GetMimeType(std::string
* mime_type
) const OVERRIDE
;
103 virtual bool GetCharset(std::string
* charset
) OVERRIDE
;
104 virtual void GetResponseInfo(HttpResponseInfo
* info
) OVERRIDE
;
105 virtual void GetLoadTimingInfo(
106 LoadTimingInfo
* load_timing_info
) const OVERRIDE
;
107 virtual bool GetResponseCookies(std::vector
<std::string
>* cookies
) OVERRIDE
;
108 virtual int GetResponseCode() const OVERRIDE
;
109 virtual Filter
* SetupFilter() const OVERRIDE
;
110 virtual bool IsSafeRedirect(const GURL
& location
) OVERRIDE
;
111 virtual bool NeedsAuth() OVERRIDE
;
112 virtual void GetAuthChallengeInfo(scoped_refptr
<AuthChallengeInfo
>*) OVERRIDE
;
113 virtual void SetAuth(const AuthCredentials
& credentials
) OVERRIDE
;
114 virtual void CancelAuth() OVERRIDE
;
115 virtual void ContinueWithCertificate(X509Certificate
* client_cert
) OVERRIDE
;
116 virtual void ContinueDespiteLastError() OVERRIDE
;
117 virtual void ResumeNetworkStart() OVERRIDE
;
118 virtual bool ReadRawData(IOBuffer
* buf
, int buf_size
,
119 int* bytes_read
) OVERRIDE
;
120 virtual void StopCaching() OVERRIDE
;
121 virtual bool GetFullRequestHeaders(
122 HttpRequestHeaders
* headers
) const OVERRIDE
;
123 virtual int64
GetTotalReceivedBytes() const OVERRIDE
;
124 virtual void DoneReading() OVERRIDE
;
125 virtual void DoneReadingRedirectResponse() OVERRIDE
;
127 virtual HostPortPair
GetSocketAddress() const OVERRIDE
;
128 virtual void NotifyURLRequestDestroyed() OVERRIDE
;
133 virtual void UpdatePacketReadTimes() OVERRIDE
;
134 void RecordPacketStats(FilterContext::StatisticSelector statistic
) const;
136 void RecordCompressionHistograms();
137 bool IsCompressibleContent() const;
139 // Starts the transaction if extensions using the webrequest API do not
141 void StartTransaction();
142 // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies
144 void MaybeStartTransactionInternal(int result
);
145 void StartTransactionInternal();
147 void RecordPerfHistograms(CompletionCause reason
);
148 void DoneWithRequest(CompletionCause reason
);
150 // Callback functions for Cookie Monster
151 void DoLoadCookies();
152 void CheckCookiePolicyAndLoad(const CookieList
& cookie_list
);
153 void OnCookiesLoaded(const std::string
& cookie_line
);
154 void DoStartTransaction();
156 // See the implementation for a description of save_next_cookie_running and
158 void OnCookieSaved(scoped_refptr
<SharedBoolean
> save_next_cookie_running
,
159 scoped_refptr
<SharedBoolean
> callback_pending
,
162 // Some servers send the body compressed, but specify the content length as
163 // the uncompressed size. If this is the case, we return true in order
164 // to request to work around this non-adherence to the HTTP standard.
165 // |rv| is the standard return value of a read function indicating the number
166 // of bytes read or, if negative, an error code.
167 bool ShouldFixMismatchedContentLength(int rv
) const;
169 // Returns the effective response headers, considering that they may be
170 // overridden by |override_response_headers_|.
171 HttpResponseHeaders
* GetResponseHeaders() const;
173 RequestPriority priority_
;
175 HttpRequestInfo request_info_
;
176 const HttpResponseInfo
* response_info_
;
178 std::vector
<std::string
> response_cookies_
;
179 size_t response_cookies_save_index_
;
180 base::Time response_date_
;
182 // Auth states for proxy and origin server.
183 AuthState proxy_auth_state_
;
184 AuthState server_auth_state_
;
185 AuthCredentials auth_credentials_
;
187 CompletionCallback start_callback_
;
188 CompletionCallback notify_before_headers_sent_callback_
;
190 bool read_in_progress_
;
192 // An URL for an SDCH dictionary as suggested in a Get-Dictionary HTTP header.
193 GURL sdch_dictionary_url_
;
195 scoped_ptr
<HttpTransaction
> transaction_
;
197 // This is used to supervise traffic and enforce exponential
198 // back-off. May be NULL.
199 scoped_refptr
<URLRequestThrottlerEntryInterface
> throttling_entry_
;
201 // Indicated if an SDCH dictionary was advertised, and hence an SDCH
202 // compressed response is expected. We use this to help detect (accidental?)
203 // proxy corruption of a response, which sometimes marks SDCH content as
204 // having no content encoding <oops>.
205 bool sdch_dictionary_advertised_
;
207 // For SDCH latency experiments, when we are able to do SDCH, we may enable
208 // either an SDCH latency test xor a pass through test. The following bools
209 // indicate what we decided on for this instance.
210 bool sdch_test_activated_
; // Advertising a dictionary for sdch.
211 bool sdch_test_control_
; // Not even accepting-content sdch.
213 // For recording of stats, we need to remember if this is cached content.
214 bool is_cached_content_
;
216 base::Time request_creation_time_
;
218 // Data used for statistics gathering. This data is only used for histograms
219 // and is not required. It is only gathered if packet_timing_enabled_ == true.
221 // TODO(jar): improve the quality of the gathered info by gathering most times
222 // at a lower point in the network stack, assuring we have actual packet
223 // boundaries, rather than approximations. Also note that input byte count
224 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
225 // true packet arrival times in such cases.
227 // Enable recording of packet arrival times for histogramming.
228 bool packet_timing_enabled_
;
229 bool done_
; // True when we are done doing work.
231 // The number of bytes that have been accounted for in packets (where some of
232 // those packets may possibly have had their time of arrival recorded).
233 int64 bytes_observed_in_packets_
;
235 // The request time may not be available when we are being destroyed, so we
236 // snapshot it early on.
237 base::Time request_time_snapshot_
;
239 // Since we don't save all packet times in packet_times_, we save the
240 // last time for use in histograms.
241 base::Time final_packet_time_
;
243 // The start time for the job, ignoring re-starts.
244 base::TimeTicks start_time_
;
246 // When the transaction finished reading the request headers.
247 base::TimeTicks receive_headers_end_
;
249 scoped_ptr
<HttpFilterContext
> filter_context_
;
250 base::WeakPtrFactory
<URLRequestHttpJob
> weak_factory_
;
252 CompletionCallback on_headers_received_callback_
;
254 // We allow the network delegate to modify a copy of the response headers.
255 // This prevents modifications of headers that are shared with the underlying
256 // layers of the network stack.
257 scoped_refptr
<HttpResponseHeaders
> override_response_headers_
;
259 // The network delegate can mark a URL as safe for redirection.
260 GURL allowed_unsafe_redirect_url_
;
262 // Flag used to verify that |this| is not deleted while we are awaiting
263 // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
264 // True if we are waiting a callback and
265 // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
266 // to inform the NetworkDelegate that it may not call back.
267 bool awaiting_callback_
;
269 const HttpUserAgentSettings
* http_user_agent_settings_
;
271 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob
);
276 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_