File tree Expand file tree Collapse file tree 2 files changed +23
-6
lines changed
Expand file tree Collapse file tree 2 files changed +23
-6
lines changed 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,11 @@ 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 :
757+ if not _DIGITS_RE .fullmatch (content_length_hdr ):
759758 raise ValueError (
760- f"Invalid Content-Length header: { content_length_hdr } "
761- ) from None
759+ f"Invalid Content-Length header: { content_length_hdr !r} "
760+ )
761+ return int (content_length_hdr )
762762
763763 @property
764764 def _writer (self ) -> asyncio .Task [None ] | None :
Original file line number Diff line number Diff line change @@ -1911,7 +1911,24 @@ async def test_get_content_length(make_client_request: _RequestMaker) -> None:
19111911
19121912 # Invalid Content-Length header
19131913 req .headers ["Content-Length" ] = "invalid"
1914- with pytest .raises (ValueError , match = "Invalid Content-Length header: invalid" ):
1914+ with pytest .raises (ValueError , match = "Invalid Content-Length header" ):
1915+ req ._get_content_length ()
1916+
1917+
1918+ async def test_get_content_length_invalid_formats (
1919+ make_client_request : _RequestMaker ,
1920+ ) -> None :
1921+ req = make_client_request ("get" , URL ("http://python.org/" ))
1922+
1923+ req .headers ["Content-Length" ] = "100"
1924+ assert req ._get_content_length () == 100
1925+
1926+ req .headers ["Content-Length" ] = "-100"
1927+ with pytest .raises (ValueError , match = "Invalid Content-Length header" ):
1928+ req ._get_content_length ()
1929+
1930+ req .headers ["Content-Length" ] = " 100"
1931+ with pytest .raises (ValueError , match = "Invalid Content-Length header" ):
19151932 req ._get_content_length ()
19161933
19171934
You can’t perform that action at this time.
0 commit comments