comparison ui/downloader.cpp @ 11:7e2f14c7aba2

Split up downloader component and further implement it
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 14 Feb 2014 11:20:15 +0000
parents fe39d93f1261
children 9121eea6d93f
comparison
equal deleted inserted replaced
10:fe39d93f1261 11:7e2f14c7aba2
1 #include "downloader.h" 1 #include "downloader.h"
2
3 #ifndef MYVERSION
4 #define MYVERSION "1"
5 #endif
6 2
7 #ifndef DOWNLOAD_SERVER 3 #ifndef DOWNLOAD_SERVER
8 #define DOWNLOAD_SERVER "https://www.intevation.de" 4 #define DOWNLOAD_SERVER "https://www.intevation.de"
9 #endif 5 #endif
10 6
11 #ifdef Q_OS_WIN 7 #ifdef Q_OS_WIN
12 #include <windows.h>
13 #include <winhttp.h>
14 #endif 8 #endif
15 9
16 #include <QFile> 10 #include <QFile>
17 #include <QDebug> 11 #include <QDebug>
18 12
19 Downloader::Downloader(QObject* parent, const QString& url) 13 Downloader::Downloader(QObject* parent, const QString& url)
20 { 14 {
21 Downloader (parent, url, QFile(":Certificates/https").readAll()); 15 QFile certResource(":certificates/https");
16 certResource.open(QFile::ReadOnly);
17 Downloader (parent, url, certResource.readAll());
22 } 18 }
23 19
24 Downloader::Downloader(QObject* parent, const QString& url, 20 Downloader::Downloader(QObject* parent, const QString& url,
25 const QByteArray& certificate) : 21 const QByteArray& certificate) :
26 mUrl(url), 22 mUrl(url),
27 mCert(certificate), 23 mCert(certificate),
28 QThread(parent) 24 QThread(parent)
29 { 25 {
30 } 26 }
31 27
32 void Downloader::run() {
33 #ifdef Q_OS_WIN
34 // We use WinAPI here instead of Qt because we want to avoid
35 // QtNetworks SSL stack which is based on OpenSSL and so
36 // we might be incompatible with GPL code. Also using the
37 // native API means that the security of the SSL implementation
38 // is tied to the security of the system.
39 BOOL bResults = FALSE;
40 HINTERNET hSession = NULL,
41 hConnect = NULL,
42 hRequest = NULL;
43 SYSTEMTIME lastModified;
44 DWORD sizeOfSystemtime = sizeof (SYSTEMTIME);
45
46 // Get a syncronous session handle
47 hSession = WinHttpOpen(L"M13 "MYVERSION,
48 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
49 WINHTTP_NO_PROXY_NAME,
50 WINHTTP_NO_PROXY_BYPASS, 0);
51
52 if (hSession) {
53 // Initialize connection. No request is done here.
54 hConnect = WinHttpConnect(hSession, L""DOWNLOAD_SERVER,
55 INTERNET_DEFAULT_HTTPS_PORT, 0);
56
57 }
58
59 if (hConnect) {
60 // Make a head request
61 hRequest = WinHttpOpenRequest(hConnect, L"HEAD",
62 L"/index.html",
63 NULL, WINHTTP_NO_REFERER,
64 WINHTTP_DEFAULT_ACCEPT_TYPES,
65 0);
66 }
67
68 if (hRequest) {
69 bResults = WinHttpSendRequest(hRequest,
70 WINHTTP_NO_ADDITIONAL_HEADERS,
71 0, WINHTTP_NO_REQUEST_DATA, 0,
72 0, 0);
73 }
74
75 if (bResults) {
76 bResults = WinHttpReceiveResponse(hRequest, NULL);
77 }
78
79
80
81 if (bResults) {
82 bResults = WinHttpQueryHeaders(hRequest,
83 WINHTTP_QUERY_LAST_MODIFIED |
84 WINHTTP_QUERY_FLAG_SYSTEMTIME,
85 NULL,
86 &lastModified,
87 &sizeOfSystemtime,
88 WINHTTP_NO_HEADER_INDEX );
89 }
90
91 qDebug() << "Last modified year: " << lastModified.wYear;
92
93
94 if (!bResults) {
95 // Report any errors.
96 qDebug() << "Error" << GetLastError();
97 emit error(tr("Unknown Problem when connecting"), Unknown);
98 }
99
100 // Cleanup
101 if (hRequest) {
102 WinHttpCloseHandle(hRequest);
103 }
104
105 if (hConnect) {
106 WinHttpCloseHandle(hConnect);
107
108 }
109
110 if (hSession) {
111 WinHttpCloseHandle(hSession);
112 }
113 #endif
114
115 for (int i=0; i< 10; i++) {
116 qDebug("Going to sleep\n");
117 sleep(10);
118 }
119 }

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