2007-07-25 20:21:06 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Dominik Riebeling
|
2007-08-14 22:47:01 +00:00
|
|
|
* $Id$
|
2007-07-25 20:21:06 +00:00
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
#include "httpget.h"
|
|
|
|
|
2008-03-05 21:12:24 +00:00
|
|
|
QDir HttpGet::m_globalCache;
|
|
|
|
QUrl HttpGet::m_globalProxy;
|
2007-07-25 20:21:06 +00:00
|
|
|
|
|
|
|
HttpGet::HttpGet(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
2007-08-27 17:40:35 +00:00
|
|
|
m_usecache = false;
|
2007-08-14 22:47:01 +00:00
|
|
|
qDebug() << "--> HttpGet::HttpGet()";
|
2007-08-15 13:28:15 +00:00
|
|
|
outputToBuffer = true;
|
2007-08-27 17:40:35 +00:00
|
|
|
cached = false;
|
2007-08-14 22:47:01 +00:00
|
|
|
getRequest = -1;
|
2008-03-01 13:52:02 +00:00
|
|
|
// if a request is cancelled before a reponse is available return some
|
|
|
|
// hint about this in the http response instead of nonsense.
|
|
|
|
response = -1;
|
|
|
|
|
2008-03-05 21:12:24 +00:00
|
|
|
// default to global proxy / cache if not empty.
|
|
|
|
// proxy is automatically enabled, disable it by setting an empty proxy
|
|
|
|
// cache is enabled to be in line, can get disabled with setCache(bool)
|
|
|
|
qDebug() << "setting global proxy / cache";
|
|
|
|
if(!m_globalProxy.isEmpty())
|
|
|
|
setProxy(m_globalProxy);
|
|
|
|
m_usecache = false;
|
|
|
|
m_cachedir = m_globalCache;
|
2007-07-25 20:21:06 +00:00
|
|
|
connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
|
|
|
|
connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
|
|
|
|
connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
|
|
|
|
connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
|
2007-08-14 22:47:01 +00:00
|
|
|
connect(&http, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
|
2007-08-15 13:28:15 +00:00
|
|
|
connect(&http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
|
2007-08-14 22:47:01 +00:00
|
|
|
|
|
|
|
connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-27 17:40:35 +00:00
|
|
|
void HttpGet::setCache(QDir d)
|
|
|
|
{
|
|
|
|
m_cachedir = d;
|
|
|
|
bool result = true;
|
2007-09-14 22:13:46 +00:00
|
|
|
|
|
|
|
QString p = m_cachedir.absolutePath() + "/rbutil-cache";
|
2007-08-27 17:40:35 +00:00
|
|
|
if(QFileInfo(m_cachedir.absolutePath()).isDir())
|
2007-09-14 22:13:46 +00:00
|
|
|
{
|
2007-08-27 17:40:35 +00:00
|
|
|
if(!QFileInfo(p).isDir())
|
|
|
|
result = m_cachedir.mkdir("rbutil-cache");
|
2007-09-14 22:13:46 +00:00
|
|
|
}
|
2007-08-27 17:40:35 +00:00
|
|
|
else result = false;
|
2008-03-05 21:12:24 +00:00
|
|
|
qDebug() << "HttpGet::setCache(QDir)" << d << result;
|
2007-09-14 21:55:54 +00:00
|
|
|
m_usecache = result;
|
2007-08-27 17:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setCache(bool c)
|
|
|
|
{
|
2008-03-05 21:12:24 +00:00
|
|
|
qDebug() << "setCache(bool)" << c;
|
2007-08-27 17:40:35 +00:00
|
|
|
m_usecache = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-14 22:47:01 +00:00
|
|
|
QByteArray HttpGet::readAll()
|
|
|
|
{
|
|
|
|
return dataBuffer;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QHttp::Error HttpGet::error()
|
|
|
|
{
|
|
|
|
return http.error();
|
|
|
|
}
|
|
|
|
|
2007-08-14 22:47:01 +00:00
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
void HttpGet::httpProgress(int read, int total)
|
|
|
|
{
|
|
|
|
emit dataReadProgress(read, total);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setProxy(const QUrl &proxy)
|
|
|
|
{
|
2008-03-05 21:12:24 +00:00
|
|
|
qDebug() << "HttpGet::setProxy(QUrl)" << proxy.toString();
|
|
|
|
m_proxy = proxy;
|
|
|
|
http.setProxy(m_proxy.host(), m_proxy.port(), m_proxy.userName(), m_proxy.password());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setProxy(bool enable)
|
|
|
|
{
|
|
|
|
qDebug() << "HttpGet::setProxy(bool)" << enable;
|
|
|
|
if(enable)
|
|
|
|
http.setProxy(m_proxy.host(), m_proxy.port(), m_proxy.userName(), m_proxy.password());
|
|
|
|
else
|
|
|
|
http.setProxy("", 0);
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setFile(QFile *file)
|
|
|
|
{
|
|
|
|
outputFile = file;
|
2007-08-15 13:28:15 +00:00
|
|
|
outputToBuffer = false;
|
2007-07-25 20:21:06 +00:00
|
|
|
qDebug() << "HttpGet::setFile" << outputFile->fileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::abort()
|
|
|
|
{
|
|
|
|
http.abort();
|
2007-08-15 13:28:15 +00:00
|
|
|
if(!outputToBuffer)
|
2007-08-14 22:47:01 +00:00
|
|
|
outputFile->close();
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HttpGet::getFile(const QUrl &url)
|
|
|
|
{
|
|
|
|
if (!url.isValid()) {
|
|
|
|
qDebug() << "Error: Invalid URL" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.scheme() != "http") {
|
|
|
|
qDebug() << "Error: URL must start with 'http:'" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.path().isEmpty()) {
|
|
|
|
qDebug() << "Error: URL has no path" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2007-08-14 22:47:01 +00:00
|
|
|
// if no output file was set write to buffer
|
2007-08-15 13:28:15 +00:00
|
|
|
if(!outputToBuffer) {
|
2007-08-14 22:47:01 +00:00
|
|
|
if (!outputFile->open(QIODevice::ReadWrite)) {
|
|
|
|
qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
|
|
|
|
<< " for writing: " << qPrintable(outputFile->errorString())
|
|
|
|
<< endl;
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
2007-08-27 17:40:35 +00:00
|
|
|
// put hash generation here so it can get reused later
|
|
|
|
QString hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
|
|
|
|
cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + hash;
|
|
|
|
if(m_usecache) {
|
|
|
|
// check if the file is present in cache
|
|
|
|
qDebug() << "[HTTP] cache ENABLED for" << url.toEncoded();
|
|
|
|
if(QFileInfo(cachefile).isReadable() && QFileInfo(cachefile).size() > 0) {
|
|
|
|
qDebug() << "[HTTP] cached file found!" << cachefile;
|
|
|
|
getRequest = -1;
|
|
|
|
QFile c(cachefile);
|
|
|
|
if(!outputToBuffer) {
|
|
|
|
qDebug() << outputFile->fileName();
|
|
|
|
c.open(QIODevice::ReadOnly);
|
|
|
|
outputFile->open(QIODevice::ReadWrite);
|
|
|
|
outputFile->write(c.readAll());
|
|
|
|
outputFile->close();
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c.open(QIODevice::ReadOnly);
|
|
|
|
dataBuffer = c.readAll();
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
response = 200; // fake "200 OK" HTTP response
|
|
|
|
cached = true;
|
|
|
|
httpDone(false); // we're done now. This will emit the correct signal too.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else qDebug() << "[HTTP] file not cached, downloading to" << cachefile;
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qDebug() << "[HTTP] cache DISABLED";
|
|
|
|
}
|
2008-03-05 21:12:24 +00:00
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
http.setHost(url.host(), url.port(80));
|
2007-08-15 14:15:24 +00:00
|
|
|
// construct query (if any)
|
|
|
|
QList<QPair<QString, QString> > qitems = url.queryItems();
|
|
|
|
if(url.hasQuery()) {
|
|
|
|
query = "?";
|
|
|
|
for(int i = 0; i < qitems.size(); i++)
|
|
|
|
query += qitems.at(i).first + "=" + qitems.at(i).second + "&";
|
|
|
|
qDebug() << query;
|
|
|
|
}
|
|
|
|
|
2007-08-15 13:28:15 +00:00
|
|
|
if(outputToBuffer) {
|
2007-08-27 17:40:35 +00:00
|
|
|
qDebug() << "[HTTP] downloading to buffer:" << url.toString();
|
2007-08-15 14:15:24 +00:00
|
|
|
getRequest = http.get(url.path() + query);
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-08-27 17:40:35 +00:00
|
|
|
qDebug() << "[HTTP] downloading to file:" << url.toString() << qPrintable(outputFile->fileName());
|
2007-08-15 14:15:24 +00:00
|
|
|
getRequest = http.get(url.path() + query, outputFile);
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
2007-08-27 17:40:35 +00:00
|
|
|
qDebug() << "[HTTP] request scheduled: GET" << getRequest;
|
2007-09-14 22:13:46 +00:00
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-08-14 22:47:01 +00:00
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
void HttpGet::httpDone(bool error)
|
|
|
|
{
|
|
|
|
if (error) {
|
2007-08-27 17:40:35 +00:00
|
|
|
qDebug() << "[HTTP] Error: " << qPrintable(http.errorString()) << httpResponse();
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
2007-08-15 14:15:24 +00:00
|
|
|
if(!outputToBuffer)
|
2007-08-14 22:47:01 +00:00
|
|
|
outputFile->close();
|
2007-08-27 17:40:35 +00:00
|
|
|
|
|
|
|
if(m_usecache && !cached) {
|
|
|
|
qDebug() << "[HTTP] creating cache file" << cachefile;
|
|
|
|
QFile c(cachefile);
|
|
|
|
c.open(QIODevice::ReadWrite);
|
|
|
|
if(!outputToBuffer) {
|
|
|
|
outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
|
|
|
|
c.write(outputFile->readAll());
|
|
|
|
outputFile->close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c.write(dataBuffer);
|
|
|
|
|
|
|
|
c.close();
|
|
|
|
}
|
2007-07-25 20:21:06 +00:00
|
|
|
emit done(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::httpFinished(int id, bool error)
|
|
|
|
{
|
2007-08-14 22:47:01 +00:00
|
|
|
qDebug() << "HttpGet::httpFinished(int, bool) =" << id << error;
|
|
|
|
if(id == getRequest) dataBuffer = http.readAll();
|
2007-08-15 13:28:15 +00:00
|
|
|
qDebug() << "pending:" << http.hasPendingRequests();
|
|
|
|
//if(!http.hasPendingRequests()) httpDone(error);
|
2007-07-25 20:21:06 +00:00
|
|
|
emit requestFinished(id, error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-08-14 22:47:01 +00:00
|
|
|
void HttpGet::httpStarted(int id)
|
|
|
|
{
|
|
|
|
qDebug() << "HttpGet::httpStarted(int) =" << id;
|
|
|
|
}
|
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
|
|
|
|
QString HttpGet::errorString()
|
|
|
|
{
|
|
|
|
return http.errorString();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
|
|
|
|
{
|
2007-08-14 22:47:01 +00:00
|
|
|
// if there is a network error abort all scheduled requests for
|
|
|
|
// this download
|
2007-07-25 20:21:06 +00:00
|
|
|
response = resp.statusCode();
|
2007-08-15 20:08:02 +00:00
|
|
|
if(response != 200) {
|
|
|
|
qDebug() << "http response error:" << response << resp.reasonPhrase();
|
|
|
|
http.abort();
|
|
|
|
}
|
|
|
|
// 301 -- moved permanently
|
2007-08-15 20:30:36 +00:00
|
|
|
// 302 -- found
|
2007-08-15 20:08:02 +00:00
|
|
|
// 303 -- see other
|
|
|
|
// 307 -- moved temporarily
|
|
|
|
// in all cases, header: location has the correct address so we can follow.
|
2007-08-15 20:30:36 +00:00
|
|
|
if(response == 301 || response == 302 || response == 303 || response == 307) {
|
2007-08-15 20:08:02 +00:00
|
|
|
// start new request with new url
|
|
|
|
qDebug() << "http response" << response << "- following";
|
2007-08-15 20:30:36 +00:00
|
|
|
getFile(resp.value("location") + query);
|
2007-08-15 20:08:02 +00:00
|
|
|
}
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int HttpGet::httpResponse()
|
|
|
|
{
|
|
|
|
return response;
|
|
|
|
}
|
2007-08-14 22:47:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::httpState(int state)
|
|
|
|
{
|
|
|
|
QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
|
|
|
|
"Reading", "Connected", "Closing"};
|
|
|
|
if(state <= 6)
|
|
|
|
qDebug() << "HttpGet::httpState() = " << s[state];
|
|
|
|
else qDebug() << "HttpGet::httpState() = " << state;
|
|
|
|
}
|
|
|
|
|