-
Notifications
You must be signed in to change notification settings - Fork 9.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[utils] Handle user:pass in URLs #28801
Conversation
4afd438
to
664d87b
Compare
LGTM |
Simplified the code and ticked the "improvement" box instead of the "bug fix" one |
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 ytdl-org#18276 (point 4) Fixes ytdl-org#20258 Fixes ytdl-org#26211 (see comment)
rebased the branch |
still LGTM |
Dear maintainers, please take some time to consider this issue, it finally adds a very important functionality to youtube-dl! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's finally add this, but with a structure matching yt-dlp.
youtube_dl/utils.py
Outdated
return compat_urllib_request.Request(sanitize_url(url), *args, **kwargs) | ||
url = sanitize_url(url) | ||
url, username, password = extract_user_pass(url) | ||
if username is not None: | ||
# password is not None | ||
auth_payload = username + ':' + password | ||
auth_payload = base64.b64encode(auth_payload.encode('utf-8')).decode('utf-8') | ||
auth_header = 'Basic ' + auth_payload | ||
headers = args[1] if len(args) >= 2 else kwargs.setdefault('headers', {}) | ||
headers['Authorization'] = auth_header |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align with yt-dlp:
return compat_urllib_request.Request(sanitize_url(url), *args, **kwargs) | |
url = sanitize_url(url) | |
url, username, password = extract_user_pass(url) | |
if username is not None: | |
# password is not None | |
auth_payload = username + ':' + password | |
auth_payload = base64.b64encode(auth_payload.encode('utf-8')).decode('utf-8') | |
auth_header = 'Basic ' + auth_payload | |
headers = args[1] if len(args) >= 2 else kwargs.setdefault('headers', {}) | |
headers['Authorization'] = auth_header | |
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) |
test/test_utils.py
Outdated
@@ -65,6 +65,8 @@ | |||
sanitize_filename, | |||
sanitize_path, | |||
sanitize_url, | |||
extract_user_pass, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align with yt-dlp:
extract_user_pass, |
* https://github.com/ytdl-org/youtube-dl: [utils] Handle user:pass in URLs (ytdl-org#28801)
Please follow the guide below
x
into all the boxes [ ] relevant to your pull request (like that [x])Before submitting a pull request make sure you have:
In order to be accepted and merged into youtube-dl each piece of code must be in public domain or released under Unlicense. Check one of the following options:
What is the purpose of your pull request?
Description of your pull request and other information
Fixes "nonnumeric port" errors when youtube-dl is given URLs with
usernames and passwords such as:
Refs:
Fixes #18276 (point 4)
Fixes #20258
Fixes #26211 (see comment)