Skip to content
This repository was archived by the owner on Aug 8, 2024. It is now read-only.

Change http_ver regexp to fix HTTP/2 case #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apache_log_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def make_regex(format_template):

def extra_request_from_first_line(matched_strings):
first_line = matched_strings['request_first_line']
match = re.match("^(?P<method>GET|HEAD|POST|OPTIONS|PUT|CONNECT|PATCH|PROPFIND|DELETE)\s?(?P<url>.{,10000}?)(\s+HTTP/(?P<http_ver>1.[01]))?$", first_line)
match = re.match("^(?P<method>GET|HEAD|POST|OPTIONS|PUT|CONNECT|PATCH|PROPFIND|DELETE)\s?(?P<url>.{,10000}?)(\s+HTTP/(?P<http_ver>(1.[01])|2))?$", first_line)
if match is None:
# Possibly garbage, ignore it
results = { 'request_first_line': first_line, 'request_method': '', 'request_url': '', 'request_http_ver': ''}
Expand Down
25 changes: 25 additions & 0 deletions apache_log_parser/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,31 @@ def test_issue10_ipv6(self):
sample1 = '10.178.98.112 2607:5300:60:2c74:: - - [24/Mar/2015:16:40:45 -0400] "GET /category/blog/page/3 HTTP/1.0" 200 41207 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/10.10 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30"'
log_data1 = parser(sample1)

def test_issue22_http2(self):
line_parser = apache_log_parser.make_parser("%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"")
sample = '''137.226.113.25 - - [31/Dec/2017:03:14:19 +0100] "GET / HTTP/2" 200 0 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"'''
log_data = line_parser(sample)
expected_data = {'bytes_tx': '0',
'remote_host': '137.226.113.25',
'remote_logname': '-',
'remote_user': '-',
'request_first_line': 'GET / HTTP/2',
'request_header_referer': '-',
'request_header_user_agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; '
'rv:50.0) Gecko/20100101 Firefox/50.0',
'request_header_user_agent__browser__family': 'Firefox',
'request_header_user_agent__browser__version_string': '50.0',
'request_header_user_agent__is_mobile': False,
'request_header_user_agent__os__family': 'Ubuntu',
'request_header_user_agent__os__version_string': '',
'request_http_ver': '2',
'request_method': 'GET',
'request_url': '/',
'status': '200',
'time_received': '[31/Dec/2017:03:14:19 +0100]'}
for k,v in expected_data.items():
self.assertEqual(log_data[k], v)

def test_doctest_readme(self):
doctest.testfile("../README.md")

Expand Down