2013-08-01 18:22:54 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Krzesimir Nowak <krzesimir@endocode.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
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2013-08-01 18:22:54 +04:00
|
|
|
*
|
|
|
|
* 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-08-01 18:22:54 +04:00
|
|
|
#include <QNetworkRequest>
|
2014-08-12 21:24:41 +04:00
|
|
|
#include <QNetworkReply>
|
2014-02-05 19:27:57 +04:00
|
|
|
#include <QNetworkProxy>
|
2014-03-06 20:45:02 +04:00
|
|
|
#include <QAuthenticator>
|
2014-05-15 11:43:07 +04:00
|
|
|
#include <QSslConfiguration>
|
2014-08-22 15:46:22 +04:00
|
|
|
#include <QNetworkCookie>
|
|
|
|
#include <QNetworkCookieJar>
|
2015-11-17 12:56:57 +03:00
|
|
|
#include <QNetworkConfiguration>
|
2017-07-13 21:24:44 +03:00
|
|
|
#include <QUuid>
|
2013-08-01 18:22:54 +04:00
|
|
|
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "cookiejar.h"
|
2014-11-10 01:25:57 +03:00
|
|
|
#include "accessmanager.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2020-06-08 16:08:13 +03:00
|
|
|
#include "httplogger.h"
|
2013-08-01 18:22:54 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2013-08-01 18:22:54 +04:00
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcAccessManager, "nextcloud.sync.accessmanager", QtInfoMsg)
|
2017-05-09 15:24:11 +03:00
|
|
|
|
2014-11-10 00:30:29 +03:00
|
|
|
AccessManager::AccessManager(QObject *parent)
|
2013-08-01 18:22:54 +04:00
|
|
|
: QNetworkAccessManager(parent)
|
2013-10-21 23:42:52 +04:00
|
|
|
{
|
2017-09-13 07:26:56 +03:00
|
|
|
#if defined(Q_OS_MAC)
|
2014-02-05 19:27:57 +04:00
|
|
|
// FIXME Workaround http://stackoverflow.com/a/15707366/2941 https://bugreports.qt-project.org/browse/QTBUG-30434
|
|
|
|
QNetworkProxy proxy = this->proxy();
|
|
|
|
proxy.setHostName(" ");
|
|
|
|
setProxy(proxy);
|
|
|
|
#endif
|
2017-02-16 18:14:55 +03:00
|
|
|
|
|
|
|
#ifndef Q_OS_LINUX
|
2015-11-17 12:56:57 +03:00
|
|
|
// Atempt to workaround for https://github.com/owncloud/client/issues/3969
|
|
|
|
setConfiguration(QNetworkConfiguration());
|
2017-02-16 18:14:55 +03:00
|
|
|
#endif
|
2014-05-14 13:11:45 +04:00
|
|
|
setCookieJar(new CookieJar);
|
2013-10-21 23:42:52 +04:00
|
|
|
}
|
2013-08-01 18:22:54 +04:00
|
|
|
|
2021-01-08 17:31:18 +03:00
|
|
|
QByteArray AccessManager::generateRequestId()
|
2017-07-13 21:24:44 +03:00
|
|
|
{
|
2021-01-27 14:39:57 +03:00
|
|
|
return QUuid::createUuid().toByteArray(QUuid::WithoutBraces);
|
2017-07-13 21:24:44 +03:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:30:29 +03:00
|
|
|
QNetworkReply *AccessManager::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
|
2013-08-01 18:22:54 +04:00
|
|
|
{
|
|
|
|
QNetworkRequest newRequest(request);
|
2014-08-22 15:46:22 +04:00
|
|
|
|
2020-10-06 17:54:07 +03:00
|
|
|
// Respect request specific user agent if any
|
2020-10-07 12:40:56 +03:00
|
|
|
if (!newRequest.header(QNetworkRequest::UserAgentHeader).isValid()) {
|
|
|
|
newRequest.setHeader(QNetworkRequest::UserAgentHeader, Utility::userAgentString());
|
2020-10-06 17:54:07 +03:00
|
|
|
}
|
2016-11-08 13:01:49 +03:00
|
|
|
|
|
|
|
// Some firewalls reject requests that have a "User-Agent" but no "Accept" header
|
|
|
|
newRequest.setRawHeader(QByteArray("Accept"), "*/*");
|
|
|
|
|
2013-10-21 23:42:52 +04:00
|
|
|
QByteArray verb = newRequest.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray();
|
|
|
|
// For PROPFIND (assumed to be a WebDAV op), set xml/utf8 as content type/encoding
|
|
|
|
// This needs extension
|
|
|
|
if (verb == "PROPFIND") {
|
|
|
|
newRequest.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("text/xml; charset=utf-8"));
|
|
|
|
}
|
2017-07-13 21:24:44 +03:00
|
|
|
|
|
|
|
// Generate a new request id
|
|
|
|
QByteArray requestId = generateRequestId();
|
|
|
|
qInfo(lcAccessManager) << op << verb << newRequest.url().toString() << "has X-Request-ID" << requestId;
|
|
|
|
newRequest.setRawHeader("X-Request-ID", requestId);
|
|
|
|
|
2020-03-02 03:47:00 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 9, 4)
|
|
|
|
// only enable HTTP2 with Qt 5.9.4 because old Qt have too many bugs (e.g. QTBUG-64359 is fixed in >= Qt 5.9.4)
|
2017-06-13 16:29:01 +03:00
|
|
|
if (newRequest.url().scheme() == "https") { // Not for "http": QTBUG-61397
|
2019-12-02 17:30:47 +03:00
|
|
|
// http2 seems to cause issues, as with our recommended server setup we don't support http2, disable it by default for now
|
2020-03-02 03:47:00 +03:00
|
|
|
static const bool http2EnabledEnv = qEnvironmentVariableIntValue("OWNCLOUD_HTTP2_ENABLED") == 1;
|
2020-02-23 05:04:56 +03:00
|
|
|
|
2022-08-04 19:17:34 +03:00
|
|
|
newRequest.setAttribute(QNetworkRequest::Http2AllowedAttribute, http2EnabledEnv);
|
2020-03-02 03:47:00 +03:00
|
|
|
}
|
|
|
|
#endif
|
2017-05-22 15:41:06 +03:00
|
|
|
|
2020-06-08 16:08:13 +03:00
|
|
|
const auto reply = QNetworkAccessManager::createRequest(op, newRequest, outgoingData);
|
2020-12-09 15:14:05 +03:00
|
|
|
HttpLogger::logRequest(reply, op, outgoingData);
|
2020-06-08 16:08:13 +03:00
|
|
|
return reply;
|
2013-08-01 18:22:54 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|