Rework and improve http download cache: check cache against file on the server and...
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blobfcc2d4163cd8f4677b1355734b4ca908399ce54e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <QtCore>
21 #include <QtNetwork>
22 #include <QtDebug>
24 #include "httpget.h"
26 QDir HttpGet::m_globalCache; //< global cach path value for new objects
27 QUrl HttpGet::m_globalProxy; //< global proxy value for new objects
29 HttpGet::HttpGet(QObject *parent)
30 : QObject(parent)
32 outputToBuffer = true;
33 m_cached = false;
34 m_noHeaderCheck = false;
35 getRequest = -1;
36 // if a request is cancelled before a reponse is available return some
37 // hint about this in the http response instead of nonsense.
38 m_response = -1;
40 // default to global proxy / cache if not empty.
41 // proxy is automatically enabled, disable it by setting an empty proxy
42 // cache is enabled to be in line, can get disabled with setCache(bool)
43 if(!m_globalProxy.isEmpty())
44 setProxy(m_globalProxy);
45 m_usecache = false;
46 m_cachedir = m_globalCache;
47 connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
48 connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
49 connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
50 connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
51 connect(&http, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
52 connect(&http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
54 connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
59 //! @brief set cache path
60 // @param d new directory to use as cache path
61 void HttpGet::setCache(QDir d)
63 m_cachedir = d;
64 bool result;
65 result = initializeCache(d);
66 qDebug() << "[HTTP]"<< __func__ << "(QDir)" << d.absolutePath() << result;
67 m_usecache = result;
71 /** @brief enable / disable cache useage
72 * @param c set cache usage
74 void HttpGet::setCache(bool c)
76 qDebug() << "[HTTP]" << __func__ << "(bool) =" << c;
77 m_usecache = c;
78 // make sure cache is initialized
79 if(c)
80 m_usecache = initializeCache(m_cachedir);
84 bool HttpGet::initializeCache(const QDir& d)
86 bool result;
87 QString p = d.absolutePath() + "/rbutil-cache";
88 if(QFileInfo(d.absolutePath()).isDir())
90 if(!QFileInfo(p).isDir())
91 result = d.mkdir("rbutil-cache");
92 else
93 result = true;
95 else
96 result = false;
98 return result;
103 /** @brief read all downloaded data into a buffer
104 * @return data
106 QByteArray HttpGet::readAll()
108 return dataBuffer;
112 /** @brief get http error
113 * @return http error
115 QHttp::Error HttpGet::error()
117 return http.error();
121 void HttpGet::httpProgress(int read, int total)
123 emit dataReadProgress(read, total);
127 void HttpGet::setProxy(const QUrl &proxy)
129 qDebug() << "[HTTP]" << __func__ << "(QUrl)" << proxy.toString();
130 m_proxy = proxy;
131 http.setProxy(m_proxy.host(), m_proxy.port(), m_proxy.userName(), m_proxy.password());
135 void HttpGet::setProxy(bool enable)
137 qDebug() << "[HTTP]" << __func__ << "(bool)" << enable;
138 if(enable)
139 http.setProxy(m_proxy.host(), m_proxy.port(), m_proxy.userName(), m_proxy.password());
140 else
141 http.setProxy("", 0);
145 void HttpGet::setFile(QFile *file)
147 outputFile = file;
148 outputToBuffer = false;
149 qDebug() << "[HTTP]" << __func__ << "(QFile*)" << outputFile->fileName();
153 void HttpGet::abort()
155 http.abort();
156 if(!outputToBuffer)
157 outputFile->close();
161 bool HttpGet::getFile(const QUrl &url)
163 if (!url.isValid()) {
164 qDebug() << "[HTTP] Error: Invalid URL" << endl;
165 return false;
168 if (url.scheme() != "http") {
169 qDebug() << "[HTTP] Error: URL must start with 'http:'" << endl;
170 return false;
173 if (url.path().isEmpty()) {
174 qDebug() << "[HTTP] Error: URL has no path" << endl;
175 return false;
177 // if no output file was set write to buffer
178 if(!outputToBuffer) {
179 if (!outputFile->open(QIODevice::ReadWrite)) {
180 qDebug() << "[HTTP] Error: Cannot open " << qPrintable(outputFile->fileName())
181 << " for writing: " << qPrintable(outputFile->errorString())
182 << endl;
183 return false;
186 qDebug() << "[HTTP] downloading" << url.toEncoded();
187 // create request
188 http.setHost(url.host(), url.port(80));
189 // construct query (if any)
190 QList<QPair<QString, QString> > qitems = url.queryItems();
191 if(url.hasQuery()) {
192 m_query = "?";
193 for(int i = 0; i < qitems.size(); i++)
194 m_query += QUrl::toPercentEncoding(qitems.at(i).first, "/") + "="
195 + QUrl::toPercentEncoding(qitems.at(i).second, "/") + "&";
198 // create hash used for caching
199 m_hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
200 m_path = QString(QUrl::toPercentEncoding(url.path(), "/"));
202 if(m_noHeaderCheck || !m_usecache) {
203 getFileFinish();
205 else {
206 // request HTTP header
207 connect(this, SIGNAL(headerFinished()), this, SLOT(getFileFinish()));
208 headRequest = http.head(m_path + m_query);
211 return true;
215 void HttpGet::getFileFinish()
217 m_cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + m_hash;
218 if(m_usecache) {
219 // check if the file is present in cache
220 qDebug() << "[HTTP] cache ENABLED";
221 QFileInfo cachefile = QFileInfo(m_cachefile);
222 if(cachefile.isReadable()
223 && cachefile.size() > 0
224 && cachefile.lastModified() > m_serverTimestamp) {
226 qDebug() << "[HTTP] cached file found:" << m_cachefile;
228 getRequest = -1;
229 QFile c(m_cachefile);
230 if(!outputToBuffer) {
231 qDebug() << "[HTTP] copying cache file to output" << outputFile->fileName();
232 c.open(QIODevice::ReadOnly);
233 outputFile->open(QIODevice::ReadWrite);
234 outputFile->write(c.readAll());
235 outputFile->close();
236 c.close();
238 else {
239 qDebug() << "[HTTP] reading cache file into buffer";
240 c.open(QIODevice::ReadOnly);
241 dataBuffer = c.readAll();
242 c.close();
244 m_response = 200; // fake "200 OK" HTTP response
245 m_cached = true;
246 httpDone(false); // we're done now. Fake http "done" signal.
247 return;
249 else {
250 if(cachefile.isReadable())
251 qDebug() << "[HTTP] file in cache timestamp:" << cachefile.lastModified();
252 else
253 qDebug() << "[HTTP] file not in cache.";
254 qDebug() << "[HTTP] server file timestamp:" << m_serverTimestamp;
255 qDebug() << "[HTTP] downloading file to" << m_cachefile;
256 // unlink old cache file
257 if(cachefile.isReadable())
258 QFile(m_cachefile).remove();
262 else {
263 qDebug() << "[HTTP] cache DISABLED";
266 if(outputToBuffer) {
267 qDebug() << "[HTTP] downloading to buffer.";
268 getRequest = http.get(m_path + m_query);
270 else {
271 qDebug() << "[HTTP] downloading to file:"
272 << qPrintable(outputFile->fileName());
273 getRequest = http.get(m_path + m_query, outputFile);
275 qDebug() << "[HTTP] GET request scheduled, id:" << getRequest;
277 return;
281 void HttpGet::httpDone(bool error)
283 if (error) {
284 qDebug() << "[HTTP] Error: " << qPrintable(http.errorString()) << httpResponse();
286 if(!outputToBuffer)
287 outputFile->close();
289 if(m_usecache && !m_cached) {
290 qDebug() << "[HTTP] creating cache file" << m_cachefile;
291 QFile c(m_cachefile);
292 c.open(QIODevice::ReadWrite);
293 if(!outputToBuffer) {
294 outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
295 c.write(outputFile->readAll());
296 outputFile->close();
298 else
299 c.write(dataBuffer);
301 c.close();
303 m_serverTimestamp = QDateTime();
304 // take care of concurring requests. If there is still one running,
305 // don't emit done(). That request will call this slot again.
306 if(http.currentId() == 0 && !http.hasPendingRequests())
307 emit done(error);
311 void HttpGet::httpFinished(int id, bool error)
313 qDebug() << "[HTTP]" << __func__ << "(int, bool) =" << id << error;
314 if(id == getRequest) {
315 dataBuffer = http.readAll();
317 emit requestFinished(id, error);
319 qDebug() << "[HTTP] hasPendingRequests =" << http.hasPendingRequests();
322 if(id == headRequest) {
323 QHttpResponseHeader h = http.lastResponse();
325 QString date = h.value("Last-Modified").simplified();
326 if(date.isEmpty()) {
327 m_serverTimestamp = QDateTime(); // no value = invalid
328 emit headerFinished();
329 return;
331 // to successfully parse the date strip weekday and timezone
332 date.remove(0, date.indexOf(" ") + 1);
333 if(date.endsWith("GMT"))
334 date.truncate(date.indexOf(" GMT"));
335 // distinguish input formats (see RFC1945)
336 // RFC 850
337 if(date.contains("-"))
338 m_serverTimestamp = QDateTime::fromString(date, "dd-MMM-yy hh:mm:ss");
339 // asctime format
340 else if(date.at(0).isLetter())
341 m_serverTimestamp = QDateTime::fromString(date, "MMM d hh:mm:ss yyyy");
342 // RFC 822
343 else
344 m_serverTimestamp = QDateTime::fromString(date, "dd MMM yyyy hh:mm:ss");
345 qDebug() << "[HTTP] Header Request Date:" << date << ", parsed:" << m_serverTimestamp;
346 emit headerFinished();
347 return;
349 if(id == getRequest)
350 emit requestFinished(id, error);
353 void HttpGet::httpStarted(int id)
355 qDebug() << "[HTTP]" << __func__ << "(int) =" << id;
356 qDebug() << "headRequest" << headRequest << "getRequest" << getRequest;
360 QString HttpGet::errorString()
362 return http.errorString();
366 void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
368 // if there is a network error abort all scheduled requests for
369 // this download
370 m_response = resp.statusCode();
371 if(m_response != 200) {
372 qDebug() << "[HTTP] response error =" << m_response << resp.reasonPhrase();
373 http.abort();
375 // 301 -- moved permanently
376 // 302 -- found
377 // 303 -- see other
378 // 307 -- moved temporarily
379 // in all cases, header: location has the correct address so we can follow.
380 if(m_response == 301 || m_response == 302 || m_response == 303 || m_response == 307) {
381 // start new request with new url
382 qDebug() << "[HTTP] response =" << m_response << "- following";
383 getFile(resp.value("location") + m_query);
388 int HttpGet::httpResponse()
390 return m_response;
394 void HttpGet::httpState(int state)
396 QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
397 "Reading", "Connected", "Closing"};
398 if(state <= 6)
399 qDebug() << "[HTTP]" << __func__ << "() = " << s[state];
400 else qDebug() << "[HTTP]" << __func__ << "() = " << state;