Browse files

Add http#req no proxy hosts handling, localhost and 127.0.0 will igno…

…re proxy by default.
  • Loading branch information...
1 parent bd33c8a commit 819d54bfcd8b47b47b6ec7c1135dd4c8c98b909f @cliffano committed Apr 29, 2013
Showing with 39 additions and 6 deletions.
  1. +1 −1 CHANGELOG.md
  2. +14 −5 lib/http.js
  3. +24 −0 test/http.js
View
2 CHANGELOG.md
@@ -1,5 +1,5 @@
### 0.1.6-pre
-*
+* Add http#req no proxy hosts handling, localhost and 127.0.0 will ignore proxy by default
### 0.1.5
* Add http#req timeout handling, default timeout changed to 2000 milliseconds
View
19 lib/http.js
@@ -1,5 +1,6 @@
/*jshint esnext: true */
-var request = require('request');
+var request = require('request'),
+ _url = require('url');
/**
* Sends a HTTP request to a specified URL with optional proxy, query strings, and handlers.
@@ -11,6 +12,7 @@ var request = require('request');
* @param {String} url: URL without query string
* @param {Object} opts: optional
* - proxy: proxy server URL with format http://user:pass@host:port
+ * - noProxyHosts: an array of host names which will ignore any proxy settings, defaults to: localhost, 127.0.0.1
* - queryStrings: object containing URL query strings with format { name: value }
* - handlers: response handlers with format { statuscode: function(result, cb) }
* - headers: object containing http headers, exact map to mikeal/request headers opt
@@ -27,11 +29,18 @@ function req(method, url, opts, cb) {
var params = { url: url, followAllRedirects: true, timeout: opts.timeout || TIMEOUT },
envProxy = proxy(url);
+ function _proxyIgnored() {
+ const NO_PROXY_HOSTS = ['localhost', '127.0.0.1'];
+ return (opts.noProxyHosts || NO_PROXY_HOSTS).indexOf(_url.parse(url).hostname) !== -1;
+ }
+
// set proxy setting based on environment variable
- if (opts.proxy) {
- params.proxy = opts.proxy;
- } else if (envProxy) {
- params.proxy = envProxy;
+ if (!_proxyIgnored()) {
+ if (opts.proxy) {
+ params.proxy = opts.proxy;
+ } else if (envProxy) {
+ params.proxy = envProxy;
+ }
}
// just a more readable opt name for query strings
View
24 test/http.js
@@ -119,6 +119,30 @@ buster.testCase('http - request', {
done();
});
},
+ 'should not set proxy when URL hostname is on the default no proxy hosts array': function (done) {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy', https_proxy: 'https://someproxy' });
+ this.stub(request, 'get', function (params, cb) {
+ assert.equals(params.url, 'http://localhost');
+ assert.equals(params.proxy, undefined);
+ cb(null, { statusCode: 200, body: 'somebody' });
+ });
+ http.request('GET', 'http://localhost', {}, function (err, result) {
+ assert.equals(result, undefined);
+ done();
+ });
+ },
+ 'should not set proxy when URL hostname is on no proxy hosts opt': function (done) {
+ this.stub(process, 'env', { http_proxy: 'http://someproxy', https_proxy: 'https://someproxy' });
+ this.stub(request, 'get', function (params, cb) {
+ assert.equals(params.url, 'http://someurl');
+ assert.equals(params.proxy, undefined);
+ cb(null, { statusCode: 200, body: 'somebody' });
+ });
+ http.request('GET', 'http://someurl', { noProxyHosts: ['someurl'] }, function (err, result) {
+ assert.equals(result, undefined);
+ done();
+ });
+ },
'should follow non-GET redirection': function (done) {
this.stub(request, 'post', function (params, cb) {
assert.isTrue(params.followAllRedirects);

0 comments on commit 819d54b

Please sign in to comment.