Skip to content

Commit

Permalink
Optimize QSslSocketPrivate::isMatchingHostname()
Browse files Browse the repository at this point in the history
- Lowercase 'peerName' only once instead of each
  time though the loop
- Use C++11 range-for instead of Q_FOREACH
- Use QMap::equal_range instead of values(Key),
  saving the creation of a QList.

Change-Id: I1229f62d706d1478960b08bb63ee9fb894364f87
Reviewed-by: Lars Knoll <[email protected]>
Reviewed-by: Richard J. Moore <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
marc-kdab committed Feb 8, 2016
1 parent b034a14 commit 8a54950
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/network/ssl/qsslsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2621,18 +2621,19 @@ QSharedPointer<QSslContext> QSslSocketPrivate::sslContext(QSslSocket *socket)

bool QSslSocketPrivate::isMatchingHostname(const QSslCertificate &cert, const QString &peerName)
{
QStringList commonNameList = cert.subjectInfo(QSslCertificate::CommonName);
const QString lowerPeerName = peerName.toLower();
const QStringList commonNames = cert.subjectInfo(QSslCertificate::CommonName);

foreach (const QString &commonName, commonNameList) {
if (isMatchingHostname(commonName.toLower(), peerName.toLower())) {
for (const QString &commonName : commonNames) {
if (isMatchingHostname(commonName.toLower(), lowerPeerName))
return true;
}
}

foreach (const QString &altName, cert.subjectAlternativeNames().values(QSsl::DnsEntry)) {
if (isMatchingHostname(altName.toLower(), peerName.toLower())) {
const auto subjectAlternativeNames = cert.subjectAlternativeNames();
const auto altNames = subjectAlternativeNames.equal_range(QSsl::DnsEntry);
for (auto it = altNames.first; it != altNames.second; ++it) {
if (isMatchingHostname(it->toLower(), lowerPeerName))
return true;
}
}

return false;
Expand Down

0 comments on commit 8a54950

Please sign in to comment.