Mercurial > trustbridge
annotate ui/downloader_win.cpp @ 12:9121eea6d93f
Fix constructor usage. Too much Java.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 14 Feb 2014 11:30:55 +0000 |
parents | 7e2f14c7aba2 |
children | 95e1b6edf2fc |
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> |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
22 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
23 #define DEBUG if (1) qDebug() << __PRETTY_FUNCTION__ |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
24 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
25 const QString getLastErrorMsg() { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
26 LPWSTR bufPtr = NULL; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
27 DWORD err = GetLastError(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
28 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
29 FORMAT_MESSAGE_FROM_SYSTEM | |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
30 FORMAT_MESSAGE_IGNORE_INSERTS, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
31 NULL, err, 0, (LPWSTR)&bufPtr, 0, NULL); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
32 const QString result = |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
33 (bufPtr) ? QString::fromUtf16((const ushort*)bufPtr).trimmed() : |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
34 QString("Unknown Error %1").arg(err); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
35 LocalFree(bufPtr); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
36 return result; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
37 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
38 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
39 /** @brief open a session with appropiate proxy settings |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
40 * |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
41 * @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
|
42 * |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
43 * 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
|
44 * |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
45 * @returns True on success, false on error. |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
46 */ |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
47 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
48 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
49 bool openSession(HINTERNET *pHSession) |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
50 { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
51 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
52 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
53 DEBUG; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
54 if (!pHSession) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
55 SetLastError(ERROR_INVALID_PARAMETER); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
56 return false; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
57 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
58 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
59 qDebug() << "2"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
60 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
|
61 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
62 if (WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig)) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
63 if (proxyConfig.fAutoDetect) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
64 // TODO Handle this |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
65 qDebug() << "Autodetect is set"; |
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 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
68 if (proxyConfig.lpszProxy || proxyConfig.lpszProxyBypass) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
69 DEBUG << "Using proxies."; |
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 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
72 if (proxyConfig.lpszProxy) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
73 *pHSession = WinHttpOpen(L"M13 "MYVERSION, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
74 WINHTTP_ACCESS_TYPE_NAMED_PROXY, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
75 proxyConfig.lpszProxy, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
76 proxyConfig.lpszProxyBypass, 0); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
77 } |
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 |
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 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
|
82 *pHSession = WinHttpOpen(L"M13 "MYVERSION, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
83 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
84 WINHTTP_NO_PROXY_NAME, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
85 WINHTTP_NO_PROXY_BYPASS, 0); |
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 // Cleanup |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
88 if (proxyConfig.lpszAutoConfigUrl) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
89 GlobalFree(proxyConfig.lpszAutoConfigUrl); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
90 } |
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 if (proxyConfig.lpszProxy) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
93 GlobalFree(proxyConfig.lpszProxy); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
94 } |
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 if (proxyConfig.lpszProxyBypass) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
97 GlobalFree(proxyConfig.lpszProxyBypass); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
98 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
99 return *pHSession; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
100 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
101 |
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 /** @brief initialize a connection in the session |
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 * @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
|
106 * @param[inout] *pHConnect pointer to the connection. |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
107 * @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
|
108 * |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
109 * 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
|
110 * |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
111 * @returns True on success, false on error. |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
112 */ |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
113 bool initializeConnection(HINTERNET hSession, HINTERNET *pHConnect, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
114 LPCWSTR url) |
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 DEBUG; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
117 if (!hSession || !pHConnect) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
118 SetLastError(ERROR_INVALID_PARAMETER); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
119 return false; |
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 // Initialize connection. No request is done here. |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
122 *pHConnect = WinHttpConnect(hSession, url, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
123 INTERNET_DEFAULT_HTTPS_PORT, 0); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
124 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
125 return *pHConnect; |
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 Create a request |
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[in] HConnect the connection to use. |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
132 * @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
|
133 * @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
|
134 * @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
|
135 * representation. |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
136 * |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
137 * 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
|
138 * 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
|
139 * it. |
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 * @returns True on success, false on error. |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
142 */ |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
143 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
144 bool createRequest(HINTERNET hSession, HINTERNET hConnect, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
145 HINTERNET *pHRequest, LPCWSTR requestType, LPCWSTR resource) |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
146 { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
147 DEBUG; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
148 if (!hSession || !hConnect || !pHRequest) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
149 SetLastError(ERROR_INVALID_PARAMETER); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
150 return false; |
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 *pHRequest = WinHttpOpenRequest(hConnect, requestType, resource, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
154 NULL, WINHTTP_NO_REFERER, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
155 WINHTTP_DEFAULT_ACCEPT_TYPES, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
156 WINHTTP_FLAG_SECURE); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
157 return *pHRequest; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
158 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
159 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
160 void Downloader::run() { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
161 BOOL bResults = FALSE; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
162 HINTERNET hSession = NULL, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
163 hConnect = NULL, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
164 hRequest = NULL; |
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 SYSTEMTIME lastModified; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
167 DWORD sizeOfSystemtime = sizeof (SYSTEMTIME); |
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 memset(&lastModified, 0, sizeof (SYSTEMTIME)); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
170 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
171 if (!openSession(&hSession)) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
172 DEBUG << "Failed to open session: " << getLastErrorMsg(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
173 return; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
174 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
175 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
176 if (!initializeConnection(hSession, &hConnect, L"www.intevation.de")) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
177 DEBUG << "Failed to initialize connection: " << getLastErrorMsg(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
178 goto cleanup; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
179 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
180 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
181 if (!createRequest(hSession, hConnect, &hRequest, L"GET", L"/")) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
182 DEBUG << "Failed to create the request: " << getLastErrorMsg(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
183 goto cleanup; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
184 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
185 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
186 if (hRequest) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
187 DEBUG << "Doing Request"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
188 bResults = WinHttpSendRequest(hRequest, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
189 WINHTTP_NO_ADDITIONAL_HEADERS, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
190 0, WINHTTP_NO_REQUEST_DATA, 0, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
191 0, 0); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
192 } else { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
193 DEBUG << "Error: " << GetLastError(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
194 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
195 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
196 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
197 if (bResults) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
198 DEBUG << "Recieving Response"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
199 bResults = WinHttpReceiveResponse(hRequest, NULL); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
200 } else { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
201 DEBUG << "Error: " << GetLastError(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
202 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
203 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
204 if (bResults) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
205 DEBUG << "Querying Headers"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
206 bResults = WinHttpQueryHeaders(hRequest, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
207 WINHTTP_QUERY_LAST_MODIFIED | |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
208 WINHTTP_QUERY_FLAG_SYSTEMTIME, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
209 NULL, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
210 &lastModified, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
211 &sizeOfSystemtime, |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
212 WINHTTP_NO_HEADER_INDEX); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
213 } else { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
214 DWORD errCode = GetLastError(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
215 switch (errCode) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
216 case ERROR_WINHTTP_HEADER_NOT_FOUND: |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
217 DEBUG << "Header not found"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
218 break; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
219 case ERROR_WINHTTP_INCORRECT_HANDLE_STATE: |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
220 DEBUG << "Incorrect handle state"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
221 break; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
222 case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE: |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
223 DEBUG << "Incorrect handle type"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
224 break; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
225 case ERROR_WINHTTP_INTERNAL_ERROR: |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
226 DEBUG << "Internal error"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
227 break; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
228 case ERROR_NOT_ENOUGH_MEMORY: |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
229 DEBUG << "OOM"; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
230 break; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
231 default: |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
232 DEBUG << "Error: " << getLastErrorMsg(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
233 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
234 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
235 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
236 DEBUG << "Last modified year: " << lastModified.wYear; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
237 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
238 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
239 if (!bResults) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
240 // Report any errors. |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
241 DEBUG << "Error" << GetLastError(); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
242 emit error(tr("Unknown Problem when connecting"), Unknown); |
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 cleanup: |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
245 if (hRequest) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
246 WinHttpCloseHandle(hRequest); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
247 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
248 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
249 if (hConnect) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
250 WinHttpCloseHandle(hConnect); |
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 |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
254 if (hSession) { |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
255 WinHttpCloseHandle(hSession); |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
256 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
257 return; |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
258 } |
7e2f14c7aba2
Split up downloader component and further implement it
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
259 #endif |