Skip to content

Commit

Permalink
Fixed headers issue (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
andruten committed Oct 19, 2023
1 parent e76a7b4 commit 695c5a9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
* Declare Django 4.2 support


0.11.1 (Unreleased)
===================

* Fixed ignored headers issue.


0.11.0 (2023-02-26)
===================

Expand Down
18 changes: 15 additions & 3 deletions revproxy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@

#: Regex used to find charset in a html content type
_get_charset_re = re.compile(r';\s*charset=(?P<charset>[^\s;]+)', re.I)
#: Regex used to clean extra HTTP prefixes in headers
_get_header_name_re = re.compile(
r'((http[-|_])*)(?P<header_name>(http[-|_]).*)',
re.I,
)


def is_html_content_type(content_type):
Expand Down Expand Up @@ -102,15 +107,22 @@ def get_charset(content_type):


def required_header(header):
"""Function that verify if the header parameter is a essential header
"""Function that verify if the header parameter is an essential header
:param header: A string represented a header
:returns: A boolean value that represent if the header is required
"""
if header in IGNORE_HEADERS:
matched = _get_header_name_re.search(header)

# Ensure there is only one HTTP prefix in the header
header_name = matched.group('header_name') if matched else header

header_name_upper = header_name.upper().replace('-', '_')

if header_name_upper in IGNORE_HEADERS:
return False

if header.startswith('HTTP_') or header == 'CONTENT_TYPE':
if header_name_upper.startswith('HTTP_') or header == 'CONTENT_TYPE':
return True

return False
Expand Down
22 changes: 21 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from django.test import TestCase

from unittest.mock import PropertyMock
from unittest.mock import PropertyMock, MagicMock

from revproxy import utils

Expand All @@ -28,9 +28,29 @@ def test_required_header(self):
def test_ignore_host_header(self):
self.assertFalse(utils.required_header('HTTP_HOST'))
self.assertFalse(utils.required_header('HTTP_REMOTE_USER'))
self.assertFalse(utils.required_header('HTTP_Remote-User'))
self.assertFalse(utils.required_header('HTTP-HTTP-Remote-User'))
self.assertFalse(utils.required_header('HTTP-Remote_User'))
self.assertFalse(utils.required_header('Http-Remote-User'))
self.assertFalse(utils.required_header('HTTP_ACCEPT_ENCODING'))
self.assertFalse(utils.required_header('WRONG_HEADER'))

def test_normalize_request_headers(self):
request = MagicMock(
META={
'HTTP_ANY_THING_AFTER_HTTP': 'value',
'HTTP-HTTP-Remote-User': 'username',
'HTTP_REMOTE_USER': 'username',
'Http-Remote-User': 'username',
'HTTP_USER_AGENT': 'useragent',
},
)
normalized_headers = utils.normalize_request_headers(request)
self.assertEqual(
normalized_headers,
{'Any-Thing-After-Http': 'value', 'User-Agent': 'useragent'},
)

def test_ignore_accept_encoding_header(self):
self.assertFalse(utils.required_header('HTTP_ACCEPT_ENCODING'))

Expand Down

0 comments on commit 695c5a9

Please sign in to comment.