Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / url_request / url_request_http_job.h
blobf72cb15bd4c6b4cc4a48f2d51e80a402c1b4c9e8
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_
8 #include <stdint.h>
10 #include <string>
11 #include <vector>
13 #include "base/compiler_specific.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/time/time.h"
17 #include "net/base/auth.h"
18 #include "net/base/completion_callback.h"
19 #include "net/base/net_export.h"
20 #include "net/base/sdch_manager.h"
21 #include "net/cookies/cookie_store.h"
22 #include "net/filter/filter.h"
23 #include "net/http/http_request_info.h"
24 #include "net/socket/connection_attempts.h"
25 #include "net/url_request/url_request_backoff_manager.h"
26 #include "net/url_request/url_request_job.h"
27 #include "net/url_request/url_request_throttler_entry_interface.h"
29 namespace net {
31 class HttpRequestHeaders;
32 class HttpResponseHeaders;
33 class HttpResponseInfo;
34 class HttpTransaction;
35 class HttpUserAgentSettings;
36 class ProxyInfo;
37 class UploadDataStream;
38 class URLRequestContext;
40 // A URLRequestJob subclass that is built on top of HttpTransaction. It
41 // provides an implementation for both HTTP and HTTPS.
42 class NET_EXPORT_PRIVATE URLRequestHttpJob : public URLRequestJob {
43 public:
44 static URLRequestJob* Factory(URLRequest* request,
45 NetworkDelegate* network_delegate,
46 const std::string& scheme);
48 protected:
49 URLRequestHttpJob(URLRequest* request,
50 NetworkDelegate* network_delegate,
51 const HttpUserAgentSettings* http_user_agent_settings);
53 ~URLRequestHttpJob() override;
55 // Overridden from URLRequestJob:
56 void SetPriority(RequestPriority priority) override;
57 void Start() override;
58 void Kill() override;
59 void GetConnectionAttempts(ConnectionAttempts* out) const override;
61 RequestPriority priority() const {
62 return priority_;
65 private:
66 enum CompletionCause {
67 ABORTED,
68 FINISHED
71 typedef base::RefCountedData<bool> SharedBoolean;
73 class HttpFilterContext;
75 // Shadows URLRequestJob's version of this method.
76 void NotifyBeforeNetworkStart(bool* defer);
78 // Shadows URLRequestJob's version of this method so we can grab cookies.
79 void NotifyHeadersComplete();
81 // Shadows URLRequestJob's method so we can record histograms.
82 void NotifyDone(const URLRequestStatus& status);
84 void DestroyTransaction();
86 void AddExtraHeaders();
87 void AddCookieHeaderAndStart();
88 void SaveCookiesAndNotifyHeadersComplete(int result);
89 void SaveNextCookie();
90 void FetchResponseCookies(std::vector<std::string>* cookies);
92 // Processes a Backoff header, if one exists.
93 void ProcessBackoffHeader();
95 // Processes the Strict-Transport-Security header, if one exists.
96 void ProcessStrictTransportSecurityHeader();
98 // Processes the Public-Key-Pins header, if one exists.
99 void ProcessPublicKeyPinsHeader();
101 // |result| should be OK, or the request is canceled.
102 void OnHeadersReceivedCallback(int result);
103 void OnStartCompleted(int result);
104 void OnReadCompleted(int result);
105 void NotifyBeforeSendHeadersCallback(int result);
106 void NotifyBeforeSendProxyHeadersCallback(
107 const ProxyInfo& proxy_info,
108 HttpRequestHeaders* request_headers);
110 void RestartTransactionWithAuth(const AuthCredentials& credentials);
112 // Overridden from URLRequestJob:
113 void SetUpload(UploadDataStream* upload) override;
114 void SetExtraRequestHeaders(const HttpRequestHeaders& headers) override;
115 LoadState GetLoadState() const override;
116 UploadProgress GetUploadProgress() const override;
117 bool GetMimeType(std::string* mime_type) const override;
118 bool GetCharset(std::string* charset) override;
119 void GetResponseInfo(HttpResponseInfo* info) override;
120 void GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
121 bool GetResponseCookies(std::vector<std::string>* cookies) override;
122 int GetResponseCode() const override;
123 Filter* SetupFilter() const override;
124 bool CopyFragmentOnRedirect(const GURL& location) const override;
125 bool IsSafeRedirect(const GURL& location) override;
126 bool NeedsAuth() override;
127 void GetAuthChallengeInfo(scoped_refptr<AuthChallengeInfo>*) override;
128 void SetAuth(const AuthCredentials& credentials) override;
129 void CancelAuth() override;
130 void ContinueWithCertificate(X509Certificate* client_cert) override;
131 void ContinueDespiteLastError() override;
132 void ResumeNetworkStart() override;
133 bool ReadRawData(IOBuffer* buf, int buf_size, int* bytes_read) override;
134 void StopCaching() override;
135 bool GetFullRequestHeaders(HttpRequestHeaders* headers) const override;
136 int64 GetTotalReceivedBytes() const override;
137 int64_t GetTotalSentBytes() const override;
138 void DoneReading() override;
139 void DoneReadingRedirectResponse() override;
141 HostPortPair GetSocketAddress() const override;
142 void NotifyURLRequestDestroyed() override;
144 void RecordTimer();
145 void ResetTimer();
147 void UpdatePacketReadTimes() override;
148 void RecordPacketStats(FilterContext::StatisticSelector statistic) const;
150 // Starts the transaction if extensions using the webrequest API do not
151 // object.
152 void StartTransaction();
153 // If |result| is OK, calls StartTransactionInternal. Otherwise notifies
154 // cancellation.
155 void MaybeStartTransactionInternal(int result);
156 void StartTransactionInternal();
158 void RecordPerfHistograms(CompletionCause reason);
159 void DoneWithRequest(CompletionCause reason);
161 // Callback functions for Cookie Monster
162 void DoLoadCookies();
163 void CheckCookiePolicyAndLoad(const CookieList& cookie_list);
164 void OnCookiesLoaded(const std::string& cookie_line);
165 void DoStartTransaction();
167 // See the implementation for a description of save_next_cookie_running and
168 // callback_pending.
169 void OnCookieSaved(scoped_refptr<SharedBoolean> save_next_cookie_running,
170 scoped_refptr<SharedBoolean> callback_pending,
171 bool cookie_status);
173 // Some servers send the body compressed, but specify the content length as
174 // the uncompressed size. If this is the case, we return true in order
175 // to request to work around this non-adherence to the HTTP standard.
176 // |rv| is the standard return value of a read function indicating the number
177 // of bytes read or, if negative, an error code.
178 bool ShouldFixMismatchedContentLength(int rv) const;
180 // Returns the effective response headers, considering that they may be
181 // overridden by |override_response_headers_|.
182 HttpResponseHeaders* GetResponseHeaders() const;
184 RequestPriority priority_;
186 HttpRequestInfo request_info_;
187 const HttpResponseInfo* response_info_;
189 std::vector<std::string> response_cookies_;
190 size_t response_cookies_save_index_;
191 base::Time response_date_;
193 // Auth states for proxy and origin server.
194 AuthState proxy_auth_state_;
195 AuthState server_auth_state_;
196 AuthCredentials auth_credentials_;
198 CompletionCallback start_callback_;
199 CompletionCallback notify_before_headers_sent_callback_;
201 bool read_in_progress_;
203 scoped_ptr<HttpTransaction> transaction_;
205 // This is used to supervise traffic and enforce exponential
206 // back-off. May be NULL.
207 scoped_refptr<URLRequestThrottlerEntryInterface> throttling_entry_;
209 // A handle to the SDCH dictionaries that were advertised in this request.
210 // May be null.
211 scoped_ptr<SdchManager::DictionarySet> dictionaries_advertised_;
213 // For SDCH latency experiments, when we are able to do SDCH, we may enable
214 // either an SDCH latency test xor a pass through test. The following bools
215 // indicate what we decided on for this instance.
216 bool sdch_test_activated_; // Advertising a dictionary for sdch.
217 bool sdch_test_control_; // Not even accepting-content sdch.
219 // For recording of stats, we need to remember if this is cached content.
220 bool is_cached_content_;
222 base::Time request_creation_time_;
224 // Data used for statistics gathering. This data is only used for histograms
225 // and is not required. It is only gathered if packet_timing_enabled_ == true.
227 // TODO(jar): improve the quality of the gathered info by gathering most times
228 // at a lower point in the network stack, assuring we have actual packet
229 // boundaries, rather than approximations. Also note that input byte count
230 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
231 // true packet arrival times in such cases.
233 // Enable recording of packet arrival times for histogramming.
234 bool packet_timing_enabled_;
235 bool done_; // True when we are done doing work.
237 // The number of bytes that have been accounted for in packets (where some of
238 // those packets may possibly have had their time of arrival recorded).
239 int64 bytes_observed_in_packets_;
241 // The request time may not be available when we are being destroyed, so we
242 // snapshot it early on.
243 base::Time request_time_snapshot_;
245 // Since we don't save all packet times in packet_times_, we save the
246 // last time for use in histograms.
247 base::Time final_packet_time_;
249 // The start time for the job, ignoring re-starts.
250 base::TimeTicks start_time_;
252 // When the transaction finished reading the request headers.
253 base::TimeTicks receive_headers_end_;
255 scoped_ptr<HttpFilterContext> filter_context_;
257 CompletionCallback on_headers_received_callback_;
259 // We allow the network delegate to modify a copy of the response headers.
260 // This prevents modifications of headers that are shared with the underlying
261 // layers of the network stack.
262 scoped_refptr<HttpResponseHeaders> override_response_headers_;
264 // The network delegate can mark a URL as safe for redirection.
265 // The reference fragment of the original URL is not appended to the redirect
266 // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|.
267 GURL allowed_unsafe_redirect_url_;
269 // Flag used to verify that |this| is not deleted while we are awaiting
270 // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
271 // True if we are waiting a callback and
272 // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
273 // to inform the NetworkDelegate that it may not call back.
274 bool awaiting_callback_;
276 const HttpUserAgentSettings* http_user_agent_settings_;
278 URLRequestBackoffManager* backoff_manager_;
280 // Keeps track of total received bytes over the network from transactions used
281 // by this job that have already been destroyed.
282 int64_t total_received_bytes_from_previous_transactions_;
283 // Keeps track of total sent bytes over the network from transactions used by
284 // this job that have already been destroyed.
285 int64_t total_sent_bytes_from_previous_transactions_;
287 base::WeakPtrFactory<URLRequestHttpJob> weak_factory_;
289 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob);
292 } // namespace net
294 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_