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>
|
|
|
|
|
2013-01-25 21:57:19 +00:00
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
|
2007-07-25 20:21:06 +00:00
|
|
|
#include "httpget.h"
|
2013-11-03 10:08:18 +00:00
|
|
|
#include "Logger.h"
|
2007-07-25 20:21:06 +00:00
|
|
|
|
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)
|
2020-11-07 11:47:48 +00:00
|
|
|
: QObject(parent),
|
|
|
|
m_mgr(this),
|
|
|
|
m_reply(nullptr),
|
|
|
|
m_cache(nullptr),
|
|
|
|
m_cachedir(m_globalCache),
|
|
|
|
m_outputFile(nullptr),
|
|
|
|
m_proxy(QNetworkProxy::NoProxy)
|
2007-07-25 20:21:06 +00:00
|
|
|
{
|
2013-01-25 21:57:19 +00:00
|
|
|
setCache(true);
|
2020-11-07 11:47:48 +00:00
|
|
|
connect(&m_mgr, &QNetworkAccessManager::finished, this,
|
|
|
|
static_cast<void (HttpGet::*)(QNetworkReply*)>(&HttpGet::requestFinished));
|
2013-01-25 21:57:19 +00:00
|
|
|
m_lastServerTimestamp = QDateTime();
|
2007-08-14 22:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 22:10:29 +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;
|
2020-07-27 19:16:31 +00:00
|
|
|
m_cachedir.setPath(d.absolutePath());
|
2013-01-25 21:57:19 +00:00
|
|
|
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();
|
2020-11-07 11:47:48 +00:00
|
|
|
m_cache = nullptr;
|
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()) {
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "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";
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "setting cache folder to" << path;
|
2013-01-25 21:57:19 +00:00
|
|
|
m_cache = new QNetworkDiskCache(this);
|
|
|
|
m_cache->setCacheDirectory(path);
|
|
|
|
}
|
2020-11-07 11:47:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 22:10:29 +00:00
|
|
|
/** @brief Set and enable Proxy to use.
|
|
|
|
* @param proxy Proxy URL.
|
|
|
|
*/
|
2007-07-25 20:21:06 +00:00
|
|
|
void HttpGet::setProxy(const QUrl &proxy)
|
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Proxy set to" << proxy;
|
2013-01-25 21:57:19 +00:00
|
|
|
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());
|
2020-11-07 11:47:48 +00:00
|
|
|
m_mgr.setProxy(m_proxy);
|
2008-03-05 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 22:10:29 +00:00
|
|
|
/** @brief Enable or disable use of previously set proxy.
|
|
|
|
* @param enable Enable proxy.
|
|
|
|
*/
|
2008-03-05 21:12:24 +00:00
|
|
|
void HttpGet::setProxy(bool enable)
|
|
|
|
{
|
2020-11-07 11:47:48 +00:00
|
|
|
if(enable) m_mgr.setProxy(m_proxy);
|
|
|
|
else m_mgr.setProxy(QNetworkProxy::NoProxy);
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 22:10:29 +00:00
|
|
|
/** @brief Set output file.
|
|
|
|
*
|
|
|
|
* Set filename for storing the downloaded file to. If no file is set the
|
|
|
|
* downloaded file will not be stored to disk but kept in memory. The result
|
|
|
|
* can then be retrieved using readAll().
|
|
|
|
*
|
|
|
|
* @param file Output file.
|
|
|
|
*/
|
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();
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Request finished, status code:" << m_lastStatusCode;
|
2013-01-25 21:57:19 +00:00
|
|
|
m_lastServerTimestamp
|
|
|
|
= reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toLocalTime();
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Data from cache:"
|
2013-01-25 21:57:19 +00:00
|
|
|
<< 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
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Redirected to" << url;
|
2013-01-25 21:57:19 +00:00
|
|
|
startRequest(url);
|
|
|
|
return;
|
2008-05-13 19:38:17 +00:00
|
|
|
}
|
2015-12-18 22:05:13 +00:00
|
|
|
else if(m_lastStatusCode == 200 ||
|
2016-01-17 19:47:08 +00:00
|
|
|
(reply->url().scheme() == "file" && reply->error() == 0)) {
|
2015-12-18 22:05:13 +00:00
|
|
|
// callers might not be aware if the request is file:// so fake 200.
|
|
|
|
m_lastStatusCode = 200;
|
2013-01-25 21:57:19 +00:00
|
|
|
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();
|
2020-11-07 11:47:48 +00:00
|
|
|
m_reply = nullptr;
|
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-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Request started";
|
2013-01-25 21:57:19 +00:00
|
|
|
QNetworkRequest req(url);
|
|
|
|
if(!m_globalUserAgent.isEmpty())
|
|
|
|
req.setRawHeader("User-Agent", m_globalUserAgent.toLatin1());
|
2007-07-25 20:21:06 +00:00
|
|
|
|
2020-11-07 11:47:48 +00:00
|
|
|
m_reply = m_mgr.get(req);
|
|
|
|
#if QT_VERSION < 0x050f00
|
|
|
|
connect(m_reply,
|
|
|
|
static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
|
|
|
|
this, &HttpGet::networkError);
|
|
|
|
#else
|
|
|
|
connect(m_reply, &QNetworkReply::errorOccurred, this, &HttpGet::networkError);
|
|
|
|
#endif
|
|
|
|
connect(m_reply, &QNetworkReply::downloadProgress, this, &HttpGet::downloadProgress);
|
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-11-03 10:08:18 +00:00
|
|
|
LOG_ERROR() << "NetworkError occured:" << error << m_reply->errorString();
|
2013-01-25 21:57:19 +00:00
|
|
|
m_lastErrorString = m_reply->errorString();
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 22:10:29 +00:00
|
|
|
/** @brief Retrieve the file pointed to by url.
|
|
|
|
*
|
|
|
|
* Note: This also handles file:// URLs. Be aware that QUrl requires file://
|
|
|
|
* URLs to be absolute, i.e. file://filename.txt doesn't work. Use
|
|
|
|
* QDir::absoluteFilePath() to convert to an absolute path first.
|
|
|
|
*
|
|
|
|
* @param url URL to download.
|
|
|
|
*/
|
|
|
|
void HttpGet::getFile(const QUrl &url)
|
2007-07-25 20:21:06 +00:00
|
|
|
{
|
2013-11-03 10:08:18 +00:00
|
|
|
LOG_INFO() << "Get URI" << url.toString();
|
2013-01-25 21:57:19 +00:00
|
|
|
m_data.clear();
|
|
|
|
startRequest(url);
|
2007-07-25 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-18 22:10:29 +00:00
|
|
|
/** @brief Retrieve string representation for most recent error.
|
|
|
|
* @return Error string.
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
2015-12-18 22:10:29 +00:00
|
|
|
/** @brief Return last HTTP response code.
|
|
|
|
* @return Response code.
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|