File indexing completed on 2024-05-19 05:05:45

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2020 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0018  ***************************************************************************/
0019 
0020 #ifndef KBIBTEX_NETWORKING_INTERNALNETWORKACCESSMANAGER_H
0021 #define KBIBTEX_NETWORKING_INTERNALNETWORKACCESSMANAGER_H
0022 
0023 #include <QNetworkAccessManager>
0024 #include <QUrl>
0025 #include <QMap>
0026 
0027 #ifdef HAVE_KF
0028 #include "kbibtexnetworking_export.h"
0029 #endif // HAVE_KF
0030 
0031 class QNetworkAccessManager;
0032 class QNetworkReply;
0033 class QTimer;
0034 
0035 class HTTPEquivCookieJar;
0036 
0037 /**
0038  * @author Thomas Fischer <fischer@unix-ag.uni-kl.de>
0039  */
0040 class KBIBTEXNETWORKING_EXPORT InternalNetworkAccessManager : public QNetworkAccessManager
0041 {
0042     Q_OBJECT
0043 
0044 public:
0045     static InternalNetworkAccessManager &instance();
0046 
0047     QNetworkReply *get(QNetworkRequest &request, const QUrl &oldUrl);
0048     QNetworkReply *get(QNetworkRequest &request, const QNetworkReply *oldReply = nullptr);
0049 
0050     void mergeHtmlHeadCookies(const QString &htmlCode, const QUrl &url);
0051 
0052     void setNetworkReplyTimeout(QNetworkReply *reply, int timeOutSec = 30);
0053 
0054     /**
0055      * Reverse the obfuscation of an API key. Given a byte
0056      * array holding the obfuscated API key, restore and
0057      * return the original API key.
0058      *
0059      * @param obfuscated obfuscated API key
0060      * @return restored original API key if succeeded, empty on error
0061      */
0062     static QString reverseObfuscate(const QByteArray &obfuscated);
0063 
0064     /**
0065      * Remove API keys from an URL.
0066      *
0067      * @param url URL where API keys have to be removed
0068      * @return a reference to the URL passed to this function
0069      */
0070     static QUrl removeApiKey(QUrl url);
0071 
0072     /**
0073      * Remove API keys from a string such an error message.
0074      *
0075      * @param text string where API keys have to be removed
0076      * @return string with the API keys removed (unmodified if not API keys were contained)
0077      */
0078     static QString removeApiKey(const QString &text);
0079 
0080 protected:
0081     InternalNetworkAccessManager(QObject *parent = nullptr);
0082     class HTTPEquivCookieJar;
0083     HTTPEquivCookieJar *cookieJar;
0084 
0085 private:
0086     QMap<QTimer *, QNetworkReply *> m_mapTimerToReply;
0087 
0088     static QString userAgentString;
0089 
0090     static QString userAgent();
0091 
0092 private Q_SLOTS:
0093     void networkReplyTimeout();
0094     void networkReplyFinished();
0095     void logSslErrors(const QList<QSslError> &errors);
0096 };
0097 
0098 #endif // KBIBTEX_NETWORKING_INTERNALNETWORKACCESSMANAGER_H