From 3013758f17beecb55bc40d28beef5b55f83dec2a Mon Sep 17 00:00:00 2001 From: Adrien Bustany Date: Mon, 23 Apr 2012 10:49:11 +0300 Subject: [PATCH] QNetworkProxyFactory: check the "no_proxy" environment variable The QNetworkProxyFactory class considered the http_proxy environment variable, but not the no_proxy one. This commit adds no_proxy handling, loosely modeled after the way curl does it. Change-Id: Ibb9e5ffcb30fed5c95dc9fc3bc4177e20d025a50 Reviewed-by: Thiago Macieira Reviewed-by: Shane Kearns --- src/network/kernel/qnetworkproxy_generic.cpp | 38 ++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/network/kernel/qnetworkproxy_generic.cpp b/src/network/kernel/qnetworkproxy_generic.cpp index c0c6b9fe42..66053b0b47 100644 --- a/src/network/kernel/qnetworkproxy_generic.cpp +++ b/src/network/kernel/qnetworkproxy_generic.cpp @@ -47,16 +47,50 @@ #ifndef QT_NO_NETWORKPROXY /* - * Construct a proxy from the environment variable http_proxy. + * Construct a proxy from the environment variables http_proxy and no_proxy. * Or no system proxy. Just return a list with NoProxy. */ QT_BEGIN_NAMESPACE -QList QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &) +static bool ignoreProxyFor(const QNetworkProxyQuery &query) +{ + const QList noProxyTokens = qgetenv("no_proxy").split(','); + + foreach (const QByteArray rawToken, noProxyTokens) { + QByteArray token = rawToken.trimmed(); + QString peerHostName = query.peerHostName(); + + // Since we use suffix matching, "*" is our 'default' behaviour + if (token.startsWith("*")) + token = token.mid(1); + + // Harmonize trailing dot notation + if (token.endsWith('.') && !peerHostName.endsWith('.')) + token = token.left(token.length()-1); + + // We prepend a dot to both values, so that when we do a suffix match, + // we don't match "donotmatch.com" with "match.com" + if (!token.startsWith('.')) + token.prepend('.'); + + if (!peerHostName.startsWith('.')) + peerHostName.prepend('.'); + + if (peerHostName.endsWith(QString::fromLatin1(token))) + return true; + } + + return false; +} + +QList QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query) { QList proxyList; + if (ignoreProxyFor(query)) + return proxyList << QNetworkProxy::NoProxy; + QByteArray proxy_env = qgetenv("http_proxy"); if (!proxy_env.isEmpty()) { QUrl url = QUrl(QString::fromLocal8Bit(proxy_env));