File indexing completed on 2024-04-28 16:44:07

0001 /*
0002     SPDX-FileCopyrightText: 2019-2021 Harald Sitter <sitter@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "connection.h"
0008 
0009 #include <KIO/TransferJob>
0010 #include <QUrlQuery>
0011 
0012 #include "bugzilla_debug.h"
0013 
0014 namespace Bugzilla
0015 {
0016 // Static container for global default connection.
0017 // We need a container here because the connection may be anything derived from
0018 // Connection and its effective type may change (e.g. in autotests).
0019 class GlobalConnection
0020 {
0021 public:
0022     ~GlobalConnection()
0023     {
0024         delete m_connection;
0025     }
0026 
0027     Connection *m_connection = new HTTPConnection;
0028 };
0029 
0030 Q_GLOBAL_STATIC(GlobalConnection, s_connection)
0031 
0032 Connection &connection()
0033 {
0034     return *(s_connection->m_connection);
0035 }
0036 
0037 void setConnection(Connection *newConnection)
0038 {
0039     delete s_connection->m_connection;
0040     s_connection->m_connection = newConnection;
0041 }
0042 
0043 HTTPConnection::HTTPConnection(const QUrl &root, QObject *parent)
0044     : Connection(parent)
0045     , m_root(root)
0046 {
0047 }
0048 
0049 HTTPConnection::~HTTPConnection() = default;
0050 
0051 void HTTPConnection::setToken(const QString &authToken)
0052 {
0053     m_token = authToken;
0054 }
0055 
0056 APIJob *HTTPConnection::get(const QString &path, const Query &query) const
0057 {
0058     qCDebug(BUGZILLA_LOG) << path << query.toString();
0059     auto job = new TransferAPIJob(KIO::get(url(path, query), KIO::Reload, KIO::HideProgressInfo));
0060     return job;
0061 }
0062 
0063 APIJob *HTTPConnection::post(const QString &path, const QByteArray &data, const Query &query) const
0064 {
0065     qCDebug(BUGZILLA_LOG) << path << query.toString();
0066     auto job = new TransferAPIJob(KIO::http_post(url(path, query), data, KIO::HideProgressInfo));
0067     return job;
0068 }
0069 
0070 APIJob *HTTPConnection::put(const QString &path, const QByteArray &data, const Query &query) const
0071 {
0072     qCDebug(BUGZILLA_LOG) << path << query.toString();
0073     auto job = new TransferAPIJob(KIO::put(url(path, query), KIO::HideProgressInfo));
0074     job->setPutData(data);
0075     return job;
0076 }
0077 
0078 QUrl HTTPConnection::root() const
0079 {
0080     return m_root;
0081 }
0082 
0083 QUrl HTTPConnection::url(const QString &appendix, Query query) const
0084 {
0085     QUrl url(m_root);
0086     url.setPath(m_root.path() + appendix);
0087 
0088     if (!m_token.isEmpty()) {
0089         query.addQueryItem(QStringLiteral("token"), m_token);
0090     }
0091 
0092     // https://bugs.kde.org/show_bug.cgi?id=413920
0093     // Force encoding. Query by default wouldn't encode '+' and bugzilla doesn't like that...
0094     // For any query argument. Tested with username, password, and products (for bug search)
0095     // on bugzilla 5.0.6. As a result let's force full encoding on every argument.
0096     QUrlQuery escapedQuery;
0097     for (auto it = query.cbegin(); it != query.cend(); ++it) {
0098         escapedQuery.addQueryItem(it.key(), QString::fromUtf8(it.value().toUtf8().toPercentEncoding()));
0099     }
0100 
0101     url.setQuery(escapedQuery);
0102     return url;
0103 }
0104 
0105 } // namespace Bugzilla