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