Skip to content

Commit 4ff6dfa

Browse files
fchevassu-antidotetr
authored andcommitted
Simplify policy_callback condition
1 parent 7321aa8 commit 4ff6dfa

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/webserver.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,18 @@ MHD_Result policy_callback(void *cls, const struct sockaddr* addr, socklen_t add
453453
// Parameter needed to respect MHD interface, but not needed here.
454454
std::ignore = addrlen;
455455

456-
if (!(static_cast<webserver*>(cls))->ban_system_enabled) return MHD_YES;
457-
458-
std::shared_lock bans_lock(bans_mutex);
459-
std::shared_lock allowances_lock(allowances_mutex);
460-
if ((((static_cast<webserver*>(cls))->default_policy == http_utils::ACCEPT) &&
461-
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr))) &&
462-
(!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr)))) ||
463-
(((static_cast<webserver*>(cls))->default_policy == http_utils::REJECT) &&
464-
((!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr))) ||
465-
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr)))))) {
456+
const auto ws = static_cast<webserver*>(cls);
457+
458+
if (!ws->ban_system_enabled) return MHD_YES;
459+
460+
std::shared_lock bans_lock(ws->bans_mutex);
461+
std::shared_lock allowances_lock(ws->allowances_mutex);
462+
const bool is_banned = ws->bans.count(ip_representation(addr));
463+
const bool is_allowed = ws->allowances.count(ip_representation(addr));
464+
465+
if ((ws->default_policy == http_utils::ACCEPT && is_banned && !is_allowed) ||
466+
(ws->default_policy == http_utils::REJECT && (!is_allowed || is_banned)))
467+
{
466468
return MHD_NO;
467469
}
468470

0 commit comments

Comments
 (0)