2007-07-25 20:21:06 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
2013-01-25 21:57:19 +00:00
|
|
|
* Copyright (C) 2013 by Dominik Riebeling
|
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 <QtNetwork>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
#include "httpget.h"
|
|
|
|
|
2008-08-30 20:51:50 +00:00
|
|
|
QString HttpGet::m_globalUserAgent; //< globally set user agent for requests
|
2013-01-25 21:57:19 +00:00
|
|
|
QDir HttpGet::m_globalCache; //< global cach path value for new objects
|
|
|
|
QNetworkProxy HttpGet::m_globalProxy;
|
2007-07-25 20:21:06 +00:00
|
|
|
|
|
|
|
HttpGet::HttpGet(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
m_mgr = new QNetworkAccessManager(this);
|
|
|
|
m_cache = NULL;
|
2008-03-05 21:12:24 +00:00
|
|
|
m_cachedir = m_globalCache;
|
2013-01-25 21:57:19 +00:00
|
|
|
setCache(true);
|
|
|
|
connect(m_mgr, SIGNAL(finished(QNetworkReply*)),
|
|
|
|
this, SLOT(requestFinished(QNetworkReply*)));
|
|
|
|
m_outputFile = NULL;
|
|
|
|
m_lastServerTimestamp = QDateTime();
|
|
|
|
m_proxy = QNetworkProxy::NoProxy;
|
|
|
|
m_reply = NULL;
|
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
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
if(m_cache && m_cachedir == d.absolutePath())
|
|
|
|
return;
|
|
|
|
m_cachedir = d.absolutePath();
|
|
|
|
setCache(true);
|
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)
|
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
// don't change cache if it's already (un)set.
|
|
|
|
if(c && m_cache) return;
|
|
|
|
if(!c && !m_cache) return;
|
|
|
|
// don't delete the old cache directly, it might still be in use. Just
|
|
|
|
// instruct it to delete itself later.
|
|
|
|
if(m_cache) m_cache->deleteLater();
|
|
|
|
m_cache = NULL;
|
2008-04-06 17:20:13 +00:00
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
QString path = m_cachedir.absolutePath();
|
2008-04-06 17:20:13 +00:00
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
if(!c || m_cachedir.absolutePath().isEmpty()) {
|
|
|
|
qDebug() << "[HttpGet] disabling download cache";
|
2008-04-06 17:20:13 +00:00
|
|
|
}
|
2013-01-25 21:57:19 +00:00
|
|
|
else {
|
|
|
|
// append the cache path to make it unique in case the path points to
|
|
|
|
// the system temporary path. In that case using it directly might
|
|
|
|
// cause problems. Extra path also used in configure dialog.
|
|
|
|
path += "/rbutil-cache";
|
|
|
|
qDebug() << "[HttpGet] setting cache folder to" << path;
|
|
|
|
m_cache = new QNetworkDiskCache(this);
|
|
|
|
m_cache->setCacheDirectory(path);
|
|
|
|
}
|
|
|
|
m_mgr->setCache(m_cache);
|
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()
|
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
return m_data;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setProxy(const QUrl &proxy)
|
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
qDebug() << "[HttpGet] Proxy set to" << proxy;
|
|
|
|
m_proxy.setType(QNetworkProxy::HttpProxy);
|
|
|
|
m_proxy.setHostName(proxy.host());
|
|
|
|
m_proxy.setPort(proxy.port());
|
|
|
|
m_proxy.setUser(proxy.userName());
|
|
|
|
m_proxy.setPassword(proxy.password());
|
|
|
|
m_mgr->setProxy(m_proxy);
|
2008-03-05 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setProxy(bool enable)
|
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
if(enable) m_mgr->setProxy(m_proxy);
|
|
|
|
else m_mgr->setProxy(QNetworkProxy::NoProxy);
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::setFile(QFile *file)
|
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
m_outputFile = file;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::abort()
|
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
if(m_reply) m_reply->abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HttpGet::requestFinished(QNetworkReply* reply)
|
|
|
|
{
|
|
|
|
m_lastStatusCode
|
|
|
|
= reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
qDebug() << "[HttpGet] Request finished, status code:" << m_lastStatusCode;
|
|
|
|
m_lastServerTimestamp
|
|
|
|
= reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toLocalTime();
|
|
|
|
qDebug() << "[HttpGet] Data from cache:"
|
|
|
|
<< reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool();
|
|
|
|
m_lastRequestCached =
|
|
|
|
reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool();
|
|
|
|
if(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).isValid()) {
|
|
|
|
// handle relative URLs using QUrl::resolved()
|
|
|
|
QUrl org = reply->request().url();
|
|
|
|
QUrl url = QUrl(org).resolved(
|
|
|
|
reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl());
|
|
|
|
// reconstruct query
|
|
|
|
#if QT_VERSION < 0x050000
|
|
|
|
QList<QPair<QByteArray, QByteArray> > qitms = org.encodedQueryItems();
|
|
|
|
for(int i = 0; i < qitms.size(); ++i)
|
|
|
|
url.addEncodedQueryItem(qitms.at(i).first, qitms.at(i).second);
|
|
|
|
#else
|
|
|
|
url.setQuery(org.query());
|
|
|
|
#endif
|
|
|
|
qDebug() << "[HttpGet] Redirected to" << url;
|
|
|
|
startRequest(url);
|
|
|
|
return;
|
2008-05-13 19:38:17 +00:00
|
|
|
}
|
2013-01-25 21:57:19 +00:00
|
|
|
else if(m_lastStatusCode == 200) {
|
|
|
|
m_data = reply->readAll();
|
|
|
|
if(m_outputFile && m_outputFile->open(QIODevice::WriteOnly)) {
|
|
|
|
m_outputFile->write(m_data);
|
|
|
|
m_outputFile->close();
|
2008-05-13 19:38:17 +00:00
|
|
|
}
|
2013-01-25 21:57:19 +00:00
|
|
|
emit done(false);
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-01-25 21:57:19 +00:00
|
|
|
m_data.clear();
|
|
|
|
emit done(true);
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
2013-01-25 21:57:19 +00:00
|
|
|
reply->deleteLater();
|
|
|
|
m_reply = NULL;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 22:47:01 +00:00
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
void HttpGet::downloadProgress(qint64 received, qint64 total)
|
2007-07-25 20:21:06 +00:00
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
emit dataReadProgress((int)received, (int)total);
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
void HttpGet::startRequest(QUrl url)
|
2007-07-25 20:21:06 +00:00
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
qDebug() << "[HttpGet] Request started";
|
|
|
|
QNetworkRequest req(url);
|
|
|
|
if(!m_globalUserAgent.isEmpty())
|
|
|
|
req.setRawHeader("User-Agent", m_globalUserAgent.toLatin1());
|
2007-07-25 20:21:06 +00:00
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
m_reply = m_mgr->get(req);
|
|
|
|
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
|
|
|
this, SLOT(networkError(QNetworkReply::NetworkError)));
|
|
|
|
connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)),
|
|
|
|
this, SLOT(downloadProgress(qint64, qint64)));
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
void HttpGet::networkError(QNetworkReply::NetworkError error)
|
2007-07-25 20:21:06 +00:00
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
qDebug() << "[HttpGet] NetworkError occured:"
|
|
|
|
<< error << m_reply->errorString();
|
|
|
|
m_lastErrorString = m_reply->errorString();
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
bool HttpGet::getFile(const QUrl &url)
|
2007-07-25 20:21:06 +00:00
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
qDebug() << "[HttpGet] Get URI" << url.toString();
|
|
|
|
m_data.clear();
|
|
|
|
startRequest(url);
|
2009-03-21 23:18:57 +00:00
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
return false;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
QString HttpGet::errorString(void)
|
2007-07-25 20:21:06 +00:00
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
return m_lastErrorString;
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
2007-08-14 22:47:01 +00:00
|
|
|
|
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
int HttpGet::httpResponse(void)
|
2007-08-14 22:47:01 +00:00
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
return m_lastStatusCode;
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
|
|
|
|