QHttpServerRequestFilter: rewrite isRequestAllowed() using STL algorithms

Now the code reads like English:

- if the whitelist isn't empty:
  - return whether any of whitelist matches
- otherwise
  - return whether none of blacklist matches

Also avoids the unintended QList detaches, and fixes the incorrect
mutable for loop variables.

Amends 2d9e986800.

Pick-to: 6.10
Change-Id: I2cc9aa0f8bed56abc97af642a2c908b9769a20a3
Reviewed-by: Lena Biliaieva <lena.biliaieva@qt.io>
Reviewed-by: Øystein Heskestad <oystein.heskestad@qt.io>
This commit is contained in:
Marc Mutz 2025-06-24 12:25:31 +02:00
parent 719e409526
commit 08c22f9138
1 changed files with 11 additions and 12 deletions

View File

@ -6,6 +6,8 @@
#include <QtCore/qdatetime.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
const int QHttpServerRequestFilterPrivate::cPeriodDurationMSec = 1000;
@ -25,20 +27,17 @@ void QHttpServerRequestFilter::setConfiguration(const QHttpServerConfiguration &
bool QHttpServerRequestFilter::isRequestAllowed(const QHostAddress &peerAddress) const
{
if (auto whitelist = m_config.whitelist(); !whitelist.empty()) {
for (auto &whitelistedSubnet : whitelist) {
if (peerAddress.isInSubnet(whitelistedSubnet))
return true;
}
return false;
}
const auto matches = [](const QHostAddress &addr) {
return [&addr] (const auto &subnet) {
return addr.isInSubnet(subnet);
};
};
for (auto &blacklistedSubnet : m_config.blacklist()) {
if (peerAddress.isInSubnet(blacklistedSubnet))
return false;
}
if (const auto whitelist = m_config.whitelist(); !whitelist.empty())
return std::any_of(whitelist.cbegin(), whitelist.cend(), matches(peerAddress));
return true;
const auto blacklist = m_config.blacklist();
return std::none_of(blacklist.cbegin(), blacklist.cend(), matches(peerAddress));
}
bool QHttpServerRequestFilter::isRequestWithinRate(const QHostAddress &peerAddress)