From bc6b9bcd6554c10aa321cbfe151272e0df1a869b Mon Sep 17 00:00:00 2001 From: pukkandan Date: Tue, 1 Jun 2021 18:05:41 +0530 Subject: [PATCH] [utils] Escape URLs in `sanitized_Request`, not `sanitize_url` d2558234cf5dd12d6896eed5427b7dcdb3ab7b5a added escaping of URLs while sanitizing. However, `sanitize_url` may not always receive an actual URL. Eg: When using `yt-dlp "search query" --default-search ytsearch`, `search query` gets escaped to `search%20query` before being prefixed with `ytsearch:` which is not the intended behavior. So the escaping is moved to `sanitized_Request` instead. --- test/test_utils.py | 1 + yt_dlp/utils.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index a8666caabd..cf541de4ad 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -239,6 +239,7 @@ def test_sanitize_url(self): self.assertEqual(sanitize_url('httpss://foo.bar'), 'https://foo.bar') self.assertEqual(sanitize_url('rmtps://foo.bar'), 'rtmps://foo.bar') self.assertEqual(sanitize_url('https://foo.bar'), 'https://foo.bar') + self.assertEqual(sanitize_url('foo bar'), 'foo bar') def test_extract_basic_auth(self): auth_header = lambda url: sanitized_Request(url).get_header('Authorization') diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 40b9c4cf38..ee43435156 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -2165,7 +2165,7 @@ def sanitize_url(url): for mistake, fixup in COMMON_TYPOS: if re.match(mistake, url): return re.sub(mistake, fixup, url) - return escape_url(url) + return url def extract_basic_auth(url): @@ -2181,7 +2181,7 @@ def extract_basic_auth(url): def sanitized_Request(url, *args, **kwargs): - url, auth_header = extract_basic_auth(sanitize_url(url)) + url, auth_header = extract_basic_auth(escape_url(sanitize_url(url))) if auth_header is not None: headers = args[1] if len(args) >= 2 else kwargs.setdefault('headers', {}) headers['Authorization'] = auth_header