annotate ui/downloader.cpp @ 66:4f79cf993737

Tiny improvement: free resources first that you have aquired last. Added TODO for better error reporting.
author Bernhard Reiter <bernhard@intevation.de>
date Tue, 18 Mar 2014 16:37:40 +0100
parents 09cd242d8443
children 7e304573ebd1
rev   line source
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
1 #include "downloader.h"
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
2
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
3 #ifndef DOWNLOAD_SERVER
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
4 #define DOWNLOAD_SERVER "https://www.intevation.de"
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
5 #endif
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
6
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
7 #include <QFile>
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
8 #include <QDir>
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9 #include <QDebug>
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
10 #include <QStandardPaths>
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
11 #include <QTextStream>
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
12 #include <QLocale>
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
13 #include <QSaveFile>
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
14
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
15 #include <polarssl/net.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
16 #include <polarssl/ssl.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
17 #include <polarssl/entropy.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
18 #include <polarssl/ctr_drbg.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
19 #include <polarssl/error.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
20 #include <polarssl/certs.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
21
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
22 #define MAX_SW_SIZE 10485760
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
23 #define MAX_LIST_SIZE 1048576
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
24
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
25
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
26 Downloader::Downloader(QObject* parent, const QString& url,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
27 const QByteArray& certificate,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
28 const QDateTime& newestSW,
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
29 const QDateTime& newestList,
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
30 const QString& resourceSW,
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
31 const QString& resourceList):
12
9121eea6d93f Fix constructor usage. Too much Java.
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
32 QThread(parent),
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
33 mLastModSW(newestSW),
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
34 mLastModList(newestList),
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
35 mResourceSW(resourceSW),
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
36 mResourceList(resourceList),
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
37 mSSLConnection(url, certificate)
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 {
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
39 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
40
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
41
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
42 Downloader::~Downloader() {
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
45 QString Downloader::getDataDirectory()
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
46 {
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
47 QString candidate =
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
48 QStandardPaths::writableLocation(QStandardPaths::DataLocation);
12
9121eea6d93f Fix constructor usage. Too much Java.
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
49
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
50 if (candidate.isEmpty()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
51 qDebug() << "Could not find writeable locaction for me";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
52 return QString();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
53 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
54
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
55 QDir cDir(candidate);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
56
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
57 if (!cDir.exists()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
58 if (!cDir.mkpath(candidate)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
59 qDebug() << "Could not create path to: " << candidate;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
60 return QString();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
61 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
62 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
63 return cDir.absolutePath();
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
64 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
65
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
66 QMap<QString, QString> Downloader::parseHeaders(QByteArray *data)
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
67 {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
68 int bodyStart = data->indexOf("\r\n\r\n");
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
69 QMap<QString, QString> retval;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
70 QByteArray headers;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
71 QTextStream responseStream(&headers);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
72 if (bodyStart == -1) {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
73 qDebug() << "Could not find header end.";
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
74 emit error(tr("Invalid response"),
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
75 SSLConnection::InvalidResponse);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
76 return retval;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
77 }
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
78
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
79 /* Take the headers with one additional line break */
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
80 headers = data->left(bodyStart + 2);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
81 /* Chop off the head */
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
82
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
83 while (1) {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
84 QString line = responseStream.readLine();
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
85 int sepPos = -1;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
86 if (line.isNull()) {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
87 break;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
88 }
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
89 sepPos = line.indexOf(": ");
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
90 if (sepPos == -1) {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
91 continue;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
92 }
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
93 QString key = line.left(sepPos);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
94 QString value = line.right(line.size() - sepPos - 2);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
95
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
96 retval.insert(key, value);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
97 }
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
98
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
99 *data = data->right(data->size() - bodyStart - 4);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
100 return retval;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
101 }
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
102
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
103 QDateTime Downloader::getLastModifiedHeader(const QString &resource) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
104 int ret = -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
105 QByteArray response;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
106 QTextStream responseStream(&response);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
107 QLocale cLocale = QLocale::c();
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
108 QMap<QString, QString> headers;
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
109 QString headRequest =
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
110 QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
111
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
112 ret = mSSLConnection.write(headRequest.toUtf8());
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
113 if (ret != 0) {
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
114 emit error (tr("Connection lost"), SSLConnection::ConnectionLost);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
115 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
116 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
117
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
118 response = mSSLConnection.read(1024);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
119
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
120 if (response.isNull()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
121 qDebug() << "No response";
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
122 emit error (tr("Connection lost"), SSLConnection::ConnectionLost);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
123 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
124 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
125
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
126 headers = parseHeaders(&response);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
127 const QString lastModified = headers.value("Last-Modified");
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
128 qDebug() << "Headers: " << headers;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
129 if (!lastModified.isEmpty()) {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
130 QDateTime candidate = cLocale.toDateTime(lastModified,
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
131 "ddd, dd MMM yyyy HH:mm:ss' GMT'");
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
132 if (candidate.isValid()) {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
133 return candidate;
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
134 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
135 }
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
136 emit error (tr("Invalid response from the server"), SSLConnection::InvalidResponse);
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
137 qDebug() << "Response from server was: " << response;
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
138 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
139 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
140
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
141 bool Downloader::downloadFile(const QString &resource,
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
142 const QString &fileName,
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
143 size_t maxSize)
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
144 {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
145 int ret = -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
146 size_t bytesRead = 0;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
147 QString getRequest =
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
148 QString::fromLatin1("GET %1 HTTP/1.0\r\n\r\n").arg(resource);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
149
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
150 QSaveFile outputFile(fileName);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
151
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
152 ret = mSSLConnection.write(getRequest.toUtf8());
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
153
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
154 // Open / Create the file to write to.
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
155 if (!outputFile.open(QIODevice::WriteOnly)) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
156 qDebug() << "Failed to open file";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
157 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
158 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
159
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
160 if (ret != 0) {
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
161 emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
162 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
163 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
164
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
165 bool inBody = false;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
166 QMap <QString, QString> headers;
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
167 do {
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
168 /* Read the response in 8KiB chunks */
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
169 int responseSize = 0;
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
170 QByteArray response = mSSLConnection.read(8192);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
171 if (response.isNull()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
172 qDebug() << "Error reading response";
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
173 emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
174 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
175 }
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
176 responseSize = response.size();
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
177 if (!inBody) {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
178 headers = parseHeaders(&response);
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
179 inBody = true;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
180 }
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
181 outputFile.write(response);
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
182 bytesRead += responseSize;
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
183 if (responseSize < 8192) {
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
184 /* Nothing more to read */
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
185 break;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
186 }
54
09cd242d8443 Split out Header parsing into it's own function
Andre Heinecke <aheinecke@intevation.de>
parents: 46
diff changeset
187 /* TODO Emit progress */
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
188 } while (bytesRead < maxSize);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
189
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
190 return outputFile.commit();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
191 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
192
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
193 void Downloader::run() {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
194 int ret;
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
195 QDateTime remoteModList;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
196 QDateTime remoteModSW;
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
197
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
198 if (!mSSLConnection.initialized()) {
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
199 emit error(tr("Failed to initialize SSL Module."), SSLConnection::ErrUnknown);
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
200 return;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
201 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
202
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
203 ret = mSSLConnection.connect();
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
204
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
205 if (ret != 0) {
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
206 emit error(tr("Failed to connect."),
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
207 mSSLConnection.getLastError());
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
208 return;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
209 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
210
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
211 emit progress(tr("Connected"), 1, -1);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
212
45
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
213 remoteModSW = getLastModifiedHeader(mResourceSW);
c6125d73faf4 Move SSLConnection into it's own class
Andre Heinecke <aheinecke@intevation.de>
parents: 35
diff changeset
214 remoteModList = getLastModifiedHeader(mResourceList);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
215
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
216 if (!remoteModSW.isValid() || !remoteModList.isValid()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
217 qDebug() << "Could not read headers";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
218 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
219 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
220
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
221 if (!mLastModSW.isValid() || remoteModSW > mLastModSW) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
222 QString dataDirectory = getDataDirectory();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
223
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
224 if (dataDirectory.isEmpty()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
225 qDebug() << "Failed to get data directory";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
226 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
227 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
228
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
229 QString fileName = dataDirectory.append("/SW-")
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
230 .append(remoteModSW.toString("yyyymmddHHmmss"))
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
231 .append(".exe");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
232
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
233 qDebug() << "fileName: " << fileName;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
234
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
235 if (!downloadFile(mResourceSW, fileName, MAX_SW_SIZE)) {
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
236 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
237 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
238
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
239 emit newSoftwareAvailable(fileName, remoteModSW);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
240 } else if (!mLastModList.isValid() || remoteModList > mLastModList) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
241 QString dataDirectory = getDataDirectory();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
242
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
243 if (dataDirectory.isEmpty()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
244 qDebug() << "Failed to get data directory";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
245 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
246 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
247
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
248 QString fileName = dataDirectory.append("/list-")
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
249 .append(remoteModSW.toString("yyyymmddHHmmss"))
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
250 .append(".txt");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
251
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
252 qDebug() << "fileName: " << fileName;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
253
46
d28e2624c1d5 Reset connection before the next request.
Andre Heinecke <aheinecke@intevation.de>
parents: 45
diff changeset
254 if (!downloadFile(mResourceList, fileName, MAX_LIST_SIZE)) {
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
255 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
256 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
257
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
258 emit newListAvailable(fileName, remoteModList);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
259 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
260
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
261 emit progress(tr("Closing"), 1, -1);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
262 }

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