File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 7878
7979_CONNECTION_CLOSED_EXCEPTION = ClientConnectionError ("Connection closed" )
8080_CONTAINS_CONTROL_CHAR_RE = re .compile (r"[^-!#$%&'*+.^_`|~0-9a-zA-Z]" )
81+ _DIGITS_RE = re .compile (r"\d+" , re .ASCII )
8182
8283
8384def _gen_default_accept_encoding () -> str :
@@ -753,12 +754,9 @@ def _get_content_length(self) -> int | None:
753754 return None
754755
755756 content_length_hdr = self .headers [hdrs .CONTENT_LENGTH ]
756- try :
757- return int (content_length_hdr )
758- except ValueError :
759- raise ValueError (
760- f"Invalid Content-Length header: { content_length_hdr } "
761- ) from None
757+ if not _DIGITS_RE .fullmatch (content_length_hdr ):
758+ raise ValueError (f"Invalid Content-Length header: { content_length_hdr !r} " )
759+ return int (content_length_hdr )
762760
763761 @property
764762 def _writer (self ) -> asyncio .Task [None ] | None :
Original file line number Diff line number Diff line change @@ -1822,7 +1822,24 @@ async def test_get_content_length(make_client_request: _RequestMaker) -> None:
18221822
18231823 # Invalid Content-Length header
18241824 req .headers ["Content-Length" ] = "invalid"
1825- with pytest .raises (ValueError , match = "Invalid Content-Length header: invalid" ):
1825+ with pytest .raises (ValueError , match = "Invalid Content-Length header" ):
1826+ req ._get_content_length ()
1827+
1828+
1829+ async def test_get_content_length_invalid_formats (
1830+ make_client_request : _RequestMaker ,
1831+ ) -> None :
1832+ req = make_client_request ("get" , URL ("http://python.org/" ))
1833+
1834+ req .headers ["Content-Length" ] = "100"
1835+ assert req ._get_content_length () == 100
1836+
1837+ req .headers ["Content-Length" ] = "-100"
1838+ with pytest .raises (ValueError , match = "Invalid Content-Length header" ):
1839+ req ._get_content_length ()
1840+
1841+ req .headers ["Content-Length" ] = "५" # Devengali number 5
1842+ with pytest .raises (ValueError , match = "Invalid Content-Length header" ):
18261843 req ._get_content_length ()
18271844
18281845
You can’t perform that action at this time.
0 commit comments