blob: 044e5f0298ba82bb7488cfea237ece51d5fbe86e [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"
[email protected]7f86564d2013-07-18 00:41:2213#include "base/message_loop/message_loop.h"
[email protected]8684a8812011-03-22 13:59:3814#include "base/metrics/field_trial.h"
[email protected]ec23f522011-02-22 21:01:3815#include "base/metrics/histogram.h"
[email protected]5b90b5d2009-04-30 23:06:0116#include "base/rand_util.h"
[email protected]4dc3ad4f2013-06-11 07:15:5017#include "base/strings/string_util.h"
[email protected]f002abb2013-06-28 02:30:2118#include "base/time/time.h"
[email protected]6d81b482011-02-22 19:47:1919#include "net/base/host_port_pair.h"
[email protected]b8430722008-09-17 20:05:4420#include "net/base/load_flags.h"
[email protected]dd29bcd72011-03-24 00:03:4421#include "net/base/mime_util.h"
initial.commit586acc5fe2008-07-26 22:42:5222#include "net/base/net_errors.h"
23#include "net/base/net_util.h"
[email protected]636eccd2011-06-28 12:28:0124#include "net/base/network_delegate.h"
[email protected]60889422008-09-23 01:18:1625#include "net/base/sdch_manager.h"
[email protected]6e7845ae2013-03-29 21:48:1126#include "net/cert/cert_status_flags.h"
[email protected]aa84a7e2012-03-15 21:29:0627#include "net/cookies/cookie_monster.h"
[email protected]7a299a92012-10-24 23:54:5028#include "net/http/http_network_session.h"
[email protected]8c76ae22010-04-20 22:15:4329#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2130#include "net/http/http_response_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5231#include "net/http/http_response_info.h"
[email protected]9094b602012-02-27 21:44:5832#include "net/http/http_status_code.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/http/http_transaction.h"
34#include "net/http/http_transaction_factory.h"
[email protected]0757e7702009-03-27 04:00:2235#include "net/http/http_util.h"
[email protected]536fd0b2013-03-14 17:41:5736#include "net/ssl/ssl_cert_request_info.h"
37#include "net/ssl/ssl_config_service.h"
[email protected]7f269182011-10-16 01:58:3438#include "net/url_request/fraudulent_certificate_reporter.h"
[email protected]ee4c30d2012-11-07 15:08:4339#include "net/url_request/http_user_agent_settings.h"
initial.commit586acc5fe2008-07-26 22:42:5240#include "net/url_request/url_request.h"
[email protected]319d9e6f2009-02-18 19:47:2141#include "net/url_request/url_request_context.h"
initial.commit586acc5fe2008-07-26 22:42:5242#include "net/url_request/url_request_error_job.h"
[email protected]e0f35c92013-05-08 16:04:3443#include "net/url_request/url_request_job_factory.h"
[email protected]06965e02009-09-04 21:36:4244#include "net/url_request/url_request_redirect_job.h"
[email protected]6b3f9642010-11-25 02:29:0645#include "net/url_request/url_request_throttler_header_adapter.h"
46#include "net/url_request/url_request_throttler_manager.h"
[email protected]f4533ba2013-11-28 09:35:4147#include "net/websockets/websocket_handshake_stream_base.h"
initial.commit586acc5fe2008-07-26 22:42:5248
[email protected]8c76ae22010-04-20 22:15:4349static const char kAvailDictionaryHeader[] = "Avail-Dictionary";
50
[email protected]4f5656c62010-12-13 10:47:0951namespace net {
52
[email protected]2e92354c2011-03-25 20:49:5353class URLRequestHttpJob::HttpFilterContext : public FilterContext {
54 public:
55 explicit HttpFilterContext(URLRequestHttpJob* job);
56 virtual ~HttpFilterContext();
57
58 // FilterContext implementation.
[email protected]1e43b6812012-08-19 19:14:0059 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
60 virtual bool GetURL(GURL* gurl) const OVERRIDE;
61 virtual base::Time GetRequestTime() const OVERRIDE;
62 virtual bool IsCachedContent() const OVERRIDE;
63 virtual bool IsDownload() const OVERRIDE;
64 virtual bool IsSdchResponse() const OVERRIDE;
65 virtual int64 GetByteReadCount() const OVERRIDE;
66 virtual int GetResponseCode() const OVERRIDE;
67 virtual void RecordPacketStats(StatisticSelector statistic) const OVERRIDE;
[email protected]2e92354c2011-03-25 20:49:5368
[email protected]46668fe52011-05-04 19:03:2369 // Method to allow us to reset filter context for a response that should have
70 // been SDCH encoded when there is an update due to an explicit HTTP header.
71 void ResetSdchResponseToFalse();
72
[email protected]2e92354c2011-03-25 20:49:5373 private:
74 URLRequestHttpJob* job_;
75
76 DISALLOW_COPY_AND_ASSIGN(HttpFilterContext);
77};
78
[email protected]fc01f232011-03-17 19:06:0179URLRequestHttpJob::HttpFilterContext::HttpFilterContext(URLRequestHttpJob* job)
80 : job_(job) {
81 DCHECK(job_);
82}
83
84URLRequestHttpJob::HttpFilterContext::~HttpFilterContext() {
85}
86
87bool URLRequestHttpJob::HttpFilterContext::GetMimeType(
88 std::string* mime_type) const {
89 return job_->GetMimeType(mime_type);
90}
91
92bool URLRequestHttpJob::HttpFilterContext::GetURL(GURL* gurl) const {
93 if (!job_->request())
94 return false;
95 *gurl = job_->request()->url();
96 return true;
97}
98
99base::Time URLRequestHttpJob::HttpFilterContext::GetRequestTime() const {
100 return job_->request() ? job_->request()->request_time() : base::Time();
101}
102
103bool URLRequestHttpJob::HttpFilterContext::IsCachedContent() const {
[email protected]dd29bcd72011-03-24 00:03:44104 return job_->is_cached_content_;
[email protected]fc01f232011-03-17 19:06:01105}
106
107bool URLRequestHttpJob::HttpFilterContext::IsDownload() const {
108 return (job_->request_info_.load_flags & LOAD_IS_DOWNLOAD) != 0;
109}
110
[email protected]46668fe52011-05-04 19:03:23111void URLRequestHttpJob::HttpFilterContext::ResetSdchResponseToFalse() {
112 DCHECK(job_->sdch_dictionary_advertised_);
113 job_->sdch_dictionary_advertised_ = false;
114}
115
[email protected]fc01f232011-03-17 19:06:01116bool URLRequestHttpJob::HttpFilterContext::IsSdchResponse() const {
117 return job_->sdch_dictionary_advertised_;
118}
119
120int64 URLRequestHttpJob::HttpFilterContext::GetByteReadCount() const {
121 return job_->filter_input_byte_count();
122}
123
124int URLRequestHttpJob::HttpFilterContext::GetResponseCode() const {
125 return job_->GetResponseCode();
126}
127
128void URLRequestHttpJob::HttpFilterContext::RecordPacketStats(
129 StatisticSelector statistic) const {
130 job_->RecordPacketStats(statistic);
131}
132
initial.commit586acc5fe2008-07-26 22:42:52133// TODO(darin): make sure the port blocking code is not lost
initial.commit586acc5fe2008-07-26 22:42:52134// static
[email protected]4f5656c62010-12-13 10:47:09135URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request,
[email protected]9f170462012-08-24 01:06:58136 NetworkDelegate* network_delegate,
[email protected]4f5656c62010-12-13 10:47:09137 const std::string& scheme) {
[email protected]f4533ba2013-11-28 09:35:41138 DCHECK(scheme == "http" || scheme == "https" || scheme == "ws" ||
139 scheme == "wss");
initial.commit586acc5fe2008-07-26 22:42:52140
[email protected]81293f482012-08-13 19:35:45141 if (!request->context()->http_transaction_factory()) {
initial.commit586acc5fe2008-07-26 22:42:52142 NOTREACHED() << "requires a valid context";
[email protected]9f170462012-08-24 01:06:58143 return new URLRequestErrorJob(
144 request, network_delegate, ERR_INVALID_ARGUMENT);
initial.commit586acc5fe2008-07-26 22:42:52145 }
146
[email protected]ce572df2012-05-04 19:47:17147 GURL redirect_url;
[email protected]8ccc69f2012-11-28 19:52:14148 if (request->GetHSTSRedirect(&redirect_url)) {
149 return new URLRequestRedirectJob(
150 request, network_delegate, redirect_url,
151 // Use status code 307 to preserve the method, so POST requests work.
[email protected]7983c4a2014-03-12 01:47:09152 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "HSTS");
[email protected]8ccc69f2012-11-28 19:52:14153 }
[email protected]ee4c30d2012-11-07 15:08:43154 return new URLRequestHttpJob(request,
155 network_delegate,
156 request->context()->http_user_agent_settings());
initial.commit586acc5fe2008-07-26 22:42:52157}
158
[email protected]ee4c30d2012-11-07 15:08:43159URLRequestHttpJob::URLRequestHttpJob(
160 URLRequest* request,
161 NetworkDelegate* network_delegate,
162 const HttpUserAgentSettings* http_user_agent_settings)
[email protected]9f170462012-08-24 01:06:58163 : URLRequestJob(request, network_delegate),
[email protected]5033ab82013-03-22 20:17:46164 priority_(DEFAULT_PRIORITY),
initial.commit586acc5fe2008-07-26 22:42:52165 response_info_(NULL),
[email protected]34602282010-02-03 22:14:15166 response_cookies_save_index_(0),
[email protected]4f5656c62010-12-13 10:47:09167 proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
168 server_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
[email protected]dd946bb2013-06-12 22:53:01169 start_callback_(base::Bind(&URLRequestHttpJob::OnStartCompleted,
170 base::Unretained(this))),
171 notify_before_headers_sent_callback_(
172 base::Bind(&URLRequestHttpJob::NotifyBeforeSendHeadersCallback,
173 base::Unretained(this))),
[email protected]3589e552008-08-20 23:11:34174 read_in_progress_(false),
[email protected]a73a2802012-05-02 19:20:15175 throttling_entry_(NULL),
[email protected]5b90b5d2009-04-30 23:06:01176 sdch_dictionary_advertised_(false),
177 sdch_test_activated_(false),
[email protected]d8fd5132009-05-15 01:06:53178 sdch_test_control_(false),
[email protected]00e48bf2010-12-03 06:15:42179 is_cached_content_(false),
[email protected]ec23f522011-02-22 21:01:38180 request_creation_time_(),
[email protected]dd29bcd72011-03-24 00:03:44181 packet_timing_enabled_(false),
[email protected]bbaea8f2011-06-24 00:11:01182 done_(false),
[email protected]dd29bcd72011-03-24 00:03:44183 bytes_observed_in_packets_(0),
[email protected]dd29bcd72011-03-24 00:03:44184 request_time_snapshot_(),
185 final_packet_time_(),
[email protected]aa249b52013-04-30 01:04:32186 filter_context_(new HttpFilterContext(this)),
187 weak_factory_(this),
[email protected]dd946bb2013-06-12 22:53:01188 on_headers_received_callback_(
189 base::Bind(&URLRequestHttpJob::OnHeadersReceivedCallback,
190 base::Unretained(this))),
[email protected]5a07c192012-07-30 20:18:22191 awaiting_callback_(false),
[email protected]ee4c30d2012-11-07 15:08:43192 http_user_agent_settings_(http_user_agent_settings) {
[email protected]a73a2802012-05-02 19:20:15193 URLRequestThrottlerManager* manager = request->context()->throttler_manager();
194 if (manager)
195 throttling_entry_ = manager->RegisterRequestUrl(request->url());
196
[email protected]ec23f522011-02-22 21:01:38197 ResetTimer();
initial.commit586acc5fe2008-07-26 22:42:52198}
199
[email protected]5033ab82013-03-22 20:17:46200URLRequestHttpJob::~URLRequestHttpJob() {
201 CHECK(!awaiting_callback_);
202
203 DCHECK(!sdch_test_control_ || !sdch_test_activated_);
204 if (!is_cached_content_) {
205 if (sdch_test_control_)
206 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_HOLDBACK);
207 if (sdch_test_activated_)
208 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_DECODE);
209 }
210 // Make sure SDCH filters are told to emit histogram data while
211 // filter_context_ is still alive.
212 DestroyFilters();
213
214 if (sdch_dictionary_url_.is_valid()) {
215 // Prior to reaching the destructor, request_ has been set to a NULL
216 // pointer, so request_->url() is no longer valid in the destructor, and we
217 // use an alternate copy |request_info_.url|.
218 SdchManager* manager = SdchManager::Global();
219 // To be extra safe, since this is a "different time" from when we decided
220 // to get the dictionary, we'll validate that an SdchManager is available.
221 // At shutdown time, care is taken to be sure that we don't delete this
222 // globally useful instance "too soon," so this check is just defensive
223 // coding to assure that IF the system is shutting down, we don't have any
224 // problem if the manager was deleted ahead of time.
225 if (manager) // Defensive programming.
226 manager->FetchDictionary(request_info_.url, sdch_dictionary_url_);
227 }
228 DoneWithRequest(ABORTED);
229}
230
231void URLRequestHttpJob::SetPriority(RequestPriority priority) {
232 priority_ = priority;
233 if (transaction_)
234 transaction_->SetPriority(priority_);
235}
236
237void URLRequestHttpJob::Start() {
238 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 ?
256 kPrivacyModeEnabled : kPrivacyModeDisabled;
[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
263 // Our consumer should have made sure that this is a safe referrer. See for
264 // 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
[email protected]175adac2008-07-30 17:28:04288void URLRequestHttpJob::NotifyHeadersComplete() {
initial.commit586acc5fe2008-07-26 22:42:52289 DCHECK(!response_info_);
290
291 response_info_ = transaction_->GetResponseInfo();
292
[email protected]d8fd5132009-05-15 01:06:53293 // Save boolean, as we'll need this info at destruction time, and filters may
294 // also need this info.
295 is_cached_content_ = response_info_->was_cached;
296
[email protected]90499482013-06-01 00:39:50297 if (!is_cached_content_ && throttling_entry_.get()) {
[email protected]ea8141e2011-10-05 13:12:51298 URLRequestThrottlerHeaderAdapter response_adapter(GetResponseHeaders());
[email protected]2fd33ee92011-03-25 22:30:21299 throttling_entry_->UpdateWithResponse(request_info_.url.host(),
300 &response_adapter);
[email protected]6b3f9642010-11-25 02:29:06301 }
302
[email protected]fecef222012-01-05 02:26:15303 // The ordering of these calls is not important.
[email protected]77f6fb432009-09-05 14:21:09304 ProcessStrictTransportSecurityHeader();
[email protected]fecef222012-01-05 02:26:15305 ProcessPublicKeyPinsHeader();
[email protected]a9cea7542009-05-20 04:30:23306
[email protected]fe219872008-09-23 02:17:00307 if (SdchManager::Global() &&
308 SdchManager::Global()->IsInSupportedDomain(request_->url())) {
[email protected]264300242011-11-07 06:03:30309 const std::string name = "Get-Dictionary";
[email protected]60889422008-09-23 01:18:16310 std::string url_text;
311 void* iter = NULL;
312 // TODO(jar): We need to not fetch dictionaries the first time they are
313 // seen, but rather wait until we can justify their usefulness.
314 // For now, we will only fetch the first dictionary, which will at least
315 // require multiple suggestions before we get additional ones for this site.
316 // Eventually we should wait until a dictionary is requested several times
317 // before we even download it (so that we don't waste memory or bandwidth).
[email protected]ea8141e2011-10-05 13:12:51318 if (GetResponseHeaders()->EnumerateHeader(&iter, name, &url_text)) {
[email protected]d55ad15d2009-02-17 19:40:50319 // request_->url() won't be valid in the destructor, so we use an
320 // alternate copy.
[email protected]dd29bcd72011-03-24 00:03:44321 DCHECK_EQ(request_->url(), request_info_.url);
[email protected]d55ad15d2009-02-17 19:40:50322 // Resolve suggested URL relative to request url.
323 sdch_dictionary_url_ = request_info_.url.Resolve(url_text);
[email protected]60889422008-09-23 01:18:16324 }
325 }
326
[email protected]0757e7702009-03-27 04:00:22327 // The HTTP transaction may be restarted several times for the purposes
328 // of sending authorization information. Each time it restarts, we get
329 // notified of the headers completion so that we can update the cookie store.
330 if (transaction_->IsReadyToRestartForAuth()) {
331 DCHECK(!response_info_->auth_challenge.get());
[email protected]87a09a92011-07-14 15:50:50332 // TODO(battre): This breaks the webrequest API for
333 // URLRequestTestHTTP.BasicAuthWithCookies
[email protected]5796dc942011-07-14 19:26:10334 // where OnBeforeSendHeaders -> OnSendHeaders -> OnBeforeSendHeaders
[email protected]87a09a92011-07-14 15:50:50335 // occurs.
[email protected]f3cf9802011-10-28 18:44:58336 RestartTransactionWithAuth(AuthCredentials());
[email protected]0757e7702009-03-27 04:00:22337 return;
338 }
339
[email protected]4f5656c62010-12-13 10:47:09340 URLRequestJob::NotifyHeadersComplete();
initial.commit586acc5fe2008-07-26 22:42:52341}
342
[email protected]85c1dce2011-07-06 12:01:29343void URLRequestHttpJob::NotifyDone(const URLRequestStatus& status) {
[email protected]bbaea8f2011-06-24 00:11:01344 DoneWithRequest(FINISHED);
[email protected]dd29bcd72011-03-24 00:03:44345 URLRequestJob::NotifyDone(status);
346}
347
[email protected]175adac2008-07-30 17:28:04348void URLRequestHttpJob::DestroyTransaction() {
[email protected]c6a4eb92010-03-03 23:51:19349 DCHECK(transaction_.get());
initial.commit586acc5fe2008-07-26 22:42:52350
[email protected]bbaea8f2011-06-24 00:11:01351 DoneWithRequest(ABORTED);
[email protected]af4876d2008-10-21 23:10:57352 transaction_.reset();
initial.commit586acc5fe2008-07-26 22:42:52353 response_info_ = NULL;
[email protected]3b23a222013-05-15 21:33:25354 receive_headers_end_ = base::TimeTicks();
initial.commit586acc5fe2008-07-26 22:42:52355}
356
[email protected]175adac2008-07-30 17:28:04357void URLRequestHttpJob::StartTransaction() {
[email protected]cc05edc2013-03-08 18:04:41358 if (network_delegate()) {
[email protected]abe1c4a2013-10-25 19:28:51359 OnCallToDelegate();
[email protected]cc05edc2013-03-08 18:04:41360 int rv = network_delegate()->NotifyBeforeSendHeaders(
[email protected]084262c2011-12-01 21:12:47361 request_, notify_before_headers_sent_callback_,
[email protected]636eccd2011-06-28 12:28:01362 &request_info_.extra_headers);
363 // If an extension blocks the request, we rely on the callback to
[email protected]b4438d32012-09-27 06:15:30364 // MaybeStartTransactionInternal().
[email protected]abe1c4a2013-10-25 19:28:51365 if (rv == ERR_IO_PENDING)
[email protected]636eccd2011-06-28 12:28:01366 return;
[email protected]b4438d32012-09-27 06:15:30367 MaybeStartTransactionInternal(rv);
368 return;
[email protected]636eccd2011-06-28 12:28:01369 }
370 StartTransactionInternal();
371}
372
373void URLRequestHttpJob::NotifyBeforeSendHeadersCallback(int result) {
[email protected]9045b8822012-01-13 20:35:35374 // Check that there are no callbacks to already canceled requests.
375 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
376
[email protected]b4438d32012-09-27 06:15:30377 MaybeStartTransactionInternal(result);
378}
379
380void URLRequestHttpJob::MaybeStartTransactionInternal(int result) {
[email protected]abe1c4a2013-10-25 19:28:51381 OnCallToDelegateComplete();
[email protected]636eccd2011-06-28 12:28:01382 if (result == OK) {
383 StartTransactionInternal();
384 } else {
[email protected]55b8a6c12012-06-13 22:03:42385 std::string source("delegate");
[email protected]636eccd2011-06-28 12:28:01386 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42387 NetLog::StringCallback("source", &source));
[email protected]636eccd2011-06-28 12:28:01388 NotifyCanceled();
[email protected]b4438d32012-09-27 06:15:30389 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
[email protected]636eccd2011-06-28 12:28:01390 }
391}
392
393void URLRequestHttpJob::StartTransactionInternal() {
initial.commit586acc5fe2008-07-26 22:42:52394 // NOTE: This method assumes that request_info_ is already setup properly.
395
[email protected]34602282010-02-03 22:14:15396 // If we already have a transaction, then we should restart the transaction
[email protected]f3cf9802011-10-28 18:44:58397 // with auth provided by auth_credentials_.
initial.commit586acc5fe2008-07-26 22:42:52398
[email protected]99c07902010-08-17 18:59:52399 int rv;
[email protected]6b3f9642010-11-25 02:29:06400
[email protected]cc05edc2013-03-08 18:04:41401 if (network_delegate()) {
402 network_delegate()->NotifySendHeaders(
[email protected]5796dc942011-07-14 19:26:10403 request_, request_info_.extra_headers);
404 }
405
[email protected]34602282010-02-03 22:14:15406 if (transaction_.get()) {
[email protected]49639fa2011-12-20 23:22:41407 rv = transaction_->RestartWithAuth(auth_credentials_, start_callback_);
[email protected]f3cf9802011-10-28 18:44:58408 auth_credentials_ = AuthCredentials();
[email protected]34602282010-02-03 22:14:15409 } else {
[email protected]34602282010-02-03 22:14:15410 DCHECK(request_->context()->http_transaction_factory());
initial.commit586acc5fe2008-07-26 22:42:52411
[email protected]99c07902010-08-17 18:59:52412 rv = request_->context()->http_transaction_factory()->CreateTransaction(
[email protected]027bd85a2013-12-27 22:39:10413 priority_, &transaction_);
[email protected]f4533ba2013-11-28 09:35:41414
415 if (rv == OK && request_info_.url.SchemeIsWSOrWSS()) {
416 // TODO(ricea): Implement WebSocket throttling semantics as defined in
417 // RFC6455 Section 4.1.
418 base::SupportsUserData::Data* data = request_->GetUserData(
419 WebSocketHandshakeStreamBase::CreateHelper::DataKey());
420 if (data) {
421 transaction_->SetWebSocketHandshakeStreamCreateHelper(
422 static_cast<WebSocketHandshakeStreamBase::CreateHelper*>(data));
423 } else {
424 rv = ERR_DISALLOWED_URL_SCHEME;
425 }
426 }
427
[email protected]4f5656c62010-12-13 10:47:09428 if (rv == OK) {
[email protected]a45840b2014-01-10 15:40:22429 transaction_->SetBeforeNetworkStartCallback(
430 base::Bind(&URLRequestHttpJob::NotifyBeforeNetworkStart,
431 base::Unretained(this)));
432
[email protected]90499482013-06-01 00:39:50433 if (!throttling_entry_.get() ||
[email protected]a1d4ab072012-06-07 13:21:15434 !throttling_entry_->ShouldRejectRequest(*request_)) {
[email protected]227b0e82011-03-25 21:11:53435 rv = transaction_->Start(
[email protected]49639fa2011-12-20 23:22:41436 &request_info_, start_callback_, request_->net_log());
[email protected]bbaea8f2011-06-24 00:11:01437 start_time_ = base::TimeTicks::Now();
[email protected]227b0e82011-03-25 21:11:53438 } else {
439 // Special error code for the exponential back-off module.
440 rv = ERR_TEMPORARILY_THROTTLED;
441 }
[email protected]34602282010-02-03 22:14:15442 }
initial.commit586acc5fe2008-07-26 22:42:52443 }
444
[email protected]4f5656c62010-12-13 10:47:09445 if (rv == ERR_IO_PENDING)
[email protected]34602282010-02-03 22:14:15446 return;
447
initial.commit586acc5fe2008-07-26 22:42:52448 // The transaction started synchronously, but we need to notify the
[email protected]4f5656c62010-12-13 10:47:09449 // URLRequest delegate via the message loop.
[email protected]2da659e2013-05-23 20:51:34450 base::MessageLoop::current()->PostTask(
[email protected]00e48bf2010-12-03 06:15:42451 FROM_HERE,
[email protected]098b29112011-12-20 21:12:34452 base::Bind(&URLRequestHttpJob::OnStartCompleted,
453 weak_factory_.GetWeakPtr(), rv));
initial.commit586acc5fe2008-07-26 22:42:52454}
455
[email protected]175adac2008-07-30 17:28:04456void URLRequestHttpJob::AddExtraHeaders() {
[email protected]c7bef94c2011-06-21 18:05:51457 // Supply Accept-Encoding field only if it is not already provided.
458 // It should be provided IF the content is known to have restrictions on
459 // potential encoding, such as streaming multi-media.
460 // For details see bug 47381.
461 // TODO(jar, enal): jpeg files etc. should set up a request header if
462 // possible. Right now it is done only by buffered_resource_loader and
463 // simple_data_source.
464 if (!request_info_.extra_headers.HasHeader(
465 HttpRequestHeaders::kAcceptEncoding)) {
466 bool advertise_sdch = SdchManager::Global() &&
467 SdchManager::Global()->IsInSupportedDomain(request_->url());
468 std::string avail_dictionaries;
469 if (advertise_sdch) {
470 SdchManager::Global()->GetAvailDictionaryList(request_->url(),
471 &avail_dictionaries);
[email protected]5b90b5d2009-04-30 23:06:01472
[email protected]c7bef94c2011-06-21 18:05:51473 // The AllowLatencyExperiment() is only true if we've successfully done a
474 // full SDCH compression recently in this browser session for this host.
475 // Note that for this path, there might be no applicable dictionaries,
476 // and hence we can't participate in the experiment.
477 if (!avail_dictionaries.empty() &&
478 SdchManager::Global()->AllowLatencyExperiment(request_->url())) {
479 // We are participating in the test (or control), and hence we'll
480 // eventually record statistics via either SDCH_EXPERIMENT_DECODE or
481 // SDCH_EXPERIMENT_HOLDBACK, and we'll need some packet timing data.
482 packet_timing_enabled_ = true;
483 if (base::RandDouble() < .01) {
484 sdch_test_control_ = true; // 1% probability.
485 advertise_sdch = false;
486 } else {
487 sdch_test_activated_ = true;
488 }
[email protected]5b90b5d2009-04-30 23:06:01489 }
490 }
[email protected]5b90b5d2009-04-30 23:06:01491
[email protected]c7bef94c2011-06-21 18:05:51492 // Supply Accept-Encoding headers first so that it is more likely that they
493 // will be in the first transmitted packet. This can sometimes make it
494 // easier to filter and analyze the streams to assure that a proxy has not
495 // damaged these headers. Some proxies deliberately corrupt Accept-Encoding
496 // headers.
497 if (!advertise_sdch) {
498 // Tell the server what compression formats we support (other than SDCH).
[email protected]8c76ae22010-04-20 22:15:43499 request_info_.extra_headers.SetHeader(
[email protected]c7bef94c2011-06-21 18:05:51500 HttpRequestHeaders::kAcceptEncoding, "gzip,deflate");
501 } else {
502 // Include SDCH in acceptable list.
503 request_info_.extra_headers.SetHeader(
504 HttpRequestHeaders::kAcceptEncoding, "gzip,deflate,sdch");
505 if (!avail_dictionaries.empty()) {
506 request_info_.extra_headers.SetHeader(
507 kAvailDictionaryHeader,
508 avail_dictionaries);
509 sdch_dictionary_advertised_ = true;
510 // Since we're tagging this transaction as advertising a dictionary,
511 // we'll definitely employ an SDCH filter (or tentative sdch filter)
512 // when we get a response. When done, we'll record histograms via
513 // SDCH_DECODE or SDCH_PASSTHROUGH. Hence we need to record packet
514 // arrival times.
515 packet_timing_enabled_ = true;
516 }
[email protected]423041b2008-10-27 17:39:28517 }
[email protected]423041b2008-10-27 17:39:28518 }
519
[email protected]ee4c30d2012-11-07 15:08:43520 if (http_user_agent_settings_) {
[email protected]84f05432013-03-15 01:00:12521 // Only add default Accept-Language if the request didn't have it
522 // specified.
[email protected]ee4c30d2012-11-07 15:08:43523 std::string accept_language =
524 http_user_agent_settings_->GetAcceptLanguage();
525 if (!accept_language.empty()) {
526 request_info_.extra_headers.SetHeaderIfMissing(
527 HttpRequestHeaders::kAcceptLanguage,
528 accept_language);
529 }
initial.commit586acc5fe2008-07-26 22:42:52530 }
initial.commit586acc5fe2008-07-26 22:42:52531}
532
[email protected]34602282010-02-03 22:14:15533void URLRequestHttpJob::AddCookieHeaderAndStart() {
534 // No matter what, we want to report our status as IO pending since we will
535 // be notifying our consumer asynchronously via OnStartCompleted.
536 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
[email protected]861fcd52009-08-26 02:33:46537
[email protected]ed24fad2011-05-10 22:44:01538 // If the request was destroyed, then there is no more work to do.
539 if (!request_)
540 return;
[email protected]34602282010-02-03 22:14:15541
[email protected]d670bbd2014-03-12 21:39:42542 CookieStore* cookie_store = GetCookieStore();
[email protected]1a6fff52011-10-20 21:00:16543 if (cookie_store && !(request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES)) {
544 net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster();
545 if (cookie_monster) {
546 cookie_monster->GetAllCookiesForURLAsync(
547 request_->url(),
548 base::Bind(&URLRequestHttpJob::CheckCookiePolicyAndLoad,
[email protected]098b29112011-12-20 21:12:34549 weak_factory_.GetWeakPtr()));
[email protected]1a6fff52011-10-20 21:00:16550 } else {
[email protected]cec80c3d2012-12-04 12:17:41551 CheckCookiePolicyAndLoad(CookieList());
[email protected]1a6fff52011-10-20 21:00:16552 }
[email protected]03d845f2011-07-29 19:06:26553 } else {
554 DoStartTransaction();
555 }
556}
557
[email protected]1a6fff52011-10-20 21:00:16558void URLRequestHttpJob::DoLoadCookies() {
559 CookieOptions options;
560 options.set_include_httponly();
[email protected]d670bbd2014-03-12 21:39:42561 GetCookieStore()->GetCookiesWithOptionsAsync(
[email protected]1a6fff52011-10-20 21:00:16562 request_->url(), options,
563 base::Bind(&URLRequestHttpJob::OnCookiesLoaded,
[email protected]098b29112011-12-20 21:12:34564 weak_factory_.GetWeakPtr()));
[email protected]1a6fff52011-10-20 21:00:16565}
566
[email protected]03d845f2011-07-29 19:06:26567void URLRequestHttpJob::CheckCookiePolicyAndLoad(
568 const CookieList& cookie_list) {
[email protected]1a6fff52011-10-20 21:00:16569 if (CanGetCookies(cookie_list))
570 DoLoadCookies();
571 else
[email protected]54f4c9362011-07-25 21:54:46572 DoStartTransaction();
[email protected]54f4c9362011-07-25 21:54:46573}
574
[email protected]dedec0b2013-02-28 04:50:10575void URLRequestHttpJob::OnCookiesLoaded(const std::string& cookie_line) {
[email protected]218aa6a12011-09-13 17:38:38576 if (!cookie_line.empty()) {
[email protected]54f4c9362011-07-25 21:54:46577 request_info_.extra_headers.SetHeader(
[email protected]218aa6a12011-09-13 17:38:38578 HttpRequestHeaders::kCookie, cookie_line);
[email protected]e6d017652013-05-17 18:01:40579 // Disable privacy mode as we are sending cookies anyway.
580 request_info_.privacy_mode = kPrivacyModeDisabled;
[email protected]54f4c9362011-07-25 21:54:46581 }
[email protected]54f4c9362011-07-25 21:54:46582 DoStartTransaction();
583}
584
585void URLRequestHttpJob::DoStartTransaction() {
[email protected]03d845f2011-07-29 19:06:26586 // We may have been canceled while retrieving cookies.
[email protected]9025016c2011-05-12 15:51:23587 if (GetStatus().is_success()) {
588 StartTransaction();
589 } else {
590 NotifyCanceled();
591 }
[email protected]0757e7702009-03-27 04:00:22592}
593
[email protected]ea8141e2011-10-05 13:12:51594void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
[email protected]abe1c4a2013-10-25 19:28:51595 // End of the call started in OnStartCompleted.
596 OnCallToDelegateComplete();
597
[email protected]ea8141e2011-10-05 13:12:51598 if (result != net::OK) {
[email protected]55b8a6c12012-06-13 22:03:42599 std::string source("delegate");
[email protected]ea8141e2011-10-05 13:12:51600 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42601 NetLog::StringCallback("source", &source));
[email protected]ea8141e2011-10-05 13:12:51602 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
603 return;
604 }
605
[email protected]34602282010-02-03 22:14:15606 DCHECK(transaction_.get());
initial.commit586acc5fe2008-07-26 22:42:52607
[email protected]4f5656c62010-12-13 10:47:09608 const HttpResponseInfo* response_info = transaction_->GetResponseInfo();
[email protected]34602282010-02-03 22:14:15609 DCHECK(response_info);
610
611 response_cookies_.clear();
612 response_cookies_save_index_ = 0;
613
[email protected]ea8141e2011-10-05 13:12:51614 FetchResponseCookies(&response_cookies_);
[email protected]34602282010-02-03 22:14:15615
[email protected]5095cd72012-11-01 10:29:16616 if (!GetResponseHeaders()->GetDateValue(&response_date_))
617 response_date_ = base::Time();
618
[email protected]34602282010-02-03 22:14:15619 // Now, loop over the response cookies, and attempt to persist each.
620 SaveNextCookie();
621}
622
[email protected]263163f2012-06-14 22:40:34623// If the save occurs synchronously, SaveNextCookie will loop and save the next
624// cookie. If the save is deferred, the callback is responsible for continuing
625// to iterate through the cookies.
626// TODO(erikwright): Modify the CookieStore API to indicate via return value
627// whether it completed synchronously or asynchronously.
628// See http://crbug.com/131066.
[email protected]34602282010-02-03 22:14:15629void URLRequestHttpJob::SaveNextCookie() {
[email protected]263163f2012-06-14 22:40:34630 // No matter what, we want to report our status as IO pending since we will
631 // be notifying our consumer asynchronously via OnStartCompleted.
632 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
633
634 // Used to communicate with the callback. See the implementation of
635 // OnCookieSaved.
636 scoped_refptr<SharedBoolean> callback_pending = new SharedBoolean(false);
637 scoped_refptr<SharedBoolean> save_next_cookie_running =
638 new SharedBoolean(true);
639
640 if (!(request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) &&
[email protected]d670bbd2014-03-12 21:39:42641 GetCookieStore() && response_cookies_.size() > 0) {
[email protected]263163f2012-06-14 22:40:34642 CookieOptions options;
643 options.set_include_httponly();
[email protected]5095cd72012-11-01 10:29:16644 options.set_server_time(response_date_);
[email protected]263163f2012-06-14 22:40:34645
646 net::CookieStore::SetCookiesCallback callback(
647 base::Bind(&URLRequestHttpJob::OnCookieSaved,
648 weak_factory_.GetWeakPtr(),
649 save_next_cookie_running,
650 callback_pending));
651
652 // Loop through the cookies as long as SetCookieWithOptionsAsync completes
653 // synchronously.
654 while (!callback_pending->data &&
655 response_cookies_save_index_ < response_cookies_.size()) {
656 if (CanSetCookie(
657 response_cookies_[response_cookies_save_index_], &options)) {
658 callback_pending->data = true;
[email protected]d670bbd2014-03-12 21:39:42659 GetCookieStore()->SetCookieWithOptionsAsync(
[email protected]263163f2012-06-14 22:40:34660 request_->url(), response_cookies_[response_cookies_save_index_],
661 options, callback);
662 }
663 ++response_cookies_save_index_;
664 }
665 }
666
667 save_next_cookie_running->data = false;
668
669 if (!callback_pending->data) {
[email protected]34602282010-02-03 22:14:15670 response_cookies_.clear();
671 response_cookies_save_index_ = 0;
672 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
673 NotifyHeadersComplete();
674 return;
675 }
[email protected]263163f2012-06-14 22:40:34676}
[email protected]34602282010-02-03 22:14:15677
[email protected]263163f2012-06-14 22:40:34678// |save_next_cookie_running| is true when the callback is bound and set to
679// false when SaveNextCookie exits, allowing the callback to determine if the
680// save occurred synchronously or asynchronously.
681// |callback_pending| is false when the callback is invoked and will be set to
682// true by the callback, allowing SaveNextCookie to detect whether the save
683// occurred synchronously.
684// See SaveNextCookie() for more information.
685void URLRequestHttpJob::OnCookieSaved(
686 scoped_refptr<SharedBoolean> save_next_cookie_running,
687 scoped_refptr<SharedBoolean> callback_pending,
688 bool cookie_status) {
689 callback_pending->data = false;
[email protected]34602282010-02-03 22:14:15690
[email protected]263163f2012-06-14 22:40:34691 // If we were called synchronously, return.
692 if (save_next_cookie_running->data) {
693 return;
[email protected]34602282010-02-03 22:14:15694 }
695
[email protected]263163f2012-06-14 22:40:34696 // We were called asynchronously, so trigger the next save.
[email protected]9025016c2011-05-12 15:51:23697 // We may have been canceled within OnSetCookie.
698 if (GetStatus().is_success()) {
699 SaveNextCookie();
700 } else {
701 NotifyCanceled();
702 }
[email protected]34602282010-02-03 22:14:15703}
704
705void URLRequestHttpJob::FetchResponseCookies(
[email protected]34602282010-02-03 22:14:15706 std::vector<std::string>* cookies) {
[email protected]264300242011-11-07 06:03:30707 const std::string name = "Set-Cookie";
initial.commit586acc5fe2008-07-26 22:42:52708 std::string value;
709
710 void* iter = NULL;
[email protected]ea8141e2011-10-05 13:12:51711 HttpResponseHeaders* headers = GetResponseHeaders();
712 while (headers->EnumerateHeader(&iter, name, &value)) {
[email protected]2adf2882010-09-27 08:30:37713 if (!value.empty())
714 cookies->push_back(value);
715 }
initial.commit586acc5fe2008-07-26 22:42:52716}
[email protected]a9cea7542009-05-20 04:30:23717
[email protected]000d9df02012-01-18 20:01:46718// NOTE: |ProcessStrictTransportSecurityHeader| and
719// |ProcessPublicKeyPinsHeader| have very similar structures, by design.
[email protected]77f6fb432009-09-05 14:21:09720void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() {
[email protected]a9cea7542009-05-20 04:30:23721 DCHECK(response_info_);
[email protected]6ed72be2013-01-08 22:07:33722 TransportSecurityState* security_state =
723 request_->context()->transport_security_state();
[email protected]e88006f2012-01-11 06:15:07724 const SSLInfo& ssl_info = response_info_->ssl_info;
[email protected]a9cea7542009-05-20 04:30:23725
[email protected]6ed72be2013-01-08 22:07:33726 // Only accept HSTS headers on HTTPS connections that have no
727 // certificate errors.
[email protected]e88006f2012-01-11 06:15:07728 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
[email protected]6ed72be2013-01-08 22:07:33729 !security_state)
[email protected]e88006f2012-01-11 06:15:07730 return;
[email protected]326e6792009-12-11 21:04:42731
[email protected]242d8562012-10-30 21:20:46732 // http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec:
733 //
734 // If a UA receives more than one STS header field in a HTTP response
735 // message over secure transport, then the UA MUST process only the
736 // first such header field.
[email protected]6ed72be2013-01-08 22:07:33737 HttpResponseHeaders* headers = GetResponseHeaders();
738 std::string value;
739 if (headers->EnumerateHeader(NULL, "Strict-Transport-Security", &value))
740 security_state->AddHSTSHeader(request_info_.url.host(), value);
[email protected]a9cea7542009-05-20 04:30:23741}
[email protected]4f5656c62010-12-13 10:47:09742
[email protected]fecef222012-01-05 02:26:15743void URLRequestHttpJob::ProcessPublicKeyPinsHeader() {
744 DCHECK(response_info_);
[email protected]6ed72be2013-01-08 22:07:33745 TransportSecurityState* security_state =
746 request_->context()->transport_security_state();
[email protected]fecef222012-01-05 02:26:15747 const SSLInfo& ssl_info = response_info_->ssl_info;
748
[email protected]6ed72be2013-01-08 22:07:33749 // Only accept HPKP headers on HTTPS connections that have no
[email protected]e88006f2012-01-11 06:15:07750 // certificate errors.
[email protected]fecef222012-01-05 02:26:15751 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
[email protected]6ed72be2013-01-08 22:07:33752 !security_state)
[email protected]fecef222012-01-05 02:26:15753 return;
[email protected]fecef222012-01-05 02:26:15754
[email protected]6ed72be2013-01-08 22:07:33755 // http://tools.ietf.org/html/draft-ietf-websec-key-pinning:
756 //
757 // If a UA receives more than one PKP header field in an HTTP
758 // response message over secure transport, then the UA MUST process
759 // only the first such header field.
[email protected]fecef222012-01-05 02:26:15760 HttpResponseHeaders* headers = GetResponseHeaders();
[email protected]fecef222012-01-05 02:26:15761 std::string value;
[email protected]6ed72be2013-01-08 22:07:33762 if (headers->EnumerateHeader(NULL, "Public-Key-Pins", &value))
763 security_state->AddHPKPHeader(request_info_.url.host(), value, ssl_info);
[email protected]fecef222012-01-05 02:26:15764}
765
[email protected]5394e422011-01-20 22:07:43766void URLRequestHttpJob::OnStartCompleted(int result) {
[email protected]ec23f522011-02-22 21:01:38767 RecordTimer();
768
[email protected]5394e422011-01-20 22:07:43769 // If the request was destroyed, then there is no more work to do.
[email protected]a83dd332011-07-13 10:41:01770 if (!request_)
[email protected]5394e422011-01-20 22:07:43771 return;
772
773 // If the transaction was destroyed, then the job was cancelled, and
774 // we can just ignore this notification.
775 if (!transaction_.get())
776 return;
777
[email protected]3b23a222013-05-15 21:33:25778 receive_headers_end_ = base::TimeTicks::Now();
779
[email protected]5394e422011-01-20 22:07:43780 // Clear the IO_PENDING status
781 SetStatus(URLRequestStatus());
782
[email protected]ef2bf422012-05-11 03:27:09783 const URLRequestContext* context = request_->context();
784
[email protected]f3572592011-12-12 21:36:31785 if (result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN &&
786 transaction_->GetResponseInfo() != NULL) {
787 FraudulentCertificateReporter* reporter =
[email protected]ef2bf422012-05-11 03:27:09788 context->fraudulent_certificate_reporter();
[email protected]f3572592011-12-12 21:36:31789 if (reporter != NULL) {
790 const SSLInfo& ssl_info = transaction_->GetResponseInfo()->ssl_info;
[email protected]551ff5172011-11-10 18:35:31791 bool sni_available = SSLConfigService::IsSNIAvailable(
[email protected]ef2bf422012-05-11 03:27:09792 context->ssl_config_service());
[email protected]f3572592011-12-12 21:36:31793 const std::string& host = request_->url().host();
[email protected]b7996452011-10-31 19:30:56794
[email protected]f3572592011-12-12 21:36:31795 reporter->SendReport(host, ssl_info, sni_available);
[email protected]381e8852011-04-14 14:30:58796 }
797 }
[email protected]551ff5172011-11-10 18:35:31798
[email protected]5394e422011-01-20 22:07:43799 if (result == OK) {
[email protected]ea8141e2011-10-05 13:12:51800 scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders();
[email protected]cc05edc2013-03-08 18:04:41801 if (network_delegate()) {
[email protected]ea8141e2011-10-05 13:12:51802 // Note that |this| may not be deleted until
803 // |on_headers_received_callback_| or
804 // |NetworkDelegate::URLRequestDestroyed()| has been called.
[email protected]abe1c4a2013-10-25 19:28:51805 OnCallToDelegate();
[email protected]cc05edc2013-03-08 18:04:41806 int error = network_delegate()->NotifyHeadersReceived(
[email protected]90499482013-06-01 00:39:50807 request_,
808 on_headers_received_callback_,
809 headers.get(),
810 &override_response_headers_);
[email protected]ea8141e2011-10-05 13:12:51811 if (error != net::OK) {
812 if (error == net::ERR_IO_PENDING) {
813 awaiting_callback_ = true;
[email protected]ea8141e2011-10-05 13:12:51814 } else {
[email protected]55b8a6c12012-06-13 22:03:42815 std::string source("delegate");
[email protected]ea8141e2011-10-05 13:12:51816 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
[email protected]55b8a6c12012-06-13 22:03:42817 NetLog::StringCallback("source",
818 &source));
[email protected]abe1c4a2013-10-25 19:28:51819 OnCallToDelegateComplete();
[email protected]ea8141e2011-10-05 13:12:51820 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, error));
821 }
822 return;
823 }
824 }
825
826 SaveCookiesAndNotifyHeadersComplete(net::OK);
[email protected]e5624f02011-09-27 19:43:53827 } else if (IsCertificateError(result)) {
[email protected]6061c142013-10-21 15:13:34828 // We encountered an SSL certificate error.
829 if (result == ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY ||
830 result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN) {
831 // These are hard failures. They're handled separately and don't have
832 // the correct cert status, so set it here.
833 SSLInfo info(transaction_->GetResponseInfo()->ssl_info);
834 info.cert_status = MapNetErrorToCertStatus(result);
835 NotifySSLCertificateError(info, true);
836 } else {
837 // Maybe overridable, maybe not. Ask the delegate to decide.
838 TransportSecurityState::DomainState domain_state;
839 const URLRequestContext* context = request_->context();
840 const bool fatal = context->transport_security_state() &&
841 context->transport_security_state()->GetDomainState(
842 request_info_.url.host(),
843 SSLConfigService::IsSNIAvailable(context->ssl_config_service()),
844 &domain_state) &&
845 domain_state.ShouldSSLErrorsBeFatal();
846 NotifySSLCertificateError(
847 transaction_->GetResponseInfo()->ssl_info, fatal);
848 }
[email protected]5394e422011-01-20 22:07:43849 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]a83dd332011-07-13 10:41:01850 NotifyCertificateRequested(
[email protected]90499482013-06-01 00:39:50851 transaction_->GetResponseInfo()->cert_request_info.get());
[email protected]5394e422011-01-20 22:07:43852 } else {
[email protected]419704c2014-01-14 11:18:06853 // Even on an error, there may be useful information in the response
854 // info (e.g. whether there's a cached copy).
855 if (transaction_.get())
856 response_info_ = transaction_->GetResponseInfo();
[email protected]5394e422011-01-20 22:07:43857 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
858 }
859}
860
[email protected]ea8141e2011-10-05 13:12:51861void URLRequestHttpJob::OnHeadersReceivedCallback(int result) {
[email protected]ea8141e2011-10-05 13:12:51862 awaiting_callback_ = false;
[email protected]9045b8822012-01-13 20:35:35863
864 // Check that there are no callbacks to already canceled requests.
865 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
866
[email protected]ea8141e2011-10-05 13:12:51867 SaveCookiesAndNotifyHeadersComplete(result);
868}
869
[email protected]5394e422011-01-20 22:07:43870void URLRequestHttpJob::OnReadCompleted(int result) {
871 read_in_progress_ = false;
872
[email protected]f001bd6a2011-12-08 04:31:37873 if (ShouldFixMismatchedContentLength(result))
[email protected]5543cbb2012-04-20 16:35:23874 result = OK;
[email protected]f001bd6a2011-12-08 04:31:37875
[email protected]5543cbb2012-04-20 16:35:23876 if (result == OK) {
[email protected]5394e422011-01-20 22:07:43877 NotifyDone(URLRequestStatus());
878 } else if (result < 0) {
879 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
880 } else {
881 // Clear the IO_PENDING status
882 SetStatus(URLRequestStatus());
883 }
884
885 NotifyReadComplete(result);
886}
887
[email protected]5394e422011-01-20 22:07:43888void URLRequestHttpJob::RestartTransactionWithAuth(
[email protected]f3cf9802011-10-28 18:44:58889 const AuthCredentials& credentials) {
890 auth_credentials_ = credentials;
[email protected]5394e422011-01-20 22:07:43891
892 // These will be reset in OnStartCompleted.
893 response_info_ = NULL;
[email protected]3b23a222013-05-15 21:33:25894 receive_headers_end_ = base::TimeTicks();
[email protected]5394e422011-01-20 22:07:43895 response_cookies_.clear();
896
[email protected]ec23f522011-02-22 21:01:38897 ResetTimer();
898
[email protected]5394e422011-01-20 22:07:43899 // Update the cookies, since the cookie store may have been updated from the
900 // headers in the 401/407. Since cookies were already appended to
901 // extra_headers, we need to strip them out before adding them again.
[email protected]ea8141e2011-10-05 13:12:51902 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kCookie);
[email protected]5394e422011-01-20 22:07:43903
904 AddCookieHeaderAndStart();
905}
906
[email protected]0736d9e2012-11-28 19:50:40907void URLRequestHttpJob::SetUpload(UploadDataStream* upload) {
[email protected]5394e422011-01-20 22:07:43908 DCHECK(!transaction_.get()) << "cannot change once started";
[email protected]0736d9e2012-11-28 19:50:40909 request_info_.upload_data_stream = upload;
[email protected]5394e422011-01-20 22:07:43910}
911
912void URLRequestHttpJob::SetExtraRequestHeaders(
913 const HttpRequestHeaders& headers) {
914 DCHECK(!transaction_.get()) << "cannot change once started";
915 request_info_.extra_headers.CopyFrom(headers);
916}
917
[email protected]5394e422011-01-20 22:07:43918LoadState URLRequestHttpJob::GetLoadState() const {
919 return transaction_.get() ?
920 transaction_->GetLoadState() : LOAD_STATE_IDLE;
921}
922
[email protected]7335ab02012-08-30 22:30:42923UploadProgress URLRequestHttpJob::GetUploadProgress() const {
924 return transaction_.get() ?
925 transaction_->GetUploadProgress() : UploadProgress();
[email protected]5394e422011-01-20 22:07:43926}
927
928bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const {
929 DCHECK(transaction_.get());
930
931 if (!response_info_)
932 return false;
933
[email protected]ea8141e2011-10-05 13:12:51934 return GetResponseHeaders()->GetMimeType(mime_type);
[email protected]5394e422011-01-20 22:07:43935}
936
937bool URLRequestHttpJob::GetCharset(std::string* charset) {
938 DCHECK(transaction_.get());
939
940 if (!response_info_)
941 return false;
942
[email protected]ea8141e2011-10-05 13:12:51943 return GetResponseHeaders()->GetCharset(charset);
[email protected]5394e422011-01-20 22:07:43944}
945
946void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) {
947 DCHECK(request_);
[email protected]5394e422011-01-20 22:07:43948
[email protected]ea8141e2011-10-05 13:12:51949 if (response_info_) {
[email protected]419704c2014-01-14 11:18:06950 DCHECK(transaction_.get());
951
[email protected]5394e422011-01-20 22:07:43952 *info = *response_info_;
[email protected]90499482013-06-01 00:39:50953 if (override_response_headers_.get())
[email protected]ea8141e2011-10-05 13:12:51954 info->headers = override_response_headers_;
955 }
[email protected]5394e422011-01-20 22:07:43956}
957
[email protected]58e32bb2013-01-21 18:23:25958void URLRequestHttpJob::GetLoadTimingInfo(
959 LoadTimingInfo* load_timing_info) const {
[email protected]3b23a222013-05-15 21:33:25960 // If haven't made it far enough to receive any headers, don't return
961 // anything. This makes for more consistent behavior in the case of errors.
962 if (!transaction_ || receive_headers_end_.is_null())
963 return;
964 if (transaction_->GetLoadTimingInfo(load_timing_info))
965 load_timing_info->receive_headers_end = receive_headers_end_;
[email protected]58e32bb2013-01-21 18:23:25966}
967
[email protected]ea8141e2011-10-05 13:12:51968bool URLRequestHttpJob::GetResponseCookies(std::vector<std::string>* cookies) {
[email protected]5394e422011-01-20 22:07:43969 DCHECK(transaction_.get());
970
971 if (!response_info_)
972 return false;
973
974 // TODO(darin): Why are we extracting response cookies again? Perhaps we
975 // should just leverage response_cookies_.
976
977 cookies->clear();
[email protected]ea8141e2011-10-05 13:12:51978 FetchResponseCookies(cookies);
[email protected]5394e422011-01-20 22:07:43979 return true;
980}
981
982int URLRequestHttpJob::GetResponseCode() const {
983 DCHECK(transaction_.get());
984
985 if (!response_info_)
986 return -1;
987
[email protected]ea8141e2011-10-05 13:12:51988 return GetResponseHeaders()->response_code();
[email protected]5394e422011-01-20 22:07:43989}
990
[email protected]5a3b4d32011-03-17 01:24:05991Filter* URLRequestHttpJob::SetupFilter() const {
[email protected]5394e422011-01-20 22:07:43992 DCHECK(transaction_.get());
993 if (!response_info_)
[email protected]5a3b4d32011-03-17 01:24:05994 return NULL;
[email protected]5394e422011-01-20 22:07:43995
[email protected]5a3b4d32011-03-17 01:24:05996 std::vector<Filter::FilterType> encoding_types;
[email protected]5394e422011-01-20 22:07:43997 std::string encoding_type;
[email protected]ea8141e2011-10-05 13:12:51998 HttpResponseHeaders* headers = GetResponseHeaders();
[email protected]5394e422011-01-20 22:07:43999 void* iter = NULL;
[email protected]ea8141e2011-10-05 13:12:511000 while (headers->EnumerateHeader(&iter, "Content-Encoding", &encoding_type)) {
[email protected]5a3b4d32011-03-17 01:24:051001 encoding_types.push_back(Filter::ConvertEncodingToType(encoding_type));
[email protected]5394e422011-01-20 22:07:431002 }
1003
[email protected]46668fe52011-05-04 19:03:231004 if (filter_context_->IsSdchResponse()) {
1005 // We are wary of proxies that discard or damage SDCH encoding. If a server
1006 // explicitly states that this is not SDCH content, then we can correct our
1007 // assumption that this is an SDCH response, and avoid the need to recover
1008 // as though the content is corrupted (when we discover it is not SDCH
1009 // encoded).
1010 std::string sdch_response_status;
1011 iter = NULL;
[email protected]ea8141e2011-10-05 13:12:511012 while (headers->EnumerateHeader(&iter, "X-Sdch-Encode",
1013 &sdch_response_status)) {
[email protected]46668fe52011-05-04 19:03:231014 if (sdch_response_status == "0") {
1015 filter_context_->ResetSdchResponseToFalse();
1016 break;
1017 }
1018 }
1019 }
1020
[email protected]5394e422011-01-20 22:07:431021 // Even if encoding types are empty, there is a chance that we need to add
1022 // some decoding, as some proxies strip encoding completely. In such cases,
1023 // we may need to add (for example) SDCH filtering (when the context suggests
1024 // it is appropriate).
[email protected]2e92354c2011-03-25 20:49:531025 Filter::FixupEncodingTypes(*filter_context_, &encoding_types);
[email protected]5394e422011-01-20 22:07:431026
[email protected]5a3b4d32011-03-17 01:24:051027 return !encoding_types.empty()
[email protected]2e92354c2011-03-25 20:49:531028 ? Filter::Factory(encoding_types, *filter_context_) : NULL;
[email protected]5394e422011-01-20 22:07:431029}
1030
[email protected]5394e422011-01-20 22:07:431031bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) {
[email protected]e0f35c92013-05-08 16:04:341032 // HTTP is always safe.
1033 // TODO(pauljensen): Remove once crbug.com/146591 is fixed.
1034 if (location.is_valid() &&
1035 (location.scheme() == "http" || location.scheme() == "https")) {
[email protected]5394e422011-01-20 22:07:431036 return true;
[email protected]5394e422011-01-20 22:07:431037 }
[email protected]e0f35c92013-05-08 16:04:341038 // Query URLRequestJobFactory as to whether |location| would be safe to
1039 // redirect to.
1040 return request_->context()->job_factory() &&
1041 request_->context()->job_factory()->IsSafeRedirectTarget(location);
[email protected]5394e422011-01-20 22:07:431042}
1043
1044bool URLRequestHttpJob::NeedsAuth() {
1045 int code = GetResponseCode();
1046 if (code == -1)
1047 return false;
1048
1049 // Check if we need either Proxy or WWW Authentication. This could happen
1050 // because we either provided no auth info, or provided incorrect info.
1051 switch (code) {
1052 case 407:
1053 if (proxy_auth_state_ == AUTH_STATE_CANCELED)
1054 return false;
1055 proxy_auth_state_ = AUTH_STATE_NEED_AUTH;
1056 return true;
1057 case 401:
1058 if (server_auth_state_ == AUTH_STATE_CANCELED)
1059 return false;
1060 server_auth_state_ = AUTH_STATE_NEED_AUTH;
1061 return true;
1062 }
1063 return false;
1064}
1065
1066void URLRequestHttpJob::GetAuthChallengeInfo(
1067 scoped_refptr<AuthChallengeInfo>* result) {
1068 DCHECK(transaction_.get());
1069 DCHECK(response_info_);
1070
1071 // sanity checks:
1072 DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH ||
1073 server_auth_state_ == AUTH_STATE_NEED_AUTH);
[email protected]9094b602012-02-27 21:44:581074 DCHECK((GetResponseHeaders()->response_code() == HTTP_UNAUTHORIZED) ||
1075 (GetResponseHeaders()->response_code() ==
1076 HTTP_PROXY_AUTHENTICATION_REQUIRED));
[email protected]5394e422011-01-20 22:07:431077
1078 *result = response_info_->auth_challenge;
1079}
1080
[email protected]f3cf9802011-10-28 18:44:581081void URLRequestHttpJob::SetAuth(const AuthCredentials& credentials) {
[email protected]5394e422011-01-20 22:07:431082 DCHECK(transaction_.get());
1083
1084 // Proxy gets set first, then WWW.
1085 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1086 proxy_auth_state_ = AUTH_STATE_HAVE_AUTH;
1087 } else {
[email protected]dd29bcd72011-03-24 00:03:441088 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
[email protected]5394e422011-01-20 22:07:431089 server_auth_state_ = AUTH_STATE_HAVE_AUTH;
1090 }
1091
[email protected]f3cf9802011-10-28 18:44:581092 RestartTransactionWithAuth(credentials);
[email protected]5394e422011-01-20 22:07:431093}
1094
1095void URLRequestHttpJob::CancelAuth() {
1096 // Proxy gets set first, then WWW.
1097 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1098 proxy_auth_state_ = AUTH_STATE_CANCELED;
1099 } else {
[email protected]dd29bcd72011-03-24 00:03:441100 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
[email protected]5394e422011-01-20 22:07:431101 server_auth_state_ = AUTH_STATE_CANCELED;
1102 }
1103
1104 // These will be reset in OnStartCompleted.
1105 response_info_ = NULL;
[email protected]3b23a222013-05-15 21:33:251106 receive_headers_end_ = base::TimeTicks::Now();
[email protected]5394e422011-01-20 22:07:431107 response_cookies_.clear();
1108
[email protected]ec23f522011-02-22 21:01:381109 ResetTimer();
1110
[email protected]5394e422011-01-20 22:07:431111 // OK, let the consumer read the error page...
1112 //
1113 // Because we set the AUTH_STATE_CANCELED flag, NeedsAuth will return false,
1114 // which will cause the consumer to receive OnResponseStarted instead of
1115 // OnAuthRequired.
1116 //
1117 // We have to do this via InvokeLater to avoid "recursing" the consumer.
1118 //
[email protected]2da659e2013-05-23 20:51:341119 base::MessageLoop::current()->PostTask(
[email protected]5394e422011-01-20 22:07:431120 FROM_HERE,
[email protected]098b29112011-12-20 21:12:341121 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1122 weak_factory_.GetWeakPtr(), OK));
[email protected]5394e422011-01-20 22:07:431123}
1124
1125void URLRequestHttpJob::ContinueWithCertificate(
1126 X509Certificate* client_cert) {
1127 DCHECK(transaction_.get());
1128
1129 DCHECK(!response_info_) << "should not have a response yet";
[email protected]3b23a222013-05-15 21:33:251130 receive_headers_end_ = base::TimeTicks();
[email protected]5394e422011-01-20 22:07:431131
[email protected]ec23f522011-02-22 21:01:381132 ResetTimer();
1133
[email protected]5394e422011-01-20 22:07:431134 // No matter what, we want to report our status as IO pending since we will
1135 // be notifying our consumer asynchronously via OnStartCompleted.
1136 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1137
[email protected]49639fa2011-12-20 23:22:411138 int rv = transaction_->RestartWithCertificate(client_cert, start_callback_);
[email protected]5394e422011-01-20 22:07:431139 if (rv == ERR_IO_PENDING)
1140 return;
1141
1142 // The transaction started synchronously, but we need to notify the
1143 // URLRequest delegate via the message loop.
[email protected]2da659e2013-05-23 20:51:341144 base::MessageLoop::current()->PostTask(
[email protected]5394e422011-01-20 22:07:431145 FROM_HERE,
[email protected]098b29112011-12-20 21:12:341146 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1147 weak_factory_.GetWeakPtr(), rv));
[email protected]5394e422011-01-20 22:07:431148}
1149
1150void URLRequestHttpJob::ContinueDespiteLastError() {
1151 // If the transaction was destroyed, then the job was cancelled.
1152 if (!transaction_.get())
1153 return;
1154
1155 DCHECK(!response_info_) << "should not have a response yet";
[email protected]3b23a222013-05-15 21:33:251156 receive_headers_end_ = base::TimeTicks();
[email protected]5394e422011-01-20 22:07:431157
[email protected]ec23f522011-02-22 21:01:381158 ResetTimer();
1159
[email protected]5394e422011-01-20 22:07:431160 // No matter what, we want to report our status as IO pending since we will
1161 // be notifying our consumer asynchronously via OnStartCompleted.
1162 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1163
[email protected]49639fa2011-12-20 23:22:411164 int rv = transaction_->RestartIgnoringLastError(start_callback_);
[email protected]5394e422011-01-20 22:07:431165 if (rv == ERR_IO_PENDING)
1166 return;
1167
1168 // The transaction started synchronously, but we need to notify the
1169 // URLRequest delegate via the message loop.
[email protected]2da659e2013-05-23 20:51:341170 base::MessageLoop::current()->PostTask(
[email protected]5394e422011-01-20 22:07:431171 FROM_HERE,
[email protected]098b29112011-12-20 21:12:341172 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1173 weak_factory_.GetWeakPtr(), rv));
[email protected]5394e422011-01-20 22:07:431174}
1175
[email protected]1826a402014-01-08 15:40:481176void URLRequestHttpJob::ResumeNetworkStart() {
1177 DCHECK(transaction_.get());
1178 transaction_->ResumeNetworkStart();
1179}
1180
[email protected]f001bd6a2011-12-08 04:31:371181bool URLRequestHttpJob::ShouldFixMismatchedContentLength(int rv) const {
1182 // Some servers send the body compressed, but specify the content length as
1183 // the uncompressed size. Although this violates the HTTP spec we want to
1184 // support it (as IE and FireFox do), but *only* for an exact match.
1185 // See http://crbug.com/79694.
[email protected]5543cbb2012-04-20 16:35:231186 if (rv == net::ERR_CONTENT_LENGTH_MISMATCH ||
1187 rv == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
[email protected]f001bd6a2011-12-08 04:31:371188 if (request_ && request_->response_headers()) {
1189 int64 expected_length = request_->response_headers()->GetContentLength();
1190 VLOG(1) << __FUNCTION__ << "() "
1191 << "\"" << request_->url().spec() << "\""
1192 << " content-length = " << expected_length
1193 << " pre total = " << prefilter_bytes_read()
1194 << " post total = " << postfilter_bytes_read();
1195 if (postfilter_bytes_read() == expected_length) {
1196 // Clear the error.
1197 return true;
1198 }
1199 }
1200 }
1201 return false;
1202}
1203
[email protected]5394e422011-01-20 22:07:431204bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size,
[email protected]b7996452011-10-31 19:30:561205 int* bytes_read) {
[email protected]5394e422011-01-20 22:07:431206 DCHECK_NE(buf_size, 0);
1207 DCHECK(bytes_read);
1208 DCHECK(!read_in_progress_);
1209
[email protected]49639fa2011-12-20 23:22:411210 int rv = transaction_->Read(
1211 buf, buf_size,
1212 base::Bind(&URLRequestHttpJob::OnReadCompleted, base::Unretained(this)));
[email protected]85c1dce2011-07-06 12:01:291213
[email protected]f001bd6a2011-12-08 04:31:371214 if (ShouldFixMismatchedContentLength(rv))
1215 rv = 0;
1216
[email protected]5394e422011-01-20 22:07:431217 if (rv >= 0) {
1218 *bytes_read = rv;
[email protected]bbaea8f2011-06-24 00:11:011219 if (!rv)
1220 DoneWithRequest(FINISHED);
[email protected]5394e422011-01-20 22:07:431221 return true;
1222 }
1223
1224 if (rv == ERR_IO_PENDING) {
1225 read_in_progress_ = true;
1226 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1227 } else {
1228 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
1229 }
1230
1231 return false;
1232}
1233
1234void URLRequestHttpJob::StopCaching() {
1235 if (transaction_.get())
1236 transaction_->StopCaching();
1237}
1238
[email protected]79e1fd62013-06-20 06:50:041239bool URLRequestHttpJob::GetFullRequestHeaders(
1240 HttpRequestHeaders* headers) const {
1241 if (!transaction_)
1242 return false;
1243
1244 return transaction_->GetFullRequestHeaders(headers);
1245}
1246
[email protected]71e5ff8f2014-01-13 09:44:041247int64 URLRequestHttpJob::GetTotalReceivedBytes() const {
1248 if (!transaction_)
1249 return 0;
1250
1251 return transaction_->GetTotalReceivedBytes();
1252}
1253
[email protected]5c04f722011-08-12 17:52:471254void URLRequestHttpJob::DoneReading() {
1255 if (transaction_.get())
1256 transaction_->DoneReading();
1257 DoneWithRequest(FINISHED);
1258}
1259
[email protected]6d81b482011-02-22 19:47:191260HostPortPair URLRequestHttpJob::GetSocketAddress() const {
1261 return response_info_ ? response_info_->socket_address : HostPortPair();
1262}
1263
[email protected]ec23f522011-02-22 21:01:381264void URLRequestHttpJob::RecordTimer() {
1265 if (request_creation_time_.is_null()) {
1266 NOTREACHED()
1267 << "The same transaction shouldn't start twice without new timing.";
1268 return;
1269 }
1270
[email protected]320a29f12011-03-21 14:47:411271 base::TimeDelta to_start = base::Time::Now() - request_creation_time_;
[email protected]ec23f522011-02-22 21:01:381272 request_creation_time_ = base::Time();
[email protected]8684a8812011-03-22 13:59:381273
[email protected]5c68d692011-08-24 04:59:411274 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start);
[email protected]ec23f522011-02-22 21:01:381275}
1276
1277void URLRequestHttpJob::ResetTimer() {
1278 if (!request_creation_time_.is_null()) {
1279 NOTREACHED()
1280 << "The timer was reset before it was recorded.";
1281 return;
1282 }
1283 request_creation_time_ = base::Time::Now();
1284}
1285
[email protected]dd29bcd72011-03-24 00:03:441286void URLRequestHttpJob::UpdatePacketReadTimes() {
1287 if (!packet_timing_enabled_)
1288 return;
1289
1290 if (filter_input_byte_count() <= bytes_observed_in_packets_) {
1291 DCHECK_EQ(filter_input_byte_count(), bytes_observed_in_packets_);
1292 return; // No new bytes have arrived.
1293 }
1294
[email protected]d6b55392011-08-05 04:04:351295 final_packet_time_ = base::Time::Now();
[email protected]dd29bcd72011-03-24 00:03:441296 if (!bytes_observed_in_packets_)
1297 request_time_snapshot_ = request_ ? request_->request_time() : base::Time();
1298
[email protected]dd29bcd72011-03-24 00:03:441299 bytes_observed_in_packets_ = filter_input_byte_count();
1300}
1301
1302void URLRequestHttpJob::RecordPacketStats(
1303 FilterContext::StatisticSelector statistic) const {
1304 if (!packet_timing_enabled_ || (final_packet_time_ == base::Time()))
1305 return;
1306
1307 base::TimeDelta duration = final_packet_time_ - request_time_snapshot_;
1308 switch (statistic) {
1309 case FilterContext::SDCH_DECODE: {
[email protected]dd29bcd72011-03-24 00:03:441310 UMA_HISTOGRAM_CUSTOM_COUNTS("Sdch3.Network_Decode_Bytes_Processed_b",
1311 static_cast<int>(bytes_observed_in_packets_), 500, 100000, 100);
[email protected]dd29bcd72011-03-24 00:03:441312 return;
1313 }
1314 case FilterContext::SDCH_PASSTHROUGH: {
1315 // Despite advertising a dictionary, we handled non-sdch compressed
1316 // content.
[email protected]dd29bcd72011-03-24 00:03:441317 return;
1318 }
1319
1320 case FilterContext::SDCH_EXPERIMENT_DECODE: {
[email protected]d6b55392011-08-05 04:04:351321 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment2_Decode",
[email protected]dd29bcd72011-03-24 00:03:441322 duration,
1323 base::TimeDelta::FromMilliseconds(20),
1324 base::TimeDelta::FromMinutes(10), 100);
[email protected]dd29bcd72011-03-24 00:03:441325 return;
1326 }
1327 case FilterContext::SDCH_EXPERIMENT_HOLDBACK: {
[email protected]d6b55392011-08-05 04:04:351328 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment2_Holdback",
[email protected]dd29bcd72011-03-24 00:03:441329 duration,
1330 base::TimeDelta::FromMilliseconds(20),
1331 base::TimeDelta::FromMinutes(10), 100);
[email protected]dd29bcd72011-03-24 00:03:441332 return;
1333 }
1334 default:
1335 NOTREACHED();
1336 return;
1337 }
1338}
1339
1340// The common type of histogram we use for all compression-tracking histograms.
1341#define COMPRESSION_HISTOGRAM(name, sample) \
1342 do { \
1343 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Compress." name, sample, \
1344 500, 1000000, 100); \
[email protected]b7996452011-10-31 19:30:561345 } while (0)
[email protected]dd29bcd72011-03-24 00:03:441346
1347void URLRequestHttpJob::RecordCompressionHistograms() {
1348 DCHECK(request_);
1349 if (!request_)
1350 return;
1351
1352 if (is_cached_content_ || // Don't record cached content
1353 !GetStatus().is_success() || // Don't record failed content
1354 !IsCompressibleContent() || // Only record compressible content
1355 !prefilter_bytes_read()) // Zero-byte responses aren't useful.
1356 return;
1357
1358 // Miniature requests aren't really compressible. Don't count them.
1359 const int kMinSize = 16;
1360 if (prefilter_bytes_read() < kMinSize)
1361 return;
1362
1363 // Only record for http or https urls.
1364 bool is_http = request_->url().SchemeIs("http");
1365 bool is_https = request_->url().SchemeIs("https");
1366 if (!is_http && !is_https)
1367 return;
1368
1369 int compressed_B = prefilter_bytes_read();
1370 int decompressed_B = postfilter_bytes_read();
1371 bool was_filtered = HasFilter();
1372
1373 // We want to record how often downloaded resources are compressed.
1374 // But, we recognize that different protocols may have different
1375 // properties. So, for each request, we'll put it into one of 3
1376 // groups:
1377 // a) SSL resources
1378 // Proxies cannot tamper with compression headers with SSL.
1379 // b) Non-SSL, loaded-via-proxy resources
1380 // In this case, we know a proxy might have interfered.
1381 // c) Non-SSL, loaded-without-proxy resources
1382 // In this case, we know there was no explicit proxy. However,
1383 // it is possible that a transparent proxy was still interfering.
1384 //
1385 // For each group, we record the same 3 histograms.
1386
1387 if (is_https) {
1388 if (was_filtered) {
1389 COMPRESSION_HISTOGRAM("SSL.BytesBeforeCompression", compressed_B);
1390 COMPRESSION_HISTOGRAM("SSL.BytesAfterCompression", decompressed_B);
1391 } else {
1392 COMPRESSION_HISTOGRAM("SSL.ShouldHaveBeenCompressed", decompressed_B);
1393 }
1394 return;
1395 }
1396
1397 if (request_->was_fetched_via_proxy()) {
1398 if (was_filtered) {
1399 COMPRESSION_HISTOGRAM("Proxy.BytesBeforeCompression", compressed_B);
1400 COMPRESSION_HISTOGRAM("Proxy.BytesAfterCompression", decompressed_B);
1401 } else {
1402 COMPRESSION_HISTOGRAM("Proxy.ShouldHaveBeenCompressed", decompressed_B);
1403 }
1404 return;
1405 }
1406
1407 if (was_filtered) {
1408 COMPRESSION_HISTOGRAM("NoProxy.BytesBeforeCompression", compressed_B);
1409 COMPRESSION_HISTOGRAM("NoProxy.BytesAfterCompression", decompressed_B);
1410 } else {
1411 COMPRESSION_HISTOGRAM("NoProxy.ShouldHaveBeenCompressed", decompressed_B);
1412 }
1413}
1414
1415bool URLRequestHttpJob::IsCompressibleContent() const {
1416 std::string mime_type;
1417 return GetMimeType(&mime_type) &&
1418 (IsSupportedJavascriptMimeType(mime_type.c_str()) ||
1419 IsSupportedNonImageMimeType(mime_type.c_str()));
1420}
1421
[email protected]bbaea8f2011-06-24 00:11:011422void URLRequestHttpJob::RecordPerfHistograms(CompletionCause reason) {
1423 if (start_time_.is_null())
1424 return;
1425
1426 base::TimeDelta total_time = base::TimeTicks::Now() - start_time_;
1427 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTime", total_time);
1428
1429 if (reason == FINISHED) {
1430 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeSuccess", total_time);
1431 } else {
1432 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCancel", total_time);
1433 }
1434
[email protected]4b4d20242012-02-23 18:27:461435 if (response_info_) {
1436 if (response_info_->was_cached) {
1437 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCached", total_time);
1438 } else {
1439 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeNotCached", total_time);
[email protected]b73656ca2011-07-22 17:42:171440 }
[email protected]bbaea8f2011-06-24 00:11:011441 }
1442
[email protected]3ed84722013-11-01 17:17:071443 if (request_info_.load_flags & LOAD_PREFETCH && !request_->was_cached())
1444 UMA_HISTOGRAM_COUNTS("Net.Prefetch.PrefilterBytesReadFromNetwork",
1445 prefilter_bytes_read());
1446
[email protected]bbaea8f2011-06-24 00:11:011447 start_time_ = base::TimeTicks();
1448}
1449
1450void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) {
1451 if (done_)
1452 return;
1453 done_ = true;
[email protected]bbaea8f2011-06-24 00:11:011454 RecordPerfHistograms(reason);
[email protected]7a299a92012-10-24 23:54:501455 if (reason == FINISHED) {
1456 request_->set_received_response_content_length(prefilter_bytes_read());
[email protected]bbaea8f2011-06-24 00:11:011457 RecordCompressionHistograms();
[email protected]7a299a92012-10-24 23:54:501458 }
[email protected]bbaea8f2011-06-24 00:11:011459}
1460
[email protected]ea8141e2011-10-05 13:12:511461HttpResponseHeaders* URLRequestHttpJob::GetResponseHeaders() const {
1462 DCHECK(transaction_.get());
1463 DCHECK(transaction_->GetResponseInfo());
1464 return override_response_headers_.get() ?
[email protected]90499482013-06-01 00:39:501465 override_response_headers_.get() :
1466 transaction_->GetResponseInfo()->headers.get();
[email protected]ea8141e2011-10-05 13:12:511467}
1468
1469void URLRequestHttpJob::NotifyURLRequestDestroyed() {
1470 awaiting_callback_ = false;
1471}
1472
[email protected]4f5656c62010-12-13 10:47:091473} // namespace net