annotate ui/downloader.cpp @ 44:b3e8e047bc2c

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

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