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/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" |
(...skipping 11 matching lines...) Expand all Loading... |
22 : URLRequestJob(request), | 22 : URLRequestJob(request), |
23 ALLOW_THIS_IN_INITIALIZER_LIST( | 23 ALLOW_THIS_IN_INITIALIZER_LIST( |
24 start_callback_(this, &URLRequestFtpJob::OnStartCompleted)), | 24 start_callback_(this, &URLRequestFtpJob::OnStartCompleted)), |
25 ALLOW_THIS_IN_INITIALIZER_LIST( | 25 ALLOW_THIS_IN_INITIALIZER_LIST( |
26 read_callback_(this, &URLRequestFtpJob::OnReadCompleted)), | 26 read_callback_(this, &URLRequestFtpJob::OnReadCompleted)), |
27 read_in_progress_(false), | 27 read_in_progress_(false), |
28 context_(request->context()), | 28 context_(request->context()), |
29 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 29 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
30 } | 30 } |
31 | 31 |
32 URLRequestFtpJob::~URLRequestFtpJob() { | |
33 } | |
34 | |
35 // static | 32 // static |
36 URLRequestJob* URLRequestFtpJob::Factory(URLRequest* request, | 33 URLRequestJob* URLRequestFtpJob::Factory(URLRequest* request, |
37 const std::string& scheme) { | 34 const std::string& scheme) { |
38 DCHECK_EQ(scheme, "ftp"); | 35 DCHECK_EQ(scheme, "ftp"); |
39 | 36 |
40 int port = request->url().IntPort(); | 37 int port = request->url().IntPort(); |
41 if (request->url().has_port() && | 38 if (request->url().has_port() && |
42 !IsPortAllowedByFtp(port) && !IsPortAllowedByOverride(port)) | 39 !IsPortAllowedByFtp(port) && !IsPortAllowedByOverride(port)) |
43 return new URLRequestErrorJob(request, ERR_UNSAFE_PORT); | 40 return new URLRequestErrorJob(request, ERR_UNSAFE_PORT); |
44 | 41 |
45 DCHECK(request->context()); | 42 DCHECK(request->context()); |
46 DCHECK(request->context()->ftp_transaction_factory()); | 43 DCHECK(request->context()->ftp_transaction_factory()); |
47 return new URLRequestFtpJob(request); | 44 return new URLRequestFtpJob(request); |
48 } | 45 } |
49 | 46 |
50 bool URLRequestFtpJob::GetMimeType(std::string* mime_type) const { | 47 bool URLRequestFtpJob::GetMimeType(std::string* mime_type) const { |
51 if (transaction_->GetResponseInfo()->is_directory_listing) { | 48 if (transaction_->GetResponseInfo()->is_directory_listing) { |
52 *mime_type = "text/vnd.chromium.ftp-dir"; | 49 *mime_type = "text/vnd.chromium.ftp-dir"; |
53 return true; | 50 return true; |
54 } | 51 } |
55 return false; | 52 return false; |
56 } | 53 } |
57 | 54 |
| 55 URLRequestFtpJob::~URLRequestFtpJob() { |
| 56 } |
| 57 |
| 58 void URLRequestFtpJob::StartTransaction() { |
| 59 // Create a transaction. |
| 60 DCHECK(!transaction_.get()); |
| 61 DCHECK(request_->context()); |
| 62 DCHECK(request_->context()->ftp_transaction_factory()); |
| 63 |
| 64 transaction_.reset( |
| 65 request_->context()->ftp_transaction_factory()->CreateTransaction()); |
| 66 |
| 67 // No matter what, we want to report our status as IO pending since we will |
| 68 // be notifying our consumer asynchronously via OnStartCompleted. |
| 69 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 70 int rv; |
| 71 if (transaction_.get()) { |
| 72 rv = transaction_->Start( |
| 73 &request_info_, &start_callback_, request_->net_log()); |
| 74 if (rv == ERR_IO_PENDING) |
| 75 return; |
| 76 } else { |
| 77 rv = ERR_FAILED; |
| 78 } |
| 79 // The transaction started synchronously, but we need to notify the |
| 80 // URLRequest delegate via the message loop. |
| 81 MessageLoop::current()->PostTask( |
| 82 FROM_HERE, |
| 83 method_factory_.NewRunnableMethod( |
| 84 &URLRequestFtpJob::OnStartCompleted, rv)); |
| 85 } |
| 86 |
| 87 void URLRequestFtpJob::OnStartCompleted(int result) { |
| 88 // Clear the IO_PENDING status |
| 89 SetStatus(URLRequestStatus()); |
| 90 |
| 91 // FTP obviously doesn't have HTTP Content-Length header. We have to pass |
| 92 // the content size information manually. |
| 93 set_expected_content_size( |
| 94 transaction_->GetResponseInfo()->expected_content_size); |
| 95 |
| 96 if (result == OK) { |
| 97 NotifyHeadersComplete(); |
| 98 } else if (transaction_->GetResponseInfo()->needs_auth) { |
| 99 GURL origin = request_->url().GetOrigin(); |
| 100 if (server_auth_ && server_auth_->state == AUTH_STATE_HAVE_AUTH) { |
| 101 request_->context()->ftp_auth_cache()->Remove(origin, |
| 102 server_auth_->username, |
| 103 server_auth_->password); |
| 104 } else if (!server_auth_) { |
| 105 server_auth_ = new AuthData(); |
| 106 } |
| 107 server_auth_->state = AUTH_STATE_NEED_AUTH; |
| 108 |
| 109 FtpAuthCache::Entry* cached_auth = |
| 110 request_->context()->ftp_auth_cache()->Lookup(origin); |
| 111 |
| 112 if (cached_auth) { |
| 113 // Retry using cached auth data. |
| 114 SetAuth(cached_auth->username, cached_auth->password); |
| 115 } else { |
| 116 // Prompt for a username/password. |
| 117 NotifyHeadersComplete(); |
| 118 } |
| 119 } else { |
| 120 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 121 } |
| 122 } |
| 123 |
| 124 void URLRequestFtpJob::OnReadCompleted(int result) { |
| 125 read_in_progress_ = false; |
| 126 if (result == 0) { |
| 127 NotifyDone(URLRequestStatus()); |
| 128 } else if (result < 0) { |
| 129 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); |
| 130 } else { |
| 131 // Clear the IO_PENDING status |
| 132 SetStatus(URLRequestStatus()); |
| 133 } |
| 134 NotifyReadComplete(result); |
| 135 } |
| 136 |
| 137 void URLRequestFtpJob::RestartTransactionWithAuth() { |
| 138 DCHECK(server_auth_ && server_auth_->state == AUTH_STATE_HAVE_AUTH); |
| 139 |
| 140 // No matter what, we want to report our status as IO pending since we will |
| 141 // be notifying our consumer asynchronously via OnStartCompleted. |
| 142 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 143 |
| 144 int rv = transaction_->RestartWithAuth(server_auth_->username, |
| 145 server_auth_->password, |
| 146 &start_callback_); |
| 147 if (rv == ERR_IO_PENDING) |
| 148 return; |
| 149 |
| 150 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 151 this, &URLRequestFtpJob::OnStartCompleted, rv)); |
| 152 } |
| 153 |
58 void URLRequestFtpJob::Start() { | 154 void URLRequestFtpJob::Start() { |
59 DCHECK(!transaction_.get()); | 155 DCHECK(!transaction_.get()); |
60 request_info_.url = request_->url(); | 156 request_info_.url = request_->url(); |
61 StartTransaction(); | 157 StartTransaction(); |
62 } | 158 } |
63 | 159 |
64 void URLRequestFtpJob::Kill() { | 160 void URLRequestFtpJob::Kill() { |
65 if (!transaction_.get()) | 161 if (!transaction_.get()) |
66 return; | 162 return; |
67 transaction_.reset(); | 163 transaction_.reset(); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 236 |
141 if (rv == ERR_IO_PENDING) { | 237 if (rv == ERR_IO_PENDING) { |
142 read_in_progress_ = true; | 238 read_in_progress_ = true; |
143 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 239 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
144 } else { | 240 } else { |
145 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | 241 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); |
146 } | 242 } |
147 return false; | 243 return false; |
148 } | 244 } |
149 | 245 |
150 void URLRequestFtpJob::OnStartCompleted(int result) { | |
151 // Clear the IO_PENDING status | |
152 SetStatus(URLRequestStatus()); | |
153 | |
154 // FTP obviously doesn't have HTTP Content-Length header. We have to pass | |
155 // the content size information manually. | |
156 set_expected_content_size( | |
157 transaction_->GetResponseInfo()->expected_content_size); | |
158 | |
159 if (result == OK) { | |
160 NotifyHeadersComplete(); | |
161 } else if (transaction_->GetResponseInfo()->needs_auth) { | |
162 GURL origin = request_->url().GetOrigin(); | |
163 if (server_auth_ && server_auth_->state == AUTH_STATE_HAVE_AUTH) { | |
164 request_->context()->ftp_auth_cache()->Remove(origin, | |
165 server_auth_->username, | |
166 server_auth_->password); | |
167 } else if (!server_auth_) { | |
168 server_auth_ = new AuthData(); | |
169 } | |
170 server_auth_->state = AUTH_STATE_NEED_AUTH; | |
171 | |
172 FtpAuthCache::Entry* cached_auth = | |
173 request_->context()->ftp_auth_cache()->Lookup(origin); | |
174 | |
175 if (cached_auth) { | |
176 // Retry using cached auth data. | |
177 SetAuth(cached_auth->username, cached_auth->password); | |
178 } else { | |
179 // Prompt for a username/password. | |
180 NotifyHeadersComplete(); | |
181 } | |
182 } else { | |
183 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | |
184 } | |
185 } | |
186 | |
187 void URLRequestFtpJob::OnReadCompleted(int result) { | |
188 read_in_progress_ = false; | |
189 if (result == 0) { | |
190 NotifyDone(URLRequestStatus()); | |
191 } else if (result < 0) { | |
192 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, result)); | |
193 } else { | |
194 // Clear the IO_PENDING status | |
195 SetStatus(URLRequestStatus()); | |
196 } | |
197 NotifyReadComplete(result); | |
198 } | |
199 | |
200 void URLRequestFtpJob::RestartTransactionWithAuth() { | |
201 DCHECK(server_auth_ && server_auth_->state == AUTH_STATE_HAVE_AUTH); | |
202 | |
203 // No matter what, we want to report our status as IO pending since we will | |
204 // be notifying our consumer asynchronously via OnStartCompleted. | |
205 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | |
206 | |
207 int rv = transaction_->RestartWithAuth(server_auth_->username, | |
208 server_auth_->password, | |
209 &start_callback_); | |
210 if (rv == ERR_IO_PENDING) | |
211 return; | |
212 | |
213 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | |
214 this, &URLRequestFtpJob::OnStartCompleted, rv)); | |
215 } | |
216 | |
217 void URLRequestFtpJob::StartTransaction() { | |
218 // Create a transaction. | |
219 DCHECK(!transaction_.get()); | |
220 DCHECK(request_->context()); | |
221 DCHECK(request_->context()->ftp_transaction_factory()); | |
222 | |
223 transaction_.reset( | |
224 request_->context()->ftp_transaction_factory()->CreateTransaction()); | |
225 | |
226 // No matter what, we want to report our status as IO pending since we will | |
227 // be notifying our consumer asynchronously via OnStartCompleted. | |
228 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | |
229 int rv; | |
230 if (transaction_.get()) { | |
231 rv = transaction_->Start( | |
232 &request_info_, &start_callback_, request_->net_log()); | |
233 if (rv == ERR_IO_PENDING) | |
234 return; | |
235 } else { | |
236 rv = ERR_FAILED; | |
237 } | |
238 // The transaction started synchronously, but we need to notify the | |
239 // URLRequest delegate via the message loop. | |
240 MessageLoop::current()->PostTask( | |
241 FROM_HERE, | |
242 method_factory_.NewRunnableMethod( | |
243 &URLRequestFtpJob::OnStartCompleted, rv)); | |
244 } | |
245 | |
246 } // namespace net | 246 } // namespace net |
OLD | NEW |