QNetworkProxy: don't allocate when parsing [1/2]: loop body

Instead of manipulating a QByteArray, do it with a QLatin1String,
which is free.  Also, don't prepend a dot to force matches at label
boundaries, check that there's a dot where the match occurred. Saves
two allocations per iteration. Finally, pull the query's
peerHostName() initialization out of the loop - it doesn't depend on
the loop variable.

Change-Id: I6dfdae711f0bd8941af69bfbcfda7873a40e4b80
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2020-04-25 17:58:03 +02:00
parent 0daed8dee8
commit 68a20d6941

View File

@ -57,30 +57,34 @@ static bool ignoreProxyFor(const QNetworkProxyQuery &query)
if (noProxy.isEmpty()) if (noProxy.isEmpty())
return false; return false;
const QString host = query.peerHostName();
const QList<QByteArray> noProxyTokens = noProxy.split(','); const QList<QByteArray> noProxyTokens = noProxy.split(',');
for (const QByteArray &rawToken : noProxyTokens) { for (const QByteArray &rawToken : noProxyTokens) {
QByteArray token = rawToken.trimmed(); auto token = QLatin1String(rawToken).trimmed();
QString peerHostName = query.peerHostName();
// Since we use suffix matching, "*" is our 'default' behaviour // Since we use suffix matching, "*" is our 'default' behaviour
if (token.startsWith('*')) if (token.startsWith(u'*'))
token = token.mid(1); token = token.mid(1);
// Harmonize trailing dot notation // Harmonize trailing dot notation
if (token.endsWith('.') && !peerHostName.endsWith('.')) if (token.endsWith(u'.') && !host.endsWith(u'.'))
token = token.left(token.length()-1); token = token.chopped(1);
// We prepend a dot to both values, so that when we do a suffix match, if (token.startsWith(u'.')) // leading dot is implied
// we don't match "donotmatch.com" with "match.com" token = token.mid(1);
if (!token.startsWith('.'))
token.prepend('.');
if (!peerHostName.startsWith('.')) if (host.endsWith(token)) {
peerHostName.prepend('.');
if (peerHostName.endsWith(QLatin1String(token))) // Make sure that when we have a suffix match,
return true; // we don't match "donotmatch.com" with "match.com"
if (host.size() == token.size()) // iow: host == token
return true;
if (host[host.size() - token.size() - 1] == u'.') // match follows a dot
return true;
}
} }
return false; return false;