Skip to content
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

support quotes & credentials in mirror config #322

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions nodeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 or '@' not in url:
return url
# Valid URLs with creds are: `<proto>://<user>:<pass>@<uri>`
# 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

Expand Down Expand Up @@ -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'
Expand Down