annotate ui/downloader_win.cpp @ 17:c12825a651ed

Read out content-length and use this to skip existing files
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 19 Feb 2014 14:22:26 +0000
parents 95e1b6edf2fc
children f4f957c58e0a
rev   line source
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
1 /**
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
2 * @file downloader_win.cpp
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
3 * @brief Downloader implementation for Windows
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
4 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
5 * We use Windows API here instead of Qt because we want to avoid
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
6 * QtNetworks SSL stack which is based on OpenSSL and so
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
7 * we might be incompatible with GPL code. Also using the
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
8 * native API means that the security of the SSL implementation
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9 * is tied to the security of the system.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
10 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
11 */
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
12 #include "downloader.h"
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
13 #ifdef Q_OS_WIN
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
14 #ifndef MYVERSION
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
15 #define MYVERSION "1"
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
16 #endif
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
17
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
18 #include <windows.h>
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
19 #include <winhttp.h>
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
20
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
21 #include <QDebug>
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
22 #include <QDateTime>
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
23 #include <QSaveFile>
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
24 #include <QFileInfo>
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
25
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
26 #define DEBUG if (1) qDebug() << __PRETTY_FUNCTION__
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
27
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
28 #define MAX_SW_SIZE 10485760
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
29 #define MAX_LIST_SIZE 1048576
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
30
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
31
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
32 #define LIST_RESOURCE "/incoming/aheinecke/test"
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
33 #define SW_RESOURCE "/incoming/aheinecke/test"
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
34
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
35
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
36 /** @brief Qt wrapper around FormatMessage
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
37 *
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
38 * @returns The error message of the error that occurred
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
39 */
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
40 const QString getLastErrorMsg() {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
41 LPWSTR bufPtr = NULL;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42 DWORD err = GetLastError();
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44 FORMAT_MESSAGE_FROM_SYSTEM |
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
45 FORMAT_MESSAGE_IGNORE_INSERTS,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
46 NULL, err, 0, (LPWSTR)&bufPtr, 0, NULL);
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
47 if (!bufPtr) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
48 HMODULE hWinhttp = GetModuleHandleW(L"winhttp");
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
49 if (hWinhttp) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
50 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
51 FORMAT_MESSAGE_FROM_HMODULE |
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
52 FORMAT_MESSAGE_IGNORE_INSERTS,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
53 hWinhttp, HRESULT_CODE(err), 0,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
54 (LPWSTR)&bufPtr, 0, NULL);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
55 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
56 }
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
57 const QString result =
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
58 (bufPtr) ? QString::fromUtf16((const ushort*)bufPtr).trimmed() :
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
59 QString("Unknown Error %1").arg(err);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60 LocalFree(bufPtr);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
61 return result;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
63
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
64
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
65 /** @brief open a session with appropriate proxy settings
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
66 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67 * @param[inout] *pHSession pointer to a HInternet structure
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
69 * On error call getLastError to get extended error information.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
70 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
71 * @returns True on success, false on error.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
72 */
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
73
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
74
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
75 bool openSession(HINTERNET *pHSession)
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
76 {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
77 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
78
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
79 DEBUG;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
80 if (!pHSession) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
81 SetLastError(ERROR_INVALID_PARAMETER);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
82 return false;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
83 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
84
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
85 memset(&proxyConfig, 0, sizeof (WINHTTP_CURRENT_USER_IE_PROXY_CONFIG));
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
86
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
87 if (WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig)) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
88 if (proxyConfig.fAutoDetect) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
89 // TODO Handle this
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
90 qDebug() << "Autodetect is set";
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
91 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
92
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
93 if (proxyConfig.lpszProxy || proxyConfig.lpszProxyBypass) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
94 DEBUG << "Using proxies.";
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
95 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
96
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
97 if (proxyConfig.lpszProxy) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
98 *pHSession = WinHttpOpen(L"M13 "MYVERSION,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
99 WINHTTP_ACCESS_TYPE_NAMED_PROXY,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
100 proxyConfig.lpszProxy,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
101 proxyConfig.lpszProxyBypass, 0);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
102 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
103 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
104
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
105 if (!*pHSession) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
106 DEBUG << "No IE Proxy falling back to default proxy";
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
107 *pHSession = WinHttpOpen(L"M13 "MYVERSION,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
108 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
109 WINHTTP_NO_PROXY_NAME,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
110 WINHTTP_NO_PROXY_BYPASS, 0);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
111 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
112 // Cleanup
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
113 if (proxyConfig.lpszAutoConfigUrl) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
114 GlobalFree(proxyConfig.lpszAutoConfigUrl);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
115 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
116
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
117 if (proxyConfig.lpszProxy) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
118 GlobalFree(proxyConfig.lpszProxy);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
119 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
120
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
121 if (proxyConfig.lpszProxyBypass) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
122 GlobalFree(proxyConfig.lpszProxyBypass);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
123 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
124 return *pHSession;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
125 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
126
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
127
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
128 /** @brief initialize a connection in the session
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
129 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
130 * @param[in] HSession the session to work in.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
131 * @param[inout] *pHConnect pointer to the connection.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
132 * @param[in] url pointer to the URL in wchar representation.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
133 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
134 * On error call getLastError to get extended error information.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
135 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
136 * @returns True on success, false on error.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
137 */
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
138 bool initializeConnection(HINTERNET hSession, HINTERNET *pHConnect,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
139 LPCWSTR url)
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
140 {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
141 DEBUG;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
142 if (!hSession || !pHConnect) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
143 SetLastError(ERROR_INVALID_PARAMETER);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
144 return false;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
145 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
146 // Initialize connection. No request is done here.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
147 *pHConnect = WinHttpConnect(hSession, url,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
148 INTERNET_DEFAULT_HTTPS_PORT, 0);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
149
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
150 return *pHConnect;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
151 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
152
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
153 /** @brief Create a request
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
154 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
155 * @param[in] HSession the session to work in.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
156 * @param[in] HConnect the connection to use.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
157 * @param[inout] *pHRequest pointer to the request structure to be filled.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
158 * @param[in] requestType the HTTP request to be made default is GET
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
159 * @param[in] resource pointer to the resource to request in wchar
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
160 * representation.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
161 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
162 * On error call getLastError to get extended error information.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
163 * This function still does not do any networking but only initializes
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
164 * it.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
165 *
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
166 * @returns True on success, false on error.
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
167 */
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
168
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
169 bool createRequest(HINTERNET hSession, HINTERNET hConnect,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
170 HINTERNET *pHRequest, LPCWSTR requestType, LPCWSTR resource)
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
171 {
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
172 DWORD dwSSLFlag;
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
173 DEBUG;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
174 if (!hSession || !hConnect || !pHRequest) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
175 SetLastError(ERROR_INVALID_PARAMETER);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
176 return false;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
177 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
178
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
179 *pHRequest = WinHttpOpenRequest(hConnect, requestType, resource,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
180 NULL, WINHTTP_NO_REFERER,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
181 WINHTTP_DEFAULT_ACCEPT_TYPES,
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
182 WINHTTP_FLAG_SECURE);
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
183
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
184 dwSSLFlag = SECURITY_FLAG_IGNORE_UNKNOWN_CA;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
185 dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
186 dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
187 dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
188
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
189 WinHttpSetOption(*pHRequest, WINHTTP_OPTION_SECURITY_FLAGS,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
190 &dwSSLFlag, sizeof(dwSSLFlag));
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
191
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
192 return *pHRequest;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
193 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
194
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
195 bool Downloader::verifyCertificate(HINTERNET hRequest)
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
196 {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
197 CERT_CONTEXT *certContext = NULL;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
198 DWORD certContextLen = sizeof(CERT_CONTEXT);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
199 bool retval = false;
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
200
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
201 if (!WinHttpQueryOption(hRequest,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
202 WINHTTP_OPTION_SERVER_CERT_CONTEXT,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
203 &certContext,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
204 &certContextLen)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
205 DEBUG << "Unable to get server certificate";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
206 return false;
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
207 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
208
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
209 QByteArray serverCert ((const char *) certContext->pbCertEncoded,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
210 certContext->cbCertEncoded);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
211
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
212 retval = (serverCert == mCert);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
213
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
214 if (!retval) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
215 DEBUG << "Certificate is not the same as the pinned one!"
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
216 << "Base64 cert: " << serverCert.toBase64();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
217 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
218
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
219 CertFreeCertificateContext(certContext);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
220 return retval;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
221 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
222
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
223 QDateTime Downloader::getLastModifiedHeader(HINTERNET hSession,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
224 HINTERNET hConnect, LPCWSTR resource)
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
225 {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
226 HINTERNET hRequest = NULL;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
227 SYSTEMTIME lMod;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
228 DWORD sizeOfSystemtime = sizeof (SYSTEMTIME);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
229 QDateTime retval;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
230 DWORD err = 0;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
231
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
232 memset(&lMod, 0, sizeof (SYSTEMTIME));
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
233
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
234 if (!hSession || !hConnect || !resource) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
235 SetLastError(ERROR_INVALID_PARAMETER);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
236 return retval;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
237 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
238
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
239 if (!createRequest(hSession, hConnect, &hRequest, L"HEAD",
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
240 resource)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
241 err = GetLastError();
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
242 goto cleanup;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
243 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
244
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
245 if (!WinHttpSendRequest(hRequest,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
246 WINHTTP_NO_ADDITIONAL_HEADERS,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
247 0, WINHTTP_NO_REQUEST_DATA, 0,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
248 0, 0)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
249 err = GetLastError();
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
250 goto cleanup;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
251 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
252
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
253
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
254 if (!WinHttpReceiveResponse(hRequest, NULL)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
255 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
256 goto cleanup;
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
257 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
258
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
259 if (!verifyCertificate(hRequest)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
260 DEBUG << "Certificate verification failed";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
261 // TODO error out
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
262 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
263
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
264 if (!(WinHttpQueryHeaders(hRequest,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
265 WINHTTP_QUERY_LAST_MODIFIED |
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
266 WINHTTP_QUERY_FLAG_SYSTEMTIME,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
267 NULL,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
268 &lMod,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
269 &sizeOfSystemtime,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
270 WINHTTP_NO_HEADER_INDEX))) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
271 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
272 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
273 }
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
274
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
275 retval = QDateTime(QDate(lMod.wYear, lMod.wMonth, lMod.wDay),
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
276 QTime(lMod.wHour, lMod.wMinute, lMod.wSecond,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
277 lMod.wMilliseconds),
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
278 Qt::UTC);
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
279 cleanup:
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
280 if (hRequest) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
281 WinHttpCloseHandle(hRequest);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
282 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
283
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
284 // Close handle might overwrite the last error.
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
285 SetLastError(err);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
286 return retval;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
287 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
288
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
289 bool Downloader::downloadFile(HINTERNET hSession, HINTERNET hConnect,
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
290 LPCWSTR resource, const QString &fileName, DWORD maxSize)
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
291 {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
292 HINTERNET hRequest = NULL;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
293 bool retval = false;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
294 DWORD bytesAvailable = 0,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
295 err = 0,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
296 bytesRead = 0,
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
297 totalDownloaded = 0,
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
298 contentLength = 0,
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
299 sizeOfDWORD = sizeof (DWORD);
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
300
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
301 QSaveFile outputFile(fileName);
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
302
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
303 if (!hSession || !hConnect || !resource) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
304 SetLastError(ERROR_INVALID_PARAMETER);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
305 return retval;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
306 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
307
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
308 if (!createRequest(hSession, hConnect, &hRequest, L"GET",
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
309 resource)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
310 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
311 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
312 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
313
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
314 if (!WinHttpSendRequest(hRequest,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
315 WINHTTP_NO_ADDITIONAL_HEADERS,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
316 0, WINHTTP_NO_REQUEST_DATA, 0,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
317 0, 0)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
318 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
319 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
320 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
321
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
322
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
323 if (!WinHttpReceiveResponse(hRequest, NULL)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
324 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
325 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
326 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
327
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
328 if (!verifyCertificate(hRequest)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
329 DEBUG << "Certificate verification failed";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
330 // TODO error out
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
331 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
332
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
333
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
334 if (!(WinHttpQueryHeaders(hRequest,
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
335 WINHTTP_QUERY_CONTENT_LENGTH |
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
336 WINHTTP_QUERY_FLAG_NUMBER,
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
337 NULL,
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
338 &contentLength,
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
339 &sizeOfDWORD,
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
340 WINHTTP_NO_HEADER_INDEX))) {
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
341 // Continue anyway as we later really check how
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
342 // much we download.
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
343 DEBUG << "No content-length";
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
344 }
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
345
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
346 if (contentLength > maxSize) {
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
347 err = ERROR_INVALID_DATA;
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
348 goto cleanup;
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
349 }
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
350
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
351 if (contentLength) {
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
352 QFileInfo finf(fileName);
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
353 if (finf.exists() && finf.isReadable() &&
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
354 finf.size() == contentLength) {
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
355 // We already have data of the same size
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
356 // No need to waste bandwidth.
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
357 DEBUG << "Skipping download because file exists";
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
358 retval = true;
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
359 goto cleanup;
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
360 }
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
361 }
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
362
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
363 // Open / Create the file to write to.
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
364 if (!outputFile.open(QIODevice::WriteOnly)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
365 DEBUG << "Failed to open file";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
366 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
367 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
368 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
369
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
370 DEBUG << "output file size: " << outputFile.size();
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
371 do
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
372 {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
373 char outBuf[8192]; // 8KB is the internal buffer size of winhttp
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
374 memset(outBuf, 0, sizeof(outBuf));
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
375 bytesRead = 0;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
376
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
377 if (!WinHttpQueryDataAvailable(hRequest, &bytesAvailable)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
378 DEBUG << "Querying for available data failed";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
379 retval = false;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
380 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
381 break;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
382 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
383
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
384 if (!bytesAvailable) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
385 // Might indicate that we are done.
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
386 break;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
387 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
388
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
389 if (bytesAvailable > maxSize) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
390 DEBUG << "File to large";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
391 retval = false;
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
392 err = ERROR_INVALID_DATA;
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
393 break;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
394 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
395
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
396 if (!WinHttpReadData(hRequest, (LPVOID)outBuf,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
397 sizeof(outBuf), &bytesRead)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
398 DEBUG << "Error reading data";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
399 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
400 break;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
401 } else {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
402 if (bytesRead) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
403 // Write data to file.
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
404 if (outputFile.write(outBuf, bytesRead) !=
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
405 bytesRead) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
406 err = GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
407 DEBUG << "Error writing to file.";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
408 retval = false;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
409 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
410 // Completed a read / write cycle. If not error follows
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
411 // the download was successful.
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
412 retval = true;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
413 } else {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
414 // Should not happen as we queried for available
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
415 // bytes before and the function did not return an
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
416 // error.
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
417 DEBUG << "Unable to read available data";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
418 retval = false;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
419 break;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
420 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
421 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
422 totalDownloaded += bytesRead;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
423
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
424 if (totalDownloaded > maxSize) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
425 DEBUG << "Downloaded too much data. Breaking.";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
426 retval = false;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
427 break;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
428 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
429 } while (bytesAvailable > 0);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
430
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
431 cleanup:
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
432
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
433 if (retval && outputFile.isOpen()) {
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
434 // Actually save the file to disk / move to homedir
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
435 retval = outputFile.commit();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
436 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
437
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
438 if (hRequest) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
439 WinHttpCloseHandle(hRequest);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
440 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
441
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
442 // Close handle might overwrite the last error.
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
443 SetLastError(err);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
444 return retval;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
445 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
446
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
447 void Downloader::run() {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
448 bool results = false;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
449 HINTERNET hSession = NULL,
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
450 hConnect = NULL;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
451 wchar_t wUrl[mUrl.size() + 1];
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
452 QDateTime lastModifiedSoftware;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
453 QDateTime lastModifiedList;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
454
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
455 int rc = 0;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
456
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
457 memset(wUrl, 0, sizeof (wchar_t) * (mUrl.size() + 1));
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
458
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
459 rc = mUrl.toWCharArray(wUrl);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
460
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
461 if (rc != mUrl.size()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
462 DEBUG << "Problem converting to wchar array";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
463 return;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
464 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
465
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
466 // Should not be necessary because we initialized the memory
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
467 wUrl[rc] = '\0';
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
468
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
469 // Initialize connection
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
470 if (!openSession(&hSession)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
471 DEBUG << "Failed to open session: " << getLastErrorMsg();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
472 return;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
473 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
474 if (!initializeConnection(hSession, &hConnect, wUrl)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
475 DEBUG << "Failed to initialize connection: " << getLastErrorMsg();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
476 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
477 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
478
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
479
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
480 lastModifiedSoftware = getLastModifiedHeader(hSession, hConnect,
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
481 L""SW_RESOURCE);
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
482
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
483 lastModifiedList = getLastModifiedHeader(hSession, hConnect,
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
484 L""LIST_RESOURCE);
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
485
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
486 if (!lastModifiedList.isValid() || !lastModifiedSoftware.isValid()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
487 DEBUG << "Could not read headers: " << getLastErrorMsg();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
488 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
489 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
490
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
491 if (!mLastModSW.isValid() || lastModifiedSoftware > mLastModSW) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
492 QString dataDirectory = getDataDirectory();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
493
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
494 if (dataDirectory.isEmpty()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
495 DEBUG << "Failed to get data directory";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
496 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
497 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
498
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
499 QString fileName = dataDirectory.append("/SW-")
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
500 .append(lastModifiedSoftware.toString("yyyymmddHHmmss"))
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
501 .append(".exe");
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
502
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
503 DEBUG << "fileName: " << fileName;
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
504
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
505 if (!downloadFile(hSession, hConnect, L""SW_RESOURCE,
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
506 fileName, MAX_SW_SIZE)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
507 DEBUG << "Error downloading File: " << getLastErrorMsg();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
508 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
509 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
510
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
511 emit newSoftwareAvailable(fileName, lastModifiedSoftware);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
512 } else if (!mLastModList.isValid() || lastModifiedList > mLastModList) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
513 QString dataDirectory = getDataDirectory();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
514
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
515 if (dataDirectory.isEmpty()) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
516 DEBUG << "Failed to get data directory";
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
517 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
518 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
519
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
520 QString fileName = dataDirectory.append("/list-")
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
521 .append(lastModifiedSoftware.toString("yyyymmddHHmmss"))
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
522 .append(".txt");
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
523
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
524 DEBUG << "fileName: " << fileName;
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
525
17
c12825a651ed Read out content-length and use this to skip existing files
Andre Heinecke <aheinecke@intevation.de>
parents: 15
diff changeset
526 if (!downloadFile(hSession, hConnect, L""LIST_RESOURCE,
15
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
527 fileName, MAX_LIST_SIZE)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
528 DEBUG << "Error downloading File: " << getLastErrorMsg();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
529 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
530 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
531
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
532 emit newListAvailable(fileName, lastModifiedList);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
533 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
534
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
535 DEBUG << "SW date: " << lastModifiedSoftware;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
536 DEBUG << "List date: " << lastModifiedList;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
537
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
538 /*if (!WinHttpQueryDataAvailable(hRequest, &dataAvaiable)) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
539 DEBUG << "Failed to query data Available: " << getLastErrorMsg();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
540 goto cleanup;
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
541 }*/
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
542
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
543 if (!results) {
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
544 // Report any errors.
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
545 DEBUG << "Error" << GetLastError();
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
546 emit error(tr("Unknown Problem when connecting"), ErrUnknown);
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
547 }
95e1b6edf2fc Implement more downloader functionality for Windows
Andre Heinecke <aheinecke@intevation.de>
parents: 11
diff changeset
548 cleanup:
11
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
549 if (hConnect) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
550 WinHttpCloseHandle(hConnect);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
551 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
552
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
553 if (hSession) {
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
554 WinHttpCloseHandle(hSession);
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
555 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
556 return;
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
557 }
7e2f14c7aba2 Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
558 #endif

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