blob: 6f8dcccb38f2392565fe4a296c3a6f438c613be4 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#ifndef NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
6#define NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_
7
8#include <string>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010014#include "base/time/time.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000015#include "net/base/auth.h"
16#include "net/base/completion_callback.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017#include "net/base/net_export.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018#include "net/cookies/cookie_store.h"
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000019#include "net/filter/filter.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020#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"
23
24namespace net {
25
Ben Murdoch116680a2014-07-20 18:25:52 -070026class HttpRequestHeaders;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000027class HttpResponseHeaders;
28class HttpResponseInfo;
29class HttpTransaction;
30class HttpUserAgentSettings;
Ben Murdoch116680a2014-07-20 18:25:52 -070031class ProxyInfo;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000032class UploadDataStream;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000033class URLRequestContext;
34
35// A URLRequestJob subclass that is built on top of HttpTransaction. It
36// provides an implementation for both HTTP and HTTPS.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000037class NET_EXPORT_PRIVATE URLRequestHttpJob : public URLRequestJob {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000038 public:
39 static URLRequestJob* Factory(URLRequest* request,
40 NetworkDelegate* network_delegate,
41 const std::string& scheme);
42
43 protected:
44 URLRequestHttpJob(URLRequest* request,
45 NetworkDelegate* network_delegate,
46 const HttpUserAgentSettings* http_user_agent_settings);
47
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000048 virtual ~URLRequestHttpJob();
49
50 // Overridden from URLRequestJob:
51 virtual void SetPriority(RequestPriority priority) OVERRIDE;
52 virtual void Start() OVERRIDE;
53 virtual void Kill() OVERRIDE;
54
55 RequestPriority priority() const {
56 return priority_;
57 }
58
59 private:
60 enum CompletionCause {
61 ABORTED,
62 FINISHED
63 };
64
65 typedef base::RefCountedData<bool> SharedBoolean;
66
67 class HttpFilterContext;
68 class HttpTransactionDelegateImpl;
69
Torne (Richard Coles)58218062012-11-14 11:43:16 +000070 // Shadows URLRequestJob's version of this method so we can grab cookies.
71 void NotifyHeadersComplete();
72
73 // Shadows URLRequestJob's method so we can record histograms.
74 void NotifyDone(const URLRequestStatus& status);
75
76 void DestroyTransaction();
77
78 void AddExtraHeaders();
79 void AddCookieHeaderAndStart();
80 void SaveCookiesAndNotifyHeadersComplete(int result);
81 void SaveNextCookie();
82 void FetchResponseCookies(std::vector<std::string>* cookies);
83
84 // Processes the Strict-Transport-Security header, if one exists.
85 void ProcessStrictTransportSecurityHeader();
86
87 // Processes the Public-Key-Pins header, if one exists.
88 void ProcessPublicKeyPinsHeader();
89
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);
Ben Murdoch116680a2014-07-20 18:25:52 -070095 void NotifyBeforeSendProxyHeadersCallback(
96 const ProxyInfo& proxy_info,
97 HttpRequestHeaders* request_headers);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000098
99 void RestartTransactionWithAuth(const AuthCredentials& credentials);
100
101 // Overridden from URLRequestJob:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000102 virtual void SetUpload(UploadDataStream* upload) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103 virtual void SetExtraRequestHeaders(
104 const HttpRequestHeaders& headers) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000105 virtual LoadState GetLoadState() const OVERRIDE;
106 virtual UploadProgress GetUploadProgress() const OVERRIDE;
107 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
108 virtual bool GetCharset(std::string* charset) OVERRIDE;
109 virtual void GetResponseInfo(HttpResponseInfo* info) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000110 virtual void GetLoadTimingInfo(
111 LoadTimingInfo* load_timing_info) const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000112 virtual bool GetResponseCookies(std::vector<std::string>* cookies) OVERRIDE;
113 virtual int GetResponseCode() const OVERRIDE;
114 virtual Filter* SetupFilter() const OVERRIDE;
Ben Murdochc5cede92014-04-10 11:22:14 +0100115 virtual bool CopyFragmentOnRedirect(const GURL& location) const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000116 virtual bool IsSafeRedirect(const GURL& location) OVERRIDE;
117 virtual bool NeedsAuth() OVERRIDE;
118 virtual void GetAuthChallengeInfo(scoped_refptr<AuthChallengeInfo>*) OVERRIDE;
119 virtual void SetAuth(const AuthCredentials& credentials) OVERRIDE;
120 virtual void CancelAuth() OVERRIDE;
121 virtual void ContinueWithCertificate(X509Certificate* client_cert) OVERRIDE;
122 virtual void ContinueDespiteLastError() OVERRIDE;
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000123 virtual void ResumeNetworkStart() OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000124 virtual bool ReadRawData(IOBuffer* buf, int buf_size,
125 int* bytes_read) OVERRIDE;
126 virtual void StopCaching() OVERRIDE;
Ben Murdocheb525c52013-07-10 11:40:50 +0100127 virtual bool GetFullRequestHeaders(
128 HttpRequestHeaders* headers) const OVERRIDE;
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000129 virtual int64 GetTotalReceivedBytes() const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000130 virtual void DoneReading() OVERRIDE;
Ben Murdocheffb81e2014-03-31 11:51:25 +0100131 virtual void DoneReadingRedirectResponse() OVERRIDE;
132
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000133 virtual HostPortPair GetSocketAddress() const OVERRIDE;
134 virtual void NotifyURLRequestDestroyed() OVERRIDE;
135
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000136 void RecordTimer();
137 void ResetTimer();
138
139 virtual void UpdatePacketReadTimes() OVERRIDE;
140 void RecordPacketStats(FilterContext::StatisticSelector statistic) const;
141
142 void RecordCompressionHistograms();
143 bool IsCompressibleContent() const;
144
145 // Starts the transaction if extensions using the webrequest API do not
146 // object.
147 void StartTransaction();
148 // If |result| is net::OK, calls StartTransactionInternal. Otherwise notifies
149 // cancellation.
150 void MaybeStartTransactionInternal(int result);
151 void StartTransactionInternal();
152
153 void RecordPerfHistograms(CompletionCause reason);
154 void DoneWithRequest(CompletionCause reason);
155
156 // Callback functions for Cookie Monster
157 void DoLoadCookies();
158 void CheckCookiePolicyAndLoad(const CookieList& cookie_list);
159 void OnCookiesLoaded(const std::string& cookie_line);
160 void DoStartTransaction();
161
162 // See the implementation for a description of save_next_cookie_running and
163 // callback_pending.
164 void OnCookieSaved(scoped_refptr<SharedBoolean> save_next_cookie_running,
165 scoped_refptr<SharedBoolean> callback_pending,
166 bool cookie_status);
167
168 // Some servers send the body compressed, but specify the content length as
169 // the uncompressed size. If this is the case, we return true in order
170 // to request to work around this non-adherence to the HTTP standard.
171 // |rv| is the standard return value of a read function indicating the number
172 // of bytes read or, if negative, an error code.
173 bool ShouldFixMismatchedContentLength(int rv) const;
174
175 // Returns the effective response headers, considering that they may be
176 // overridden by |override_response_headers_|.
177 HttpResponseHeaders* GetResponseHeaders() const;
178
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000179 RequestPriority priority_;
180
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000181 HttpRequestInfo request_info_;
182 const HttpResponseInfo* response_info_;
183
184 std::vector<std::string> response_cookies_;
185 size_t response_cookies_save_index_;
186 base::Time response_date_;
187
188 // Auth states for proxy and origin server.
189 AuthState proxy_auth_state_;
190 AuthState server_auth_state_;
191 AuthCredentials auth_credentials_;
192
193 CompletionCallback start_callback_;
194 CompletionCallback notify_before_headers_sent_callback_;
195
196 bool read_in_progress_;
197
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000198 scoped_ptr<HttpTransaction> transaction_;
199
200 // This is used to supervise traffic and enforce exponential
201 // back-off. May be NULL.
202 scoped_refptr<URLRequestThrottlerEntryInterface> throttling_entry_;
203
204 // Indicated if an SDCH dictionary was advertised, and hence an SDCH
205 // compressed response is expected. We use this to help detect (accidental?)
206 // proxy corruption of a response, which sometimes marks SDCH content as
207 // having no content encoding <oops>.
208 bool sdch_dictionary_advertised_;
209
210 // For SDCH latency experiments, when we are able to do SDCH, we may enable
211 // either an SDCH latency test xor a pass through test. The following bools
212 // indicate what we decided on for this instance.
213 bool sdch_test_activated_; // Advertising a dictionary for sdch.
214 bool sdch_test_control_; // Not even accepting-content sdch.
215
216 // For recording of stats, we need to remember if this is cached content.
217 bool is_cached_content_;
218
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000219 base::Time request_creation_time_;
220
221 // Data used for statistics gathering. This data is only used for histograms
222 // and is not required. It is only gathered if packet_timing_enabled_ == true.
223 //
224 // TODO(jar): improve the quality of the gathered info by gathering most times
225 // at a lower point in the network stack, assuring we have actual packet
226 // boundaries, rather than approximations. Also note that input byte count
227 // as gathered here is post-SSL, and post-cache-fetch, and does not reflect
228 // true packet arrival times in such cases.
229
230 // Enable recording of packet arrival times for histogramming.
231 bool packet_timing_enabled_;
232 bool done_; // True when we are done doing work.
233
234 // The number of bytes that have been accounted for in packets (where some of
235 // those packets may possibly have had their time of arrival recorded).
236 int64 bytes_observed_in_packets_;
237
238 // The request time may not be available when we are being destroyed, so we
239 // snapshot it early on.
240 base::Time request_time_snapshot_;
241
242 // Since we don't save all packet times in packet_times_, we save the
243 // last time for use in histograms.
244 base::Time final_packet_time_;
245
246 // The start time for the job, ignoring re-starts.
247 base::TimeTicks start_time_;
248
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100249 // When the transaction finished reading the request headers.
250 base::TimeTicks receive_headers_end_;
251
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000252 scoped_ptr<HttpFilterContext> filter_context_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000253
254 CompletionCallback on_headers_received_callback_;
255
256 // We allow the network delegate to modify a copy of the response headers.
257 // This prevents modifications of headers that are shared with the underlying
258 // layers of the network stack.
259 scoped_refptr<HttpResponseHeaders> override_response_headers_;
260
Ben Murdocheffb81e2014-03-31 11:51:25 +0100261 // The network delegate can mark a URL as safe for redirection.
Ben Murdochc5cede92014-04-10 11:22:14 +0100262 // The reference fragment of the original URL is not appended to the redirect
263 // URL when the redirect URL is equal to |allowed_unsafe_redirect_url_|.
Ben Murdocheffb81e2014-03-31 11:51:25 +0100264 GURL allowed_unsafe_redirect_url_;
265
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000266 // Flag used to verify that |this| is not deleted while we are awaiting
267 // a callback from the NetworkDelegate. Used as a fail-fast mechanism.
268 // True if we are waiting a callback and
269 // NetworkDelegate::NotifyURLRequestDestroyed has not been called, yet,
270 // to inform the NetworkDelegate that it may not call back.
271 bool awaiting_callback_;
272
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000273 const HttpUserAgentSettings* http_user_agent_settings_;
274
Torne (Richard Coles)cedac222014-06-03 10:58:34 +0100275 base::WeakPtrFactory<URLRequestHttpJob> weak_factory_;
276
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000277 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJob);
278};
279
280} // namespace net
281
282#endif // NET_URL_REQUEST_URL_REQUEST_HTTP_JOB_H_