mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-11 09:32:12 +01:00
[CaBundleUpdater] Correct the error handling when downloading bundle
This commit is contained in:
parent
cd5ea7d66a
commit
cc479f29e1
|
@ -34,9 +34,14 @@ CaBundleUpdater::CaBundleUpdater(NetworkManager* manager, QObject* parent)
|
||||||
, m_reply(0)
|
, m_reply(0)
|
||||||
, m_latestBundleVersion(0)
|
, m_latestBundleVersion(0)
|
||||||
{
|
{
|
||||||
m_bundleVersionFileName = DataPaths::path(DataPaths::Config) + "/certificates/bundle_version";
|
m_bundleVersionFileName = DataPaths::path(DataPaths::Config) + QL1S("/certificates/bundle_version");
|
||||||
m_bundleFileName = DataPaths::path(DataPaths::Config) + "/certificates/ca-bundle.crt";
|
m_bundleFileName = DataPaths::path(DataPaths::Config) + QL1S("/certificates/ca-bundle.crt");
|
||||||
m_lastUpdateFileName = DataPaths::path(DataPaths::Config) + "/certificates/last_update";
|
m_lastUpdateFileName = DataPaths::path(DataPaths::Config) + QL1S("/certificates/last_update");
|
||||||
|
|
||||||
|
// Make sure the certificates directory exists
|
||||||
|
QDir certDir(DataPaths::path(DataPaths::Config) + QL1S("/certificates"));
|
||||||
|
if (!certDir.exists())
|
||||||
|
certDir.mkpath(certDir.absolutePath());
|
||||||
|
|
||||||
int updateTime = 30 * 1000;
|
int updateTime = 30 * 1000;
|
||||||
|
|
||||||
|
@ -54,13 +59,13 @@ void CaBundleUpdater::start()
|
||||||
bool updateNow = false;
|
bool updateNow = false;
|
||||||
|
|
||||||
if (updateFile.exists()) {
|
if (updateFile.exists()) {
|
||||||
if (!updateFile.open(QFile::ReadOnly)) {
|
if (updateFile.open(QFile::ReadOnly)) {
|
||||||
qWarning() << "CaBundleUpdater::start cannot open file for reading" << m_lastUpdateFileName;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
QDateTime updateTime = QDateTime::fromString(updateFile.readAll());
|
QDateTime updateTime = QDateTime::fromString(updateFile.readAll());
|
||||||
updateNow = updateTime.addDays(5) < QDateTime::currentDateTime();
|
updateNow = updateTime.addDays(5) < QDateTime::currentDateTime();
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
qWarning() << "CaBundleUpdater::start cannot open file for reading" << m_lastUpdateFileName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
updateNow = true;
|
updateNow = true;
|
||||||
|
@ -69,7 +74,7 @@ void CaBundleUpdater::start()
|
||||||
if (updateNow) {
|
if (updateNow) {
|
||||||
m_progress = CheckLastUpdate;
|
m_progress = CheckLastUpdate;
|
||||||
|
|
||||||
QUrl url = QUrl::fromEncoded(QString(Qz::WWWADDRESS + "/certs/bundle_version").toUtf8());
|
QUrl url = QUrl::fromEncoded(QString(Qz::WWWADDRESS + QL1S("/certs/bundle_version")).toUtf8());
|
||||||
m_reply = m_manager->get(QNetworkRequest(url));
|
m_reply = m_manager->get(QNetworkRequest(url));
|
||||||
connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
|
connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
|
||||||
}
|
}
|
||||||
|
@ -86,27 +91,30 @@ void CaBundleUpdater::replyFinished()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_latestBundleVersion = response.toInt();
|
bool ok;
|
||||||
int currentBundleVersion = QzTools::readAllFileContents(m_bundleVersionFileName).trimmed().toInt();
|
|
||||||
|
|
||||||
if (m_latestBundleVersion == 0) {
|
m_latestBundleVersion = response.toInt(&ok);
|
||||||
|
if (!ok || m_latestBundleVersion <= 0)
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (m_latestBundleVersion > currentBundleVersion) {
|
int currentBundleVersion = QzTools::readAllFileContents(m_bundleVersionFileName).trimmed().toInt(&ok);
|
||||||
m_progress = LoadBundle;
|
if (!ok)
|
||||||
|
currentBundleVersion = 0;
|
||||||
QUrl url = QUrl::fromEncoded(QString(Qz::WWWADDRESS + "/certs/ca-bundle.crt").toUtf8());
|
|
||||||
m_reply = m_manager->get(QNetworkRequest(url));
|
|
||||||
connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
|
|
||||||
}
|
|
||||||
|
|
||||||
QFile file(m_lastUpdateFileName);
|
QFile file(m_lastUpdateFileName);
|
||||||
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
||||||
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_lastUpdateFileName;
|
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_lastUpdateFileName;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write(QDateTime::currentDateTime().toString().toUtf8());
|
file.write(QDateTime::currentDateTime().toString().toUtf8());
|
||||||
|
|
||||||
|
if (m_latestBundleVersion > currentBundleVersion) {
|
||||||
|
m_progress = LoadBundle;
|
||||||
|
|
||||||
|
QUrl url = QUrl::fromEncoded(QString(Qz::WWWADDRESS + QL1S("/certs/ca-bundle.crt")).toUtf8());
|
||||||
|
m_reply = m_manager->get(QNetworkRequest(url));
|
||||||
|
connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (m_progress == LoadBundle) {
|
else if (m_progress == LoadBundle) {
|
||||||
QByteArray response = m_reply->readAll();
|
QByteArray response = m_reply->readAll();
|
||||||
|
@ -120,6 +128,7 @@ void CaBundleUpdater::replyFinished()
|
||||||
QFile file(m_bundleVersionFileName);
|
QFile file(m_bundleVersionFileName);
|
||||||
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
||||||
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_bundleVersionFileName;
|
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_bundleVersionFileName;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write(QByteArray::number(m_latestBundleVersion));
|
file.write(QByteArray::number(m_latestBundleVersion));
|
||||||
|
@ -128,6 +137,7 @@ void CaBundleUpdater::replyFinished()
|
||||||
file.setFileName(m_bundleFileName);
|
file.setFileName(m_bundleFileName);
|
||||||
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
|
||||||
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_bundleFileName;
|
qWarning() << "CaBundleUpdater::replyFinished cannot open file for writing" << m_bundleFileName;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write(response);
|
file.write(response);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user