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