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 <tobias.doerffel@gmail.com>
This commit is contained in:
Tobias Doerffel
2009-07-17 11:44:57 +02:00
parent 1a19b60e97
commit e115124075
2 changed files with 42 additions and 30 deletions

View File

@@ -25,14 +25,13 @@
#ifndef _WEB_RESOURCE_PROVIDER_H
#define _WEB_RESOURCE_PROVIDER_H
#include <QtCore/QBuffer>
#include <QtXml/QDomNode>
#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<int> m_downloadIDs;
} ;

View File

@@ -22,6 +22,7 @@
*
*/
#include <QtCore/QBuffer>
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QUrl>
@@ -35,15 +36,8 @@ QList<int> 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 );
}
}