Browse Source

Fix downloadthread.* coding style (Issue #2192).

adaptive-webui-19844
Vladimir Golovnev (Glassez) 10 years ago
parent
commit
f1bce0b8e0
  1. 499
      src/core/downloadthread.cpp
  2. 35
      src/core/downloadthread.h

499
src/core/downloadthread.cpp

@ -43,270 +43,299 @@
/** Download Thread **/ /** Download Thread **/
DownloadThread::DownloadThread(QObject* parent) : QObject(parent) { DownloadThread::DownloadThread(QObject* parent)
connect(&m_networkManager, SIGNAL(finished (QNetworkReply*)), this, SLOT(processDlFinished(QNetworkReply*))); : QObject(parent)
{
connect(&m_networkManager, SIGNAL(finished (QNetworkReply*)), this, SLOT(processDlFinished(QNetworkReply*)));
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
connect(&m_networkManager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(ignoreSslErrors(QNetworkReply*,QList<QSslError>))); connect(&m_networkManager, SIGNAL(sslErrors(QNetworkReply*, QList<QSslError>)), this, SLOT(ignoreSslErrors(QNetworkReply*, QList<QSslError>)));
#endif #endif
} }
QByteArray DownloadThread::gUncompress(Bytef *inData, size_t len) { QByteArray DownloadThread::gUncompress(Bytef *inData, size_t len)
if (len <= 4) { {
qWarning("gUncompress: Input data is truncated"); if (len <= 4) {
return QByteArray(); qWarning("gUncompress: Input data is truncated");
}
QByteArray result;
z_stream strm;
static const int CHUNK_SIZE = 1024;
char out[CHUNK_SIZE];
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = len;
strm.next_in = inData;
const int windowBits = 15;
const int ENABLE_ZLIB_GZIP = 32;
int ret = inflateInit2(&strm, windowBits|ENABLE_ZLIB_GZIP ); // gzip decoding
if (ret != Z_OK)
return QByteArray();
// run inflate()
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = reinterpret_cast<unsigned char*>(out);
ret = inflate(&strm, Z_NO_FLUSH);
Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered
switch (ret) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void) inflateEnd(&strm);
return QByteArray(); return QByteArray();
} }
result.append(out, CHUNK_SIZE - strm.avail_out); QByteArray result;
} while (!strm.avail_out);
z_stream strm;
static const int CHUNK_SIZE = 1024;
char out[CHUNK_SIZE];
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = len;
strm.next_in = inData;
const int windowBits = 15;
const int ENABLE_ZLIB_GZIP = 32;
int ret = inflateInit2(&strm, windowBits | ENABLE_ZLIB_GZIP); // gzip decoding
if (ret != Z_OK)
return QByteArray();
// run inflate()
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = reinterpret_cast<unsigned char*>(out);
ret = inflate(&strm, Z_NO_FLUSH);
Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered
switch (ret) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void) inflateEnd(&strm);
return QByteArray();
}
// clean up and return result.append(out, CHUNK_SIZE - strm.avail_out);
inflateEnd(&strm); }
return result; while (!strm.avail_out);
// clean up and return
inflateEnd(&strm);
return result;
} }
void DownloadThread::processDlFinished(QNetworkReply* reply) { void DownloadThread::processDlFinished(QNetworkReply *reply)
QString url = reply->url().toString(); {
qDebug("Download finished: %s", qPrintable(url)); QString url = reply->url().toString();
// Check if the request was successful qDebug("Download finished: %s", qPrintable(url));
if (reply->error() != QNetworkReply::NoError) { // Check if the request was successful
// Failure if (reply->error() != QNetworkReply::NoError) {
qDebug("Download failure (%s), reason: %s", qPrintable(url), qPrintable(errorCodeToString(reply->error()))); // Failure
emit downloadFailure(url, errorCodeToString(reply->error())); qDebug("Download failure (%s), reason: %s", qPrintable(url), qPrintable(errorCodeToString(reply->error())));
reply->deleteLater(); emit downloadFailure(url, errorCodeToString(reply->error()));
return; reply->deleteLater();
} return;
// Check if the server ask us to redirect somewhere lese
const QVariant redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (redirection.isValid()) {
// We should redirect
QUrl newUrl = redirection.toUrl();
// Resolve relative urls
if (newUrl.isRelative())
newUrl = reply->url().resolved(newUrl);
const QString newUrlString = newUrl.toString();
qDebug("Redirecting from %s to %s", qPrintable(url), qPrintable(newUrlString));
// Redirect to magnet workaround
if (newUrlString.startsWith("magnet:", Qt::CaseInsensitive)) {
qDebug("Magnet redirect detected.");
reply->abort();
emit magnetRedirect(newUrlString, url);
reply->deleteLater();
return;
} }
m_redirectMapping.insert(newUrlString, url);
// redirecting with first cookies // Check if the server ask us to redirect somewhere else
downloadUrl(newUrlString, m_networkManager.cookieJar()->cookiesForUrl(url)); const QVariant redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
reply->deleteLater(); if (redirection.isValid()) {
return; // We should redirect
} QUrl newUrl = redirection.toUrl();
// Checking if it was redirected, restoring initial URL // Resolve relative urls
if (m_redirectMapping.contains(url)) { if (newUrl.isRelative())
url = m_redirectMapping.take(url); newUrl = reply->url().resolved(newUrl);
} const QString newUrlString = newUrl.toString();
// Success qDebug("Redirecting from %s to %s", qPrintable(url), qPrintable(newUrlString));
QTemporaryFile *tmpfile = new QTemporaryFile;
if (tmpfile->open()) { // Redirect to magnet workaround
tmpfile->setAutoRemove(false); if (newUrlString.startsWith("magnet:", Qt::CaseInsensitive)) {
QString filePath = tmpfile->fileName(); qDebug("Magnet redirect detected.");
qDebug("Temporary filename is: %s", qPrintable(filePath)); reply->abort();
if (reply->isOpen() || reply->open(QIODevice::ReadOnly)) { emit magnetRedirect(newUrlString, url);
QByteArray replyData = reply->readAll(); reply->deleteLater();
if (reply->rawHeader("Content-Encoding") == "gzip") { return;
// uncompress gzip reply }
replyData = gUncompress(reinterpret_cast<unsigned char*>(replyData.data()), replyData.length());
} m_redirectMapping.insert(newUrlString, url);
tmpfile->write(replyData); // redirecting with first cookies
tmpfile->close(); downloadUrl(newUrlString, m_networkManager.cookieJar()->cookiesForUrl(url));
// XXX: tmpfile needs to be deleted on Windows before using the file reply->deleteLater();
// or it will complain that the file is used by another process. return;
delete tmpfile; }
// Send finished signal
emit downloadFinished(url, filePath); // Checking if it was redirected, restoring initial URL
} else { if (m_redirectMapping.contains(url))
delete tmpfile; url = m_redirectMapping.take(url);
fsutils::forceRemove(filePath);
// Error when reading the request // Success
emit downloadFailure(url, tr("I/O Error")); QTemporaryFile *tmpfile = new QTemporaryFile;
if (tmpfile->open()) {
tmpfile->setAutoRemove(false);
QString filePath = tmpfile->fileName();
qDebug("Temporary filename is: %s", qPrintable(filePath));
if (reply->isOpen() || reply->open(QIODevice::ReadOnly)) {
QByteArray replyData = reply->readAll();
if (reply->rawHeader("Content-Encoding") == "gzip") {
// uncompress gzip reply
replyData = gUncompress(reinterpret_cast<unsigned char*>(replyData.data()), replyData.length());
}
tmpfile->write(replyData);
tmpfile->close();
// XXX: tmpfile needs to be deleted on Windows before using the file
// or it will complain that the file is used by another process.
delete tmpfile;
// Send finished signal
emit downloadFinished(url, filePath);
}
else {
delete tmpfile;
fsutils::forceRemove(filePath);
// Error when reading the request
emit downloadFailure(url, tr("I/O Error"));
}
}
else {
delete tmpfile;
emit downloadFailure(url, tr("I/O Error"));
} }
} else {
delete tmpfile; // Clean up
emit downloadFailure(url, tr("I/O Error")); reply->deleteLater();
}
// Clean up
reply->deleteLater();
} }
void DownloadThread::downloadTorrentUrl(const QString &url, const QList<QNetworkCookie>& cookies) void DownloadThread::downloadTorrentUrl(const QString &url, const QList<QNetworkCookie> &cookies)
{ {
// Process request // Process request
QNetworkReply *reply = downloadUrl(url, cookies); QNetworkReply *reply = downloadUrl(url, cookies);
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64))); connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(checkDownloadSize(qint64, qint64)));
} }
QNetworkReply* DownloadThread::downloadUrl(const QString &url, const QList<QNetworkCookie>& cookies) { QNetworkReply *DownloadThread::downloadUrl(const QString &url, const QList<QNetworkCookie> &cookies)
// Update proxy settings {
applyProxySettings(); // Update proxy settings
// Set cookies applyProxySettings();
if (!cookies.empty()) {
qDebug("Setting %d cookies for url: %s", cookies.size(), qPrintable(url)); // Set cookies
m_networkManager.cookieJar()->setCookiesFromUrl(cookies, url); if (!cookies.empty()) {
} qDebug("Setting %d cookies for url: %s", cookies.size(), qPrintable(url));
// Process download request m_networkManager.cookieJar()->setCookiesFromUrl(cookies, url);
qDebug("url is %s", qPrintable(url)); }
const QUrl qurl = QUrl::fromEncoded(url.toUtf8());
QNetworkRequest request(qurl); // Process download request
// Spoof Firefox 3.5 user agent to avoid qDebug("url is %s", qPrintable(url));
// Web server banning const QUrl qurl = QUrl::fromEncoded(url.toUtf8());
request.setRawHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"); QNetworkRequest request(qurl);
qDebug("Downloading %s...", request.url().toEncoded().data());
qDebug("%d cookies for this URL", m_networkManager.cookieJar()->cookiesForUrl(url).size()); // Spoof Firefox 3.5 user agent to avoid
for (int i=0; i<m_networkManager.cookieJar()->cookiesForUrl(url).size(); ++i) { // Web server banning
qDebug("%s=%s", m_networkManager.cookieJar()->cookiesForUrl(url).at(i).name().data(), m_networkManager.cookieJar()->cookiesForUrl(url).at(i).value().data()); request.setRawHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5");
qDebug("Domain: %s, Path: %s", qPrintable(m_networkManager.cookieJar()->cookiesForUrl(url).at(i).domain()), qPrintable(m_networkManager.cookieJar()->cookiesForUrl(url).at(i).path()));
} qDebug("Downloading %s...", request.url().toEncoded().data());
// accept gzip qDebug("%d cookies for this URL", m_networkManager.cookieJar()->cookiesForUrl(url).size());
request.setRawHeader("Accept-Encoding", "gzip"); for (int i = 0; i < m_networkManager.cookieJar()->cookiesForUrl(url).size(); ++i) {
return m_networkManager.get(request); qDebug("%s=%s", m_networkManager.cookieJar()->cookiesForUrl(url).at(i).name().data(), m_networkManager.cookieJar()->cookiesForUrl(url).at(i).value().data());
qDebug("Domain: %s, Path: %s", qPrintable(m_networkManager.cookieJar()->cookiesForUrl(url).at(i).domain()), qPrintable(m_networkManager.cookieJar()->cookiesForUrl(url).at(i).path()));
}
// accept gzip
request.setRawHeader("Accept-Encoding", "gzip");
return m_networkManager.get(request);
} }
void DownloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) { void DownloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal)
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); {
if (!reply) return; QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (bytesTotal > 0) { if (!reply) return;
// Total number of bytes is available
if (bytesTotal > 1048576*10) { if (bytesTotal > 0) {
// More than 10MB, this is probably not a torrent file, aborting... // Total number of bytes is available
reply->abort(); if (bytesTotal > 10485760) {
reply->deleteLater(); // More than 10MB, this is probably not a torrent file, aborting...
} else { reply->abort();
disconnect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64))); reply->deleteLater();
}
else {
disconnect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(checkDownloadSize(qint64, qint64)));
}
} }
} else { else {
if (bytesReceived > 1048576*10) { if (bytesReceived > 10485760) {
// More than 10MB, this is probably not a torrent file, aborting... // More than 10MB, this is probably not a torrent file, aborting...
reply->abort(); reply->abort();
reply->deleteLater(); reply->deleteLater();
}
} }
}
} }
void DownloadThread::applyProxySettings() { void DownloadThread::applyProxySettings()
QNetworkProxy proxy; {
const Preferences* const pref = Preferences::instance(); QNetworkProxy proxy;
if (pref->isProxyEnabled()) { const Preferences* const pref = Preferences::instance();
// Proxy enabled
proxy.setHostName(pref->getProxyIp()); if (pref->isProxyEnabled()) {
proxy.setPort(pref->getProxyPort()); // Proxy enabled
// Default proxy type is HTTP, we must change if it is SOCKS5 proxy.setHostName(pref->getProxyIp());
const int proxy_type = pref->getProxyType(); proxy.setPort(pref->getProxyPort());
if (proxy_type == Proxy::SOCKS5 || proxy_type == Proxy::SOCKS5_PW) { // Default proxy type is HTTP, we must change if it is SOCKS5
qDebug() << Q_FUNC_INFO << "using SOCKS proxy"; const int proxyType = pref->getProxyType();
proxy.setType(QNetworkProxy::Socks5Proxy); if ((proxyType == Proxy::SOCKS5) || (proxyType == Proxy::SOCKS5_PW)) {
} else { qDebug() << Q_FUNC_INFO << "using SOCKS proxy";
qDebug() << Q_FUNC_INFO << "using HTTP proxy"; proxy.setType(QNetworkProxy::Socks5Proxy);
proxy.setType(QNetworkProxy::HttpProxy); }
else {
qDebug() << Q_FUNC_INFO << "using HTTP proxy";
proxy.setType(QNetworkProxy::HttpProxy);
}
// Authentication?
if (pref->isProxyAuthEnabled()) {
qDebug("Proxy requires authentication, authenticating");
proxy.setUser(pref->getProxyUsername());
proxy.setPassword(pref->getProxyPassword());
}
} }
// Authentication? else {
if (pref->isProxyAuthEnabled()) { proxy.setType(QNetworkProxy::NoProxy);
qDebug("Proxy requires authentication, authenticating");
proxy.setUser(pref->getProxyUsername());
proxy.setPassword(pref->getProxyPassword());
} }
} else {
proxy.setType(QNetworkProxy::NoProxy); m_networkManager.setProxy(proxy);
}
m_networkManager.setProxy(proxy);
} }
QString DownloadThread::errorCodeToString(QNetworkReply::NetworkError status) { QString DownloadThread::errorCodeToString(QNetworkReply::NetworkError status)
switch(status) { {
case QNetworkReply::HostNotFoundError: switch(status) {
return tr("The remote host name was not found (invalid hostname)"); case QNetworkReply::HostNotFoundError:
case QNetworkReply::OperationCanceledError: return tr("The remote host name was not found (invalid hostname)");
return tr("The operation was canceled"); case QNetworkReply::OperationCanceledError:
case QNetworkReply::RemoteHostClosedError: return tr("The operation was canceled");
return tr("The remote server closed the connection prematurely, before the entire reply was received and processed"); case QNetworkReply::RemoteHostClosedError:
case QNetworkReply::TimeoutError: return tr("The remote server closed the connection prematurely, before the entire reply was received and processed");
return tr("The connection to the remote server timed out"); case QNetworkReply::TimeoutError:
case QNetworkReply::SslHandshakeFailedError: return tr("The connection to the remote server timed out");
return tr("SSL/TLS handshake failed"); case QNetworkReply::SslHandshakeFailedError:
case QNetworkReply::ConnectionRefusedError: return tr("SSL/TLS handshake failed");
return tr("The remote server refused the connection"); case QNetworkReply::ConnectionRefusedError:
case QNetworkReply::ProxyConnectionRefusedError: return tr("The remote server refused the connection");
return tr("The connection to the proxy server was refused"); case QNetworkReply::ProxyConnectionRefusedError:
case QNetworkReply::ProxyConnectionClosedError: return tr("The connection to the proxy server was refused");
return tr("The proxy server closed the connection prematurely"); case QNetworkReply::ProxyConnectionClosedError:
case QNetworkReply::ProxyNotFoundError: return tr("The proxy server closed the connection prematurely");
return tr("The proxy host name was not found"); case QNetworkReply::ProxyNotFoundError:
case QNetworkReply::ProxyTimeoutError: return tr("The proxy host name was not found");
return tr("The connection to the proxy timed out or the proxy did not reply in time to the request sent"); case QNetworkReply::ProxyTimeoutError:
case QNetworkReply::ProxyAuthenticationRequiredError: return tr("The connection to the proxy timed out or the proxy did not reply in time to the request sent");
return tr("The proxy requires authentication in order to honour the request but did not accept any credentials offered"); case QNetworkReply::ProxyAuthenticationRequiredError:
case QNetworkReply::ContentAccessDenied: return tr("The proxy requires authentication in order to honour the request but did not accept any credentials offered");
return tr("The access to the remote content was denied (401)"); case QNetworkReply::ContentAccessDenied:
case QNetworkReply::ContentOperationNotPermittedError: return tr("The access to the remote content was denied (401)");
return tr("The operation requested on the remote content is not permitted"); case QNetworkReply::ContentOperationNotPermittedError:
case QNetworkReply::ContentNotFoundError: return tr("The operation requested on the remote content is not permitted");
return tr("The remote content was not found at the server (404)"); case QNetworkReply::ContentNotFoundError:
case QNetworkReply::AuthenticationRequiredError: return tr("The remote content was not found at the server (404)");
return tr("The remote server requires authentication to serve the content but the credentials provided were not accepted"); case QNetworkReply::AuthenticationRequiredError:
case QNetworkReply::ProtocolUnknownError: return tr("The remote server requires authentication to serve the content but the credentials provided were not accepted");
return tr("The Network Access API cannot honor the request because the protocol is not known"); case QNetworkReply::ProtocolUnknownError:
case QNetworkReply::ProtocolInvalidOperationError: return tr("The Network Access API cannot honor the request because the protocol is not known");
return tr("The requested operation is invalid for this protocol"); case QNetworkReply::ProtocolInvalidOperationError:
case QNetworkReply::UnknownNetworkError: return tr("The requested operation is invalid for this protocol");
return tr("An unknown network-related error was detected"); case QNetworkReply::UnknownNetworkError:
case QNetworkReply::UnknownProxyError: return tr("An unknown network-related error was detected");
return tr("An unknown proxy-related error was detected"); case QNetworkReply::UnknownProxyError:
case QNetworkReply::UnknownContentError: return tr("An unknown proxy-related error was detected");
return tr("An unknown error related to the remote content was detected"); case QNetworkReply::UnknownContentError:
case QNetworkReply::ProtocolFailure: return tr("An unknown error related to the remote content was detected");
return tr("A breakdown in protocol was detected"); case QNetworkReply::ProtocolFailure:
default: return tr("A breakdown in protocol was detected");
return tr("Unknown error"); default:
} return tr("Unknown error");
}
} }
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void DownloadThread::ignoreSslErrors(QNetworkReply* reply, const QList<QSslError> &errors) { void DownloadThread::ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
Q_UNUSED(errors) {
// Ignore all SSL errors Q_UNUSED(errors)
reply->ignoreSslErrors(); // Ignore all SSL errors
reply->ignoreSslErrors();
} }
#endif #endif

