File indexing completed on 2024-04-28 03:55:13

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Torben Weis <weis@kde.org>
0004     SPDX-FileCopyrightText: 2000 Waldo Bastain <bastain@kde.org>
0005     SPDX-FileCopyrightText: 2000 Dawit Alemayehu <adawit@kde.org>
0006     SPDX-FileCopyrightText: 2008 Jarosław Staniek <staniek@kde.org>
0007     SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0008 
0009     SPDX-License-Identifier: LGPL-2.0-only
0010 */
0011 
0012 #ifndef KPROTOCOLMANAGER_P_H
0013 #define KPROTOCOLMANAGER_P_H
0014 
0015 #include <kiocore_export.h>
0016 
0017 #include <QCache>
0018 #include <QHostAddress>
0019 #include <QMutex>
0020 #include <QString>
0021 #include <QUrl>
0022 
0023 #include <KSharedConfig>
0024 
0025 #include "kprotocolmanager.h"
0026 
0027 class KProxyData : public QObject
0028 {
0029     Q_OBJECT
0030 public:
0031     KProxyData(const QString &workerProtocol, const QStringList &proxyAddresses)
0032         : protocol(workerProtocol)
0033         , proxyList(proxyAddresses)
0034     {
0035     }
0036 
0037     void removeAddress(const QString &address)
0038     {
0039         proxyList.removeAll(address);
0040     }
0041 
0042     QString protocol;
0043     QStringList proxyList;
0044 };
0045 
0046 class KIOCORE_EXPORT KProtocolManagerPrivate
0047 {
0048 public:
0049     using SubnetPair = QPair<QHostAddress, int>;
0050 
0051     /**
0052      * Types of proxy configuration
0053      * @li NoProxy     - No proxy is used
0054      * @li ManualProxy - Proxies are manually configured
0055      * @li PACProxy    - A Proxy configuration URL has been given
0056      * @li WPADProxy   - A proxy should be automatically discovered
0057      * @li EnvVarProxy - Use the proxy values set through environment variables.
0058      */
0059     enum ProxyType {
0060         NoProxy,
0061         ManualProxy,
0062         PACProxy,
0063         WPADProxy,
0064         EnvVarProxy,
0065     };
0066 
0067     KProtocolManagerPrivate();
0068     ~KProtocolManagerPrivate();
0069     bool shouldIgnoreProxyFor(const QUrl &url);
0070     void sync();
0071     ProxyType proxyType();
0072     bool useReverseProxy();
0073     QString readNoProxyFor();
0074     QString proxyFor(const QString &protocol);
0075     QStringList getSystemProxyFor(const QUrl &url);
0076 
0077     QMutex mutex; // protects all member vars
0078     KSharedConfig::Ptr configPtr;
0079     KSharedConfig::Ptr http_config;
0080     QString modifiers;
0081     QString useragent;
0082     QString noProxyFor;
0083     QList<SubnetPair> noProxySubnets;
0084     QCache<QString, KProxyData> cachedProxyData;
0085 
0086     QMap<QString /*mimetype*/, QString /*protocol*/> protocolForArchiveMimetypes;
0087 
0088     /**
0089      * Return the protocol to use in order to handle the given @p url
0090      * It's usually the same, except that FTP, when handled by a proxy,
0091      * needs an HTTP KIO worker.
0092      *
0093      * When a proxy is to be used, proxy contains the URL for the proxy.
0094      * @param url the url to check
0095      * @param proxy the URL of the proxy to use
0096      * @return the worker protocol (e.g. 'http'), can be null if unknown
0097      *
0098      */
0099     static QString workerProtocol(const QUrl &url, QStringList &proxy);
0100 
0101     /**
0102      * Returns all the possible proxy server addresses for @p url.
0103      *
0104      * If the selected proxy type is @ref PACProxy or @ref WPADProxy, then a
0105      * helper kded module, proxyscout, is used to determine the proxy information.
0106      * Otherwise, @ref proxyFor is used to find the proxy to use for the given url.
0107      *
0108      * If this function returns empty list, then the request is to a proxy server
0109      * must be denied. For a direct connection, this function will return a single
0110      * entry of "DIRECT".
0111      *
0112      *
0113      * @param url the URL whose proxy info is needed
0114      * @returns the proxy server address if one is available, otherwise an empty list .
0115      */
0116     static QStringList proxiesForUrl(const QUrl &url);
0117 
0118     /**
0119      * Returns the default user-agent value used for web browsing, for example
0120      * "Mozilla/5.0 (compatible; Konqueror/4.0; Linux; X11; i686; en_US) KHTML/4.0.1 (like Gecko)"
0121      *
0122      * @param keys can be any of the following:
0123      * @li 'o'    Show OS
0124      * @li 'v'    Show OS Version
0125      * @li 'p'    Show platform (only for X11)
0126      * @li 'm'    Show machine architecture
0127      * @li 'l'    Show language
0128      * @return the default user-agent value with the given @p keys
0129      */
0130     static QString defaultUserAgent(const QString &keys);
0131 
0132     /**
0133      * Returns system name, version and machine type, for example "Windows", "5.1", "i686".
0134      * This information can be used for constructing custom user-agent strings.
0135      *
0136      * @param systemName system name
0137      * @param systemVersion system version
0138      * @param machine machine type
0139 
0140      * @return true if system name, version and machine type has been provided
0141      */
0142     static bool getSystemNameVersionAndMachine(QString &systemName, QString &systemVersion, QString &machine);
0143 };
0144 
0145 #endif