Skip to content

Commit

Permalink
Handle user:pass in URLs
Browse files Browse the repository at this point in the history
Fixes ytdl-org#18276 (point 4)
Fixes ytdl-org#20258
Fixes ytdl-org#26211 (see comment)
  • Loading branch information
hhirtz committed Apr 19, 2021
1 parent 9f6c03a commit e4e795c
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion youtube_dl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,31 @@ def sanitize_url(url):


def sanitized_Request(url, *args, **kwargs):
return compat_urllib_request.Request(sanitize_url(url), *args, **kwargs)
url = sanitize_url(url)
parts = compat_urllib_parse.urlsplit(url)
username = parts.username
password = parts.password
if username is not None:
# Remove "user:pass" from "url" since they are not recognized by
# "compat_urllib_request.Request".
netloc = parts.hostname
if parts.port is not None:
netloc = parts.hostname + ':' + parts.port
parts = parts._replace(netloc=netloc)
url = compat_urllib_parse.urlunsplit(parts)
# Add the Authorization header.
if password is None:
password = ''
auth_payload = username + ':' + password
auth_payload = base64.b64encode(bytes(auth_payload, 'utf-8')).decode('utf-8')
auth_header = 'Basic ' + auth_payload
if len(args) >= 2:
args[1]['Authorization'] = auth_header
else:
if 'headers' not in kwargs:
kwargs['headers'] = {}
kwargs['headers']['Authorization'] = 'Basic ' + auth_payload
return compat_urllib_request.Request(url, *args, **kwargs)


def expand_path(s):
Expand Down

0 comments on commit e4e795c

Please sign in to comment.