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