Mercurial > trustbridge
comparison ui/downloader.cpp @ 54:09cd242d8443
Split out Header parsing into it's own function
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Mon, 17 Mar 2014 14:48:05 +0000 |
parents | d28e2624c1d5 |
children | 7e304573ebd1 |
comparison
equal
deleted
inserted
replaced
53:0aacc291c04b | 54:09cd242d8443 |
---|---|
61 } | 61 } |
62 } | 62 } |
63 return cDir.absolutePath(); | 63 return cDir.absolutePath(); |
64 } | 64 } |
65 | 65 |
66 QMap<QString, QString> Downloader::parseHeaders(QByteArray *data) | |
67 { | |
68 int bodyStart = data->indexOf("\r\n\r\n"); | |
69 QMap<QString, QString> retval; | |
70 QByteArray headers; | |
71 QTextStream responseStream(&headers); | |
72 if (bodyStart == -1) { | |
73 qDebug() << "Could not find header end."; | |
74 emit error(tr("Invalid response"), | |
75 SSLConnection::InvalidResponse); | |
76 return retval; | |
77 } | |
78 | |
79 /* Take the headers with one additional line break */ | |
80 headers = data->left(bodyStart + 2); | |
81 /* Chop off the head */ | |
82 | |
83 while (1) { | |
84 QString line = responseStream.readLine(); | |
85 int sepPos = -1; | |
86 if (line.isNull()) { | |
87 break; | |
88 } | |
89 sepPos = line.indexOf(": "); | |
90 if (sepPos == -1) { | |
91 continue; | |
92 } | |
93 QString key = line.left(sepPos); | |
94 QString value = line.right(line.size() - sepPos - 2); | |
95 | |
96 retval.insert(key, value); | |
97 } | |
98 | |
99 *data = data->right(data->size() - bodyStart - 4); | |
100 return retval; | |
101 } | |
66 | 102 |
67 QDateTime Downloader::getLastModifiedHeader(const QString &resource) { | 103 QDateTime Downloader::getLastModifiedHeader(const QString &resource) { |
68 int ret = -1; | 104 int ret = -1; |
69 QByteArray response; | 105 QByteArray response; |
70 QTextStream responseStream(&response); | 106 QTextStream responseStream(&response); |
71 QLocale cLocale = QLocale::c(); | 107 QLocale cLocale = QLocale::c(); |
108 QMap<QString, QString> headers; | |
72 QString headRequest = | 109 QString headRequest = |
73 QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource); | 110 QString::fromLatin1("HEAD %1 HTTP/1.0\r\n\r\n").arg(resource); |
74 | 111 |
75 ret = mSSLConnection.write(headRequest.toUtf8()); | 112 ret = mSSLConnection.write(headRequest.toUtf8()); |
76 if (ret != 0) { | 113 if (ret != 0) { |
84 qDebug() << "No response"; | 121 qDebug() << "No response"; |
85 emit error (tr("Connection lost"), SSLConnection::ConnectionLost); | 122 emit error (tr("Connection lost"), SSLConnection::ConnectionLost); |
86 return QDateTime(); | 123 return QDateTime(); |
87 } | 124 } |
88 | 125 |
89 while (1) { | 126 headers = parseHeaders(&response); |
90 QString line = responseStream.readLine(); | 127 const QString lastModified = headers.value("Last-Modified"); |
91 if (line.isNull()) { | 128 qDebug() << "Headers: " << headers; |
92 break; | 129 if (!lastModified.isEmpty()) { |
93 } | 130 QDateTime candidate = cLocale.toDateTime(lastModified, |
94 if (line.startsWith("Last-Modified:")) { | 131 "ddd, dd MMM yyyy HH:mm:ss' GMT'"); |
95 QDateTime candidate = cLocale.toDateTime(line, "'Last-Modified: 'ddd, dd MMM yyyy HH:mm:ss' GMT'"); | 132 if (candidate.isValid()) { |
96 qDebug() << "Parsed line : " << line << " to " << candidate; | 133 return candidate; |
97 if (candidate.isValid()) { | 134 } |
98 return candidate; | 135 } |
99 } | |
100 } | |
101 } | |
102 qDebug() << "Response: " << response; | |
103 emit error (tr("Invalid response from the server"), SSLConnection::InvalidResponse); | 136 emit error (tr("Invalid response from the server"), SSLConnection::InvalidResponse); |
137 qDebug() << "Response from server was: " << response; | |
104 return QDateTime(); | 138 return QDateTime(); |
105 } | 139 } |
106 | 140 |
107 bool Downloader::downloadFile(const QString &resource, | 141 bool Downloader::downloadFile(const QString &resource, |
108 const QString &fileName, | 142 const QString &fileName, |
126 if (ret != 0) { | 160 if (ret != 0) { |
127 emit error(tr("Connection lost"), SSLConnection::ConnectionLost); | 161 emit error(tr("Connection lost"), SSLConnection::ConnectionLost); |
128 return false; | 162 return false; |
129 } | 163 } |
130 | 164 |
165 bool inBody = false; | |
166 QMap <QString, QString> headers; | |
131 do { | 167 do { |
132 /* Read the response in 8KiB chunks */ | 168 /* Read the response in 8KiB chunks */ |
169 int responseSize = 0; | |
133 QByteArray response = mSSLConnection.read(8192); | 170 QByteArray response = mSSLConnection.read(8192); |
134 if (response.isNull()) { | 171 if (response.isNull()) { |
135 qDebug() << "Error reading response"; | 172 qDebug() << "Error reading response"; |
136 emit error(tr("Connection lost"), SSLConnection::ConnectionLost); | 173 emit error(tr("Connection lost"), SSLConnection::ConnectionLost); |
137 return false; | 174 return false; |
138 } | 175 } |
176 responseSize = response.size(); | |
177 if (!inBody) { | |
178 headers = parseHeaders(&response); | |
179 inBody = true; | |
180 } | |
139 outputFile.write(response); | 181 outputFile.write(response); |
140 qDebug() << "Wrote: "<< response.size(); | 182 bytesRead += responseSize; |
141 bytesRead += response.size(); | 183 if (responseSize < 8192) { |
142 if (response.size() < 8192) { | 184 /* Nothing more to read */ |
143 /* Nothing more to read for us */ | |
144 break; | 185 break; |
145 } | 186 } |
187 /* TODO Emit progress */ | |
146 } while (bytesRead < maxSize); | 188 } while (bytesRead < maxSize); |
147 | 189 |
148 return outputFile.commit(); | 190 return outputFile.commit(); |
149 } | 191 } |
150 | 192 |