Revert "Do not store the redirected URL in the config file."

This reverts commit 905f70a186.

Patch still has issues, defer until after Beta 4
This commit is contained in:
Daniel Molkentin 2013-06-20 19:50:34 +02:00
parent 905f70a186
commit 0bc9b6f44e
9 changed files with 77 additions and 63 deletions

View file

@ -321,7 +321,7 @@ void Application::slotCheckAuthentication()
this,SLOT(slotAuthCheck(QString,QNetworkReply*)));
qDebug() << "# checking for authentication settings.";
ownCloudInfo::instance()->getWebDAVPath(QLatin1String("/") ); // this call needs to be authenticated.
ownCloudInfo::instance()->getRequest(QLatin1String("/"), true ); // this call needs to be authenticated.
// simply GET the webdav root, will fail if credentials are wrong.
// continue in slotAuthCheck here :-)
}

View file

@ -163,7 +163,7 @@ void ConnectionValidator::slotCheckAuthentication()
this, SLOT(slotAuthCheck(QString,QNetworkReply*)));
qDebug() << "# checking for authentication settings.";
ownCloudInfo::instance()->getWebDAVPath(QLatin1String("/") ); // this call needs to be authenticated.
ownCloudInfo::instance()->getRequest(QLatin1String("/"), true ); // this call needs to be authenticated.
// simply GET the webdav root, will fail if credentials are wrong.
// continue in slotAuthCheck here :-)
}

View file

