aboutsummaryrefslogtreecommitdiff
path: root/lib/jobs
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-08-24 09:41:51 +0200
committerAlexey Rusakov <Kitsune-Ral@users.sf.net>2022-08-24 09:41:51 +0200
commit82f4efb0227e7e22e831733fae3952818b063ac2 (patch)
tree3b154a16f9d355996a59c611230d0e010edab57f /lib/jobs
parent16d4f4e48304543a0ab59b235edba07f5f2c2204 (diff)
parent6308bff3336ca7680eee54d9bd125f780fa9f033 (diff)
downloadlibquotient-82f4efb0227e7e22e831733fae3952818b063ac2.tar.gz
libquotient-82f4efb0227e7e22e831733fae3952818b063ac2.zip
Merge branch 'dev' into device-verification
# Conflicts: # autotests/testfilecrypto.cpp # lib/connection.cpp # lib/connection.h # lib/database.cpp # lib/database.h # lib/e2ee/qolmoutboundsession.cpp # lib/e2ee/qolmoutboundsession.h # lib/eventitem.h # lib/events/encryptedevent.cpp # lib/events/encryptedevent.h # lib/events/encryptedfile.cpp # lib/events/encryptedfile.h # lib/events/keyverificationevent.cpp # lib/events/keyverificationevent.h # lib/events/roomkeyevent.h # lib/room.cpp # lib/room.h
Diffstat (limited to 'lib/jobs')
-rw-r--r--lib/jobs/basejob.cpp28
-rw-r--r--lib/jobs/downloadfilejob.cpp56
-rw-r--r--lib/jobs/downloadfilejob.h5
-rw-r--r--lib/jobs/requestdata.cpp6
-rw-r--r--lib/jobs/requestdata.h22
-rw-r--r--lib/jobs/syncjob.h2
6 files changed, 60 insertions, 59 deletions
diff --git a/lib/jobs/basejob.cpp b/lib/jobs/basejob.cpp
index b6858b5a..da645a2d 100644
--- a/lib/jobs/basejob.cpp
+++ b/lib/jobs/basejob.cpp
@@ -138,9 +138,8 @@ public:
QTimer timer;
QTimer retryTimer;
- static constexpr std::array<const JobTimeoutConfig, 3> errorStrategy {
- { { 90s, 5s }, { 90s, 10s }, { 120s, 30s } }
- };
+ static constexpr auto errorStrategy = std::to_array<const JobTimeoutConfig>(
+ { { 90s, 5s }, { 90s, 10s }, { 120s, 30s } });
int maxRetries = int(errorStrategy.size());
int retriesTaken = 0;
@@ -152,10 +151,8 @@ public:
[[nodiscard]] QString dumpRequest() const
{
- // FIXME: use std::array {} when Apple stdlib gets deduction guides for it
- static const auto verbs =
- make_array(QStringLiteral("GET"), QStringLiteral("PUT"),
- QStringLiteral("POST"), QStringLiteral("DELETE"));
+ static const std::array verbs { "GET"_ls, "PUT"_ls, "POST"_ls,
+ "DELETE"_ls };
const auto verbWord = verbs.at(size_t(verb));
return verbWord % ' '
% (reply ? reply->url().toString(QUrl::RemoveQuery)
@@ -301,16 +298,10 @@ void BaseJob::Private::sendRequest()
QNetworkRequest::NoLessSafeRedirectPolicy);
req.setMaximumRedirectsAllowed(10);
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
- req.setAttribute(
-#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
- QNetworkRequest::Http2AllowedAttribute
-#else
- QNetworkRequest::HTTP2AllowedAttribute
-#endif
// Qt doesn't combine HTTP2 with SSL quite right, occasionally crashing at
// what seems like an attempt to write to a closed channel. If/when that
// changes, false should be turned to true below.
- , false);
+ req.setAttribute(QNetworkRequest::Http2AllowedAttribute, false);
Q_ASSERT(req.url().isValid());
for (auto it = requestHeaders.cbegin(); it != requestHeaders.cend(); ++it)
req.setRawHeader(it.key(), it.value());
@@ -754,11 +745,14 @@ QString BaseJob::statusCaption() const
}
}
-int BaseJob::error() const { return d->status.code; }
+int BaseJob::error() const {
+ return d->status.code; }
-QString BaseJob::errorString() const { return d->status.message; }
+QString BaseJob::errorString() const {
+ return d->status.message; }
-QUrl BaseJob::errorUrl() const { return d->errorUrl; }
+QUrl BaseJob::errorUrl() const {
+ return d->errorUrl; }
void BaseJob::setStatus(Status s)
{
diff --git a/lib/jobs/downloadfilejob.cpp b/lib/jobs/downloadfilejob.cpp
index d00fc5f4..759d52c9 100644
--- a/lib/jobs/downloadfilejob.cpp
+++ b/lib/jobs/downloadfilejob.cpp
@@ -8,8 +8,9 @@
#include <QtNetwork/QNetworkReply>
#ifdef Quotient_E2EE_ENABLED
-# include <QtCore/QCryptographicHash>
-# include "events/encryptedfile.h"
+# include "events/filesourceinfo.h"
+
+# include <QtCore/QCryptographicHash>
#endif
using namespace Quotient;
@@ -26,7 +27,7 @@ public:
QScopedPointer<QFile> tempFile;
#ifdef Quotient_E2EE_ENABLED
- Omittable<EncryptedFile> encryptedFile;
+ Omittable<EncryptedFileMetadata> encryptedFileMetadata;
#endif
};
@@ -49,14 +50,14 @@ DownloadFileJob::DownloadFileJob(const QString& serverName,
#ifdef Quotient_E2EE_ENABLED
DownloadFileJob::DownloadFileJob(const QString& serverName,
const QString& mediaId,
- const EncryptedFile& file,
+ const EncryptedFileMetadata& file,
const QString& localFilename)
: GetContentJob(serverName, mediaId)
, d(localFilename.isEmpty() ? makeImpl<Private>()
: makeImpl<Private>(localFilename))
{
setObjectName(QStringLiteral("DownloadFileJob"));
- d->encryptedFile = file;
+ d->encryptedFileMetadata = file;
}
#endif
QString DownloadFileJob::targetFileName() const
@@ -118,27 +119,31 @@ void DownloadFileJob::beforeAbandon()
d->tempFile->remove();
}
+void decryptFile(QFile& sourceFile, const EncryptedFileMetadata& metadata,
+ QFile& targetFile)
+{
+ sourceFile.seek(0);
+ const auto encrypted = sourceFile.readAll(); // TODO: stream decryption
+ const auto decrypted = decryptFile(encrypted, metadata);
+ targetFile.write(decrypted);
+}
+
BaseJob::Status DownloadFileJob::prepareResult()
{
if (d->targetFile) {
#ifdef Quotient_E2EE_ENABLED
- if (d->encryptedFile.has_value()) {
- d->tempFile->seek(0);
- QByteArray encrypted = d->tempFile->readAll();
-
- EncryptedFile file = *d->encryptedFile;
- const auto decrypted = file.decryptFile(encrypted);
- d->targetFile->write(decrypted);
+ if (d->encryptedFileMetadata.has_value()) {
+ decryptFile(*d->tempFile, *d->encryptedFileMetadata, *d->targetFile);
d->tempFile->remove();
} else {
#endif
d->targetFile->close();
if (!d->targetFile->remove()) {
- qCWarning(JOBS) << "Failed to remove the target file placeholder";
+ qWarning(JOBS) << "Failed to remove the target file placeholder";
return { FileError, "Couldn't finalise the download" };
}
if (!d->tempFile->rename(d->targetFile->fileName())) {
- qCWarning(JOBS) << "Failed to rename" << d->tempFile->fileName()
+ qWarning(JOBS) << "Failed to rename" << d->tempFile->fileName()
<< "to" << d->targetFile->fileName();
return { FileError, "Couldn't finalise the download" };
}
@@ -147,13 +152,20 @@ BaseJob::Status DownloadFileJob::prepareResult()
#endif
} else {
#ifdef Quotient_E2EE_ENABLED
- if (d->encryptedFile.has_value()) {
- d->tempFile->seek(0);
- const auto encrypted = d->tempFile->readAll();
-
- EncryptedFile file = *d->encryptedFile;
- const auto decrypted = file.decryptFile(encrypted);
- d->tempFile->write(decrypted);
+ if (d->encryptedFileMetadata.has_value()) {
+ QTemporaryFile tempTempFile; // Assuming it to be next to tempFile
+ decryptFile(*d->tempFile, *d->encryptedFileMetadata, tempTempFile);
+ d->tempFile->close();
+ if (!d->tempFile->remove()) {
+ qWarning(JOBS)
+ << "Failed to remove the decrypted file placeholder";
+ return { FileError, "Couldn't finalise the download" };
+ }
+ if (!tempTempFile.rename(d->tempFile->fileName())) {
+ qWarning(JOBS) << "Failed to rename" << tempTempFile.fileName()
+ << "to" << d->tempFile->fileName();
+ return { FileError, "Couldn't finalise the download" };
+ }
} else {
#endif
d->tempFile->close();
@@ -161,6 +173,6 @@ BaseJob::Status DownloadFileJob::prepareResult()
}
#endif
}
- qCDebug(JOBS) << "Saved a file as" << targetFileName();
+ qDebug(JOBS) << "Saved a file as" << targetFileName();
return Success;
}
diff --git a/lib/jobs/downloadfilejob.h b/lib/jobs/downloadfilejob.h
index ffa3d055..cbbfd244 100644
--- a/lib/jobs/downloadfilejob.h
+++ b/lib/jobs/downloadfilejob.h
@@ -4,7 +4,8 @@
#pragma once
#include "csapi/content-repo.h"
-#include "events/encryptedfile.h"
+
+#include "events/filesourceinfo.h"
namespace Quotient {
class QUOTIENT_API DownloadFileJob : public GetContentJob {
@@ -16,7 +17,7 @@ public:
const QString& localFilename = {});
#ifdef Quotient_E2EE_ENABLED
- DownloadFileJob(const QString& serverName, const QString& mediaId, const EncryptedFile& file, const QString& localFilename = {});
+ DownloadFileJob(const QString& serverName, const QString& mediaId, const EncryptedFileMetadata& file, const QString& localFilename = {});
#endif
QString targetFileName() const;
diff --git a/lib/jobs/requestdata.cpp b/lib/jobs/requestdata.cpp
index 2c001ccc..ab249f6d 100644
--- a/lib/jobs/requestdata.cpp
+++ b/lib/jobs/requestdata.cpp
@@ -14,7 +14,7 @@ using namespace Quotient;
auto fromData(const QByteArray& data)
{
- auto source = std::make_unique<QBuffer>();
+ auto source = makeImpl<QBuffer, QIODevice>();
source->setData(data);
source->open(QIODevice::ReadOnly);
return source;
@@ -33,7 +33,5 @@ RequestData::RequestData(const QJsonObject& jo) : _source(fromJson(jo)) {}
RequestData::RequestData(const QJsonArray& ja) : _source(fromJson(ja)) {}
RequestData::RequestData(QIODevice* source)
- : _source(std::unique_ptr<QIODevice>(source))
+ : _source(acquireImpl(source))
{}
-
-RequestData::~RequestData() = default;
diff --git a/lib/jobs/requestdata.h b/lib/jobs/requestdata.h
index 41ad833a..accc8f71 100644
--- a/lib/jobs/requestdata.h
+++ b/lib/jobs/requestdata.h
@@ -3,11 +3,7 @@
#pragma once
-#include "quotient_export.h"
-
-#include <QtCore/QByteArray>
-
-#include <memory>
+#include "util.h"
class QJsonObject;
class QJsonArray;
@@ -23,17 +19,17 @@ namespace Quotient {
*/
class QUOTIENT_API RequestData {
public:
- RequestData(const QByteArray& a = {});
- RequestData(const QJsonObject& jo);
- RequestData(const QJsonArray& ja);
- RequestData(QIODevice* source);
- RequestData(RequestData&&) = default;
- RequestData& operator=(RequestData&&) = default;
- ~RequestData();
+ // NOLINTBEGIN(google-explicit-constructor): that check should learn about
+ // explicit(false)
+ QUO_IMPLICIT RequestData(const QByteArray& a = {});
+ QUO_IMPLICIT RequestData(const QJsonObject& jo);
+ QUO_IMPLICIT RequestData(const QJsonArray& ja);
+ QUO_IMPLICIT RequestData(QIODevice* source);
+ // NOLINTEND(google-explicit-constructor)
QIODevice* source() const { return _source.get(); }
private:
- std::unique_ptr<QIODevice> _source;
+ ImplPtr<QIODevice> _source;
};
} // namespace Quotient
diff --git a/lib/jobs/syncjob.h b/lib/jobs/syncjob.h
index 830a7c71..b7bfbbb3 100644
--- a/lib/jobs/syncjob.h
+++ b/lib/jobs/syncjob.h
@@ -15,7 +15,7 @@ public:
explicit SyncJob(const QString& since, const Filter& filter,
int timeout = -1, const QString& presence = {});
- SyncData&& takeData() { return std::move(d); }
+ SyncData takeData() { return std::move(d); }
protected:
Status prepareResult() override;