2011-04-27 09:05:54 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-03-29 19:22:55 +01:00
|
|
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
2011-04-27 09:05:54 +02:00
|
|
|
*
|
|
|
|
* 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 3 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-04-26 19:47:12 +02:00
|
|
|
#include "networkproxyfactory.h"
|
|
|
|
#include "mainapplication.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2013-03-29 19:22:55 +01:00
|
|
|
#include "pac/pacmanager.h"
|
2011-04-26 19:47:12 +02:00
|
|
|
|
2013-04-06 23:44:59 +02:00
|
|
|
WildcardMatcher::WildcardMatcher(const QString &pattern)
|
|
|
|
: m_regExp(0)
|
|
|
|
{
|
|
|
|
setPattern(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
WildcardMatcher::~WildcardMatcher()
|
|
|
|
{
|
|
|
|
delete m_regExp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WildcardMatcher::setPattern(const QString &pattern)
|
|
|
|
{
|
|
|
|
m_pattern = pattern;
|
|
|
|
|
|
|
|
if (m_pattern.contains(QLatin1Char('?')) || m_pattern.contains(QLatin1Char('*'))) {
|
|
|
|
QString regexp = m_pattern;
|
|
|
|
regexp.replace(QLatin1Char('.'), QLatin1String("\\."))
|
|
|
|
.replace(QLatin1Char('*'), QLatin1String(".*"))
|
|
|
|
.replace(QLatin1Char('?'), QLatin1Char('.'));
|
|
|
|
regexp = QString("^.*%1.*$").arg(regexp);
|
|
|
|
|
|
|
|
m_regExp = new QzRegExp(regexp, Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString WildcardMatcher::pattern() const
|
|
|
|
{
|
|
|
|
return m_pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WildcardMatcher::match(const QString &str) const
|
|
|
|
{
|
|
|
|
if (!m_regExp) {
|
|
|
|
return str.contains(m_pattern, Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_regExp->indexIn(str) > -1;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
NetworkProxyFactory::NetworkProxyFactory()
|
|
|
|
: QNetworkProxyFactory()
|
2013-03-29 19:22:55 +01:00
|
|
|
, m_pacManager(new PacManager)
|
2011-11-06 17:01:23 +01:00
|
|
|
, m_proxyPreference(SystemProxy)
|
2011-04-26 19:47:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkProxyFactory::loadSettings()
|
|
|
|
{
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-04-26 19:47:12 +02:00
|
|
|
settings.beginGroup("Web-Proxy");
|
|
|
|
m_proxyPreference = ProxyPreference(settings.value("UseProxy", SystemProxy).toInt());
|
|
|
|
m_proxyType = QNetworkProxy::ProxyType(settings.value("ProxyType", QNetworkProxy::HttpProxy).toInt());
|
2012-03-21 15:05:04 +01:00
|
|
|
m_useDifferentProxyForHttps = settings.value("UseDifferentProxyForHttps", false).toBool();
|
|
|
|
|
2012-09-02 11:42:41 +02:00
|
|
|
m_hostName = settings.value("HostName", QString()).toString();
|
2011-04-26 19:47:12 +02:00
|
|
|
m_port = settings.value("Port", 8080).toInt();
|
2012-09-02 11:42:41 +02:00
|
|
|
m_username = settings.value("Username", QString()).toString();
|
|
|
|
m_password = settings.value("Password", QString()).toString();
|
2012-03-21 15:05:04 +01:00
|
|
|
|
2012-09-02 11:42:41 +02:00
|
|
|
m_httpsHostName = settings.value("HttpsHostName", QString()).toString();
|
2012-03-21 15:05:04 +01:00
|
|
|
m_httpsPort = settings.value("HttpsPort", 8080).toInt();
|
2012-09-02 11:42:41 +02:00
|
|
|
m_httpsUsername = settings.value("HttpsUsername", QString()).toString();
|
|
|
|
m_httpsPassword = settings.value("HttpsPassword", QString()).toString();
|
2012-03-21 15:05:04 +01:00
|
|
|
|
2013-04-06 23:44:59 +02:00
|
|
|
QStringList exceptions = settings.value("ProxyExceptions", QStringList() << "localhost" << "127.0.0.1").toStringList();
|
2011-04-26 19:47:12 +02:00
|
|
|
settings.endGroup();
|
2013-03-29 19:22:55 +01:00
|
|
|
|
2013-04-06 23:44:59 +02:00
|
|
|
qDeleteAll(m_proxyExceptions);
|
|
|
|
m_proxyExceptions.clear();
|
|
|
|
|
|
|
|
foreach (const QString &exception, exceptions) {
|
|
|
|
m_proxyExceptions.append(new WildcardMatcher(exception.trimmed()));
|
|
|
|
}
|
|
|
|
|
2013-03-29 19:22:55 +01:00
|
|
|
m_pacManager->loadSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
PacManager* NetworkProxyFactory::pacManager() const
|
|
|
|
{
|
|
|
|
return m_pacManager;
|
2011-04-26 19:47:12 +02:00
|
|
|
}
|
|
|
|
|
2013-05-27 22:20:30 +02:00
|
|
|
NetworkProxyFactory::ProxyPreference NetworkProxyFactory::proxyPreference() const
|
|
|
|
{
|
|
|
|
return m_proxyPreference;
|
|
|
|
}
|
|
|
|
|
2011-04-26 19:47:12 +02:00
|
|
|
QList<QNetworkProxy> NetworkProxyFactory::queryProxy(const QNetworkProxyQuery &query)
|
|
|
|
{
|
2013-03-29 19:40:15 +01:00
|
|
|
QList<QNetworkProxy> proxyList;
|
2011-04-26 19:47:12 +02:00
|
|
|
|
2013-04-06 23:44:59 +02:00
|
|
|
if (m_proxyPreference == NoProxy) {
|
2013-03-29 19:40:15 +01:00
|
|
|
proxyList.append(QNetworkProxy::NoProxy);
|
|
|
|
return proxyList;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-04-26 19:47:12 +02:00
|
|
|
|
2013-04-06 23:44:59 +02:00
|
|
|
const QString &urlHost = query.url().host();
|
|
|
|
foreach (WildcardMatcher* m, m_proxyExceptions) {
|
|
|
|
if (m->match(urlHost)) {
|
|
|
|
proxyList.append(QNetworkProxy::NoProxy);
|
|
|
|
return proxyList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-26 19:47:12 +02:00
|
|
|
switch (m_proxyPreference) {
|
|
|
|
case SystemProxy:
|
2013-03-29 19:40:15 +01:00
|
|
|
proxyList.append(systemProxyForQuery(query));
|
|
|
|
break;
|
2011-04-26 19:47:12 +02:00
|
|
|
|
2013-03-29 19:22:55 +01:00
|
|
|
case ProxyAutoConfig:
|
2013-03-29 19:40:15 +01:00
|
|
|
proxyList.append(m_pacManager->queryProxy(query.url()));
|
|
|
|
break;
|
2013-03-29 19:22:55 +01:00
|
|
|
|
2013-03-29 19:40:15 +01:00
|
|
|
case DefinedProxy: {
|
|
|
|
QNetworkProxy proxy(m_proxyType);
|
2012-03-21 15:05:04 +01:00
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
if (m_useDifferentProxyForHttps && query.protocolTag() == QLatin1String("https")) {
|
2012-03-21 15:05:04 +01:00
|
|
|
proxy.setHostName(m_httpsHostName);
|
|
|
|
proxy.setPort(m_httpsPort);
|
|
|
|
proxy.setUser(m_httpsUsername);
|
|
|
|
proxy.setPassword(m_httpsPassword);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
proxy.setHostName(m_hostName);
|
|
|
|
proxy.setPort(m_port);
|
|
|
|
proxy.setUser(m_username);
|
|
|
|
proxy.setPassword(m_password);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proxy.hostName().isEmpty()) {
|
|
|
|
proxy = QNetworkProxy::NoProxy;
|
|
|
|
}
|
|
|
|
|
2013-03-29 19:40:15 +01:00
|
|
|
proxyList.append(proxy);
|
2012-03-21 15:05:04 +01:00
|
|
|
break;
|
2013-03-29 19:40:15 +01:00
|
|
|
}
|
2012-03-21 15:05:04 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
qWarning("NetworkProxyFactory::queryProxy Unknown proxy type!");
|
2011-04-26 19:47:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-03-29 19:40:15 +01:00
|
|
|
if (!proxyList.contains(QNetworkProxy::NoProxy)) {
|
|
|
|
proxyList.append(QNetworkProxy::NoProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxyList;
|
2011-04-26 19:47:12 +02:00
|
|
|
}
|
2013-04-06 23:44:59 +02:00
|
|
|
|
|
|
|
NetworkProxyFactory::~NetworkProxyFactory()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_proxyExceptions);
|
|
|
|
}
|