@ -18,7 +18,6 @@
#include "mirall/syncresult.h"
#include "mirall/inotify.h"
#include "mirall/theme.h"
#include "owncloudinfo.h"
#ifdef Q_OS_MAC
#include <CoreServices/CoreServices.h>
@ -250,8 +249,10 @@ Folder* FolderMan::setupFolderFromConfigFile(const QString &file) {
if (!backend.isEmpty()) {
if( backend == QLatin1String("owncloud") ) {
MirallConfigFile cfgFile;
// assemble the owncloud url to pass to csync, incl. webdav
QString oCUrl = ownCloudInfo::instance()->webdavUrl( );
QString oCUrl = cfgFile.ownCloudUrl( QString::null, true );
// cut off the leading slash, oCUrl always has a trailing.
if( targetPath.startsWith(QLatin1Char('/')) ) {

View file

@ -228,6 +228,19 @@ bool MirallConfigFile::writePassword( const QString& passwd, const QString& conn
return true;
}
// set the url, called from redirect handling.
void MirallConfigFile::setOwnCloudUrl( const QString& connection, const QString & url )
{
const QString file = configFile();
QSettings settings( file, QSettings::IniFormat);
settings.setIniCodec( "UTF-8" );
settings.beginGroup( connection );
settings.setValue( QLatin1String("url"), url );
settings.sync();
}
QByteArray MirallConfigFile::caCerts( )
{
QSettings settings( configFile(), QSettings::IniFormat );

View file

@ -58,6 +58,8 @@ public:
QString ownCloudUser( const QString& connection = QString() ) const;
QString ownCloudUrl( const QString& connection = QString(), bool webdav = false ) const;
void setOwnCloudUrl(const QString &connection, const QString& );
// the certs do not depend on a connection.
QByteArray caCerts();
void setCaCerts( const QByteArray& );

View file

@ -124,16 +124,17 @@ void ownCloudFolder::setProxy()
{
if( _csync_ctx ) {
/* Store proxy */
QUrl proxyUrl(ownCloudInfo::instance()->webdavUrl());
MirallConfigFile cfgFile;
QUrl proxyUrl(cfgFile.ownCloudUrl());
QList<QNetworkProxy> proxies = QNetworkProxyFactory::proxyForQuery(proxyUrl);
// We set at least one in Application
Q_ASSERT(proxies.count() > 0);
QNetworkProxy proxy = proxies.first();
if (proxy.type() == QNetworkProxy::NoProxy) {
qDebug() << "Passing NO proxy to csync for" << proxyUrl;
qDebug() << "Passing NO proxy to csync for" << cfgFile.ownCloudUrl();
} else {
qDebug() << "Passing" << proxy.hostName() << "of proxy type " << proxy.type()
<< " to csync for" << proxyUrl;
<< " to csync for" << cfgFile.ownCloudUrl();
}
int proxyPort = proxy.port();
@ -240,7 +241,8 @@ bool ownCloudFolder::isBusy() const
QString ownCloudFolder::secondPath() const
{
QString re(Folder::secondPath());
QString ocUrl = ownCloudInfo::instance()->webdavUrl();
MirallConfigFile cfg;
QString ocUrl = cfg.ownCloudUrl(QString::null, true);
if (ocUrl.endsWith(QLatin1Char('/')))
ocUrl.chop(1);

View file

@ -116,32 +116,28 @@ bool ownCloudInfo::isConfigured()
QNetworkReply *ownCloudInfo::checkInstallation()
{
_redirectCount = 0;
MirallConfigFile cfgFile( _configHandle );
QUrl url ( cfgFile.ownCloudUrl( _connection ) + QLatin1String("status.php") );
/* No authentication required for this. */
return getRequest(url);
return getRequest( QLatin1String("status.php"), false );
}
QNetworkReply* ownCloudInfo::getWebDAVPath( const QString& path )
{
_redirectCount = 0;
QUrl url ( webdavUrl( _connection ) + path );
QNetworkReply *reply = getRequest(url);
_directories[reply] = path;
return reply;
return getRequest( path, true );
}
QNetworkReply* ownCloudInfo::getRequest( const QUrl& url )
QNetworkReply* ownCloudInfo::getRequest( const QString& path, bool webdav )
{
qDebug() << "Get Request to " << url;
qDebug() << "Get Request to " << path;
MirallConfigFile cfgFile( _configHandle );
QString url = cfgFile.ownCloudUrl( _connection, webdav ) + path;
QNetworkRequest request;
request.setUrl( url );
request.setUrl( QUrl( url ) );
setupHeaders( request, 0 );
QNetworkReply *reply = _manager->get( request );
connect( reply, SIGNAL(finished()), SLOT(slotReplyFinished()));
_directories[reply] = path;
if( !_configHandle.isEmpty() ) {
qDebug() << "Setting config handle " << _configHandle;
@ -159,7 +155,7 @@ QNetworkReply* ownCloudInfo::mkdirRequest( const QString& dir )
qDebug() << "OCInfo Making dir " << dir;
MirallConfigFile cfgFile( _configHandle );
QUrl url = QUrl( webdavUrl(_connection) + dir );
QUrl url = QUrl( cfgFile.ownCloudUrl( _connection, true ) + dir );
QHttp::ConnectionMode conMode = QHttp::ConnectionModeHttp;
if (url.scheme() == "https")
conMode = QHttp::ConnectionModeHttps;
@ -324,6 +320,20 @@ QList<QSslCertificate> ownCloudInfo::certificateChain() const
return _certificateChain;
}
QUrl ownCloudInfo::redirectUrl(const QUrl& possibleRedirectUrl,
const QUrl& oldRedirectUrl) const {
QUrl redirectUrl;
/*
* Check if the URL is empty and
* that we aren't being fooled into a infinite redirect loop.
*/
if(!possibleRedirectUrl.isEmpty() &&
possibleRedirectUrl != oldRedirectUrl) {
redirectUrl = possibleRedirectUrl;
}
return redirectUrl;
}
//
// There have been problems with the finish-signal coming from the networkmanager.
// To avoid that, the reply-signals were connected and the data is taken from the
@ -344,17 +354,12 @@ void ownCloudInfo::slotReplyFinished()
}
// Detect redirect url
QUrl possibleRedirUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
QVariant possibleRedirUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
/* We'll deduct if the redirection is valid in the redirectUrl function */
_urlRedirectedTo = redirectUrl( possibleRedirUrl.toUrl(),
_urlRedirectedTo );
if (!possibleRedirUrl.isEmpty() && _redirectCount++ > 10) {
// Are we in a redirect loop
qDebug() << "Redirect loop while redirecting to" << possibleRedirUrl;
possibleRedirUrl.clear();
}
if(!possibleRedirUrl.isEmpty()) {
if(!_urlRedirectedTo.isEmpty()) {
QString configHandle;
qDebug() << "Redirected to " << possibleRedirUrl;
@ -369,21 +374,22 @@ void ownCloudInfo::slotReplyFinished()
QString path = _directories[reply];
qDebug() << "This path was redirected: " << path;
QString newUrl = possibleRedirUrl.toString();
MirallConfigFile cfgFile( configHandle );
QString newUrl = _urlRedirectedTo.toString();
if( newUrl.endsWith( path )) {
// cut off the trailing path
newUrl.chop( path.length() );
_urlRedirectedTo = newUrl;
qDebug() << "Updated url to" << newUrl;
cfgFile.setOwnCloudUrl( _connection, newUrl );
qDebug() << "Update the config file url to " << newUrl;
getRequest( path, false ); // FIXME: Redirect for webdav!
reply->deleteLater();
return;
} else {
qDebug() << "WRN: Path is not part of the redirect URL. NO redirect.";
}
getRequest( possibleRedirUrl );
reply->deleteLater();
_directories.remove(reply);
_configHandleMap.remove(reply);
return;
}
_urlRedirectedTo.clear();
// TODO: check if this is always the correct encoding
const QString version = QString::fromUtf8( reply->readAll() );
@ -449,13 +455,15 @@ void ownCloudInfo::slotReplyFinished()
QString dir(QLatin1String("unknown"));
if( _directories.contains(reply) ) {
dir = _directories[reply];
_directories.remove(reply);
}
emit ownCloudDirExists( dir, reply );
}
if( _configHandleMap.contains(reply)) {
_configHandleMap.remove(reply);
}
reply->deleteLater();
_directories.remove(reply);
_configHandleMap.remove(reply);
}
void ownCloudInfo::resetSSLUntrust()
@ -528,7 +536,9 @@ void ownCloudInfo::setCredentials( const QString& user, const QString& passwd,
// ============================================================================
void ownCloudInfo::setupHeaders( QNetworkRequest & req, quint64 size )
{
QUrl url( webdavUrl() );
MirallConfigFile cfgFile(_configHandle );
QUrl url( cfgFile.ownCloudUrl( QString::null, false ) );
qDebug() << "Setting up host header: " << url.host();
req.setRawHeader( QByteArray("Host"), url.host().toUtf8() );
req.setRawHeader( QByteArray("User-Agent"), Utility::userAgentString());
@ -562,14 +572,5 @@ QNetworkReply* ownCloudInfo::davRequest(const QString& reqVerb, QNetworkRequest
}
}
#endif
QString ownCloudInfo::webdavUrl(const QString &connection)
{
if (!_urlRedirectedTo.isEmpty())
return _urlRedirectedTo.toString();
MirallConfigFile cfgFile(_configHandle );
return cfgFile.ownCloudUrl( connection, true );
}
}

View file

@ -47,6 +47,12 @@ public:
*/
QNetworkReply* checkInstallation();
/**
* a general GET request to the ownCloud. If the second bool parameter is
* true, the WebDAV server is queried.
*/
QNetworkReply* getRequest( const QString&, bool );
/**
* convenience: GET request to the WebDAV server.
*/
@ -104,12 +110,6 @@ public:
void setCredentials( const QString&, const QString&,
const QString& configHandle = QString::null );
/**
* returns the owncloud webdav url.
* It may be different from the one in the config if there was a HTTP redirection
*/
QString webdavUrl(const QString& connection = QString());
signals:
// result signal with url- and version string.
void ownCloudInfoFound( const QString&, const QString&, const QString&, const QString& );
@ -138,11 +138,7 @@ protected slots:
private:
explicit ownCloudInfo();
/**
* a general GET request to the ownCloud WebDAV.
*/
QNetworkReply* getRequest( const QUrl &url);
QUrl redirectUrl(const QUrl&, const QUrl& ) const;
~ownCloudInfo();
@ -165,7 +161,6 @@ private:
int _authAttempts;
QMap<QString, oCICredentials> _credentials;
QMutex _certChainMutex;
int _redirectCount;
};
};

View file

@ -329,7 +329,7 @@ void OwncloudSetupWizard::checkRemoteFolder()
qDebug() << "# checking for authentication settings.";
ownCloudInfo::instance()->setCustomConfigHandle(_configHandle);
_checkRemoteFolderRequest = ownCloudInfo::instance()->getWebDAVPath(_remoteFolder ); // this call needs to be authenticated.
_checkRemoteFolderRequest = ownCloudInfo::instance()->getRequest(_remoteFolder, true ); // this call needs to be authenticated.
// continue in slotAuthCheckReply
}