Fix some quotation marks. Thanks to Alexander Levin for pointing it out.
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blobc1e541391b3ffc06d2bb29d4a8bcdde6217d2c8d
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;
27 QUrl HttpGet::m_globalProxy;
29 HttpGet::HttpGet(QObject *parent)
30 : QObject(parent)
32 m_usecache = false;
33 qDebug() << "--> HttpGet::HttpGet()";
34 outputToBuffer = true;
35 cached = false;
36 getRequest = -1;
37 // if a request is cancelled before a reponse is available return some
38 // hint about this in the http response instead of nonsense.
39 response = -1;
41 // default to global proxy / cache if not empty.
42 // proxy is automatically enabled, disable it by setting an empty proxy
43 // cache is enabled to be in line, can get disabled with setCache(bool)
44 qDebug() << "setting global proxy / cache";
45 if(!m_globalProxy.isEmpty())
46 setProxy(m_globalProxy);
47 m_usecache = false;
48 m_cachedir = m_globalCache;
49 connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
50 connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
51 connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
52 connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
53 connect(&http, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
54 connect(&http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
56 connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
61 void HttpGet::setCache(QDir d)
63 m_cachedir = d;
64 bool result = true;
66 QString p = m_cachedir.absolutePath() + "/rbutil-cache";
67 if(QFileInfo(m_cachedir.absolutePath()).isDir())
69 if(!QFileInfo(p).isDir())
70 result = m_cachedir.mkdir("rbutil-cache");
72 else result = false;
73 qDebug() << "HttpGet::setCache(QDir)" << d.absolutePath() << result;
74 m_usecache = result;
78 void HttpGet::setCache(bool c)
80 qDebug() << "setCache(bool)" << c;
81 m_usecache = c;
85 QByteArray HttpGet::readAll()
87 return dataBuffer;
91 QHttp::Error HttpGet::error()
93 return http.error();
97 void HttpGet::httpProgress(int read, int total)
99 emit dataReadProgress(read, total);
103 void HttpGet::setProxy(const QUrl &proxy)
105 qDebug() << "HttpGet::setProxy(QUrl)" << proxy.toString();
106 m_proxy = proxy;
107 http.setProxy(m_proxy.host(), m_proxy.port(), m_proxy.userName(), m_proxy.password());
111 void HttpGet::setProxy(bool enable)
113 qDebug() << "HttpGet::setProxy(bool)" << enable;
114 if(enable)
115 http.setProxy(m_proxy.host(), m_proxy.port(), m_proxy.userName(), m_proxy.password());
116 else
117 http.setProxy("", 0);
121 void HttpGet::setFile(QFile *file)
123 outputFile = file;
124 outputToBuffer = false;
125 qDebug() << "HttpGet::setFile" << outputFile->fileName();
129 void HttpGet::abort()
131 http.abort();
132 if(!outputToBuffer)
133 outputFile->close();
137 bool HttpGet::getFile(const QUrl &url)
139 if (!url.isValid()) {
140 qDebug() << "Error: Invalid URL" << endl;
141 return false;
144 if (url.scheme() != "http") {
145 qDebug() << "Error: URL must start with 'http:'" << endl;
146 return false;
149 if (url.path().isEmpty()) {
150 qDebug() << "Error: URL has no path" << endl;
151 return false;
153 // if no output file was set write to buffer
154 if(!outputToBuffer) {
155 if (!outputFile->open(QIODevice::ReadWrite)) {
156 qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
157 << " for writing: " << qPrintable(outputFile->errorString())
158 << endl;
159 return false;
162 // put hash generation here so it can get reused later
163 QString hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
164 cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + hash;
165 if(m_usecache) {
166 // check if the file is present in cache
167 qDebug() << "[HTTP] cache ENABLED for" << url.toEncoded();
168 if(QFileInfo(cachefile).isReadable() && QFileInfo(cachefile).size() > 0) {
169 qDebug() << "[HTTP] cached file found!" << cachefile;
170 getRequest = -1;
171 QFile c(cachefile);
172 if(!outputToBuffer) {
173 qDebug() << outputFile->fileName();
174 c.open(QIODevice::ReadOnly);
175 outputFile->open(QIODevice::ReadWrite);
176 outputFile->write(c.readAll());
177 outputFile->close();
178 c.close();
180 else {
181 c.open(QIODevice::ReadOnly);
182 dataBuffer = c.readAll();
183 c.close();
185 response = 200; // fake "200 OK" HTTP response
186 cached = true;
187 httpDone(false); // we're done now. This will emit the correct signal too.
188 return true;
190 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile;
193 else {
194 qDebug() << "[HTTP] cache DISABLED";
197 http.setHost(url.host(), url.port(80));
198 // construct query (if any)
199 QList<QPair<QString, QString> > qitems = url.queryItems();
200 if(url.hasQuery()) {
201 query = "?";
202 for(int i = 0; i < qitems.size(); i++)
203 query += qitems.at(i).first + "=" + qitems.at(i).second + "&";
204 qDebug() << query;
207 if(outputToBuffer) {
208 qDebug() << "[HTTP] downloading to buffer:" << url.toString();
209 getRequest = http.get(url.path() + query);
211 else {
212 qDebug() << "[HTTP] downloading to file:" << url.toString() << qPrintable(outputFile->fileName());
213 getRequest = http.get(url.path() + query, outputFile);
215 qDebug() << "[HTTP] request scheduled: GET" << getRequest;
217 return true;
221 void HttpGet::httpDone(bool error)
223 if (error) {
224 qDebug() << "[HTTP] Error: " << qPrintable(http.errorString()) << httpResponse();
226 if(!outputToBuffer)
227 outputFile->close();
229 if(m_usecache && !cached) {
230 qDebug() << "[HTTP] creating cache file" << cachefile;
231 QFile c(cachefile);
232 c.open(QIODevice::ReadWrite);
233 if(!outputToBuffer) {
234 outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
235 c.write(outputFile->readAll());
236 outputFile->close();
238 else
239 c.write(dataBuffer);
241 c.close();
243 emit done(error);
247 void HttpGet::httpFinished(int id, bool error)
249 qDebug() << "HttpGet::httpFinished(int, bool) =" << id << error;
250 if(id == getRequest) dataBuffer = http.readAll();
251 qDebug() << "pending:" << http.hasPendingRequests();
252 //if(!http.hasPendingRequests()) httpDone(error);
253 emit requestFinished(id, error);
257 void HttpGet::httpStarted(int id)
259 qDebug() << "HttpGet::httpStarted(int) =" << id;
263 QString HttpGet::errorString()
265 return http.errorString();
269 void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
271 // if there is a network error abort all scheduled requests for
272 // this download
273 response = resp.statusCode();
274 if(response != 200) {
275 qDebug() << "http response error:" << response << resp.reasonPhrase();
276 http.abort();
278 // 301 -- moved permanently
279 // 302 -- found
280 // 303 -- see other
281 // 307 -- moved temporarily
282 // in all cases, header: location has the correct address so we can follow.
283 if(response == 301 || response == 302 || response == 303 || response == 307) {
284 // start new request with new url
285 qDebug() << "http response" << response << "- following";
286 getFile(resp.value("location") + query);
291 int HttpGet::httpResponse()
293 return response;
297 void HttpGet::httpState(int state)
299 QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
300 "Reading", "Connected", "Closing"};
301 if(state <= 6)
302 qDebug() << "HttpGet::httpState() = " << s[state];
303 else qDebug() << "HttpGet::httpState() = " << state;