From d5dc6f64e1368f70e585fb4b7beee64c51d21e27 Mon Sep 17 00:00:00 2001 From: Winston Liu <50Wliu@users.noreply.github.com> Date: Tue, 14 May 2019 12:19:09 -0400 Subject: [PATCH] lib: support HTTPS_PROXY Fixes: https://github.com/nodejs/node-gyp/issues/1749 --- README.md | 2 +- lib/install.js | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 01d428bc6c..e78fe46685 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ Some additional resources for addons and writing `gyp` files: | `--devdir=$path` | SDK download directory (default is OS cache directory) | `--ensure` | Don't reinstall headers if already present | `--dist-url=$url` | Download header tarball from custom URL -| `--proxy=$url` | Set HTTP proxy for downloading header tarball +| `--proxy=$url` | Set HTTP(S) proxy for downloading header tarball | `--cafile=$cafile` | Override default CA chain (to download tarball) | `--nodedir=$path` | Set the path to the node source code | `--python=$path` | Set path to the Python 2 binary diff --git a/lib/install.js b/lib/install.js index 735c31c0d0..e17b25b3a4 100644 --- a/lib/install.js +++ b/lib/install.js @@ -14,6 +14,7 @@ var fs = require('graceful-fs') , os = require('os') , tar = require('tar') , path = require('path') + , url = require('url') , crypto = require('crypto') , log = require('npmlog') , semver = require('semver') @@ -413,11 +414,11 @@ function install (fs, gyp, argv, callback) { } -function download (gyp, env, url) { - log.http('GET', url) +function download (gyp, env, uri) { + log.http('GET', uri) var requestOpts = { - uri: url + uri , headers: { 'User-Agent': 'node-gyp v' + gyp.version + ' (node ' + process.version + ')' } @@ -429,10 +430,22 @@ function download (gyp, env, url) { } // basic support for a proxy server - var proxyUrl = gyp.opts.proxy - || env.http_proxy - || env.HTTP_PROXY - || env.npm_config_proxy + var proxyUrl = gyp.opts.proxy; + + // prefer HTTPS proxy if contacting an HTTPS uri + if (!proxyUrl && url.parse(uri).protocol === 'https:') { + proxyUrl = env.https_proxy + || env.HTTPS_PROXY + || env.npm_config_https_proxy + } + + // but fallback to an HTTP proxy as needed + if (!proxyUrl) { + proxyUrl = env.http_proxy + || env.HTTP_PROXY + || env.npm_config_proxy + } + if (proxyUrl) { if (/^https?:\/\//i.test(proxyUrl)) { log.verbose('download', 'using proxy url: "%s"', proxyUrl)