Fix constructor on NO_PROXY with wildcard domain (#216)
* fix contrstructor on NO_PROXY with wildcard domain * escape * in NO_PROXY regex only if needed keep the original behavior for back-compat purposes * patch for no proxy with wildcards
This commit is contained in:
Родитель
305ef3e65b
Коммит
f21ab8a105
|
@ -128,7 +128,7 @@ export class HttpClient implements ifm.IHttpClient {
|
|||
if (no_proxy) {
|
||||
this._httpProxyBypassHosts = [];
|
||||
no_proxy.split(',').forEach(bypass => {
|
||||
this._httpProxyBypassHosts.push(new RegExp(bypass, 'i'));
|
||||
this._httpProxyBypassHosts.push(util.buildProxyBypassRegexFromEnv(bypass));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
22
lib/Util.ts
22
lib/Util.ts
|
@ -98,6 +98,28 @@ export async function decompressGzippedContent(buffer: Buffer, charset?: string)
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a RegExp to test urls against for deciding
|
||||
* wether to bypass proxy from an entry of the
|
||||
* environment variable setting NO_PROXY
|
||||
*
|
||||
* @param {string} bypass
|
||||
* @return {RegExp}
|
||||
*/
|
||||
export function buildProxyBypassRegexFromEnv(bypass : string) : RegExp {
|
||||
try {
|
||||
// We need to keep this around for back-compat purposes
|
||||
return new RegExp(bypass, 'i')
|
||||
}
|
||||
catch(err) {
|
||||
if (err instanceof SyntaxError && (bypass || "").startsWith("*")) {
|
||||
let wildcardEscaped = bypass.replace('*', '(.*)');
|
||||
return new RegExp(wildcardEscaped, 'i');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain Response's Content Charset.
|
||||
* Through inspecting `content-type` response header.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "typed-rest-client",
|
||||
"version": "1.8.2",
|
||||
"version": "1.8.3",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "typed-rest-client",
|
||||
"version": "1.8.2",
|
||||
"version": "1.8.3",
|
||||
"description": "Node Rest and Http Clients for use with TypeScript",
|
||||
"main": "./RestClient.js",
|
||||
"scripts": {
|
||||
|
|
|
@ -462,3 +462,43 @@ describe('Http Tests with keepAlive', function () {
|
|||
assert(res.message.statusCode == 501, "status code should be 501");
|
||||
});
|
||||
});
|
||||
|
||||
describe('Http Tests with NO_PROXY environment variable', function () {
|
||||
let noProxyBeforeTest = undefined;
|
||||
before(() => {
|
||||
|
||||
});
|
||||
|
||||
after(() => {
|
||||
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
noProxyBeforeTest = process.env["NO_PROXY"];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (undefined !== noProxyBeforeTest) {
|
||||
process.env["NO_PROXY"] = noProxyBeforeTest;
|
||||
} else {
|
||||
delete process.env["NO_PROXY"];
|
||||
}
|
||||
})
|
||||
|
||||
it('constructs with NO_PROXY', () => {
|
||||
this.timeout(1000);
|
||||
process.env["NO_PROXY"] = "microsoft.com";
|
||||
let http: httpm.HttpClient = new httpm.HttpClient('typed-test-client-tests');
|
||||
assert(http, 'http client should not be null');
|
||||
assert(http.userAgent, 'user-agent should not be null')
|
||||
});
|
||||
|
||||
it('constructs with wildcard in NO_PROXY', () => {
|
||||
this.timeout(1000);
|
||||
process.env["NO_PROXY"] = "*.microsoft.com";
|
||||
let http: httpm.HttpClient = new httpm.HttpClient('typed-test-client-tests');
|
||||
assert(http, 'http client should not be null');
|
||||
assert(http.userAgent, 'user-agent should not be null')
|
||||
});
|
||||
|
||||
});
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
import assert = require('assert');
|
||||
import url = require("url");
|
||||
import * as util from '../../lib/Util';
|
||||
|
||||
describe('Util Tests', function () {
|
||||
|
||||
before(() => {
|
||||
|
||||
});
|
||||
|
||||
after(() => {
|
||||
});
|
||||
|
||||
describe('buildProxyBypassRegexFromEnv', () => {
|
||||
it('constructs RegExp for domain', () => {
|
||||
let regExp = util.buildProxyBypassRegexFromEnv('microsoft.com');
|
||||
assert(regExp, 'regExp should not be null');
|
||||
});
|
||||
|
||||
it('constructs RegExp for wildcard domain', () => {
|
||||
let regExp = util.buildProxyBypassRegexFromEnv('*.microsoft.com');
|
||||
assert(regExp, 'regExp should not be null');
|
||||
});
|
||||
|
||||
it('bypasses same domain', () => {
|
||||
let regExp = util.buildProxyBypassRegexFromEnv('microsoft.com');
|
||||
assert(regExp, 'regExp should not be null');
|
||||
let parsedUrl = url.parse("https://microsoft.com/api/resource");
|
||||
let bypassed = regExp.test(parsedUrl.href);
|
||||
assert.equal(bypassed, true);
|
||||
});
|
||||
|
||||
it('bypasses subdomain using wildcard', () => {
|
||||
let regExp = util.buildProxyBypassRegexFromEnv('*.microsoft.com');
|
||||
assert(regExp, 'regExp should not be null');
|
||||
let parsedUrl = url.parse("https://subdomain.microsoft.com/api/resource");
|
||||
let bypassed = regExp.test(parsedUrl.href);
|
||||
assert.equal(bypassed, true);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
Загрузка…
Ссылка в новой задаче