From 196eb0fe77b78e2e5ca02c506c3837c2b1a7964c Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Wed, 20 Dec 2023 19:15:38 +1300 Subject: [PATCH] [networking] Strip whitespace around header values (#8802) Fixes https://github.com/yt-dlp/yt-dlp/issues/8729 Authored by: coletdjnz --- test/test_utils.py | 5 +++++ yt_dlp/utils/networking.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/test_utils.py b/test/test_utils.py index 100f11788..6c8571f98 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -2370,6 +2370,11 @@ def test_http_header_dict(self): headers4 = HTTPHeaderDict({'ytdl-test': 'data;'}) self.assertEqual(set(headers4.items()), {('Ytdl-Test', 'data;')}) + # common mistake: strip whitespace from values + # https://github.com/yt-dlp/yt-dlp/issues/8729 + headers5 = HTTPHeaderDict({'ytdl-test': ' data; '}) + self.assertEqual(set(headers5.items()), {('Ytdl-Test', 'data;')}) + def test_extract_basic_auth(self): assert extract_basic_auth('http://:foo.bar') == ('http://:foo.bar', None) assert extract_basic_auth('http://foo.bar') == ('http://foo.bar', None) diff --git a/yt_dlp/utils/networking.py b/yt_dlp/utils/networking.py index ed0250011..4b73252cb 100644 --- a/yt_dlp/utils/networking.py +++ b/yt_dlp/utils/networking.py @@ -67,7 +67,7 @@ def __init__(self, *args, **kwargs): def __setitem__(self, key, value): if isinstance(value, bytes): value = value.decode('latin-1') - super().__setitem__(key.title(), str(value)) + super().__setitem__(key.title(), str(value).strip()) def __getitem__(self, key): return super().__getitem__(key.title())