Skip to content

Commit

Permalink
Merge 8e86cc1 into b3a218d
Browse files Browse the repository at this point in the history
  • Loading branch information
paztis committed May 7, 2019
2 parents b3a218d + 8e86cc1 commit 60def84
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 36 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -833,6 +833,7 @@ The first argument can be either a `url` or an `options` object. The only requir

- `localAddress` - local interface to bind for network connections.
- `proxy` - an HTTP proxy to be used. Supports proxy Auth with Basic Auth, identical to support for the `url` parameter (by embedding the auth info in the `uri`)
- `noProxy` - no_proxy option
- `strictSSL` - if `true`, requires 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.
- `tunnel` - controls the behavior of
[HTTP `CONNECT` tunneling](https://en.wikipedia.org/wiki/HTTP_tunnel#HTTP_CONNECT_tunneling)
Expand Down
37 changes: 1 addition & 36 deletions lib/getProxyFromURI.js
@@ -1,41 +1,6 @@
'use strict'

function formatHostname (hostname) {
// canonicalize the hostname, so that 'oogle.com' won't match 'google.com'
return hostname.replace(/^\.*/, '.').toLowerCase()
}

function parseNoProxyZone (zone) {
zone = zone.trim().toLowerCase()

var zoneParts = zone.split(':', 2)
var zoneHost = formatHostname(zoneParts[0])
var zonePort = zoneParts[1]
var hasPort = zone.indexOf(':') > -1

return {hostname: zoneHost, port: zonePort, hasPort: hasPort}
}

function uriInNoProxy (uri, noProxy) {
var port = uri.port || (uri.protocol === 'https:' ? '443' : '80')
var hostname = formatHostname(uri.hostname)
var noProxyList = noProxy.split(',')

// iterate through the noProxyList until it finds a match.
return noProxyList.map(parseNoProxyZone).some(function (noProxyZone) {
var isMatchedAt = hostname.indexOf(noProxyZone.hostname)
var hostnameMatched = (
isMatchedAt > -1 &&
(isMatchedAt === hostname.length - noProxyZone.hostname.length)
)

if (noProxyZone.hasPort) {
return (port === noProxyZone.port) && hostnameMatched
}

return hostnameMatched
})
}
var uriInNoProxy = require('./uriInNoProxy')

function getProxyFromURI (uri) {
// Decide the proper request proxy to use based on the request URI object and the
Expand Down
40 changes: 40 additions & 0 deletions lib/uriInNoProxy.js
@@ -0,0 +1,40 @@
'use strict'

function formatHostname (hostname) {
// canonicalize the hostname, so that 'oogle.com' won't match 'google.com'
return hostname.replace(/^\.*/, '.').toLowerCase()
}

function parseNoProxyZone (zone) {
zone = zone.trim().toLowerCase()

var zoneParts = zone.split(':', 2)
var zoneHost = formatHostname(zoneParts[0])
var zonePort = zoneParts[1]
var hasPort = zone.indexOf(':') > -1

return {hostname: zoneHost, port: zonePort, hasPort: hasPort}
}

function uriInNoProxy (uri, noProxy) {
var port = uri.port || (uri.protocol === 'https:' ? '443' : '80')
var hostname = formatHostname(uri.hostname)
var noProxyList = noProxy.split(',')

// iterate through the noProxyList until it finds a match.
return noProxyList.map(parseNoProxyZone).some(function (noProxyZone) {
var isMatchedAt = hostname.indexOf(noProxyZone.hostname)
var hostnameMatched = (
isMatchedAt > -1 &&
(isMatchedAt === hostname.length - noProxyZone.hostname.length)
)

if (noProxyZone.hasPort) {
return (port === noProxyZone.port) && hostnameMatched
}

return hostnameMatched
})
}

module.exports = uriInNoProxy
4 changes: 4 additions & 0 deletions request.js
Expand Up @@ -19,6 +19,7 @@ var isTypedArray = require('is-typedarray').strict
var helpers = require('./lib/helpers')
var cookies = require('./lib/cookies')
var getProxyFromURI = require('./lib/getProxyFromURI')
var uriInNoProxy = require('./lib/uriInNoProxy')
var Querystring = require('./lib/querystring').Querystring
var Har = require('./lib/har').Har
var Auth = require('./lib/auth').Auth
Expand Down Expand Up @@ -275,6 +276,9 @@ Request.prototype.init = function (options) {

if (!self.hasOwnProperty('proxy')) {
self.proxy = getProxyFromURI(self.uri)
} else if ((self.noProxy === '*' || uriInNoProxy(self.uri, self.noProxy))) {
// noProxy option only operate over the proxy option, not over env vars
self.proxy = null
}

self.tunnel = self._tunnel.isEnabled()
Expand Down
10 changes: 10 additions & 0 deletions tests/test-proxy.js
Expand Up @@ -264,6 +264,16 @@ function addTests () {
}
}, true)

runTest('noProxy used with explicit proxy passed', {
proxy: s.url,
noProxy: '*'
}, false)

runTest('noProxy used with explicit proxy passed', {
proxy: s.url,
noProxy: 'google.com'
}, false)

// misc

// this fails if the check 'isMatchedAt > -1' in lib/getProxyFromURI.js is
Expand Down

0 comments on commit 60def84

Please sign in to comment.