[libcalamares] Refactor request internals
- distinguish timeouts from other failures - git synchronousPing() a more detailed result, which is still bool-compatible.
This commit is contained in:
parent
f0be7fd4aa
commit
e2c6591a77
@ -107,7 +107,7 @@ Manager::setCheckHasInternetUrl( const QUrl& url )
|
||||
* 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 QPair< RequestStatus, QNetworkReply* >
|
||||
synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl& url, const RequestOptions& options )
|
||||
{
|
||||
QNetworkRequest request = QNetworkRequest( url );
|
||||
@ -119,7 +119,7 @@ synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl&
|
||||
if ( reply->error() )
|
||||
{
|
||||
reply->deleteLater();
|
||||
return nullptr;
|
||||
return qMakePair( RequestStatus( RequestStatus::Failed ), nullptr );
|
||||
}
|
||||
|
||||
options.applyToRequest( &request );
|
||||
@ -135,23 +135,30 @@ synchronousRun( const std::unique_ptr< QNetworkAccessManager >& nam, const QUrl&
|
||||
if ( options.hasTimeout() && !timer.isActive() )
|
||||
{
|
||||
reply->deleteLater();
|
||||
return nullptr;
|
||||
return qMakePair( RequestStatus( RequestStatus::Timeout ), nullptr );
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
return reply;
|
||||
return qMakePair( RequestStatus( RequestStatus::Ok ), reply );
|
||||
}
|
||||
|
||||
bool
|
||||
RequestStatus
|
||||
Manager::synchronousPing( const QUrl& url, const RequestOptions& options )
|
||||
{
|
||||
if ( !url.isValid() )
|
||||
{
|
||||
return false;
|
||||
return RequestStatus::Failed;
|
||||
}
|
||||
|
||||
auto* reply = synchronousRun( d->m_nam, url, options );
|
||||
return reply && reply->bytesAvailable();
|
||||
auto reply = synchronousRun( d->m_nam, url, options );
|
||||
if ( reply.first )
|
||||
{
|
||||
return reply.second->bytesAvailable() ? RequestStatus::Ok : RequestStatus::Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
return reply.first;
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray
|
||||
@ -162,8 +169,8 @@ Manager::synchronousGet( const QUrl& url, const RequestOptions& options )
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
auto* reply = synchronousRun( d->m_nam, url, options );
|
||||
return reply ? reply->readAll() : QByteArray();
|
||||
auto reply = synchronousRun( d->m_nam, url, options );
|
||||
return reply.first ? reply.second->readAll() : QByteArray();
|
||||
}
|
||||
|
||||
} // namespace Network
|
||||
|
@ -70,6 +70,22 @@ private:
|
||||
|
||||
Q_DECLARE_OPERATORS_FOR_FLAGS( RequestOptions::Flags );
|
||||
|
||||
struct RequestStatus
|
||||
{
|
||||
enum State
|
||||
{
|
||||
Ok,
|
||||
Timeout, // Timeout exceeded
|
||||
Failed, // bad Url
|
||||
Empty // for ping(), response is empty
|
||||
};
|
||||
|
||||
RequestStatus( State s = Ok );
|
||||
operator bool() const { return m_s == Ok; }
|
||||
|
||||
State m_s;
|
||||
};
|
||||
|
||||
class DLLEXPORT Manager : QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -87,15 +103,19 @@ public:
|
||||
|
||||
/** @brief Checks if the given @p url returns data.
|
||||
*
|
||||
* Returns @c true if it does; @c false means no data, typically
|
||||
* because of an error or no network access.
|
||||
* Returns a RequestStatus, which converts to @c true if the ping
|
||||
* was successful. Other status reasons convert to @c false,
|
||||
* typically because of no data, a Url error or no network access.
|
||||
*
|
||||
* May return Empty if the request was successful but returned
|
||||
* no data at all.
|
||||
*/
|
||||
bool synchronousPing( const QUrl& url, const RequestOptions& options = RequestOptions() );
|
||||
RequestStatus synchronousPing( const QUrl& url, const RequestOptions& options = RequestOptions() );
|
||||
|
||||
/** @brief Downloads the data from a given @p url
|
||||
*
|
||||
* Returns the data as a QByteArray, or an empty
|
||||
* array if any error occurred.
|
||||
* array if any error occurred (or no data was returned).
|
||||
*/
|
||||
QByteArray synchronousGet( const QUrl& url, const RequestOptions& options = RequestOptions() );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user