2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00

Resolving proxy from env on redirect (#4436)

* Fixing http adapter to recompute proxy on redirect

Redirections can target different hosts or change the protocol
from http to https or vice versa. When the proxy option is
inferred from the environment, it should be recomputed when
the protocol or host changes because the proxy host can differ
or even whether to proxy or not can differ.

* Fixing proxy protocol handling

1) setProxy now changes request options protocol when using a proxy with explicit protocol.
2) As a result, selection of the correct transport can be simplified.
3) Legacy agent selection needs to be moved done accordingly. (Is 'agent' option even still used?)

* Using proxy-from-env library to handle proxy env vars

The proxy-from-env library is a popular, lightweight library that is
very easy to use and covers a few more cases, not to mention it has
extensive test coverage.

* Fixing proxy auth handling

* Adding test proving env vars are re-resolved on redirect

* Revert unnecessary change

* Fixing proxy beforeRedirect regression

* Fixing lint errors

* Revert "Fixing lint errors"

This reverts commit 2de3cabc60db2444e63a699bae9ec45531218a84.

* Revert "Fixing proxy beforeRedirect regression"

This reverts commit 57befc3215980e47333fedc1e9028cc22297540b.
This commit is contained in:
Maxime Bargiel
2022-05-11 13:24:14 -04:00
committed by GitHub
parent de5e006142
commit 495d5fb133
3 changed files with 96 additions and 77 deletions
+51 -1
View File
@@ -687,7 +687,7 @@ describe('supports http with nodejs', function () {
proxy: {
host: 'localhost',
port: 4000,
protocol: 'https'
protocol: 'https:'
},
httpsAgent: new https.Agent({
rejectUnauthorized: false
@@ -798,6 +798,56 @@ describe('supports http with nodejs', function () {
});
});
it('should re-evaluate proxy on redirect when proxy set via env var', function (done) {
process.env.http_proxy = 'http://localhost:4000'
process.env.no_proxy = 'localhost:4000'
var proxyUseCount = 0;
server = http.createServer(function (req, res) {
res.setHeader('Location', 'http://localhost:4000/redirected');
res.statusCode = 302;
res.end();
}).listen(4444, function () {
proxy = http.createServer(function (request, response) {
var parsed = url.parse(request.url);
if (parsed.pathname === '/redirected') {
response.statusCode = 200;
response.end();
return;
}
proxyUseCount += 1;
var opts = {
host: parsed.hostname,
port: parsed.port,
path: parsed.path,
protocol: parsed.protocol,
rejectUnauthorized: false
};
http.get(opts, function (res) {
var body = '';
res.on('data', function (data) {
body += data;
});
res.on('end', function () {
response.setHeader('Content-Type', 'text/html; charset=UTF-8');
response.setHeader('Location', res.headers.location);
response.end(body);
});
});
}).listen(4000, function () {
axios.get('http://localhost:4444/').then(function(res) {
assert.equal(res.status, 200);
assert.equal(proxyUseCount, 1);
done();
}).catch(done);
});
});
});
it('should not use proxy for domains in no_proxy', function (done) {
server = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');