Disable signin-to-Chrome when using Guest profile.
[chromium-blink-merge.git] / net / url_request / url_request_http_job.cc
blob7207777517db74bcfff9d1a4abb8ceb80b655dc5
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/url_request/url_request_http_job.h"
7 #include "base/base_switches.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/command_line.h"
11 #include "base/compiler_specific.h"
12 #include "base/file_version_info.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/field_trial.h"
15 #include "base/metrics/histogram.h"
16 #include "base/rand_util.h"
17 #include "base/strings/string_util.h"
18 #include "base/time/time.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/base/load_flags.h"
21 #include "net/base/mime_util.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/net_util.h"
24 #include "net/base/network_delegate.h"
25 #include "net/base/sdch_manager.h"
26 #include "net/cert/cert_status_flags.h"
27 #include "net/cookies/cookie_monster.h"
28 #include "net/http/http_network_session.h"
29 #include "net/http/http_request_headers.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_response_info.h"
32 #include "net/http/http_status_code.h"
33 #include "net/http/http_transaction.h"
34 #include "net/http/http_transaction_factory.h"
35 #include "net/http/http_util.h"
36 #include "net/ssl/ssl_cert_request_info.h"
37 #include "net/ssl/ssl_config_service.h"
38 #include "net/url_request/fraudulent_certificate_reporter.h"
39 #include "net/url_request/http_user_agent_settings.h"
40 #include "net/url_request/url_request.h"
41 #include "net/url_request/url_request_context.h"
42 #include "net/url_request/url_request_error_job.h"
43 #include "net/url_request/url_request_job_factory.h"
44 #include "net/url_request/url_request_redirect_job.h"
45 #include "net/url_request/url_request_throttler_header_adapter.h"
46 #include "net/url_request/url_request_throttler_manager.h"
47 #include "net/websockets/websocket_handshake_stream_base.h"
49 static const char kAvailDictionaryHeader[] = "Avail-Dictionary";
51 namespace net {
53 class URLRequestHttpJob::HttpFilterContext : public FilterContext {
54 public:
55 explicit HttpFilterContext(URLRequestHttpJob* job);
56 virtual ~HttpFilterContext();
58 // FilterContext implementation.
59 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;
69 // 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();
73 private:
74 URLRequestHttpJob* job_;
76 DISALLOW_COPY_AND_ASSIGN(HttpFilterContext);
79 URLRequestHttpJob::HttpFilterContext::HttpFilterContext(URLRequestHttpJob* job)
80 : job_(job) {
81 DCHECK(job_);
84 URLRequestHttpJob::HttpFilterContext::~HttpFilterContext() {
87 bool URLRequestHttpJob::HttpFilterContext::GetMimeType(
88 std::string* mime_type) const {
89 return job_->GetMimeType(mime_type);
92 bool URLRequestHttpJob::HttpFilterContext::GetURL(GURL* gurl) const {
93 if (!job_->request())
94 return false;
95 *gurl = job_->request()->url();
96 return true;
99 base::Time URLRequestHttpJob::HttpFilterContext::GetRequestTime() const {
100 return job_->request() ? job_->request()->request_time() : base::Time();
103 bool URLRequestHttpJob::HttpFilterContext::IsCachedContent() const {
104 return job_->is_cached_content_;
107 bool URLRequestHttpJob::HttpFilterContext::IsDownload() const {
108 return (job_->request_info_.load_flags & LOAD_IS_DOWNLOAD) != 0;
111 void URLRequestHttpJob::HttpFilterContext::ResetSdchResponseToFalse() {
112 DCHECK(job_->sdch_dictionary_advertised_);
113 job_->sdch_dictionary_advertised_ = false;
116 bool URLRequestHttpJob::HttpFilterContext::IsSdchResponse() const {
117 return job_->sdch_dictionary_advertised_;
120 int64 URLRequestHttpJob::HttpFilterContext::GetByteReadCount() const {
121 return job_->filter_input_byte_count();
124 int URLRequestHttpJob::HttpFilterContext::GetResponseCode() const {
125 return job_->GetResponseCode();
128 void URLRequestHttpJob::HttpFilterContext::RecordPacketStats(
129 StatisticSelector statistic) const {
130 job_->RecordPacketStats(statistic);
133 // TODO(darin): make sure the port blocking code is not lost
134 // static
135 URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request,
136 NetworkDelegate* network_delegate,
137 const std::string& scheme) {
138 DCHECK(scheme == "http" || scheme == "https" || scheme == "ws" ||
139 scheme == "wss");
141 if (!request->context()->http_transaction_factory()) {
142 NOTREACHED() << "requires a valid context";
143 return new URLRequestErrorJob(
144 request, network_delegate, ERR_INVALID_ARGUMENT);
147 GURL redirect_url;
148 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.
152 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "HSTS");
154 return new URLRequestHttpJob(request,
155 network_delegate,
156 request->context()->http_user_agent_settings());
159 URLRequestHttpJob::URLRequestHttpJob(
160 URLRequest* request,
161 NetworkDelegate* network_delegate,
162 const HttpUserAgentSettings* http_user_agent_settings)
163 : URLRequestJob(request, network_delegate),
164 priority_(DEFAULT_PRIORITY),
165 response_info_(NULL),
166 response_cookies_save_index_(0),
167 proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
168 server_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
169 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))),
174 read_in_progress_(false),
175 throttling_entry_(NULL),
176 sdch_dictionary_advertised_(false),
177 sdch_test_activated_(false),
178 sdch_test_control_(false),
179 is_cached_content_(false),
180 request_creation_time_(),
181 packet_timing_enabled_(false),
182 done_(false),
183 bytes_observed_in_packets_(0),
184 request_time_snapshot_(),
185 final_packet_time_(),
186 filter_context_(new HttpFilterContext(this)),
187 weak_factory_(this),
188 on_headers_received_callback_(
189 base::Bind(&URLRequestHttpJob::OnHeadersReceivedCallback,
190 base::Unretained(this))),
191 awaiting_callback_(false),
192 http_user_agent_settings_(http_user_agent_settings) {
193 URLRequestThrottlerManager* manager = request->context()->throttler_manager();
194 if (manager)
195 throttling_entry_ = manager->RegisterRequestUrl(request->url());
197 ResetTimer();
200 URLRequestHttpJob::~URLRequestHttpJob() {
201 CHECK(!awaiting_callback_);
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);
210 // Make sure SDCH filters are told to emit histogram data while
211 // filter_context_ is still alive.
212 DestroyFilters();
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_);
228 DoneWithRequest(ABORTED);
231 void URLRequestHttpJob::SetPriority(RequestPriority priority) {
232 priority_ = priority;
233 if (transaction_)
234 transaction_->SetPriority(priority_);
237 void URLRequestHttpJob::Start() {
238 DCHECK(!transaction_.get());
240 // URLRequest::SetReferrer ensures that we do not send username and password
241 // fields in the referrer.
242 GURL referrer(request_->referrer());
244 request_info_.url = request_->url();
245 request_info_.method = request_->method();
246 request_info_.load_flags = request_->load_flags();
247 // 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;
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);
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());
270 request_info_.extra_headers.SetHeaderIfMissing(
271 HttpRequestHeaders::kUserAgent,
272 http_user_agent_settings_ ?
273 http_user_agent_settings_->GetUserAgent() : std::string());
275 AddExtraHeaders();
276 AddCookieHeaderAndStart();
279 void URLRequestHttpJob::Kill() {
280 if (!transaction_.get())
281 return;
283 weak_factory_.InvalidateWeakPtrs();
284 DestroyTransaction();
285 URLRequestJob::Kill();
288 void URLRequestHttpJob::NotifyHeadersComplete() {
289 DCHECK(!response_info_);
291 response_info_ = transaction_->GetResponseInfo();
293 // 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;
297 if (!is_cached_content_ && throttling_entry_.get()) {
298 URLRequestThrottlerHeaderAdapter response_adapter(GetResponseHeaders());
299 throttling_entry_->UpdateWithResponse(request_info_.url.host(),
300 &response_adapter);
303 // The ordering of these calls is not important.
304 ProcessStrictTransportSecurityHeader();
305 ProcessPublicKeyPinsHeader();
307 if (SdchManager::Global() &&
308 SdchManager::Global()->IsInSupportedDomain(request_->url())) {
309 const std::string name = "Get-Dictionary";
310 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).
318 if (GetResponseHeaders()->EnumerateHeader(&iter, name, &url_text)) {
319 // request_->url() won't be valid in the destructor, so we use an
320 // alternate copy.
321 DCHECK_EQ(request_->url(), request_info_.url);
322 // Resolve suggested URL relative to request url.
323 sdch_dictionary_url_ = request_info_.url.Resolve(url_text);
327 // 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());
332 // TODO(battre): This breaks the webrequest API for
333 // URLRequestTestHTTP.BasicAuthWithCookies
334 // where OnBeforeSendHeaders -> OnSendHeaders -> OnBeforeSendHeaders
335 // occurs.
336 RestartTransactionWithAuth(AuthCredentials());
337 return;
340 URLRequestJob::NotifyHeadersComplete();
343 void URLRequestHttpJob::NotifyDone(const URLRequestStatus& status) {
344 DoneWithRequest(FINISHED);
345 URLRequestJob::NotifyDone(status);
348 void URLRequestHttpJob::DestroyTransaction() {
349 DCHECK(transaction_.get());
351 DoneWithRequest(ABORTED);
352 transaction_.reset();
353 response_info_ = NULL;
354 receive_headers_end_ = base::TimeTicks();
357 void URLRequestHttpJob::StartTransaction() {
358 if (network_delegate()) {
359 OnCallToDelegate();
360 int rv = network_delegate()->NotifyBeforeSendHeaders(
361 request_, notify_before_headers_sent_callback_,
362 &request_info_.extra_headers);
363 // If an extension blocks the request, we rely on the callback to
364 // MaybeStartTransactionInternal().
365 if (rv == ERR_IO_PENDING)
366 return;
367 MaybeStartTransactionInternal(rv);
368 return;
370 StartTransactionInternal();
373 void URLRequestHttpJob::NotifyBeforeSendHeadersCallback(int result) {
374 // Check that there are no callbacks to already canceled requests.
375 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
377 MaybeStartTransactionInternal(result);
380 void URLRequestHttpJob::MaybeStartTransactionInternal(int result) {
381 OnCallToDelegateComplete();
382 if (result == OK) {
383 StartTransactionInternal();
384 } else {
385 std::string source("delegate");
386 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
387 NetLog::StringCallback("source", &source));
388 NotifyCanceled();
389 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
393 void URLRequestHttpJob::StartTransactionInternal() {
394 // NOTE: This method assumes that request_info_ is already setup properly.
396 // If we already have a transaction, then we should restart the transaction
397 // with auth provided by auth_credentials_.
399 int rv;
401 if (network_delegate()) {
402 network_delegate()->NotifySendHeaders(
403 request_, request_info_.extra_headers);
406 if (transaction_.get()) {
407 rv = transaction_->RestartWithAuth(auth_credentials_, start_callback_);
408 auth_credentials_ = AuthCredentials();
409 } else {
410 DCHECK(request_->context()->http_transaction_factory());
412 rv = request_->context()->http_transaction_factory()->CreateTransaction(
413 priority_, &transaction_);
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;
428 if (rv == OK) {
429 transaction_->SetBeforeNetworkStartCallback(
430 base::Bind(&URLRequestHttpJob::NotifyBeforeNetworkStart,
431 base::Unretained(this)));
433 if (!throttling_entry_.get() ||
434 !throttling_entry_->ShouldRejectRequest(*request_)) {
435 rv = transaction_->Start(
436 &request_info_, start_callback_, request_->net_log());
437 start_time_ = base::TimeTicks::Now();
438 } else {
439 // Special error code for the exponential back-off module.
440 rv = ERR_TEMPORARILY_THROTTLED;
445 if (rv == ERR_IO_PENDING)
446 return;
448 // The transaction started synchronously, but we need to notify the
449 // URLRequest delegate via the message loop.
450 base::MessageLoop::current()->PostTask(
451 FROM_HERE,
452 base::Bind(&URLRequestHttpJob::OnStartCompleted,
453 weak_factory_.GetWeakPtr(), rv));
456 void URLRequestHttpJob::AddExtraHeaders() {
457 // 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);
473 // 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;
492 // 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).
499 request_info_.extra_headers.SetHeader(
500 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;
520 if (http_user_agent_settings_) {
521 // Only add default Accept-Language if the request didn't have it
522 // specified.
523 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);
533 void 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));
538 // If the request was destroyed, then there is no more work to do.
539 if (!request_)
540 return;
542 CookieStore* cookie_store = GetCookieStore();
543 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,
549 weak_factory_.GetWeakPtr()));
550 } else {
551 CheckCookiePolicyAndLoad(CookieList());
553 } else {
554 DoStartTransaction();
558 void URLRequestHttpJob::DoLoadCookies() {
559 CookieOptions options;
560 options.set_include_httponly();
561 GetCookieStore()->GetCookiesWithOptionsAsync(
562 request_->url(), options,
563 base::Bind(&URLRequestHttpJob::OnCookiesLoaded,
564 weak_factory_.GetWeakPtr()));
567 void URLRequestHttpJob::CheckCookiePolicyAndLoad(
568 const CookieList& cookie_list) {
569 if (CanGetCookies(cookie_list))
570 DoLoadCookies();
571 else
572 DoStartTransaction();
575 void URLRequestHttpJob::OnCookiesLoaded(const std::string& cookie_line) {
576 if (!cookie_line.empty()) {
577 request_info_.extra_headers.SetHeader(
578 HttpRequestHeaders::kCookie, cookie_line);
579 // Disable privacy mode as we are sending cookies anyway.
580 request_info_.privacy_mode = kPrivacyModeDisabled;
582 DoStartTransaction();
585 void URLRequestHttpJob::DoStartTransaction() {
586 // We may have been canceled while retrieving cookies.
587 if (GetStatus().is_success()) {
588 StartTransaction();
589 } else {
590 NotifyCanceled();
594 void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
595 // End of the call started in OnStartCompleted.
596 OnCallToDelegateComplete();
598 if (result != net::OK) {
599 std::string source("delegate");
600 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
601 NetLog::StringCallback("source", &source));
602 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
603 return;
606 DCHECK(transaction_.get());
608 const HttpResponseInfo* response_info = transaction_->GetResponseInfo();
609 DCHECK(response_info);
611 response_cookies_.clear();
612 response_cookies_save_index_ = 0;
614 FetchResponseCookies(&response_cookies_);
616 if (!GetResponseHeaders()->GetDateValue(&response_date_))
617 response_date_ = base::Time();
619 // Now, loop over the response cookies, and attempt to persist each.
620 SaveNextCookie();
623 // 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.
629 void URLRequestHttpJob::SaveNextCookie() {
630 // 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));
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);
640 if (!(request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) &&
641 GetCookieStore() && response_cookies_.size() > 0) {
642 CookieOptions options;
643 options.set_include_httponly();
644 options.set_server_time(response_date_);
646 net::CookieStore::SetCookiesCallback callback(
647 base::Bind(&URLRequestHttpJob::OnCookieSaved,
648 weak_factory_.GetWeakPtr(),
649 save_next_cookie_running,
650 callback_pending));
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;
659 GetCookieStore()->SetCookieWithOptionsAsync(
660 request_->url(), response_cookies_[response_cookies_save_index_],
661 options, callback);
663 ++response_cookies_save_index_;
667 save_next_cookie_running->data = false;
669 if (!callback_pending->data) {
670 response_cookies_.clear();
671 response_cookies_save_index_ = 0;
672 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
673 NotifyHeadersComplete();
674 return;
678 // |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.
685 void 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;
691 // If we were called synchronously, return.
692 if (save_next_cookie_running->data) {
693 return;
696 // We were called asynchronously, so trigger the next save.
697 // We may have been canceled within OnSetCookie.
698 if (GetStatus().is_success()) {
699 SaveNextCookie();
700 } else {
701 NotifyCanceled();
705 void URLRequestHttpJob::FetchResponseCookies(
706 std::vector<std::string>* cookies) {
707 const std::string name = "Set-Cookie";
708 std::string value;
710 void* iter = NULL;
711 HttpResponseHeaders* headers = GetResponseHeaders();
712 while (headers->EnumerateHeader(&iter, name, &value)) {
713 if (!value.empty())
714 cookies->push_back(value);
718 // NOTE: |ProcessStrictTransportSecurityHeader| and
719 // |ProcessPublicKeyPinsHeader| have very similar structures, by design.
720 void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() {
721 DCHECK(response_info_);
722 TransportSecurityState* security_state =
723 request_->context()->transport_security_state();
724 const SSLInfo& ssl_info = response_info_->ssl_info;
726 // Only accept HSTS headers on HTTPS connections that have no
727 // certificate errors.
728 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
729 !security_state)
730 return;
732 // http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec:
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.
737 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);
743 void URLRequestHttpJob::ProcessPublicKeyPinsHeader() {
744 DCHECK(response_info_);
745 TransportSecurityState* security_state =
746 request_->context()->transport_security_state();
747 const SSLInfo& ssl_info = response_info_->ssl_info;
749 // Only accept HPKP headers on HTTPS connections that have no
750 // certificate errors.
751 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
752 !security_state)
753 return;
755 // http://tools.ietf.org/html/draft-ietf-websec-key-pinning:
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.
760 HttpResponseHeaders* headers = GetResponseHeaders();
761 std::string value;
762 if (headers->EnumerateHeader(NULL, "Public-Key-Pins", &value))
763 security_state->AddHPKPHeader(request_info_.url.host(), value, ssl_info);
766 void URLRequestHttpJob::OnStartCompleted(int result) {
767 RecordTimer();
769 // If the request was destroyed, then there is no more work to do.
770 if (!request_)
771 return;
773 // If the job is done (due to cancellation), can just ignore this
774 // notification.
775 if (done_)
776 return;
778 receive_headers_end_ = base::TimeTicks::Now();
780 // Clear the IO_PENDING status
781 SetStatus(URLRequestStatus());
783 const URLRequestContext* context = request_->context();
785 if (result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN &&
786 transaction_->GetResponseInfo() != NULL) {
787 FraudulentCertificateReporter* reporter =
788 context->fraudulent_certificate_reporter();
789 if (reporter != NULL) {
790 const SSLInfo& ssl_info = transaction_->GetResponseInfo()->ssl_info;
791 bool sni_available = SSLConfigService::IsSNIAvailable(
792 context->ssl_config_service());
793 const std::string& host = request_->url().host();
795 reporter->SendReport(host, ssl_info, sni_available);
799 if (result == OK) {
800 scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders();
801 if (network_delegate()) {
802 // Note that |this| may not be deleted until
803 // |on_headers_received_callback_| or
804 // |NetworkDelegate::URLRequestDestroyed()| has been called.
805 OnCallToDelegate();
806 int error = network_delegate()->NotifyHeadersReceived(
807 request_,
808 on_headers_received_callback_,
809 headers.get(),
810 &override_response_headers_);
811 if (error != net::OK) {
812 if (error == net::ERR_IO_PENDING) {
813 awaiting_callback_ = true;
814 } else {
815 std::string source("delegate");
816 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
817 NetLog::StringCallback("source",
818 &source));
819 OnCallToDelegateComplete();
820 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, error));
822 return;
826 SaveCookiesAndNotifyHeadersComplete(net::OK);
827 } else if (IsCertificateError(result)) {
828 // 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);
849 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
850 NotifyCertificateRequested(
851 transaction_->GetResponseInfo()->cert_request_info.get());
852 } else {
853 // 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();
857 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
861 void URLRequestHttpJob::OnHeadersReceivedCallback(int result) {
862 awaiting_callback_ = false;
864 // Check that there are no callbacks to already canceled requests.
865 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
867 SaveCookiesAndNotifyHeadersComplete(result);
870 void URLRequestHttpJob::OnReadCompleted(int result) {
871 read_in_progress_ = false;
873 if (ShouldFixMismatchedContentLength(result))
874 result = OK;
876 if (result == OK) {
877 NotifyDone(URLRequestStatus());
878 } else if (result < 0) {
879 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
880 } else {
881 // Clear the IO_PENDING status
882 SetStatus(URLRequestStatus());
885 NotifyReadComplete(result);
888 void URLRequestHttpJob::RestartTransactionWithAuth(
889 const AuthCredentials& credentials) {
890 auth_credentials_ = credentials;
892 // These will be reset in OnStartCompleted.
893 response_info_ = NULL;
894 receive_headers_end_ = base::TimeTicks();
895 response_cookies_.clear();
897 ResetTimer();
899 // 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.
902 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kCookie);
904 AddCookieHeaderAndStart();
907 void URLRequestHttpJob::SetUpload(UploadDataStream* upload) {
908 DCHECK(!transaction_.get()) << "cannot change once started";
909 request_info_.upload_data_stream = upload;
912 void URLRequestHttpJob::SetExtraRequestHeaders(
913 const HttpRequestHeaders& headers) {
914 DCHECK(!transaction_.get()) << "cannot change once started";
915 request_info_.extra_headers.CopyFrom(headers);
918 LoadState URLRequestHttpJob::GetLoadState() const {
919 return transaction_.get() ?
920 transaction_->GetLoadState() : LOAD_STATE_IDLE;
923 UploadProgress URLRequestHttpJob::GetUploadProgress() const {
924 return transaction_.get() ?
925 transaction_->GetUploadProgress() : UploadProgress();
928 bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const {
929 DCHECK(transaction_.get());
931 if (!response_info_)
932 return false;
934 return GetResponseHeaders()->GetMimeType(mime_type);
937 bool URLRequestHttpJob::GetCharset(std::string* charset) {
938 DCHECK(transaction_.get());
940 if (!response_info_)
941 return false;
943 return GetResponseHeaders()->GetCharset(charset);
946 void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) {
947 DCHECK(request_);
949 if (response_info_) {
950 DCHECK(transaction_.get());
952 *info = *response_info_;
953 if (override_response_headers_.get())
954 info->headers = override_response_headers_;
958 void URLRequestHttpJob::GetLoadTimingInfo(
959 LoadTimingInfo* load_timing_info) const {
960 // 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_;
968 bool URLRequestHttpJob::GetResponseCookies(std::vector<std::string>* cookies) {
969 DCHECK(transaction_.get());
971 if (!response_info_)
972 return false;
974 // TODO(darin): Why are we extracting response cookies again? Perhaps we
975 // should just leverage response_cookies_.
977 cookies->clear();
978 FetchResponseCookies(cookies);
979 return true;
982 int URLRequestHttpJob::GetResponseCode() const {
983 DCHECK(transaction_.get());
985 if (!response_info_)
986 return -1;
988 return GetResponseHeaders()->response_code();
991 Filter* URLRequestHttpJob::SetupFilter() const {
992 DCHECK(transaction_.get());
993 if (!response_info_)
994 return NULL;
996 std::vector<Filter::FilterType> encoding_types;
997 std::string encoding_type;
998 HttpResponseHeaders* headers = GetResponseHeaders();
999 void* iter = NULL;
1000 while (headers->EnumerateHeader(&iter, "Content-Encoding", &encoding_type)) {
1001 encoding_types.push_back(Filter::ConvertEncodingToType(encoding_type));
1004 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;
1012 while (headers->EnumerateHeader(&iter, "X-Sdch-Encode",
1013 &sdch_response_status)) {
1014 if (sdch_response_status == "0") {
1015 filter_context_->ResetSdchResponseToFalse();
1016 break;
1021 // 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).
1025 Filter::FixupEncodingTypes(*filter_context_, &encoding_types);
1027 return !encoding_types.empty()
1028 ? Filter::Factory(encoding_types, *filter_context_) : NULL;
1031 bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) {
1032 // 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")) {
1036 return true;
1038 // 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);
1044 bool URLRequestHttpJob::NeedsAuth() {
1045 int code = GetResponseCode();
1046 if (code == -1)
1047 return false;
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;
1063 return false;
1066 void URLRequestHttpJob::GetAuthChallengeInfo(
1067 scoped_refptr<AuthChallengeInfo>* result) {
1068 DCHECK(transaction_.get());
1069 DCHECK(response_info_);
1071 // sanity checks:
1072 DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH ||
1073 server_auth_state_ == AUTH_STATE_NEED_AUTH);
1074 DCHECK((GetResponseHeaders()->response_code() == HTTP_UNAUTHORIZED) ||
1075 (GetResponseHeaders()->response_code() ==
1076 HTTP_PROXY_AUTHENTICATION_REQUIRED));
1078 *result = response_info_->auth_challenge;
1081 void URLRequestHttpJob::SetAuth(const AuthCredentials& credentials) {
1082 DCHECK(transaction_.get());
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 {
1088 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
1089 server_auth_state_ = AUTH_STATE_HAVE_AUTH;
1092 RestartTransactionWithAuth(credentials);
1095 void 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 {
1100 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
1101 server_auth_state_ = AUTH_STATE_CANCELED;
1104 // These will be reset in OnStartCompleted.
1105 response_info_ = NULL;
1106 receive_headers_end_ = base::TimeTicks::Now();
1107 response_cookies_.clear();
1109 ResetTimer();
1111 // OK, let the consumer read the error page...
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.
1117 // We have to do this via InvokeLater to avoid "recursing" the consumer.
1119 base::MessageLoop::current()->PostTask(
1120 FROM_HERE,
1121 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1122 weak_factory_.GetWeakPtr(), OK));
1125 void URLRequestHttpJob::ContinueWithCertificate(
1126 X509Certificate* client_cert) {
1127 DCHECK(transaction_.get());
1129 DCHECK(!response_info_) << "should not have a response yet";
1130 receive_headers_end_ = base::TimeTicks();
1132 ResetTimer();
1134 // 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));
1138 int rv = transaction_->RestartWithCertificate(client_cert, start_callback_);
1139 if (rv == ERR_IO_PENDING)
1140 return;
1142 // The transaction started synchronously, but we need to notify the
1143 // URLRequest delegate via the message loop.
1144 base::MessageLoop::current()->PostTask(
1145 FROM_HERE,
1146 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1147 weak_factory_.GetWeakPtr(), rv));
1150 void URLRequestHttpJob::ContinueDespiteLastError() {
1151 // If the transaction was destroyed, then the job was cancelled.
1152 if (!transaction_.get())
1153 return;
1155 DCHECK(!response_info_) << "should not have a response yet";
1156 receive_headers_end_ = base::TimeTicks();
1158 ResetTimer();
1160 // 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));
1164 int rv = transaction_->RestartIgnoringLastError(start_callback_);
1165 if (rv == ERR_IO_PENDING)
1166 return;
1168 // The transaction started synchronously, but we need to notify the
1169 // URLRequest delegate via the message loop.
1170 base::MessageLoop::current()->PostTask(
1171 FROM_HERE,
1172 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1173 weak_factory_.GetWeakPtr(), rv));
1176 void URLRequestHttpJob::ResumeNetworkStart() {
1177 DCHECK(transaction_.get());
1178 transaction_->ResumeNetworkStart();
1181 bool 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.
1186 if (rv == net::ERR_CONTENT_LENGTH_MISMATCH ||
1187 rv == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
1188 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;
1201 return false;
1204 bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size,
1205 int* bytes_read) {
1206 DCHECK_NE(buf_size, 0);
1207 DCHECK(bytes_read);
1208 DCHECK(!read_in_progress_);
1210 int rv = transaction_->Read(
1211 buf, buf_size,
1212 base::Bind(&URLRequestHttpJob::OnReadCompleted, base::Unretained(this)));
1214 if (ShouldFixMismatchedContentLength(rv))
1215 rv = 0;
1217 if (rv >= 0) {
1218 *bytes_read = rv;
1219 if (!rv)
1220 DoneWithRequest(FINISHED);
1221 return true;
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));
1231 return false;
1234 void URLRequestHttpJob::StopCaching() {
1235 if (transaction_.get())
1236 transaction_->StopCaching();
1239 bool URLRequestHttpJob::GetFullRequestHeaders(
1240 HttpRequestHeaders* headers) const {
1241 if (!transaction_)
1242 return false;
1244 return transaction_->GetFullRequestHeaders(headers);
1247 int64 URLRequestHttpJob::GetTotalReceivedBytes() const {
1248 if (!transaction_)
1249 return 0;
1251 return transaction_->GetTotalReceivedBytes();
1254 void URLRequestHttpJob::DoneReading() {
1255 if (transaction_.get())
1256 transaction_->DoneReading();
1257 DoneWithRequest(FINISHED);
1260 HostPortPair URLRequestHttpJob::GetSocketAddress() const {
1261 return response_info_ ? response_info_->socket_address : HostPortPair();
1264 void URLRequestHttpJob::RecordTimer() {
1265 if (request_creation_time_.is_null()) {
1266 NOTREACHED()
1267 << "The same transaction shouldn't start twice without new timing.";
1268 return;
1271 base::TimeDelta to_start = base::Time::Now() - request_creation_time_;
1272 request_creation_time_ = base::Time();
1274 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start);
1277 void URLRequestHttpJob::ResetTimer() {
1278 if (!request_creation_time_.is_null()) {
1279 NOTREACHED()
1280 << "The timer was reset before it was recorded.";
1281 return;
1283 request_creation_time_ = base::Time::Now();
1286 void URLRequestHttpJob::UpdatePacketReadTimes() {
1287 if (!packet_timing_enabled_)
1288 return;
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.
1295 final_packet_time_ = base::Time::Now();
1296 if (!bytes_observed_in_packets_)
1297 request_time_snapshot_ = request_ ? request_->request_time() : base::Time();
1299 bytes_observed_in_packets_ = filter_input_byte_count();
1302 void URLRequestHttpJob::RecordPacketStats(
1303 FilterContext::StatisticSelector statistic) const {
1304 if (!packet_timing_enabled_ || (final_packet_time_ == base::Time()))
1305 return;
1307 base::TimeDelta duration = final_packet_time_ - request_time_snapshot_;
1308 switch (statistic) {
1309 case FilterContext::SDCH_DECODE: {
1310 UMA_HISTOGRAM_CUSTOM_COUNTS("Sdch3.Network_Decode_Bytes_Processed_b",
1311 static_cast<int>(bytes_observed_in_packets_), 500, 100000, 100);
1312 return;
1314 case FilterContext::SDCH_PASSTHROUGH: {
1315 // Despite advertising a dictionary, we handled non-sdch compressed
1316 // content.
1317 return;
1320 case FilterContext::SDCH_EXPERIMENT_DECODE: {
1321 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment2_Decode",
1322 duration,
1323 base::TimeDelta::FromMilliseconds(20),
1324 base::TimeDelta::FromMinutes(10), 100);
1325 return;
1327 case FilterContext::SDCH_EXPERIMENT_HOLDBACK: {
1328 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment2_Holdback",
1329 duration,
1330 base::TimeDelta::FromMilliseconds(20),
1331 base::TimeDelta::FromMinutes(10), 100);
1332 return;
1334 default:
1335 NOTREACHED();
1336 return;
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); \
1345 } while (0)
1347 void URLRequestHttpJob::RecordCompressionHistograms() {
1348 DCHECK(request_);
1349 if (!request_)
1350 return;
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;
1358 // Miniature requests aren't really compressible. Don't count them.
1359 const int kMinSize = 16;
1360 if (prefilter_bytes_read() < kMinSize)
1361 return;
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;
1369 int compressed_B = prefilter_bytes_read();
1370 int decompressed_B = postfilter_bytes_read();
1371 bool was_filtered = HasFilter();
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.
1385 // For each group, we record the same 3 histograms.
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);
1394 return;
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);
1404 return;
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);
1415 bool 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()));
1422 void URLRequestHttpJob::RecordPerfHistograms(CompletionCause reason) {
1423 if (start_time_.is_null())
1424 return;
1426 base::TimeDelta total_time = base::TimeTicks::Now() - start_time_;
1427 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTime", total_time);
1429 if (reason == FINISHED) {
1430 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeSuccess", total_time);
1431 } else {
1432 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCancel", total_time);
1435 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);
1443 if (request_info_.load_flags & LOAD_PREFETCH && !request_->was_cached())
1444 UMA_HISTOGRAM_COUNTS("Net.Prefetch.PrefilterBytesReadFromNetwork",
1445 prefilter_bytes_read());
1447 start_time_ = base::TimeTicks();
1450 void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) {
1451 if (done_)
1452 return;
1453 done_ = true;
1454 RecordPerfHistograms(reason);
1455 if (reason == FINISHED) {
1456 request_->set_received_response_content_length(prefilter_bytes_read());
1457 RecordCompressionHistograms();
1461 HttpResponseHeaders* URLRequestHttpJob::GetResponseHeaders() const {
1462 DCHECK(transaction_.get());
1463 DCHECK(transaction_->GetResponseInfo());
1464 return override_response_headers_.get() ?
1465 override_response_headers_.get() :
1466 transaction_->GetResponseInfo()->headers.get();
1469 void URLRequestHttpJob::NotifyURLRequestDestroyed() {
1470 awaiting_callback_ = false;
1473 } // namespace net