2013-10-18 14:24:29 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
#include <QLoggingCategory>
|
2013-10-18 14:24:29 +04:00
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkAccessManager>
|
2013-10-28 23:01:59 +04:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
2013-10-24 02:29:08 +04:00
|
|
|
#include <QSslConfiguration>
|
2015-08-11 13:18:25 +03:00
|
|
|
#include <QSslCipher>
|
2013-10-21 23:42:52 +04:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
#include <QStringList>
|
2013-10-23 16:48:44 +04:00
|
|
|
#include <QStack>
|
2013-11-14 20:02:41 +04:00
|
|
|
#include <QTimer>
|
2013-11-22 17:01:11 +04:00
|
|
|
#include <QMutex>
|
2014-01-28 14:06:40 +04:00
|
|
|
#include <QCoreApplication>
|
2017-04-26 13:15:33 +03:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2017-11-21 12:18:15 +03:00
|
|
|
#include <QPainter>
|
2013-10-18 14:24:29 +04:00
|
|
|
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "networkjobs.h"
|
|
|
|
#include "account.h"
|
2014-12-10 15:01:36 +03:00
|
|
|
#include "owncloudpropagator.h"
|
2013-10-18 14:24:29 +04:00
|
|
|
|
2013-11-22 17:01:11 +04:00
|
|
|
#include "creds/abstractcredentials.h"
|
2017-09-07 15:58:45 +03:00
|
|
|
#include "creds/httpcredentials.h"
|
2013-10-28 23:01:59 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2013-10-18 14:24:29 +04:00
|
|
|
|
2017-03-30 14:46:20 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcEtagJob, "sync.networkjob.etag", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcLsColJob, "sync.networkjob.lscol", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcCheckServerJob, "sync.networkjob.checkserver", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcPropfindJob, "sync.networkjob.propfind", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcAvatarJob, "sync.networkjob.avatar", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcMkColJob, "sync.networkjob.mkcol", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcProppatchJob, "sync.networkjob.proppatch", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcJsonApiJob, "sync.networkjob.jsonapi", QtInfoMsg)
|
2017-09-07 15:58:45 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcDetermineAuthTypeJob, "sync.networkjob.determineauthtype", QtInfoMsg)
|
2017-05-09 15:24:11 +03:00
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
RequestEtagJob::RequestEtagJob(AccountPtr account, const QString &path, QObject *parent)
|
2013-10-23 16:48:44 +04:00
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-11-14 22:20:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestEtagJob::start()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-10-23 16:48:44 +04:00
|
|
|
QNetworkRequest req;
|
2015-10-16 12:52:27 +03:00
|
|
|
if (_account && _account->rootEtagChangesNotOnlySubFolderEtags()) {
|
|
|
|
// Fixed from 8.1 https://github.com/owncloud/client/issues/3730
|
|
|
|
req.setRawHeader("Depth", "0");
|
|
|
|
} else {
|
|
|
|
// Let's always request all entries inside a directory. There are/were bugs in the server
|
|
|
|
// where a root or root-folder ETag is not updated when its contents change. We work around
|
|
|
|
// this by concatenating the ETags of the root and its contents.
|
|
|
|
req.setRawHeader("Depth", "1");
|
|
|
|
// See https://github.com/owncloud/core/issues/5255 and others
|
|
|
|
}
|
2015-03-05 19:49:12 +03:00
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
QByteArray xml("<?xml version=\"1.0\" ?>\n"
|
|
|
|
"<d:propfind xmlns:d=\"DAV:\">\n"
|
|
|
|
" <d:prop>\n"
|
2013-10-23 16:48:44 +04:00
|
|
|
" <d:getetag/>\n"
|
2013-10-18 14:24:29 +04:00
|
|
|
" </d:prop>\n"
|
|
|
|
"</d:propfind>\n");
|
2013-11-14 22:20:09 +04:00
|
|
|
QBuffer *buf = new QBuffer(this);
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setData(xml);
|
|
|
|
buf->open(QIODevice::ReadOnly);
|
|
|
|
// assumes ownership
|
2017-03-03 13:20:53 +03:00
|
|
|
sendRequest("PROPFIND", makeDavUrl(path()), req, buf);
|
2013-10-18 14:24:29 +04:00
|
|
|
|
|
|
|
if (reply()->error() != QNetworkReply::NoError) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcEtagJob) << "request network error: " << reply()->errorString();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2014-04-14 17:08:43 +04:00
|
|
|
bool RequestEtagJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcEtagJob) << "Request Etag of" << reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< reply()->error()
|
|
|
|
<< (reply()->error() == QNetworkReply::NoError ? QLatin1String("") : errorString());
|
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
if (reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 207) {
|
|
|
|
// Parse DAV response
|
|
|
|
QXmlStreamReader reader(reply());
|
|
|
|
reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
|
|
|
|
QString etag;
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
|
|
|
if (type == QXmlStreamReader::StartElement && reader.namespaceUri() == QLatin1String("DAV:")) {
|
|
|
|
QString name = reader.name().toString();
|
|
|
|
if (name == QLatin1String("getetag")) {
|
|
|
|
etag += reader.readElementText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit etagRetreived(etag);
|
|
|
|
}
|
2014-04-14 17:08:43 +04:00
|
|
|
return true;
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
MkColJob::MkColJob(AccountPtr account, const QString &path, QObject *parent)
|
2013-10-23 16:48:44 +04:00
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-11-14 22:20:09 +04:00
|
|
|
}
|
|
|
|
|
2016-09-16 17:36:46 +03:00
|
|
|
MkColJob::MkColJob(AccountPtr account, const QUrl &url,
|
|
|
|
const QMap<QByteArray, QByteArray> &extraHeaders, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, QString(), parent)
|
|
|
|
, _url(url)
|
|
|
|
, _extraHeaders(extraHeaders)
|
2016-08-02 14:48:56 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-14 22:20:09 +04:00
|
|
|
void MkColJob::start()
|
|
|
|
{
|
2015-05-20 15:03:37 +03:00
|
|
|
// add 'Content-Length: 0' header (see https://github.com/owncloud/client/issues/3256)
|
|
|
|
QNetworkRequest req;
|
|
|
|
req.setRawHeader("Content-Length", "0");
|
2016-09-16 17:36:46 +03:00
|
|
|
for (auto it = _extraHeaders.constBegin(); it != _extraHeaders.constEnd(); ++it) {
|
|
|
|
req.setRawHeader(it.key(), it.value());
|
|
|
|
}
|
2015-05-20 15:03:37 +03:00
|
|
|
|
|
|
|
// assumes ownership
|
2017-03-03 13:20:53 +03:00
|
|
|
if (_url.isValid()) {
|
|
|
|
sendRequest("MKCOL", _url, req);
|
|
|
|
} else {
|
|
|
|
sendRequest("MKCOL", makeDavUrl(path()), req);
|
|
|
|
}
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2014-04-14 17:08:43 +04:00
|
|
|
bool MkColJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcMkColJob) << "MKCOL of" << reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< reply()->error()
|
|
|
|
<< (reply()->error() == QNetworkReply::NoError ? QLatin1String("") : errorString());
|
|
|
|
|
2013-10-28 23:01:59 +04:00
|
|
|
emit finished(reply()->error());
|
2014-04-14 17:08:43 +04:00
|
|
|
return true;
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2015-04-14 09:36:17 +03:00
|
|
|
/*********************************************************************************************/
|
|
|
|
// supposed to read <D:collection> when pointing to <D:resourcetype><D:collection></D:resourcetype>..
|
|
|
|
static QString readContentsAsString(QXmlStreamReader &reader)
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
int level = 0;
|
|
|
|
do {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
|
|
|
if (type == QXmlStreamReader::StartElement) {
|
|
|
|
level++;
|
|
|
|
result += "<" + reader.name().toString() + ">";
|
|
|
|
} else if (type == QXmlStreamReader::Characters) {
|
|
|
|
result += reader.text();
|
|
|
|
} else if (type == QXmlStreamReader::EndElement) {
|
|
|
|
level--;
|
|
|
|
if (level < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result += "</" + reader.name().toString() + ">";
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (!reader.atEnd());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LsColXMLParser::LsColXMLParser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:19:14 +03:00
|
|
|
bool LsColXMLParser::parse(const QByteArray &xml, QHash<QString, qint64> *sizes, const QString &expectedPath)
|
2015-04-14 09:36:17 +03:00
|
|
|
{
|
|
|
|
// Parse DAV response
|
|
|
|
QXmlStreamReader reader(xml);
|
|
|
|
reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
|
|
|
|
|
|
|
|
QStringList folders;
|
|
|
|
QString currentHref;
|
|
|
|
QMap<QString, QString> currentTmpProperties;
|
|
|
|
QMap<QString, QString> currentHttp200Properties;
|
|
|
|
bool currentPropsHaveHttp200 = false;
|
|
|
|
bool insidePropstat = false;
|
|
|
|
bool insideProp = false;
|
2015-04-14 15:56:25 +03:00
|
|
|
bool insideMultiStatus = false;
|
2015-04-14 09:36:17 +03:00
|
|
|
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
|
|
|
QString name = reader.name().toString();
|
|
|
|
// Start elements with DAV:
|
|
|
|
if (type == QXmlStreamReader::StartElement && reader.namespaceUri() == QLatin1String("DAV:")) {
|
|
|
|
if (name == QLatin1String("href")) {
|
2015-05-07 18:19:14 +03:00
|
|
|
// We don't use URL encoding in our request URL (which is the expected path) (QNAM will do it for us)
|
|
|
|
// but the result will have URL encoding..
|
|
|
|
QString hrefString = QString::fromUtf8(QByteArray::fromPercentEncoding(reader.readElementText().toUtf8()));
|
|
|
|
if (!hrefString.startsWith(expectedPath)) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcLsColJob) << "Invalid href" << hrefString << "expected starting with" << expectedPath;
|
2015-05-07 18:19:14 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
currentHref = hrefString;
|
2015-04-14 09:36:17 +03:00
|
|
|
} else if (name == QLatin1String("response")) {
|
|
|
|
} else if (name == QLatin1String("propstat")) {
|
|
|
|
insidePropstat = true;
|
|
|
|
} else if (name == QLatin1String("status") && insidePropstat) {
|
|
|
|
QString httpStatus = reader.readElementText();
|
|
|
|
if (httpStatus.startsWith("HTTP/1.1 200")) {
|
|
|
|
currentPropsHaveHttp200 = true;
|
|
|
|
} else {
|
|
|
|
currentPropsHaveHttp200 = false;
|
|
|
|
}
|
|
|
|
} else if (name == QLatin1String("prop")) {
|
|
|
|
insideProp = true;
|
|
|
|
continue;
|
2015-04-14 15:56:25 +03:00
|
|
|
} else if (name == QLatin1String("multistatus")) {
|
|
|
|
insideMultiStatus = true;
|
|
|
|
continue;
|
2015-04-14 09:36:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == QXmlStreamReader::StartElement && insidePropstat && insideProp) {
|
|
|
|
// All those elements are properties
|
|
|
|
QString propertyContent = readContentsAsString(reader);
|
|
|
|
if (name == QLatin1String("resourcetype") && propertyContent.contains("collection")) {
|
|
|
|
folders.append(currentHref);
|
2016-02-11 17:06:00 +03:00
|
|
|
} else if (name == QLatin1String("size")) {
|
2015-04-14 09:36:17 +03:00
|
|
|
bool ok = false;
|
|
|
|
auto s = propertyContent.toLongLong(&ok);
|
|
|
|
if (ok && sizes) {
|
|
|
|
sizes->insert(currentHref, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentTmpProperties.insert(reader.name().toString(), propertyContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// End elements with DAV:
|
|
|
|
if (type == QXmlStreamReader::EndElement) {
|
|
|
|
if (reader.namespaceUri() == QLatin1String("DAV:")) {
|
|
|
|
if (reader.name() == "response") {
|
|
|
|
if (currentHref.endsWith('/')) {
|
|
|
|
currentHref.chop(1);
|
|
|
|
}
|
|
|
|
emit directoryListingIterated(currentHref, currentHttp200Properties);
|
|
|
|
currentHref.clear();
|
|
|
|
currentHttp200Properties.clear();
|
|
|
|
} else if (reader.name() == "propstat") {
|
|
|
|
insidePropstat = false;
|
|
|
|
if (currentPropsHaveHttp200) {
|
|
|
|
currentHttp200Properties = QMap<QString, QString>(currentTmpProperties);
|
|
|
|
}
|
|
|
|
currentTmpProperties.clear();
|
|
|
|
currentPropsHaveHttp200 = false;
|
|
|
|
} else if (reader.name() == "prop") {
|
|
|
|
insideProp = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reader.hasError()) {
|
|
|
|
// XML Parser error? Whatever had been emitted before will come as directoryListingIterated
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcLsColJob) << "ERROR" << reader.errorString() << xml;
|
2015-04-14 15:56:25 +03:00
|
|
|
return false;
|
|
|
|
} else if (!insideMultiStatus) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcLsColJob) << "ERROR no WebDAV response?" << xml;
|
2015-04-14 09:36:17 +03:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
emit directoryListingSubfolders(folders);
|
|
|
|
emit finishedWithoutError();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
LsColJob::LsColJob(AccountPtr account, const QString &path, QObject *parent)
|
2013-10-23 16:48:44 +04:00
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-11-14 22:20:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-02 18:14:44 +03:00
|
|
|
LsColJob::LsColJob(AccountPtr account, const QUrl &url, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, QString(), parent)
|
|
|
|
, _url(url)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-02 13:00:37 +03:00
|
|
|
void LsColJob::setProperties(QList<QByteArray> properties)
|
|
|
|
{
|
|
|
|
_properties = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QByteArray> LsColJob::properties() const
|
|
|
|
{
|
|
|
|
return _properties;
|
|
|
|
}
|
|
|
|
|
2013-11-14 22:20:09 +04:00
|
|
|
void LsColJob::start()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2015-03-02 13:00:37 +03:00
|
|
|
QList<QByteArray> properties = _properties;
|
|
|
|
|
|
|
|
if (properties.isEmpty()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcLsColJob) << "Propfind with no properties!";
|
2015-03-02 13:00:37 +03:00
|
|
|
}
|
|
|
|
QByteArray propStr;
|
|
|
|
foreach (const QByteArray &prop, properties) {
|
|
|
|
if (prop.contains(':')) {
|
|
|
|
int colIdx = prop.lastIndexOf(":");
|
|
|
|
auto ns = prop.left(colIdx);
|
|
|
|
if (ns == "http://owncloud.org/ns") {
|
|
|
|
propStr += " <oc:" + prop.mid(colIdx + 1) + " />\n";
|
|
|
|
} else {
|
|
|
|
propStr += " <" + prop.mid(colIdx + 1) + " xmlns=\"" + ns + "\" />\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
propStr += " <d:" + prop + " />\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
QNetworkRequest req;
|
2013-10-18 14:24:29 +04:00
|
|
|
req.setRawHeader("Depth", "1");
|
|
|
|
QByteArray xml("<?xml version=\"1.0\" ?>\n"
|
2014-12-02 14:25:51 +03:00
|
|
|
"<d:propfind xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\">\n"
|
2013-10-18 14:24:29 +04:00
|
|
|
" <d:prop>\n"
|
|
|
|
+ propStr + " </d:prop>\n"
|
|
|
|
"</d:propfind>\n");
|
2013-11-14 22:20:09 +04:00
|
|
|
QBuffer *buf = new QBuffer(this);
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setData(xml);
|
|
|
|
buf->open(QIODevice::ReadOnly);
|
2017-03-03 13:20:53 +03:00
|
|
|
if (_url.isValid()) {
|
|
|
|
sendRequest("PROPFIND", _url, req, buf);
|
|
|
|
} else {
|
|
|
|
sendRequest("PROPFIND", makeDavUrl(path()), req, buf);
|
|
|
|
}
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2015-04-13 17:04:24 +03:00
|
|
|
// TODO: Instead of doing all in this slot, we should iteratively parse in readyRead(). This
|
|
|
|
// would allow us to be more asynchronous in processing while data is coming from the network,
|
2015-10-05 06:20:09 +03:00
|
|
|
// not all in one big blob at the end.
|
2014-04-14 17:08:43 +04:00
|
|
|
bool LsColJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcLsColJob) << "LSCOL of" << reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< reply()->error()
|
|
|
|
<< (reply()->error() == QNetworkReply::NoError ? QLatin1String("") : errorString());
|
|
|
|
|
2014-12-02 14:25:51 +03:00
|
|
|
QString contentType = reply()->header(QNetworkRequest::ContentTypeHeader).toString();
|
|
|
|
int httpCode = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
if (httpCode == 207 && contentType.contains("application/xml; charset=utf-8")) {
|
2015-04-14 09:36:17 +03:00
|
|
|
LsColXMLParser parser;
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(&parser, &LsColXMLParser::directoryListingSubfolders,
|
|
|
|
this, &LsColJob::directoryListingSubfolders);
|
|
|
|
connect(&parser, &LsColXMLParser::directoryListingIterated,
|
|
|
|
this, &LsColJob::directoryListingIterated);
|
|
|
|
connect(&parser, &LsColXMLParser::finishedWithError,
|
|
|
|
this, &LsColJob::finishedWithError);
|
|
|
|
connect(&parser, &LsColXMLParser::finishedWithoutError,
|
|
|
|
this, &LsColJob::finishedWithoutError);
|
2015-04-14 09:36:17 +03:00
|
|
|
|
2015-05-07 18:19:14 +03:00
|
|
|
QString expectedPath = reply()->request().url().path(); // something like "/owncloud/remote.php/webdav/folder"
|
|
|
|
if (!parser.parse(reply()->readAll(), &_sizes, expectedPath)) {
|
2015-04-14 09:36:17 +03:00
|
|
|
// XML parse error
|
2015-04-13 17:04:24 +03:00
|
|
|
emit finishedWithError(reply());
|
|
|
|
}
|
2014-12-02 14:25:51 +03:00
|
|
|
} else if (httpCode == 207) {
|
|
|
|
// wrong content type
|
|
|
|
emit finishedWithError(reply());
|
|
|
|
} else {
|
2015-04-13 17:04:24 +03:00
|
|
|
// wrong HTTP code or any other network error
|
2014-12-02 14:25:51 +03:00
|
|
|
emit finishedWithError(reply());
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
2015-04-14 09:36:17 +03:00
|
|
|
|
2014-04-14 17:08:43 +04:00
|
|
|
return true;
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2014-04-14 17:10:08 +04:00
|
|
|
namespace {
|
|
|
|
const char statusphpC[] = "status.php";
|
2018-01-23 15:43:44 +03:00
|
|
|
const char nextcloudDirC[] = "nextcloud/";
|
2014-04-14 17:10:08 +04:00
|
|
|
}
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
CheckServerJob::CheckServerJob(AccountPtr account, QObject *parent)
|
2014-04-14 17:10:08 +04:00
|
|
|
: AbstractNetworkJob(account, QLatin1String(statusphpC), parent)
|
|
|
|
, _subdirFallback(false)
|
2017-09-08 12:59:45 +03:00
|
|
|
, _permanentRedirects(0)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2014-03-03 19:37:29 +04:00
|
|
|
setIgnoreCredentialFailure(true);
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(this, &AbstractNetworkJob::redirected,
|
|
|
|
this, &CheckServerJob::slotRedirected);
|
2013-11-14 22:20:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckServerJob::start()
|
|
|
|
{
|
2017-09-08 12:59:45 +03:00
|
|
|
_serverUrl = account()->url();
|
|
|
|
sendRequest("GET", Utility::concatUrlPath(_serverUrl, path()));
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(reply(), &QNetworkReply::metaDataChanged, this, &CheckServerJob::metaDataChangedSlot);
|
|
|
|
connect(reply(), &QNetworkReply::encrypted, this, &CheckServerJob::encryptedSlot);
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2017-03-23 17:53:22 +03:00
|
|
|
void CheckServerJob::onTimedOut()
|
2013-11-14 20:02:41 +04:00
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcCheckServerJob) << "TIMEOUT";
|
2014-10-29 11:37:52 +03:00
|
|
|
if (reply() && reply()->isRunning()) {
|
2013-11-14 20:02:41 +04:00
|
|
|
emit timeout(reply()->url());
|
2014-10-29 11:37:52 +03:00
|
|
|
} else if (!reply()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcCheckServerJob) << "Timeout even there was no reply?";
|
2014-10-29 11:37:52 +03:00
|
|
|
}
|
2014-09-12 19:58:01 +04:00
|
|
|
deleteLater();
|
2013-11-14 20:02:41 +04:00
|
|
|
}
|
|
|
|
|
2017-04-26 13:15:33 +03:00
|
|
|
QString CheckServerJob::version(const QJsonObject &info)
|
2013-10-23 16:48:44 +04:00
|
|
|
{
|
|
|
|
return info.value(QLatin1String("version")).toString();
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:15:33 +03:00
|
|
|
QString CheckServerJob::versionString(const QJsonObject &info)
|
2013-10-23 16:48:44 +04:00
|
|
|
{
|
|
|
|
return info.value(QLatin1String("versionstring")).toString();
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:15:33 +03:00
|
|
|
bool CheckServerJob::installed(const QJsonObject &info)
|
2013-10-23 16:48:44 +04:00
|
|
|
{
|
|
|
|
return info.value(QLatin1String("installed")).toBool();
|
|
|
|
}
|
|
|
|
|
2015-08-11 13:18:25 +03:00
|
|
|
static void mergeSslConfigurationForSslButton(const QSslConfiguration &config, AccountPtr account)
|
|
|
|
{
|
|
|
|
if (config.peerCertificateChain().length() > 0) {
|
|
|
|
account->_peerCertificateChain = config.peerCertificateChain();
|
|
|
|
}
|
|
|
|
if (!config.sessionCipher().isNull()) {
|
|
|
|
account->_sessionCipher = config.sessionCipher();
|
|
|
|
}
|
|
|
|
if (config.sessionTicket().length() > 0) {
|
|
|
|
account->_sessionTicket = config.sessionTicket();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckServerJob::encryptedSlot()
|
|
|
|
{
|
|
|
|
mergeSslConfigurationForSslButton(reply()->sslConfiguration(), account());
|
|
|
|
}
|
|
|
|
|
2017-09-08 12:59:45 +03:00
|
|
|
void CheckServerJob::slotRedirected(QNetworkReply *reply, const QUrl &targetUrl, int redirectCount)
|
|
|
|
{
|
|
|
|
QByteArray slashStatusPhp("/");
|
|
|
|
slashStatusPhp.append(statusphpC);
|
|
|
|
|
|
|
|
int httpCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
QString path = targetUrl.path();
|
|
|
|
if ((httpCode == 301 || httpCode == 308) // permanent redirection
|
|
|
|
&& redirectCount == _permanentRedirects // don't apply permanent redirects after a temporary one
|
|
|
|
&& path.endsWith(slashStatusPhp)) {
|
|
|
|
_serverUrl = targetUrl;
|
|
|
|
_serverUrl.setPath(path.left(path.size() - slashStatusPhp.size()));
|
|
|
|
qCInfo(lcCheckServerJob) << "status.php was permanently redirected to"
|
|
|
|
<< targetUrl << "new server url is" << _serverUrl;
|
|
|
|
++_permanentRedirects;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-05 13:58:04 +03:00
|
|
|
void CheckServerJob::metaDataChangedSlot()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2014-01-21 04:45:02 +04:00
|
|
|
account()->setSslConfiguration(reply()->sslConfiguration());
|
2015-08-11 13:18:25 +03:00
|
|
|
mergeSslConfigurationForSslButton(reply()->sslConfiguration(), account());
|
2015-08-05 13:58:04 +03:00
|
|
|
}
|
|
|
|
|
2013-10-24 02:29:08 +04:00
|
|
|
|
2015-08-05 13:58:04 +03:00
|
|
|
bool CheckServerJob::finished()
|
|
|
|
{
|
2015-05-08 15:21:27 +03:00
|
|
|
if (reply()->request().url().scheme() == QLatin1String("https")
|
2015-07-30 19:15:55 +03:00
|
|
|
&& reply()->sslConfiguration().sessionTicket().isEmpty()
|
|
|
|
&& reply()->error() == QNetworkReply::NoError) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcCheckServerJob) << "No SSL session identifier / session ticket is used, this might impact sync performance negatively.";
|
2015-05-08 15:21:27 +03:00
|
|
|
}
|
|
|
|
|
2015-08-11 13:18:25 +03:00
|
|
|
mergeSslConfigurationForSslButton(reply()->sslConfiguration(), account());
|
|
|
|
|
2015-10-05 06:20:09 +03:00
|
|
|
// The server installs to /owncloud. Let's try that if the file wasn't found
|
2014-04-14 17:10:08 +04:00
|
|
|
// at the original location
|
|
|
|
if ((reply()->error() == QNetworkReply::ContentNotFoundError) && (!_subdirFallback)) {
|
|
|
|
_subdirFallback = true;
|
2018-01-23 15:43:44 +03:00
|
|
|
setPath(QLatin1String(nextcloudDirC) + QLatin1String(statusphpC));
|
2014-04-14 17:10:08 +04:00
|
|
|
start();
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcCheckServerJob) << "Retrying with" << reply()->url();
|
2014-04-14 17:10:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 13:43:15 +03:00
|
|
|
QByteArray body = reply()->peek(4 * 1024);
|
2014-10-09 13:05:04 +04:00
|
|
|
int httpStatus = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
if (body.isEmpty() || httpStatus != 200) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcCheckServerJob) << "error: status.php replied " << httpStatus << body;
|
2014-04-14 17:10:08 +04:00
|
|
|
emit instanceNotFound(reply());
|
2014-06-24 17:17:33 +04:00
|
|
|
} else {
|
2017-04-26 13:15:33 +03:00
|
|
|
QJsonParseError error;
|
|
|
|
auto status = QJsonDocument::fromJson(body, &error);
|
2014-06-24 17:17:33 +04:00
|
|
|
// empty or invalid response
|
2017-04-26 13:15:33 +03:00
|
|
|
if (error.error != QJsonParseError::NoError || status.isNull()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcCheckServerJob) << "status.php from server is not valid JSON!" << body << reply()->request().url() << error.errorString();
|
2014-06-24 17:17:33 +04:00
|
|
|
}
|
|
|
|
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcCheckServerJob) << "status.php returns: " << status << " " << reply()->error() << " Reply: " << reply();
|
2017-04-26 13:15:33 +03:00
|
|
|
if (status.object().contains("installed")) {
|
2017-09-08 12:59:45 +03:00
|
|
|
emit instanceFound(_serverUrl, status.object());
|
2014-06-24 17:17:33 +04:00
|
|
|
} else {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcCheckServerJob) << "No proper answer on " << reply()->url();
|
2014-06-24 17:17:33 +04:00
|
|
|
emit instanceNotFound(reply());
|
|
|
|
}
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
2014-04-14 17:08:43 +04:00
|
|
|
return true;
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 19:36:23 +04:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
PropfindJob::PropfindJob(AccountPtr account, const QString &path, QObject *parent)
|
2013-10-23 16:48:44 +04:00
|
|
|
: AbstractNetworkJob(account, path, parent)
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2013-11-14 22:20:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropfindJob::start()
|
|
|
|
{
|
|
|
|
QList<QByteArray> properties = _properties;
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
if (properties.isEmpty()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcLsColJob) << "Propfind with no properties!";
|
2013-10-23 16:48:44 +04:00
|
|
|
}
|
|
|
|
QNetworkRequest req;
|
2016-02-25 19:40:24 +03:00
|
|
|
// Always have a higher priority than the propagator because we use this from the UI
|
|
|
|
// and really want this to be done first (no matter what internal scheduling QNAM uses).
|
|
|
|
// Also possibly useful for avoiding false timeouts.
|
|
|
|
req.setPriority(QNetworkRequest::HighPriority);
|
2013-10-18 14:24:29 +04:00
|
|
|
req.setRawHeader("Depth", "0");
|
2013-10-23 16:48:44 +04:00
|
|
|
QByteArray propStr;
|
|
|
|
foreach (const QByteArray &prop, properties) {
|
2014-11-13 20:57:07 +03:00
|
|
|
if (prop.contains(':')) {
|
2014-11-14 17:03:48 +03:00
|
|
|
int colIdx = prop.lastIndexOf(":");
|
|
|
|
propStr += " <" + prop.mid(colIdx + 1) + " xmlns=\"" + prop.left(colIdx) + "\" />\n";
|
2014-11-13 20:57:07 +03:00
|
|
|
} else {
|
|
|
|
propStr += " <d:" + prop + " />\n";
|
|
|
|
}
|
2013-10-23 16:48:44 +04:00
|
|
|
}
|
|
|
|
QByteArray xml = "<?xml version=\"1.0\" ?>\n"
|
|
|
|
"<d:propfind xmlns:d=\"DAV:\">\n"
|
|
|
|
" <d:prop>\n"
|
|
|
|
+ propStr + " </d:prop>\n"
|
|
|
|
"</d:propfind>\n";
|
|
|
|
|
2013-11-14 22:20:09 +04:00
|
|
|
QBuffer *buf = new QBuffer(this);
|
2013-10-18 14:24:29 +04:00
|
|
|
buf->setData(xml);
|
|
|
|
buf->open(QIODevice::ReadOnly);
|
2017-03-03 13:20:53 +03:00
|
|
|
sendRequest("PROPFIND", makeDavUrl(path()), req, buf);
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-14 22:20:09 +04:00
|
|
|
void PropfindJob::setProperties(QList<QByteArray> properties)
|
|
|
|
{
|
|
|
|
_properties = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QByteArray> PropfindJob::properties() const
|
|
|
|
{
|
|
|
|
return _properties;
|
|
|
|
}
|
|
|
|
|
2014-04-14 17:08:43 +04:00
|
|
|
bool PropfindJob::finished()
|
2013-10-18 14:24:29 +04:00
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcPropfindJob) << "PROPFIND of" << reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< reply()->error()
|
|
|
|
<< (reply()->error() == QNetworkReply::NoError ? QLatin1String("") : errorString());
|
|
|
|
|
2013-10-18 14:24:29 +04:00
|
|
|
int http_result_code = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
|
|
|
if (http_result_code == 207) {
|
|
|
|
// Parse DAV response
|
|
|
|
QXmlStreamReader reader(reply());
|
|
|
|
reader.addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration("d", "DAV:"));
|
|
|
|
|
2013-10-23 16:48:44 +04:00
|
|
|
QVariantMap items;
|
|
|
|
// introduced to nesting is ignored
|
|
|
|
QStack<QString> curElement;
|
2013-10-18 14:24:29 +04:00
|
|
|
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader.readNext();
|
2014-11-13 20:57:07 +03:00
|
|
|
if (type == QXmlStreamReader::StartElement) {
|
2014-11-14 17:03:48 +03:00
|
|
|
if (!curElement.isEmpty() && curElement.top() == QLatin1String("prop")) {
|
2015-05-22 12:49:39 +03:00
|
|
|
items.insert(reader.name().toString(), reader.readElementText(QXmlStreamReader::SkipChildElements));
|
2015-04-07 11:35:27 +03:00
|
|
|
} else {
|
|
|
|
curElement.push(reader.name().toString());
|
2013-10-23 16:48:44 +04:00
|
|
|
}
|
|
|
|
}
|
2014-11-13 20:57:07 +03:00
|
|
|
if (type == QXmlStreamReader::EndElement) {
|
2013-10-23 16:48:44 +04:00
|
|
|
if (curElement.top() == reader.name()) {
|
|
|
|
curElement.pop();
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
}
|
2013-10-23 16:48:44 +04:00
|
|
|
}
|
2016-03-17 12:29:35 +03:00
|
|
|
if (reader.hasError()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcPropfindJob) << "XML parser error: " << reader.errorString();
|
2016-03-17 12:29:35 +03:00
|
|
|
emit finishedWithError(reply());
|
|
|
|
} else {
|
|
|
|
emit result(items);
|
|
|
|
}
|
2013-10-18 14:24:29 +04:00
|
|
|
} else {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcPropfindJob) << "*not* successful, http result code is" << http_result_code
|
2014-02-24 18:20:36 +04:00
|
|
|
<< (http_result_code == 302 ? reply()->header(QNetworkRequest::LocationHeader).toString() : QLatin1String(""));
|
2016-02-10 17:38:21 +03:00
|
|
|
emit finishedWithError(reply());
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
2014-04-14 17:08:43 +04:00
|
|
|
return true;
|
2013-10-18 14:24:29 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 19:36:23 +04:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2017-11-23 12:11:39 +03:00
|
|
|
#ifndef TOKEN_AUTH_ONLY
|
2017-11-21 12:18:15 +03:00
|
|
|
AvatarJob::AvatarJob(AccountPtr account, const QString &userId, int size, QObject *parent)
|
2017-01-22 15:52:19 +03:00
|
|
|
: AbstractNetworkJob(account, QString(), parent)
|
|
|
|
{
|
2018-01-09 13:33:43 +03:00
|
|
|
if (account->serverVersionInt() >= Account::makeServerVersion(10, 0, 0)) {
|
2018-01-11 12:46:50 +03:00
|
|
|
_avatarUrl = Utility::concatUrlPath(account->url(), QString("remote.php/dav/avatars/%1/%2.png").arg(userId, QString::number(size)));
|
2018-01-09 13:33:43 +03:00
|
|
|
} else {
|
2018-01-11 12:46:50 +03:00
|
|
|
_avatarUrl = Utility::concatUrlPath(account->url(), QString("index.php/avatar/%1/%2").arg(userId, QString::number(size)));
|
2018-01-09 13:33:43 +03:00
|
|
|
}
|
2017-01-22 15:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AvatarJob::start()
|
|
|
|
{
|
|
|
|
QNetworkRequest req;
|
2017-04-13 12:40:27 +03:00
|
|
|
sendRequest("GET", _avatarUrl, req);
|
2017-01-22 15:52:19 +03:00
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
2017-11-21 12:18:15 +03:00
|
|
|
QImage AvatarJob::makeCircularAvatar(const QImage &baseAvatar)
|
|
|
|
{
|
|
|
|
int dim = baseAvatar.width();
|
|
|
|
|
|
|
|
QImage avatar(dim, dim, baseAvatar.format());
|
|
|
|
avatar.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter painter(&avatar);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
path.addEllipse(0, 0, dim, dim);
|
|
|
|
painter.setClipPath(path);
|
|
|
|
|
|
|
|
painter.drawImage(0, 0, baseAvatar);
|
|
|
|
painter.end();
|
|
|
|
|
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
|
2017-01-22 15:52:19 +03:00
|
|
|
bool AvatarJob::finished()
|
|
|
|
{
|
|
|
|
int http_result_code = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
2017-03-10 00:34:36 +03:00
|
|
|
QImage avImage;
|
2017-01-22 15:52:19 +03:00
|
|
|
|
|
|
|
if (http_result_code == 200) {
|
|
|
|
QByteArray pngData = reply()->readAll();
|
|
|
|
if (pngData.size()) {
|
2017-03-10 00:34:36 +03:00
|
|
|
if (avImage.loadFromData(pngData)) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCDebug(lcAvatarJob) << "Retrieved Avatar pixmap!";
|
2017-01-22 15:52:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 00:34:36 +03:00
|
|
|
emit(avatarPixmap(avImage));
|
2017-01-22 15:52:19 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-23 12:11:39 +03:00
|
|
|
#endif
|
2017-01-22 15:52:19 +03:00
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2015-11-12 13:20:45 +03:00
|
|
|
ProppatchJob::ProppatchJob(AccountPtr account, const QString &path, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProppatchJob::start()
|
|
|
|
{
|
|
|
|
if (_properties.isEmpty()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcProppatchJob) << "Proppatch with no properties!";
|
2015-11-12 13:20:45 +03:00
|
|
|
}
|
|
|
|
QNetworkRequest req;
|
|
|
|
|
|
|
|
QByteArray propStr;
|
|
|
|
QMapIterator<QByteArray, QByteArray> it(_properties);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
QByteArray keyName = it.key();
|
|
|
|
QByteArray keyNs;
|
|
|
|
if (keyName.contains(':')) {
|
|
|
|
int colIdx = keyName.lastIndexOf(":");
|
|
|
|
keyNs = keyName.left(colIdx);
|
|
|
|
keyName = keyName.mid(colIdx + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
propStr += " <" + keyName;
|
|
|
|
if (!keyNs.isEmpty()) {
|
|
|
|
propStr += " xmlns=\"" + keyNs + "\" ";
|
|
|
|
}
|
|
|
|
propStr += ">";
|
|
|
|
propStr += it.value();
|
|
|
|
propStr += "</" + keyName + ">\n";
|
|
|
|
}
|
|
|
|
QByteArray xml = "<?xml version=\"1.0\" ?>\n"
|
|
|
|
"<d:propertyupdate xmlns:d=\"DAV:\">\n"
|
|
|
|
" <d:set><d:prop>\n"
|
|
|
|
+ propStr + " </d:prop></d:set>\n"
|
|
|
|
"</d:propertyupdate>\n";
|
|
|
|
|
|
|
|
QBuffer *buf = new QBuffer(this);
|
|
|
|
buf->setData(xml);
|
|
|
|
buf->open(QIODevice::ReadOnly);
|
2017-03-03 13:20:53 +03:00
|
|
|
sendRequest("PROPPATCH", makeDavUrl(path()), req, buf);
|
2015-11-12 13:20:45 +03:00
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProppatchJob::setProperties(QMap<QByteArray, QByteArray> properties)
|
|
|
|
{
|
|
|
|
_properties = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QByteArray, QByteArray> ProppatchJob::properties() const
|
|
|
|
{
|
|
|
|
return _properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProppatchJob::finished()
|
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcProppatchJob) << "PROPPATCH of" << reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< reply()->error()
|
|
|
|
<< (reply()->error() == QNetworkReply::NoError ? QLatin1String("") : errorString());
|
|
|
|
|
2015-11-12 13:20:45 +03:00
|
|
|
int http_result_code = reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
|
|
|
|
if (http_result_code == 207) {
|
|
|
|
emit success();
|
|
|
|
} else {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcProppatchJob) << "*not* successful, http result code is" << http_result_code
|
2015-11-12 13:20:45 +03:00
|
|
|
<< (http_result_code == 302 ? reply()->header(QNetworkRequest::LocationHeader).toString() : QLatin1String(""));
|
|
|
|
emit finishedWithError();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
EntityExistsJob::EntityExistsJob(AccountPtr account, const QString &path, QObject *parent)
|
2013-10-28 23:01:59 +04:00
|
|
|
: AbstractNetworkJob(account, path, parent)
|
|
|
|
{
|
2013-11-14 22:20:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void EntityExistsJob::start()
|
|
|
|
{
|
2017-03-03 13:20:53 +03:00
|
|
|
sendRequest("HEAD", makeAccountUrl(path()));
|
2013-11-22 17:01:11 +04:00
|
|
|
AbstractNetworkJob::start();
|
2013-10-28 23:01:59 +04:00
|
|
|
}
|
|
|
|
|
2014-04-14 17:08:43 +04:00
|
|
|
bool EntityExistsJob::finished()
|
2013-10-28 23:01:59 +04:00
|
|
|
{
|
|
|
|
emit exists(reply());
|
2014-04-14 17:08:43 +04:00
|
|
|
return true;
|
2013-10-28 23:01:59 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 19:36:23 +04:00
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2015-01-26 17:33:00 +03:00
|
|
|
JsonApiJob::JsonApiJob(const AccountPtr &account, const QString &path, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-08 12:10:28 +03:00
|
|
|
void JsonApiJob::addQueryParams(const QUrlQuery ¶ms)
|
2015-11-02 19:37:23 +03:00
|
|
|
{
|
|
|
|
_additionalParams = params;
|
|
|
|
}
|
|
|
|
|
2018-03-02 00:19:04 +03:00
|
|
|
void JsonApiJob::addRawHeader(const QByteArray &headerName, const QByteArray &value)
|
|
|
|
{
|
|
|
|
_request.setRawHeader(headerName, value);
|
|
|
|
}
|
|
|
|
|
2015-01-26 17:33:00 +03:00
|
|
|
void JsonApiJob::start()
|
|
|
|
{
|
2018-03-02 00:19:04 +03:00
|
|
|
addRawHeader("OCS-APIREQUEST", "true");
|
2017-12-08 12:10:28 +03:00
|
|
|
auto query = _additionalParams;
|
|
|
|
query.addQueryItem(QLatin1String("format"), QLatin1String("json"));
|
|
|
|
QUrl url = Utility::concatUrlPath(account()->url(), path(), query);
|
2018-03-02 00:19:04 +03:00
|
|
|
sendRequest("GET", url, _request);
|
2015-01-26 17:33:00 +03:00
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonApiJob::finished()
|
|
|
|
{
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcJsonApiJob) << "JsonApiJob of" << reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< reply()->error()
|
|
|
|
<< (reply()->error() == QNetworkReply::NoError ? QLatin1String("") : errorString());
|
|
|
|
|
2015-11-19 17:59:10 +03:00
|
|
|
int statusCode = 0;
|
2015-01-26 17:33:00 +03:00
|
|
|
if (reply()->error() != QNetworkReply::NoError) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcJsonApiJob) << "Network error: " << path() << errorString() << reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
2017-04-26 13:15:33 +03:00
|
|
|
emit jsonReceived(QJsonDocument(), statusCode);
|
2015-01-26 17:33:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString jsonStr = QString::fromUtf8(reply()->readAll());
|
2015-11-19 17:59:10 +03:00
|
|
|
if (jsonStr.contains("<?xml version=\"1.0\"?>")) {
|
|
|
|
QRegExp rex("<statuscode>(\\d+)</statuscode>");
|
|
|
|
if (jsonStr.contains(rex)) {
|
|
|
|
// this is a error message coming back from ocs.
|
|
|
|
statusCode = rex.cap(1).toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
QRegExp rex("\"statuscode\":(\\d+),");
|
|
|
|
// example: "{"ocs":{"meta":{"status":"ok","statuscode":100,"message":null},"data":{"version":{"major":8,"minor":"... (504)
|
|
|
|
if (jsonStr.contains(rex)) {
|
|
|
|
statusCode = rex.cap(1).toInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:15:33 +03:00
|
|
|
QJsonParseError error;
|
|
|
|
auto json = QJsonDocument::fromJson(jsonStr.toUtf8(), &error);
|
2015-01-26 17:33:00 +03:00
|
|
|
// empty or invalid response
|
2017-04-26 13:15:33 +03:00
|
|
|
if (error.error != QJsonParseError::NoError || json.isNull()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcJsonApiJob) << "invalid JSON!" << jsonStr << error.errorString();
|
2017-04-26 13:15:33 +03:00
|
|
|
emit jsonReceived(json, statusCode);
|
2015-01-26 17:33:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-02 00:19:04 +03:00
|
|
|
if(reply()->rawHeaderList().contains("ETag"))
|
|
|
|
emit etagResponseHeaderReceived(reply()->rawHeader("ETag"), statusCode);
|
|
|
|
|
2015-11-19 17:59:10 +03:00
|
|
|
emit jsonReceived(json, statusCode);
|
2015-01-26 17:33:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:58:45 +03:00
|
|
|
DetermineAuthTypeJob::DetermineAuthTypeJob(AccountPtr account, QObject *parent)
|
2017-11-01 13:16:26 +03:00
|
|
|
: QObject(parent)
|
|
|
|
, _account(account)
|
2017-09-07 15:58:45 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DetermineAuthTypeJob::start()
|
|
|
|
{
|
2017-11-01 13:16:26 +03:00
|
|
|
qCInfo(lcDetermineAuthTypeJob) << "Determining auth type for" << _account->davUrl();
|
2017-09-07 15:58:45 +03:00
|
|
|
|
2017-11-01 13:16:26 +03:00
|
|
|
QNetworkRequest req;
|
|
|
|
// Prevent HttpCredentialsAccessManager from setting an Authorization header.
|
|
|
|
req.setAttribute(HttpCredentials::DontAddCredentialsAttribute, true);
|
|
|
|
// Don't reuse previous auth credentials
|
|
|
|
req.setAttribute(QNetworkRequest::AuthenticationReuseAttribute, QNetworkRequest::Manual);
|
|
|
|
// Don't send cookies, we can't determine the auth type if we're logged in
|
|
|
|
req.setAttribute(QNetworkRequest::CookieLoadControlAttribute, QNetworkRequest::Manual);
|
2017-09-07 15:58:45 +03:00
|
|
|
|
2017-11-01 13:16:26 +03:00
|
|
|
// Start two parallel requests, one determines whether it's a shib server
|
|
|
|
// and the other checks the HTTP auth method.
|
|
|
|
auto get = _account->sendRequest("GET", _account->davUrl(), req);
|
|
|
|
auto propfind = _account->sendRequest("PROPFIND", _account->davUrl(), req);
|
|
|
|
get->setTimeout(30 * 1000);
|
|
|
|
propfind->setTimeout(30 * 1000);
|
|
|
|
get->setIgnoreCredentialFailure(true);
|
|
|
|
propfind->setIgnoreCredentialFailure(true);
|
|
|
|
|
|
|
|
connect(get, &AbstractNetworkJob::redirected, this, [this, get](QNetworkReply *, const QUrl &target, int) {
|
2017-09-07 15:58:45 +03:00
|
|
|
#ifndef NO_SHIBBOLETH
|
|
|
|
QRegExp shibbolethyWords("SAML|wayf");
|
|
|
|
shibbolethyWords.setCaseSensitivity(Qt::CaseInsensitive);
|
2017-11-01 13:16:26 +03:00
|
|
|
if (target.toString().contains(shibbolethyWords)) {
|
|
|
|
_resultGet = Shibboleth;
|
|
|
|
get->setFollowRedirects(false);
|
|
|
|
}
|
2017-09-07 15:58:45 +03:00
|
|
|
#endif
|
2017-11-01 13:16:26 +03:00
|
|
|
});
|
|
|
|
connect(get, &SimpleNetworkJob::finishedSignal, this, [this]() {
|
|
|
|
_getDone = true;
|
|
|
|
checkBothDone();
|
|
|
|
});
|
|
|
|
connect(propfind, &SimpleNetworkJob::finishedSignal, this, [this](QNetworkReply *reply) {
|
|
|
|
auto authChallenge = reply->rawHeader("WWW-Authenticate").toLower();
|
|
|
|
if (authChallenge.contains("bearer ")) {
|
|
|
|
_resultPropfind = OAuth;
|
2017-11-09 13:04:12 +03:00
|
|
|
} else if (authChallenge.isEmpty()) {
|
|
|
|
qCWarning(lcDetermineAuthTypeJob) << "Did not receive WWW-Authenticate reply to auth-test PROPFIND";
|
2017-09-07 15:58:45 +03:00
|
|
|
}
|
2017-11-01 13:16:26 +03:00
|
|
|
_propfindDone = true;
|
|
|
|
checkBothDone();
|
|
|
|
});
|
2017-09-07 15:58:45 +03:00
|
|
|
}
|
|
|
|
|
2017-11-01 13:16:26 +03:00
|
|
|
void DetermineAuthTypeJob::checkBothDone()
|
2017-09-07 15:58:45 +03:00
|
|
|
{
|
2017-11-01 13:16:26 +03:00
|
|
|
if (!_getDone || !_propfindDone)
|
|
|
|
return;
|
|
|
|
auto result = _resultPropfind;
|
|
|
|
// OAuth > Shib > Basic
|
|
|
|
if (_resultGet == Shibboleth && result != OAuth)
|
|
|
|
result = Shibboleth;
|
|
|
|
qCInfo(lcDetermineAuthTypeJob) << "Auth type for" << _account->davUrl() << "is" << result;
|
|
|
|
emit authType(result);
|
|
|
|
deleteLater();
|
2017-09-07 15:58:45 +03:00
|
|
|
}
|
|
|
|
|
2017-09-08 17:43:59 +03:00
|
|
|
SimpleNetworkJob::SimpleNetworkJob(AccountPtr account, QObject *parent)
|
|
|
|
: AbstractNetworkJob(account, QString(), parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkReply *SimpleNetworkJob::startRequest(const QByteArray &verb, const QUrl &url,
|
|
|
|
QNetworkRequest req, QIODevice *requestBody)
|
|
|
|
{
|
|
|
|
auto reply = sendRequest(verb, url, req, requestBody);
|
|
|
|
start();
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleNetworkJob::finished()
|
|
|
|
{
|
|
|
|
emit finishedSignal(reply());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:12:07 +03:00
|
|
|
void fetchPrivateLinkUrl(AccountPtr account, const QString &remotePath,
|
|
|
|
const QByteArray &numericFileId, QObject *target,
|
|
|
|
std::function<void(const QString &url)> targetFun)
|
|
|
|
{
|
|
|
|
QString oldUrl;
|
|
|
|
if (!numericFileId.isEmpty())
|
|
|
|
oldUrl = account->deprecatedPrivateLinkUrl(numericFileId).toString(QUrl::FullyEncoded);
|
|
|
|
|
|
|
|
// Retrieve the new link by PROPFIND
|
|
|
|
PropfindJob *job = new PropfindJob(account, remotePath, target);
|
|
|
|
job->setProperties(
|
|
|
|
QList<QByteArray>()
|
|
|
|
<< "http://owncloud.org/ns:fileid" // numeric file id for fallback private link generation
|
|
|
|
<< "http://owncloud.org/ns:privatelink");
|
|
|
|
job->setTimeout(10 * 1000);
|
|
|
|
QObject::connect(job, &PropfindJob::result, target, [=](const QVariantMap &result) {
|
|
|
|
auto privateLinkUrl = result["privatelink"].toString();
|
|
|
|
auto numericFileId = result["fileid"].toByteArray();
|
|
|
|
if (!privateLinkUrl.isEmpty()) {
|
|
|
|
targetFun(privateLinkUrl);
|
|
|
|
} else if (!numericFileId.isEmpty()) {
|
|
|
|
targetFun(account->deprecatedPrivateLinkUrl(numericFileId).toString(QUrl::FullyEncoded));
|
|
|
|
} else {
|
|
|
|
targetFun(oldUrl);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
QObject::connect(job, &PropfindJob::finishedWithError, target, [=](QNetworkReply *) {
|
|
|
|
targetFun(oldUrl);
|
|
|
|
});
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|