Skip to content

Commit

Permalink
Private links: Account for overwritehost #6146
Browse files Browse the repository at this point in the history
Retrieve ocs/v1.php/config to determine the desired host and build the
private link urls with that instead.
  • Loading branch information
ckamm committed Nov 14, 2017
1 parent 18091f9 commit b0f986c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/libsync/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ QUrl Account::davUrl() const

QUrl Account::deprecatedPrivateLinkUrl(const QByteArray &numericFileId) const
{
return Utility::concatUrlPath(url(),
return Utility::concatUrlPath(_userVisibleUrl,
QLatin1String("/index.php/f/") + QUrl::toPercentEncoding(QString::fromLatin1(numericFileId)));
}

Expand Down Expand Up @@ -311,6 +311,12 @@ void Account::setSslErrorHandler(AbstractSslErrorHandler *handler)
void Account::setUrl(const QUrl &url)
{
_url = url;
_userVisibleUrl = url;
}

void Account::setUserVisibleHost(const QString &host)
{
_userVisibleUrl.setHost(host);
}

QVariant Account::credentialSetting(const QString &key) const
Expand Down
12 changes: 12 additions & 0 deletions src/libsync/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject
void setUrl(const QUrl &url);
QUrl url() const { return _url; }

/// Adjusts _userVisibleUrl once the host to use is discovered.
void setUserVisibleHost(const QString &host);

/**
* @brief The possibly themed dav path for the account. It has
* a trailing slash.
Expand Down Expand Up @@ -266,6 +269,15 @@ protected Q_SLOTS:
QImage _avatarImg;
QMap<QString, QVariant> _settingsMap;
QUrl _url;

/** If url to use for any user-visible urls.
*
* If the server configures overwritehost this can be different from
* the connection url in _url. We retrieve the visible host through
* the ocs/v1.php/config endpoint in ConnectionValidator.
*/
QUrl _userVisibleUrl;

QList<QSslCertificate> _approvedCerts;
QSslConfiguration _sslConfiguration;
Capabilities _capabilities;
Expand Down
24 changes: 24 additions & 0 deletions src/libsync/connectionvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QNetworkReply>
#include <QNetworkProxyFactory>
#include <QPixmap>
#include <QXmlStreamReader>

#include "connectionvalidator.h"
#include "account.h"
Expand Down Expand Up @@ -247,10 +248,22 @@ void ConnectionValidator::slotAuthSuccess()

void ConnectionValidator::checkServerCapabilities()
{
// The main flow now needs the capabilities
JsonApiJob *job = new JsonApiJob(_account, QLatin1String("ocs/v1.php/cloud/capabilities"), this);
job->setTimeout(timeoutToUseMsec);
QObject::connect(job, &JsonApiJob::jsonReceived, this, &ConnectionValidator::slotCapabilitiesRecieved);
job->start();

// And we'll retrieve the ocs config in parallel
// note that 'this' might be destroyed before the job finishes, so intentionally not parented
auto configJob = new JsonApiJob(_account, QLatin1String("ocs/v1.php/config"));
configJob->setTimeout(timeoutToUseMsec);
auto account = _account; // capturing account by value will make it live long enough
QObject::connect(configJob, &JsonApiJob::jsonReceived, _account.data(),
[=](const QJsonDocument &json) {
ocsConfigReceived(json, account);
});
configJob->start();
}

void ConnectionValidator::slotCapabilitiesRecieved(const QJsonDocument &json)
Expand All @@ -268,6 +281,17 @@ void ConnectionValidator::slotCapabilitiesRecieved(const QJsonDocument &json)
fetchUser();
}

void ConnectionValidator::ocsConfigReceived(const QJsonDocument &json, AccountPtr account)
{
QString host = json.object().value("ocs").toObject().value("data").toObject().value("host").toString();
if (host.isEmpty()) {
qCWarning(lcConnectionValidator) << "Could not extract 'host' from ocs config reply";
return;
}
qCInfo(lcConnectionValidator) << "Determined user-visible host to be" << host;
account->setUserVisibleHost(host);
}

void ConnectionValidator::fetchUser()
{
JsonApiJob *job = new JsonApiJob(_account, QLatin1String("ocs/v1.php/cloud/user"), this);
Expand Down
7 changes: 4 additions & 3 deletions src/libsync/connectionvalidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ namespace OCC {
|
+---------------------------+
|
+-> checkServerCapabilities (cloud/capabilities)
JsonApiJob
|
+-> checkServerCapabilities --------------v (in parallel)
JsonApiJob (cloud/capabilities) JsonApiJob (ocs/v1.php/config)
| +-> ocsConfigReceived
+-> slotCapabilitiesRecieved -+
|
+-----------------------------------+
Expand Down Expand Up @@ -129,6 +129,7 @@ protected slots:
void reportResult(Status status);
void checkServerCapabilities();
void fetchUser();
static void ocsConfigReceived(const QJsonDocument &json, AccountPtr account);

/** Sets the account's server version
*
Expand Down

0 comments on commit b0f986c

Please sign in to comment.