diff ui/sslconnection_bare.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 9bcd8ed36f4b
line wrap: on
line diff
--- a/ui/sslconnection_bare.cpp	Thu Aug 14 11:24:13 2014 +0200
+++ b/ui/sslconnection_bare.cpp	Mon Aug 18 18:51:33 2014 +0200
@@ -11,6 +11,7 @@
 #include "sslhelp.h"
 
 #include <QFile>
+#include <QSaveFile>
 #include <QUuid>
 #include <QApplication>
 
@@ -359,4 +360,113 @@
     return retval;
 }
 
+QMap<QString, QString> SSLConnectionBare::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.";
+        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 SSLConnectionBare::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 = write(headRequest.toUtf8());
+    if (ret != 0) {
+        return QDateTime();
+    }
+
+    response = read(1024);
+
+    qDebug() << "Response from server was: " << response;
+
+    if (response.isNull()) {
+        qDebug() << "No response";
+        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;
+        }
+    }
+    return QDateTime();
+}
+
+bool SSLConnectionBare::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 = write(getRequest.toUtf8());
+
+    // Open / Create the file to write to.
+    if (!outputFile.open(QIODevice::WriteOnly)) {
+        qDebug() << "Failed to open file";
+        return false;
+    }
+
+    bool inBody = false;
+    QMap <QString, QString> headers;
+    do {
+        /* Read the response in 8KiB chunks */
+        int responseSize = 0;
+        QByteArray response = read(8192);
+        if (response.isNull()) {
+            qDebug() << "Error reading response";
+            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;
+        }
+    } while (bytesRead < maxSize);
+
+    return outputFile.commit();
+}
+

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