2016-02-25 20:19:27 +00:00
// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
# include "stdpch.h"
2016-06-12 12:11:58 +00:00
# include "operation.h"
2016-02-25 20:19:27 +00:00
# include "downloader.h"
2016-10-18 18:34:25 +00:00
# include "utils.h"
2016-02-25 20:19:27 +00:00
# include "nel/misc/system_info.h"
# include "nel/misc/path.h"
# ifdef DEBUG_NEW
# define new DEBUG_NEW
# endif
2016-06-12 12:11:58 +00:00
CDownloader : : CDownloader ( QObject * parent , IOperationProgressListener * listener ) : QObject ( parent ) , m_listener ( listener ) , m_manager ( NULL ) , m_reply ( NULL ) , m_timer ( NULL ) ,
2016-02-25 20:19:27 +00:00
m_offset ( 0 ) , m_size ( 0 ) , m_supportsAcceptRanges ( false ) , m_supportsContentRange ( false ) ,
2016-06-12 12:11:58 +00:00
m_downloadAfterHead ( false ) , m_file ( NULL )
2016-02-25 20:19:27 +00:00
{
m_manager = new QNetworkAccessManager ( this ) ;
m_timer = new QTimer ( this ) ;
connect ( m_timer , SIGNAL ( timeout ( ) ) , this , SLOT ( onTimeout ( ) ) ) ;
}
CDownloader : : ~ CDownloader ( )
{
stopTimer ( ) ;
closeFile ( ) ;
}
bool CDownloader : : getHtmlPageContent ( const QString & url )
{
if ( url . isEmpty ( ) ) return false ;
QNetworkRequest request ( url ) ;
request . setHeader ( QNetworkRequest : : UserAgentHeader , " Ryzom Installer/1.0 " ) ;
QNetworkReply * reply = m_manager - > get ( request ) ;
connect ( reply , SIGNAL ( finished ( ) ) , SLOT ( onHtmlPageFinished ( ) ) ) ;
return true ;
}
bool CDownloader : : prepareFile ( const QString & url , const QString & fullPath )
{
if ( url . isEmpty ( ) ) return false ;
m_downloadAfterHead = false ;
2016-06-12 12:11:58 +00:00
if ( m_listener ) m_listener - > operationPrepare ( ) ;
2016-02-25 20:19:27 +00:00
m_fullPath = fullPath ;
m_url = url ;
getFileHead ( ) ;
return true ;
}
bool CDownloader : : getFile ( )
{
if ( m_fullPath . isEmpty ( ) | | m_url . isEmpty ( ) )
{
qDebug ( ) < < " You forget to call prepareFile before " ;
return false ;
}
m_downloadAfterHead = true ;
getFileHead ( ) ;
return true ;
}
void CDownloader : : startTimer ( )
{
stopTimer ( ) ;
2016-09-28 21:31:26 +00:00
m_timer - > setInterval ( 30000 ) ;
2016-02-25 20:19:27 +00:00
m_timer - > setSingleShot ( true ) ;
m_timer - > start ( ) ;
}
void CDownloader : : stopTimer ( )
{
if ( m_timer - > isActive ( ) ) m_timer - > stop ( ) ;
}
bool CDownloader : : openFile ( )
{
closeFile ( ) ;
m_file = new QFile ( m_fullPath ) ;
if ( m_file - > open ( QFile : : Append ) ) return true ;
closeFile ( ) ;
return false ;
}
void CDownloader : : closeFile ( )
{
if ( m_file )
{
m_file - > close ( ) ;
delete m_file ;
m_file = NULL ;
}
}
void CDownloader : : getFileHead ( )
{
if ( m_supportsAcceptRanges )
{
QFileInfo fileInfo ( m_fullPath ) ;
if ( fileInfo . exists ( ) )
{
m_offset = fileInfo . size ( ) ;
}
else
{
m_offset = 0 ;
}
// continue if offset less than size
if ( m_offset > = m_size )
{
if ( checkDownloadedFile ( ) )
{
// file is already downloaded
2016-06-12 12:11:58 +00:00
if ( m_listener ) m_listener - > operationSuccess ( m_size ) ;
2016-06-18 17:52:51 +00:00
emit downloadDone ( ) ;
2016-02-25 20:19:27 +00:00
}
else
{
// or has wrong size
2016-09-16 16:58:33 +00:00
if ( m_listener ) m_listener - > operationFail ( tr ( " File is larger (%1B) than expected (%2B) " ) . arg ( m_offset ) . arg ( m_size ) ) ;
2016-02-25 20:19:27 +00:00
}
return ;
}
}
QNetworkRequest request ( m_url ) ;
request . setHeader ( QNetworkRequest : : UserAgentHeader , " Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0 " ) ;
if ( m_supportsAcceptRanges )
{
request . setRawHeader ( " Range " , QString ( " bytes=%1- " ) . arg ( m_offset ) . toLatin1 ( ) ) ;
}
m_reply = m_manager - > head ( request ) ;
connect ( m_reply , SIGNAL ( finished ( ) ) , SLOT ( onHeadFinished ( ) ) ) ;
connect ( m_reply , SIGNAL ( error ( QNetworkReply : : NetworkError ) ) , SLOT ( onError ( QNetworkReply : : NetworkError ) ) ) ;
startTimer ( ) ;
}
void CDownloader : : downloadFile ( )
{
qint64 freeSpace = NLMISC : : CSystemInfo : : availableHDSpace ( m_fullPath . toUtf8 ( ) . constData ( ) ) ;
2016-10-18 18:34:25 +00:00
if ( freeSpace = = 0 )
{
if ( m_listener )
{
QString error = qFromUtf8 ( NLMISC : : formatErrorMessage ( NLMISC : : getLastError ( ) ) ) ;
m_listener - > operationFail ( tr ( " Error '%1' occured when trying to check free disk space on %2. " ) . arg ( error ) . arg ( m_fullPath ) ) ;
}
return ;
}
2016-02-25 20:19:27 +00:00
if ( freeSpace < m_size - m_offset )
{
// we have not enough free disk space to continue download
2016-09-16 16:58:33 +00:00
if ( m_listener ) m_listener - > operationFail ( tr ( " You only have %1 bytes left on the device, but %2 bytes are needed. " ) . arg ( freeSpace ) . arg ( m_size - m_offset ) ) ;
2016-02-25 20:19:27 +00:00
return ;
}
if ( ! openFile ( ) )
{
2016-06-12 12:11:58 +00:00
if ( m_listener ) m_listener - > operationFail ( tr ( " Unable to write file " ) ) ;
2016-02-25 20:19:27 +00:00
return ;
}
QNetworkRequest request ( m_url ) ;
request . setHeader ( QNetworkRequest : : UserAgentHeader , " Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.17 " ) ;
if ( supportsResume ( ) )
{
request . setRawHeader ( " Range " , QString ( " bytes=%1-%2 " ) . arg ( m_offset ) . arg ( m_size - 1 ) . toLatin1 ( ) ) ;
}
m_reply = m_manager - > get ( request ) ;
connect ( m_reply , SIGNAL ( finished ( ) ) , SLOT ( onDownloadFinished ( ) ) ) ;
connect ( m_reply , SIGNAL ( error ( QNetworkReply : : NetworkError ) ) , SLOT ( onError ( QNetworkReply : : NetworkError ) ) ) ;
connect ( m_reply , SIGNAL ( downloadProgress ( qint64 , qint64 ) ) , SLOT ( onDownloadProgress ( qint64 , qint64 ) ) ) ;
connect ( m_reply , SIGNAL ( readyRead ( ) ) , SLOT ( onDownloadRead ( ) ) ) ;
2016-06-12 12:11:58 +00:00
if ( m_listener ) m_listener - > operationStart ( ) ;
2016-02-25 20:19:27 +00:00
startTimer ( ) ;
}
bool CDownloader : : checkDownloadedFile ( )
{
QFileInfo file ( m_fullPath ) ;
return file . size ( ) = = m_size & & file . lastModified ( ) . toUTC ( ) = = m_lastModified ;
}
void CDownloader : : onTimeout ( )
{
qDebug ( ) < < " Timeout " ;
2016-06-12 12:11:58 +00:00
if ( m_listener ) m_listener - > operationFail ( tr ( " Timeout " ) ) ;
2016-02-25 20:19:27 +00:00
}
void CDownloader : : onHtmlPageFinished ( )
{
QNetworkReply * reply = qobject_cast < QNetworkReply * > ( sender ( ) ) ;
int status = reply - > attribute ( QNetworkRequest : : HttpStatusCodeAttribute ) . toInt ( ) ;
QString html = QString : : fromUtf8 ( reply - > readAll ( ) ) ;
reply - > deleteLater ( ) ;
emit htmlPageContent ( html ) ;
}
void CDownloader : : onHeadFinished ( )
{
stopTimer ( ) ;
int status = m_reply - > attribute ( QNetworkRequest : : HttpStatusCodeAttribute ) . toInt ( ) ;
QString redirection = m_reply - > header ( QNetworkRequest : : LocationHeader ) . toString ( ) ;
m_size = m_reply - > header ( QNetworkRequest : : ContentLengthHeader ) . toInt ( ) ;
m_lastModified = m_reply - > header ( QNetworkRequest : : LastModifiedHeader ) . toDateTime ( ) . toUTC ( ) ;
QString acceptRanges = QString : : fromLatin1 ( m_reply - > rawHeader ( " Accept-Ranges " ) ) ;
QString contentRange = QString : : fromLatin1 ( m_reply - > rawHeader ( " Content-Range " ) ) ;
m_reply - > deleteLater ( ) ;
m_reply = NULL ;
// redirection
if ( status = = 302 )
{
if ( redirection . isEmpty ( ) )
{
2016-06-12 12:11:58 +00:00
if ( m_listener ) m_listener - > operationFail ( tr ( " Redirection URL is not defined " ) ) ;
2016-02-25 20:19:27 +00:00
return ;
}
// redirection on another server, recheck resume
m_supportsAcceptRanges = false ;
m_supportsContentRange = false ;
m_referer = m_url ;
// update real URL
m_url = redirection ;
getFileHead ( ) ;
return ;
}
// we requested without range
else if ( status = = 200 )
{
// update size
2016-06-12 12:11:58 +00:00
if ( m_listener ) m_listener - > operationInit ( 0 , m_size ) ;
2016-02-25 20:19:27 +00:00
if ( ! m_supportsAcceptRanges & & acceptRanges = = " bytes " )
{
// server supports resume, part 1
m_supportsAcceptRanges = true ;
// request range
getFileHead ( ) ;
return ;
}
// server doesn't support resume or
// we requested range, but server always returns 200
// download from the beginning
}
// we requested with a range
else if ( status = = 206 )
{
// server supports resume
QRegExp regexp ( " ^bytes ([0-9]+) - ( [ 0 - 9 ] + ) / ( [ 0 - 9 ] + ) $ " ) ;
if ( m_supportsAcceptRanges & & regexp . exactMatch ( contentRange ) )
{
m_supportsContentRange = true ;
m_offset = regexp . cap ( 1 ) . toLongLong ( ) ;
// when resuming, Content-Length is the size of missing parts to download
m_size = regexp . cap ( 3 ) . toLongLong ( ) ;
// update offset and size
2016-06-12 12:11:58 +00:00
if ( m_listener ) m_listener - > operationInit ( m_offset , m_size ) ;
2016-02-25 20:19:27 +00:00
}
else
{
qDebug ( ) < < " Unable to parse " ;
}
}
2016-09-22 09:40:13 +00:00
// error when download is not yet ready
else if ( status = = 307 )
{
if ( m_listener ) m_listener - > operationFail ( tr ( " File is not available, please retry later (status code: %1) " ) . arg ( status ) ) ;
return ;
}
2016-02-25 20:19:27 +00:00
// other status
else
{
2016-09-16 16:58:33 +00:00
if ( m_listener ) m_listener - > operationFail ( tr ( " Incorrect status code: %1 " ) . arg ( status ) ) ;
2016-02-25 20:19:27 +00:00
return ;
}
if ( m_downloadAfterHead )
{
if ( checkDownloadedFile ( ) )
{
qDebug ( ) < < " same date and size " ;
}
else
{
downloadFile ( ) ;
}
}
2016-06-18 17:52:51 +00:00
else
{
emit downloadPrepared ( ) ;
}
2016-02-25 20:19:27 +00:00
}
void CDownloader : : onDownloadFinished ( )
{
2016-10-16 15:29:10 +00:00
int status = m_reply - > attribute ( QNetworkRequest : : HttpStatusCodeAttribute ) . toInt ( ) ;
2016-02-25 20:19:27 +00:00
m_reply - > deleteLater ( ) ;
m_reply = NULL ;
closeFile ( ) ;
2016-06-12 12:11:58 +00:00
if ( m_listener & & m_listener - > operationShouldStop ( ) )
2016-02-25 20:19:27 +00:00
{
2016-06-12 12:11:58 +00:00
m_listener - > operationStop ( ) ;
2016-02-25 20:19:27 +00:00
}
else
{
2016-10-16 15:29:10 +00:00
if ( QFileInfo ( m_fullPath ) . size ( ) = = m_size )
{
bool ok = NLMISC : : CFile : : setFileModificationDate ( m_fullPath . toUtf8 ( ) . constData ( ) , m_lastModified . toTime_t ( ) ) ;
2016-02-25 20:19:27 +00:00
2016-10-16 15:29:10 +00:00
if ( m_listener ) m_listener - > operationSuccess ( m_size ) ;
2016-06-18 17:52:51 +00:00
2016-10-16 15:29:10 +00:00
emit downloadDone ( ) ;
}
else
{
m_listener - > operationFail ( tr ( " HTTP error: %1 " ) . arg ( status ) ) ;
}
2016-02-25 20:19:27 +00:00
}
}
void CDownloader : : onError ( QNetworkReply : : NetworkError error )
{
2016-06-12 12:11:58 +00:00
if ( ! m_listener ) return ;
2016-02-25 20:19:27 +00:00
if ( error = = QNetworkReply : : OperationCanceledError )
{
2016-06-12 12:11:58 +00:00
m_listener - > operationStop ( ) ;
2016-02-25 20:19:27 +00:00
}
}
void CDownloader : : onDownloadProgress ( qint64 current , qint64 total )
{
stopTimer ( ) ;
2016-06-12 12:11:58 +00:00
if ( ! m_listener ) return ;
2016-06-18 17:52:51 +00:00
m_listener - > operationProgress ( m_offset + current , m_url ) ;
2016-06-12 12:11:58 +00:00
// abort download
if ( m_listener - > operationShouldStop ( ) & & m_reply ) m_reply - > abort ( ) ;
2016-02-25 20:19:27 +00:00
}
void CDownloader : : onDownloadRead ( )
{
if ( m_file ) m_file - > write ( m_reply - > readAll ( ) ) ;
}