OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_cache_transaction.h" | 5 #include "net/http/http_cache_transaction.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
10 #include <unistd.h> | 10 #include <unistd.h> |
11 #endif | 11 #endif |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
16 #include "base/metrics/field_trial.h" | 16 #include "base/metrics/field_trial.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/ref_counted.h" | 18 #include "base/ref_counted.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/time.h" | 20 #include "base/time.h" |
| 21 #include "base/utf_string_conversions.h" |
| 22 #include "net/base/auth.h" |
21 #include "net/base/cert_status_flags.h" | 23 #include "net/base/cert_status_flags.h" |
22 #include "net/base/io_buffer.h" | 24 #include "net/base/io_buffer.h" |
23 #include "net/base/load_flags.h" | 25 #include "net/base/load_flags.h" |
24 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
25 #include "net/base/net_log.h" | 27 #include "net/base/net_log.h" |
| 28 #include "net/base/net_util.h" |
26 #include "net/base/ssl_cert_request_info.h" | 29 #include "net/base/ssl_cert_request_info.h" |
27 #include "net/base/ssl_config_service.h" | 30 #include "net/base/ssl_config_service.h" |
28 #include "net/disk_cache/disk_cache.h" | 31 #include "net/disk_cache/disk_cache.h" |
29 #include "net/http/disk_cache_based_ssl_host_info.h" | 32 #include "net/http/disk_cache_based_ssl_host_info.h" |
30 #include "net/http/http_request_info.h" | 33 #include "net/http/http_request_info.h" |
31 #include "net/http/http_response_headers.h" | 34 #include "net/http/http_response_headers.h" |
32 #include "net/http/http_transaction.h" | 35 #include "net/http/http_transaction.h" |
33 #include "net/http/http_util.h" | 36 #include "net/http/http_util.h" |
34 #include "net/http/partial_data.h" | 37 #include "net/http/partial_data.h" |
35 | 38 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 //----------------------------------------------------------------------------- | 101 //----------------------------------------------------------------------------- |
99 | 102 |
100 HttpCache::Transaction::Transaction(HttpCache* cache) | 103 HttpCache::Transaction::Transaction(HttpCache* cache) |
101 : next_state_(STATE_NONE), | 104 : next_state_(STATE_NONE), |
102 request_(NULL), | 105 request_(NULL), |
103 cache_(cache->AsWeakPtr()), | 106 cache_(cache->AsWeakPtr()), |
104 entry_(NULL), | 107 entry_(NULL), |
105 new_entry_(NULL), | 108 new_entry_(NULL), |
106 network_trans_(NULL), | 109 network_trans_(NULL), |
107 callback_(NULL), | 110 callback_(NULL), |
| 111 tls_login_auth_data_(NULL), |
108 new_response_(NULL), | 112 new_response_(NULL), |
109 mode_(NONE), | 113 mode_(NONE), |
110 target_state_(STATE_NONE), | 114 target_state_(STATE_NONE), |
111 reading_(false), | 115 reading_(false), |
112 invalid_range_(false), | 116 invalid_range_(false), |
113 truncated_(false), | 117 truncated_(false), |
114 is_sparse_(false), | 118 is_sparse_(false), |
115 server_responded_206_(false), | 119 server_responded_206_(false), |
116 cache_pending_(false), | 120 cache_pending_(false), |
117 read_offset_(0), | 121 read_offset_(0), |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 return ERR_UNEXPECTED; | 290 return ERR_UNEXPECTED; |
287 | 291 |
288 int rv = RestartNetworkRequestWithCertificate(client_cert); | 292 int rv = RestartNetworkRequestWithCertificate(client_cert); |
289 | 293 |
290 if (rv == ERR_IO_PENDING) | 294 if (rv == ERR_IO_PENDING) |
291 callback_ = callback; | 295 callback_ = callback; |
292 | 296 |
293 return rv; | 297 return rv; |
294 } | 298 } |
295 | 299 |
| 300 void HttpCache::Transaction::SetTLSLoginAuthData(AuthData* auth_data) { |
| 301 if (network_trans_.get()) |
| 302 network_trans_->SetTLSLoginAuthData(auth_data); |
| 303 tls_login_auth_data_ = auth_data; |
| 304 } |
| 305 |
| 306 int HttpCache::Transaction::RestartWithTLSLogin(CompletionCallback *callback) { |
| 307 DCHECK(callback); |
| 308 |
| 309 // Ensure that we only have one asynchronous call at a time. |
| 310 DCHECK(!callback_); |
| 311 |
| 312 if (!cache_) |
| 313 return ERR_UNEXPECTED; |
| 314 |
| 315 int rv; |
| 316 |
| 317 if (network_trans_.get()) { |
| 318 rv = RestartNetworkRequestWithTLSLogin(); |
| 319 } else { |
| 320 // Start the transaction. |
| 321 next_state_ = STATE_SEND_REQUEST; |
| 322 rv = DoLoop(OK); |
| 323 } |
| 324 |
| 325 if (rv == ERR_IO_PENDING) |
| 326 callback_ = callback; |
| 327 |
| 328 return rv; |
| 329 } |
| 330 |
296 int HttpCache::Transaction::RestartWithAuth( | 331 int HttpCache::Transaction::RestartWithAuth( |
297 const string16& username, | 332 const string16& username, |
298 const string16& password, | 333 const string16& password, |
299 CompletionCallback* callback) { | 334 CompletionCallback* callback) { |
300 DCHECK(auth_response_.headers); | 335 DCHECK(auth_response_.headers); |
301 DCHECK(callback); | 336 DCHECK(callback); |
302 | 337 |
303 // Ensure that we only have one asynchronous call at a time. | 338 // Ensure that we only have one asynchronous call at a time. |
304 DCHECK(!callback_); | 339 DCHECK(!callback_); |
305 | 340 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 410 } |
376 | 411 |
377 void HttpCache::Transaction::StopCaching() { | 412 void HttpCache::Transaction::StopCaching() { |
378 } | 413 } |
379 | 414 |
380 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const { | 415 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const { |
381 // Null headers means we encountered an error or haven't a response yet | 416 // Null headers means we encountered an error or haven't a response yet |
382 if (auth_response_.headers) | 417 if (auth_response_.headers) |
383 return &auth_response_; | 418 return &auth_response_; |
384 return (response_.headers || response_.ssl_info.cert || | 419 return (response_.headers || response_.ssl_info.cert || |
385 response_.cert_request_info) ? &response_ : NULL; | 420 !response_.ssl_info.tls_username.empty() || |
| 421 response_.cert_request_info || response_.login_request_info) ? |
| 422 &response_ : NULL; |
386 } | 423 } |
387 | 424 |
388 LoadState HttpCache::Transaction::GetLoadState() const { | 425 LoadState HttpCache::Transaction::GetLoadState() const { |
389 LoadState state = GetWriterLoadState(); | 426 LoadState state = GetWriterLoadState(); |
390 if (state != LOAD_STATE_WAITING_FOR_CACHE) | 427 if (state != LOAD_STATE_WAITING_FOR_CACHE) |
391 return state; | 428 return state; |
392 | 429 |
393 if (cache_) | 430 if (cache_) |
394 return cache_->GetLoadStateForPendingTransaction(this); | 431 return cache_->GetLoadStateForPendingTransaction(this); |
395 | 432 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 | 672 |
636 int HttpCache::Transaction::DoSendRequest() { | 673 int HttpCache::Transaction::DoSendRequest() { |
637 DCHECK(mode_ & WRITE || mode_ == NONE); | 674 DCHECK(mode_ & WRITE || mode_ == NONE); |
638 DCHECK(!network_trans_.get()); | 675 DCHECK(!network_trans_.get()); |
639 | 676 |
640 // Create a network transaction. | 677 // Create a network transaction. |
641 int rv = cache_->network_layer_->CreateTransaction(&network_trans_); | 678 int rv = cache_->network_layer_->CreateTransaction(&network_trans_); |
642 if (rv != OK) | 679 if (rv != OK) |
643 return rv; | 680 return rv; |
644 | 681 |
| 682 if (tls_login_auth_data_ && |
| 683 tls_login_auth_data_->state == AUTH_STATE_HAVE_AUTH) { |
| 684 network_trans_->SetTLSLoginAuthData(tls_login_auth_data_); |
| 685 } |
| 686 |
645 next_state_ = STATE_SEND_REQUEST_COMPLETE; | 687 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
646 rv = network_trans_->Start(request_, &io_callback_, net_log_); | 688 rv = network_trans_->Start(request_, &io_callback_, net_log_); |
647 return rv; | 689 return rv; |
648 } | 690 } |
649 | 691 |
650 int HttpCache::Transaction::DoSendRequestComplete(int result) { | 692 int HttpCache::Transaction::DoSendRequestComplete(int result) { |
651 if (!cache_) | 693 if (!cache_) |
652 return ERR_UNEXPECTED; | 694 return ERR_UNEXPECTED; |
653 | 695 |
654 if (result == OK) { | 696 if (result == OK) { |
655 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST; | 697 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST; |
656 return OK; | 698 return OK; |
657 } | 699 } |
658 | 700 |
659 if (IsCertificateError(result)) { | 701 if (IsCertificateError(result)) { |
660 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); | 702 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); |
661 // If we get a certificate error, then there is a certificate in ssl_info, | 703 // If we get a certificate error, then there is a certificate in ssl_info, |
662 // so GetResponseInfo() should never returns NULL here. | 704 // so GetResponseInfo() should never returns NULL here. |
663 DCHECK(response); | 705 DCHECK(response); |
664 response_.ssl_info = response->ssl_info; | 706 response_.ssl_info = response->ssl_info; |
665 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { | 707 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
666 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); | 708 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); |
667 DCHECK(response); | 709 DCHECK(response); |
668 response_.cert_request_info = response->cert_request_info; | 710 response_.cert_request_info = response->cert_request_info; |
| 711 } else if (result == ERR_TLS_CLIENT_LOGIN_NEEDED || |
| 712 result == ERR_TLS_CLIENT_LOGIN_FAILED) { |
| 713 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); |
| 714 DCHECK(response); |
| 715 response_.login_request_info = response->login_request_info; |
669 } | 716 } |
670 return result; | 717 return result; |
671 } | 718 } |
672 | 719 |
673 // We received the response headers and there is no error. | 720 // We received the response headers and there is no error. |
674 int HttpCache::Transaction::DoSuccessfulSendRequest() { | 721 int HttpCache::Transaction::DoSuccessfulSendRequest() { |
675 DCHECK(!new_response_); | 722 DCHECK(!new_response_); |
676 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); | 723 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); |
677 if (new_response->headers->response_code() == 401 || | 724 if (new_response->headers->response_code() == 401 || |
678 new_response->headers->response_code() == 407) { | 725 new_response->headers->response_code() == 407) { |
(...skipping 885 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1564 DCHECK(network_trans_.get()); | 1611 DCHECK(network_trans_.get()); |
1565 DCHECK_EQ(STATE_NONE, next_state_); | 1612 DCHECK_EQ(STATE_NONE, next_state_); |
1566 | 1613 |
1567 next_state_ = STATE_SEND_REQUEST_COMPLETE; | 1614 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
1568 int rv = network_trans_->RestartWithCertificate(client_cert, &io_callback_); | 1615 int rv = network_trans_->RestartWithCertificate(client_cert, &io_callback_); |
1569 if (rv != ERR_IO_PENDING) | 1616 if (rv != ERR_IO_PENDING) |
1570 return DoLoop(rv); | 1617 return DoLoop(rv); |
1571 return rv; | 1618 return rv; |
1572 } | 1619 } |
1573 | 1620 |
| 1621 int HttpCache::Transaction::RestartNetworkRequestWithTLSLogin() { |
| 1622 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 1623 DCHECK(network_trans_.get()); |
| 1624 DCHECK_EQ(STATE_NONE, next_state_); |
| 1625 |
| 1626 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 1627 int rv = network_trans_->RestartWithTLSLogin(&io_callback_); |
| 1628 if (rv != ERR_IO_PENDING) |
| 1629 return DoLoop(rv); |
| 1630 return rv; |
| 1631 } |
| 1632 |
1574 int HttpCache::Transaction::RestartNetworkRequestWithAuth( | 1633 int HttpCache::Transaction::RestartNetworkRequestWithAuth( |
1575 const string16& username, | 1634 const string16& username, |
1576 const string16& password) { | 1635 const string16& password) { |
1577 DCHECK(mode_ & WRITE || mode_ == NONE); | 1636 DCHECK(mode_ & WRITE || mode_ == NONE); |
1578 DCHECK(network_trans_.get()); | 1637 DCHECK(network_trans_.get()); |
1579 DCHECK_EQ(STATE_NONE, next_state_); | 1638 DCHECK_EQ(STATE_NONE, next_state_); |
1580 | 1639 |
1581 next_state_ = STATE_SEND_REQUEST_COMPLETE; | 1640 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
1582 int rv = network_trans_->RestartWithAuth(username, password, &io_callback_); | 1641 int rv = network_trans_->RestartWithAuth(username, password, &io_callback_); |
1583 if (rv != ERR_IO_PENDING) | 1642 if (rv != ERR_IO_PENDING) |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1908 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | 1967 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; |
1909 } | 1968 } |
1910 return result; | 1969 return result; |
1911 } | 1970 } |
1912 | 1971 |
1913 void HttpCache::Transaction::OnIOComplete(int result) { | 1972 void HttpCache::Transaction::OnIOComplete(int result) { |
1914 DoLoop(result); | 1973 DoLoop(result); |
1915 } | 1974 } |
1916 | 1975 |
1917 } // namespace net | 1976 } // namespace net |
OLD | NEW |