comparison ui/downloader.cpp @ 46:d28e2624c1d5

Reset connection before the next request. This makes the downloader work for the first simple test.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 17 Mar 2014 10:51:47 +0000
parents c6125d73faf4
children 09cd242d8443
comparison
equal deleted inserted replaced
45:c6125d73faf4 46:d28e2624c1d5
19 #include <polarssl/error.h> 19 #include <polarssl/error.h>
20 #include <polarssl/certs.h> 20 #include <polarssl/certs.h>
21 21
22 #define MAX_SW_SIZE 10485760 22 #define MAX_SW_SIZE 10485760
23 #define MAX_LIST_SIZE 1048576 23 #define MAX_LIST_SIZE 1048576
24
25 #define LIST_RESOURCE "/incoming/aheinecke/test"
26 #define SW_RESOURCE "/incoming/aheinecke/test"
27 24
28 25
29 Downloader::Downloader(QObject* parent, const QString& url, 26 Downloader::Downloader(QObject* parent, const QString& url,
30 const QByteArray& certificate, 27 const QByteArray& certificate,
31 const QDateTime& newestSW, 28 const QDateTime& newestSW,
71 int ret = -1; 68 int ret = -1;
72 QByteArray response; 69 QByteArray response;
73 QTextStream responseStream(&response); 70 QTextStream responseStream(&response);
74 QLocale cLocale = QLocale::c(); 71 QLocale cLocale = QLocale::c();
75 QString headRequest = 72 QString headRequest =
76 QString::fromLatin1("HEAD %1 HTTP/1.1\r\n" 73 QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource);
77 "Connection: Keep-Alive\r\n"
78 "\r\n\r\n").arg(resource);
79 74
80 ret = mSSLConnection.write(headRequest.toUtf8()); 75 ret = mSSLConnection.write(headRequest.toUtf8());
81 if (ret != 0) { 76 if (ret != 0) {
82 emit error (tr("Connection lost"), SSLConnection::ConnectionLost); 77 emit error (tr("Connection lost"), SSLConnection::ConnectionLost);
83 return QDateTime(); 78 return QDateTime();
102 if (candidate.isValid()) { 97 if (candidate.isValid()) {
103 return candidate; 98 return candidate;
104 } 99 }
105 } 100 }
106 } 101 }
107 102 qDebug() << "Response: " << response;
108 emit error (tr("Invalid response from the server"), SSLConnection::InvalidResponse); 103 emit error (tr("Invalid response from the server"), SSLConnection::InvalidResponse);
109 return QDateTime(); 104 return QDateTime();
110 } 105 }
111 106
112 bool Downloader::downloadFile(const QString &resource, 107 bool Downloader::downloadFile(const QString &resource,
114 size_t maxSize) 109 size_t maxSize)
115 { 110 {
116 int ret = -1; 111 int ret = -1;
117 size_t bytesRead = 0; 112 size_t bytesRead = 0;
118 QString getRequest = 113 QString getRequest =
119 QString::fromLatin1("GET %1 HTTP/1.1\r\n\r\n").arg(resource); 114 QString::fromLatin1("GET %1 HTTP/1.0\r\n\r\n").arg(resource);
120 115
121 QSaveFile outputFile(fileName); 116 QSaveFile outputFile(fileName);
122 117
123 ret = mSSLConnection.write(getRequest.toUtf8()); 118 ret = mSSLConnection.write(getRequest.toUtf8());
124 119
132 emit error(tr("Connection lost"), SSLConnection::ConnectionLost); 127 emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
133 return false; 128 return false;
134 } 129 }
135 130
136 do { 131 do {
132 /* Read the response in 8KiB chunks */
137 QByteArray response = mSSLConnection.read(8192); 133 QByteArray response = mSSLConnection.read(8192);
138 if (response.isNull()) { 134 if (response.isNull()) {
139 qDebug() << "Error reading response"; 135 qDebug() << "Error reading response";
140 emit error(tr("Connection lost"), SSLConnection::ConnectionLost); 136 emit error(tr("Connection lost"), SSLConnection::ConnectionLost);
141 return false; 137 return false;
142 } 138 }
143 if (response.isEmpty()) { 139 outputFile.write(response);
144 /* We have read everything there is to read */ 140 qDebug() << "Wrote: "<< response.size();
141 bytesRead += response.size();
142 if (response.size() < 8192) {
143 /* Nothing more to read for us */
145 break; 144 break;
146 } 145 }
147
148 outputFile.write(response);
149 bytesRead += response.size();
150 } while (bytesRead < maxSize); 146 } while (bytesRead < maxSize);
151 147
152 return outputFile.commit(); 148 return outputFile.commit();
153 } 149 }
154 150
192 .append(remoteModSW.toString("yyyymmddHHmmss")) 188 .append(remoteModSW.toString("yyyymmddHHmmss"))
193 .append(".exe"); 189 .append(".exe");
194 190
195 qDebug() << "fileName: " << fileName; 191 qDebug() << "fileName: " << fileName;
196 192
197 if (!downloadFile(QString::fromLatin1(SW_RESOURCE), 193 if (!downloadFile(mResourceSW, fileName, MAX_SW_SIZE)) {
198 fileName, MAX_SW_SIZE)) {
199 return; 194 return;
200 } 195 }
201 196
202 emit newSoftwareAvailable(fileName, remoteModSW); 197 emit newSoftwareAvailable(fileName, remoteModSW);
203 } else if (!mLastModList.isValid() || remoteModList > mLastModList) { 198 } else if (!mLastModList.isValid() || remoteModList > mLastModList) {
212 .append(remoteModSW.toString("yyyymmddHHmmss")) 207 .append(remoteModSW.toString("yyyymmddHHmmss"))
213 .append(".txt"); 208 .append(".txt");
214 209
215 qDebug() << "fileName: " << fileName; 210 qDebug() << "fileName: " << fileName;
216 211
217 if (!downloadFile(QString::fromLatin1(LIST_RESOURCE), 212 if (!downloadFile(mResourceList, fileName, MAX_LIST_SIZE)) {
218 fileName, MAX_LIST_SIZE)) {
219 return; 213 return;
220 } 214 }
221 215
222 emit newListAvailable(fileName, remoteModList); 216 emit newListAvailable(fileName, remoteModList);
223 } 217 }

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