OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/url_request/url_request.h" | 5 #include "net/url_request/url_request.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/metrics/stats_counters.h" | 10 #include "base/metrics/stats_counters.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 #include "net/base/auth.h" |
12 #include "net/base/host_port_pair.h" | 13 #include "net/base/host_port_pair.h" |
13 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
15 #include "net/base/net_log.h" | 16 #include "net/base/net_log.h" |
16 #include "net/base/network_delegate.h" | 17 #include "net/base/network_delegate.h" |
17 #include "net/base/ssl_cert_request_info.h" | 18 #include "net/base/ssl_cert_request_info.h" |
18 #include "net/base/upload_data.h" | 19 #include "net/base/upload_data.h" |
19 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
20 #include "net/http/http_util.h" | 21 #include "net/http/http_util.h" |
21 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 load_flags_(LOAD_NORMAL), | 138 load_flags_(LOAD_NORMAL), |
138 delegate_(delegate), | 139 delegate_(delegate), |
139 is_pending_(false), | 140 is_pending_(false), |
140 redirect_limit_(kMaxRedirects), | 141 redirect_limit_(kMaxRedirects), |
141 final_upload_progress_(0), | 142 final_upload_progress_(0), |
142 priority_(LOWEST), | 143 priority_(LOWEST), |
143 identifier_(GenerateURLRequestIdentifier()), | 144 identifier_(GenerateURLRequestIdentifier()), |
144 blocked_on_delegate_(false), | 145 blocked_on_delegate_(false), |
145 ALLOW_THIS_IN_INITIALIZER_LIST( | 146 ALLOW_THIS_IN_INITIALIZER_LIST( |
146 before_request_callback_(this, &URLRequest::BeforeRequestComplete)), | 147 before_request_callback_(this, &URLRequest::BeforeRequestComplete)), |
147 has_notified_completion_(false) { | 148 has_notified_completion_(false), |
| 149 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 150 auth_required_callback_( |
| 151 this, &URLRequest::NotifyAuthRequiredComplete)) { |
148 SIMPLE_STATS_COUNTER("URLRequestCount"); | 152 SIMPLE_STATS_COUNTER("URLRequestCount"); |
149 | 153 |
150 // Sanity check out environment. | 154 // Sanity check out environment. |
151 DCHECK(MessageLoop::current()) << | 155 DCHECK(MessageLoop::current()) << |
152 "The current MessageLoop must exist"; | 156 "The current MessageLoop must exist"; |
153 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 157 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
154 "The current MessageLoop must be TYPE_IO"; | 158 "The current MessageLoop must be TYPE_IO"; |
155 } | 159 } |
156 | 160 |
157 URLRequest::~URLRequest() { | 161 URLRequest::~URLRequest() { |
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) { | 767 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) { |
764 // TODO(battre): We could simulate a redirection there as follows: | 768 // TODO(battre): We could simulate a redirection there as follows: |
765 // if (context_ && context_->network_delegate()) { | 769 // if (context_ && context_->network_delegate()) { |
766 // // We simulate a redirection. | 770 // // We simulate a redirection. |
767 // context_->network_delegate()->NotifyBeforeRedirect(this, url()); | 771 // context_->network_delegate()->NotifyBeforeRedirect(this, url()); |
768 //} | 772 //} |
769 // This fixes URLRequestTestHTTP.BasicAuth but not | 773 // This fixes URLRequestTestHTTP.BasicAuth but not |
770 // URLRequestTestHTTP.BasicAuthWithCookies. In both cases we observe a | 774 // URLRequestTestHTTP.BasicAuthWithCookies. In both cases we observe a |
771 // call sequence of OnBeforeSendHeaders -> OnSendHeaders -> | 775 // call sequence of OnBeforeSendHeaders -> OnSendHeaders -> |
772 // OnBeforeSendHeaders. | 776 // OnBeforeSendHeaders. |
773 if (context_ && context_->network_delegate()) | 777 int rv = OK; |
774 context_->network_delegate()->NotifyAuthRequired(this, *auth_info); | 778 auth_info_ = auth_info; |
| 779 if (context_ && context_->network_delegate()) { |
| 780 // Will this delete the callback when it is executed? Or can |
| 781 // it be used multiple times? |
| 782 rv = context_->network_delegate()->NotifyAuthRequired( |
| 783 this, *auth_info, &auth_required_callback_, &auth_credentials_); |
| 784 } |
775 | 785 |
| 786 if (rv == ERR_IO_PENDING) { |
| 787 SetBlockedOnDelegate(); |
| 788 } else { |
| 789 NotifyAuthRequiredComplete(rv); |
| 790 } |
| 791 } |
| 792 |
| 793 void URLRequest::NotifyAuthRequiredComplete(int result) { |
| 794 // Not sure if we need to handle the cancelation case. |
| 795 CHECK(result == OK || result == ERR_EMPTY_RESPONSE); |
| 796 |
| 797 // NotifyAuthRequired may be called multiple times, such as |
| 798 // when an authentication attempt fails. Clear out the data |
| 799 // so it can be reset on another round. |
| 800 AuthCredentials credentials = auth_credentials_; |
| 801 auth_credentials_ = AuthCredentials(); |
| 802 |
| 803 // If the NetworkDelegate provided a username and password, try |
| 804 // using that. |
| 805 if (credentials.is_valid) { |
| 806 SetAuth(credentials.username, credentials.password); |
| 807 return; |
| 808 } |
| 809 |
| 810 // Otherwise, defer to the URLRequest Delegate. |
776 if (delegate_) | 811 if (delegate_) |
777 delegate_->OnAuthRequired(this, auth_info); | 812 delegate_->OnAuthRequired(this, auth_info_.get()); |
778 } | 813 } |
779 | 814 |
| 815 |
780 void URLRequest::NotifyCertificateRequested( | 816 void URLRequest::NotifyCertificateRequested( |
781 SSLCertRequestInfo* cert_request_info) { | 817 SSLCertRequestInfo* cert_request_info) { |
782 if (delegate_) | 818 if (delegate_) |
783 delegate_->OnCertificateRequested(this, cert_request_info); | 819 delegate_->OnCertificateRequested(this, cert_request_info); |
784 } | 820 } |
785 | 821 |
786 void URLRequest::NotifySSLCertificateError(int cert_error, | 822 void URLRequest::NotifySSLCertificateError(int cert_error, |
787 X509Certificate* cert) { | 823 X509Certificate* cert) { |
788 if (delegate_) | 824 if (delegate_) |
789 delegate_->OnSSLCertificateError(this, cert_error, cert); | 825 delegate_->OnSSLCertificateError(this, cert_error, cert); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); | 867 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); |
832 } | 868 } |
833 | 869 |
834 void URLRequest::SetUnblockedOnDelegate() { | 870 void URLRequest::SetUnblockedOnDelegate() { |
835 blocked_on_delegate_ = false; | 871 blocked_on_delegate_ = false; |
836 load_state_param_.clear(); | 872 load_state_param_.clear(); |
837 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); | 873 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); |
838 } | 874 } |
839 | 875 |
840 } // namespace net | 876 } // namespace net |
OLD | NEW |