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 HttpResponseHeaders
;
27 class HttpResponseInfo
;
28 class HttpTransaction
;
29 class HttpUserAgentSettings
;
30 class UploadDataStream
;
31 class URLRequestContext
;
33 // A URLRequestJob subclass that is built on top of HttpTransaction. It
34 // provides an implementation for both HTTP and HTTPS.
35 class NET_EXPORT_PRIVATE URLRequestHttpJob
: public URLRequestJob
{
37 static URLRequestJob
* Factory(URLRequest
* request
,
38 NetworkDelegate
* network_delegate
,
39 const std::string
& scheme
);
42 URLRequestHttpJob(URLRequest
* request
,
43 NetworkDelegate
* network_delegate
,
44 const HttpUserAgentSettings
* http_user_agent_settings
);
46 virtual ~URLRequestHttpJob();
48 // Overridden from URLRequestJob:
49 virtual void SetPriority(RequestPriority priority
) OVERRIDE
;
50 virtual void Start() OVERRIDE
;
51 virtual void Kill() OVERRIDE
;
53 RequestPriority
priority() const {
58 enum CompletionCause
{
63 typedef base::RefCountedData
<bool> SharedBoolean
;
65 class HttpFilterContext
;
66 class HttpTransactionDelegateImpl
;
68 // Shadows URLRequestJob's version of this method so we can grab cookies.
69 void NotifyHeadersComplete();
71 // Shadows URLRequestJob's method so we can record histograms.
72 void NotifyDone(const URLRequestStatus
& status
);
74 void DestroyTransaction();
76 void AddExtraHeaders();
77 void AddCookieHeaderAndStart();
78 void SaveCookiesAndNotifyHeadersComplete(int result
);
79 void SaveNextCookie();
80 void FetchResponseCookies(std::vector
<std::string
>* cookies
);
82 // Processes the Strict-Transport-Security header, if one exists.
83 void ProcessStrictTransportSecurityHeader();
85 // Processes the Public-Key-Pins header, if one exists.
86 void ProcessPublicKeyPinsHeader();
88 // |result| should be net::OK, or the request is canceled.
89 void OnHeadersReceivedCallback(int result
);
90 void OnStartCompleted(int result
);
91 void OnReadCompleted(int result
);
92 void NotifyBeforeSendHeadersCallback(int result
);
94 void RestartTransactionWithAuth(const AuthCredentials
& credentials
);
96 // Overridden from URLRequestJob:
97 virtual void SetUpload(UploadDataStream
* upload
) OVERRIDE
;
98 virtual void SetExtraRequestHeaders(
99 const HttpRequestHeaders
& headers
) OVERRIDE
;
100 virtual LoadState
GetLoadState() const OVERRIDE
;
101 virtual UploadProgress
GetUploadProgress() const OVERRIDE
;
102 virtual bool GetMimeType(std::string
* mime_type
) const OVERRIDE
;
103 virtual bool GetCharset(std::string
* charset
) OVERRIDE
;
104 virtual void GetResponseInfo(HttpResponseInfo
* info
) OVERRIDE
;
105 virtual void GetLoadTimingInfo(
106 LoadTimingInfo
* load_timing_info
) const OVERRIDE
;
107 virtual bool GetResponseCookies(std::vector
<std::string
>* cookies
) OVERRIDE
;
108 virtual int GetResponseCode() const OVERRIDE
;
109 virtual Filter
* SetupFilter() const OVERRIDE
;
110 virtual bool CopyFragmentOnRedirect(const GURL
& location
) const OVERRIDE
;
111 virtual bool IsSafeRedirect(const GURL
& location
) OVERRIDE
;
112 virtual bool NeedsAuth() OVERRIDE
;
113 virtual void GetAuthChallengeInfo(scoped_refptr
<AuthChallengeInfo
>*) OVERRIDE
;
114 virtual void SetAuth(const AuthCredentials
& credentials
) OVERRIDE
;
115 virtual void CancelAuth() OVERRIDE
;
116 virtual void ContinueWithCertificate(X509Certificate
* client_cert
) OVERRIDE
;
117 virtual void ContinueDespiteLastError() OVERRIDE
;
118 virtual void ResumeNetworkStart() OVERRIDE
;
119 virtual bool ReadRawData(IOBuffer
* buf
, int buf_size
,
120 int* bytes_read
) OVERRIDE
;
121 virtual void StopCaching() OVERRIDE
;
122 virtual bool GetFullRequestHeaders(
123 HttpRequestHeaders
* headers
) const OVERRIDE
;
124 virtual int64
GetTotalReceivedBytes() const OVERRIDE
;
125 virtual void DoneReading() OVERRIDE
;
126 virtual void DoneReadingRedirectResponse() OVERRIDE
;
128 virtual HostPortPair
GetSocketAddress() const OVERRIDE
;
129 virtual void NotifyURLRequestDestroyed() OVERRIDE
;
134 virtual void UpdatePacketReadTimes() OVERRIDE
;
135 void RecordPacketStats(FilterContext::StatisticSelector statistic
) const;
137 void RecordCompressionHistograms();
138 bool IsCompressibleContent() const;
140 // Starts the transaction if extensions using the webrequest API do not
142 void StartTransaction();
143 // If |result| is net::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 // An URL for an SDCH dictionary as suggested in a Get-Dictionary HTTP header.
194 GURL sdch_dictionary_url_
;
196 scoped_ptr
<HttpTransaction
> transaction_
;
198 // This is used to supervise traffic and enforce exponential
199 // back-off. May be NULL.
200 scoped_refptr
<URLRequestThrottlerEntryInterface
> throttling_entry_
;
202 // Indicated if an SDCH dictionary was advertised, and hence an SDCH
203 // compressed response is expected. We use this to help detect (accidental?)
204 // proxy corruption of a response, which sometimes marks SDCH content as
205 // having no content encoding <oops>.
206 bool sdch_dictionary_advertised_
;
208 // For SDCH latency experiments, when we are able to do SDCH, we may enable
209 // either an SDCH latency test xor a pass through test. The following bools
210 // indicate what we decided on for this instance.
211 bool sdch_test_activated_
; // Advertising a dictionary for sdch.
212 bool sdch_test_control_
; // Not even accepting-content sdch.
214 // For recording of stats, we need to remember if this is cached content.
215 bool is_cached_content_
;
217 base::Time request_creation_time_
;
219 // Data used for statistics gathering. This data is only used for histograms
220 // and is not required. It is only gathered if packet_timing_enabled_ == true.
222 // TODO(jar): improve the quality of the gathered info by gathering most times
223 // at a lower point in the network stack, assuring we have actual packet
224 // boundaries, rather than approximations. Also note that input byte count
225 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
226 // true packet arrival times in such cases.
228 // Enable recording of packet arrival times for histogramming.
229 bool packet_timing_enabled_
;
230 bool done_
; // True when we are done doing work.
232 // The number of bytes that have been accounted for in packets (where some of
233 // those packets may possibly have had their time of arrival recorded).
234 int64 bytes_observed_in_packets_
;
236 // The request time may not be available when we are being destroyed, so we
237 // snapshot it early on.
238 base::Time request_time_snapshot_
;
240 // Since we don't save all packet times in packet_times_, we save the
241 // last time for use in histograms.
242 base::Time final_packet_time_
;
244 // The start time for the job, ignoring re-starts.
245 base::TimeTicks start_time_
;
247 // When the transaction finished reading the request headers.
248 base::TimeTicks receive_headers_end_
;
250 scoped_ptr
<HttpFilterContext
> filter_context_
;
252 CompletionCallback on_headers_received_callback_
;
254 // We allow the network delegate to modify a copy of the response headers.
255 // This prevents modifications of headers that are shared with the underlying
256 // layers of the network stack.
257 scoped_refptr
<HttpResponseHeaders
> override_response_headers_
;
259 // The network delegate can mark a URL as safe for redirection.
260 // The reference fragment of the original URL is not appended to the redirect
261 // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|.
262 GURL allowed_unsafe_redirect_url_
;
264 // Flag used to verify that |this| is not deleted while we are awaiting
265 // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
266 // True if we are waiting a callback and
267 // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
268 // to inform the NetworkDelegate that it may not call back.
269 bool awaiting_callback_
;
271 const HttpUserAgentSettings
* http_user_agent_settings_
;
273 base::WeakPtrFactory
<URLRequestHttpJob
> weak_factory_
;
275 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob
);
280 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_