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 HttpRequestHeaders
;
27 class HttpResponseHeaders
;
28 class HttpResponseInfo
;
29 class HttpTransaction
;
30 class HttpUserAgentSettings
;
32 class UploadDataStream
;
33 class URLRequestContext
;
35 // A URLRequestJob subclass that is built on top of HttpTransaction. It
36 // provides an implementation for both HTTP and HTTPS.
37 class NET_EXPORT_PRIVATE URLRequestHttpJob
: public URLRequestJob
{
39 static URLRequestJob
* Factory(URLRequest
* request
,
40 NetworkDelegate
* network_delegate
,
41 const std::string
& scheme
);
44 URLRequestHttpJob(URLRequest
* request
,
45 NetworkDelegate
* network_delegate
,
46 const HttpUserAgentSettings
* http_user_agent_settings
);
48 virtual ~URLRequestHttpJob();
50 // Overridden from URLRequestJob:
51 virtual void SetPriority(RequestPriority priority
) OVERRIDE
;
52 virtual void Start() OVERRIDE
;
53 virtual void Kill() OVERRIDE
;
55 RequestPriority
priority() const {
60 enum CompletionCause
{
65 typedef base::RefCountedData
<bool> SharedBoolean
;
67 class HttpFilterContext
;
68 class HttpTransactionDelegateImpl
;
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 virtual void SetUpload(UploadDataStream
* upload
) OVERRIDE
;
103 virtual void SetExtraRequestHeaders(
104 const HttpRequestHeaders
& headers
) OVERRIDE
;
105 virtual LoadState
GetLoadState() const OVERRIDE
;
106 virtual UploadProgress
GetUploadProgress() const OVERRIDE
;
107 virtual bool GetMimeType(std::string
* mime_type
) const OVERRIDE
;
108 virtual bool GetCharset(std::string
* charset
) OVERRIDE
;
109 virtual void GetResponseInfo(HttpResponseInfo
* info
) OVERRIDE
;
110 virtual void GetLoadTimingInfo(
111 LoadTimingInfo
* load_timing_info
) const OVERRIDE
;
112 virtual bool GetResponseCookies(std::vector
<std::string
>* cookies
) OVERRIDE
;
113 virtual int GetResponseCode() const OVERRIDE
;
114 virtual Filter
* SetupFilter() const OVERRIDE
;
115 virtual bool CopyFragmentOnRedirect(const GURL
& location
) const OVERRIDE
;
116 virtual bool IsSafeRedirect(const GURL
& location
) OVERRIDE
;
117 virtual bool NeedsAuth() OVERRIDE
;
118 virtual void GetAuthChallengeInfo(scoped_refptr
<AuthChallengeInfo
>*) OVERRIDE
;
119 virtual void SetAuth(const AuthCredentials
& credentials
) OVERRIDE
;
120 virtual void CancelAuth() OVERRIDE
;
121 virtual void ContinueWithCertificate(X509Certificate
* client_cert
) OVERRIDE
;
122 virtual void ContinueDespiteLastError() OVERRIDE
;
123 virtual void ResumeNetworkStart() OVERRIDE
;
124 virtual bool ReadRawData(IOBuffer
* buf
, int buf_size
,
125 int* bytes_read
) OVERRIDE
;
126 virtual void StopCaching() OVERRIDE
;
127 virtual bool GetFullRequestHeaders(
128 HttpRequestHeaders
* headers
) const OVERRIDE
;
129 virtual int64
GetTotalReceivedBytes() const OVERRIDE
;
130 virtual void DoneReading() OVERRIDE
;
131 virtual void DoneReadingRedirectResponse() OVERRIDE
;
133 virtual HostPortPair
GetSocketAddress() const OVERRIDE
;
134 virtual void NotifyURLRequestDestroyed() OVERRIDE
;
139 virtual void UpdatePacketReadTimes() OVERRIDE
;
140 void RecordPacketStats(FilterContext::StatisticSelector statistic
) const;
142 void RecordCompressionHistograms();
143 bool IsCompressibleContent() const;
145 // Starts the transaction if extensions using the webrequest API do not
147 void StartTransaction();
148 // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies
150 void MaybeStartTransactionInternal(int result
);
151 void StartTransactionInternal();
153 void RecordPerfHistograms(CompletionCause reason
);
154 void DoneWithRequest(CompletionCause reason
);
156 // Callback functions for Cookie Monster
157 void DoLoadCookies();
158 void CheckCookiePolicyAndLoad(const CookieList
& cookie_list
);
159 void OnCookiesLoaded(const std::string
& cookie_line
);
160 void DoStartTransaction();
162 // See the implementation for a description of save_next_cookie_running and
164 void OnCookieSaved(scoped_refptr
<SharedBoolean
> save_next_cookie_running
,
165 scoped_refptr
<SharedBoolean
> callback_pending
,
168 // Some servers send the body compressed, but specify the content length as
169 // the uncompressed size. If this is the case, we return true in order
170 // to request to work around this non-adherence to the HTTP standard.
171 // |rv| is the standard return value of a read function indicating the number
172 // of bytes read or, if negative, an error code.
173 bool ShouldFixMismatchedContentLength(int rv
) const;
175 // Returns the effective response headers, considering that they may be
176 // overridden by |override_response_headers_|.
177 HttpResponseHeaders
* GetResponseHeaders() const;
179 RequestPriority priority_
;
181 HttpRequestInfo request_info_
;
182 const HttpResponseInfo
* response_info_
;
184 std::vector
<std::string
> response_cookies_
;
185 size_t response_cookies_save_index_
;
186 base::Time response_date_
;
188 // Auth states for proxy and origin server.
189 AuthState proxy_auth_state_
;
190 AuthState server_auth_state_
;
191 AuthCredentials auth_credentials_
;
193 CompletionCallback start_callback_
;
194 CompletionCallback notify_before_headers_sent_callback_
;
196 bool read_in_progress_
;
198 scoped_ptr
<HttpTransaction
> transaction_
;
200 // This is used to supervise traffic and enforce exponential
201 // back-off. May be NULL.
202 scoped_refptr
<URLRequestThrottlerEntryInterface
> throttling_entry_
;
204 // Indicated if an SDCH dictionary was advertised, and hence an SDCH
205 // compressed response is expected. We use this to help detect (accidental?)
206 // proxy corruption of a response, which sometimes marks SDCH content as
207 // having no content encoding <oops>.
208 bool sdch_dictionary_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 base::WeakPtrFactory
<URLRequestHttpJob
> weak_factory_
;
277 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob
);
282 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_