Skip to content

Commit 75a7a51

Browse files
authored
Fix byte regular expressions (#320)
* Correct byte regular expressions * Format * [Mega-Linter] Apply linters fixes Co-authored-by: TimPansino <[email protected]>
1 parent 59b3dbe commit 75a7a51

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

newrelic/api/html_insertion.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,26 @@
1414

1515
import re
1616

17-
_head_re = re.compile(r'<head[^>]*>', re.IGNORECASE)
17+
_head_re = re.compile(b"<head[^>]*>", re.IGNORECASE)
1818

19-
_xua_meta_re = re.compile(r"""<\s*meta[^>]+http-equiv\s*=\s*['"]"""
20-
r"""x-ua-compatible['"][^>]*>""", re.IGNORECASE)
19+
_xua_meta_re = re.compile(
20+
b"""<\\s*meta[^>]+http-equiv\\s*=\\s*['"]""" b"""x-ua-compatible['"][^>]*>""",
21+
re.IGNORECASE,
22+
)
2123

22-
_charset_meta_re = re.compile(r"""<\s*meta[^>]+charset\s*=[^>]*>""",
23-
re.IGNORECASE)
24+
_charset_meta_re = re.compile(b"""<\\s*meta[^>]+charset\\s*=[^>]*>""", re.IGNORECASE)
2425

25-
_attachment_meta_re = re.compile(r"""<\s*meta[^>]+http-equiv\s*=\s*['"]"""
26-
r"""content-disposition['"][^>]*content\s*=\s*(?P<quote>['"])"""
27-
r"""\s*attachment(\s*;[^>]*)?(?P=quote)[^>]*>""",
28-
re.IGNORECASE)
26+
_attachment_meta_re = re.compile(
27+
b"""<\\s*meta[^>]+http-equiv\\s*=\\s*['"]"""
28+
b"""content-disposition['"][^>]*content\\s*=\\s*(?P<quote>['"])"""
29+
b"""\\s*attachment(\\s*;[^>]*)?(?P=quote)[^>]*>""",
30+
re.IGNORECASE,
31+
)
2932

30-
_body_re = re.compile(r'<body[^>]*>', re.IGNORECASE)
33+
_body_re = re.compile(b"<body[^>]*>", re.IGNORECASE)
3134

32-
def insert_html_snippet(data, html_to_be_inserted, search_limit=64*1024):
35+
36+
def insert_html_snippet(data, html_to_be_inserted, search_limit=64 * 1024):
3337
# First determine if we have a body tag. If we don't we
3438
# always give up even though strictly speaking we may not
3539
# actually need it to exist. This is to ensure that we have
@@ -57,10 +61,11 @@ def insert_html_snippet(data, html_to_be_inserted, search_limit=64*1024):
5761
# patterns which follow seem to be expensive enough that a
5862
# short a string as possible helps.
5963

60-
tail, data = data[body.start():], data[:body.start()]
64+
start = body.start()
65+
tail, data = data[start:], data[:start]
6166

6267
def insert_at_index(index):
63-
return b''.join((data[:index], text, data[index:], tail))
68+
return b"".join((data[:index], text, data[index:], tail))
6469

6570
# Search for instance of a content disposition meta tag
6671
# indicating that the response is actually being served up
@@ -76,22 +81,24 @@ def insert_at_index(index):
7681
xua_meta = _xua_meta_re.search(data)
7782
charset_meta = _charset_meta_re.search(data)
7883

79-
index = max(xua_meta and xua_meta.end() or 0,
80-
charset_meta and charset_meta.end() or 0)
84+
index = max(
85+
xua_meta and xua_meta.end() or 0, charset_meta and charset_meta.end() or 0
86+
)
8187

8288
if index:
83-
return insert_at_index(index)
89+
return insert_at_index(index)
8490

8591
# Next try for the start of head section.
8692

8793
head = _head_re.search(data)
8894

8995
if head:
90-
return insert_at_index(head.end())
96+
return insert_at_index(head.end())
9197

9298
# Finally if no joy, insert before the start of the body.
9399

94100
return insert_at_index(body.start())
95101

102+
96103
def verify_body_exists(data):
97104
return _body_re.search(data)

0 commit comments

Comments
 (0)