Skip to content

Commit 589ff8f

Browse files
Simplify policy_callback condition
1 parent 145e9aa commit 589ff8f

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/webserver.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -407,16 +407,18 @@ MHD_Result policy_callback(void *cls, const struct sockaddr* addr, socklen_t add
407407
// Parameter needed to respect MHD interface, but not needed here.
408408
std::ignore = addrlen;
409409

410-
if (!(static_cast<webserver*>(cls))->ban_system_enabled) return MHD_YES;
411-
412-
std::shared_lock bans_lock(bans_mutex);
413-
std::shared_lock allowances_lock(allowances_mutex);
414-
if ((((static_cast<webserver*>(cls))->default_policy == http_utils::ACCEPT) &&
415-
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr))) &&
416-
(!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr)))) ||
417-
(((static_cast<webserver*>(cls))->default_policy == http_utils::REJECT) &&
418-
((!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr))) ||
419-
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr)))))) {
410+
const auto ws = static_cast<webserver*>(cls);
411+
412+
if (!ws->ban_system_enabled) return MHD_YES;
413+
414+
std::shared_lock bans_lock(ws->bans_mutex);
415+
std::shared_lock allowances_lock(ws->allowances_mutex);
416+
const bool is_banned = ws->bans.count(ip_representation(addr));
417+
const bool is_allowed = ws->allowances.count(ip_representation(addr));
418+
419+
if ((ws->default_policy == http_utils::ACCEPT && is_banned && !is_allowed) ||
420+
(ws->default_policy == http_utils::REJECT && (!is_allowed || is_banned)))
421+
{
420422
return MHD_NO;
421423
}
422424

0 commit comments

Comments
 (0)