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
;
69 class HttpTransactionDelegateImpl
;
71 // Shadows URLRequestJob's version of this method so we can grab cookies.
72 void NotifyHeadersComplete();
74 // Shadows URLRequestJob's method so we can record histograms.
75 void NotifyDone(const URLRequestStatus
& status
);
77 void DestroyTransaction();
79 void AddExtraHeaders();
80 void AddCookieHeaderAndStart();
81 void SaveCookiesAndNotifyHeadersComplete(int result
);
82 void SaveNextCookie();
83 void FetchResponseCookies(std::vector
<std::string
>* cookies
);
85 // Processes the Strict-Transport-Security header, if one exists.
86 void ProcessStrictTransportSecurityHeader();
88 // Processes the Public-Key-Pins header, if one exists.
89 void ProcessPublicKeyPinsHeader();
91 // |result| should be net::OK, or the request is canceled.
92 void OnHeadersReceivedCallback(int result
);
93 void OnStartCompleted(int result
);
94 void OnReadCompleted(int result
);
95 void NotifyBeforeSendHeadersCallback(int result
);
96 void NotifyBeforeSendProxyHeadersCallback(
97 const ProxyInfo
& proxy_info
,
98 HttpRequestHeaders
* request_headers
);
100 void RestartTransactionWithAuth(const AuthCredentials
& credentials
);
102 // Overridden from URLRequestJob:
103 void SetUpload(UploadDataStream
* upload
) override
;
104 void SetExtraRequestHeaders(const HttpRequestHeaders
& headers
) override
;
105 LoadState
GetLoadState() const override
;
106 UploadProgress
GetUploadProgress() const override
;
107 bool GetMimeType(std::string
* mime_type
) const override
;
108 bool GetCharset(std::string
* charset
) override
;
109 void GetResponseInfo(HttpResponseInfo
* info
) override
;
110 void GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
111 bool GetResponseCookies(std::vector
<std::string
>* cookies
) override
;
112 int GetResponseCode() const override
;
113 Filter
* SetupFilter() const override
;
114 bool CopyFragmentOnRedirect(const GURL
& location
) const override
;
115 bool IsSafeRedirect(const GURL
& location
) override
;
116 bool NeedsAuth() override
;
117 void GetAuthChallengeInfo(scoped_refptr
<AuthChallengeInfo
>*) override
;
118 void SetAuth(const AuthCredentials
& credentials
) override
;
119 void CancelAuth() override
;
120 void ContinueWithCertificate(X509Certificate
* client_cert
) override
;
121 void ContinueDespiteLastError() override
;
122 void ResumeNetworkStart() override
;
123 bool ReadRawData(IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
124 void StopCaching() override
;
125 bool GetFullRequestHeaders(HttpRequestHeaders
* headers
) const override
;
126 int64
GetTotalReceivedBytes() const override
;
127 void DoneReading() override
;
128 void DoneReadingRedirectResponse() override
;
130 HostPortPair
GetSocketAddress() const override
;
131 void NotifyURLRequestDestroyed() override
;
136 void UpdatePacketReadTimes() override
;
137 void RecordPacketStats(FilterContext::StatisticSelector statistic
) const;
139 // Starts the transaction if extensions using the webrequest API do not
141 void StartTransaction();
142 // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies
144 void MaybeStartTransactionInternal(int result
);
145 void StartTransactionInternal();
147 void RecordPerfHistograms(CompletionCause reason
);
148 void DoneWithRequest(CompletionCause reason
);
150 // Callback functions for Cookie Monster
151 void DoLoadCookies();
152 void CheckCookiePolicyAndLoad(const CookieList
& cookie_list
);
153 void OnCookiesLoaded(const std::string
& cookie_line
);
154 void DoStartTransaction();
156 // See the implementation for a description of save_next_cookie_running and
158 void OnCookieSaved(scoped_refptr
<SharedBoolean
> save_next_cookie_running
,
159 scoped_refptr
<SharedBoolean
> callback_pending
,
162 // Some servers send the body compressed, but specify the content length as
163 // the uncompressed size. If this is the case, we return true in order
164 // to request to work around this non-adherence to the HTTP standard.
165 // |rv| is the standard return value of a read function indicating the number
166 // of bytes read or, if negative, an error code.
167 bool ShouldFixMismatchedContentLength(int rv
) const;
169 // Returns the effective response headers, considering that they may be
170 // overridden by |override_response_headers_|.
171 HttpResponseHeaders
* GetResponseHeaders() const;
173 RequestPriority priority_
;
175 HttpRequestInfo request_info_
;
176 const HttpResponseInfo
* response_info_
;
178 std::vector
<std::string
> response_cookies_
;
179 size_t response_cookies_save_index_
;
180 base::Time response_date_
;
182 // Auth states for proxy and origin server.
183 AuthState proxy_auth_state_
;
184 AuthState server_auth_state_
;
185 AuthCredentials auth_credentials_
;
187 CompletionCallback start_callback_
;
188 CompletionCallback notify_before_headers_sent_callback_
;
190 bool read_in_progress_
;
192 scoped_ptr
<HttpTransaction
> transaction_
;
194 // This is used to supervise traffic and enforce exponential
195 // back-off. May be NULL.
196 scoped_refptr
<URLRequestThrottlerEntryInterface
> throttling_entry_
;
198 // A handle to the SDCH dictionaries that were advertised in this request.
200 scoped_ptr
<SdchManager::DictionarySet
> dictionaries_advertised_
;
202 // For SDCH latency experiments, when we are able to do SDCH, we may enable
203 // either an SDCH latency test xor a pass through test. The following bools
204 // indicate what we decided on for this instance.
205 bool sdch_test_activated_
; // Advertising a dictionary for sdch.
206 bool sdch_test_control_
; // Not even accepting-content sdch.
208 // For recording of stats, we need to remember if this is cached content.
209 bool is_cached_content_
;
211 base::Time request_creation_time_
;
213 // Data used for statistics gathering. This data is only used for histograms
214 // and is not required. It is only gathered if packet_timing_enabled_ == true.
216 // TODO(jar): improve the quality of the gathered info by gathering most times
217 // at a lower point in the network stack, assuring we have actual packet
218 // boundaries, rather than approximations. Also note that input byte count
219 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
220 // true packet arrival times in such cases.
222 // Enable recording of packet arrival times for histogramming.
223 bool packet_timing_enabled_
;
224 bool done_
; // True when we are done doing work.
226 // The number of bytes that have been accounted for in packets (where some of
227 // those packets may possibly have had their time of arrival recorded).
228 int64 bytes_observed_in_packets_
;
230 // The request time may not be available when we are being destroyed, so we
231 // snapshot it early on.
232 base::Time request_time_snapshot_
;
234 // Since we don't save all packet times in packet_times_, we save the
235 // last time for use in histograms.
236 base::Time final_packet_time_
;
238 // The start time for the job, ignoring re-starts.
239 base::TimeTicks start_time_
;
241 // When the transaction finished reading the request headers.
242 base::TimeTicks receive_headers_end_
;
244 scoped_ptr
<HttpFilterContext
> filter_context_
;
246 CompletionCallback on_headers_received_callback_
;
248 // We allow the network delegate to modify a copy of the response headers.
249 // This prevents modifications of headers that are shared with the underlying
250 // layers of the network stack.
251 scoped_refptr
<HttpResponseHeaders
> override_response_headers_
;
253 // The network delegate can mark a URL as safe for redirection.
254 // The reference fragment of the original URL is not appended to the redirect
255 // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|.
256 GURL allowed_unsafe_redirect_url_
;
258 // Flag used to verify that |this| is not deleted while we are awaiting
259 // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
260 // True if we are waiting a callback and
261 // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
262 // to inform the NetworkDelegate that it may not call back.
263 bool awaiting_callback_
;
265 const HttpUserAgentSettings
* http_user_agent_settings_
;
267 base::WeakPtrFactory
<URLRequestHttpJob
> weak_factory_
;
269 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob
);
274 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_