Skip to content

Commit bcf74c0

Browse files
committed
Add support for http proxy environment variables
1 parent 48db0e1 commit bcf74c0

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

lib/requests/http.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ var httpRequest = function (method, uri, data, additionalHeaders, responseParams
3030
}
3131

3232
var parsedUrl = url.parse(uri);
33-
var isHttp = parsedUrl.protocol === 'http:';
33+
var protocol = parsedUrl.protocol.replace(':','');
34+
var interface = protocol === 'http' ? http : https;
3435

3536
var options = {
3637
hostname: parsedUrl.hostname,
@@ -40,7 +41,26 @@ var httpRequest = function (method, uri, data, additionalHeaders, responseParams
4041
headers: headers
4142
};
4243

43-
var interface = isHttp ? http : https;
44+
if (process.env[`${protocol}_proxy`]) {
45+
/*
46+
* Proxied requests don't work with Node's https module so use http to
47+
* talk to the proxy server regardless of the endpoint protocol. This
48+
* is unsuitable for environments where requests are expected to be
49+
* using end-to-end TLS.
50+
*/
51+
interface = http;
52+
const proxyUrl = url.parse(process.env.http_proxy);
53+
options = {
54+
hostname: proxyUrl.hostname,
55+
port: proxyUrl.port,
56+
path: parsedUrl.href,
57+
method: method,
58+
headers: {
59+
host: parsedUrl.host,
60+
...headers,
61+
}
62+
};
63+
}
4464

4565
var request = interface.request(options, function (res) {
4666
var rawData = '';
@@ -103,4 +123,4 @@ var httpRequest = function (method, uri, data, additionalHeaders, responseParams
103123
request.end();
104124
};
105125

106-
module.exports = httpRequest;
126+
module.exports = httpRequest;

0 commit comments

Comments
 (0)