diff options
author | Hubert Hirtz <hubert@hirtz.pm> | 2021-04-19 14:07:45 +0200 |
---|---|---|
committer | pukkandan <pukkandan.ytdlp@gmail.com> | 2021-05-24 03:38:02 +0530 |
commit | 5435dcf96ec444c92a402d0eb169d94015c0e6ba (patch) | |
tree | b1e840df5ef5aca1ebc05e7a469f7929cc3c450b /yt_dlp/utils.py | |
parent | f17c70227055a4415f604d644704eec9c3f4fe21 (diff) | |
download | hypervideo-pre-5435dcf96ec444c92a402d0eb169d94015c0e6ba.tar.lz hypervideo-pre-5435dcf96ec444c92a402d0eb169d94015c0e6ba.tar.xz hypervideo-pre-5435dcf96ec444c92a402d0eb169d94015c0e6ba.zip |
Handle Basic Auth `user:pass` in URLs
Fixes https://github.com/ytdl-org/youtube-dl/issues/20258, https://github.com/ytdl-org/youtube-dl/issues/26211
Authored by: hhirtz, pukkandan
Diffstat (limited to 'yt_dlp/utils.py')
-rw-r--r-- | yt_dlp/utils.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 9c9e27694..ec8f007d5 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -2168,8 +2168,24 @@ def sanitize_url(url): return escape_url(url) +def extract_basic_auth(url): + parts = compat_urlparse.urlsplit(url) + if parts.username is None: + return url, None + url = compat_urlparse.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 ' + auth_payload.decode('utf-8') + + def sanitized_Request(url, *args, **kwargs): - return compat_urllib_request.Request(sanitize_url(url), *args, **kwargs) + url, auth_header = extract_basic_auth(sanitize_url(url)) + if auth_header is not None: + headers = args[1] if len(args) >= 2 else kwargs.setdefault('headers', {}) + headers['Authorization'] = auth_header + return compat_urllib_request.Request(url, *args, **kwargs) def expand_path(s): |