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"
|
|
|
|
|
|
|
|
|
|
|
|
HttpGet::HttpGet(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
2007-08-14 22:47:01 +00:00
|
|
|
qDebug() << "--> HttpGet::HttpGet()";
|
2007-07-25 20:21:06 +00:00
|
|
|
outputFile = new QFile(this);
|
2007-08-14 22:47:01 +00:00
|
|
|
outputFile->setFileName("");
|
|
|
|
getRequest = -1;
|
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)));
|
|
|
|
//connect(&http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
|
|
|
|
|
|
|
|
connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
qDebug() << "HttpGet::setProxy" << proxy.toString();
|
|
|
|
http.setProxy(proxy.host(), proxy.port(), proxy.userName(), proxy.password());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setFile(QFile *file)
|
|
|
|
{
|
|
|
|
outputFile = file;
|
|
|
|
qDebug() << "HttpGet::setFile" << outputFile->fileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::abort()
|
|
|
|
{
|
|
|
|
http.abort();
|
2007-08-14 22:47:01 +00:00
|
|
|
if(!outputFile->fileName().isEmpty());
|
|
|
|
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
|
|
|
|
if(!outputFile->fileName().isEmpty()) {
|
|
|
|
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-14 22:47:01 +00:00
|
|
|
qDebug() << "starting download to " << qPrintable(outputFile->fileName());
|
2007-07-25 20:21:06 +00:00
|
|
|
http.setHost(url.host(), url.port(80));
|
2007-08-14 22:47:01 +00:00
|
|
|
if(outputFile->fileName().isEmpty()) {
|
|
|
|
qDebug() << "downloading to buffer";
|
|
|
|
getRequest = http.get(QString(url.toEncoded()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qDebug() << "downloading to file";
|
|
|
|
getRequest = http.get(QString(url.toEncoded()), outputFile);
|
|
|
|
}
|
|
|
|
qDebug() << "request scheduled: GET" << getRequest;
|
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
http.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-08-14 22:47:01 +00:00
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
void HttpGet::httpDone(bool error)
|
|
|
|
{
|
2007-08-14 22:47:01 +00:00
|
|
|
qDebug() << "bytesAvailable =" << http.bytesAvailable();
|
2007-07-25 20:21:06 +00:00
|
|
|
if (error) {
|
|
|
|
qDebug() << "Error: " << qPrintable(http.errorString()) << endl;
|
|
|
|
} else {
|
|
|
|
qDebug() << "File downloaded as " << qPrintable(outputFile->fileName())
|
|
|
|
<< endl;
|
|
|
|
}
|
2007-08-14 22:47:01 +00:00
|
|
|
if(!outputFile->fileName().isEmpty())
|
|
|
|
outputFile->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-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();
|
|
|
|
if(response != 200) http.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|