Handle some media links without scheme in offline wikipedia/wiktionary via ArticleNetworkAccessManager

This commit is contained in:
Abs62 2022-12-22 17:57:43 +03:00
parent 02c02829f7
commit cd57031d44

View file

@ -238,35 +238,46 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op,
QNetworkRequest const & req,
QIODevice * outgoingData )
{
QNetworkRequest localReq( req );
if( localReq.url().scheme() == "gdlookup" && localReq.url().host() == "upload.wikimedia.org" )
{
// Handle some requests from offline wikipedia/wiktionary without scheme
QUrl newUrl( req.url() );
newUrl.setScheme( "https" );
localReq.setUrl( newUrl );
}
if ( op == GetOperation )
{
if ( req.url().scheme() == "qrcx" )
if ( localReq.url().scheme() == "qrcx" )
{
// We have to override the local load policy for the qrc scheme, hence
// we use qrcx and redirect it here back to qrc
QUrl newUrl( req.url() );
QUrl newUrl( localReq.url() );
newUrl.setScheme( "qrc" );
newUrl.setHost( "" );
QNetworkRequest newReq( req );
newReq.setUrl( newUrl );
localReq.setUrl( newUrl );
return QNetworkAccessManager::createRequest( op, newReq, outgoingData );
return QNetworkAccessManager::createRequest( op, localReq, outgoingData );
}
#if QT_VERSION >= 0x050300 // Qt 5.3+
// Workaround of same-origin policy
if( ( req.url().scheme().startsWith( "http" ) || req.url().scheme() == "ftp" )
&& req.hasRawHeader( "Referer" ) )
if( ( localReq.url().scheme().startsWith( "http" ) || localReq.url().scheme() == "ftp" )
&& localReq.hasRawHeader( "Referer" ) )
{
QByteArray referer = req.rawHeader( "Referer" );
QByteArray referer = localReq.rawHeader( "Referer" );
QUrl refererUrl = QUrl::fromEncoded( referer );
if( refererUrl.scheme().startsWith( "http") || refererUrl.scheme() == "ftp" )
{
// Only for pages from network resources
if ( !req.url().host().endsWith( refererUrl.host() ) )
if ( !localReq.url().host().endsWith( refererUrl.host() ) )
{
QUrl frameUrl;
frameUrl.setScheme( refererUrl.scheme() );
@ -277,7 +288,7 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op,
if( !value.origin )
value.setOrigin( frameUrl );
QPair< QString, QString > target( req.url().scheme(), req.url().host() );
QPair< QString, QString > target( localReq.url().scheme(), localReq.url().host() );
if( value.hostsToAccess.find( target ) == value.hostsToAccess.end() )
{
value.hostsToAccess.insert( target );
@ -291,68 +302,66 @@ QNetworkReply * ArticleNetworkAccessManager::createRequest( Operation op,
QString contentType;
sptr< Dictionary::DataRequest > dr = getResource( req.url(), contentType );
sptr< Dictionary::DataRequest > dr = getResource( localReq.url(), contentType );
if ( dr.get() )
return new ArticleResourceReply( this, req, dr, contentType );
return new ArticleResourceReply( this, localReq, dr, contentType );
}
// Check the Referer. If the user has opted-in to block elements from external
// pages, we block them.
if ( disallowContentFromOtherSites && req.hasRawHeader( "Referer" ) )
if ( disallowContentFromOtherSites && localReq.hasRawHeader( "Referer" ) )
{
QByteArray referer = req.rawHeader( "Referer" );
QByteArray referer = localReq.rawHeader( "Referer" );
//DPRINTF( "Referer: %s\n", referer.data() );
QUrl refererUrl = QUrl::fromEncoded( referer );
//DPRINTF( "Considering %s vs %s\n", getHostBase( req.url() ).toUtf8().data(),
//DPRINTF( "Considering %s vs %s\n", getHostBase( localReq.url() ).toUtf8().data(),
// getHostBase( refererUrl ).toUtf8().data() );
if ( !req.url().host().endsWith( refererUrl.host() ) &&
getHostBase( req.url() ) != getHostBase( refererUrl ) && !req.url().scheme().startsWith("data") )
if ( !localReq.url().host().endsWith( refererUrl.host() ) &&
getHostBase( localReq.url() ) != getHostBase( refererUrl ) && !localReq.url().scheme().startsWith("data") )
{
gdWarning( "Blocking element \"%s\"\n", req.url().toEncoded().data() );
gdWarning( "Blocking element \"%s\"\n", localReq.url().toEncoded().data() );
return new BlockedNetworkReply( this );
}
}
if( req.url().scheme() == "file" )
if( localReq.url().scheme() == "file" )
{
// Check file presence and adjust path if necessary
QString fileName = req.url().toLocalFile();
if( req.url().host().isEmpty() && articleMaker.adjustFilePath( fileName ) )
QString fileName = localReq.url().toLocalFile();
if( localReq.url().host().isEmpty() && articleMaker.adjustFilePath( fileName ) )
{
QUrl newUrl( req.url() );
QUrl newUrl( localReq.url() );
QUrl localUrl = QUrl::fromLocalFile( fileName );
newUrl.setHost( localUrl.host() );
newUrl.setPath( Qt4x5::Url::ensureLeadingSlash( localUrl.path() ) );
QNetworkRequest newReq( req );
newReq.setUrl( newUrl );
localReq.setUrl( newUrl );
return QNetworkAccessManager::createRequest( op, newReq, outgoingData );
return QNetworkAccessManager::createRequest( op, localReq, outgoingData );
}
}
QNetworkReply *reply = 0;
// spoof User-Agent
if ( hideGoldenDictHeader && req.url().scheme().startsWith("http", Qt::CaseInsensitive))
if ( hideGoldenDictHeader && localReq.url().scheme().startsWith("http", Qt::CaseInsensitive))
{
QNetworkRequest newReq( req );
newReq.setRawHeader("User-Agent", req.rawHeader("User-Agent").replace(qApp->applicationName(), ""));
reply = QNetworkAccessManager::createRequest( op, newReq, outgoingData );
localReq.setRawHeader("User-Agent", localReq.rawHeader("User-Agent").replace(qApp->applicationName(), ""));
reply = QNetworkAccessManager::createRequest( op, localReq, outgoingData );
}
if( !reply )
reply = QNetworkAccessManager::createRequest( op, req, outgoingData );
reply = QNetworkAccessManager::createRequest( op, localReq, outgoingData );
if( req.url().scheme() == "https")
if( localReq.url().scheme() == "https")
{
#ifndef QT_NO_OPENSSL
connect( reply, SIGNAL( sslErrors( QList< QSslError > ) ),