Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / url_request / url_request_http_job.cc
blob91ba0fb50709c305a4da7a9b75169cd1aaa90504
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/profiler/scoped_tracker.h"
17 #include "base/rand_util.h"
18 #include "base/strings/string_util.h"
19 #include "base/time/time.h"
20 #include "net/base/host_port_pair.h"
21 #include "net/base/load_flags.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/base/sdch_net_log_params.h"
27 #include "net/cert/cert_status_flags.h"
28 #include "net/cookies/cookie_store.h"
29 #include "net/http/http_content_disposition.h"
30 #include "net/http/http_network_session.h"
31 #include "net/http/http_request_headers.h"
32 #include "net/http/http_response_headers.h"
33 #include "net/http/http_response_info.h"
34 #include "net/http/http_status_code.h"
35 #include "net/http/http_transaction.h"
36 #include "net/http/http_transaction_factory.h"
37 #include "net/http/http_util.h"
38 #include "net/proxy/proxy_info.h"
39 #include "net/ssl/ssl_cert_request_info.h"
40 #include "net/ssl/ssl_config_service.h"
41 #include "net/url_request/fraudulent_certificate_reporter.h"
42 #include "net/url_request/http_user_agent_settings.h"
43 #include "net/url_request/url_request.h"
44 #include "net/url_request/url_request_context.h"
45 #include "net/url_request/url_request_error_job.h"
46 #include "net/url_request/url_request_job_factory.h"
47 #include "net/url_request/url_request_redirect_job.h"
48 #include "net/url_request/url_request_throttler_header_adapter.h"
49 #include "net/url_request/url_request_throttler_manager.h"
50 #include "net/websockets/websocket_handshake_stream_base.h"
52 static const char kAvailDictionaryHeader[] = "Avail-Dictionary";
54 namespace net {
56 class URLRequestHttpJob::HttpFilterContext : public FilterContext {
57 public:
58 explicit HttpFilterContext(URLRequestHttpJob* job);
59 ~HttpFilterContext() override;
61 // FilterContext implementation.
62 bool GetMimeType(std::string* mime_type) const override;
63 bool GetURL(GURL* gurl) const override;
64 base::Time GetRequestTime() const override;
65 bool IsCachedContent() const override;
66 SdchManager::DictionarySet* SdchDictionariesAdvertised() const override;
67 int64 GetByteReadCount() const override;
68 int GetResponseCode() const override;
69 const URLRequestContext* GetURLRequestContext() const override;
70 void RecordPacketStats(StatisticSelector statistic) const override;
71 const BoundNetLog& GetNetLog() const override;
73 private:
74 URLRequestHttpJob* job_;
76 // URLRequestHttpJob may be detached from URLRequest, but we still need to
77 // return something.
78 BoundNetLog dummy_log_;
80 DISALLOW_COPY_AND_ASSIGN(HttpFilterContext);
83 URLRequestHttpJob::HttpFilterContext::HttpFilterContext(URLRequestHttpJob* job)
84 : job_(job) {
85 DCHECK(job_);
88 URLRequestHttpJob::HttpFilterContext::~HttpFilterContext() {
91 bool URLRequestHttpJob::HttpFilterContext::GetMimeType(
92 std::string* mime_type) const {
93 return job_->GetMimeType(mime_type);
96 bool URLRequestHttpJob::HttpFilterContext::GetURL(GURL* gurl) const {
97 if (!job_->request())
98 return false;
99 *gurl = job_->request()->url();
100 return true;
103 base::Time URLRequestHttpJob::HttpFilterContext::GetRequestTime() const {
104 return job_->request() ? job_->request()->request_time() : base::Time();
107 bool URLRequestHttpJob::HttpFilterContext::IsCachedContent() const {
108 return job_->is_cached_content_;
111 SdchManager::DictionarySet*
112 URLRequestHttpJob::HttpFilterContext::SdchDictionariesAdvertised() const {
113 return job_->dictionaries_advertised_.get();
116 int64 URLRequestHttpJob::HttpFilterContext::GetByteReadCount() const {
117 return job_->prefilter_bytes_read();
120 int URLRequestHttpJob::HttpFilterContext::GetResponseCode() const {
121 return job_->GetResponseCode();
124 const URLRequestContext*
125 URLRequestHttpJob::HttpFilterContext::GetURLRequestContext() const {
126 return job_->request() ? job_->request()->context() : NULL;
129 void URLRequestHttpJob::HttpFilterContext::RecordPacketStats(
130 StatisticSelector statistic) const {
131 job_->RecordPacketStats(statistic);
134 const BoundNetLog& URLRequestHttpJob::HttpFilterContext::GetNetLog() const {
135 return job_->request() ? job_->request()->net_log() : dummy_log_;
138 // TODO(darin): make sure the port blocking code is not lost
139 // static
140 URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request,
141 NetworkDelegate* network_delegate,
142 const std::string& scheme) {
143 DCHECK(scheme == "http" || scheme == "https" || scheme == "ws" ||
144 scheme == "wss");
146 if (!request->context()->http_transaction_factory()) {
147 NOTREACHED() << "requires a valid context";
148 return new URLRequestErrorJob(
149 request, network_delegate, ERR_INVALID_ARGUMENT);
152 GURL redirect_url;
153 if (request->GetHSTSRedirect(&redirect_url)) {
154 return new URLRequestRedirectJob(
155 request, network_delegate, redirect_url,
156 // Use status code 307 to preserve the method, so POST requests work.
157 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "HSTS");
159 return new URLRequestHttpJob(request,
160 network_delegate,
161 request->context()->http_user_agent_settings());
164 URLRequestHttpJob::URLRequestHttpJob(
165 URLRequest* request,
166 NetworkDelegate* network_delegate,
167 const HttpUserAgentSettings* http_user_agent_settings)
168 : URLRequestJob(request, network_delegate),
169 priority_(DEFAULT_PRIORITY),
170 response_info_(NULL),
171 response_cookies_save_index_(0),
172 proxy_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
173 server_auth_state_(AUTH_STATE_DONT_NEED_AUTH),
174 start_callback_(base::Bind(&URLRequestHttpJob::OnStartCompleted,
175 base::Unretained(this))),
176 notify_before_headers_sent_callback_(
177 base::Bind(&URLRequestHttpJob::NotifyBeforeSendHeadersCallback,
178 base::Unretained(this))),
179 read_in_progress_(false),
180 throttling_entry_(NULL),
181 sdch_test_activated_(false),
182 sdch_test_control_(false),
183 is_cached_content_(false),
184 request_creation_time_(),
185 packet_timing_enabled_(false),
186 done_(false),
187 bytes_observed_in_packets_(0),
188 request_time_snapshot_(),
189 final_packet_time_(),
190 filter_context_(new HttpFilterContext(this)),
191 on_headers_received_callback_(
192 base::Bind(&URLRequestHttpJob::OnHeadersReceivedCallback,
193 base::Unretained(this))),
194 awaiting_callback_(false),
195 http_user_agent_settings_(http_user_agent_settings),
196 weak_factory_(this) {
197 URLRequestThrottlerManager* manager = request->context()->throttler_manager();
198 if (manager)
199 throttling_entry_ = manager->RegisterRequestUrl(request->url());
201 ResetTimer();
204 URLRequestHttpJob::~URLRequestHttpJob() {
205 CHECK(!awaiting_callback_);
207 DCHECK(!sdch_test_control_ || !sdch_test_activated_);
208 if (!is_cached_content_) {
209 if (sdch_test_control_)
210 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_HOLDBACK);
211 if (sdch_test_activated_)
212 RecordPacketStats(FilterContext::SDCH_EXPERIMENT_DECODE);
214 // Make sure SDCH filters are told to emit histogram data while
215 // filter_context_ is still alive.
216 DestroyFilters();
218 DoneWithRequest(ABORTED);
221 void URLRequestHttpJob::SetPriority(RequestPriority priority) {
222 priority_ = priority;
223 if (transaction_)
224 transaction_->SetPriority(priority_);
227 void URLRequestHttpJob::Start() {
228 DCHECK(!transaction_.get());
230 // URLRequest::SetReferrer ensures that we do not send username and password
231 // fields in the referrer.
232 GURL referrer(request_->referrer());
234 request_info_.url = request_->url();
235 request_info_.method = request_->method();
236 request_info_.load_flags = request_->load_flags();
237 // Enable privacy mode if cookie settings or flags tell us not send or
238 // save cookies.
239 bool enable_privacy_mode =
240 (request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES) ||
241 (request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) ||
242 CanEnablePrivacyMode();
243 // Privacy mode could still be disabled in OnCookiesLoaded if we are going
244 // to send previously saved cookies.
245 request_info_.privacy_mode = enable_privacy_mode ?
246 PRIVACY_MODE_ENABLED : PRIVACY_MODE_DISABLED;
248 // Strip Referer from request_info_.extra_headers to prevent, e.g., plugins
249 // from overriding headers that are controlled using other means. Otherwise a
250 // plugin could set a referrer although sending the referrer is inhibited.
251 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kReferer);
253 // Our consumer should have made sure that this is a safe referrer. See for
254 // instance WebCore::FrameLoader::HideReferrer.
255 if (referrer.is_valid()) {
256 request_info_.extra_headers.SetHeader(HttpRequestHeaders::kReferer,
257 referrer.spec());
260 request_info_.extra_headers.SetHeaderIfMissing(
261 HttpRequestHeaders::kUserAgent,
262 http_user_agent_settings_ ?
263 http_user_agent_settings_->GetUserAgent() : std::string());
265 AddExtraHeaders();
266 AddCookieHeaderAndStart();
269 void URLRequestHttpJob::Kill() {
270 if (!transaction_.get())
271 return;
273 weak_factory_.InvalidateWeakPtrs();
274 DestroyTransaction();
275 URLRequestJob::Kill();
278 void URLRequestHttpJob::NotifyBeforeSendProxyHeadersCallback(
279 const ProxyInfo& proxy_info,
280 HttpRequestHeaders* request_headers) {
281 DCHECK(request_headers);
282 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
283 if (network_delegate()) {
284 network_delegate()->NotifyBeforeSendProxyHeaders(
285 request_,
286 proxy_info,
287 request_headers);
291 void URLRequestHttpJob::NotifyHeadersComplete() {
292 DCHECK(!response_info_);
294 response_info_ = transaction_->GetResponseInfo();
296 // Save boolean, as we'll need this info at destruction time, and filters may
297 // also need this info.
298 is_cached_content_ = response_info_->was_cached;
300 if (!is_cached_content_ && throttling_entry_.get()) {
301 URLRequestThrottlerHeaderAdapter response_adapter(GetResponseHeaders());
302 throttling_entry_->UpdateWithResponse(request_info_.url.host(),
303 &response_adapter);
306 // The ordering of these calls is not important.
307 ProcessStrictTransportSecurityHeader();
308 ProcessPublicKeyPinsHeader();
310 // Handle the server notification of a new SDCH dictionary.
311 SdchManager* sdch_manager(request()->context()->sdch_manager());
312 if (sdch_manager) {
313 SdchProblemCode rv = sdch_manager->IsInSupportedDomain(request()->url());
314 if (rv != SDCH_OK) {
315 // If SDCH is just disabled, it is not a real error.
316 if (rv != SDCH_DISABLED && rv != SDCH_SECURE_SCHEME_NOT_SUPPORTED) {
317 SdchManager::SdchErrorRecovery(rv);
318 request()->net_log().AddEvent(
319 NetLog::TYPE_SDCH_DECODING_ERROR,
320 base::Bind(&NetLogSdchResourceProblemCallback, rv));
322 } else {
323 const std::string name = "Get-Dictionary";
324 std::string url_text;
325 void* iter = NULL;
326 // TODO(jar): We need to not fetch dictionaries the first time they are
327 // seen, but rather wait until we can justify their usefulness.
328 // For now, we will only fetch the first dictionary, which will at least
329 // require multiple suggestions before we get additional ones for this
330 // site. Eventually we should wait until a dictionary is requested
331 // several times
332 // before we even download it (so that we don't waste memory or
333 // bandwidth).
334 if (GetResponseHeaders()->EnumerateHeader(&iter, name, &url_text)) {
335 // Resolve suggested URL relative to request url.
336 GURL sdch_dictionary_url = request_->url().Resolve(url_text);
337 if (sdch_dictionary_url.is_valid()) {
338 rv = sdch_manager->OnGetDictionary(request_->url(),
339 sdch_dictionary_url);
340 if (rv != SDCH_OK) {
341 SdchManager::SdchErrorRecovery(rv);
342 request_->net_log().AddEvent(
343 NetLog::TYPE_SDCH_DICTIONARY_ERROR,
344 base::Bind(&NetLogSdchDictionaryFetchProblemCallback, rv,
345 sdch_dictionary_url, false));
352 // Handle the server signalling no SDCH encoding.
353 if (dictionaries_advertised_) {
354 // We are wary of proxies that discard or damage SDCH encoding. If a server
355 // explicitly states that this is not SDCH content, then we can correct our
356 // assumption that this is an SDCH response, and avoid the need to recover
357 // as though the content is corrupted (when we discover it is not SDCH
358 // encoded).
359 std::string sdch_response_status;
360 void* iter = NULL;
361 while (GetResponseHeaders()->EnumerateHeader(&iter, "X-Sdch-Encode",
362 &sdch_response_status)) {
363 if (sdch_response_status == "0") {
364 dictionaries_advertised_.reset();
365 break;
370 // The HTTP transaction may be restarted several times for the purposes
371 // of sending authorization information. Each time it restarts, we get
372 // notified of the headers completion so that we can update the cookie store.
373 if (transaction_->IsReadyToRestartForAuth()) {
374 DCHECK(!response_info_->auth_challenge.get());
375 // TODO(battre): This breaks the webrequest API for
376 // URLRequestTestHTTP.BasicAuthWithCookies
377 // where OnBeforeSendHeaders -> OnSendHeaders -> OnBeforeSendHeaders
378 // occurs.
379 RestartTransactionWithAuth(AuthCredentials());
380 return;
383 URLRequestJob::NotifyHeadersComplete();
386 void URLRequestHttpJob::NotifyDone(const URLRequestStatus& status) {
387 DoneWithRequest(FINISHED);
388 URLRequestJob::NotifyDone(status);
391 void URLRequestHttpJob::DestroyTransaction() {
392 DCHECK(transaction_.get());
394 DoneWithRequest(ABORTED);
395 transaction_.reset();
396 response_info_ = NULL;
397 receive_headers_end_ = base::TimeTicks();
400 void URLRequestHttpJob::StartTransaction() {
401 if (network_delegate()) {
402 OnCallToDelegate();
403 int rv = network_delegate()->NotifyBeforeSendHeaders(
404 request_, notify_before_headers_sent_callback_,
405 &request_info_.extra_headers);
406 // If an extension blocks the request, we rely on the callback to
407 // MaybeStartTransactionInternal().
408 if (rv == ERR_IO_PENDING)
409 return;
410 MaybeStartTransactionInternal(rv);
411 return;
413 StartTransactionInternal();
416 void URLRequestHttpJob::NotifyBeforeSendHeadersCallback(int result) {
417 // Check that there are no callbacks to already canceled requests.
418 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
420 MaybeStartTransactionInternal(result);
423 void URLRequestHttpJob::MaybeStartTransactionInternal(int result) {
424 OnCallToDelegateComplete();
425 if (result == OK) {
426 StartTransactionInternal();
427 } else {
428 std::string source("delegate");
429 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
430 NetLog::StringCallback("source", &source));
431 NotifyCanceled();
432 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
436 void URLRequestHttpJob::StartTransactionInternal() {
437 // NOTE: This method assumes that request_info_ is already setup properly.
439 // If we already have a transaction, then we should restart the transaction
440 // with auth provided by auth_credentials_.
442 int rv;
444 if (network_delegate()) {
445 network_delegate()->NotifySendHeaders(
446 request_, request_info_.extra_headers);
449 if (transaction_.get()) {
450 rv = transaction_->RestartWithAuth(auth_credentials_, start_callback_);
451 auth_credentials_ = AuthCredentials();
452 } else {
453 DCHECK(request_->context()->http_transaction_factory());
455 rv = request_->context()->http_transaction_factory()->CreateTransaction(
456 priority_, &transaction_);
458 if (rv == OK && request_info_.url.SchemeIsWSOrWSS()) {
459 base::SupportsUserData::Data* data = request_->GetUserData(
460 WebSocketHandshakeStreamBase::CreateHelper::DataKey());
461 if (data) {
462 transaction_->SetWebSocketHandshakeStreamCreateHelper(
463 static_cast<WebSocketHandshakeStreamBase::CreateHelper*>(data));
464 } else {
465 rv = ERR_DISALLOWED_URL_SCHEME;
469 if (rv == OK) {
470 transaction_->SetBeforeNetworkStartCallback(
471 base::Bind(&URLRequestHttpJob::NotifyBeforeNetworkStart,
472 base::Unretained(this)));
473 transaction_->SetBeforeProxyHeadersSentCallback(
474 base::Bind(&URLRequestHttpJob::NotifyBeforeSendProxyHeadersCallback,
475 base::Unretained(this)));
477 if (!throttling_entry_.get() ||
478 !throttling_entry_->ShouldRejectRequest(*request_,
479 network_delegate())) {
480 rv = transaction_->Start(
481 &request_info_, start_callback_, request_->net_log());
482 start_time_ = base::TimeTicks::Now();
483 } else {
484 // Special error code for the exponential back-off module.
485 rv = ERR_TEMPORARILY_THROTTLED;
490 if (rv == ERR_IO_PENDING)
491 return;
493 // The transaction started synchronously, but we need to notify the
494 // URLRequest delegate via the message loop.
495 base::MessageLoop::current()->PostTask(
496 FROM_HERE,
497 base::Bind(&URLRequestHttpJob::OnStartCompleted,
498 weak_factory_.GetWeakPtr(), rv));
501 void URLRequestHttpJob::AddExtraHeaders() {
502 SdchManager* sdch_manager = request()->context()->sdch_manager();
504 // Supply Accept-Encoding field only if it is not already provided.
505 // It should be provided IF the content is known to have restrictions on
506 // potential encoding, such as streaming multi-media.
507 // For details see bug 47381.
508 // TODO(jar, enal): jpeg files etc. should set up a request header if
509 // possible. Right now it is done only by buffered_resource_loader and
510 // simple_data_source.
511 if (!request_info_.extra_headers.HasHeader(
512 HttpRequestHeaders::kAcceptEncoding)) {
513 // We don't support SDCH responses to POST as there is a possibility
514 // of having SDCH encoded responses returned (e.g. by the cache)
515 // which we cannot decode, and in those situations, we will need
516 // to retransmit the request without SDCH, which is illegal for a POST.
517 bool advertise_sdch = sdch_manager != NULL && request()->method() != "POST";
518 if (advertise_sdch) {
519 SdchProblemCode rv = sdch_manager->IsInSupportedDomain(request()->url());
520 if (rv != SDCH_OK) {
521 advertise_sdch = false;
522 // If SDCH is just disabled, it is not a real error.
523 if (rv != SDCH_DISABLED && rv != SDCH_SECURE_SCHEME_NOT_SUPPORTED) {
524 SdchManager::SdchErrorRecovery(rv);
525 request()->net_log().AddEvent(
526 NetLog::TYPE_SDCH_DECODING_ERROR,
527 base::Bind(&NetLogSdchResourceProblemCallback, rv));
531 if (advertise_sdch) {
532 dictionaries_advertised_ =
533 sdch_manager->GetDictionarySet(request_->url());
536 // The AllowLatencyExperiment() is only true if we've successfully done a
537 // full SDCH compression recently in this browser session for this host.
538 // Note that for this path, there might be no applicable dictionaries,
539 // and hence we can't participate in the experiment.
540 if (dictionaries_advertised_ &&
541 sdch_manager->AllowLatencyExperiment(request_->url())) {
542 // We are participating in the test (or control), and hence we'll
543 // eventually record statistics via either SDCH_EXPERIMENT_DECODE or
544 // SDCH_EXPERIMENT_HOLDBACK, and we'll need some packet timing data.
545 packet_timing_enabled_ = true;
546 if (base::RandDouble() < .01) {
547 sdch_test_control_ = true; // 1% probability.
548 dictionaries_advertised_.reset();
549 advertise_sdch = false;
550 } else {
551 sdch_test_activated_ = true;
555 // Supply Accept-Encoding headers first so that it is more likely that they
556 // will be in the first transmitted packet. This can sometimes make it
557 // easier to filter and analyze the streams to assure that a proxy has not
558 // damaged these headers. Some proxies deliberately corrupt Accept-Encoding
559 // headers.
560 if (!advertise_sdch) {
561 // Tell the server what compression formats we support (other than SDCH).
562 request_info_.extra_headers.SetHeader(
563 HttpRequestHeaders::kAcceptEncoding, "gzip, deflate");
564 } else {
565 // Include SDCH in acceptable list.
566 request_info_.extra_headers.SetHeader(
567 HttpRequestHeaders::kAcceptEncoding, "gzip, deflate, sdch");
568 if (dictionaries_advertised_) {
569 request_info_.extra_headers.SetHeader(
570 kAvailDictionaryHeader,
571 dictionaries_advertised_->GetDictionaryClientHashList());
572 // Since we're tagging this transaction as advertising a dictionary,
573 // we'll definitely employ an SDCH filter (or tentative sdch filter)
574 // when we get a response. When done, we'll record histograms via
575 // SDCH_DECODE or SDCH_PASSTHROUGH. Hence we need to record packet
576 // arrival times.
577 packet_timing_enabled_ = true;
582 if (http_user_agent_settings_) {
583 // Only add default Accept-Language if the request didn't have it
584 // specified.
585 std::string accept_language =
586 http_user_agent_settings_->GetAcceptLanguage();
587 if (!accept_language.empty()) {
588 request_info_.extra_headers.SetHeaderIfMissing(
589 HttpRequestHeaders::kAcceptLanguage,
590 accept_language);
595 void URLRequestHttpJob::AddCookieHeaderAndStart() {
596 // No matter what, we want to report our status as IO pending since we will
597 // be notifying our consumer asynchronously via OnStartCompleted.
598 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
600 // If the request was destroyed, then there is no more work to do.
601 if (!request_)
602 return;
604 CookieStore* cookie_store = request_->context()->cookie_store();
605 if (cookie_store && !(request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES)) {
606 cookie_store->GetAllCookiesForURLAsync(
607 request_->url(),
608 base::Bind(&URLRequestHttpJob::CheckCookiePolicyAndLoad,
609 weak_factory_.GetWeakPtr()));
610 } else {
611 DoStartTransaction();
615 void URLRequestHttpJob::DoLoadCookies() {
616 CookieOptions options;
617 options.set_include_httponly();
619 // TODO(mkwst): Drop this `if` once we decide whether or not to ship
620 // first-party cookies: https://crbug.com/459154
621 if (network_delegate() &&
622 network_delegate()->FirstPartyOnlyCookieExperimentEnabled())
623 options.set_first_party_url(request_->first_party_for_cookies());
624 else
625 options.set_include_first_party_only();
627 request_->context()->cookie_store()->GetCookiesWithOptionsAsync(
628 request_->url(), options, base::Bind(&URLRequestHttpJob::OnCookiesLoaded,
629 weak_factory_.GetWeakPtr()));
632 void URLRequestHttpJob::CheckCookiePolicyAndLoad(
633 const CookieList& cookie_list) {
634 if (CanGetCookies(cookie_list))
635 DoLoadCookies();
636 else
637 DoStartTransaction();
640 void URLRequestHttpJob::OnCookiesLoaded(const std::string& cookie_line) {
641 if (!cookie_line.empty()) {
642 request_info_.extra_headers.SetHeader(
643 HttpRequestHeaders::kCookie, cookie_line);
644 // Disable privacy mode as we are sending cookies anyway.
645 request_info_.privacy_mode = PRIVACY_MODE_DISABLED;
647 DoStartTransaction();
650 void URLRequestHttpJob::DoStartTransaction() {
651 // We may have been canceled while retrieving cookies.
652 if (GetStatus().is_success()) {
653 StartTransaction();
654 } else {
655 NotifyCanceled();
659 void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
660 // End of the call started in OnStartCompleted.
661 OnCallToDelegateComplete();
663 if (result != net::OK) {
664 std::string source("delegate");
665 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
666 NetLog::StringCallback("source", &source));
667 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
668 return;
671 DCHECK(transaction_.get());
673 const HttpResponseInfo* response_info = transaction_->GetResponseInfo();
674 DCHECK(response_info);
676 response_cookies_.clear();
677 response_cookies_save_index_ = 0;
679 FetchResponseCookies(&response_cookies_);
681 if (!GetResponseHeaders()->GetDateValue(&response_date_))
682 response_date_ = base::Time();
684 // Now, loop over the response cookies, and attempt to persist each.
685 SaveNextCookie();
688 // If the save occurs synchronously, SaveNextCookie will loop and save the next
689 // cookie. If the save is deferred, the callback is responsible for continuing
690 // to iterate through the cookies.
691 // TODO(erikwright): Modify the CookieStore API to indicate via return value
692 // whether it completed synchronously or asynchronously.
693 // See http://crbug.com/131066.
694 void URLRequestHttpJob::SaveNextCookie() {
695 // No matter what, we want to report our status as IO pending since we will
696 // be notifying our consumer asynchronously via OnStartCompleted.
697 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
699 // Used to communicate with the callback. See the implementation of
700 // OnCookieSaved.
701 scoped_refptr<SharedBoolean> callback_pending = new SharedBoolean(false);
702 scoped_refptr<SharedBoolean> save_next_cookie_running =
703 new SharedBoolean(true);
705 if (!(request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) &&
706 request_->context()->cookie_store() && response_cookies_.size() > 0) {
707 CookieOptions options;
708 options.set_include_httponly();
709 options.set_server_time(response_date_);
711 net::CookieStore::SetCookiesCallback callback(
712 base::Bind(&URLRequestHttpJob::OnCookieSaved,
713 weak_factory_.GetWeakPtr(),
714 save_next_cookie_running,
715 callback_pending));
717 // Loop through the cookies as long as SetCookieWithOptionsAsync completes
718 // synchronously.
719 while (!callback_pending->data &&
720 response_cookies_save_index_ < response_cookies_.size()) {
721 if (CanSetCookie(
722 response_cookies_[response_cookies_save_index_], &options)) {
723 callback_pending->data = true;
724 request_->context()->cookie_store()->SetCookieWithOptionsAsync(
725 request_->url(), response_cookies_[response_cookies_save_index_],
726 options, callback);
728 ++response_cookies_save_index_;
732 save_next_cookie_running->data = false;
734 if (!callback_pending->data) {
735 response_cookies_.clear();
736 response_cookies_save_index_ = 0;
737 SetStatus(URLRequestStatus()); // Clear the IO_PENDING status
738 NotifyHeadersComplete();
739 return;
743 // |save_next_cookie_running| is true when the callback is bound and set to
744 // false when SaveNextCookie exits, allowing the callback to determine if the
745 // save occurred synchronously or asynchronously.
746 // |callback_pending| is false when the callback is invoked and will be set to
747 // true by the callback, allowing SaveNextCookie to detect whether the save
748 // occurred synchronously.
749 // See SaveNextCookie() for more information.
750 void URLRequestHttpJob::OnCookieSaved(
751 scoped_refptr<SharedBoolean> save_next_cookie_running,
752 scoped_refptr<SharedBoolean> callback_pending,
753 bool cookie_status) {
754 callback_pending->data = false;
756 // If we were called synchronously, return.
757 if (save_next_cookie_running->data) {
758 return;
761 // We were called asynchronously, so trigger the next save.
762 // We may have been canceled within OnSetCookie.
763 if (GetStatus().is_success()) {
764 SaveNextCookie();
765 } else {
766 NotifyCanceled();
770 void URLRequestHttpJob::FetchResponseCookies(
771 std::vector<std::string>* cookies) {
772 const std::string name = "Set-Cookie";
773 std::string value;
775 void* iter = NULL;
776 HttpResponseHeaders* headers = GetResponseHeaders();
777 while (headers->EnumerateHeader(&iter, name, &value)) {
778 if (!value.empty())
779 cookies->push_back(value);
783 // NOTE: |ProcessStrictTransportSecurityHeader| and
784 // |ProcessPublicKeyPinsHeader| have very similar structures, by design.
785 void URLRequestHttpJob::ProcessStrictTransportSecurityHeader() {
786 DCHECK(response_info_);
787 TransportSecurityState* security_state =
788 request_->context()->transport_security_state();
789 const SSLInfo& ssl_info = response_info_->ssl_info;
791 // Only accept HSTS headers on HTTPS connections that have no
792 // certificate errors.
793 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
794 !security_state)
795 return;
797 // Don't accept HSTS headers when the hostname is an IP address.
798 if (request_info_.url.HostIsIPAddress())
799 return;
801 // http://tools.ietf.org/html/draft-ietf-websec-strict-transport-sec:
803 // If a UA receives more than one STS header field in a HTTP response
804 // message over secure transport, then the UA MUST process only the
805 // first such header field.
806 HttpResponseHeaders* headers = GetResponseHeaders();
807 std::string value;
808 if (headers->EnumerateHeader(NULL, "Strict-Transport-Security", &value))
809 security_state->AddHSTSHeader(request_info_.url.host(), value);
812 void URLRequestHttpJob::ProcessPublicKeyPinsHeader() {
813 DCHECK(response_info_);
814 TransportSecurityState* security_state =
815 request_->context()->transport_security_state();
816 const SSLInfo& ssl_info = response_info_->ssl_info;
818 // Only accept HPKP headers on HTTPS connections that have no
819 // certificate errors.
820 if (!ssl_info.is_valid() || IsCertStatusError(ssl_info.cert_status) ||
821 !security_state)
822 return;
824 // Don't accept HSTS headers when the hostname is an IP address.
825 if (request_info_.url.HostIsIPAddress())
826 return;
828 // http://tools.ietf.org/html/draft-ietf-websec-key-pinning:
830 // If a UA receives more than one PKP header field in an HTTP
831 // response message over secure transport, then the UA MUST process
832 // only the first such header field.
833 HttpResponseHeaders* headers = GetResponseHeaders();
834 std::string value;
835 if (headers->EnumerateHeader(NULL, "Public-Key-Pins", &value))
836 security_state->AddHPKPHeader(request_info_.url.host(), value, ssl_info);
839 void URLRequestHttpJob::OnStartCompleted(int result) {
840 RecordTimer();
842 // If the request was destroyed, then there is no more work to do.
843 if (!request_)
844 return;
846 // If the job is done (due to cancellation), can just ignore this
847 // notification.
848 if (done_)
849 return;
851 receive_headers_end_ = base::TimeTicks::Now();
853 // Clear the IO_PENDING status
854 SetStatus(URLRequestStatus());
856 const URLRequestContext* context = request_->context();
858 if (result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN &&
859 transaction_->GetResponseInfo() != NULL) {
860 FraudulentCertificateReporter* reporter =
861 context->fraudulent_certificate_reporter();
862 if (reporter != NULL) {
863 const SSLInfo& ssl_info = transaction_->GetResponseInfo()->ssl_info;
864 const std::string& host = request_->url().host();
866 reporter->SendReport(host, ssl_info);
870 if (result == OK) {
871 if (transaction_ && transaction_->GetResponseInfo()) {
872 SetProxyServer(transaction_->GetResponseInfo()->proxy_server);
874 scoped_refptr<HttpResponseHeaders> headers = GetResponseHeaders();
875 if (network_delegate()) {
876 // Note that |this| may not be deleted until
877 // |on_headers_received_callback_| or
878 // |NetworkDelegate::URLRequestDestroyed()| has been called.
879 OnCallToDelegate();
880 allowed_unsafe_redirect_url_ = GURL();
881 int error = network_delegate()->NotifyHeadersReceived(
882 request_,
883 on_headers_received_callback_,
884 headers.get(),
885 &override_response_headers_,
886 &allowed_unsafe_redirect_url_);
887 if (error != net::OK) {
888 if (error == net::ERR_IO_PENDING) {
889 awaiting_callback_ = true;
890 } else {
891 std::string source("delegate");
892 request_->net_log().AddEvent(NetLog::TYPE_CANCELLED,
893 NetLog::StringCallback("source",
894 &source));
895 OnCallToDelegateComplete();
896 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, error));
898 return;
902 SaveCookiesAndNotifyHeadersComplete(net::OK);
903 } else if (IsCertificateError(result)) {
904 // We encountered an SSL certificate error.
905 if (result == ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY ||
906 result == ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN) {
907 // These are hard failures. They're handled separately and don't have
908 // the correct cert status, so set it here.
909 SSLInfo info(transaction_->GetResponseInfo()->ssl_info);
910 info.cert_status = MapNetErrorToCertStatus(result);
911 NotifySSLCertificateError(info, true);
912 } else {
913 // Maybe overridable, maybe not. Ask the delegate to decide.
914 TransportSecurityState* state = context->transport_security_state();
915 const bool fatal =
916 state && state->ShouldSSLErrorsBeFatal(request_info_.url.host());
917 NotifySSLCertificateError(
918 transaction_->GetResponseInfo()->ssl_info, fatal);
920 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
921 NotifyCertificateRequested(
922 transaction_->GetResponseInfo()->cert_request_info.get());
923 } else {
924 // Even on an error, there may be useful information in the response
925 // info (e.g. whether there's a cached copy).
926 if (transaction_.get())
927 response_info_ = transaction_->GetResponseInfo();
928 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, result));
932 void URLRequestHttpJob::OnHeadersReceivedCallback(int result) {
933 awaiting_callback_ = false;
935 // Check that there are no callbacks to already canceled requests.
936 DCHECK_NE(URLRequestStatus::CANCELED, GetStatus().status());
938 SaveCookiesAndNotifyHeadersComplete(result);
941 void URLRequestHttpJob::OnReadCompleted(int result) {
942 read_in_progress_ = false;
944 if (ShouldFixMismatchedContentLength(result))
945 result = OK;
947 if (result == OK) {
948 NotifyDone(URLRequestStatus());
949 } else if (result < 0) {
950 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result));
951 } else {
952 // Clear the IO_PENDING status
953 SetStatus(URLRequestStatus());
956 NotifyReadComplete(result);
959 void URLRequestHttpJob::RestartTransactionWithAuth(
960 const AuthCredentials& credentials) {
961 auth_credentials_ = credentials;
963 // These will be reset in OnStartCompleted.
964 response_info_ = NULL;
965 receive_headers_end_ = base::TimeTicks();
966 response_cookies_.clear();
968 ResetTimer();
970 // Update the cookies, since the cookie store may have been updated from the
971 // headers in the 401/407. Since cookies were already appended to
972 // extra_headers, we need to strip them out before adding them again.
973 request_info_.extra_headers.RemoveHeader(HttpRequestHeaders::kCookie);
975 AddCookieHeaderAndStart();
978 void URLRequestHttpJob::SetUpload(UploadDataStream* upload) {
979 DCHECK(!transaction_.get()) << "cannot change once started";
980 request_info_.upload_data_stream = upload;
983 void URLRequestHttpJob::SetExtraRequestHeaders(
984 const HttpRequestHeaders& headers) {
985 DCHECK(!transaction_.get()) << "cannot change once started";
986 request_info_.extra_headers.CopyFrom(headers);
989 LoadState URLRequestHttpJob::GetLoadState() const {
990 return transaction_.get() ?
991 transaction_->GetLoadState() : LOAD_STATE_IDLE;
994 UploadProgress URLRequestHttpJob::GetUploadProgress() const {
995 return transaction_.get() ?
996 transaction_->GetUploadProgress() : UploadProgress();
999 bool URLRequestHttpJob::GetMimeType(std::string* mime_type) const {
1000 DCHECK(transaction_.get());
1002 if (!response_info_)
1003 return false;
1005 HttpResponseHeaders* headers = GetResponseHeaders();
1006 if (!headers)
1007 return false;
1008 return headers->GetMimeType(mime_type);
1011 bool URLRequestHttpJob::GetCharset(std::string* charset) {
1012 DCHECK(transaction_.get());
1014 if (!response_info_)
1015 return false;
1017 return GetResponseHeaders()->GetCharset(charset);
1020 void URLRequestHttpJob::GetResponseInfo(HttpResponseInfo* info) {
1021 DCHECK(request_);
1023 if (response_info_) {
1024 DCHECK(transaction_.get());
1026 *info = *response_info_;
1027 if (override_response_headers_.get())
1028 info->headers = override_response_headers_;
1032 void URLRequestHttpJob::GetLoadTimingInfo(
1033 LoadTimingInfo* load_timing_info) const {
1034 // If haven't made it far enough to receive any headers, don't return
1035 // anything. This makes for more consistent behavior in the case of errors.
1036 if (!transaction_ || receive_headers_end_.is_null())
1037 return;
1038 if (transaction_->GetLoadTimingInfo(load_timing_info))
1039 load_timing_info->receive_headers_end = receive_headers_end_;
1042 bool URLRequestHttpJob::GetResponseCookies(std::vector<std::string>* cookies) {
1043 DCHECK(transaction_.get());
1045 if (!response_info_)
1046 return false;
1048 // TODO(darin): Why are we extracting response cookies again? Perhaps we
1049 // should just leverage response_cookies_.
1051 cookies->clear();
1052 FetchResponseCookies(cookies);
1053 return true;
1056 int URLRequestHttpJob::GetResponseCode() const {
1057 DCHECK(transaction_.get());
1059 if (!response_info_)
1060 return -1;
1062 return GetResponseHeaders()->response_code();
1065 Filter* URLRequestHttpJob::SetupFilter() const {
1066 DCHECK(transaction_.get());
1067 if (!response_info_)
1068 return NULL;
1070 std::vector<Filter::FilterType> encoding_types;
1071 std::string encoding_type;
1072 HttpResponseHeaders* headers = GetResponseHeaders();
1073 void* iter = NULL;
1074 while (headers->EnumerateHeader(&iter, "Content-Encoding", &encoding_type)) {
1075 encoding_types.push_back(Filter::ConvertEncodingToType(encoding_type));
1078 // Even if encoding types are empty, there is a chance that we need to add
1079 // some decoding, as some proxies strip encoding completely. In such cases,
1080 // we may need to add (for example) SDCH filtering (when the context suggests
1081 // it is appropriate).
1082 Filter::FixupEncodingTypes(*filter_context_, &encoding_types);
1084 return !encoding_types.empty()
1085 ? Filter::Factory(encoding_types, *filter_context_) : NULL;
1088 bool URLRequestHttpJob::CopyFragmentOnRedirect(const GURL& location) const {
1089 // Allow modification of reference fragments by default, unless
1090 // |allowed_unsafe_redirect_url_| is set and equal to the redirect URL.
1091 // When this is the case, we assume that the network delegate has set the
1092 // desired redirect URL (with or without fragment), so it must not be changed
1093 // any more.
1094 return !allowed_unsafe_redirect_url_.is_valid() ||
1095 allowed_unsafe_redirect_url_ != location;
1098 bool URLRequestHttpJob::IsSafeRedirect(const GURL& location) {
1099 // HTTP is always safe.
1100 // TODO(pauljensen): Remove once crbug.com/146591 is fixed.
1101 if (location.is_valid() &&
1102 (location.scheme() == "http" || location.scheme() == "https")) {
1103 return true;
1105 // Delegates may mark a URL as safe for redirection.
1106 if (allowed_unsafe_redirect_url_.is_valid() &&
1107 allowed_unsafe_redirect_url_ == location) {
1108 return true;
1110 // Query URLRequestJobFactory as to whether |location| would be safe to
1111 // redirect to.
1112 return request_->context()->job_factory() &&
1113 request_->context()->job_factory()->IsSafeRedirectTarget(location);
1116 bool URLRequestHttpJob::NeedsAuth() {
1117 int code = GetResponseCode();
1118 if (code == -1)
1119 return false;
1121 // Check if we need either Proxy or WWW Authentication. This could happen
1122 // because we either provided no auth info, or provided incorrect info.
1123 switch (code) {
1124 case 407:
1125 if (proxy_auth_state_ == AUTH_STATE_CANCELED)
1126 return false;
1127 proxy_auth_state_ = AUTH_STATE_NEED_AUTH;
1128 return true;
1129 case 401:
1130 if (server_auth_state_ == AUTH_STATE_CANCELED)
1131 return false;
1132 server_auth_state_ = AUTH_STATE_NEED_AUTH;
1133 return true;
1135 return false;
1138 void URLRequestHttpJob::GetAuthChallengeInfo(
1139 scoped_refptr<AuthChallengeInfo>* result) {
1140 DCHECK(transaction_.get());
1141 DCHECK(response_info_);
1143 // sanity checks:
1144 DCHECK(proxy_auth_state_ == AUTH_STATE_NEED_AUTH ||
1145 server_auth_state_ == AUTH_STATE_NEED_AUTH);
1146 DCHECK((GetResponseHeaders()->response_code() == HTTP_UNAUTHORIZED) ||
1147 (GetResponseHeaders()->response_code() ==
1148 HTTP_PROXY_AUTHENTICATION_REQUIRED));
1150 *result = response_info_->auth_challenge;
1153 void URLRequestHttpJob::SetAuth(const AuthCredentials& credentials) {
1154 DCHECK(transaction_.get());
1156 // Proxy gets set first, then WWW.
1157 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1158 proxy_auth_state_ = AUTH_STATE_HAVE_AUTH;
1159 } else {
1160 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
1161 server_auth_state_ = AUTH_STATE_HAVE_AUTH;
1164 RestartTransactionWithAuth(credentials);
1167 void URLRequestHttpJob::CancelAuth() {
1168 // Proxy gets set first, then WWW.
1169 if (proxy_auth_state_ == AUTH_STATE_NEED_AUTH) {
1170 proxy_auth_state_ = AUTH_STATE_CANCELED;
1171 } else {
1172 DCHECK_EQ(server_auth_state_, AUTH_STATE_NEED_AUTH);
1173 server_auth_state_ = AUTH_STATE_CANCELED;
1176 // These will be reset in OnStartCompleted.
1177 response_info_ = NULL;
1178 receive_headers_end_ = base::TimeTicks::Now();
1179 response_cookies_.clear();
1181 ResetTimer();
1183 // OK, let the consumer read the error page...
1185 // Because we set the AUTH_STATE_CANCELED flag, NeedsAuth will return false,
1186 // which will cause the consumer to receive OnResponseStarted instead of
1187 // OnAuthRequired.
1189 // We have to do this via InvokeLater to avoid "recursing" the consumer.
1191 base::MessageLoop::current()->PostTask(
1192 FROM_HERE,
1193 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1194 weak_factory_.GetWeakPtr(), OK));
1197 void URLRequestHttpJob::ContinueWithCertificate(
1198 X509Certificate* client_cert) {
1199 DCHECK(transaction_.get());
1201 DCHECK(!response_info_) << "should not have a response yet";
1202 receive_headers_end_ = base::TimeTicks();
1204 ResetTimer();
1206 // No matter what, we want to report our status as IO pending since we will
1207 // be notifying our consumer asynchronously via OnStartCompleted.
1208 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1210 int rv = transaction_->RestartWithCertificate(client_cert, start_callback_);
1211 if (rv == ERR_IO_PENDING)
1212 return;
1214 // The transaction started synchronously, but we need to notify the
1215 // URLRequest delegate via the message loop.
1216 base::MessageLoop::current()->PostTask(
1217 FROM_HERE,
1218 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1219 weak_factory_.GetWeakPtr(), rv));
1222 void URLRequestHttpJob::ContinueDespiteLastError() {
1223 // If the transaction was destroyed, then the job was cancelled.
1224 if (!transaction_.get())
1225 return;
1227 DCHECK(!response_info_) << "should not have a response yet";
1228 receive_headers_end_ = base::TimeTicks();
1230 ResetTimer();
1232 // No matter what, we want to report our status as IO pending since we will
1233 // be notifying our consumer asynchronously via OnStartCompleted.
1234 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1236 int rv = transaction_->RestartIgnoringLastError(start_callback_);
1237 if (rv == ERR_IO_PENDING)
1238 return;
1240 // The transaction started synchronously, but we need to notify the
1241 // URLRequest delegate via the message loop.
1242 base::MessageLoop::current()->PostTask(
1243 FROM_HERE,
1244 base::Bind(&URLRequestHttpJob::OnStartCompleted,
1245 weak_factory_.GetWeakPtr(), rv));
1248 void URLRequestHttpJob::ResumeNetworkStart() {
1249 DCHECK(transaction_.get());
1250 transaction_->ResumeNetworkStart();
1253 bool URLRequestHttpJob::ShouldFixMismatchedContentLength(int rv) const {
1254 // Some servers send the body compressed, but specify the content length as
1255 // the uncompressed size. Although this violates the HTTP spec we want to
1256 // support it (as IE and FireFox do), but *only* for an exact match.
1257 // See http://crbug.com/79694.
1258 if (rv == net::ERR_CONTENT_LENGTH_MISMATCH ||
1259 rv == net::ERR_INCOMPLETE_CHUNKED_ENCODING) {
1260 if (request_ && request_->response_headers()) {
1261 int64 expected_length = request_->response_headers()->GetContentLength();
1262 VLOG(1) << __FUNCTION__ << "() "
1263 << "\"" << request_->url().spec() << "\""
1264 << " content-length = " << expected_length
1265 << " pre total = " << prefilter_bytes_read()
1266 << " post total = " << postfilter_bytes_read();
1267 if (postfilter_bytes_read() == expected_length) {
1268 // Clear the error.
1269 return true;
1273 return false;
1276 bool URLRequestHttpJob::ReadRawData(IOBuffer* buf, int buf_size,
1277 int* bytes_read) {
1278 DCHECK_NE(buf_size, 0);
1279 DCHECK(bytes_read);
1280 DCHECK(!read_in_progress_);
1282 int rv = transaction_->Read(
1283 buf, buf_size,
1284 base::Bind(&URLRequestHttpJob::OnReadCompleted, base::Unretained(this)));
1286 if (ShouldFixMismatchedContentLength(rv))
1287 rv = 0;
1289 if (rv >= 0) {
1290 *bytes_read = rv;
1291 if (!rv)
1292 DoneWithRequest(FINISHED);
1293 return true;
1296 if (rv == ERR_IO_PENDING) {
1297 read_in_progress_ = true;
1298 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
1299 } else {
1300 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
1303 return false;
1306 void URLRequestHttpJob::StopCaching() {
1307 if (transaction_.get())
1308 transaction_->StopCaching();
1311 bool URLRequestHttpJob::GetFullRequestHeaders(
1312 HttpRequestHeaders* headers) const {
1313 if (!transaction_)
1314 return false;
1316 return transaction_->GetFullRequestHeaders(headers);
1319 int64 URLRequestHttpJob::GetTotalReceivedBytes() const {
1320 if (!transaction_)
1321 return 0;
1323 return transaction_->GetTotalReceivedBytes();
1326 void URLRequestHttpJob::DoneReading() {
1327 if (transaction_) {
1328 transaction_->DoneReading();
1330 DoneWithRequest(FINISHED);
1333 void URLRequestHttpJob::DoneReadingRedirectResponse() {
1334 if (transaction_) {
1335 if (transaction_->GetResponseInfo()->headers->IsRedirect(NULL)) {
1336 // If the original headers indicate a redirect, go ahead and cache the
1337 // response, even if the |override_response_headers_| are a redirect to
1338 // another location.
1339 transaction_->DoneReading();
1340 } else {
1341 // Otherwise, |override_response_headers_| must be non-NULL and contain
1342 // bogus headers indicating a redirect.
1343 DCHECK(override_response_headers_.get());
1344 DCHECK(override_response_headers_->IsRedirect(NULL));
1345 transaction_->StopCaching();
1348 DoneWithRequest(FINISHED);
1351 HostPortPair URLRequestHttpJob::GetSocketAddress() const {
1352 return response_info_ ? response_info_->socket_address : HostPortPair();
1355 void URLRequestHttpJob::RecordTimer() {
1356 if (request_creation_time_.is_null()) {
1357 NOTREACHED()
1358 << "The same transaction shouldn't start twice without new timing.";
1359 return;
1362 base::TimeDelta to_start = base::Time::Now() - request_creation_time_;
1363 request_creation_time_ = base::Time();
1365 UMA_HISTOGRAM_MEDIUM_TIMES("Net.HttpTimeToFirstByte", to_start);
1368 void URLRequestHttpJob::ResetTimer() {
1369 if (!request_creation_time_.is_null()) {
1370 NOTREACHED()
1371 << "The timer was reset before it was recorded.";
1372 return;
1374 request_creation_time_ = base::Time::Now();
1377 void URLRequestHttpJob::UpdatePacketReadTimes() {
1378 if (!packet_timing_enabled_)
1379 return;
1381 DCHECK_GT(prefilter_bytes_read(), bytes_observed_in_packets_);
1383 base::Time now(base::Time::Now());
1384 if (!bytes_observed_in_packets_)
1385 request_time_snapshot_ = now;
1386 final_packet_time_ = now;
1388 bytes_observed_in_packets_ = prefilter_bytes_read();
1391 void URLRequestHttpJob::RecordPacketStats(
1392 FilterContext::StatisticSelector statistic) const {
1393 if (!packet_timing_enabled_ || (final_packet_time_ == base::Time()))
1394 return;
1396 base::TimeDelta duration = final_packet_time_ - request_time_snapshot_;
1397 switch (statistic) {
1398 case FilterContext::SDCH_DECODE: {
1399 UMA_HISTOGRAM_CUSTOM_COUNTS("Sdch3.Network_Decode_Bytes_Processed_b",
1400 static_cast<int>(bytes_observed_in_packets_), 500, 100000, 100);
1401 return;
1403 case FilterContext::SDCH_PASSTHROUGH: {
1404 // Despite advertising a dictionary, we handled non-sdch compressed
1405 // content.
1406 return;
1409 case FilterContext::SDCH_EXPERIMENT_DECODE: {
1410 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment3_Decode",
1411 duration,
1412 base::TimeDelta::FromMilliseconds(20),
1413 base::TimeDelta::FromMinutes(10), 100);
1414 return;
1416 case FilterContext::SDCH_EXPERIMENT_HOLDBACK: {
1417 UMA_HISTOGRAM_CUSTOM_TIMES("Sdch3.Experiment3_Holdback",
1418 duration,
1419 base::TimeDelta::FromMilliseconds(20),
1420 base::TimeDelta::FromMinutes(10), 100);
1421 return;
1423 default:
1424 NOTREACHED();
1425 return;
1429 void URLRequestHttpJob::RecordPerfHistograms(CompletionCause reason) {
1430 if (start_time_.is_null())
1431 return;
1433 base::TimeDelta total_time = base::TimeTicks::Now() - start_time_;
1434 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTime", total_time);
1436 if (reason == FINISHED) {
1437 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeSuccess", total_time);
1438 } else {
1439 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCancel", total_time);
1442 if (response_info_) {
1443 if (response_info_->was_cached) {
1444 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeCached", total_time);
1445 } else {
1446 UMA_HISTOGRAM_TIMES("Net.HttpJob.TotalTimeNotCached", total_time);
1450 if (request_info_.load_flags & LOAD_PREFETCH && !request_->was_cached())
1451 UMA_HISTOGRAM_COUNTS("Net.Prefetch.PrefilterBytesReadFromNetwork",
1452 prefilter_bytes_read());
1454 start_time_ = base::TimeTicks();
1457 void URLRequestHttpJob::DoneWithRequest(CompletionCause reason) {
1458 if (done_)
1459 return;
1460 done_ = true;
1461 RecordPerfHistograms(reason);
1462 if (reason == FINISHED) {
1463 request_->set_received_response_content_length(prefilter_bytes_read());
1467 HttpResponseHeaders* URLRequestHttpJob::GetResponseHeaders() const {
1468 DCHECK(transaction_.get());
1469 DCHECK(transaction_->GetResponseInfo());
1470 return override_response_headers_.get() ?
1471 override_response_headers_.get() :
1472 transaction_->GetResponseInfo()->headers.get();
1475 void URLRequestHttpJob::NotifyURLRequestDestroyed() {
1476 awaiting_callback_ = false;
1479 } // namespace net