[libcalamares] Add synchronousGet() to network service

- Synchronous download of a given URL; not something to
   do from the GUI thread.
 - Use it from the GeoIP service, which downloads in a
   separate thread to do GeoIP lookups.
 - Drop now-unused headers.
 - Adjust tests for GeoIP to use network service
This commit is contained in:
Adriaan de Groot 2019-08-22 16:03:21 +02:00
parent eae931f2ed
commit 8ea1ea6662
4 changed files with 32 additions and 45 deletions

View File

@ -24,9 +24,7 @@
#endif #endif
#include "Handler.h" #include "Handler.h"
#include <QNetworkAccessManager> #include "network/Manager.h"
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QtTest/QtTest> #include <QtTest/QtTest>
@ -197,27 +195,9 @@ GeoIPTests::testSplitTZ()
} }
static QByteArray
synchronous_get( const char* urlstring )
{
QUrl url( urlstring );
QNetworkAccessManager manager;
QEventLoop loop;
qDebug() << "Fetching" << url;
QObject::connect( &manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit );
QNetworkRequest request( url );
QNetworkReply* reply = manager.get( request );
loop.exec();
reply->deleteLater();
return reply->readAll();
}
#define CHECK_GET( t, selector, url ) \ #define CHECK_GET( t, selector, url ) \
{ \ { \
auto tz = GeoIP##t( selector ).processReply( synchronous_get( url ) ); \ auto tz = GeoIP##t( selector ).processReply( CalamaresUtils::Network::Manager::instance().synchronousGet( QUrl( url ) ) ); \
qDebug() << tz; \ qDebug() << tz; \
QCOMPARE( default_tz, tz ); \ QCOMPARE( default_tz, tz ); \
auto tz2 = CalamaresUtils::GeoIP::Handler( "" #t, url, selector ).get(); \ auto tz2 = CalamaresUtils::GeoIP::Handler( "" #t, url, selector ).get(); \
@ -236,7 +216,7 @@ GeoIPTests::testGet()
GeoIPJSON default_handler; GeoIPJSON default_handler;
// Call the KDE service the definitive source. // Call the KDE service the definitive source.
auto default_tz = default_handler.processReply( synchronous_get( "https://geoip.kde.org/v1/calamares" ) ); auto default_tz = default_handler.processReply( CalamaresUtils::Network::Manager::instance().synchronousGet( QUrl( "https://geoip.kde.org/v1/calamares" ) ) );
// This is bogus, because the test isn't always run by me // This is bogus, because the test isn't always run by me
// QCOMPARE( default_tz.first, QStringLiteral("Europe") ); // QCOMPARE( default_tz.first, QStringLiteral("Europe") );

View File

@ -23,14 +23,11 @@
#include "GeoIPXML.h" #include "GeoIPXML.h"
#endif #endif
#include "network/Manager.h"
#include "utils/Logger.h" #include "utils/Logger.h"
#include "utils/NamedEnum.h" #include "utils/NamedEnum.h"
#include "utils/Variant.h" #include "utils/Variant.h"
#include <QEventLoop>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <memory> #include <memory>
static const NamedEnumTable< CalamaresUtils::GeoIP::Handler::Type >& static const NamedEnumTable< CalamaresUtils::GeoIP::Handler::Type >&
@ -87,22 +84,6 @@ Handler::Handler( const QString& implementation, const QString& url, const QStri
Handler::~Handler() {} Handler::~Handler() {}
static QByteArray
synchronous_get( const QString& urlstring )
{
QUrl url( urlstring );
QNetworkAccessManager manager;
QEventLoop loop;
QObject::connect( &manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit );
QNetworkRequest request( url );
QNetworkReply* reply = manager.get( request );
loop.exec();
reply->deleteLater();
return reply->readAll();
}
static std::unique_ptr< Interface > static std::unique_ptr< Interface >
create_interface( Handler::Type t, const QString& selector ) create_interface( Handler::Type t, const QString& selector )
{ {
@ -131,7 +112,7 @@ do_query( Handler::Type type, const QString& url, const QString& selector )
return RegionZonePair(); return RegionZonePair();
} }
return interface->processReply( synchronous_get( url ) ); return interface->processReply( CalamaresUtils::Network::Manager::instance().synchronousGet( url ) );
} }
static QString static QString
@ -143,7 +124,7 @@ do_raw_query( Handler::Type type, const QString& url, const QString& selector )
return QString(); return QString();
} }
return interface->rawReply( synchronous_get( url ) ); return interface->rawReply( CalamaresUtils::Network::Manager::instance().synchronousGet( url ) );
} }
RegionZonePair RegionZonePair

View File

@ -95,5 +95,23 @@ CalamaresUtils::Network::Manager::synchronousPing( const QUrl& url )
QEventLoop loop; QEventLoop loop;
connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit ); connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit );
loop.exec(); loop.exec();
reply->deleteLater();
return reply->bytesAvailable(); return reply->bytesAvailable();
} }
QByteArray
CalamaresUtils::Network::Manager::synchronousGet( const QUrl& url )
{
if ( !url.isValid() )
{
return QByteArray();
}
QNetworkRequest request( url );
QNetworkReply* reply = d->m_nam->get( request );
QEventLoop loop;
connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit );
loop.exec();
reply->deleteLater();
return reply->readAll();
}

View File

@ -21,6 +21,7 @@
#include "DllMacro.h" #include "DllMacro.h"
#include <QByteArray>
#include <QObject> #include <QObject>
#include <QUrl> #include <QUrl>
@ -52,6 +53,13 @@ public:
*/ */
bool synchronousPing( const QUrl& url ); bool synchronousPing( const QUrl& url );
/** @brief Downloads the data from a given @p url
*
* Returns the data as a QByteArray, or an empty
* array if any error occurred.
*/
QByteArray synchronousGet( const QUrl& url );
/// @brief Set the URL which is used for the general "is there internet" check. /// @brief Set the URL which is used for the general "is there internet" check.
void setCheckHasInternetUrl( const QUrl& url ); void setCheckHasInternetUrl( const QUrl& url );
/** @brief Do an explicit check for internet connectivity. /** @brief Do an explicit check for internet connectivity.