Optimized CommonMiddleware's check for DISALLOWED_USER_AGENTS.

Avoided getattribute + getitem for each DISALLOWED_USER_AGENTS.
This commit is contained in:
Brad 2019-03-30 09:02:57 -04:00 committed by Tim Graham
parent e8774a74cc
commit ad72895c60
1 changed files with 3 additions and 2 deletions

View File

@ -38,9 +38,10 @@ class CommonMiddleware(MiddlewareMixin):
"""
# Check for denied User-Agents
if 'HTTP_USER_AGENT' in request.META:
user_agent = request.META.get('HTTP_USER_AGENT')
if user_agent is not None:
for user_agent_regex in settings.DISALLOWED_USER_AGENTS:
if user_agent_regex.search(request.META['HTTP_USER_AGENT']):
if user_agent_regex.search(user_agent):
raise PermissionDenied('Forbidden user agent')
# Check for a redirect based on settings.PREPEND_WWW