diff options
author | kh1 <karsten.heimrich@nokia.com> | 2011-11-03 15:26:30 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@nokia.com> | 2011-11-03 16:17:31 +0100 |
commit | 3e742ba4937aa1336314850c27aa8874400d8533 (patch) | |
tree | 2c31d067437028f86a6b72f750d1e3d4efdeb352 /installerbuilder | |
parent | c950c215cc065fa89e1cdad7f69650c0211b8d75 (diff) |
Implement basic authentication and proxy support.
Change-Id: I4ec8966cb15497bdcc1332ab89cedbdace7240ee
Reviewed-by: Alexander Lenhardt <alexander.lenhardt@nokia.com>
Reviewed-by: Tim Jenssen <tim.jenssen@nokia.com>
Diffstat (limited to 'installerbuilder')
3 files changed, 88 insertions, 8 deletions
diff --git a/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader.cpp b/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader.cpp index b61f7ab41..0c564cda9 100644 --- a/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader.cpp +++ b/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader.cpp @@ -214,10 +214,16 @@ struct KDUpdater::FileDownloader::FileDownloaderData , m_currentSpeedBin(0) , m_sampleIndex(0) , m_downloadSpeed(0) + , m_factory(0) { memset(m_samples, 0, sizeof(m_samples)); } + ~FileDownloaderData() + { + delete m_factory; + } + QUrl url; QString scheme; QByteArray sha1Sum; @@ -235,6 +241,9 @@ struct KDUpdater::FileDownloader::FileDownloaderData mutable qint64 m_currentSpeedBin; mutable quint32 m_sampleIndex; mutable qint64 m_downloadSpeed; + + QAuthenticator m_authenticator; + FileDownloaderProxyFactory *m_factory; }; KDUpdater::FileDownloader::FileDownloader(const QString &scheme, QObject* parent) @@ -470,7 +479,45 @@ void KDUpdater::FileDownloader::emitEstimatedDownloadTime() emit estimatedDownloadTime((d->m_bytesToReceive - d->m_bytesReceived) / d->m_downloadSpeed); } +/*! + Returns a copy of the proxy factory that this FileDownloader object is using to determine the proxies to + be used for requests. +*/ +FileDownloaderProxyFactory *KDUpdater::FileDownloader::proxyFactory() const +{ + if (d->m_factory) + return d->m_factory->clone(); + return 0; +} +/*! + Sets the proxy factory for this class to be \a factory. A proxy factory is used to determine a more + specific list of proxies to be used for a given request, instead of trying to use the same proxy value + for all requests. This might only be of use for http or ftp requests. +*/ +void KDUpdater::FileDownloader::setProxyFactory(FileDownloaderProxyFactory *factory) +{ + delete d->m_factory; + d->m_factory = factory; +} + +/*! + Returns a copy of the authenticator that this FileDownloader object is using to set the username and + password for download request. +*/ +QAuthenticator KDUpdater::FileDownloader::authenticator() const +{ + return d->m_authenticator; +} + +/*! + Sets the authenticator object for this class to be \a authenticator. A authenticator is used to + pass on the required authentication information. This might only be of use for http or ftp requests. +*/ +void KDUpdater::FileDownloader::setAuthenticator(const QAuthenticator &authenticator) +{ + d->m_authenticator = authenticator; +} // -- KDUpdater::LocalFileDownloader @@ -834,8 +881,15 @@ void KDUpdater::FtpDownloader::doDownload() qint64))); connect(d->ftp, SIGNAL(readyRead()), this, SLOT(ftpReadyRead())); + if (FileDownloaderProxyFactory *factory = proxyFactory()) { + const QList<QNetworkProxy> proxies = factory->queryProxy(QNetworkProxyQuery(url())); + if (!proxies.isEmpty()) + d->ftp->setProxy(proxies.at(0).hostName(), proxies.at(0).port()); + delete factory; + } + d->ftp->connectToHost(url().host(), url().port(21)); - d->ftp->login(); + d->ftp->login(authenticator().user(), authenticator().password()); } QString KDUpdater::FtpDownloader::downloadedFileName() const @@ -1004,7 +1058,8 @@ struct KDUpdater::HttpDownloader::HttpDownloaderData , destination(0) , downloaded(false) , aborted(false) - , retrying(false) { } + , retrying(false) + , m_authenticationDone(false) { } HttpDownloader* const q; QNetworkAccessManager manager; @@ -1014,6 +1069,7 @@ struct KDUpdater::HttpDownloader::HttpDownloaderData bool downloaded; bool aborted; bool retrying; + bool m_authenticationDone; void shutDown() { @@ -1031,6 +1087,8 @@ KDUpdater::HttpDownloader::HttpDownloader(QObject* parent) : KDUpdater::FileDownloader(QLatin1String("http"), parent) , d (new HttpDownloaderData(this)) { + connect(&d->manager, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), this, + SLOT(onAuthenticationRequired(QNetworkReply*, QAuthenticator*))); } KDUpdater::HttpDownloader::~HttpDownloader() @@ -1059,12 +1117,6 @@ void KDUpdater::HttpDownloader::doDownload() if (d->http) return; - //// In a future update, authentication should also be supported. - //connect(&d->manager, SIGNAL(authenticationRequired(QString, QAuthenticator*)), this, - // SLOT(httpAuth(QString,QAuthenticator*))); - //connect(&d->manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy, QAuthenticator*)), this, - // SLOT(httpProxyAuth(QNetworkProxy, QAuthenticator*))); - startDownload(url()); runDownloadSpeedTimer(); } @@ -1211,6 +1263,8 @@ void KDUpdater::HttpDownloader::timerEvent(QTimerEvent *event) void KDUpdater::HttpDownloader::startDownload(const QUrl &url) { + d->m_authenticationDone = false; + d->manager.setProxyFactory(proxyFactory()); d->http = d->manager.get(QNetworkRequest(url)); connect(d->http, SIGNAL(readyRead()), this, SLOT(httpReadyRead())); @@ -1236,6 +1290,16 @@ void KDUpdater::HttpDownloader::startDownload(const QUrl &url) } } +void KDUpdater::HttpDownloader::onAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator) +{ + qDebug() << reply->readAll(); + if (!d->m_authenticationDone) { + d->m_authenticationDone = true; + authenticator->setUser(this->authenticator().user()); + authenticator->setPassword(this->authenticator().password()); + } +} + // -- SignatureVerificationDownloader diff --git a/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader.h b/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader.h index 9f10f4cf4..def79a236 100644 --- a/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader.h +++ b/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader.h @@ -30,6 +30,9 @@ #include <QtCore/QUrl> #include <QtCore/QCryptographicHash> +#include <QtNetwork/QAuthenticator> +#include <QtNetwork/QNetworkProxyFactory> + namespace KDUpdater { KDTOOLS_UPDATER_EXPORT QByteArray calculateHash(QIODevice* device, QCryptographicHash::Algorithm algo); @@ -37,6 +40,12 @@ namespace KDUpdater class HashVerificationJob; + class KDTOOLS_UPDATER_EXPORT FileDownloaderProxyFactory : public QNetworkProxyFactory + { + public: + virtual FileDownloaderProxyFactory *clone() = 0; + }; + class KDTOOLS_UPDATER_EXPORT FileDownloader : public QObject { Q_OBJECT @@ -71,6 +80,12 @@ namespace KDUpdater void setFollowRedirects(bool val); bool followRedirects() const; + FileDownloaderProxyFactory *proxyFactory() const; + void setProxyFactory(FileDownloaderProxyFactory *factory); + + QAuthenticator authenticator() const; + void setAuthenticator(const QAuthenticator &authenticator); + public Q_SLOTS: virtual void cancelDownload(); void sha1SumVerified(KDUpdater::HashVerificationJob* job); diff --git a/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader_p.h b/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader_p.h index 005dae035..452fdbe59 100644 --- a/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader_p.h +++ b/installerbuilder/libinstaller/3rdparty/kdtools/KDUpdater/kdupdaterfiledownloader_p.h @@ -197,6 +197,7 @@ namespace KDUpdater void httpError(QNetworkReply::NetworkError); void httpDone(bool error); void httpReqFinished(); + void onAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator); private: void startDownload(const QUrl &url); |