httpget class: if a request is cancelled before a response is available give a hint...
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blobacd8940b772fd4ccb80719b97ae464848b74b243
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"
27 HttpGet::HttpGet(QObject *parent)
28 : QObject(parent)
30 m_usecache = false;
31 qDebug() << "--> HttpGet::HttpGet()";
32 outputToBuffer = true;
33 cached = false;
34 getRequest = -1;
35 // if a request is cancelled before a reponse is available return some
36 // hint about this in the http response instead of nonsense.
37 response = -1;
39 connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
40 connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
41 connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
42 connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
43 connect(&http, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
44 connect(&http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
46 connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
51 void HttpGet::setCache(QDir d)
53 m_cachedir = d;
54 bool result = true;
56 QString p = m_cachedir.absolutePath() + "/rbutil-cache";
57 if(QFileInfo(m_cachedir.absolutePath()).isDir())
59 if(!QFileInfo(p).isDir())
60 result = m_cachedir.mkdir("rbutil-cache");
62 else result = false;
63 qDebug() << "HttpGet::setCache(QDir)" << result;
64 m_usecache = result;
68 void HttpGet::setCache(bool c)
70 m_usecache = c;
74 QByteArray HttpGet::readAll()
76 return dataBuffer;
80 QHttp::Error HttpGet::error()
82 return http.error();
86 void HttpGet::httpProgress(int read, int total)
88 emit dataReadProgress(read, total);
92 void HttpGet::setProxy(const QUrl &proxy)
94 qDebug() << "HttpGet::setProxy" << proxy.toString();
95 http.setProxy(proxy.host(), proxy.port(), proxy.userName(), proxy.password());
99 void HttpGet::setFile(QFile *file)
101 outputFile = file;
102 outputToBuffer = false;
103 qDebug() << "HttpGet::setFile" << outputFile->fileName();
107 void HttpGet::abort()
109 http.abort();
110 if(!outputToBuffer)
111 outputFile->close();
115 bool HttpGet::getFile(const QUrl &url)
117 if (!url.isValid()) {
118 qDebug() << "Error: Invalid URL" << endl;
119 return false;
122 if (url.scheme() != "http") {
123 qDebug() << "Error: URL must start with 'http:'" << endl;
124 return false;
127 if (url.path().isEmpty()) {
128 qDebug() << "Error: URL has no path" << endl;
129 return false;
131 // if no output file was set write to buffer
132 if(!outputToBuffer) {
133 if (!outputFile->open(QIODevice::ReadWrite)) {
134 qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
135 << " for writing: " << qPrintable(outputFile->errorString())
136 << endl;
137 return false;
140 // put hash generation here so it can get reused later
141 QString hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
142 cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + hash;
143 if(m_usecache) {
144 // check if the file is present in cache
145 qDebug() << "[HTTP] cache ENABLED for" << url.toEncoded();
146 if(QFileInfo(cachefile).isReadable() && QFileInfo(cachefile).size() > 0) {
147 qDebug() << "[HTTP] cached file found!" << cachefile;
148 getRequest = -1;
149 QFile c(cachefile);
150 if(!outputToBuffer) {
151 qDebug() << outputFile->fileName();
152 c.open(QIODevice::ReadOnly);
153 outputFile->open(QIODevice::ReadWrite);
154 outputFile->write(c.readAll());
155 outputFile->close();
156 c.close();
158 else {
159 c.open(QIODevice::ReadOnly);
160 dataBuffer = c.readAll();
161 c.close();
163 response = 200; // fake "200 OK" HTTP response
164 cached = true;
165 httpDone(false); // we're done now. This will emit the correct signal too.
166 return true;
168 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile;
171 else {
172 qDebug() << "[HTTP] cache DISABLED";
174 http.setHost(url.host(), url.port(80));
175 // construct query (if any)
176 QList<QPair<QString, QString> > qitems = url.queryItems();
177 if(url.hasQuery()) {
178 query = "?";
179 for(int i = 0; i < qitems.size(); i++)
180 query += qitems.at(i).first + "=" + qitems.at(i).second + "&";
181 qDebug() << query;
184 if(outputToBuffer) {
185 qDebug() << "[HTTP] downloading to buffer:" << url.toString();
186 getRequest = http.get(url.path() + query);
188 else {
189 qDebug() << "[HTTP] downloading to file:" << url.toString() << qPrintable(outputFile->fileName());
190 getRequest = http.get(url.path() + query, outputFile);
192 qDebug() << "[HTTP] request scheduled: GET" << getRequest;
194 return true;
198 void HttpGet::httpDone(bool error)
200 if (error) {
201 qDebug() << "[HTTP] Error: " << qPrintable(http.errorString()) << httpResponse();
203 if(!outputToBuffer)
204 outputFile->close();
206 if(m_usecache && !cached) {
207 qDebug() << "[HTTP] creating cache file" << cachefile;
208 QFile c(cachefile);
209 c.open(QIODevice::ReadWrite);
210 if(!outputToBuffer) {
211 outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
212 c.write(outputFile->readAll());
213 outputFile->close();
215 else
216 c.write(dataBuffer);
218 c.close();
220 emit done(error);
224 void HttpGet::httpFinished(int id, bool error)
226 qDebug() << "HttpGet::httpFinished(int, bool) =" << id << error;
227 if(id == getRequest) dataBuffer = http.readAll();
228 qDebug() << "pending:" << http.hasPendingRequests();
229 //if(!http.hasPendingRequests()) httpDone(error);
230 emit requestFinished(id, error);
234 void HttpGet::httpStarted(int id)
236 qDebug() << "HttpGet::httpStarted(int) =" << id;
240 QString HttpGet::errorString()
242 return http.errorString();
246 void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
248 // if there is a network error abort all scheduled requests for
249 // this download
250 response = resp.statusCode();
251 if(response != 200) {
252 qDebug() << "http response error:" << response << resp.reasonPhrase();
253 http.abort();
255 // 301 -- moved permanently
256 // 302 -- found
257 // 303 -- see other
258 // 307 -- moved temporarily
259 // in all cases, header: location has the correct address so we can follow.
260 if(response == 301 || response == 302 || response == 303 || response == 307) {
261 // start new request with new url
262 qDebug() << "http response" << response << "- following";
263 getFile(resp.value("location") + query);
268 int HttpGet::httpResponse()
270 return response;
274 void HttpGet::httpState(int state)
276 QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
277 "Reading", "Connected", "Closing"};
278 if(state <= 6)
279 qDebug() << "HttpGet::httpState() = " << s[state];
280 else qDebug() << "HttpGet::httpState() = " << state;