2007-07-25 20:21:06 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Dominik Riebeling
|
|
|
|
*
|
|
|
|
* 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-04-13 18:43:51 +00:00
|
|
|
QDir HttpGet::m_globalCache; //< global cach path value for new objects
|
|
|
|
QUrl HttpGet::m_globalProxy; //< global proxy value for new objects
|
2008-05-17 19:36:54 +00:00
|
|
|
bool HttpGet::m_globalDumbCache = false; //< globally set cache "dumb" mode
|
2008-08-30 20:51:50 +00:00
|
|
|
QString HttpGet::m_globalUserAgent; //< globally set user agent for requests
|
2007-07-25 20:21:06 +00:00
|
|
|
|
|
|
|
HttpGet::HttpGet(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
2007-08-15 13:28:15 +00:00
|
|
|
outputToBuffer = true;
|
2008-05-13 19:38:17 +00:00
|
|
|
m_cached = false;
|
2008-05-17 19:36:54 +00:00
|
|
|
m_dumbCache = m_globalDumbCache;
|
2007-08-14 22:47:01 +00:00
|
|
|
getRequest = -1;
|
2008-06-09 20:43:59 +00:00
|
|
|
headRequest = -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.
|
2008-05-13 19:38:17 +00:00
|
|
|
m_response = -1;
|
2009-05-19 22:46:57 +00:00
|
|
|
m_useproxy = false;
|
2008-03-01 13:52:02 +00:00
|
|
|
|
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)
|
|
|
|
if(!m_globalProxy.isEmpty())
|
|
|
|
setProxy(m_globalProxy);
|
|
|
|
m_usecache = false;
|
|
|
|
m_cachedir = m_globalCache;
|
2008-06-02 19:29:54 +00:00
|
|
|
|
|
|
|
m_serverTimestamp = QDateTime();
|
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
|
2009-03-22 08:50:55 +00:00
|
|
|
connect(&http, SIGNAL(dataReadProgress(int, int)),
|
|
|
|
this, SIGNAL(dataReadProgress(int, int)));
|
|
|
|
connect(&http, SIGNAL(requestFinished(int, bool)),
|
|
|
|
this, SLOT(httpFinished(int, bool)));
|
|
|
|
connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)),
|
|
|
|
this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
|
2009-03-21 23:18:57 +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
|
|
|
|
2009-03-22 08:50:55 +00:00
|
|
|
connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)),
|
|
|
|
this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
|
|
|
|
connect(this, SIGNAL(headerFinished()), this, SLOT(getFileFinish()));
|
2007-08-14 22:47:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-13 18:43:51 +00:00
|
|
|
//! @brief set cache path
|
|
|
|
// @param d new directory to use as cache path
|
2009-03-08 09:06:05 +00:00
|
|
|
void HttpGet::setCache(const QDir& d)
|
2007-08-27 17:40:35 +00:00
|
|
|
{
|
|
|
|
m_cachedir = d;
|
2008-04-06 17:20:13 +00:00
|
|
|
bool result;
|
|
|
|
result = initializeCache(d);
|
2007-09-14 21:55:54 +00:00
|
|
|
m_usecache = result;
|
2007-08-27 17:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-13 18:43:51 +00:00
|
|
|
/** @brief enable / disable cache useage
|
|
|
|
* @param c set cache usage
|
|
|
|
*/
|
2007-08-27 17:40:35 +00:00
|
|
|
void HttpGet::setCache(bool c)
|
|
|
|
{
|
|
|
|
m_usecache = c;
|
2008-04-06 17:20:13 +00:00
|
|
|
// make sure cache is initialized
|
|
|
|
if(c)
|
|
|
|
m_usecache = initializeCache(m_cachedir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HttpGet::initializeCache(const QDir& d)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
QString p = d.absolutePath() + "/rbutil-cache";
|
|
|
|
if(QFileInfo(d.absolutePath()).isDir())
|
|
|
|
{
|
|
|
|
if(!QFileInfo(p).isDir())
|
|
|
|
result = d.mkdir("rbutil-cache");
|
2008-04-06 18:00:32 +00:00
|
|
|
else
|
|
|
|
result = true;
|
2008-04-06 17:20:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result = false;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
2007-08-27 17:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-13 19:38:17 +00:00
|
|
|
/** @brief read all downloaded data into a buffer
|
|
|
|
* @return data
|
|
|
|
*/
|
2007-08-14 22:47:01 +00:00
|
|
|
QByteArray HttpGet::readAll()
|
|
|
|
{
|
|
|
|
return dataBuffer;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-13 18:43:51 +00:00
|
|
|
/** @brief get http error
|
|
|
|
* @return http error
|
|
|
|
*/
|
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::setProxy(const QUrl &proxy)
|
|
|
|
{
|
2008-03-05 21:12:24 +00:00
|
|
|
m_proxy = proxy;
|
2009-05-19 22:46:57 +00:00
|
|
|
m_useproxy = true;
|
|
|
|
http.setProxy(m_proxy.host(), m_proxy.port(),
|
|
|
|
m_proxy.userName(), m_proxy.password());
|
2008-03-05 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setProxy(bool enable)
|
|
|
|
{
|
2009-05-19 22:46:57 +00:00
|
|
|
if(enable) {
|
|
|
|
m_useproxy = true;
|
|
|
|
http.setProxy(m_proxy.host(), m_proxy.port(),
|
|
|
|
m_proxy.userName(), m_proxy.password());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_useproxy = false;
|
2008-03-05 21:12:24 +00:00
|
|
|
http.setProxy("", 0);
|
2009-05-19 22:46:57 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::abort()
|
|
|
|
{
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] Aborting requests, pending:" << http.hasPendingRequests();
|
2007-07-25 20:21:06 +00:00
|
|
|
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()) {
|
2008-05-13 19:38:17 +00:00
|
|
|
qDebug() << "[HTTP] Error: Invalid URL" << endl;
|
2007-07-25 20:21:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.scheme() != "http") {
|
2008-05-13 19:38:17 +00:00
|
|
|
qDebug() << "[HTTP] Error: URL must start with 'http:'" << endl;
|
2007-07-25 20:21:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.path().isEmpty()) {
|
2008-05-13 19:38:17 +00:00
|
|
|
qDebug() << "[HTTP] Error: URL has no path" << endl;
|
2007-07-25 20:21:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-06-02 21:46:23 +00:00
|
|
|
m_serverTimestamp = QDateTime();
|
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)) {
|
2008-05-13 19:38:17 +00:00
|
|
|
qDebug() << "[HTTP] Error: Cannot open " << qPrintable(outputFile->fileName())
|
2009-03-21 23:18:57 +00:00
|
|
|
<< " for writing: " << qPrintable(outputFile->errorString());
|
2007-08-14 22:47:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
2009-03-21 18:01:59 +00:00
|
|
|
else {
|
|
|
|
// output to buffer. Make sure buffer is empty so no old data gets
|
|
|
|
// returned in case the object is reused.
|
|
|
|
dataBuffer.clear();
|
|
|
|
}
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] GET URI" << url.toEncoded();
|
2008-05-13 19:38:17 +00:00
|
|
|
// create request
|
|
|
|
http.setHost(url.host(), url.port(80));
|
|
|
|
// construct query (if any)
|
|
|
|
QList<QPair<QString, QString> > qitems = url.queryItems();
|
|
|
|
if(url.hasQuery()) {
|
|
|
|
m_query = "?";
|
|
|
|
for(int i = 0; i < qitems.size(); i++)
|
|
|
|
m_query += QUrl::toPercentEncoding(qitems.at(i).first, "/") + "="
|
|
|
|
+ QUrl::toPercentEncoding(qitems.at(i).second, "/") + "&";
|
|
|
|
}
|
|
|
|
|
|
|
|
// create hash used for caching
|
|
|
|
m_hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
|
2009-05-19 22:46:57 +00:00
|
|
|
// RFC2616: the absoluteURI form must get used when the request is being
|
|
|
|
// sent to a proxy.
|
|
|
|
m_path.clear();
|
|
|
|
if(m_useproxy)
|
|
|
|
m_path = url.scheme() + "://" + url.host();
|
|
|
|
m_path += QString(QUrl::toPercentEncoding(url.path(), "/"));
|
2008-05-13 19:38:17 +00:00
|
|
|
|
2008-08-30 20:51:50 +00:00
|
|
|
// construct request header
|
|
|
|
m_header.setValue("Host", url.host());
|
|
|
|
m_header.setValue("User-Agent", m_globalUserAgent);
|
|
|
|
m_header.setValue("Connection", "Keep-Alive");
|
|
|
|
|
2008-05-17 19:36:54 +00:00
|
|
|
if(m_dumbCache || !m_usecache) {
|
2008-05-13 19:38:17 +00:00
|
|
|
getFileFinish();
|
|
|
|
}
|
|
|
|
else {
|
2008-08-30 20:51:50 +00:00
|
|
|
// schedule HTTP header request
|
|
|
|
m_header.setRequest("HEAD", m_path + m_query);
|
|
|
|
headRequest = http.request(m_header);
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] HEAD scheduled: " << headRequest;
|
2008-05-13 19:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::getFileFinish()
|
|
|
|
{
|
|
|
|
m_cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + m_hash;
|
2009-10-14 20:21:23 +00:00
|
|
|
QString indexFile = m_cachedir.absolutePath() + "/rbutil-cache/cache.txt";
|
2007-08-27 17:40:35 +00:00
|
|
|
if(m_usecache) {
|
|
|
|
// check if the file is present in cache
|
2008-05-13 19:38:17 +00:00
|
|
|
QFileInfo cachefile = QFileInfo(m_cachefile);
|
|
|
|
if(cachefile.isReadable()
|
|
|
|
&& cachefile.size() > 0
|
|
|
|
&& cachefile.lastModified() > m_serverTimestamp) {
|
|
|
|
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] Cache: up-to-date file found:" << m_cachefile;
|
2008-05-13 19:38:17 +00:00
|
|
|
|
2007-08-27 17:40:35 +00:00
|
|
|
getRequest = -1;
|
2008-05-13 19:38:17 +00:00
|
|
|
QFile c(m_cachefile);
|
2007-08-27 17:40:35 +00:00
|
|
|
if(!outputToBuffer) {
|
2009-05-19 22:46:57 +00:00
|
|
|
qDebug() << "[HTTP] Cache: copying file to output"
|
|
|
|
<< outputFile->fileName();
|
2007-08-27 17:40:35 +00:00
|
|
|
c.open(QIODevice::ReadOnly);
|
|
|
|
outputFile->open(QIODevice::ReadWrite);
|
|
|
|
outputFile->write(c.readAll());
|
|
|
|
outputFile->close();
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
else {
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] Cache: reading file into buffer";
|
2007-08-27 17:40:35 +00:00
|
|
|
c.open(QIODevice::ReadOnly);
|
|
|
|
dataBuffer = c.readAll();
|
|
|
|
c.close();
|
|
|
|
}
|
2008-05-13 19:38:17 +00:00
|
|
|
m_response = 200; // fake "200 OK" HTTP response
|
|
|
|
m_cached = true;
|
2009-03-21 21:04:24 +00:00
|
|
|
httpDone(false); // we're done now. Handle http "done" signal.
|
2008-05-13 19:38:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// unlink old cache file
|
2009-04-17 22:09:39 +00:00
|
|
|
if(cachefile.isReadable()) {
|
2008-05-13 19:38:17 +00:00
|
|
|
QFile(m_cachefile).remove();
|
2009-04-17 22:09:39 +00:00
|
|
|
qDebug() << "[HTTP] Cache: outdated, timestamp:"
|
|
|
|
<< cachefile.lastModified();
|
|
|
|
}
|
|
|
|
qDebug() << "[HTTP] Cache: caching as" << m_cachefile;
|
2009-10-14 20:21:23 +00:00
|
|
|
// update cache index file
|
|
|
|
QFile idxFile(indexFile);
|
|
|
|
idxFile.open(QIODevice::ReadOnly);
|
|
|
|
QByteArray currLine;
|
|
|
|
do {
|
|
|
|
QByteArray currLine = idxFile.readLine(1000);
|
|
|
|
if(currLine.startsWith(m_hash.toUtf8()))
|
|
|
|
break;
|
|
|
|
} while(!currLine.isEmpty());
|
|
|
|
idxFile.close();
|
|
|
|
if(currLine.isEmpty()) {
|
|
|
|
idxFile.open(QIODevice::Append);
|
|
|
|
QString outline = m_hash + "\t" + m_header.value("Host") + "\t"
|
|
|
|
+ m_path + "\t" + m_query + "\n";
|
|
|
|
idxFile.write(outline.toUtf8());
|
|
|
|
idxFile.close();
|
|
|
|
}
|
2007-08-27 17:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qDebug() << "[HTTP] cache DISABLED";
|
|
|
|
}
|
2008-08-30 20:51:50 +00:00
|
|
|
// schedule GET request
|
2009-05-19 22:46:57 +00:00
|
|
|
|
2008-08-30 20:51:50 +00:00
|
|
|
m_header.setRequest("GET", m_path + m_query);
|
2007-08-15 13:28:15 +00:00
|
|
|
if(outputToBuffer) {
|
2008-05-13 19:38:17 +00:00
|
|
|
qDebug() << "[HTTP] downloading to buffer.";
|
2008-08-30 20:51:50 +00:00
|
|
|
getRequest = http.request(m_header);
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-05-13 19:38:17 +00:00
|
|
|
qDebug() << "[HTTP] downloading to file:"
|
|
|
|
<< qPrintable(outputFile->fileName());
|
2008-08-30 20:51:50 +00:00
|
|
|
getRequest = http.request(m_header, 0, outputFile);
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] GET scheduled: " << getRequest;
|
2007-09-14 22:13:46 +00:00
|
|
|
|
2008-05-13 19:38:17 +00:00
|
|
|
return;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 22:47:01 +00:00
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
void HttpGet::httpDone(bool error)
|
|
|
|
{
|
|
|
|
if (error) {
|
2009-03-21 23:18:57 +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
|
|
|
|
2008-06-07 17:17:19 +00:00
|
|
|
if(m_usecache && !m_cached && !error) {
|
2008-05-13 19:38:17 +00:00
|
|
|
qDebug() << "[HTTP] creating cache file" << m_cachefile;
|
|
|
|
QFile c(m_cachefile);
|
2007-08-27 17:40:35 +00:00
|
|
|
c.open(QIODevice::ReadWrite);
|
|
|
|
if(!outputToBuffer) {
|
|
|
|
outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
|
|
|
|
c.write(outputFile->readAll());
|
|
|
|
outputFile->close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
c.write(dataBuffer);
|
|
|
|
|
|
|
|
c.close();
|
|
|
|
}
|
2008-05-13 19:38:17 +00:00
|
|
|
// take care of concurring requests. If there is still one running,
|
|
|
|
// don't emit done(). That request will call this slot again.
|
|
|
|
if(http.currentId() == 0 && !http.hasPendingRequests())
|
|
|
|
emit done(error);
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::httpFinished(int id, bool error)
|
|
|
|
{
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] Request finished:" << id << "Error:" << error
|
|
|
|
<< "pending requests:" << http.hasPendingRequests();
|
2008-05-13 19:38:17 +00:00
|
|
|
if(id == getRequest) {
|
|
|
|
dataBuffer = http.readAll();
|
|
|
|
emit requestFinished(id, error);
|
|
|
|
}
|
2007-07-25 20:21:06 +00:00
|
|
|
|
2008-05-13 19:38:17 +00:00
|
|
|
if(id == headRequest) {
|
|
|
|
QHttpResponseHeader h = http.lastResponse();
|
|
|
|
|
|
|
|
QString date = h.value("Last-Modified").simplified();
|
|
|
|
if(date.isEmpty()) {
|
|
|
|
m_serverTimestamp = QDateTime(); // no value = invalid
|
|
|
|
emit headerFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// to successfully parse the date strip weekday and timezone
|
|
|
|
date.remove(0, date.indexOf(" ") + 1);
|
|
|
|
if(date.endsWith("GMT"))
|
|
|
|
date.truncate(date.indexOf(" GMT"));
|
|
|
|
// distinguish input formats (see RFC1945)
|
|
|
|
// RFC 850
|
|
|
|
if(date.contains("-"))
|
2010-01-20 22:48:11 +00:00
|
|
|
m_serverTimestamp = QLocale::c().toDateTime(date, "dd-MMM-yy hh:mm:ss");
|
2008-05-13 19:38:17 +00:00
|
|
|
// asctime format
|
|
|
|
else if(date.at(0).isLetter())
|
2010-01-20 22:48:11 +00:00
|
|
|
m_serverTimestamp = QLocale::c().toDateTime(date, "MMM d hh:mm:ss yyyy");
|
2008-05-13 19:38:17 +00:00
|
|
|
// RFC 822
|
|
|
|
else
|
2009-12-15 21:58:07 +00:00
|
|
|
m_serverTimestamp = QLocale::c().toDateTime(date, "dd MMM yyyy hh:mm:ss");
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] HEAD finished, server date:" << date << ", parsed:" << m_serverTimestamp;
|
2008-05-13 19:38:17 +00:00
|
|
|
emit headerFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(id == getRequest)
|
|
|
|
emit requestFinished(id, error);
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 22:47:01 +00:00
|
|
|
void HttpGet::httpStarted(int id)
|
|
|
|
{
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] Request started: " << id << "Header req:"
|
|
|
|
<< headRequest << "Get req:" << getRequest;
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
2008-05-13 19:38:17 +00:00
|
|
|
m_response = resp.statusCode();
|
2009-09-19 21:00:07 +00:00
|
|
|
|
2007-08-15 20:08:02 +00:00
|
|
|
// 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.
|
2008-05-13 19:38:17 +00:00
|
|
|
if(m_response == 301 || m_response == 302 || m_response == 303 || m_response == 307) {
|
2009-09-19 21:00:07 +00:00
|
|
|
//abort without sending any signals
|
|
|
|
http.blockSignals(true);
|
|
|
|
http.abort();
|
|
|
|
http.blockSignals(false);
|
2007-08-15 20:08:02 +00:00
|
|
|
// start new request with new url
|
2008-05-13 19:38:17 +00:00
|
|
|
qDebug() << "[HTTP] response =" << m_response << "- following";
|
|
|
|
getFile(resp.value("location") + m_query);
|
2007-08-15 20:08:02 +00:00
|
|
|
}
|
2009-03-21 23:18:57 +00:00
|
|
|
else if(m_response != 200) {
|
|
|
|
// all other errors are fatal.
|
2009-09-19 21:00:07 +00:00
|
|
|
http.abort();
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] Response error:" << m_response << resp.reasonPhrase();
|
|
|
|
}
|
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int HttpGet::httpResponse()
|
|
|
|
{
|
2008-05-13 19:38:17 +00:00
|
|
|
return m_response;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
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)
|
2009-03-21 23:18:57 +00:00
|
|
|
qDebug() << "[HTTP] State:" << s[state];
|
|
|
|
else qDebug() << "[HTTP] State:" << state;
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
|
|
|
|