From e115124075ccf777eaeccc1a2d6a81529a4c1951 Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Fri, 17 Jul 2009 11:44:57 +0200 Subject: [PATCH] WebResourcesProvider: support for HTTP forwardings + cleanups Properly handle HTTP status code 302 which indicates a moved site, i.e. try to fetch data from the new location. This finally makes WebResources work again with LSP being moved to http://lmms.info. Furthermore cleaned up code a bit. There's no need for a global QHttp object or a global QBuffer object. Instead allocate them on the stack in the according methods. Furthermore removed unused/incomplete support for non-blocking download. We should introduce support for a ProgressDialog solution (also allowing to cancel operations) later. Signed-off-by: Tobias Doerffel --- include/WebResourceProvider.h | 14 +++----- src/core/WebResourceProvider.cpp | 58 ++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/include/WebResourceProvider.h b/include/WebResourceProvider.h index 571c2a1a7..12976cf76 100644 --- a/include/WebResourceProvider.h +++ b/include/WebResourceProvider.h @@ -25,14 +25,13 @@ #ifndef _WEB_RESOURCE_PROVIDER_H #define _WEB_RESOURCE_PROVIDER_H -#include #include #include "ResourceProvider.h" #include "ResourceItem.h" -class QHttp; +class QBuffer; class WebResourceProvider : public ResourceProvider @@ -42,12 +41,12 @@ public: WebResourceProvider( const QString & _url ); virtual ~WebResourceProvider(); - virtual QString providerName( void ) const + virtual QString providerName() const { return "WebResourceProvider"; } - virtual void updateDatabase( void ); + virtual void updateDatabase(); virtual int dataSize( const ResourceItem * _item ) const { @@ -58,7 +57,7 @@ public: virtual QByteArray fetchData( const ResourceItem * _item, int _maxSize = -1 ) const; - virtual bool isLocal( void ) const + virtual bool isLocal() const { return false; } @@ -73,11 +72,8 @@ private: ResourceItem * _item ); void importNodeIntoDB( const QDomNode & n, ResourceTreeItem * _parent ); - void download( const QString & _path, QBuffer * _target, - bool _wait = false ) const; + void download( const QString & _path, QBuffer * _target ) const; - QHttp * m_http; - QBuffer m_indexBuffer; static QList m_downloadIDs; } ; diff --git a/src/core/WebResourceProvider.cpp b/src/core/WebResourceProvider.cpp index 414fe43d6..baecfaaee 100644 --- a/src/core/WebResourceProvider.cpp +++ b/src/core/WebResourceProvider.cpp @@ -22,6 +22,7 @@ * */ +#include #include #include #include @@ -35,15 +36,8 @@ QList WebResourceProvider::m_downloadIDs; WebResourceProvider::WebResourceProvider( const QString & _url ) : - ResourceProvider( _url ), - m_http( NULL ), - m_indexBuffer() + ResourceProvider( _url ) { - QUrl u( _url ); - m_http = new QHttp( u.host(), u.port() > 0 ? u.port() : 80, this ); - connect( m_http, SIGNAL( requestFinished( int, bool ) ), - this, SLOT( finishDownload( int, bool ) ) ); - database()->init(); } @@ -58,16 +52,16 @@ WebResourceProvider::~WebResourceProvider() -void WebResourceProvider::updateDatabase( void ) +void WebResourceProvider::updateDatabase() { - m_indexBuffer.close(); - m_indexBuffer.open( QBuffer::ReadWrite ); - download( "/WebResources/Index", &m_indexBuffer, true ); + QBuffer indexBuffer; + indexBuffer.open( QBuffer::ReadWrite ); + download( url() + "/WebResources/Index", &indexBuffer ); - m_indexBuffer.seek( 0 ); + indexBuffer.seek( 0 ); QDomDocument doc; - doc.setContent( &m_indexBuffer ); + doc.setContent( &indexBuffer ); importNodeIntoDB( doc.firstChildElement( "webresources" ), database()->topLevelNode() ); @@ -82,7 +76,7 @@ QByteArray WebResourceProvider::fetchData( const ResourceItem * _item, QBuffer buffer; buffer.open( QBuffer::ReadWrite ); - download( "/WebResources/" + _item->hash(), &buffer, true ); + download( url() + "/WebResources/" + _item->hash(), &buffer ); return buffer.data(); } @@ -198,17 +192,39 @@ void WebResourceProvider::importNodeIntoDB( const QDomNode & _n, void WebResourceProvider::download( const QString & _path, - QBuffer * _target, bool _wait ) const + QBuffer * _target ) const { - const int id = m_http->get( _path, _target ); + // create local http object; + QHttp http; + connect( &http, SIGNAL( requestFinished( int, bool ) ), + this, SLOT( finishDownload( int, bool ) ) ); - if( _wait ) + // set current URL for http object + QUrl u( _path ); + http.setHost( u.host(), u.port() > 0 ? u.port() : 80 ); + + // start the download + const int id = http.get( _path, _target ); + + // wait for the download to finish + while( !m_downloadIDs.contains( id ) ) { - while( !m_downloadIDs.contains( id ) ) + QCoreApplication::instance()->processEvents(); + } + m_downloadIDs.removeAll( id ); + + if( http.lastResponse().statusCode() == 302 ) + { + const QString newLocation = + http.lastResponse().value( "location" ); + if( newLocation != _path ) { - QCoreApplication::instance()->processEvents(); + qDebug() << "HTTP forwarding to" << newLocation; + + _target->seek( 0 ); + _target->buffer().clear(); + download( newLocation, _target ); } - m_downloadIDs.removeAll( id ); } }