refactor: more net cleanup

This runs clang-tidy on some other files in launcher/net/.

This also makes use of some JSON wrappers in HttpMetaCache, instead of
using the Qt stuff directly.

Lastly, this removes useless null checks (crashes don't occur because of
this, but because of concurrent usage / free of the QByteArray pointer),
and fix a fixme in Download.h
This commit is contained in:
flow
2022-04-27 18:36:11 -03:00
parent efa3fbff39
commit 040ee919e5
9 changed files with 228 additions and 300 deletions

View File

@ -1,55 +1,47 @@
#pragma once
#include "Validator.h"
#include <QCryptographicHash>
#include <memory>
#include <QFile>
namespace Net {
class ChecksumValidator: public Validator
{
public: /* con/des */
class ChecksumValidator : public Validator {
public:
ChecksumValidator(QCryptographicHash::Algorithm algorithm, QByteArray expected = QByteArray())
:m_checksum(algorithm), m_expected(expected)
{
};
virtual ~ChecksumValidator() {};
: m_checksum(algorithm), m_expected(expected){};
virtual ~ChecksumValidator() = default;
public: /* methods */
bool init(QNetworkRequest &) override
public:
auto init(QNetworkRequest&) -> bool override
{
m_checksum.reset();
return true;
}
bool write(QByteArray & data) override
auto write(QByteArray& data) -> bool override
{
m_checksum.addData(data);
return true;
}
bool abort() override
auto abort() -> bool override { return true; }
auto validate(QNetworkReply&) -> bool override
{
return true;
}
bool validate(QNetworkReply &) override
{
if(m_expected.size() && m_expected != hash())
{
if (m_expected.size() && m_expected != hash()) {
qWarning() << "Checksum mismatch, download is bad.";
return false;
}
return true;
}
QByteArray hash()
{
return m_checksum.result();
}
void setExpected(QByteArray expected)
{
m_expected = expected;
}
private: /* data */
auto hash() -> QByteArray { return m_checksum.result(); }
void setExpected(QByteArray expected) { m_expected = expected; }
private:
QCryptographicHash m_checksum;
QByteArray m_expected;
};
}
} // namespace Net