Add support for the Creative ZEN and renew the way firmwares are
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blob1ceaeb002c50d3b0f64c56afe91ef380429a10ec
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 m_usecache = false;
33 outputToBuffer = true;
34 cached = 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 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() << "HttpGet::setCache(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() << "setCache(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 QByteArray HttpGet::readAll()
105 return dataBuffer;
109 /** @brief get http error
110 * @return http error
112 QHttp::Error HttpGet::error()
114 return http.error();
118 void HttpGet::httpProgress(int read, int total)
120 emit dataReadProgress(read, total);
124 void HttpGet::setProxy(const QUrl &proxy)
126 qDebug() << "HttpGet::setProxy(QUrl)" << proxy.toString();
127 m_proxy = proxy;
128 http.setProxy(m_proxy.host(), m_proxy.port(), m_proxy.userName(), m_proxy.password());
132 void HttpGet::setProxy(bool enable)
134 qDebug() << "HttpGet::setProxy(bool)" << enable;
135 if(enable)
136 http.setProxy(m_proxy.host(), m_proxy.port(), m_proxy.userName(), m_proxy.password());
137 else
138 http.setProxy("", 0);
142 void HttpGet::setFile(QFile *file)
144 outputFile = file;
145 outputToBuffer = false;
146 qDebug() << "HttpGet::setFile" << outputFile->fileName();
150 void HttpGet::abort()
152 http.abort();
153 if(!outputToBuffer)
154 outputFile->close();
158 bool HttpGet::getFile(const QUrl &url)
160 if (!url.isValid()) {
161 qDebug() << "Error: Invalid URL" << endl;
162 return false;
165 if (url.scheme() != "http") {
166 qDebug() << "Error: URL must start with 'http:'" << endl;
167 return false;
170 if (url.path().isEmpty()) {
171 qDebug() << "Error: URL has no path" << endl;
172 return false;
174 // if no output file was set write to buffer
175 if(!outputToBuffer) {
176 if (!outputFile->open(QIODevice::ReadWrite)) {
177 qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
178 << " for writing: " << qPrintable(outputFile->errorString())
179 << endl;
180 return false;
183 // put hash generation here so it can get reused later
184 QString hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
185 cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + hash;
186 if(m_usecache) {
187 // check if the file is present in cache
188 qDebug() << "[HTTP] cache ENABLED for" << url.toEncoded();
189 if(QFileInfo(cachefile).isReadable() && QFileInfo(cachefile).size() > 0) {
190 qDebug() << "[HTTP] cached file found!" << cachefile;
191 getRequest = -1;
192 QFile c(cachefile);
193 if(!outputToBuffer) {
194 qDebug() << outputFile->fileName();
195 c.open(QIODevice::ReadOnly);
196 outputFile->open(QIODevice::ReadWrite);
197 outputFile->write(c.readAll());
198 outputFile->close();
199 c.close();
201 else {
202 c.open(QIODevice::ReadOnly);
203 dataBuffer = c.readAll();
204 c.close();
206 response = 200; // fake "200 OK" HTTP response
207 cached = true;
208 httpDone(false); // we're done now. This will emit the correct signal too.
209 return true;
211 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile;
214 else {
215 qDebug() << "[HTTP] cache DISABLED";
218 http.setHost(url.host(), url.port(80));
219 // construct query (if any)
220 QList<QPair<QString, QString> > qitems = url.queryItems();
221 if(url.hasQuery()) {
222 query = "?";
223 for(int i = 0; i < qitems.size(); i++)
224 query += qitems.at(i).first + "=" + qitems.at(i).second + "&";
225 qDebug() << query;
228 QString path;
229 path = QString(QUrl::toPercentEncoding(url.path(), "/"));
230 if(outputToBuffer) {
231 qDebug() << "[HTTP] downloading to buffer:" << url.toString();
232 getRequest = http.get(path + query);
234 else {
235 qDebug() << "[HTTP] downloading to file:" << url.toString() << qPrintable(outputFile->fileName());
236 getRequest = http.get(path + query, outputFile);
238 qDebug() << "[HTTP] request scheduled: GET" << getRequest;
240 return true;
244 void HttpGet::httpDone(bool error)
246 if (error) {
247 qDebug() << "[HTTP] Error: " << qPrintable(http.errorString()) << httpResponse();
249 if(!outputToBuffer)
250 outputFile->close();
252 if(m_usecache && !cached) {
253 qDebug() << "[HTTP] creating cache file" << cachefile;
254 QFile c(cachefile);
255 c.open(QIODevice::ReadWrite);
256 if(!outputToBuffer) {
257 outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
258 c.write(outputFile->readAll());
259 outputFile->close();
261 else
262 c.write(dataBuffer);
264 c.close();
266 emit done(error);
270 void HttpGet::httpFinished(int id, bool error)
272 qDebug() << "HttpGet::httpFinished(int, bool) =" << id << error;
273 if(id == getRequest) dataBuffer = http.readAll();
274 qDebug() << "pending:" << http.hasPendingRequests();
275 //if(!http.hasPendingRequests()) httpDone(error);
276 emit requestFinished(id, error);
280 void HttpGet::httpStarted(int id)
282 qDebug() << "HttpGet::httpStarted(int) =" << id;
286 QString HttpGet::errorString()
288 return http.errorString();
292 void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
294 // if there is a network error abort all scheduled requests for
295 // this download
296 response = resp.statusCode();
297 if(response != 200) {
298 qDebug() << "http response error:" << response << resp.reasonPhrase();
299 http.abort();
301 // 301 -- moved permanently
302 // 302 -- found
303 // 303 -- see other
304 // 307 -- moved temporarily
305 // in all cases, header: location has the correct address so we can follow.
306 if(response == 301 || response == 302 || response == 303 || response == 307) {
307 // start new request with new url
308 qDebug() << "http response" << response << "- following";
309 getFile(resp.value("location") + query);
314 int HttpGet::httpResponse()
316 return response;
320 void HttpGet::httpState(int state)
322 QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
323 "Reading", "Connected", "Closing"};
324 if(state <= 6)
325 qDebug() << "HttpGet::httpState() = " << s[state];
326 else qDebug() << "HttpGet::httpState() = " << state;