Skip to content

Commit

Permalink
Simplify policy_callback condition
Browse files Browse the repository at this point in the history
  • Loading branch information
fchevassu-antidot committed Sep 26, 2023
1 parent 145e9aa commit 589ff8f
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,18 @@ MHD_Result policy_callback(void *cls, const struct sockaddr* addr, socklen_t add
// Parameter needed to respect MHD interface, but not needed here.
std::ignore = addrlen;

if (!(static_cast<webserver*>(cls))->ban_system_enabled) return MHD_YES;

std::shared_lock bans_lock(bans_mutex);
std::shared_lock allowances_lock(allowances_mutex);
if ((((static_cast<webserver*>(cls))->default_policy == http_utils::ACCEPT) &&
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr))) &&
(!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr)))) ||
(((static_cast<webserver*>(cls))->default_policy == http_utils::REJECT) &&
((!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr))) ||
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr)))))) {
const auto ws = static_cast<webserver*>(cls);

if (!ws->ban_system_enabled) return MHD_YES;

std::shared_lock bans_lock(ws->bans_mutex);
std::shared_lock allowances_lock(ws->allowances_mutex);
const bool is_banned = ws->bans.count(ip_representation(addr));
const bool is_allowed = ws->allowances.count(ip_representation(addr));

if ((ws->default_policy == http_utils::ACCEPT && is_banned && !is_allowed) ||
(ws->default_policy == http_utils::REJECT && (!is_allowed || is_banned)))
{
return MHD_NO;
}

Expand Down

0 comments on commit 589ff8f

Please sign in to comment.