comparison ui/downloader.cpp @ 908:d1c951b3012d

Curl based implementation of sslconnection
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 13 Aug 2014 19:35:08 +0200
parents 4b702f6cf9b6
children eaed02defe6a
comparison
equal deleted inserted replaced
907:7bd75417e14e 908:d1c951b3012d
26 #include <polarssl/certs.h> 26 #include <polarssl/certs.h>
27 27
28 #define MAX_SW_SIZE 15728640 28 #define MAX_SW_SIZE 15728640
29 #define MAX_LIST_SIZE 1048576 29 #define MAX_LIST_SIZE 1048576
30 30
31 #include "sslconnection_curl.h"
32 #include "sslconnection_bare.h"
31 33
32 Downloader::Downloader(QObject* parent, const QString& url, 34 Downloader::Downloader(QObject* parent, const QString& url,
33 const QByteArray& certificate, 35 const QByteArray& certificate,
34 const QDateTime& newestSW, 36 const QDateTime& newestSW,
35 const QDateTime& newestList, 37 const QDateTime& newestList,
39 QThread(parent), 41 QThread(parent),
40 mLastModSW(newestSW), 42 mLastModSW(newestSW),
41 mLastModList(newestList), 43 mLastModList(newestList),
42 mResourceSW(resourceSW), 44 mResourceSW(resourceSW),
43 mResourceList(resourceList), 45 mResourceList(resourceList),
44 mDownloadSW(downloadSW), 46 mDownloadSW(downloadSW)
45 mSSLConnection(url, certificate) 47 {
46 { 48 #ifdef USE_CURL
49 mSSLConnection = new SSLConnectionCurl(url, certificate);
50 #else
51 mSSLConnection = new SSLConnectionBare(url, certificate);
52 #endif
47 } 53 }
48 54
49 55
50 Downloader::~Downloader() { 56 Downloader::~Downloader() {
57 delete mSSLConnection;
51 } 58 }
52 59
53 QString Downloader::getDataDirectory() 60 QString Downloader::getDataDirectory()
54 { 61 {
55 QString candidate = 62 QString candidate =
110 QLocale cLocale = QLocale::c(); 117 QLocale cLocale = QLocale::c();
111 QMap<QString, QString> headers; 118 QMap<QString, QString> headers;
112 QString headRequest = 119 QString headRequest =
113 QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource); 120 QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource);
114 121
115 ret = mSSLConnection.write(headRequest.toUtf8()); 122 ret = mSSLConnection->write(headRequest.toUtf8());
116 if (ret != 0) { 123 if (ret != 0) {
117 emit error (tr("Connection lost"), SSLConnection::ConnectionLost); 124 emit error (tr("Connection lost"), SSLConnection::ConnectionLost);
118 return QDateTime(); 125 return QDateTime();
119 } 126 }
120 127
121 response = mSSLConnection.read(1024); 128 response = mSSLConnection->read(1024);
122 129
123 qDebug() << "Response from server was: " << response; 130 qDebug() << "Response from server was: " << response;
124 131
125 if (response.isNull()) { 132 if (response.isNull()) {
126 qDebug() << "No response"; 133 qDebug() << "No response";
150 QString getRequest = 157 QString getRequest =
151 QString::fromLatin1("GET %1 HTTP/1.0\r\n\r\n").arg(resource); 158 QString::fromLatin1("GET %1 HTTP/1.0\r\n\r\n").arg(resource);
152 159
153 QSaveFile outputFile(fileName); 160 QSaveFile outputFile(fileName);
154 161
155 ret = mSSLConnection.write(getRequest.toUtf8()); 162 ret = mSSLConnection->write(getRequest.toUtf8());
156 163
157 // Open / Create the file to write to. 164 // Open / Create the file to write to.
158 if (!outputFile.open(QIODevice::WriteOnly)) { 165 if (!outputFile.open(QIODevice::WriteOnly)) {
159 qDebug() << "Failed to open file"; 166 qDebug() << "Failed to open file";
160 return false; 167 return false;
168 bool inBody = false; 175 bool inBody = false;
169 QMap <QString, QString> headers; 176 QMap <QString, QString> headers;
170 do { 177 do {
171 /* Read the response in 8KiB chunks */ 178 /* Read the response in 8KiB chunks */
172 int responseSize = 0; 179 int responseSize = 0;
173 QByteArray response = mSSLConnection.read(8192); 180 QByteArray response = mSSLConnection->read(8192);
174 if (response.isNull()) { 181 if (response.isNull()) {
175 qDebug() << "Error reading response"; 182 qDebug() << "Error reading response";
176 emit error(tr("Connection lost"), SSLConnection::ConnectionLost); 183 emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
177 return false; 184 return false;
178 } 185 }
196 void Downloader::run() { 203 void Downloader::run() {
197 int ret; 204 int ret;
198 QDateTime remoteModList; 205 QDateTime remoteModList;
199 QDateTime remoteModSW; 206 QDateTime remoteModSW;
200 207
201 if (!mSSLConnection.initialized()) { 208 if (!mSSLConnection->initialized()) {
202 emit error(tr("Failed to initialize SSL Module."), SSLConnection::ErrUnknown); 209 emit error(tr("Failed to initialize SSL Module."), SSLConnection::ErrUnknown);
203 return; 210 return;
204 } 211 }
205 212
206 ret = mSSLConnection.connect(); 213 ret = mSSLConnection->connect();
207 214
208 if (ret != 0) { 215 if (ret != 0) {
209 emit error(tr("Failed to connect."), 216 emit error(tr("Failed to connect."),
210 mSSLConnection.getLastError()); 217 mSSLConnection->getLastError());
211 return; 218 return;
212 } 219 }
213 220
214 emit progress(tr("Connected"), 1, -1); 221 emit progress(tr("Connected"), 1, -1);
215 222

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