[libcalamares] Make failures in the internal methods obvious

- internally, timeout and error will return nullptr
This commit is contained in:
Adriaan de Groot 2019-08-26 14:43:41 +02:00
parent ededebbc6c
commit f0be7fd4aa

View File

@ -99,6 +99,14 @@ Manager::setCheckHasInternetUrl( const QUrl& url )
d->m_hasInternetUrl = url; d->m_hasInternetUrl = url;
} }
/** @brief Does a request synchronously, returns the request itself
*
* The extra options for the request are taken from @p options,
* including the timeout setting.
*
* On failure, returns nullptr (e.g. bad URL, timeout). The request
* is marked for later automatic deletion, so don't store the pointer.
*/
static QNetworkReply* static QNetworkReply*
synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl& url, const RequestOptions& options ) synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl& url, const RequestOptions& options )
{ {
@ -107,10 +115,11 @@ synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl&
QEventLoop loop; QEventLoop loop;
QTimer timer; QTimer timer;
// Bail out early if the request is bad
if ( reply->error() ) if ( reply->error() )
{ {
reply->deleteLater(); reply->deleteLater();
return reply; return nullptr;
} }
options.applyToRequest( &request ); options.applyToRequest( &request );
@ -123,6 +132,12 @@ synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl&
QObject::connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit ); QObject::connect( reply, &QNetworkReply::finished, &loop, &QEventLoop::quit );
loop.exec(); loop.exec();
if ( options.hasTimeout() && !timer.isActive() )
{
reply->deleteLater();
return nullptr;
}
reply->deleteLater(); reply->deleteLater();
return reply; return reply;
} }
@ -135,8 +150,8 @@ Manager::synchronousPing( const QUrl& url, const RequestOptions& options )
return false; return false;
} }
auto reply = synchronousRun( d->m_nam, url, options ); auto* reply = synchronousRun( d->m_nam, url, options );
return reply->bytesAvailable(); return reply && reply->bytesAvailable();
} }
QByteArray QByteArray
@ -147,8 +162,8 @@ Manager::synchronousGet( const QUrl& url, const RequestOptions& options )
return QByteArray(); return QByteArray();
} }
auto reply = synchronousRun( d->m_nam, url, options ); auto* reply = synchronousRun( d->m_nam, url, options );
return reply->readAll(); return reply ? reply->readAll() : QByteArray();
} }
} // namespace Network } // namespace Network