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