35
src/core/downloadthread.h

@ -42,35 +42,34 @@ QT_BEGIN_NAMESPACE
class QNetworkAccessManager; class QNetworkAccessManager;
QT_END_NAMESPACE QT_END_NAMESPACE
class DownloadThread : public QObject { class DownloadThread : public QObject
Q_OBJECT {
Q_OBJECT
public: public:
DownloadThread(QObject* parent = 0); DownloadThread(QObject *parent = 0);
QNetworkReply* downloadUrl(const QString &url, const QList<QNetworkCookie>& cookies = QList<QNetworkCookie>()); QNetworkReply *downloadUrl(const QString &url, const QList<QNetworkCookie> &cookies = QList<QNetworkCookie>());
void downloadTorrentUrl(const QString &url, const QList<QNetworkCookie>& cookies = QList<QNetworkCookie>()); void downloadTorrentUrl(const QString &url, const QList<QNetworkCookie> &cookies = QList<QNetworkCookie>());
//void setProxy(QString IP, int port, QString username, QString password);
signals: signals:
void downloadFinished(const QString &url, const QString &file_path); void downloadFinished(const QString &url, const QString &file_path);
void downloadFailure(const QString &url, const QString &reason); void downloadFailure(const QString &url, const QString &reason);
void magnetRedirect(const QString &url_new, const QString &url_old); void magnetRedirect(const QString &url_new, const QString &url_old);
private slots: private slots:
void processDlFinished(QNetworkReply* reply); void processDlFinished(QNetworkReply *reply);
void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal); void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal);
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void ignoreSslErrors(QNetworkReply*,const QList<QSslError>&); void ignoreSslErrors(QNetworkReply *,const QList<QSslError> &);
#endif #endif
private: private:
static QByteArray gUncompress(Bytef *inData, size_t len); static QByteArray gUncompress(Bytef *inData, size_t len);
QString errorCodeToString(QNetworkReply::NetworkError status); QString errorCodeToString(QNetworkReply::NetworkError status);
void applyProxySettings(); void applyProxySettings();
private: QNetworkAccessManager m_networkManager;
QNetworkAccessManager m_networkManager; QHash<QString, QString> m_redirectMapping;
QHash<QString, QString> m_redirectMapping;
}; };

Loading…
Cancel
Save