Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

NoProxy configuration #469

Closed
wants to merge 3 commits into from

3 participants

@vvision

When using request with noproxy, check if the url requested must use a proxy (if present) or not. This allows requests on lan without having to change/disable the proxy parameter each time.

"noproxy" is a string containing hostnames. When using request with an url matching one of these hostnames, the specifed proxy won't be used.

So, this add a way to specify a list of hostnames that will not ever go through a proxy.

@vvision vvision referenced this pull request
Closed

Add noproxy configuration #407

@mikeal
Owner

wasn't there another PR that did a map of hosts to proxies? i want to make sure the two features aren't in conflict and can't be merged.

@vvision

I've searched for another PR with the same feature and haven't found any.
Maybe you have in mind the previous closed version of this PR (request/request#407)?

@danielflower

Hi, is there any plan to merge this PR? Bower uses Request and I'm hoping Bower can support non-proxied hosts so we can use it internally.

@mikeal
Owner

actually, you can support this yourself with the redirect events. Just listen for the "redirect" event and then set your request instances proxy value.

@danielflower

Thanks, I'll have a look at that.

@mikeal
Owner

no longer merges cleanly :(

@mikeal mikeal closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 8, 2013
  1. @vvision

    Added noproxy configuration.

    vvision authored
Commits on Mar 11, 2013
  1. @vvision

    Merge remote-tracking branch 'upstream/master'

    vvision authored
    Conflicts:
    	tests/run.js
  2. @vvision

    Changed listening port.

    vvision authored
This page is out of date. Refresh to see the latest.
Showing with 104 additions and 1 deletion.
  1. +1 −0  README.md
  2. +9 −1 index.js
  3. +94 −0 tests/test-noproxy.js
View
1  README.md
@@ -206,6 +206,7 @@ The first argument can be either a url or an options object. The only required o
* `pool.maxSockets` - Integer containing the maximum amount of sockets in the pool.
* `timeout` - Integer containing the number of milliseconds to wait for a request to respond before aborting the request
* `proxy` - An HTTP proxy to be used. Support proxy Auth with Basic Auth the same way it's supported with the `url` parameter by embedding the auth info in the uri.
+* `noproxy` - String of hostnames which won't use proxy when being requested.
* `oauth` - Options for OAuth HMAC-SHA1 signing, see documentation above.
* `hawk` - Options for [Hawk signing](https://github.com/hueniverse/hawk). The `credentials` key must contain the necessary signing info, [see hawk docs for details](https://github.com/hueniverse/hawk#usage-example).
* `strictSSL` - Set to `true` to require that SSL certificates be valid. Note: to use your own certificate authority, you need to specify an agent that was created with that ca as an option.
View
10 index.js
@@ -147,7 +147,15 @@ Request.prototype.init = function (options) {
} else {
if (typeof self.uri == "string") self.uri = url.parse(self.uri)
}
-
+
+ if(self.noproxy) {
+ if(typeof self.noproxy == 'string') {
+ if(self.noproxy.search(self.uri.hostname) !== -1) {
+ delete self.proxy
+ }
+ }
+ }
+
if (self.proxy) {
if (typeof self.proxy == 'string') self.proxy = url.parse(self.proxy)
View
94 tests/test-noproxy.js
@@ -0,0 +1,94 @@
+/*
+** Test noproxy configuration.
+**
+** We create a server and a proxy.
+** Server listens on localhost:4242.
+** Proxy listens on localhost:8080.
+** The proxy redirects all requests to /proxy on the server.
+** On the server, /proxy sends "proxy" .
+** When server is directly requested, it answers with "noproxy" .
+**
+**
+** So we perform 2 tests, both with proxy equal to "http://localhost:8080".
+** -A test is performed with noproxy equal to "null". In this case,
+** the server responds with "proxy" because the proxy is used.
+** -In the other test, noproxy equal "localhost, example.com".
+** Since localhost is part of noproxy, request is made directly
+** to the server and proxy is ignored.
+*/
+
+var assert = require("assert")
+ , http = require('http')
+ , request = require('../index.js')
+ //We create a server and a proxy.
+ , server = http.createServer(function(req, res){
+ res.statusCode = 200
+ if(req.url == '/proxy') {
+ res.end('proxy')
+ } else {
+ res.end('noproxy')
+ }
+ })
+ , proxy = http.createServer(function (req, res) {
+ res.statusCode = 200
+ var url = 'http://localhost:4242/proxy'
+ var x = request(url)
+ req.pipe(x)
+ x.pipe(res)
+ })
+ ;
+
+//Launch server and proxy
+var initialize = function (cb) {
+ server.listen(4242, 'localhost', function () {
+ proxy.listen(8080, 'localhost', cb)
+ })
+}
+
+//Tests
+initialize(function () {
+ //Checking the route for server and proxy
+ request.get("http://localhost:4242/test", function (err, res, body) {
+ assert.equal(res.statusCode, 200)
+ request.get("http://localhost:4242/proxy", function (err, res2, body) {
+ assert.equal(res2.statusCode, 200)
+ request.get("http://localhost:8080/test", function (err, res3, body) {
+ assert.equal(res3.statusCode, 200)
+ makeNoProxyTest(function () {
+ makeProxyTest(function () {
+ closeServer(server)
+ closeServer(proxy)
+ })
+ })
+ })
+ })
+ })
+})
+
+//Request with noproxy
+var makeNoProxyTest = function (cb) {
+ request ({
+ url: 'http://localhost:4242/test',
+ proxy: 'http://localhost:8080',
+ noproxy: 'localhost, example.com'
+ }, function (err, res, body) {
+ assert.equal(body, 'noproxy')
+ cb()
+ })
+}
+
+//Request with proxy
+var makeProxyTest = function (cb) {
+ request ({
+ url: 'http://localhost:4242/test',
+ proxy: 'http://localhost:8080',
+ noproxy: 'null'
+ }, function (err, res, body) {
+ assert.equal(body, 'proxy')
+ cb()
+ })
+}
+
+var closeServer = function (s) {
+ s.close()
+}
Something went wrong with that request. Please try again.