Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / net / url_request / url_request_http_job.h
blob3a23e8a6c0414a05d88e78efeba4e0747bc9fd42
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 <string>
9 #include <vector>
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"
25 namespace net {
27 class HttpRequestHeaders;
28 class HttpResponseHeaders;
29 class HttpResponseInfo;
30 class HttpTransaction;
31 class HttpUserAgentSettings;
32 class ProxyInfo;
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 {
39 public:
40 static URLRequestJob* Factory(URLRequest* request,
41 NetworkDelegate* network_delegate,
42 const std::string& scheme);
44 protected:
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;
54 void Kill() override;
56 RequestPriority priority() const {
57 return priority_;
60 private:
61 enum CompletionCause {
62 ABORTED,
63 FINISHED
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;
133 void RecordTimer();
134 void ResetTimer();
136 void UpdatePacketReadTimes() override;
137 void RecordPacketStats(FilterContext::StatisticSelector statistic) const;
139 void RecordCompressionHistograms();
140 bool IsCompressibleContent() const;
142 // Starts the transaction if extensions using the webrequest API do not
143 // object.
144 void StartTransaction();
145 // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies
146 // cancellation.
147 void MaybeStartTransactionInternal(int result);
148 void StartTransactionInternal();
150 void RecordPerfHistograms(CompletionCause reason);
151 void DoneWithRequest(CompletionCause reason);
153 // Callback functions for Cookie Monster
154 void DoLoadCookies();
155 void CheckCookiePolicyAndLoad(const CookieList& cookie_list);
156 void OnCookiesLoaded(const std::string& cookie_line);
157 void DoStartTransaction();
159 // See the implementation for a description of save_next_cookie_running and
160 // callback_pending.
161 void OnCookieSaved(scoped_refptr<SharedBoolean> save_next_cookie_running,
162 scoped_refptr<SharedBoolean> callback_pending,
163 bool cookie_status);
165 // Some servers send the body compressed, but specify the content length as
166 // the uncompressed size. If this is the case, we return true in order
167 // to request to work around this non-adherence to the HTTP standard.
168 // |rv| is the standard return value of a read function indicating the number
169 // of bytes read or, if negative, an error code.
170 bool ShouldFixMismatchedContentLength(int rv) const;
172 // Returns the effective response headers, considering that they may be
173 // overridden by |override_response_headers_|.
174 HttpResponseHeaders* GetResponseHeaders() const;
176 RequestPriority priority_;
178 HttpRequestInfo request_info_;
179 const HttpResponseInfo* response_info_;
181 std::vector<std::string> response_cookies_;
182 size_t response_cookies_save_index_;
183 base::Time response_date_;
185 // Auth states for proxy and origin server.
186 AuthState proxy_auth_state_;
187 AuthState server_auth_state_;
188 AuthCredentials auth_credentials_;
190 CompletionCallback start_callback_;
191 CompletionCallback notify_before_headers_sent_callback_;
193 bool read_in_progress_;
195 scoped_ptr<HttpTransaction> transaction_;
197 // This is used to supervise traffic and enforce exponential
198 // back-off. May be NULL.
199 scoped_refptr<URLRequestThrottlerEntryInterface> throttling_entry_;
201 // A handle to the SDCH dictionaries that were advertised in this request.
202 // May be null.
203 scoped_ptr<SdchManager::DictionarySet> dictionaries_advertised_;
205 // For SDCH latency experiments, when we are able to do SDCH, we may enable
206 // either an SDCH latency test xor a pass through test. The following bools
207 // indicate what we decided on for this instance.
208 bool sdch_test_activated_; // Advertising a dictionary for sdch.
209 bool sdch_test_control_; // Not even accepting-content sdch.
211 // For recording of stats, we need to remember if this is cached content.
212 bool is_cached_content_;
214 base::Time request_creation_time_;
216 // Data used for statistics gathering. This data is only used for histograms
217 // and is not required. It is only gathered if packet_timing_enabled_ == true.
219 // TODO(jar): improve the quality of the gathered info by gathering most times
220 // at a lower point in the network stack, assuring we have actual packet
221 // boundaries, rather than approximations. Also note that input byte count
222 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
223 // true packet arrival times in such cases.
225 // Enable recording of packet arrival times for histogramming.
226 bool packet_timing_enabled_;
227 bool done_; // True when we are done doing work.
229 // The number of bytes that have been accounted for in packets (where some of
230 // those packets may possibly have had their time of arrival recorded).
231 int64 bytes_observed_in_packets_;
233 // The request time may not be available when we are being destroyed, so we
234 // snapshot it early on.
235 base::Time request_time_snapshot_;
237 // Since we don't save all packet times in packet_times_, we save the
238 // last time for use in histograms.
239 base::Time final_packet_time_;
241 // The start time for the job, ignoring re-starts.
242 base::TimeTicks start_time_;
244 // When the transaction finished reading the request headers.
245 base::TimeTicks receive_headers_end_;
247 scoped_ptr<HttpFilterContext> filter_context_;
249 CompletionCallback on_headers_received_callback_;
251 // We allow the network delegate to modify a copy of the response headers.
252 // This prevents modifications of headers that are shared with the underlying
253 // layers of the network stack.
254 scoped_refptr<HttpResponseHeaders> override_response_headers_;
256 // The network delegate can mark a URL as safe for redirection.
257 // The reference fragment of the original URL is not appended to the redirect
258 // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|.
259 GURL allowed_unsafe_redirect_url_;
261 // Flag used to verify that |this| is not deleted while we are awaiting
262 // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
263 // True if we are waiting a callback and
264 // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
265 // to inform the NetworkDelegate that it may not call back.
266 bool awaiting_callback_;
268 const HttpUserAgentSettings* http_user_agent_settings_;
270 base::WeakPtrFactory<URLRequestHttpJob> weak_factory_;
272 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob);
275 } // namespace net
277 #endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_