annotate ui/downloader.cpp @ 33:25c08d63d2b7

Add CMakeLists in UI
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 14 Mar 2014 08:57:51 +0100
parents d8e93fa1fc93
children 56ba6376426e
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>
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
11 #include <QUuid>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
12 #include <QApplication>
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
13 #include <QTextStream>
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
14 #include <QLocale>
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
15 #include <QSaveFile>
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
16
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
17 #include <polarssl/net.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
18 #include <polarssl/ssl.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
19 #include <polarssl/entropy.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
20 #include <polarssl/ctr_drbg.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
21 #include <polarssl/error.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
22 #include <polarssl/certs.h>
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
23
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
24 #define MAX_SW_SIZE 10485760
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
25 #define MAX_LIST_SIZE 1048576
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
26 #define MAX_IO_TRIES 10
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
27
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
28 #define LIST_RESOURCE "/incoming/aheinecke/test"
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
29 #define SW_RESOURCE "/incoming/aheinecke/test"
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
30
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
31
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
32 #ifdef CONNECTION_DEBUG
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
33 static void my_debug(void *ctx, int level, const char *str)
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
34 {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
35 fprintf((FILE *) ctx, "%s", str);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
36 fflush((FILE *) ctx);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
37 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
38 #endif
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 QString getErrorMsg(int ret)
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 char errbuf[255];
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
43 polarssl_strerror(ret, errbuf, 255);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
44 errbuf[254] = '\0'; /* Just to be sure */
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
45 return QString::fromLatin1(errbuf);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
46 }
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
47
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
48 Downloader::Downloader(QObject* parent, const QString& url,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
49 const QByteArray& certificate,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
50 const QDateTime& newestSW,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
51 const QDateTime& newestList):
12
9121eea6d93f Fix constructor usage. Too much Java.
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
52 QThread(parent),
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
53 mUrl(url),
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
54 mPinnedCert(certificate),
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
55 mLastModSW(newestSW),
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
56 mLastModList(newestList),
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
57 mErrorState(NoError),
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
58 mInitialized(false),
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
59 mServerFD(-1)
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60 {
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
61 int ret = -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
62
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
63 memset(&mSSL, 0, sizeof(ssl_context));
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
64
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
65 if (certificate.isEmpty()) {
18
f4f957c58e0a Move listuitol and add cert pinning with a test certificate
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
66 QFile certResource(":certs/kolab.org");
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
67 certResource.open(QFile::ReadOnly);
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
68 mPinnedCert = certResource.readAll();
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
69 certResource.close();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
70 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
71
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
72 ret = init();
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
73 if (ret == 0) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
74 mInitialized = true;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
75 } else {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
76 qDebug() << "Initialization error: " + getErrorMsg(ret);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
77 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
78 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
79
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
80 int Downloader::init()
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
81 {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
82 int ret = -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
83 QUuid uuid = QUuid::createUuid();
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
84 QString personalString = QApplication::applicationName() + uuid.toString();
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
85 QByteArray personalBa = personalString.toLocal8Bit();
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
86
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
87 x509_crt_init(&mX509PinnedCert);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
88 entropy_init(&mEntropy);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
89
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
90 ret = ssl_init(&mSSL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
91 if (ret != 0) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
92 /* The only documented error is malloc failed */
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
93 mErrorState = ErrUnknown;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
94 return ret;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
95 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
96
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
97 /*
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
98 * Initialize random generator.
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
99 * Personalisation string, does not need to be random but
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
100 * should be unique according to documentation.
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
101 *
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
102 * the ctr_drbg structure does not need to be freed explicitly.
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
103 */
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
104 ret = ctr_drbg_init(&mCtr_drbg, entropy_func, &mEntropy,
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
105 (const unsigned char*) personalBa.constData(),
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
106 personalBa.size());
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
107 if (ret != 0) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
108 ssl_free(&mSSL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
109 mErrorState = ErrUnknown;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
110 return ret;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
111 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
112
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
113 ret = x509_crt_parse(&mX509PinnedCert,
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
114 (const unsigned char*) mPinnedCert.constData(),
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
115 mPinnedCert.size());
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
116 if (ret != 0){
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
117 ssl_free(&mSSL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
118 mErrorState = InvalidPinnedCertificate;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
119 return ret;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
120 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
121
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
122 ssl_set_endpoint(&mSSL, SSL_IS_CLIENT);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
123 ssl_set_authmode(&mSSL, SSL_VERIFY_OPTIONAL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
124 ssl_set_ca_chain(&mSSL, &mX509PinnedCert, NULL, NULL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
125 ssl_set_renegotiation(&mSSL, SSL_RENEGOTIATION_DISABLED);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
126 ssl_set_rng(&mSSL, ctr_drbg_random, &mCtr_drbg);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
127 #ifdef RELEASE_BUILD
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
128 ssl_set_min_version(&mSSL, SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_3);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
129 #endif
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
130
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
131 #ifdef CONNECTION_DEBUG
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
132 ssl_set_dbg(&mSSL, my_debug, stdout);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
133 #endif
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
134
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
135 return 0;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
136 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
137
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
138 Downloader::~Downloader() {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
139 x509_crt_free(&mX509PinnedCert);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
140 entropy_free(&mEntropy);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
141 if (mInitialized) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
142 ssl_free(&mSSL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
143 }
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
144 }
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
145
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
146 QString Downloader::getDataDirectory()
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
147 {
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
148 QString candidate =
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
149 QStandardPaths::writableLocation(QStandardPaths::DataLocation);
12
9121eea6d93f Fix constructor usage. Too much Java.
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
150
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
151 if (candidate.isEmpty()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
152 qDebug() << "Could not find writeable locaction for me";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
153 return QString();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
154 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
155
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
156 QDir cDir(candidate);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
157
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
158 if (!cDir.exists()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
159 if (!cDir.mkpath(candidate)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
160 qDebug() << "Could not create path to: " << candidate;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
161 return QString();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
162 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
163 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 12
diff changeset
164 return cDir.absolutePath();
10
fe39d93f1261 Start on Downloader component
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
165 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
166
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
167 int Downloader::establishSSLConnection() {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
168 int ret = -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
169 const x509_crt *peerCert;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
170
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
171 if (mServerFD == -1 || !mInitialized) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
172 mErrorState = ErrUnknown;
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
173 return -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
174 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
175
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
176 ssl_set_bio(&mSSL, net_recv, &mServerFD,
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
177 net_send, &mServerFD);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
178
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
179 while ((ret = ssl_handshake(&mSSL)) != 0) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
180 if (ret != POLARSSL_ERR_NET_WANT_READ &&
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
181 ret != POLARSSL_ERR_NET_WANT_WRITE) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
182 qDebug() << "SSL Handshake failed: "
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
183 << getErrorMsg(ret);
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
184 mErrorState = SSLHandshakeFailed;
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
185 return ret;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
186 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
187 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
188
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
189 /* we might want to set the verify function
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
190 * with ssl_set_verify before to archive the
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
191 * certificate pinning. */
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
192
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
193 ret = ssl_get_verify_result(&mSSL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
194
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
195 if (ret != 0 ) {
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
196 if((ret & BADCERT_EXPIRED) != 0)
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
197 qDebug() << "server certificate has expired";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
198 if((ret & BADCERT_REVOKED) != 0)
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
199 qDebug() << "server certificate has been revoked";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
200 if((ret & BADCERT_CN_MISMATCH) != 0)
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
201 qDebug() << "CN mismatch";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
202 if((ret & BADCERT_NOT_TRUSTED) != 0)
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
203 qDebug() << "self-signed or not signed by a trusted CA";
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
204 ret = -1;
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
205 #ifdef RELEASE_BUILD
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
206 mErrorState = InvalidCertificate;
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
207 return -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
208 #endif
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 peerCert = ssl_get_peer_cert(&mSSL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
212
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
213 if (!peerCert) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
214 mErrorState = InvalidCertificate;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
215 qDebug() << "Failed to get peer cert";
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
216 return -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
217 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
218
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
219 if (peerCert->raw.len == 0 ||
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
220 peerCert->raw.len != mX509PinnedCert.raw.len) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
221 mErrorState = InvalidCertificate;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
222 qDebug() << "Certificate length mismatch";
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
223 return -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
224 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
225
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
226 /* You can never be sure what those c++ operators do..
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
227 if (mPinnedCert != QByteArray::fromRawData(
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
228 (const char*) peerCert->raw.p,
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
229 peerCert->raw.len)) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
230 qDebug() << "Certificate content mismatch";
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
231 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
232 */
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
233
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
234 for (unsigned int i = 0; i < peerCert->raw.len; i++) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
235 if (peerCert->raw.p[i] != mX509PinnedCert.raw.p[i]) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
236 qDebug() << "Certificate content mismatch";
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
237 mErrorState = InvalidCertificate;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
238 return -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
239 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
240 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
241 return 0;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
242 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
243
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
244 /* Helper around polarssl bare bone api */
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
245 int polarSSLWrite (ssl_context *ssl, const QByteArray& request)
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 unsigned int tries = 0;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
248 int ret = -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
249
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
250 const unsigned char *buf = (const unsigned char *) request.constData();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
251 size_t len = (size_t) request.size();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
252
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
253 qDebug() << "Seinding request: " << request;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
254 /* According to doc for ssl_write:
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
255 *
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
256 * When this function returns POLARSSL_ERR_NET_WANT_WRITE,
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
257 * it must be called later with the same arguments,
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
258 * until it returns a positive value.
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
259 */
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
260 do {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
261 ret = ssl_write(ssl, buf, len);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
262 if (ret >= 0) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
263 if ((unsigned int) ret == len) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
264 return 0;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
265 } else {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
266 qDebug() << "Write failed to write everything";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
267 return -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
268 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
269 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
270
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
271 if (ret != POLARSSL_ERR_NET_WANT_WRITE) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
272 return ret;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
273 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
274 tries++;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
275 net_usleep(100000); /* sleep 100ms to give the socket a chance
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
276 to clean up. */
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
277 } while (tries < MAX_IO_TRIES);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
278
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
279 return ret;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
280 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
281
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
282 /* Helper around polarssl bare bone api read at most len bytes
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
283 * and return them as a byte array returns a NULL byte array on error*/
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
284 QByteArray polarSSLRead (ssl_context *ssl, size_t len)
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
285 {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
286 unsigned char buf[len];
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
287 QByteArray retval("");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
288 int ret = -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
289
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
290 do {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
291 memset (buf, 0, sizeof(buf));
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
292 ret = ssl_read(ssl, buf, len);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
293 if (ret == 0 ||
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
294 ret == POLARSSL_ERR_SSL_CONN_EOF) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
295 /* EOF */
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
296 return retval;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
297 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
298 if (ret <= 0) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
299 qDebug() << "Read failed: " << getErrorMsg(ret);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
300 return QByteArray();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
301 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
302 if (len < (len - (unsigned int) ret)) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
303 /* Should never happen if ssl_read behaves */
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
304 qDebug() << "integer overflow in polarSSLRead";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
305 return QByteArray();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
306 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
307 len -= (unsigned int) ret;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
308 retval.append((const char *)buf, len);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
309 } while (len > 0);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
310
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
311 return retval;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
312 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
313
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
314
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
315 QDateTime Downloader::getLastModifiedHeader(const QString &resource) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
316 int ret = -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
317 QByteArray response;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
318 QTextStream responseStream(&response);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
319 QLocale cLocale = QLocale::c();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
320 QString headRequest =
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
321 QString::fromLatin1("HEAD %1 HTTP/1.1\r\n"
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
322 "Connection: Keep-Alive\r\n"
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
323 "\r\n\r\n").arg(resource);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
324
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
325 ret = polarSSLWrite (&mSSL, headRequest.toUtf8());
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
326 if (ret != 0) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
327 mErrorState = ConnectionLost;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
328 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
329 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
330
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
331 response = polarSSLRead(&mSSL, 1024);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
332
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
333 if (response.isNull()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
334 qDebug() << "No response";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
335 mErrorState = ConnectionLost;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
336 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
337 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
338
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
339 while (1) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
340 QString line = responseStream.readLine();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
341 if (line.isNull()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
342 break;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
343 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
344 if (line.startsWith("Last-Modified:")) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
345 QDateTime candidate = cLocale.toDateTime(line, "'Last-Modified: 'ddd, dd MMM yyyy HH:mm:ss' GMT'");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
346 qDebug() << "Parsed line : " << line << " to " << candidate;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
347 if (candidate.isValid()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
348 return candidate;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
349 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
350 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
351 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
352
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
353 mErrorState = InvalidResponse;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
354 return QDateTime();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
355 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
356
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
357 bool Downloader::downloadFile(const QString &resource,
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
358 const QString &fileName,
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
359 size_t maxSize)
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
360 {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
361 int ret = -1;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
362 size_t bytesRead = 0;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
363 QString getRequest =
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
364 QString::fromLatin1("GET %1 HTTP/1.1\r\n\r\n").arg(resource);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
365
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
366 QSaveFile outputFile(fileName);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
367
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
368 ret = polarSSLWrite (&mSSL, getRequest.toUtf8());
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
369
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
370 // Open / Create the file to write to.
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
371 if (!outputFile.open(QIODevice::WriteOnly)) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
372 qDebug() << "Failed to open file";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
373 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
374 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
375
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
376 if (ret != 0) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
377 mErrorState = ConnectionLost;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
378 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
379 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
380
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
381 do {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
382 QByteArray response = polarSSLRead(&mSSL, 8192);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
383 if (response.isNull()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
384 qDebug() << "Error reading response";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
385 mErrorState = ConnectionLost;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
386 return false;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
387 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
388 if (response.isEmpty()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
389 /* We have read everything there is to read */
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
390 break;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
391 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
392
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
393 outputFile.write(response);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
394 bytesRead += response.size();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
395 } while (bytesRead < maxSize);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
396
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
397 return outputFile.commit();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
398 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
399
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
400 void Downloader::run() {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
401 int ret;
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
402 QDateTime remoteModList;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
403 QDateTime remoteModSW;
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
404
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
405 if (!mInitialized) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
406 emit error(tr("Failed to initialize SSL Module."), ErrUnknown);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
407 return;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
408 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
409
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
410 ret = net_connect(&mServerFD, mUrl.host().toLatin1().constData(),
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
411 mUrl.port(443));
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
412
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
413 if (ret != 0) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
414 mErrorState = NoConnection;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
415 emit error(tr("Failed to connect to %1.").arg(mUrl.host()),
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
416 mErrorState);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
417 return;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
418 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
419
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
420 emit progress(tr("Connected"), 1, -1);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
421
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
422 ret = establishSSLConnection();
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
423 if (ret != 0) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
424 qDebug() << "SSL conncetion failed: " << getErrorMsg(ret);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
425 emit error(tr("Failed to connect to %1.").arg(mUrl.host()),
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
426 mErrorState);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
427 return;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
428 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
429
32
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
430 remoteModSW = getLastModifiedHeader(QString::fromLatin1("/incoming/aheinecke/test"));
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
431 remoteModList = getLastModifiedHeader(QString::fromLatin1("/incoming/aheinecke/test"));
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
432
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
433 if (!remoteModSW.isValid() || !remoteModList.isValid()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
434 qDebug() << "Could not read headers";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
435 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
436 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
437
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
438 if (!mLastModSW.isValid() || remoteModSW > mLastModSW) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
439 QString dataDirectory = getDataDirectory();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
440
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
441 if (dataDirectory.isEmpty()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
442 qDebug() << "Failed to get data directory";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
443 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
444 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
445
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
446 QString fileName = dataDirectory.append("/SW-")
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
447 .append(remoteModSW.toString("yyyymmddHHmmss"))
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
448 .append(".exe");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
449
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
450 qDebug() << "fileName: " << fileName;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
451
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
452 if (!downloadFile(QString::fromLatin1(SW_RESOURCE),
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
453 fileName, MAX_SW_SIZE)) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
454 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
455 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
456
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
457 emit newSoftwareAvailable(fileName, remoteModSW);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
458 } else if (!mLastModList.isValid() || remoteModList > mLastModList) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
459 QString dataDirectory = getDataDirectory();
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
460
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
461 if (dataDirectory.isEmpty()) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
462 qDebug() << "Failed to get data directory";
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
463 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
464 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
465
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
466 QString fileName = dataDirectory.append("/list-")
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
467 .append(remoteModSW.toString("yyyymmddHHmmss"))
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
468 .append(".txt");
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
469
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
470 qDebug() << "fileName: " << fileName;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
471
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
472 if (!downloadFile(QString::fromLatin1(LIST_RESOURCE),
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
473 fileName, MAX_LIST_SIZE)) {
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
474 return;
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
475 }
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
476
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
477 emit newListAvailable(fileName, remoteModList);
d8e93fa1fc93 Downloader logic
Andre Heinecke <andre.heinecke@intevation.de>
parents: 27
diff changeset
478 }
27
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
479
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
480 emit progress(tr("Closing"), 1, -1);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
481 ssl_close_notify (&mSSL);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
482
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
483 if (mServerFD != -1) {
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
484 net_close(mServerFD);
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
485 mServerFD = -1;
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
486 }
62cd56cea09b Start on polarssl Downloader.
Andre Heinecke <andre.heinecke@intevation.de>
parents: 18
diff changeset
487 }

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