From 325650c6b2da69f770e073db6c95f56221360420 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 9 Jan 2023 20:37:53 +0000 Subject: [PATCH 1/2] support quotes & credentials in mirror config this change performs two parts. The first is removing any extra quotes from the configuration file so the examples in the README work as expected. The second is supporting HTTP Basic Authentication credentials within the mirror argument itself so for folks fetching from a source that requires authentication they are able to do so. --- nodeenv.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/nodeenv.py b/nodeenv.py index 3315d85..50e0b69 100644 --- a/nodeenv.py +++ b/nodeenv.py @@ -614,6 +614,7 @@ def download_node_src(node_url, src_dir, args): def urlopen(url): home_url = "https://github.com/ekalinin/nodeenv/" headers = {'User-Agent': 'nodeenv/%s (%s)' % (nodeenv_version, home_url)} + url = parse_credentials(url) req = urllib2.Request(url, None, headers) if ignore_ssl_certs: # py27: protocol required, py3: optional @@ -623,6 +624,31 @@ def urlopen(url): return urllib2.urlopen(req, context=context) return urllib2.urlopen(req) + +def parse_credentials(url): + if '://' not in url and '@' not in url: + return url + # Valid URLs with creds are: `://:@` + # If it's not in that form, just pass it on too urllib. + if url.index('@') < url.index('://'): + return url + url_protocol_split = url.split('://') + protocol = url_protocol_split[0] + # This will only apply to HTTP Basic AUTH + if protocol not in ['http', 'https']: + return url + credentials_split = url_protocol_split[1].split('@') + credentials = credentials_split[0].split(':') + new_url = '{}://{}'.format(protocol, credentials_split[1]) + username = credentials[0] + password = credentials[1] if len(credentials) > 1 else '' + password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() + password_manager.add_password(None, new_url, username, password) + urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler( + password_manager + ))) + return new_url + # --------------------------------------------------------- # Virtual environment functions @@ -1078,9 +1104,9 @@ def main(): src_domain = None if args.mirror: if '://' in args.mirror: - src_base_url = args.mirror + src_base_url = args.mirror.strip('\'"') else: - src_domain = args.mirror + src_domain = args.mirror.strip('\'"') # use unofficial builds only if musl and no explicitly chosen mirror elif is_x86_64_musl(): src_domain = 'unofficial-builds.nodejs.org' From 5aed7cc7bbc14f88c0c7e3603a366759e94e52e6 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 17 Jan 2023 17:39:45 +0000 Subject: [PATCH 2/2] and vs or --- nodeenv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodeenv.py b/nodeenv.py index 50e0b69..d87e111 100644 --- a/nodeenv.py +++ b/nodeenv.py @@ -626,7 +626,7 @@ def urlopen(url): def parse_credentials(url): - if '://' not in url and '@' not in url: + if '://' not in url or '@' not in url: return url # Valid URLs with creds are: `://:@` # If it's not in that form, just pass it on too urllib.