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 ~URLRequestHttpJob() override
;
50 // Overridden from URLRequestJob:
51 void SetPriority(RequestPriority priority
) override
;
52 void Start() 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 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 void RecordCompressionHistograms();
139 bool IsCompressibleContent() const;
141 // Starts the transaction if extensions using the webrequest API do not
143 void StartTransaction();
144 // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies
146 void MaybeStartTransactionInternal(int result
);
147 void StartTransactionInternal();
149 void RecordPerfHistograms(CompletionCause reason
);
150 void DoneWithRequest(CompletionCause reason
);
152 // Callback functions for Cookie Monster
153 void DoLoadCookies();
154 void CheckCookiePolicyAndLoad(const CookieList
& cookie_list
);
155 void OnCookiesLoaded(const std::string
& cookie_line
);
156 void DoStartTransaction();
158 // See the implementation for a description of save_next_cookie_running and
160 void OnCookieSaved(scoped_refptr
<SharedBoolean
> save_next_cookie_running
,
161 scoped_refptr
<SharedBoolean
> callback_pending
,
164 // Some servers send the body compressed, but specify the content length as
165 // the uncompressed size. If this is the case, we return true in order
166 // to request to work around this non-adherence to the HTTP standard.
167 // |rv| is the standard return value of a read function indicating the number
168 // of bytes read or, if negative, an error code.
169 bool ShouldFixMismatchedContentLength(int rv
) const;
171 // Returns the effective response headers, considering that they may be
172 // overridden by |override_response_headers_|.
173 HttpResponseHeaders
* GetResponseHeaders() const;
175 RequestPriority priority_
;
177 HttpRequestInfo request_info_
;
178 const HttpResponseInfo
* response_info_
;
180 std::vector
<std::string
> response_cookies_
;
181 size_t response_cookies_save_index_
;
182 base::Time response_date_
;
184 // Auth states for proxy and origin server.
185 AuthState proxy_auth_state_
;
186 AuthState server_auth_state_
;
187 AuthCredentials auth_credentials_
;
189 CompletionCallback start_callback_
;
190 CompletionCallback notify_before_headers_sent_callback_
;
192 bool read_in_progress_
;
194 scoped_ptr
<HttpTransaction
> transaction_
;
196 // This is used to supervise traffic and enforce exponential
197 // back-off. May be NULL.
198 scoped_refptr
<URLRequestThrottlerEntryInterface
> throttling_entry_
;
200 // Indicated if an SDCH dictionary was advertised, and hence an SDCH
201 // compressed response is expected. We use this to help detect (accidental?)
202 // proxy corruption of a response, which sometimes marks SDCH content as
203 // having no content encoding <oops>.
204 bool sdch_dictionary_advertised_
;
206 // For SDCH latency experiments, when we are able to do SDCH, we may enable
207 // either an SDCH latency test xor a pass through test. The following bools
208 // indicate what we decided on for this instance.
209 bool sdch_test_activated_
; // Advertising a dictionary for sdch.
210 bool sdch_test_control_
; // Not even accepting-content sdch.
212 // For recording of stats, we need to remember if this is cached content.
213 bool is_cached_content_
;
215 base::Time request_creation_time_
;
217 // Data used for statistics gathering. This data is only used for histograms
218 // and is not required. It is only gathered if packet_timing_enabled_ == true.
220 // TODO(jar): improve the quality of the gathered info by gathering most times
221 // at a lower point in the network stack, assuring we have actual packet
222 // boundaries, rather than approximations. Also note that input byte count
223 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
224 // true packet arrival times in such cases.
226 // Enable recording of packet arrival times for histogramming.
227 bool packet_timing_enabled_
;
228 bool done_
; // True when we are done doing work.
230 // The number of bytes that have been accounted for in packets (where some of
231 // those packets may possibly have had their time of arrival recorded).
232 int64 bytes_observed_in_packets_
;
234 // The request time may not be available when we are being destroyed, so we
235 // snapshot it early on.
236 base::Time request_time_snapshot_
;
238 // Since we don't save all packet times in packet_times_, we save the
239 // last time for use in histograms.
240 base::Time final_packet_time_
;
242 // The start time for the job, ignoring re-starts.
243 base::TimeTicks start_time_
;
245 // When the transaction finished reading the request headers.
246 base::TimeTicks receive_headers_end_
;
248 scoped_ptr
<HttpFilterContext
> filter_context_
;
250 CompletionCallback on_headers_received_callback_
;
252 // We allow the network delegate to modify a copy of the response headers.
253 // This prevents modifications of headers that are shared with the underlying
254 // layers of the network stack.
255 scoped_refptr
<HttpResponseHeaders
> override_response_headers_
;
257 // The network delegate can mark a URL as safe for redirection.
258 // The reference fragment of the original URL is not appended to the redirect
259 // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|.
260 GURL allowed_unsafe_redirect_url_
;
262 // Flag used to verify that |this| is not deleted while we are awaiting
263 // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
264 // True if we are waiting a callback and
265 // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
266 // to inform the NetworkDelegate that it may not call back.
267 bool awaiting_callback_
;
269 const HttpUserAgentSettings
* http_user_agent_settings_
;
271 base::WeakPtrFactory
<URLRequestHttpJob
> weak_factory_
;
273 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob
);
278 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_