diff ui/downloader.cpp @ 910:eaed02defe6a

More SSLConnection refactoring. Fixes curl downloader.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 18 Aug 2014 18:51:33 +0200
parents d1c951b3012d
children 75085f7adc78
line wrap: on
line diff
--- a/ui/downloader.cpp	Thu Aug 14 11:24:13 2014 +0200
+++ b/ui/downloader.cpp	Mon Aug 18 18:51:33 2014 +0200
@@ -78,127 +78,6 @@
     return cDir.absolutePath();
 }
 
-QMap<QString, QString> Downloader::parseHeaders(QByteArray *data)
-{
-    int bodyStart = data->indexOf("\r\n\r\n");
-    QMap<QString, QString> retval;
-    QByteArray headers;
-    QString response(*data);
-    if (bodyStart == -1) {
-        qDebug() << "Could not find header end.";
-        emit error(tr("Invalid response"),
-                SSLConnection::InvalidResponse);
-        return retval;
-    }
-
-    /* Take the headers with one additional line break */
-    headers = data->left(bodyStart + 2);
-    /* Chop off the head */
-
-    foreach (const QString& line, response.split("\r\n")) {
-        int sepPos = -1;
-        sepPos = line.indexOf(": ");
-        if (sepPos == -1) {
-            continue;
-        }
-        QString key = line.left(sepPos);
-        QString value = line.right(line.size() - sepPos - 2);
-
-        retval.insert(key, value);
-    }
-
-    *data = data->right(data->size() - bodyStart - 4);
-    return retval;
-}
-
-QDateTime Downloader::getLastModifiedHeader(const QString &resource) {
-    int ret = -1;
-    QByteArray response;
-    QLocale cLocale = QLocale::c();
-    QMap<QString, QString> headers;
-    QString headRequest =
-        QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource);
-
-    ret = mSSLConnection->write(headRequest.toUtf8());
-    if (ret != 0) {
-        emit error (tr("Connection lost"), SSLConnection::ConnectionLost);
-        return QDateTime();
-    }
-
-    response = mSSLConnection->read(1024);
-
-    qDebug() << "Response from server was: " << response;
-
-    if (response.isNull()) {
-        qDebug() << "No response";
-        emit error (tr("Connection lost"), SSLConnection::ConnectionLost);
-        return QDateTime();
-    }
-
-    headers = parseHeaders(&response);
-    const QString lastModified = headers.value("Last-Modified");
-    if (!lastModified.isEmpty()) {
-        QDateTime candidate = cLocale.toDateTime(lastModified,
-                "ddd, dd MMM yyyy HH:mm:ss' GMT'");
-        if (candidate.isValid()) {
-            return candidate;
-        }
-    }
-    emit error (tr("Invalid response from the server"), SSLConnection::InvalidResponse);
-    return QDateTime();
-}
-
-bool Downloader::downloadFile(const QString &resource,
-                              const QString &fileName,
-                              size_t maxSize)
-{
-    int ret = -1;
-    size_t bytesRead = 0;
-    QString getRequest =
-        QString::fromLatin1("GET %1 HTTP/1.0\r\n\r\n").arg(resource);
-
-    QSaveFile outputFile(fileName);
-
-    ret = mSSLConnection->write(getRequest.toUtf8());
-
-    // Open / Create the file to write to.
-    if (!outputFile.open(QIODevice::WriteOnly)) {
-        qDebug() << "Failed to open file";
-        return false;
-    }
-
-    if (ret != 0) {
-        emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
-        return false;
-    }
-
-    bool inBody = false;
-    QMap <QString, QString> headers;
-    do {
-        /* Read the response in 8KiB chunks */
-        int responseSize = 0;
-        QByteArray response = mSSLConnection->read(8192);
-        if (response.isNull()) {
-            qDebug() << "Error reading response";
-            emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
-            return false;
-        }
-        responseSize = response.size();
-        if (!inBody) {
-            headers = parseHeaders(&response);
-            inBody = true;
-        }
-        outputFile.write(response);
-        bytesRead += responseSize;
-        if (responseSize < 8192) {
-            /* Nothing more to read */
-            break;
-        }
-        /* TODO Emit progress */
-    } while (bytesRead < maxSize);
-
-    return outputFile.commit();
-}
 
 void Downloader::run() {
     int ret;
@@ -220,10 +99,11 @@
 
     emit progress(tr("Connected"), 1, -1);
 
-    remoteModSW = getLastModifiedHeader(mResourceSW);
+    remoteModSW = mSSLConnection->getLastModifiedHeader(mResourceSW);
     emit lastModifiedDate(remoteModSW);
 
     if (!remoteModSW.isValid()) {
+        emit error (tr("Connection failed."), SSLConnection::InvalidResponse);
         qDebug() << "Could not parse headers for Software";
         return;
     }
@@ -247,7 +127,8 @@
         qDebug() << "fileName: " << fileName;
 
         if (mDownloadSW) {
-            if (!downloadFile(mResourceSW, fileName, MAX_SW_SIZE)) {
+            if (!mSSLConnection->downloadFile(mResourceSW, fileName, MAX_SW_SIZE)) {
+                emit error(tr("Failed to download File"), SSLConnection::ConnectionLost);
                 qDebug() << "Failed to download software update.";
                 return;
             }
@@ -260,8 +141,9 @@
         return;
     }
 
-    remoteModList = getLastModifiedHeader(mResourceList);
+    remoteModList = mSSLConnection->getLastModifiedHeader(mResourceList);
     if (!remoteModList.isValid()) {
+        emit error (tr("Connection failed."), SSLConnection::InvalidResponse);
         qDebug() << "Could not parse headers for List";
         return;
     }
@@ -280,7 +162,7 @@
 
         qDebug() << "fileName: " << fileName;
 
-        if (!downloadFile(mResourceList, fileName, MAX_LIST_SIZE)) {
+        if (!mSSLConnection->downloadFile(mResourceList, fileName, MAX_LIST_SIZE)) {
             return;
         }
 

http://wald.intevation.org/projects/trustbridge/