comparison ui/downloader.h @ 27:62cd56cea09b

Start on polarssl Downloader. This breaks the windows build for now
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 12 Mar 2014 16:15:52 +0100
parents 95e1b6edf2fc
children d8e93fa1fc93
comparison
equal deleted inserted replaced
26:cbd57d767dfa 27:62cd56cea09b
8 8
9 #include <QThread> 9 #include <QThread>
10 #include <QString> 10 #include <QString>
11 #include <QByteArray> 11 #include <QByteArray>
12 #include <QDateTime> 12 #include <QDateTime>
13 #include <QUrl>
13 14
14 #ifdef Q_OS_WIN 15 #include <polarssl/x509_crt.h>
15 #include <windows.h> 16 #include <polarssl/entropy.h>
16 #include <winhttp.h> 17 #include <polarssl/ctr_drbg.h>
17 #endif 18 #include <polarssl/ssl.h>
18
19 19
20 class Downloader: public QThread 20 class Downloader: public QThread
21 { 21 {
22 Q_OBJECT 22 Q_OBJECT
23 23
42 Downloader(QObject* parent, const QString& url, 42 Downloader(QObject* parent, const QString& url,
43 const QByteArray& certificate = QByteArray(), 43 const QByteArray& certificate = QByteArray(),
44 const QDateTime& newestSW = QDateTime(), 44 const QDateTime& newestSW = QDateTime(),
45 const QDateTime& newestList = QDateTime()); 45 const QDateTime& newestList = QDateTime());
46 46
47 ~Downloader();
47 48
48 enum ErrorCode { 49 enum ErrorCode {
50 NoError,
49 NoConnection, 51 NoConnection,
50 InvalidCertificate, 52 InvalidCertificate,
53 InvalidPinnedCertificate,
51 ConnectionLost, 54 ConnectionLost,
52 Timeout, 55 Timeout,
53 ErrUnknown 56 ErrUnknown
54 }; 57 };
55 58
61 * 64 *
62 * @returns The directory in which downloaded files are placed. 65 * @returns The directory in which downloaded files are placed.
63 **/ 66 **/
64 QString getDataDirectory(); 67 QString getDataDirectory();
65 68
69 /**
70 * @brief get the current error state
71 *
72 * @returns The current error state.
73 **/
74 ErrorCode getErrorState();
66 75
67 protected: 76 protected:
68 void run(); 77 void run();
69 78
70 private: 79 private:
71 QString mUrl; 80 QUrl mUrl;
72 QByteArray mCert; 81 QByteArray mPinnedCert;
82 x509_crt mX509PinnedCert;
83 entropy_context mEntropy;
84 ctr_drbg_context mCtr_drbg;
85 ssl_context mSSL;
73 86
74 QDateTime mLastModSW; 87 QDateTime mLastModSW;
75 QDateTime mLastModList; 88 QDateTime mLastModList;
76 89
90 /* Convienience to avoid having to parse all
91 * PolarSSL errors */
92 ErrorCode mErrorState;
93
94 bool mInitialized;
95
96 int mServerFD;
97
98
99 /* @brief: Initialize polarssl structures
100 *
101 * This wraps polarssl initialization functions
102 * that can return an error.
103 * Sets the error state accordingly.
104 *
105 * @returns: 0 on success a polarssl error otherwise.
106 */
107 int init();
108
109 /** @brief: Establish the connection
110 *
111 * @returns 0 on success otherwise a polarssl error or -1 is returned
112 */
113 int establishSSLConnection();
77 #ifdef Q_OS_WIN 114 #ifdef Q_OS_WIN
78 /** @brief Download a file from the Internet
79 *
80 * @param[in] HSession the session to work in.
81 * @param[in] HConnect the connection to use.
82 * @param[in] resource the resource to download.
83 * @param[in] filename where the file should be saved.
84 * @param[in] maxSize maximum amount of bytes to download
85 *
86 * @returns True if the download was successful.
87 */
88 bool downloadFile(HINTERNET hSession, HINTERNET hConnect,
89 LPCWSTR resource, const QString &filename, DWORD maxSize);
90
91 /** @brief get the last modified header of a resource.
92 *
93 * On error call getLastError to get extended error information.
94 * This function still does not do any networking but only initializes
95 * it.
96 *
97 * @param[in] HSession the session to work in.
98 * @param[in] HConnect the connection to use.
99 * @param[in] resource the resource to check the last-modified date on
100 *
101 * @returns the last modified date or a null datetime in case of errors
102 */
103 QDateTime getLastModifiedHeader(HINTERNET hSession,
104 HINTERNET hConnect, LPCWSTR resource);
105
106 /** @brief verify that the certificate of the request matches
107 *
108 * Validates the certificate against the member variable certificate
109 *
110 * @param[in] hRequest: The request from which to get the certificate
111 *
112 * @returns True if the certificate exactly matches the one in hRequest
113 */
114
115 bool verifyCertificate(HINTERNET hRequest);
116 #endif 115 #endif
117 116
118 Q_SIGNALS: 117 Q_SIGNALS:
119 /** 118 /**
120 * @brief software update is available 119 * @brief software update is available
140 * @brief An error happened 139 * @brief An error happened
141 * 140 *
142 * @param[out] message: A message to show. Can be empty. 141 * @param[out] message: A message to show. Can be empty.
143 * @param[out] errorCode: ErrorCode of this error. 142 * @param[out] errorCode: ErrorCode of this error.
144 */ 143 */
145 void error(const QString &message, ErrorCode error); 144 void error(const QString &message, Downloader::ErrorCode error);
146 }; 145 };
147 #endif 146 #endif

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