1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qnetworkaccessftpbackend_p.h"
|
---|
43 | #include "qnetworkaccessmanager_p.h"
|
---|
44 | #include "QtNetwork/qauthenticator.h"
|
---|
45 |
|
---|
46 | #ifndef QT_NO_FTP
|
---|
47 |
|
---|
48 | QT_BEGIN_NAMESPACE
|
---|
49 |
|
---|
50 | enum {
|
---|
51 | DefaultFtpPort = 21
|
---|
52 | };
|
---|
53 |
|
---|
54 | static QByteArray makeCacheKey(const QUrl &url)
|
---|
55 | {
|
---|
56 | QUrl copy = url;
|
---|
57 | copy.setPort(url.port(DefaultFtpPort));
|
---|
58 | return "ftp-connection:" +
|
---|
59 | copy.toEncoded(QUrl::RemovePassword | QUrl::RemovePath | QUrl::RemoveQuery |
|
---|
60 | QUrl::RemoveFragment);
|
---|
61 | }
|
---|
62 |
|
---|
63 | QNetworkAccessBackend *
|
---|
64 | QNetworkAccessFtpBackendFactory::create(QNetworkAccessManager::Operation op,
|
---|
65 | const QNetworkRequest &request) const
|
---|
66 | {
|
---|
67 | // is it an operation we know of?
|
---|
68 | switch (op) {
|
---|
69 | case QNetworkAccessManager::GetOperation:
|
---|
70 | case QNetworkAccessManager::PutOperation:
|
---|
71 | break;
|
---|
72 |
|
---|
73 | default:
|
---|
74 | // no, we can't handle this operation
|
---|
75 | return 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | QUrl url = request.url();
|
---|
79 | if (url.scheme() == QLatin1String("ftp"))
|
---|
80 | return new QNetworkAccessFtpBackend;
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | class QNetworkAccessFtpIODevice: public QIODevice
|
---|
85 | {
|
---|
86 | //Q_OBJECT
|
---|
87 | public:
|
---|
88 | QNetworkAccessFtpBackend *backend;
|
---|
89 | bool eof;
|
---|
90 |
|
---|
91 | inline QNetworkAccessFtpIODevice(QNetworkAccessFtpBackend *parent)
|
---|
92 | : QIODevice(parent), backend(parent), eof(false)
|
---|
93 | { open(ReadOnly); }
|
---|
94 |
|
---|
95 | bool isSequential() const { return true; }
|
---|
96 | bool atEnd() const { return backend->upstreamBytesAvailable() == 0; }
|
---|
97 |
|
---|
98 | qint64 bytesAvailable() const { return backend->upstreamBytesAvailable(); }
|
---|
99 | qint64 bytesToWrite() const { return backend->downstreamBytesToConsume(); }
|
---|
100 | protected:
|
---|
101 | qint64 readData(char *data, qint64 maxlen)
|
---|
102 | {
|
---|
103 | const QByteArray toSend = backend->readUpstream();
|
---|
104 | maxlen = qMin<qint64>(maxlen, toSend.size());
|
---|
105 | if (!maxlen)
|
---|
106 | return eof ? -1 : 0;
|
---|
107 |
|
---|
108 | backend->upstreamBytesConsumed(maxlen);
|
---|
109 | memcpy(data, toSend.constData(), maxlen);
|
---|
110 | return maxlen;
|
---|
111 | }
|
---|
112 |
|
---|
113 | qint64 writeData(const char *, qint64)
|
---|
114 | { return -1; }
|
---|
115 |
|
---|
116 | friend class QNetworkAccessFtpBackend;
|
---|
117 | };
|
---|
118 |
|
---|
119 | class QNetworkAccessFtpFtp: public QFtp, public QNetworkAccessCache::CacheableObject
|
---|
120 | {
|
---|
121 | // Q_OBJECT
|
---|
122 | public:
|
---|
123 | QNetworkAccessFtpFtp()
|
---|
124 | {
|
---|
125 | setExpires(true);
|
---|
126 | setShareable(false);
|
---|
127 | }
|
---|
128 |
|
---|
129 | void dispose()
|
---|
130 | {
|
---|
131 | connect(this, SIGNAL(done(bool)), this, SLOT(deleteLater()));
|
---|
132 | close();
|
---|
133 | }
|
---|
134 | };
|
---|
135 |
|
---|
136 | QNetworkAccessFtpBackend::QNetworkAccessFtpBackend()
|
---|
137 | : ftp(0), uploadDevice(0), totalBytes(0), helpId(-1), sizeId(-1), mdtmId(-1),
|
---|
138 | supportsSize(false), supportsMdtm(false), state(Idle)
|
---|
139 | {
|
---|
140 | }
|
---|
141 |
|
---|
142 | QNetworkAccessFtpBackend::~QNetworkAccessFtpBackend()
|
---|
143 | {
|
---|
144 | disconnectFromFtp();
|
---|
145 | }
|
---|
146 |
|
---|
147 | void QNetworkAccessFtpBackend::open()
|
---|
148 | {
|
---|
149 | #ifndef QT_NO_NETWORKPROXY
|
---|
150 | QNetworkProxy proxy;
|
---|
151 | foreach (const QNetworkProxy &p, proxyList()) {
|
---|
152 | // use the first FTP proxy
|
---|
153 | // or no proxy at all
|
---|
154 | if (p.type() == QNetworkProxy::FtpCachingProxy
|
---|
155 | || p.type() == QNetworkProxy::NoProxy) {
|
---|
156 | proxy = p;
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | // did we find an FTP proxy or a NoProxy?
|
---|
162 | if (proxy.type() == QNetworkProxy::DefaultProxy) {
|
---|
163 | // unsuitable proxies
|
---|
164 | error(QNetworkReply::ProxyNotFoundError,
|
---|
165 | tr("No suitable proxy found"));
|
---|
166 | finished();
|
---|
167 | return;
|
---|
168 | }
|
---|
169 |
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | QUrl url = this->url();
|
---|
173 | if (url.path().isEmpty()) {
|
---|
174 | url.setPath(QLatin1String("/"));
|
---|
175 | setUrl(url);
|
---|
176 | }
|
---|
177 | if (url.path().endsWith(QLatin1Char('/'))) {
|
---|
178 | error(QNetworkReply::ContentOperationNotPermittedError,
|
---|
179 | tr("Cannot open %1: is a directory").arg(url.toString()));
|
---|
180 | finished();
|
---|
181 | return;
|
---|
182 | }
|
---|
183 | state = LoggingIn;
|
---|
184 |
|
---|
185 | QNetworkAccessCache* cache = QNetworkAccessManagerPrivate::getCache(this);
|
---|
186 | QByteArray cacheKey = makeCacheKey(url);
|
---|
187 | if (!cache->requestEntry(cacheKey, this,
|
---|
188 | SLOT(ftpConnectionReady(QNetworkAccessCache::CacheableObject*)))) {
|
---|
189 | ftp = new QNetworkAccessFtpFtp;
|
---|
190 | #ifndef QT_NO_NETWORKPROXY
|
---|
191 | if (proxy.type() == QNetworkProxy::FtpCachingProxy)
|
---|
192 | ftp->setProxy(proxy.hostName(), proxy.port());
|
---|
193 | #endif
|
---|
194 | ftp->connectToHost(url.host(), url.port(DefaultFtpPort));
|
---|
195 | ftp->login(url.userName(), url.password());
|
---|
196 |
|
---|
197 | cache->addEntry(cacheKey, ftp);
|
---|
198 | ftpConnectionReady(ftp);
|
---|
199 | }
|
---|
200 |
|
---|
201 | uploadDevice = new QNetworkAccessFtpIODevice(this);
|
---|
202 | }
|
---|
203 |
|
---|
204 | void QNetworkAccessFtpBackend::closeDownstreamChannel()
|
---|
205 | {
|
---|
206 | state = Disconnecting;
|
---|
207 | if (operation() == QNetworkAccessManager::GetOperation)
|
---|
208 | #ifndef Q_OS_WINCE
|
---|
209 | abort();
|
---|
210 | #else
|
---|
211 | exit(3);
|
---|
212 | #endif
|
---|
213 | }
|
---|
214 |
|
---|
215 | void QNetworkAccessFtpBackend::closeUpstreamChannel()
|
---|
216 | {
|
---|
217 | if (operation() == QNetworkAccessManager::PutOperation) {
|
---|
218 | Q_ASSERT(uploadDevice);
|
---|
219 | uploadDevice->eof = true;
|
---|
220 | if (!upstreamBytesAvailable())
|
---|
221 | emit uploadDevice->readyRead();
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | bool QNetworkAccessFtpBackend::waitForDownstreamReadyRead(int ms)
|
---|
226 | {
|
---|
227 | if (!ftp)
|
---|
228 | return false;
|
---|
229 |
|
---|
230 | if (ftp->bytesAvailable()) {
|
---|
231 | ftpReadyRead();
|
---|
232 | return true;
|
---|
233 | }
|
---|
234 |
|
---|
235 | if (ms == 0)
|
---|
236 | return false;
|
---|
237 |
|
---|
238 | qCritical("QNetworkAccess: FTP backend does not support waitForReadyRead()");
|
---|
239 | return false;
|
---|
240 | }
|
---|
241 |
|
---|
242 | bool QNetworkAccessFtpBackend::waitForUpstreamBytesWritten(int ms)
|
---|
243 | {
|
---|
244 | Q_UNUSED(ms);
|
---|
245 | qCritical("QNetworkAccess: FTP backend does not support waitForBytesWritten()");
|
---|
246 | return false;
|
---|
247 | }
|
---|
248 |
|
---|
249 | void QNetworkAccessFtpBackend::upstreamReadyRead()
|
---|
250 | {
|
---|
251 | // uh... how does QFtp operate?
|
---|
252 | }
|
---|
253 |
|
---|
254 | void QNetworkAccessFtpBackend::downstreamReadyWrite()
|
---|
255 | {
|
---|
256 | if (state == Transferring && ftp && ftp->bytesAvailable())
|
---|
257 | ftpReadyRead();
|
---|
258 | }
|
---|
259 |
|
---|
260 | void QNetworkAccessFtpBackend::ftpConnectionReady(QNetworkAccessCache::CacheableObject *o)
|
---|
261 | {
|
---|
262 | ftp = static_cast<QNetworkAccessFtpFtp *>(o);
|
---|
263 | connect(ftp, SIGNAL(done(bool)), SLOT(ftpDone()));
|
---|
264 | connect(ftp, SIGNAL(rawCommandReply(int,QString)), SLOT(ftpRawCommandReply(int,QString)));
|
---|
265 | connect(ftp, SIGNAL(readyRead()), SLOT(ftpReadyRead()));
|
---|
266 |
|
---|
267 | // is the login process done already?
|
---|
268 | if (ftp->state() == QFtp::LoggedIn)
|
---|
269 | ftpDone();
|
---|
270 |
|
---|
271 | // no, defer the actual operation until after we've logged in
|
---|
272 | }
|
---|
273 |
|
---|
274 | void QNetworkAccessFtpBackend::disconnectFromFtp()
|
---|
275 | {
|
---|
276 | state = Disconnecting;
|
---|
277 |
|
---|
278 | if (ftp) {
|
---|
279 | disconnect(ftp, 0, this, 0);
|
---|
280 |
|
---|
281 | QByteArray key = makeCacheKey(url());
|
---|
282 | QNetworkAccessManagerPrivate::getCache(this)->releaseEntry(key);
|
---|
283 |
|
---|
284 | ftp = 0;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | void QNetworkAccessFtpBackend::ftpDone()
|
---|
289 | {
|
---|
290 | // the last command we sent is done
|
---|
291 | if (state == LoggingIn && ftp->state() != QFtp::LoggedIn) {
|
---|
292 | if (ftp->state() == QFtp::Connected) {
|
---|
293 | // the login did not succeed
|
---|
294 | QUrl newUrl = url();
|
---|
295 | newUrl.setUserInfo(QString());
|
---|
296 | setUrl(newUrl);
|
---|
297 |
|
---|
298 | QAuthenticator auth;
|
---|
299 | authenticationRequired(&auth);
|
---|
300 |
|
---|
301 | if (!auth.isNull()) {
|
---|
302 | // try again:
|
---|
303 | newUrl.setUserName(auth.user());
|
---|
304 | ftp->login(auth.user(), auth.password());
|
---|
305 | return;
|
---|
306 | }
|
---|
307 |
|
---|
308 | error(QNetworkReply::AuthenticationRequiredError,
|
---|
309 | tr("Logging in to %1 failed: authentication required")
|
---|
310 | .arg(url().host()));
|
---|
311 | } else {
|
---|
312 | // we did not connect
|
---|
313 | QNetworkReply::NetworkError code;
|
---|
314 | switch (ftp->error()) {
|
---|
315 | case QFtp::HostNotFound:
|
---|
316 | code = QNetworkReply::HostNotFoundError;
|
---|
317 | break;
|
---|
318 |
|
---|
319 | case QFtp::ConnectionRefused:
|
---|
320 | code = QNetworkReply::ConnectionRefusedError;
|
---|
321 | break;
|
---|
322 |
|
---|
323 | default:
|
---|
324 | code = QNetworkReply::ProtocolFailure;
|
---|
325 | break;
|
---|
326 | }
|
---|
327 |
|
---|
328 | error(code, ftp->errorString());
|
---|
329 | }
|
---|
330 |
|
---|
331 | // we're not connected, so remove the cache entry:
|
---|
332 | QByteArray key = makeCacheKey(url());
|
---|
333 | QNetworkAccessManagerPrivate::getCache(this)->removeEntry(key);
|
---|
334 |
|
---|
335 | disconnect(ftp, 0, this, 0);
|
---|
336 | ftp->dispose();
|
---|
337 | ftp = 0;
|
---|
338 |
|
---|
339 | state = Disconnecting;
|
---|
340 | finished();
|
---|
341 | return;
|
---|
342 | }
|
---|
343 |
|
---|
344 | // check for errors:
|
---|
345 | if (ftp->error() != QFtp::NoError) {
|
---|
346 | QString msg;
|
---|
347 | if (operation() == QNetworkAccessManager::GetOperation)
|
---|
348 | msg = tr("Error while downloading %1: %2");
|
---|
349 | else
|
---|
350 | msg = tr("Error while uploading %1: %2");
|
---|
351 | msg = msg.arg(url().toString(), ftp->errorString());
|
---|
352 |
|
---|
353 | if (state == Statting)
|
---|
354 | // file probably doesn't exist
|
---|
355 | error(QNetworkReply::ContentNotFoundError, msg);
|
---|
356 | else
|
---|
357 | error(QNetworkReply::ContentAccessDenied, msg);
|
---|
358 |
|
---|
359 | disconnectFromFtp();
|
---|
360 | finished();
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (state == LoggingIn) {
|
---|
364 | state = CheckingFeatures;
|
---|
365 | if (operation() == QNetworkAccessManager::GetOperation) {
|
---|
366 | // send help command to find out if server supports "SIZE" and "MDTM"
|
---|
367 | QString command = url().path();
|
---|
368 | command.prepend(QLatin1String("%1 "));
|
---|
369 | helpId = ftp->rawCommand(QLatin1String("HELP")); // get supported commands
|
---|
370 | } else {
|
---|
371 | ftpDone();
|
---|
372 | }
|
---|
373 | } else if (state == CheckingFeatures) {
|
---|
374 | state = Statting;
|
---|
375 | if (operation() == QNetworkAccessManager::GetOperation) {
|
---|
376 | // logged in successfully, send the stat requests (if supported)
|
---|
377 | QString command = url().path();
|
---|
378 | command.prepend(QLatin1String("%1 "));
|
---|
379 | if (supportsSize)
|
---|
380 | sizeId = ftp->rawCommand(command.arg(QLatin1String("SIZE"))); // get size
|
---|
381 | if (supportsMdtm)
|
---|
382 | mdtmId = ftp->rawCommand(command.arg(QLatin1String("MDTM"))); // get modified time
|
---|
383 | if (!supportsSize && !supportsMdtm)
|
---|
384 | ftpDone(); // no commands sent, move to the next state
|
---|
385 | } else {
|
---|
386 | ftpDone();
|
---|
387 | }
|
---|
388 | } else if (state == Statting) {
|
---|
389 | // statted successfully, send the actual request
|
---|
390 | emit metaDataChanged();
|
---|
391 | state = Transferring;
|
---|
392 |
|
---|
393 | QFtp::TransferType type = QFtp::Binary;
|
---|
394 | if (operation() == QNetworkAccessManager::GetOperation) {
|
---|
395 | setCachingEnabled(true);
|
---|
396 | ftp->get(url().path(), 0, type);
|
---|
397 | } else {
|
---|
398 | ftp->put(uploadDevice, url().path(), type);
|
---|
399 | }
|
---|
400 |
|
---|
401 | } else if (state == Transferring) {
|
---|
402 | // upload or download finished
|
---|
403 | disconnectFromFtp();
|
---|
404 | finished();
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | void QNetworkAccessFtpBackend::ftpReadyRead()
|
---|
409 | {
|
---|
410 | writeDownstreamData(ftp->readAll());
|
---|
411 | }
|
---|
412 |
|
---|
413 | void QNetworkAccessFtpBackend::ftpRawCommandReply(int code, const QString &text)
|
---|
414 | {
|
---|
415 | //qDebug() << "FTP reply:" << code << text;
|
---|
416 | int id = ftp->currentId();
|
---|
417 |
|
---|
418 | if ((id == helpId) && ((code == 200) || (code == 214))) { // supported commands
|
---|
419 | // the "FEAT" ftp command would be nice here, but it is not part of the
|
---|
420 | // initial FTP RFC 959, neither ar "SIZE" nor "MDTM" (they are all specified
|
---|
421 | // in RFC 3659)
|
---|
422 | if (text.contains(QLatin1String("SIZE"), Qt::CaseSensitive))
|
---|
423 | supportsSize = true;
|
---|
424 | if (text.contains(QLatin1String("MDTM"), Qt::CaseSensitive))
|
---|
425 | supportsMdtm = true;
|
---|
426 | } else if (code == 213) { // file status
|
---|
427 | if (id == sizeId) {
|
---|
428 | // reply to the size command
|
---|
429 | setHeader(QNetworkRequest::ContentLengthHeader, text.toLongLong());
|
---|
430 | #ifndef QT_NO_DATESTRING
|
---|
431 | } else if (id == mdtmId) {
|
---|
432 | QDateTime dt = QDateTime::fromString(text, QLatin1String("yyyyMMddHHmmss"));
|
---|
433 | setHeader(QNetworkRequest::LastModifiedHeader, dt);
|
---|
434 | #endif
|
---|
435 | }
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | QT_END_NAMESPACE
|
---|
440 |
|
---|
441 | #endif // QT_NO_FTP
|
---|