aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl
diff options
context:
space:
mode:
authorHubert Hirtz <hubert@hirtz.pm>2024-03-04 01:27:55 +0000
committerGitHub <noreply@github.com>2024-03-04 01:27:55 +0000
commitf0812d784836d18fd25ea32f9b5a0c9c6e92425b (patch)
tree173402901f9b0ea2c025d67287451f916cec447c /youtube_dl
parent40bd5c18153afe765caa6726302ee1dd8a9a2ce6 (diff)
downloadyoutube-dl-f0812d784836d18fd25ea32f9b5a0c9c6e92425b.tar.xz
[utils] Handle user:pass in URLs (#28801)
* Handle user:pass in URLs Fixes "nonnumeric port" errors when youtube-dl is given URLs with usernames and passwords such as: http://username:password@example.com/myvideo.mp4 Refs: - https://en.wikipedia.org/wiki/Basic_access_authentication - https://tools.ietf.org/html/rfc1738#section-3.1 - https://docs.python.org/3.8/library/urllib.parse.html#urllib.parse.urlsplit Fixes #18276 (point 4) Fixes #20258 Fixes #26211 (see comment) * Align code with yt-dlp --------- Co-authored-by: dirkf <fieldhouse@gmx.net>
Diffstat (limited to 'youtube_dl')
-rw-r--r--youtube_dl/utils.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index 61b94d84c..c249e7168 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -2182,8 +2182,28 @@ def sanitize_url(url):
return url
+def extract_basic_auth(url):
+ parts = compat_urllib_parse.urlsplit(url)
+ if parts.username is None:
+ return url, None
+ url = compat_urllib_parse.urlunsplit(parts._replace(netloc=(
+ parts.hostname if parts.port is None
+ else '%s:%d' % (parts.hostname, parts.port))))
+ auth_payload = base64.b64encode(
+ ('%s:%s' % (parts.username, parts.password or '')).encode('utf-8'))
+ return url, 'Basic {0}'.format(auth_payload.decode('ascii'))
+
+
def sanitized_Request(url, *args, **kwargs):
- return compat_urllib_request.Request(escape_url(sanitize_url(url)), *args, **kwargs)
+ url, auth_header = extract_basic_auth(escape_url(sanitize_url(url)))
+ if auth_header is not None:
+ headers = args[1] if len(args) > 1 else kwargs.get('headers')
+ headers = headers or {}
+ headers['Authorization'] = auth_header
+ if len(args) <= 1 and kwargs.get('headers') is None:
+ kwargs['headers'] = headers
+ kwargs = compat_kwargs(kwargs)
+ return compat_urllib_request.Request(url, *args, **kwargs)
def expand_path(s):