| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_ftp_job.h" | 5 #include "net/url_request/url_request_ftp_job.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "net/base/auth.h" | 10 #include "net/base/auth.h" |
| 11 #include "net/base/host_port_pair.h" | 11 #include "net/base/host_port_pair.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 14 #include "net/ftp/ftp_response_info.h" | 14 #include "net/ftp/ftp_response_info.h" |
| 15 #include "net/ftp/ftp_transaction_factory.h" | 15 #include "net/ftp/ftp_transaction_factory.h" |
| 16 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_error_job.h" | 18 #include "net/url_request/url_request_error_job.h" |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 URLRequestFtpJob::URLRequestFtpJob(URLRequest* request) | 22 URLRequestFtpJob::URLRequestFtpJob( |
| 23 : URLRequestJob(request, request->context()->network_delegate()), | 23 URLRequest* request, |
| 24 NetworkDelegate* network_delegate, |
| 25 FtpTransactionFactory* ftp_transaction_factory, |
| 26 FtpAuthCache* ftp_auth_cache) |
| 27 : URLRequestJob(request, network_delegate), |
| 24 read_in_progress_(false), | 28 read_in_progress_(false), |
| 25 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 29 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 30 ftp_transaction_factory_(ftp_transaction_factory), |
| 31 ftp_auth_cache_(ftp_auth_cache) { |
| 32 DCHECK(ftp_transaction_factory); |
| 33 DCHECK(ftp_auth_cache); |
| 26 } | 34 } |
| 27 | 35 |
| 28 // static | 36 // static |
| 29 URLRequestJob* URLRequestFtpJob::Factory(URLRequest* request, | 37 URLRequestJob* URLRequestFtpJob::Factory(URLRequest* request, |
| 30 const std::string& scheme) { | 38 const std::string& scheme) { |
| 31 DCHECK_EQ(scheme, "ftp"); | 39 DCHECK_EQ(scheme, "ftp"); |
| 32 | 40 |
| 33 int port = request->url().IntPort(); | 41 int port = request->url().IntPort(); |
| 34 if (request->url().has_port() && | 42 if (request->url().has_port() && |
| 35 !IsPortAllowedByFtp(port) && !IsPortAllowedByOverride(port)) | 43 !IsPortAllowedByFtp(port) && !IsPortAllowedByOverride(port)) { |
| 36 return new URLRequestErrorJob(request, ERR_UNSAFE_PORT); | 44 return new URLRequestErrorJob(request, ERR_UNSAFE_PORT); |
| 45 } |
| 37 | 46 |
| 38 DCHECK(request->context()); | 47 return new URLRequestFtpJob(request, |
| 39 DCHECK(request->context()->ftp_transaction_factory()); | 48 request->context()->network_delegate(), |
| 40 return new URLRequestFtpJob(request); | 49 request->context()->ftp_transaction_factory(), |
| 50 request->context()->ftp_auth_cache()); |
| 41 } | 51 } |
| 42 | 52 |
| 43 bool URLRequestFtpJob::GetMimeType(std::string* mime_type) const { | 53 bool URLRequestFtpJob::GetMimeType(std::string* mime_type) const { |
| 44 if (transaction_->GetResponseInfo()->is_directory_listing) { | 54 if (transaction_->GetResponseInfo()->is_directory_listing) { |
| 45 *mime_type = "text/vnd.chromium.ftp-dir"; | 55 *mime_type = "text/vnd.chromium.ftp-dir"; |
| 46 return true; | 56 return true; |
| 47 } | 57 } |
| 48 return false; | 58 return false; |
| 49 } | 59 } |
| 50 | 60 |
| 51 HostPortPair URLRequestFtpJob::GetSocketAddress() const { | 61 HostPortPair URLRequestFtpJob::GetSocketAddress() const { |
| 52 if (!transaction_.get()) { | 62 if (!transaction_.get()) { |
| 53 return HostPortPair(); | 63 return HostPortPair(); |
| 54 } | 64 } |
| 55 return transaction_->GetResponseInfo()->socket_address; | 65 return transaction_->GetResponseInfo()->socket_address; |
| 56 } | 66 } |
| 57 | 67 |
| 58 URLRequestFtpJob::~URLRequestFtpJob() { | 68 URLRequestFtpJob::~URLRequestFtpJob() { |
| 59 } | 69 } |
| 60 | 70 |
| 61 void URLRequestFtpJob::StartTransaction() { | 71 void URLRequestFtpJob::StartTransaction() { |
| 62 // Create a transaction. | 72 // Create a transaction. |
| 63 DCHECK(!transaction_.get()); | 73 DCHECK(!transaction_.get()); |
| 64 DCHECK(request_->context()); | |
| 65 DCHECK(request_->context()->ftp_transaction_factory()); | |
| 66 | 74 |
| 67 transaction_.reset( | 75 transaction_.reset(ftp_transaction_factory_->CreateTransaction()); |
| 68 request_->context()->ftp_transaction_factory()->CreateTransaction()); | |
| 69 | 76 |
| 70 // No matter what, we want to report our status as IO pending since we will | 77 // No matter what, we want to report our status as IO pending since we will |
| 71 // be notifying our consumer asynchronously via OnStartCompleted. | 78 // be notifying our consumer asynchronously via OnStartCompleted. |
| 72 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 79 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 73 int rv; | 80 int rv; |
| 74 if (transaction_.get()) { | 81 if (transaction_.get()) { |
| 75 rv = transaction_->Start( | 82 rv = transaction_->Start( |
| 76 &request_info_, | 83 &request_info_, |
| 77 base::Bind(&URLRequestFtpJob::OnStartCompleted, | 84 base::Bind(&URLRequestFtpJob::OnStartCompleted, |
| 78 base::Unretained(this)), | 85 base::Unretained(this)), |
| (...skipping 18 matching lines...) Expand all Loading... |
| 97 // FTP obviously doesn't have HTTP Content-Length header. We have to pass | 104 // FTP obviously doesn't have HTTP Content-Length header. We have to pass |
| 98 // the content size information manually. | 105 // the content size information manually. |
| 99 set_expected_content_size( | 106 set_expected_content_size( |
| 100 transaction_->GetResponseInfo()->expected_content_size); | 107 transaction_->GetResponseInfo()->expected_content_size); |
| 101 | 108 |
| 102 if (result == OK) { | 109 if (result == OK) { |
| 103 NotifyHeadersComplete(); | 110 NotifyHeadersComplete(); |
| 104 } else if (transaction_->GetResponseInfo()->needs_auth) { | 111 } else if (transaction_->GetResponseInfo()->needs_auth) { |
| 105 GURL origin = request_->url().GetOrigin(); | 112 GURL origin = request_->url().GetOrigin(); |
| 106 if (server_auth_ && server_auth_->state == AUTH_STATE_HAVE_AUTH) { | 113 if (server_auth_ && server_auth_->state == AUTH_STATE_HAVE_AUTH) { |
| 107 request_->context()->ftp_auth_cache()->Remove( | 114 ftp_auth_cache_->Remove(origin, server_auth_->credentials); |
| 108 origin, server_auth_->credentials); | |
| 109 } else if (!server_auth_) { | 115 } else if (!server_auth_) { |
| 110 server_auth_ = new AuthData(); | 116 server_auth_ = new AuthData(); |
| 111 } | 117 } |
| 112 server_auth_->state = AUTH_STATE_NEED_AUTH; | 118 server_auth_->state = AUTH_STATE_NEED_AUTH; |
| 113 | 119 |
| 114 FtpAuthCache::Entry* cached_auth = | 120 FtpAuthCache::Entry* cached_auth = ftp_auth_cache_->Lookup(origin); |
| 115 request_->context()->ftp_auth_cache()->Lookup(origin); | |
| 116 | |
| 117 if (cached_auth) { | 121 if (cached_auth) { |
| 118 // Retry using cached auth data. | 122 // Retry using cached auth data. |
| 119 SetAuth(cached_auth->credentials); | 123 SetAuth(cached_auth->credentials); |
| 120 } else { | 124 } else { |
| 121 // Prompt for a username/password. | 125 // Prompt for a username/password. |
| 122 NotifyHeadersComplete(); | 126 NotifyHeadersComplete(); |
| 123 } | 127 } |
| 124 } else { | 128 } else { |
| 125 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | 129 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 126 } | 130 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 DCHECK(auth_info->scheme.empty()); | 201 DCHECK(auth_info->scheme.empty()); |
| 198 DCHECK(auth_info->realm.empty()); | 202 DCHECK(auth_info->realm.empty()); |
| 199 result->swap(auth_info); | 203 result->swap(auth_info); |
| 200 } | 204 } |
| 201 | 205 |
| 202 void URLRequestFtpJob::SetAuth(const AuthCredentials& credentials) { | 206 void URLRequestFtpJob::SetAuth(const AuthCredentials& credentials) { |
| 203 DCHECK(NeedsAuth()); | 207 DCHECK(NeedsAuth()); |
| 204 server_auth_->state = AUTH_STATE_HAVE_AUTH; | 208 server_auth_->state = AUTH_STATE_HAVE_AUTH; |
| 205 server_auth_->credentials = credentials; | 209 server_auth_->credentials = credentials; |
| 206 | 210 |
| 207 request_->context()->ftp_auth_cache()->Add(request_->url().GetOrigin(), | 211 ftp_auth_cache_->Add(request_->url().GetOrigin(), server_auth_->credentials); |
| 208 server_auth_->credentials); | |
| 209 | 212 |
| 210 RestartTransactionWithAuth(); | 213 RestartTransactionWithAuth(); |
| 211 } | 214 } |
| 212 | 215 |
| 213 void URLRequestFtpJob::CancelAuth() { | 216 void URLRequestFtpJob::CancelAuth() { |
| 214 DCHECK(NeedsAuth()); | 217 DCHECK(NeedsAuth()); |
| 215 server_auth_->state = AUTH_STATE_CANCELED; | 218 server_auth_->state = AUTH_STATE_CANCELED; |
| 216 | 219 |
| 217 // Once the auth is cancelled, we proceed with the request as though | 220 // Once the auth is cancelled, we proceed with the request as though |
| 218 // there were no auth. Schedule this for later so that we don't cause | 221 // there were no auth. Schedule this for later so that we don't cause |
| (...skipping 26 matching lines...) Expand all Loading... |
| 245 if (rv == ERR_IO_PENDING) { | 248 if (rv == ERR_IO_PENDING) { |
| 246 read_in_progress_ = true; | 249 read_in_progress_ = true; |
| 247 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 250 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 248 } else { | 251 } else { |
| 249 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | 252 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
| 250 } | 253 } |
| 251 return false; | 254 return false; |
| 252 } | 255 } |
| 253 | 256 |
| 254 } // namespace net | 257 } // namespace net |
| OLD | NEW |