blob: c317469cb137ad7ec840f229a7ffeb26dce62ad1 [file] [log] [blame]
[email protected]fecef222012-01-05 02:26:151// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]175adac2008-07-30 17:28:045#include "net/url_request/url_request_http_job.h"
initial.commit586acc5fe2008-07-26 22:42:526
[email protected]4ed2755f2008-12-15 09:01:337#include "base/base_switches.h"
[email protected]4f9e5c82011-11-17 16:04:568#include "base/bind.h"
[email protected]084262c2011-12-01 21:12:479#include "base/bind_helpers.h"
[email protected]4ed2755f2008-12-15 09:01:3310#include "base/command_line.h"
[email protected]39ce5c02008-08-22 04:03:4411#include "base/compiler_specific.h"
[email protected]60889422008-09-23 01:18:1612#include "base/file_version_info.h"
skyostil4891b25b2015-06-11 11:43:4513#include "base/location.h"
[email protected]8684a8812011-03-22 13:59:3814#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3715#include "base/metrics/histogram_macros.h"
vadimt29494d82014-11-04 20:06:2616#include "base/profiler/scoped_tracker.h"
[email protected]5b90b5d2009-04-30 23:06:0117#include "base/rand_util.h"
skyostil4891b25b2015-06-11 11:43:4518#include "base/single_thread_task_runner.h"
[email protected]4dc3ad4f2013-06-11 07:15:5019#include "base/strings/string_util.h"
skyostil4891b25b2015-06-11 11:43:4520#include "base/thread_task_runner_handle.h"
[email protected]f002abb2013-06-28 02:30:2121#include "base/time/time.h"
estade5e5529d2015-05-21 20:59:1122#include "base/values.h"
[email protected]6d81b482011-02-22 19:47:1923#include "net/base/host_port_pair.h"
[email protected]b8430722008-09-17 20:05:4424#include "net/base/load_flags.h"
initial.commit586acc5fe2008-07-26 22:42:5225#include "net/base/net_errors.h"
26#include "net/base/net_util.h"
[email protected]636eccd2011-06-28 12:28:0127#include "net/base/network_delegate.h"
tbansal79ed5cd2015-08-10 18:53:5628#include "net/base/network_quality_estimator.h"
[email protected]60889422008-09-23 01:18:1629#include "net/base/sdch_manager.h"
baranovichc5e38652014-11-14 03:08:1530#include "net/base/sdch_net_log_params.h"
[email protected]6e7845ae2013-03-29 21:48:1131#include "net/cert/cert_status_flags.h"
[email protected]dc8313a2014-03-24 21:38:1432#include "net/cookies/cookie_store.h"
[email protected]262191712014-03-22 00:46:5733#include "net/http/http_content_disposition.h"
[email protected]7a299a92012-10-24 23:54:5034#include "net/http/http_network_session.h"
[email protected]8c76ae22010-04-20 22:15:4335#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2136#include "net/http/http_response_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5237#include "net/http/http_response_info.h"
[email protected]9094b602012-02-27 21:44:5838#include "net/http/http_status_code.h"
initial.commit586acc5fe2008-07-26 22:42:5239#include "net/http/http_transaction.h"
40#include "net/http/http_transaction_factory.h"
[email protected]0757e7702009-03-27 04:00:2241#include "net/http/http_util.h"
[email protected]597a1ab2014-06-26 08:12:2742#include "net/proxy/proxy_info.h"
[email protected]536fd0b2013-03-14 17:41:5743#include "net/ssl/ssl_cert_request_info.h"
44#include "net/ssl/ssl_config_service.h"
[email protected]ee4c30d2012-11-07 15:08:4345#include "net/url_request/http_user_agent_settings.h"
initial.commit586acc5fe2008-07-26 22:42:5246#include "net/url_request/url_request.h"
xunjieli3bb781a2015-07-22 22:40:3447#include "net/url_request/url_request_backoff_manager.h"
[email protected]319d9e6f2009-02-18 19:47:2148#include "net/url_request/url_request_context.h"
initial.commit586acc5fe2008-07-26 22:42:5249#include "net/url_request/url_request_error_job.h"
[email protected]e0f35c92013-05-08 16:04:3450#include "net/url_request/url_request_job_factory.h"
[email protected]06965e02009-09-04 21:36:4251#include "net/url_request/url_request_redirect_job.h"
[email protected]6b3f9642010-11-25 02:29:0652#include "net/url_request/url_request_throttler_manager.h"
[email protected]f4533ba2013-11-28 09:35:4153#include "net/websockets/websocket_handshake_stream_base.h"
initial.commit586acc5fe2008-07-26 22:42:5254
[email protected]8c76ae22010-04-20 22:15:4355static const char kAvailDictionaryHeader[] = "Avail-Dictionary";
56
[email protected]4f5656c62010-12-13 10:47:0957namespace net {
58
[email protected]2e92354c2011-03-25 20:49:5359class URLRequestHttpJob::HttpFilterContext : public FilterContext {
60 public:
61 explicit HttpFilterContext(URLRequestHttpJob* job);
dchengb03027d2014-10-21 12:00:2062 ~HttpFilterContext() override;
[email protected]2e92354c2011-03-25 20:49:5363
64 // FilterContext implementation.
dchengb03027d2014-10-21 12:00:2065 bool GetMimeType(std::string* mime_type) const override;
66 bool GetURL(GURL* gurl) const override;
dchengb03027d2014-10-21 12:00:2067 base::Time GetRequestTime() const override;
68 bool IsCachedContent() const override;
rdsmith81f607562014-11-21 18:35:1669 SdchManager::DictionarySet* SdchDictionariesAdvertised() const override;
dchengb03027d2014-10-21 12:00:2070 int64 GetByteReadCount() const override;
71 int GetResponseCode() const override;
72 const URLRequestContext* GetURLRequestContext() const override;
73 void RecordPacketStats(StatisticSelector statistic) const override;
baranovichc5e38652014-11-14 03:08:1574 const BoundNetLog& GetNetLog() const override;
[email protected]2e92354c2011-03-25 20:49:5375
76 private:
77 URLRequestHttpJob* job_;
78
baranovichc5e38652014-11-14 03:08:1579 // URLRequestHttpJob may be detached from URLRequest, but we still need to
80 // return something.
81 BoundNetLog dummy_log_;
82
[email protected]2e92354c2011-03-25 20:49:5383 DISALLOW_COPY_AND_ASSIGN(HttpFilterContext);
84};
85
[email protected]fc01f232011-03-17 19:06:0186URLRequestHttpJob::HttpFilterContext::HttpFilterContext(URLRequestHttpJob* job)
87 : job_(job) {
88 DCHECK(job_);
89}
90
91URLRequestHttpJob::HttpFilterContext::~HttpFilterContext() {
92}
93
94bool URLRequestHttpJob::HttpFilterContext::GetMimeType(
95 std::string* mime_type) const {
96 return job_->GetMimeType(mime_type);
97}
98
99bool URLRequestHttpJob::HttpFilterContext::GetURL(GURL* gurl) const {
100 if (!job_->request())
101 return false;
102 *gurl = job_->request()->url();
103 return true;
104}
105
106base::Time URLRequestHttpJob::HttpFilterContext::GetRequestTime() const {
107 return job_->request() ? job_->request()->request_time() : base::Time();
108}
109
110bool URLRequestHttpJob::HttpFilterContext::IsCachedContent() const {
[email protected]dd29bcd72011-03-24 00:03:44111 return job_->is_cached_content_;
[email protected]fc01f232011-03-17 19:06:01112}
113
rdsmith81f607562014-11-21 18:35:16114SdchManager::DictionarySet*
115URLRequestHttpJob::HttpFilterContext::SdchDictionariesAdvertised() const {
116 return job_->dictionaries_advertised_.get();
[email protected]fc01f232011-03-17 19:06:01117}
118
119int64 URLRequestHttpJob::HttpFilterContext::GetByteReadCount() const {
mmenkebbf19be22015-04-11 02:44:48120 return job_->prefilter_bytes_read();
[email protected]fc01f232011-03-17 19:06:01121}
122
123int URLRequestHttpJob::HttpFilterContext::GetResponseCode() const {
124 return job_->GetResponseCode();
125}
126
[email protected]6a586762014-06-15 16:02:22127const URLRequestContext*
128URLRequestHttpJob::HttpFilterContext::GetURLRequestContext() const {
129 return job_->request() ? job_->request()->context() : NULL;
130}
131
[email protected]fc01f232011-03-17 19:06:01132void URLRequestHttpJob::HttpFilterContext::RecordPacketStats(
133 StatisticSelector statistic) const {
134 job_->RecordPacketStats(statistic);
135}
136
baranovichc5e38652014-11-14 03:08:15137const BoundNetLog& URLRequestHttpJob::HttpFilterContext::GetNetLog() const {
138 return job_->request() ? job_->request()->net_log() : dummy_log_;
139}
140
initial.commit586acc5fe2008-07-26 22:42:52141// TODO(darin): make sure the port blocking code is not lost
initial.commit586acc5fe2008-07-26 22:42:52142// static
[email protected]4f5656c62010-12-13 10:47:09143URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request,
[email protected]9f170462012-08-24 01:06:58144 NetworkDelegate* network_delegate,
[email protected]4f5656c62010-12-13 10:47:09145 const std::string& scheme) {
[email protected]f4533ba2013-11-28 09:35:41146 DCHECK(scheme == "http" || scheme == "https" || scheme == "ws" ||
147 scheme == "wss");
initial.commit586acc5fe2008-07-26 22:42:52148
[email protected]81293f482012-08-13 19:35:45149 if (!request->context()->http_transaction_factory()) {
initial.commit586acc5fe2008-07-26 22:42:52150 NOTREACHED() << "requires a valid context";
[email protected]9f170462012-08-24 01:06:58151 return new URLRequestErrorJob(
152 request, network_delegate, ERR_INVALID_ARGUMENT);
initial.commit586acc5fe2008-07-26 22:42:52153 }
154
[email protected]ce572df2012-05-04 19:47:17155 GURL redirect_url;
[email protected]8ccc69f2012-11-28 19:52:14156 if (request->GetHSTSRedirect(&redirect_url)) {
157 return new URLRequestRedirectJob(
158 request, network_delegate, redirect_url,
159 // Use status code 307 to preserve the method, so POST requests work.
[email protected]7983c4a2014-03-12 01:47:09160 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "HSTS");
[email protected]8ccc69f2012-11-28 19:52:14161 }
[email protected]ee4c30d2012-11-07 15:08:43162 return new URLRequestHttpJob(request,
163 network_delegate,
164 request->context()->http_user_agent_settings());
initial.commit586acc5fe2008-07-26 22:42:52165}
166
[email protected]ee4c30d2012-11-07 15:08:43167URLRequestHttpJob::URLRequestHttpJob(
168 URLRequest* request,
169 NetworkDelegate* network_delegate,
170 const HttpUserAgentSettings* http_user_agent_settings)
[email protected]9f170462012-08-24 01:06:58171 : URLRequestJob(request, network_delegate),
[email protected]5033ab82013-03-22 20:17:46172 priority_(DEFAULT_PRIORITY),
initial.commit586acc5fe2008-07-26 22:42:52173 response_info_(NULL),
[email protected]34602282010-02-03 22:14:15174 response_cookies_save_index_(0),
[email protected]4f5656c62010-12-13 10:47:09175 proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
176 server_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
[email protected]dd946bb2013-06-12 22:53:01177 start_callback_(base::Bind(&URLRequestHttpJob::OnStartCompleted,
178 base::Unretained(this))),
179 notify_before_headers_sent_callback_(
180 base::Bind(&URLRequestHttpJob::NotifyBeforeSendHeadersCallback,
181 base::Unretained(this))),
[email protected]3589e552008-08-20 23:11:34182 read_in_progress_(false),
[email protected]a73a2802012-05-02 19:20:15183 throttling_entry_(NULL),
[email protected]5b90b5d2009-04-30 23:06:01184 sdch_test_activated_(false),
[email protected]d8fd5132009-05-15 01:06:53185 sdch_test_control_(false),
[email protected]00e48bf2010-12-03 06:15:42186 is_cached_content_(false),
[email protected]ec23f522011-02-22 21:01:38187 request_creation_time_(),
[email protected]dd29bcd72011-03-24 00:03:44188 packet_timing_enabled_(false),
[email protected]bbaea8f2011-06-24 00:11:01189 done_(false),
[email protected]dd29bcd72011-03-24 00:03:44190 bytes_observed_in_packets_(0),
[email protected]dd29bcd72011-03-24 00:03:44191 request_time_snapshot_(),
192 final_packet_time_(),
[email protected]aa249b52013-04-30 01:04:32193 filter_context_(new HttpFilterContext(this)),
[email protected]dd946bb2013-06-12 22:53:01194 on_headers_received_callback_(
195 base::Bind(&URLRequestHttpJob::OnHeadersReceivedCallback,
196 base::Unretained(this))),
[email protected]5a07c192012-07-30 20:18:22197 awaiting_callback_(false),
[email protected]09812102014-05-24 00:04:11198 http_user_agent_settings_(http_user_agent_settings),
xunjieli3bb781a2015-07-22 22:40:34199 backoff_manager_(request->context()->backoff_manager()),
sclittlece72c482015-08-24 20:20:59200 total_received_bytes_from_previous_transactions_(0),
sclittlefb249892015-09-10 21:33:22201 total_sent_bytes_from_previous_transactions_(0),
[email protected]09812102014-05-24 00:04:11202 weak_factory_(this) {
[email protected]a73a2802012-05-02 19:20:15203 URLRequestThrottlerManager* manager = request->context()->throttler_manager();
204 if (manager)
205 throttling_entry_ = manager->RegisterRequestUrl(request->url());
206
[email protected]ec23f522011-02-22 21:01:38207 ResetTimer();
initial.commit586acc5fe2008-07-26 22:42:52208}
209
[email protected]5033ab82013-03-22 20:17:46210URLRequestHttpJob::~URLRequestHttpJob() {
211 CHECK(!awaiting_callback_);
212
213 DCHECK(!sdch_test_control_ || !sdch_test_activated_);
214 if (!is_cached_content_) {
215 if (sdch_test_control_)
216 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_HOLDBACK);
217 if (sdch_test_activated_)
218 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_DECODE);
219 }
220 // Make sure SDCH filters are told to emit histogram data while
221 // filter_context_ is still alive.
222 DestroyFilters();
223
[email protected]5033ab82013-03-22 20:17:46224 DoneWithRequest(ABORTED);
225}
226
227void URLRequestHttpJob::SetPriority(RequestPriority priority) {
228 priority_ = priority;
229 if (transaction_)
230 transaction_->SetPriority(priority_);
231}
232
233void URLRequestHttpJob::Start() {
mmenke833437d2015-04-23 20:56:54234 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
235 tracked_objects::ScopedTracker tracking_profile(
236 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequestHttpJob::Start"));
237
[email protected]5033ab82013-03-22 20:17:46238 DCHECK(!transaction_.get());
239
[email protected]99ecf6e2013-04-10 22:46:13240 // URLRequest::SetReferrer ensures that we do not send username and password
241 // fields in the referrer.
242 GURL referrer(request_->referrer());
[email protected]5033ab82013-03-22 20:17:46243
244 request_info_.url = request_->url();
245 request_info_.method = request_->method();
246 request_info_.load_flags = request_->load_flags();
[email protected]e6d017652013-05-17 18:01:40247 // Enable privacy mode if cookie settings or flags tell us not send or
248 // save cookies.
249 bool enable_privacy_mode =
250 (request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES) ||
251 (request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) ||
252 CanEnablePrivacyMode();
253 // Privacy mode could still be disabled in OnCookiesLoaded if we are going
254 // to send previously saved cookies.
255 request_info_.privacy_mode = enable_privacy_mode ?
[email protected]314b03992014-04-01 01:28:53256 PRIVACY_MODE_ENABLED : PRIVACY_MODE_DISABLED;
[email protected]5033ab82013-03-22 20:17:46257
258 // Strip Referer from request_info_.extra_headers to prevent, e.g., plugins
259 // from overriding headers that are controlled using other means. Otherwise a
260 // plugin could set a referrer although sending the referrer is inhibited.
261 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kReferer);
262
rdsmith81f607562014-11-21 18:35:16263 // Our consumer should have made sure that this is a safe referrer. See for
[email protected]5033ab82013-03-22 20:17:46264 // instance WebCore::FrameLoader::HideReferrer.
265 if (referrer.is_valid()) {
266 request_info_.extra_headers.SetHeader(HttpRequestHeaders::kReferer,
267 referrer.spec());
268 }
269
270 request_info_.extra_headers.SetHeaderIfMissing(
271 HttpRequestHeaders::kUserAgent,
272 http_user_agent_settings_ ?
[email protected]aa051272014-03-10 05:56:56273 http_user_agent_settings_->GetUserAgent() : std::string());
[email protected]5033ab82013-03-22 20:17:46274
275 AddExtraHeaders();
276 AddCookieHeaderAndStart();
277}
278
279void URLRequestHttpJob::Kill() {
[email protected]5033ab82013-03-22 20:17:46280 if (!transaction_.get())
281 return;
282
283 weak_factory_.InvalidateWeakPtrs();
284 DestroyTransaction();
285 URLRequestJob::Kill();
286}
287
ttuttle3ae06922015-05-11 23:41:52288void URLRequestHttpJob::GetConnectionAttempts(ConnectionAttempts* out) const {
289 if (transaction_)
290 transaction_->GetConnectionAttempts(out);
291 else
292 out->clear();
293}
294
[email protected]597a1ab2014-06-26 08:12:27295void URLRequestHttpJob::NotifyBeforeSendProxyHeadersCallback(
[email protected]1252d42f2014-07-01 21:20:20296 const ProxyInfo& proxy_info,
297 HttpRequestHeaders* request_headers) {
298 DCHECK(request_headers);
[email protected]597a1ab2014-06-26 08:12:27299 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
300 if (network_delegate()) {
301 network_delegate()->NotifyBeforeSendProxyHeaders(
302 request_,
303 proxy_info,
[email protected]1252d42f2014-07-01 21:20:20304 request_headers);
[email protected]597a1ab2014-06-26 08:12:27305 }
306}
307
xunjieli3bb781a2015-07-22 22:40:34308void URLRequestHttpJob::NotifyBeforeNetworkStart(bool* defer) {
309 if (!request_)
310 return;
311 if (backoff_manager_) {
xunjieli33550b842015-09-24 22:26:58312 if ((request_->load_flags() & LOAD_MAYBE_USER_GESTURE) == 0 &&
313 backoff_manager_->ShouldRejectRequest(request()->url(),
xunjieli3bb781a2015-07-22 22:40:34314 request()->request_time())) {
315 *defer = true;
316 base::MessageLoop::current()->PostTask(
317 FROM_HERE,
318 base::Bind(&URLRequestHttpJob::OnStartCompleted,
319 weak_factory_.GetWeakPtr(), ERR_TEMPORARY_BACKOFF));
320 return;
321 }
322 }
323 URLRequestJob::NotifyBeforeNetworkStart(defer);
324}
325
[email protected]175adac2008-07-30 17:28:04326void URLRequestHttpJob::NotifyHeadersComplete() {
initial.commit586acc5fe2008-07-26 22:42:52327 DCHECK(!response_info_);
328
329 response_info_ = transaction_->GetResponseInfo();
330
[email protected]d8fd5132009-05-15 01:06:53331 // Save boolean, as we'll need this info at destruction time, and filters may
332 // also need this info.
333 is_cached_content_ = response_info_->was_cached;
334
xunjieli041e9392015-05-19 21:51:33335 if (!is_cached_content_ && throttling_entry_.get())
336 throttling_entry_->UpdateWithResponse(GetResponseCode());
[email protected]6b3f9642010-11-25 02:29:06337
xunjieli3bb781a2015-07-22 22:40:34338 if (!is_cached_content_)
339 ProcessBackoffHeader();
340
[email protected]fecef222012-01-05 02:26:15341 // The ordering of these calls is not important.
[email protected]77f6fb432009-09-05 14:21:09342 ProcessStrictTransportSecurityHeader();
[email protected]fecef222012-01-05 02:26:15343 ProcessPublicKeyPinsHeader();
[email protected]a9cea7542009-05-20 04:30:23344
rdsmith81f607562014-11-21 18:35:16345 // Handle the server notification of a new SDCH dictionary.
[email protected]6a586762014-06-15 16:02:22346 SdchManager* sdch_manager(request()->context()->sdch_manager());
baranovichc5e38652014-11-14 03:08:15347 if (sdch_manager) {
348 SdchProblemCode rv = sdch_manager->IsInSupportedDomain(request()->url());
349 if (rv != SDCH_OK) {
ellyjones085ac462015-08-18 12:02:53350 SdchManager::SdchErrorRecovery(rv);
351 request()->net_log().AddEvent(
352 NetLog::TYPE_SDCH_DECODING_ERROR,
353 base::Bind(&NetLogSdchResourceProblemCallback, rv));
baranovichc5e38652014-11-14 03:08:15354 } else {
355 const std::string name = "Get-Dictionary";
356 std::string url_text;
357 void* iter = NULL;
358 // TODO(jar): We need to not fetch dictionaries the first time they are
359 // seen, but rather wait until we can justify their usefulness.
360 // For now, we will only fetch the first dictionary, which will at least
361 // require multiple suggestions before we get additional ones for this
362 // site. Eventually we should wait until a dictionary is requested
363 // several times
364 // before we even download it (so that we don't waste memory or
365 // bandwidth).
366 if (GetResponseHeaders()->EnumerateHeader(&iter, name, &url_text)) {
367 // Resolve suggested URL relative to request url.
368 GURL sdch_dictionary_url = request_->url().Resolve(url_text);
369 if (sdch_dictionary_url.is_valid()) {
370 rv = sdch_manager->OnGetDictionary(request_->url(),
371 sdch_dictionary_url);
372 if (rv != SDCH_OK) {
373 SdchManager::SdchErrorRecovery(rv);
374 request_->net_log().AddEvent(
375 NetLog::TYPE_SDCH_DICTIONARY_ERROR,
376 base::Bind(&NetLogSdchDictionaryFetchProblemCallback, rv,
377 sdch_dictionary_url, false));
378 }
379 }
[email protected]6a586762014-06-15 16:02:22380 }
[email protected]60889422008-09-23 01:18:16381 }
382 }
383
rdsmith81f607562014-11-21 18:35:16384 // Handle the server signalling no SDCH encoding.
385 if (dictionaries_advertised_) {
386 // We are wary of proxies that discard or damage SDCH encoding. If a server
387 // explicitly states that this is not SDCH content, then we can correct our
388 // assumption that this is an SDCH response, and avoid the need to recover
389 // as though the content is corrupted (when we discover it is not SDCH
390 // encoded).
391 std::string sdch_response_status;
392 void* iter = NULL;
393 while (GetResponseHeaders()->EnumerateHeader(&iter, "X-Sdch-Encode",
394 &sdch_response_status)) {
395 if (sdch_response_status == "0") {
396 dictionaries_advertised_.reset();
397 break;
398 }
399 }
400 }
401
[email protected]0757e7702009-03-27 04:00:22402 // The HTTP transaction may be restarted several times for the purposes
403 // of sending authorization information. Each time it restarts, we get
404 // notified of the headers completion so that we can update the cookie store.
405 if (transaction_->IsReadyToRestartForAuth()) {
406 DCHECK(!response_info_->auth_challenge.get());
[email protected]87a09a92011-07-14 15:50:50407 // TODO(battre): This breaks the webrequest API for
408 // URLRequestTestHTTP.BasicAuthWithCookies
[email protected]5796dc942011-07-14 19:26:10409 // where OnBeforeSendHeaders -> OnSendHeaders -> OnBeforeSendHeaders
[email protected]87a09a92011-07-14 15:50:50410 // occurs.
[email protected]f3cf9802011-10-28 18:44:58411 RestartTransactionWithAuth(AuthCredentials());
[email protected]0757e7702009-03-27 04:00:22412 return;
413 }
414
[email protected]4f5656c62010-12-13 10:47:09415 URLRequestJob::NotifyHeadersComplete();
initial.commit586acc5fe2008-07-26 22:42:52416}
417
[email protected]85c1dce2011-07-06 12:01:29418void URLRequestHttpJob::NotifyDone(const URLRequestStatus& status) {
[email protected]bbaea8f2011-06-24 00:11:01419 DoneWithRequest(FINISHED);
[email protected]dd29bcd72011-03-24 00:03:44420 URLRequestJob::NotifyDone(status);
421}
422
[email protected]175adac2008-07-30 17:28:04423void URLRequestHttpJob::DestroyTransaction() {
[email protected]c6a4eb92010-03-03 23:51:19424 DCHECK(transaction_.get());
initial.commit586acc5fe2008-07-26 22:42:52425
[email protected]bbaea8f2011-06-24 00:11:01426 DoneWithRequest(ABORTED);
sclittlece72c482015-08-24 20:20:59427
428 total_received_bytes_from_previous_transactions_ +=
429 transaction_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22430 total_sent_bytes_from_previous_transactions_ +=
431 transaction_->GetTotalSentBytes();
[email protected]af4876d2008-10-21 23:10:57432 transaction_.reset();
initial.commit586acc5fe2008-07-26 22:42:52433 response_info_ = NULL;
[email protected]3b23a222013-05-15 21:33:25434 receive_headers_end_ = base::TimeTicks();
initial.commit586acc5fe2008-07-26 22:42:52435}
436
[email protected]175adac2008-07-30 17:28:04437void URLRequestHttpJob::StartTransaction() {
mmenke833437d2015-04-23 20:56:54438 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
439 tracked_objects::ScopedTracker tracking_profile(
440 FROM_HERE_WITH_EXPLICIT_FUNCTION(
441 "456327 URLRequestHttpJob::StartTransaction"));
442
[email protected]cc05edc2013-03-08 18:04:41443 if (network_delegate()) {
[email protected]abe1c4a2013-10-25 19:28:51444 OnCallToDelegate();
[email protected]cc05edc2013-03-08 18:04:41445 int rv = network_delegate()->NotifyBeforeSendHeaders(
[email protected]084262c2011-12-01 21:12:47446 request_, notify_before_headers_sent_callback_,
[email protected]636eccd2011-06-28 12:28:01447 &request_info_.extra_headers);
448 // If an extension blocks the request, we rely on the callback to
[email protected]b4438d32012-09-27 06:15:30449 // MaybeStartTransactionInternal().
[email protected]abe1c4a2013-10-25 19:28:51450 if (rv == ERR_IO_PENDING)
[email protected]636eccd2011-06-28 12:28:01451 return;
[email protected]b4438d32012-09-27 06:15:30452 MaybeStartTransactionInternal(rv);
453 return;
[email protected]636eccd2011-06-28 12:28:01454 }
455 StartTransactionInternal();
456}
457
458void URLRequestHttpJob::NotifyBeforeSendHeadersCallback(int result) {
[email protected]9045b8822012-01-13 20:35:35459 // Check that there are no callbacks to already canceled requests.
460 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
461
[email protected]b4438d32012-09-27 06:15:30462 MaybeStartTransactionInternal(result);
463}
464
465void URLRequestHttpJob::MaybeStartTransactionInternal(int result) {
mmenke833437d2015-04-23 20:56:54466 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
467 tracked_objects::ScopedTracker tracking_profile(
468 FROM_HERE_WITH_EXPLICIT_FUNCTION(
469 "456327 URLRequestHttpJob::MaybeStartTransactionInternal"));
470
[email protected]abe1c4a2013-10-25 19:28:51471 OnCallToDelegateComplete();
[email protected]636eccd2011-06-28 12:28:01472 if (result == OK) {
473 StartTransactionInternal();
474 } else {
[email protected]55b8a6c12012-06-13 22:03:42475 std::string source("delegate");
[email protected]636eccd2011-06-28 12:28:01476 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42477 NetLog::StringCallback("source", &source));
[email protected]636eccd2011-06-28 12:28:01478 NotifyCanceled();
[email protected]b4438d32012-09-27 06:15:30479 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
[email protected]636eccd2011-06-28 12:28:01480 }
481}
482
483void URLRequestHttpJob::StartTransactionInternal() {
initial.commit586acc5fe2008-07-26 22:42:52484 // NOTE: This method assumes that request_info_ is already setup properly.
485
[email protected]34602282010-02-03 22:14:15486 // If we already have a transaction, then we should restart the transaction
[email protected]f3cf9802011-10-28 18:44:58487 // with auth provided by auth_credentials_.
initial.commit586acc5fe2008-07-26 22:42:52488
[email protected]99c07902010-08-17 18:59:52489 int rv;
[email protected]6b3f9642010-11-25 02:29:06490
[email protected]cc05edc2013-03-08 18:04:41491 if (network_delegate()) {
492 network_delegate()->NotifySendHeaders(
[email protected]5796dc942011-07-14 19:26:10493 request_, request_info_.extra_headers);
494 }
495
[email protected]34602282010-02-03 22:14:15496 if (transaction_.get()) {
[email protected]49639fa2011-12-20 23:22:41497 rv = transaction_->RestartWithAuth(auth_credentials_, start_callback_);
[email protected]f3cf9802011-10-28 18:44:58498 auth_credentials_ = AuthCredentials();
[email protected]34602282010-02-03 22:14:15499 } else {
[email protected]34602282010-02-03 22:14:15500 DCHECK(request_->context()->http_transaction_factory());
initial.commit586acc5fe2008-07-26 22:42:52501
[email protected]99c07902010-08-17 18:59:52502 rv = request_->context()->http_transaction_factory()->CreateTransaction(
[email protected]027bd85a2013-12-27 22:39:10503 priority_, &transaction_);
[email protected]f4533ba2013-11-28 09:35:41504
505 if (rv == OK && request_info_.url.SchemeIsWSOrWSS()) {
[email protected]f4533ba2013-11-28 09:35:41506 base::SupportsUserData::Data* data = request_->GetUserData(
507 WebSocketHandshakeStreamBase::CreateHelper::DataKey());
508 if (data) {
509 transaction_->SetWebSocketHandshakeStreamCreateHelper(
510 static_cast<WebSocketHandshakeStreamBase::CreateHelper*>(data));
511 } else {
512 rv = ERR_DISALLOWED_URL_SCHEME;
513 }
514 }
515
[email protected]4f5656c62010-12-13 10:47:09516 if (rv == OK) {
[email protected]a45840b2014-01-10 15:40:22517 transaction_->SetBeforeNetworkStartCallback(
518 base::Bind(&URLRequestHttpJob::NotifyBeforeNetworkStart,
519 base::Unretained(this)));
[email protected]597a1ab2014-06-26 08:12:27520 transaction_->SetBeforeProxyHeadersSentCallback(
521 base::Bind(&URLRequestHttpJob::NotifyBeforeSendProxyHeadersCallback,
522 base::Unretained(this)));
[email protected]a45840b2014-01-10 15:40:22523
dchengc2e01e82014-08-27 00:24:42524 if (!throttling_entry_.get() ||
xunjieli41edcdd2015-06-24 14:26:40525 !throttling_entry_->ShouldRejectRequest(*request_)) {
[email protected]227b0e82011-03-25 21:11:53526 rv = transaction_->Start(
[email protected]49639fa2011-12-20 23:22:41527 &request_info_, start_callback_, request_->net_log());
[email protected]bbaea8f2011-06-24 00:11:01528 start_time_ = base::TimeTicks::Now();
[email protected]227b0e82011-03-25 21:11:53529 } else {
530 // Special error code for the exponential back-off module.
531 rv = ERR_TEMPORARILY_THROTTLED;
532 }
[email protected]34602282010-02-03 22:14:15533 }
initial.commit586acc5fe2008-07-26 22:42:52534 }
535
[email protected]4f5656c62010-12-13 10:47:09536 if (rv == ERR_IO_PENDING)
[email protected]34602282010-02-03 22:14:15537 return;
538
initial.commit586acc5fe2008-07-26 22:42:52539 // The transaction started synchronously, but we need to notify the
[email protected]4f5656c62010-12-13 10:47:09540 // URLRequest delegate via the message loop.
skyostil4891b25b2015-06-11 11:43:45541 base::ThreadTaskRunnerHandle::Get()->PostTask(
542 FROM_HERE, base::Bind(&URLRequestHttpJob::OnStartCompleted,
543 weak_factory_.GetWeakPtr(), rv));
initial.commit586acc5fe2008-07-26 22:42:52544}
545
[email protected]175adac2008-07-30 17:28:04546void URLRequestHttpJob::AddExtraHeaders() {
[email protected]6a586762014-06-15 16:02:22547 SdchManager* sdch_manager = request()->context()->sdch_manager();
548
[email protected]c7bef94c2011-06-21 18:05:51549 // Supply Accept-Encoding field only if it is not already provided.
550 // It should be provided IF the content is known to have restrictions on
551 // potential encoding, such as streaming multi-media.
552 // For details see bug 47381.
553 // TODO(jar, enal): jpeg files etc. should set up a request header if
554 // possible. Right now it is done only by buffered_resource_loader and
555 // simple_data_source.
556 if (!request_info_.extra_headers.HasHeader(
557 HttpRequestHeaders::kAcceptEncoding)) {
baranovichc5e38652014-11-14 03:08:15558 // We don't support SDCH responses to POST as there is a possibility
559 // of having SDCH encoded responses returned (e.g. by the cache)
560 // which we cannot decode, and in those situations, we will need
561 // to retransmit the request without SDCH, which is illegal for a POST.
562 bool advertise_sdch = sdch_manager != NULL && request()->method() != "POST";
563 if (advertise_sdch) {
564 SdchProblemCode rv = sdch_manager->IsInSupportedDomain(request()->url());
565 if (rv != SDCH_OK) {
566 advertise_sdch = false;
ellyjones085ac462015-08-18 12:02:53567 SdchManager::SdchErrorRecovery(rv);
568 request()->net_log().AddEvent(
569 NetLog::TYPE_SDCH_DECODING_ERROR,
570 base::Bind(&NetLogSdchResourceProblemCallback, rv));
baranovichc5e38652014-11-14 03:08:15571 }
572 }
[email protected]c7bef94c2011-06-21 18:05:51573 if (advertise_sdch) {
rdsmith81f607562014-11-21 18:35:16574 dictionaries_advertised_ =
575 sdch_manager->GetDictionarySet(request_->url());
576 }
[email protected]5b90b5d2009-04-30 23:06:01577
rdsmith81f607562014-11-21 18:35:16578 // The AllowLatencyExperiment() is only true if we've successfully done a
579 // full SDCH compression recently in this browser session for this host.
580 // Note that for this path, there might be no applicable dictionaries,
581 // and hence we can't participate in the experiment.
582 if (dictionaries_advertised_ &&
583 sdch_manager->AllowLatencyExperiment(request_->url())) {
584 // We are participating in the test (or control), and hence we'll
585 // eventually record statistics via either SDCH_EXPERIMENT_DECODE or
586 // SDCH_EXPERIMENT_HOLDBACK, and we'll need some packet timing data.
587 packet_timing_enabled_ = true;
588 if (base::RandDouble() < .01) {
589 sdch_test_control_ = true; // 1% probability.
590 dictionaries_advertised_.reset();
591 advertise_sdch = false;
592 } else {
593 sdch_test_activated_ = true;
[email protected]5b90b5d2009-04-30 23:06:01594 }
595 }
[email protected]5b90b5d2009-04-30 23:06:01596
[email protected]c7bef94c2011-06-21 18:05:51597 // Supply Accept-Encoding headers first so that it is more likely that they
rdsmith81f607562014-11-21 18:35:16598 // will be in the first transmitted packet. This can sometimes make it
[email protected]c7bef94c2011-06-21 18:05:51599 // easier to filter and analyze the streams to assure that a proxy has not
rdsmith81f607562014-11-21 18:35:16600 // damaged these headers. Some proxies deliberately corrupt Accept-Encoding
[email protected]c7bef94c2011-06-21 18:05:51601 // headers.
602 if (!advertise_sdch) {
603 // Tell the server what compression formats we support (other than SDCH).
[email protected]8c76ae22010-04-20 22:15:43604 request_info_.extra_headers.SetHeader(
jgraettinger8f00db8b2014-09-24 23:00:06605 HttpRequestHeaders::kAcceptEncoding, "gzip, deflate");
[email protected]c7bef94c2011-06-21 18:05:51606 } else {
607 // Include SDCH in acceptable list.
608 request_info_.extra_headers.SetHeader(
jgraettinger8f00db8b2014-09-24 23:00:06609 HttpRequestHeaders::kAcceptEncoding, "gzip, deflate, sdch");
rdsmith81f607562014-11-21 18:35:16610 if (dictionaries_advertised_) {
[email protected]c7bef94c2011-06-21 18:05:51611 request_info_.extra_headers.SetHeader(
612 kAvailDictionaryHeader,
rdsmith81f607562014-11-21 18:35:16613 dictionaries_advertised_->GetDictionaryClientHashList());
[email protected]c7bef94c2011-06-21 18:05:51614 // Since we're tagging this transaction as advertising a dictionary,
615 // we'll definitely employ an SDCH filter (or tentative sdch filter)
rdsmith81f607562014-11-21 18:35:16616 // when we get a response. When done, we'll record histograms via
617 // SDCH_DECODE or SDCH_PASSTHROUGH. Hence we need to record packet
[email protected]c7bef94c2011-06-21 18:05:51618 // arrival times.
619 packet_timing_enabled_ = true;
620 }
[email protected]423041b2008-10-27 17:39:28621 }
[email protected]423041b2008-10-27 17:39:28622 }
623
[email protected]ee4c30d2012-11-07 15:08:43624 if (http_user_agent_settings_) {
[email protected]84f05432013-03-15 01:00:12625 // Only add default Accept-Language if the request didn't have it
626 // specified.
[email protected]ee4c30d2012-11-07 15:08:43627 std::string accept_language =
628 http_user_agent_settings_->GetAcceptLanguage();
629 if (!accept_language.empty()) {
630 request_info_.extra_headers.SetHeaderIfMissing(
631 HttpRequestHeaders::kAcceptLanguage,
632 accept_language);
633 }
initial.commit586acc5fe2008-07-26 22:42:52634 }
initial.commit586acc5fe2008-07-26 22:42:52635}
636
[email protected]34602282010-02-03 22:14:15637void URLRequestHttpJob::AddCookieHeaderAndStart() {
638 // No matter what, we want to report our status as IO pending since we will
639 // be notifying our consumer asynchronously via OnStartCompleted.
640 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
[email protected]861fcd52009-08-26 02:33:46641
[email protected]ed24fad2011-05-10 22:44:01642 // If the request was destroyed, then there is no more work to do.
643 if (!request_)
644 return;
[email protected]34602282010-02-03 22:14:15645
davidben151423e2015-03-23 18:48:36646 CookieStore* cookie_store = request_->context()->cookie_store();
[email protected]1a6fff52011-10-20 21:00:16647 if (cookie_store && !(request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES)) {
[email protected]dc8313a2014-03-24 21:38:14648 cookie_store->GetAllCookiesForURLAsync(
649 request_->url(),
650 base::Bind(&URLRequestHttpJob::CheckCookiePolicyAndLoad,
651 weak_factory_.GetWeakPtr()));
[email protected]03d845f2011-07-29 19:06:26652 } else {
653 DoStartTransaction();
654 }
655}
656
[email protected]1a6fff52011-10-20 21:00:16657void URLRequestHttpJob::DoLoadCookies() {
658 CookieOptions options;
659 options.set_include_httponly();
mkwst0513c9d2015-04-01 05:53:15660
661 // TODO(mkwst): Drop this `if` once we decide whether or not to ship
662 // first-party cookies: https://crbug.com/459154
663 if (network_delegate() &&
664 network_delegate()->FirstPartyOnlyCookieExperimentEnabled())
665 options.set_first_party_url(request_->first_party_for_cookies());
666 else
667 options.set_include_first_party_only();
mkwstae819bb2015-02-23 05:10:31668
davidben151423e2015-03-23 18:48:36669 request_->context()->cookie_store()->GetCookiesWithOptionsAsync(
670 request_->url(), options, base::Bind(&URLRequestHttpJob::OnCookiesLoaded,
671 weak_factory_.GetWeakPtr()));
[email protected]1a6fff52011-10-20 21:00:16672}
673
[email protected]03d845f2011-07-29 19:06:26674void URLRequestHttpJob::CheckCookiePolicyAndLoad(
675 const CookieList& cookie_list) {
[email protected]1a6fff52011-10-20 21:00:16676 if (CanGetCookies(cookie_list))
677 DoLoadCookies();
678 else
[email protected]54f4c9362011-07-25 21:54:46679 DoStartTransaction();
[email protected]54f4c9362011-07-25 21:54:46680}
681
[email protected]dedec0b2013-02-28 04:50:10682void URLRequestHttpJob::OnCookiesLoaded(const std::string& cookie_line) {
[email protected]218aa6a12011-09-13 17:38:38683 if (!cookie_line.empty()) {
[email protected]54f4c9362011-07-25 21:54:46684 request_info_.extra_headers.SetHeader(
[email protected]218aa6a12011-09-13 17:38:38685 HttpRequestHeaders::kCookie, cookie_line);
[email protected]e6d017652013-05-17 18:01:40686 // Disable privacy mode as we are sending cookies anyway.
[email protected]314b03992014-04-01 01:28:53687 request_info_.privacy_mode = PRIVACY_MODE_DISABLED;
[email protected]54f4c9362011-07-25 21:54:46688 }
[email protected]54f4c9362011-07-25 21:54:46689 DoStartTransaction();
690}
691
692void URLRequestHttpJob::DoStartTransaction() {
[email protected]03d845f2011-07-29 19:06:26693 // We may have been canceled while retrieving cookies.
[email protected]9025016c2011-05-12 15:51:23694 if (GetStatus().is_success()) {
695 StartTransaction();
696 } else {
697 NotifyCanceled();
698 }
[email protected]0757e7702009-03-27 04:00:22699}
700
[email protected]ea8141e2011-10-05 13:12:51701void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
[email protected]abe1c4a2013-10-25 19:28:51702 // End of the call started in OnStartCompleted.
703 OnCallToDelegateComplete();
704
ttuttle859dc7a2015-04-23 19:42:29705 if (result != OK) {
[email protected]55b8a6c12012-06-13 22:03:42706 std::string source("delegate");
[email protected]ea8141e2011-10-05 13:12:51707 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42708 NetLog::StringCallback("source", &source));
[email protected]ea8141e2011-10-05 13:12:51709 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
710 return;
711 }
712
[email protected]34602282010-02-03 22:14:15713 DCHECK(transaction_.get());
initial.commit586acc5fe2008-07-26 22:42:52714
[email protected]4f5656c62010-12-13 10:47:09715 const HttpResponseInfo* response_info = transaction_->GetResponseInfo();
[email protected]34602282010-02-03 22:14:15716 DCHECK(response_info);
717
718 response_cookies_.clear();
719 response_cookies_save_index_ = 0;
720
[email protected]ea8141e2011-10-05 13:12:51721 FetchResponseCookies(&response_cookies_);
[email protected]34602282010-02-03 22:14:15722
[email protected]5095cd72012-11-01 10:29:16723 if (!GetResponseHeaders()->GetDateValue(&response_date_))
724 response_date_ = base::Time();
725
[email protected]34602282010-02-03 22:14:15726 // Now, loop over the response cookies, and attempt to persist each.
727 SaveNextCookie();
728}
729
[email protected]263163f2012-06-14 22:40:34730// If the save occurs synchronously, SaveNextCookie will loop and save the next
731// cookie. If the save is deferred, the callback is responsible for continuing
732// to iterate through the cookies.
733// TODO(erikwright): Modify the CookieStore API to indicate via return value
734// whether it completed synchronously or asynchronously.
735// See http://crbug.com/131066.
[email protected]34602282010-02-03 22:14:15736void URLRequestHttpJob::SaveNextCookie() {
[email protected]263163f2012-06-14 22:40:34737 // No matter what, we want to report our status as IO pending since we will
738 // be notifying our consumer asynchronously via OnStartCompleted.
739 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
740
741 // Used to communicate with the callback. See the implementation of
742 // OnCookieSaved.
743 scoped_refptr<SharedBoolean> callback_pending = new SharedBoolean(false);
744 scoped_refptr<SharedBoolean> save_next_cookie_running =
745 new SharedBoolean(true);
746
747 if (!(request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) &&
davidben151423e2015-03-23 18:48:36748 request_->context()->cookie_store() && response_cookies_.size() > 0) {
[email protected]263163f2012-06-14 22:40:34749 CookieOptions options;
750 options.set_include_httponly();
[email protected]5095cd72012-11-01 10:29:16751 options.set_server_time(response_date_);
[email protected]263163f2012-06-14 22:40:34752
ttuttle859dc7a2015-04-23 19:42:29753 CookieStore::SetCookiesCallback callback(base::Bind(
754 &URLRequestHttpJob::OnCookieSaved, weak_factory_.GetWeakPtr(),
755 save_next_cookie_running, callback_pending));
[email protected]263163f2012-06-14 22:40:34756
757 // Loop through the cookies as long as SetCookieWithOptionsAsync completes
758 // synchronously.
759 while (!callback_pending->data &&
760 response_cookies_save_index_ < response_cookies_.size()) {
761 if (CanSetCookie(
762 response_cookies_[response_cookies_save_index_], &options)) {
763 callback_pending->data = true;
davidben151423e2015-03-23 18:48:36764 request_->context()->cookie_store()->SetCookieWithOptionsAsync(
[email protected]263163f2012-06-14 22:40:34765 request_->url(), response_cookies_[response_cookies_save_index_],
766 options, callback);
767 }
768 ++response_cookies_save_index_;
769 }
770 }
771
772 save_next_cookie_running->data = false;
773
774 if (!callback_pending->data) {
[email protected]34602282010-02-03 22:14:15775 response_cookies_.clear();
776 response_cookies_save_index_ = 0;
777 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
778 NotifyHeadersComplete();
779 return;
780 }
[email protected]263163f2012-06-14 22:40:34781}
[email protected]34602282010-02-03 22:14:15782
[email protected]263163f2012-06-14 22:40:34783// |save_next_cookie_running| is true when the callback is bound and set to
784// false when SaveNextCookie exits, allowing the callback to determine if the
785// save occurred synchronously or asynchronously.
786// |callback_pending| is false when the callback is invoked and will be set to
787// true by the callback, allowing SaveNextCookie to detect whether the save
788// occurred synchronously.
789// See SaveNextCookie() for more information.
790void URLRequestHttpJob::OnCookieSaved(
791 scoped_refptr<SharedBoolean> save_next_cookie_running,
792 scoped_refptr<SharedBoolean> callback_pending,
793 bool cookie_status) {
794 callback_pending->data = false;
[email protected]34602282010-02-03 22:14:15795
[email protected]263163f2012-06-14 22:40:34796 // If we were called synchronously, return.
797 if (save_next_cookie_running->data) {
798 return;
[email protected]34602282010-02-03 22:14:15799 }
800
[email protected]263163f2012-06-14 22:40:34801 // We were called asynchronously, so trigger the next save.
[email protected]9025016c2011-05-12 15:51:23802 // We may have been canceled within OnSetCookie.
803 if (GetStatus().is_success()) {
804 SaveNextCookie();
805 } else {
806 NotifyCanceled();
807 }
[email protected]34602282010-02-03 22:14:15808}
809
810void URLRequestHttpJob::FetchResponseCookies(
[email protected]34602282010-02-03 22:14:15811 std::vector<std::string>* cookies) {
[email protected]264300242011-11-07 06:03:30812 const std::string name = "Set-Cookie";
initial.commit586acc5fe2008-07-26 22:42:52813 std::string value;
814
815 void* iter = NULL;
[email protected]ea8141e2011-10-05 13:12:51816 HttpResponseHeaders* headers = GetResponseHeaders();
817 while (headers->EnumerateHeader(&iter, name, &value)) {
[email protected]2adf2882010-09-27 08:30:37818 if (!value.empty())
819 cookies->push_back(value);
820 }
initial.commit586acc5fe2008-07-26 22:42:52821}
[email protected]a9cea7542009-05-20 04:30:23822
xunjieli3bb781a2015-07-22 22:40:34823void URLRequestHttpJob::ProcessBackoffHeader() {
824 DCHECK(response_info_);
825
826 if (!backoff_manager_)
827 return;
828
829 TransportSecurityState* security_state =
830 request_->context()->transport_security_state();
831 const SSLInfo& ssl_info = response_info_->ssl_info;
832
833 // Only accept Backoff headers on HTTPS connections that have no
834 // certificate errors.
835 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
836 !security_state)
837 return;
838
839 backoff_manager_->UpdateWithResponse(request()->url(), GetResponseHeaders(),
840 base::Time::Now());
841}
842
[email protected]000d9df02012-01-18 20:01:46843// NOTE: |ProcessStrictTransportSecurityHeader| and
844// |ProcessPublicKeyPinsHeader| have very similar structures, by design.
[email protected]77f6fb432009-09-05 14:21:09845void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() {
[email protected]a9cea7542009-05-20 04:30:23846 DCHECK(response_info_);
[email protected]6ed72be2013-01-08 22:07:33847 TransportSecurityState* security_state =
848 request_->context()->transport_security_state();
[email protected]e88006f2012-01-11 06:15:07849 const SSLInfo& ssl_info = response_info_->ssl_info;
[email protected]a9cea7542009-05-20 04:30:23850
[email protected]6ed72be2013-01-08 22:07:33851 // Only accept HSTS headers on HTTPS connections that have no
852 // certificate errors.
[email protected]e88006f2012-01-11 06:15:07853 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
[email protected]6ed72be2013-01-08 22:07:33854 !security_state)
[email protected]e88006f2012-01-11 06:15:07855 return;
[email protected]326e6792009-12-11 21:04:42856
estarka5da76702015-04-09 04:00:16857 // Don't accept HSTS headers when the hostname is an IP address.
858 if (request_info_.url.HostIsIPAddress())
859 return;
860
[email protected]242d8562012-10-30 21:20:46861 // http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec:
862 //
863 // If a UA receives more than one STS header field in a HTTP response
864 // message over secure transport, then the UA MUST process only the
865 // first such header field.
[email protected]6ed72be2013-01-08 22:07:33866 HttpResponseHeaders* headers = GetResponseHeaders();
867 std::string value;
868 if (headers->EnumerateHeader(NULL, "Strict-Transport-Security", &value))
869 security_state->AddHSTSHeader(request_info_.url.host(), value);
[email protected]a9cea7542009-05-20 04:30:23870}
[email protected]4f5656c62010-12-13 10:47:09871
[email protected]fecef222012-01-05 02:26:15872void URLRequestHttpJob::ProcessPublicKeyPinsHeader() {
873 DCHECK(response_info_);
[email protected]6ed72be2013-01-08 22:07:33874 TransportSecurityState* security_state =
875 request_->context()->transport_security_state();
[email protected]fecef222012-01-05 02:26:15876 const SSLInfo& ssl_info = response_info_->ssl_info;
877
[email protected]6ed72be2013-01-08 22:07:33878 // Only accept HPKP headers on HTTPS connections that have no
[email protected]e88006f2012-01-11 06:15:07879 // certificate errors.
[email protected]fecef222012-01-05 02:26:15880 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
[email protected]6ed72be2013-01-08 22:07:33881 !security_state)
[email protected]fecef222012-01-05 02:26:15882 return;
[email protected]fecef222012-01-05 02:26:15883
estarka5da76702015-04-09 04:00:16884 // Don't accept HSTS headers when the hostname is an IP address.
885 if (request_info_.url.HostIsIPAddress())
886 return;
887
estark53fee7c2015-08-03 18:31:51888 // http://tools.ietf.org/html/rfc7469:
[email protected]6ed72be2013-01-08 22:07:33889 //
890 // If a UA receives more than one PKP header field in an HTTP
891 // response message over secure transport, then the UA MUST process
892 // only the first such header field.
[email protected]fecef222012-01-05 02:26:15893 HttpResponseHeaders* headers = GetResponseHeaders();
[email protected]fecef222012-01-05 02:26:15894 std::string value;
estark53fee7c2015-08-03 18:31:51895 if (headers->EnumerateHeader(nullptr, "Public-Key-Pins", &value))
[email protected]6ed72be2013-01-08 22:07:33896 security_state->AddHPKPHeader(request_info_.url.host(), value, ssl_info);
estark53fee7c2015-08-03 18:31:51897 if (headers->EnumerateHeader(nullptr, "Public-Key-Pins-Report-Only",
898 &value)) {
899 security_state->ProcessHPKPReportOnlyHeader(
900 value, HostPortPair::FromURL(request_info_.url), ssl_info);
901 }
[email protected]fecef222012-01-05 02:26:15902}
903
[email protected]5394e422011-01-20 22:07:43904void URLRequestHttpJob::OnStartCompleted(int result) {
[email protected]ec23f522011-02-22 21:01:38905 RecordTimer();
906
[email protected]5394e422011-01-20 22:07:43907 // If the request was destroyed, then there is no more work to do.
[email protected]a83dd332011-07-13 10:41:01908 if (!request_)
[email protected]5394e422011-01-20 22:07:43909 return;
910
[email protected]80abdad2014-03-15 00:20:54911 // If the job is done (due to cancellation), can just ignore this
912 // notification.
913 if (done_)
[email protected]5394e422011-01-20 22:07:43914 return;
915
[email protected]3b23a222013-05-15 21:33:25916 receive_headers_end_ = base::TimeTicks::Now();
917
[email protected]5394e422011-01-20 22:07:43918 // Clear the IO_PENDING status
919 SetStatus(URLRequestStatus());
920
[email protected]ef2bf422012-05-11 03:27:09921 const URLRequestContext* context = request_->context();
922
[email protected]5394e422011-01-20 22:07:43923 if (result == OK) {
[email protected]d8fc4722014-06-13 13:17:15924 if (transaction_ && transaction_->GetResponseInfo()) {
925 SetProxyServer(transaction_->GetResponseInfo()->proxy_server);
926 }
[email protected]ea8141e2011-10-05 13:12:51927 scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders();
[email protected]cc05edc2013-03-08 18:04:41928 if (network_delegate()) {
[email protected]ea8141e2011-10-05 13:12:51929 // Note that |this| may not be deleted until
930 // |on_headers_received_callback_| or
931 // |NetworkDelegate::URLRequestDestroyed()| has been called.
[email protected]abe1c4a2013-10-25 19:28:51932 OnCallToDelegate();
[email protected]5f714132014-03-26 10:41:16933 allowed_unsafe_redirect_url_ = GURL();
[email protected]cc05edc2013-03-08 18:04:41934 int error = network_delegate()->NotifyHeadersReceived(
[email protected]90499482013-06-01 00:39:50935 request_,
936 on_headers_received_callback_,
937 headers.get(),
[email protected]5f714132014-03-26 10:41:16938 &override_response_headers_,
939 &allowed_unsafe_redirect_url_);
ttuttle859dc7a2015-04-23 19:42:29940 if (error != OK) {
941 if (error == ERR_IO_PENDING) {
[email protected]ea8141e2011-10-05 13:12:51942 awaiting_callback_ = true;
[email protected]ea8141e2011-10-05 13:12:51943 } else {
[email protected]55b8a6c12012-06-13 22:03:42944 std::string source("delegate");
[email protected]ea8141e2011-10-05 13:12:51945 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42946 NetLog::StringCallback("source",
947 &source));
[email protected]abe1c4a2013-10-25 19:28:51948 OnCallToDelegateComplete();
[email protected]ea8141e2011-10-05 13:12:51949 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, error));
950 }
951 return;
952 }
953 }
954
ttuttle859dc7a2015-04-23 19:42:29955 SaveCookiesAndNotifyHeadersComplete(OK);
[email protected]e5624f02011-09-27 19:43:53956 } else if (IsCertificateError(result)) {
[email protected]6061c142013-10-21 15:13:34957 // We encountered an SSL certificate error.
958 if (result == ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY ||
959 result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN) {
960 // These are hard failures. They're handled separately and don't have
961 // the correct cert status, so set it here.
962 SSLInfo info(transaction_->GetResponseInfo()->ssl_info);
963 info.cert_status = MapNetErrorToCertStatus(result);
964 NotifySSLCertificateError(info, true);
965 } else {
966 // Maybe overridable, maybe not. Ask the delegate to decide.
[email protected]9e6968d2014-05-07 21:46:26967 TransportSecurityState* state = context->transport_security_state();
968 const bool fatal =
Adam Langley5cbb7d7a2014-09-25 23:14:12969 state && state->ShouldSSLErrorsBeFatal(request_info_.url.host());
[email protected]6061c142013-10-21 15:13:34970 NotifySSLCertificateError(
971 transaction_->GetResponseInfo()->ssl_info, fatal);
972 }
[email protected]5394e422011-01-20 22:07:43973 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]a83dd332011-07-13 10:41:01974 NotifyCertificateRequested(
[email protected]90499482013-06-01 00:39:50975 transaction_->GetResponseInfo()->cert_request_info.get());
[email protected]5394e422011-01-20 22:07:43976 } else {
[email protected]419704c2014-01-14 11:18:06977 // Even on an error, there may be useful information in the response
978 // info (e.g. whether there's a cached copy).
979 if (transaction_.get())
980 response_info_ = transaction_->GetResponseInfo();
[email protected]5394e422011-01-20 22:07:43981 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
982 }
983}
984
[email protected]ea8141e2011-10-05 13:12:51985void URLRequestHttpJob::OnHeadersReceivedCallback(int result) {
[email protected]ea8141e2011-10-05 13:12:51986 awaiting_callback_ = false;
[email protected]9045b8822012-01-13 20:35:35987
988 // Check that there are no callbacks to already canceled requests.
989 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
990
[email protected]ea8141e2011-10-05 13:12:51991 SaveCookiesAndNotifyHeadersComplete(result);
992}
993
[email protected]5394e422011-01-20 22:07:43994void URLRequestHttpJob::OnReadCompleted(int result) {
995 read_in_progress_ = false;
996
[email protected]f001bd6a2011-12-08 04:31:37997 if (ShouldFixMismatchedContentLength(result))
[email protected]5543cbb2012-04-20 16:35:23998 result = OK;
[email protected]f001bd6a2011-12-08 04:31:37999
[email protected]5543cbb2012-04-20 16:35:231000 if (result == OK) {
[email protected]5394e422011-01-20 22:07:431001 NotifyDone(URLRequestStatus());
1002 } else if (result < 0) {
1003 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
1004 } else {
1005 // Clear the IO_PENDING status
1006 SetStatus(URLRequestStatus());
1007 }
1008
1009 NotifyReadComplete(result);
1010}
1011
[email protected]5394e422011-01-20 22:07:431012void URLRequestHttpJob::RestartTransactionWithAuth(
[email protected]f3cf9802011-10-28 18:44:581013 const AuthCredentials& credentials) {
1014 auth_credentials_ = credentials;
[email protected]5394e422011-01-20 22:07:431015
1016 // These will be reset in OnStartCompleted.
1017 response_info_ = NULL;
[email protected]3b23a222013-05-15 21:33:251018 receive_headers_end_ = base::TimeTicks();
[email protected]5394e422011-01-20 22:07:431019 response_cookies_.clear();
1020
[email protected]ec23f522011-02-22 21:01:381021 ResetTimer();
1022
[email protected]5394e422011-01-20 22:07:431023 // Update the cookies, since the cookie store may have been updated from the
1024 // headers in the 401/407. Since cookies were already appended to
1025 // extra_headers, we need to strip them out before adding them again.
[email protected]ea8141e2011-10-05 13:12:511026 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kCookie);
[email protected]5394e422011-01-20 22:07:431027
1028 AddCookieHeaderAndStart();
1029}
1030
[email protected]0736d9e2012-11-28 19:50:401031void URLRequestHttpJob::SetUpload(UploadDataStream* upload) {
[email protected]5394e422011-01-20 22:07:431032 DCHECK(!transaction_.get()) << "cannot change once started";
[email protected]0736d9e2012-11-28 19:50:401033 request_info_.upload_data_stream = upload;
[email protected]5394e422011-01-20 22:07:431034}
1035
1036void URLRequestHttpJob::SetExtraRequestHeaders(
1037 const HttpRequestHeaders& headers) {
1038 DCHECK(!transaction_.get()) << "cannot change once started";
1039 request_info_.extra_headers.CopyFrom(headers);
1040}
1041
[email protected]5394e422011-01-20 22:07:431042LoadState URLRequestHttpJob::GetLoadState() const {
1043 return transaction_.get() ?
1044 transaction_->GetLoadState() : LOAD_STATE_IDLE;
1045}
1046
[email protected]7335ab02012-08-30 22:30:421047UploadProgress URLRequestHttpJob::GetUploadProgress() const {
1048 return transaction_.get() ?
1049 transaction_->GetUploadProgress() : UploadProgress();
[email protected]5394e422011-01-20 22:07:431050}
1051
1052bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const {
1053 DCHECK(transaction_.get());
1054
1055 if (!response_info_)
1056 return false;
1057
ellyjones0e9d5e82015-02-17 23:06:281058 HttpResponseHeaders* headers = GetResponseHeaders();
1059 if (!headers)
1060 return false;
1061 return headers->GetMimeType(mime_type);
[email protected]5394e422011-01-20 22:07:431062}
1063
1064bool URLRequestHttpJob::GetCharset(std::string* charset) {
1065 DCHECK(transaction_.get());
1066
1067 if (!response_info_)
1068 return false;
1069
[email protected]ea8141e2011-10-05 13:12:511070 return GetResponseHeaders()->GetCharset(charset);
[email protected]5394e422011-01-20 22:07:431071}
1072
1073void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) {
1074 DCHECK(request_);
[email protected]5394e422011-01-20 22:07:431075
[email protected]ea8141e2011-10-05 13:12:511076 if (response_info_) {
[email protected]419704c2014-01-14 11:18:061077 DCHECK(transaction_.get());
1078
[email protected]5394e422011-01-20 22:07:431079 *info = *response_info_;
[email protected]90499482013-06-01 00:39:501080 if (override_response_headers_.get())
[email protected]ea8141e2011-10-05 13:12:511081 info->headers = override_response_headers_;
1082 }
[email protected]5394e422011-01-20 22:07:431083}
1084
[email protected]58e32bb2013-01-21 18:23:251085void URLRequestHttpJob::GetLoadTimingInfo(
1086 LoadTimingInfo* load_timing_info) const {
[email protected]3b23a222013-05-15 21:33:251087 // If haven't made it far enough to receive any headers, don't return
rdsmith81f607562014-11-21 18:35:161088 // anything. This makes for more consistent behavior in the case of errors.
[email protected]3b23a222013-05-15 21:33:251089 if (!transaction_ || receive_headers_end_.is_null())
1090 return;
1091 if (transaction_->GetLoadTimingInfo(load_timing_info))
1092 load_timing_info->receive_headers_end = receive_headers_end_;
[email protected]58e32bb2013-01-21 18:23:251093}
1094
ttuttled9dbc652015-09-29 20:00:591095bool URLRequestHttpJob::GetRemoteEndpoint(IPEndPoint* endpoint) const {
1096 if (!transaction_)
1097 return false;
1098
1099 return transaction_->GetRemoteEndpoint(endpoint);
1100}
1101
[email protected]ea8141e2011-10-05 13:12:511102bool URLRequestHttpJob::GetResponseCookies(std::vector<std::string>* cookies) {
[email protected]5394e422011-01-20 22:07:431103 DCHECK(transaction_.get());
1104
1105 if (!response_info_)
1106 return false;
1107
1108 // TODO(darin): Why are we extracting response cookies again? Perhaps we
1109 // should just leverage response_cookies_.
1110
1111 cookies->clear();
[email protected]ea8141e2011-10-05 13:12:511112 FetchResponseCookies(cookies);
[email protected]5394e422011-01-20 22:07:431113 return true;
1114}
1115
1116int URLRequestHttpJob::GetResponseCode() const {
1117 DCHECK(transaction_.get());
1118
1119 if (!response_info_)
1120 return -1;
1121
[email protected]ea8141e2011-10-05 13:12:511122 return GetResponseHeaders()->response_code();
[email protected]5394e422011-01-20 22:07:431123}
1124
[email protected]5a3b4d32011-03-17 01:24:051125Filter* URLRequestHttpJob::SetupFilter() const {
[email protected]5394e422011-01-20 22:07:431126 DCHECK(transaction_.get());
1127 if (!response_info_)
[email protected]5a3b4d32011-03-17 01:24:051128 return NULL;
[email protected]5394e422011-01-20 22:07:431129
[email protected]5a3b4d32011-03-17 01:24:051130 std::vector<Filter::FilterType> encoding_types;
[email protected]5394e422011-01-20 22:07:431131 std::string encoding_type;
[email protected]ea8141e2011-10-05 13:12:511132 HttpResponseHeaders* headers = GetResponseHeaders();
[email protected]5394e422011-01-20 22:07:431133 void* iter = NULL;
[email protected]ea8141e2011-10-05 13:12:511134 while (headers->EnumerateHeader(&iter, "Content-Encoding", &encoding_type)) {
[email protected]5a3b4d32011-03-17 01:24:051135 encoding_types.push_back(Filter::ConvertEncodingToType(encoding_type));
[email protected]5394e422011-01-20 22:07:431136 }
1137
1138 // Even if encoding types are empty, there is a chance that we need to add
1139 // some decoding, as some proxies strip encoding completely. In such cases,
1140 // we may need to add (for example) SDCH filtering (when the context suggests
1141 // it is appropriate).
[email protected]2e92354c2011-03-25 20:49:531142 Filter::FixupEncodingTypes(*filter_context_, &encoding_types);
[email protected]5394e422011-01-20 22:07:431143
[email protected]5a3b4d32011-03-17 01:24:051144 return !encoding_types.empty()
[email protected]2e92354c2011-03-25 20:49:531145 ? Filter::Factory(encoding_types, *filter_context_) : NULL;
[email protected]5394e422011-01-20 22:07:431146}
1147
[email protected]f878230e2014-04-03 15:36:141148bool URLRequestHttpJob::CopyFragmentOnRedirect(const GURL& location) const {
1149 // Allow modification of reference fragments by default, unless
1150 // |allowed_unsafe_redirect_url_| is set and equal to the redirect URL.
1151 // When this is the case, we assume that the network delegate has set the
1152 // desired redirect URL (with or without fragment), so it must not be changed
1153 // any more.
1154 return !allowed_unsafe_redirect_url_.is_valid() ||
1155 allowed_unsafe_redirect_url_ != location;
1156}
1157
[email protected]5394e422011-01-20 22:07:431158bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) {
[email protected]e0f35c92013-05-08 16:04:341159 // HTTP is always safe.
1160 // TODO(pauljensen): Remove once crbug.com/146591 is fixed.
1161 if (location.is_valid() &&
1162 (location.scheme() == "http" || location.scheme() == "https")) {
[email protected]5394e422011-01-20 22:07:431163 return true;
[email protected]5394e422011-01-20 22:07:431164 }
[email protected]f878230e2014-04-03 15:36:141165 // Delegates may mark a URL as safe for redirection.
1166 if (allowed_unsafe_redirect_url_.is_valid() &&
1167 allowed_unsafe_redirect_url_ == location) {
1168 return true;
[email protected]5f714132014-03-26 10:41:161169 }
[email protected]e0f35c92013-05-08 16:04:341170 // Query URLRequestJobFactory as to whether |location| would be safe to
1171 // redirect to.
1172 return request_->context()->job_factory() &&
1173 request_->context()->job_factory()->IsSafeRedirectTarget(location);
[email protected]5394e422011-01-20 22:07:431174}
1175
1176bool URLRequestHttpJob::NeedsAuth() {
1177 int code = GetResponseCode();
1178 if (code == -1)
1179 return false;
1180
rdsmith81f607562014-11-21 18:35:161181 // Check if we need either Proxy or WWW Authentication. This could happen
[email protected]5394e422011-01-20 22:07:431182 // because we either provided no auth info, or provided incorrect info.
1183 switch (code) {
1184 case 407:
1185 if (proxy_auth_state_ == AUTH_STATE_CANCELED)
1186 return false;
1187 proxy_auth_state_ = AUTH_STATE_NEED_AUTH;
1188 return true;
1189 case 401:
1190 if (server_auth_state_ == AUTH_STATE_CANCELED)
1191 return false;
1192 server_auth_state_ = AUTH_STATE_NEED_AUTH;
1193 return true;
1194 }
1195 return false;
1196}
1197
1198void URLRequestHttpJob::GetAuthChallengeInfo(
1199 scoped_refptr<AuthChallengeInfo>* result) {
1200 DCHECK(transaction_.get());
1201 DCHECK(response_info_);
1202
1203 // sanity checks:
1204 DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH ||
1205 server_auth_state_ == AUTH_STATE_NEED_AUTH);
[email protected]9094b602012-02-27 21:44:581206 DCHECK((GetResponseHeaders()->response_code() == HTTP_UNAUTHORIZED) ||
1207 (GetResponseHeaders()->response_code() ==
1208 HTTP_PROXY_AUTHENTICATION_REQUIRED));
[email protected]5394e422011-01-20 22:07:431209
1210 *result = response_info_->auth_challenge;
1211}
1212
[email protected]f3cf9802011-10-28 18:44:581213void URLRequestHttpJob::SetAuth(const AuthCredentials& credentials) {
[email protected]5394e422011-01-20 22:07:431214 DCHECK(transaction_.get());
1215
1216 // Proxy gets set first, then WWW.
1217 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1218 proxy_auth_state_ = AUTH_STATE_HAVE_AUTH;
1219 } else {
[email protected]dd29bcd72011-03-24 00:03:441220 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
[email protected]5394e422011-01-20 22:07:431221 server_auth_state_ = AUTH_STATE_HAVE_AUTH;
1222 }
1223
[email protected]f3cf9802011-10-28 18:44:581224 RestartTransactionWithAuth(credentials);
[email protected]5394e422011-01-20 22:07:431225}
1226
1227void URLRequestHttpJob::CancelAuth() {
1228 // Proxy gets set first, then WWW.
1229 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1230 proxy_auth_state_ = AUTH_STATE_CANCELED;
1231 } else {
[email protected]dd29bcd72011-03-24 00:03:441232 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
[email protected]5394e422011-01-20 22:07:431233 server_auth_state_ = AUTH_STATE_CANCELED;
1234 }
1235
1236 // These will be reset in OnStartCompleted.
1237 response_info_ = NULL;
[email protected]3b23a222013-05-15 21:33:251238 receive_headers_end_ = base::TimeTicks::Now();
[email protected]5394e422011-01-20 22:07:431239 response_cookies_.clear();
1240
[email protected]ec23f522011-02-22 21:01:381241 ResetTimer();
1242
[email protected]5394e422011-01-20 22:07:431243 // OK, let the consumer read the error page...
1244 //
1245 // Because we set the AUTH_STATE_CANCELED flag, NeedsAuth will return false,
1246 // which will cause the consumer to receive OnResponseStarted instead of
1247 // OnAuthRequired.
1248 //
1249 // We have to do this via InvokeLater to avoid "recursing" the consumer.
1250 //
skyostil4891b25b2015-06-11 11:43:451251 base::ThreadTaskRunnerHandle::Get()->PostTask(
1252 FROM_HERE, base::Bind(&URLRequestHttpJob::OnStartCompleted,
1253 weak_factory_.GetWeakPtr(), OK));
[email protected]5394e422011-01-20 22:07:431254}
1255
1256void URLRequestHttpJob::ContinueWithCertificate(
1257 X509Certificate* client_cert) {
1258 DCHECK(transaction_.get());
1259
1260 DCHECK(!response_info_) << "should not have a response yet";
[email protected]3b23a222013-05-15 21:33:251261 receive_headers_end_ = base::TimeTicks();
[email protected]5394e422011-01-20 22:07:431262
[email protected]ec23f522011-02-22 21:01:381263 ResetTimer();
1264
[email protected]5394e422011-01-20 22:07:431265 // No matter what, we want to report our status as IO pending since we will
1266 // be notifying our consumer asynchronously via OnStartCompleted.
1267 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1268
[email protected]49639fa2011-12-20 23:22:411269 int rv = transaction_->RestartWithCertificate(client_cert, start_callback_);
[email protected]5394e422011-01-20 22:07:431270 if (rv == ERR_IO_PENDING)
1271 return;
1272
1273 // The transaction started synchronously, but we need to notify the
1274 // URLRequest delegate via the message loop.
skyostil4891b25b2015-06-11 11:43:451275 base::ThreadTaskRunnerHandle::Get()->PostTask(
1276 FROM_HERE, base::Bind(&URLRequestHttpJob::OnStartCompleted,
1277 weak_factory_.GetWeakPtr(), rv));
[email protected]5394e422011-01-20 22:07:431278}
1279
1280void URLRequestHttpJob::ContinueDespiteLastError() {
1281 // If the transaction was destroyed, then the job was cancelled.
1282 if (!transaction_.get())
1283 return;
1284
1285 DCHECK(!response_info_) << "should not have a response yet";
[email protected]3b23a222013-05-15 21:33:251286 receive_headers_end_ = base::TimeTicks();
[email protected]5394e422011-01-20 22:07:431287
[email protected]ec23f522011-02-22 21:01:381288 ResetTimer();
1289
[email protected]5394e422011-01-20 22:07:431290 // No matter what, we want to report our status as IO pending since we will
1291 // be notifying our consumer asynchronously via OnStartCompleted.
1292 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1293
[email protected]49639fa2011-12-20 23:22:411294 int rv = transaction_->RestartIgnoringLastError(start_callback_);
[email protected]5394e422011-01-20 22:07:431295 if (rv == ERR_IO_PENDING)
1296 return;
1297
1298 // The transaction started synchronously, but we need to notify the
1299 // URLRequest delegate via the message loop.
skyostil4891b25b2015-06-11 11:43:451300 base::ThreadTaskRunnerHandle::Get()->PostTask(
1301 FROM_HERE, base::Bind(&URLRequestHttpJob::OnStartCompleted,
1302 weak_factory_.GetWeakPtr(), rv));
[email protected]5394e422011-01-20 22:07:431303}
1304
[email protected]1826a402014-01-08 15:40:481305void URLRequestHttpJob::ResumeNetworkStart() {
1306 DCHECK(transaction_.get());
1307 transaction_->ResumeNetworkStart();
1308}
1309
[email protected]f001bd6a2011-12-08 04:31:371310bool URLRequestHttpJob::ShouldFixMismatchedContentLength(int rv) const {
1311 // Some servers send the body compressed, but specify the content length as
rdsmith81f607562014-11-21 18:35:161312 // the uncompressed size. Although this violates the HTTP spec we want to
[email protected]f001bd6a2011-12-08 04:31:371313 // support it (as IE and FireFox do), but *only* for an exact match.
1314 // See http://crbug.com/79694.
ttuttle859dc7a2015-04-23 19:42:291315 if (rv == ERR_CONTENT_LENGTH_MISMATCH ||
1316 rv == ERR_INCOMPLETE_CHUNKED_ENCODING) {
[email protected]f001bd6a2011-12-08 04:31:371317 if (request_ && request_->response_headers()) {
1318 int64 expected_length = request_->response_headers()->GetContentLength();
1319 VLOG(1) << __FUNCTION__ << "() "
1320 << "\"" << request_->url().spec() << "\""
1321 << " content-length = " << expected_length
1322 << " pre total = " << prefilter_bytes_read()
1323 << " post total = " << postfilter_bytes_read();
1324 if (postfilter_bytes_read() == expected_length) {
1325 // Clear the error.
1326 return true;
1327 }
1328 }
1329 }
1330 return false;
1331}
1332
[email protected]5394e422011-01-20 22:07:431333bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size,
[email protected]b7996452011-10-31 19:30:561334 int* bytes_read) {
[email protected]5394e422011-01-20 22:07:431335 DCHECK_NE(buf_size, 0);
1336 DCHECK(bytes_read);
1337 DCHECK(!read_in_progress_);
1338
[email protected]49639fa2011-12-20 23:22:411339 int rv = transaction_->Read(
1340 buf, buf_size,
1341 base::Bind(&URLRequestHttpJob::OnReadCompleted, base::Unretained(this)));
[email protected]85c1dce2011-07-06 12:01:291342
[email protected]f001bd6a2011-12-08 04:31:371343 if (ShouldFixMismatchedContentLength(rv))
1344 rv = 0;
1345
[email protected]5394e422011-01-20 22:07:431346 if (rv >= 0) {
1347 *bytes_read = rv;
pkasting941842c2015-04-11 01:51:301348 if (!rv)
[email protected]bbaea8f2011-06-24 00:11:011349 DoneWithRequest(FINISHED);
[email protected]5394e422011-01-20 22:07:431350 return true;
1351 }
1352
1353 if (rv == ERR_IO_PENDING) {
1354 read_in_progress_ = true;
1355 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1356 } else {
1357 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
1358 }
1359
1360 return false;
1361}
1362
1363void URLRequestHttpJob::StopCaching() {
1364 if (transaction_.get())
1365 transaction_->StopCaching();
1366}
1367
[email protected]79e1fd62013-06-20 06:50:041368bool URLRequestHttpJob::GetFullRequestHeaders(
1369 HttpRequestHeaders* headers) const {
1370 if (!transaction_)
1371 return false;
1372
1373 return transaction_->GetFullRequestHeaders(headers);
1374}
1375
[email protected]71e5ff8f2014-01-13 09:44:041376int64 URLRequestHttpJob::GetTotalReceivedBytes() const {
sclittlece72c482015-08-24 20:20:591377 int64_t total_received_bytes =
1378 total_received_bytes_from_previous_transactions_;
1379 if (transaction_)
1380 total_received_bytes += transaction_->GetTotalReceivedBytes();
1381 return total_received_bytes;
[email protected]71e5ff8f2014-01-13 09:44:041382}
1383
sclittlefb249892015-09-10 21:33:221384int64_t URLRequestHttpJob::GetTotalSentBytes() const {
1385 int64_t total_sent_bytes = total_sent_bytes_from_previous_transactions_;
1386 if (transaction_)
1387 total_sent_bytes += transaction_->GetTotalSentBytes();
1388 return total_sent_bytes;
1389}
1390
[email protected]5c04f722011-08-12 17:52:471391void URLRequestHttpJob::DoneReading() {
[email protected]e50efea2014-03-24 18:41:001392 if (transaction_) {
[email protected]5c04f722011-08-12 17:52:471393 transaction_->DoneReading();
[email protected]e50efea2014-03-24 18:41:001394 }
1395 DoneWithRequest(FINISHED);
1396}
1397
1398void URLRequestHttpJob::DoneReadingRedirectResponse() {
1399 if (transaction_) {
1400 if (transaction_->GetResponseInfo()->headers->IsRedirect(NULL)) {
1401 // If the original headers indicate a redirect, go ahead and cache the
1402 // response, even if the |override_response_headers_| are a redirect to
1403 // another location.
1404 transaction_->DoneReading();
1405 } else {
1406 // Otherwise, |override_response_headers_| must be non-NULL and contain
1407 // bogus headers indicating a redirect.
dchengc2e01e82014-08-27 00:24:421408 DCHECK(override_response_headers_.get());
[email protected]e50efea2014-03-24 18:41:001409 DCHECK(override_response_headers_->IsRedirect(NULL));
1410 transaction_->StopCaching();
1411 }
1412 }
[email protected]5c04f722011-08-12 17:52:471413 DoneWithRequest(FINISHED);
1414}
1415
[email protected]6d81b482011-02-22 19:47:191416HostPortPair URLRequestHttpJob::GetSocketAddress() const {
1417 return response_info_ ? response_info_->socket_address : HostPortPair();
1418}
1419
[email protected]ec23f522011-02-22 21:01:381420void URLRequestHttpJob::RecordTimer() {
1421 if (request_creation_time_.is_null()) {
1422 NOTREACHED()
1423 << "The same transaction shouldn't start twice without new timing.";
1424 return;
1425 }
1426
[email protected]320a29f12011-03-21 14:47:411427 base::TimeDelta to_start = base::Time::Now() - request_creation_time_;
[email protected]ec23f522011-02-22 21:01:381428 request_creation_time_ = base::Time();
[email protected]8684a8812011-03-22 13:59:381429
[email protected]5c68d692011-08-24 04:59:411430 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start);
[email protected]ec23f522011-02-22 21:01:381431}
1432
1433void URLRequestHttpJob::ResetTimer() {
1434 if (!request_creation_time_.is_null()) {
1435 NOTREACHED()
1436 << "The timer was reset before it was recorded.";
1437 return;
1438 }
1439 request_creation_time_ = base::Time::Now();
1440}
1441
[email protected]dd29bcd72011-03-24 00:03:441442void URLRequestHttpJob::UpdatePacketReadTimes() {
1443 if (!packet_timing_enabled_)
1444 return;
1445
mmenkebbf19be22015-04-11 02:44:481446 DCHECK_GT(prefilter_bytes_read(), bytes_observed_in_packets_);
[email protected]dd29bcd72011-03-24 00:03:441447
[email protected]006ce1a2014-07-30 14:41:231448 base::Time now(base::Time::Now());
[email protected]dd29bcd72011-03-24 00:03:441449 if (!bytes_observed_in_packets_)
[email protected]006ce1a2014-07-30 14:41:231450 request_time_snapshot_ = now;
1451 final_packet_time_ = now;
[email protected]dd29bcd72011-03-24 00:03:441452
mmenkebbf19be22015-04-11 02:44:481453 bytes_observed_in_packets_ = prefilter_bytes_read();
[email protected]dd29bcd72011-03-24 00:03:441454}
1455
1456void URLRequestHttpJob::RecordPacketStats(
1457 FilterContext::StatisticSelector statistic) const {
1458 if (!packet_timing_enabled_ || (final_packet_time_ == base::Time()))
1459 return;
1460
1461 base::TimeDelta duration = final_packet_time_ - request_time_snapshot_;
1462 switch (statistic) {
1463 case FilterContext::SDCH_DECODE: {
[email protected]dd29bcd72011-03-24 00:03:441464 UMA_HISTOGRAM_CUSTOM_COUNTS("Sdch3.Network_Decode_Bytes_Processed_b",
1465 static_cast<int>(bytes_observed_in_packets_), 500, 100000, 100);
[email protected]dd29bcd72011-03-24 00:03:441466 return;
1467 }
1468 case FilterContext::SDCH_PASSTHROUGH: {
1469 // Despite advertising a dictionary, we handled non-sdch compressed
1470 // content.
[email protected]dd29bcd72011-03-24 00:03:441471 return;
1472 }
1473
1474 case FilterContext::SDCH_EXPERIMENT_DECODE: {
[email protected]006ce1a2014-07-30 14:41:231475 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment3_Decode",
[email protected]dd29bcd72011-03-24 00:03:441476 duration,
1477 base::TimeDelta::FromMilliseconds(20),
1478 base::TimeDelta::FromMinutes(10), 100);
[email protected]dd29bcd72011-03-24 00:03:441479 return;
1480 }
1481 case FilterContext::SDCH_EXPERIMENT_HOLDBACK: {
[email protected]006ce1a2014-07-30 14:41:231482 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment3_Holdback",
[email protected]dd29bcd72011-03-24 00:03:441483 duration,
1484 base::TimeDelta::FromMilliseconds(20),
1485 base::TimeDelta::FromMinutes(10), 100);
[email protected]dd29bcd72011-03-24 00:03:441486 return;
1487 }
1488 default:
1489 NOTREACHED();
1490 return;
1491 }
1492}
1493
[email protected]bbaea8f2011-06-24 00:11:011494void URLRequestHttpJob::RecordPerfHistograms(CompletionCause reason) {
1495 if (start_time_.is_null())
1496 return;
1497
1498 base::TimeDelta total_time = base::TimeTicks::Now() - start_time_;
1499 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTime", total_time);
1500
1501 if (reason == FINISHED) {
1502 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeSuccess", total_time);
1503 } else {
1504 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCancel", total_time);
1505 }
1506
[email protected]4b4d20242012-02-23 18:27:461507 if (response_info_) {
rtennetie55c4b72015-08-03 21:48:351508 // QUIC (by default) supports https scheme only, thus track https URLs only
1509 // for QUIC.
1510 bool is_https_google = request() && request()->url().SchemeIs("https") &&
1511 HasGoogleHost(request()->url());
rtenneti72928bb2015-04-28 18:28:131512 bool used_quic = response_info_->DidUseQuic();
rtennetie55c4b72015-08-03 21:48:351513 if (is_https_google) {
rtenneti72928bb2015-04-28 18:28:131514 if (used_quic) {
rtennetie55c4b72015-08-03 21:48:351515 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpJob.TotalTime.Secure.Quic",
1516 total_time);
rtenneti72928bb2015-04-28 18:28:131517 } else {
rtennetie55c4b72015-08-03 21:48:351518 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpJob.TotalTime.Secure.NotQuic",
1519 total_time);
rtenneti72928bb2015-04-28 18:28:131520 }
1521 }
[email protected]4b4d20242012-02-23 18:27:461522 if (response_info_->was_cached) {
1523 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCached", total_time);
rtennetie55c4b72015-08-03 21:48:351524 if (is_https_google) {
rtenneti72928bb2015-04-28 18:28:131525 if (used_quic) {
rtennetie55c4b72015-08-03 21:48:351526 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpJob.TotalTimeCached.Secure.Quic",
rtenneti72928bb2015-04-28 18:28:131527 total_time);
1528 } else {
rtennetie55c4b72015-08-03 21:48:351529 UMA_HISTOGRAM_MEDIUM_TIMES(
1530 "Net.HttpJob.TotalTimeCached.Secure.NotQuic", total_time);
rtenneti72928bb2015-04-28 18:28:131531 }
1532 }
[email protected]4b4d20242012-02-23 18:27:461533 } else {
1534 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeNotCached", total_time);
rtennetie55c4b72015-08-03 21:48:351535 if (is_https_google) {
rtenneti72928bb2015-04-28 18:28:131536 if (used_quic) {
rtennetie55c4b72015-08-03 21:48:351537 UMA_HISTOGRAM_MEDIUM_TIMES(
1538 "Net.HttpJob.TotalTimeNotCached.Secure.Quic", total_time);
rtenneti72928bb2015-04-28 18:28:131539 } else {
rtennetie55c4b72015-08-03 21:48:351540 UMA_HISTOGRAM_MEDIUM_TIMES(
1541 "Net.HttpJob.TotalTimeNotCached.Secure.NotQuic", total_time);
rtenneti72928bb2015-04-28 18:28:131542 }
1543 }
[email protected]b73656ca2011-07-22 17:42:171544 }
[email protected]bbaea8f2011-06-24 00:11:011545 }
1546
[email protected]3ed84722013-11-01 17:17:071547 if (request_info_.load_flags & LOAD_PREFETCH && !request_->was_cached())
1548 UMA_HISTOGRAM_COUNTS("Net.Prefetch.PrefilterBytesReadFromNetwork",
1549 prefilter_bytes_read());
1550
[email protected]bbaea8f2011-06-24 00:11:011551 start_time_ = base::TimeTicks();
1552}
1553
1554void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) {
1555 if (done_)
1556 return;
1557 done_ = true;
tbansal79ed5cd2015-08-10 18:53:561558
1559 // Notify NetworkQualityEstimator.
1560 if (request() && (reason == FINISHED || reason == ABORTED)) {
1561 NetworkQualityEstimator* network_quality_estimator =
1562 request()->context()->network_quality_estimator();
1563 if (network_quality_estimator)
1564 network_quality_estimator->NotifyRequestCompleted(*request());
1565 }
1566
[email protected]bbaea8f2011-06-24 00:11:011567 RecordPerfHistograms(reason);
kundajib8c0d102015-07-27 21:47:411568 if (request_)
[email protected]7a299a92012-10-24 23:54:501569 request_->set_received_response_content_length(prefilter_bytes_read());
[email protected]bbaea8f2011-06-24 00:11:011570}
1571
[email protected]ea8141e2011-10-05 13:12:511572HttpResponseHeaders* URLRequestHttpJob::GetResponseHeaders() const {
1573 DCHECK(transaction_.get());
1574 DCHECK(transaction_->GetResponseInfo());
1575 return override_response_headers_.get() ?
[email protected]90499482013-06-01 00:39:501576 override_response_headers_.get() :
1577 transaction_->GetResponseInfo()->headers.get();
[email protected]ea8141e2011-10-05 13:12:511578}
1579
1580void URLRequestHttpJob::NotifyURLRequestDestroyed() {
1581 awaiting_callback_ = false;
1582}
1583
[email protected]4f5656c62010-12-13 10:47:091584} // namespace net