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_
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/time/time.h"
17 #include "net/base/auth.h"
18 #include "net/base/completion_callback.h"
19 #include "net/base/net_export.h"
20 #include "net/base/sdch_manager.h"
21 #include "net/cookies/cookie_store.h"
22 #include "net/filter/filter.h"
23 #include "net/http/http_request_info.h"
24 #include "net/socket/connection_attempts.h"
25 #include "net/url_request/url_request_backoff_manager.h"
26 #include "net/url_request/url_request_job.h"
27 #include "net/url_request/url_request_throttler_entry_interface.h"
31 class HttpRequestHeaders
;
32 class HttpResponseHeaders
;
33 class HttpResponseInfo
;
34 class HttpTransaction
;
35 class HttpUserAgentSettings
;
37 class UploadDataStream
;
38 class URLRequestContext
;
40 // A URLRequestJob subclass that is built on top of HttpTransaction. It
41 // provides an implementation for both HTTP and HTTPS.
42 class NET_EXPORT_PRIVATE URLRequestHttpJob
: public URLRequestJob
{
44 static URLRequestJob
* Factory(URLRequest
* request
,
45 NetworkDelegate
* network_delegate
,
46 const std::string
& scheme
);
49 URLRequestHttpJob(URLRequest
* request
,
50 NetworkDelegate
* network_delegate
,
51 const HttpUserAgentSettings
* http_user_agent_settings
);
53 ~URLRequestHttpJob() override
;
55 // Overridden from URLRequestJob:
56 void SetPriority(RequestPriority priority
) override
;
57 void Start() override
;
59 void GetConnectionAttempts(ConnectionAttempts
* out
) const override
;
61 RequestPriority
priority() const {
66 enum CompletionCause
{
71 typedef base::RefCountedData
<bool> SharedBoolean
;
73 class HttpFilterContext
;
75 // Shadows URLRequestJob's version of this method.
76 void NotifyBeforeNetworkStart(bool* defer
);
78 // Shadows URLRequestJob's version of this method so we can grab cookies.
79 void NotifyHeadersComplete();
81 // Shadows URLRequestJob's method so we can record histograms.
82 void NotifyDone(const URLRequestStatus
& status
);
84 void DestroyTransaction();
86 void AddExtraHeaders();
87 void AddCookieHeaderAndStart();
88 void SaveCookiesAndNotifyHeadersComplete(int result
);
89 void SaveNextCookie();
90 void FetchResponseCookies(std::vector
<std::string
>* cookies
);
92 // Processes a Backoff header, if one exists.
93 void ProcessBackoffHeader();
95 // Processes the Strict-Transport-Security header, if one exists.
96 void ProcessStrictTransportSecurityHeader();
98 // Processes the Public-Key-Pins header, if one exists.
99 void ProcessPublicKeyPinsHeader();
101 // |result| should be OK, or the request is canceled.
102 void OnHeadersReceivedCallback(int result
);
103 void OnStartCompleted(int result
);
104 void OnReadCompleted(int result
);
105 void NotifyBeforeSendHeadersCallback(int result
);
106 void NotifyBeforeSendProxyHeadersCallback(
107 const ProxyInfo
& proxy_info
,
108 HttpRequestHeaders
* request_headers
);
110 void RestartTransactionWithAuth(const AuthCredentials
& credentials
);
112 // Overridden from URLRequestJob:
113 void SetUpload(UploadDataStream
* upload
) override
;
114 void SetExtraRequestHeaders(const HttpRequestHeaders
& headers
) override
;
115 LoadState
GetLoadState() const override
;
116 UploadProgress
GetUploadProgress() const override
;
117 bool GetMimeType(std::string
* mime_type
) const override
;
118 bool GetCharset(std::string
* charset
) override
;
119 void GetResponseInfo(HttpResponseInfo
* info
) override
;
120 void GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
121 bool GetResponseCookies(std::vector
<std::string
>* cookies
) override
;
122 int GetResponseCode() const override
;
123 Filter
* SetupFilter() const override
;
124 bool CopyFragmentOnRedirect(const GURL
& location
) const override
;
125 bool IsSafeRedirect(const GURL
& location
) override
;
126 bool NeedsAuth() override
;
127 void GetAuthChallengeInfo(scoped_refptr
<AuthChallengeInfo
>*) override
;
128 void SetAuth(const AuthCredentials
& credentials
) override
;
129 void CancelAuth() override
;
130 void ContinueWithCertificate(X509Certificate
* client_cert
) override
;
131 void ContinueDespiteLastError() override
;
132 void ResumeNetworkStart() override
;
133 bool ReadRawData(IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
134 void StopCaching() override
;
135 bool GetFullRequestHeaders(HttpRequestHeaders
* headers
) const override
;
136 int64
GetTotalReceivedBytes() const override
;
137 void DoneReading() override
;
138 void DoneReadingRedirectResponse() override
;
140 HostPortPair
GetSocketAddress() const override
;
141 void NotifyURLRequestDestroyed() override
;
146 void UpdatePacketReadTimes() override
;
147 void RecordPacketStats(FilterContext::StatisticSelector statistic
) const;
149 // Starts the transaction if extensions using the webrequest API do not
151 void StartTransaction();
152 // If |result| is OK, calls StartTransactionInternal. Otherwise notifies
154 void MaybeStartTransactionInternal(int result
);
155 void StartTransactionInternal();
157 void RecordPerfHistograms(CompletionCause reason
);
158 void DoneWithRequest(CompletionCause reason
);
160 // Callback functions for Cookie Monster
161 void DoLoadCookies();
162 void CheckCookiePolicyAndLoad(const CookieList
& cookie_list
);
163 void OnCookiesLoaded(const std::string
& cookie_line
);
164 void DoStartTransaction();
166 // See the implementation for a description of save_next_cookie_running and
168 void OnCookieSaved(scoped_refptr
<SharedBoolean
> save_next_cookie_running
,
169 scoped_refptr
<SharedBoolean
> callback_pending
,
172 // Some servers send the body compressed, but specify the content length as
173 // the uncompressed size. If this is the case, we return true in order
174 // to request to work around this non-adherence to the HTTP standard.
175 // |rv| is the standard return value of a read function indicating the number
176 // of bytes read or, if negative, an error code.
177 bool ShouldFixMismatchedContentLength(int rv
) const;
179 // Returns the effective response headers, considering that they may be
180 // overridden by |override_response_headers_|.
181 HttpResponseHeaders
* GetResponseHeaders() const;
183 RequestPriority priority_
;
185 HttpRequestInfo request_info_
;
186 const HttpResponseInfo
* response_info_
;
188 std::vector
<std::string
> response_cookies_
;
189 size_t response_cookies_save_index_
;
190 base::Time response_date_
;
192 // Auth states for proxy and origin server.
193 AuthState proxy_auth_state_
;
194 AuthState server_auth_state_
;
195 AuthCredentials auth_credentials_
;
197 CompletionCallback start_callback_
;
198 CompletionCallback notify_before_headers_sent_callback_
;
200 bool read_in_progress_
;
202 scoped_ptr
<HttpTransaction
> transaction_
;
204 // This is used to supervise traffic and enforce exponential
205 // back-off. May be NULL.
206 scoped_refptr
<URLRequestThrottlerEntryInterface
> throttling_entry_
;
208 // A handle to the SDCH dictionaries that were advertised in this request.
210 scoped_ptr
<SdchManager::DictionarySet
> dictionaries_advertised_
;
212 // For SDCH latency experiments, when we are able to do SDCH, we may enable
213 // either an SDCH latency test xor a pass through test. The following bools
214 // indicate what we decided on for this instance.
215 bool sdch_test_activated_
; // Advertising a dictionary for sdch.
216 bool sdch_test_control_
; // Not even accepting-content sdch.
218 // For recording of stats, we need to remember if this is cached content.
219 bool is_cached_content_
;
221 base::Time request_creation_time_
;
223 // Data used for statistics gathering. This data is only used for histograms
224 // and is not required. It is only gathered if packet_timing_enabled_ == true.
226 // TODO(jar): improve the quality of the gathered info by gathering most times
227 // at a lower point in the network stack, assuring we have actual packet
228 // boundaries, rather than approximations. Also note that input byte count
229 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
230 // true packet arrival times in such cases.
232 // Enable recording of packet arrival times for histogramming.
233 bool packet_timing_enabled_
;
234 bool done_
; // True when we are done doing work.
236 // The number of bytes that have been accounted for in packets (where some of
237 // those packets may possibly have had their time of arrival recorded).
238 int64 bytes_observed_in_packets_
;
240 // The request time may not be available when we are being destroyed, so we
241 // snapshot it early on.
242 base::Time request_time_snapshot_
;
244 // Since we don't save all packet times in packet_times_, we save the
245 // last time for use in histograms.
246 base::Time final_packet_time_
;
248 // The start time for the job, ignoring re-starts.
249 base::TimeTicks start_time_
;
251 // When the transaction finished reading the request headers.
252 base::TimeTicks receive_headers_end_
;
254 scoped_ptr
<HttpFilterContext
> filter_context_
;
256 CompletionCallback on_headers_received_callback_
;
258 // We allow the network delegate to modify a copy of the response headers.
259 // This prevents modifications of headers that are shared with the underlying
260 // layers of the network stack.
261 scoped_refptr
<HttpResponseHeaders
> override_response_headers_
;
263 // The network delegate can mark a URL as safe for redirection.
264 // The reference fragment of the original URL is not appended to the redirect
265 // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|.
266 GURL allowed_unsafe_redirect_url_
;
268 // Flag used to verify that |this| is not deleted while we are awaiting
269 // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
270 // True if we are waiting a callback and
271 // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
272 // to inform the NetworkDelegate that it may not call back.
273 bool awaiting_callback_
;
275 const HttpUserAgentSettings
* http_user_agent_settings_
;
277 URLRequestBackoffManager
* backoff_manager_
;
279 // Keeps track of total received bytes over the network from transactions used
280 // by this job that have already been destroyed.
281 int64_t total_received_bytes_from_previous_transactions_
;
283 base::WeakPtrFactory
<URLRequestHttpJob
> weak_factory_
;
285 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob
);
290 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_