Skip to content

Commit 18eaf29

Browse files
committed
perf: Avoid checks for known header names
1 parent a24b6e3 commit 18eaf29

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

multipart.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ def parse_options_header(header, options=None, unquote=header_unquote):
287287
##############################################################################
288288

289289

290+
# Constants used by the parser
291+
_HEADER_EXPECTED = frozenset(["Content-Disposition", "Content-Type", "Content-Length"])
290292
# Parser states as constants
291293
_PREAMBLE = "PREAMBLE"
292294
_HEADER = "HEADER"
@@ -555,8 +557,9 @@ def _on_segment_headerline(self, line: Union[bytes, bytearray]):
555557
name = name.strip().title()
556558
if not col or not name:
557559
raise ParserError("Malformed segment header")
558-
if " " in name or not name.isascii() or not name.isprintable():
559-
raise ParserError("Invalid segment header name")
560+
if name not in _HEADER_EXPECTED:
561+
if " " in name or not name.isascii() or not name.isprintable():
562+
raise ParserError("Invalid segment header name")
560563
value = value.strip()
561564
except UnicodeDecodeError as err:
562565
raise ParserError("Segment header failed to decode", err)

0 commit comments

Comments
 (0)