3 function formatHostname(hostname) {
4 // canonicalize the hostname, so that 'oogle.com' won't match 'google.com'
5 return hostname.replace(/^\.*/, '.').toLowerCase()
8 function parseNoProxyZone(zone) {
9 zone = zone.trim().toLowerCase()
11 var zoneParts = zone.split(':', 2)
12 , zoneHost = formatHostname(zoneParts[0])
13 , zonePort = zoneParts[1]
14 , hasPort = zone.indexOf(':') > -1
16 return {hostname: zoneHost, port: zonePort, hasPort: hasPort}
19 function uriInNoProxy(uri, noProxy) {
20 var port = uri.port || (uri.protocol === 'https:' ? '443' : '80')
21 , hostname = formatHostname(uri.hostname)
22 , noProxyList = noProxy.split(',')
24 // iterate through the noProxyList until it finds a match.
25 return noProxyList.map(parseNoProxyZone).some(function(noProxyZone) {
26 var isMatchedAt = hostname.indexOf(noProxyZone.hostname)
29 (isMatchedAt === hostname.length - noProxyZone.hostname.length)
32 if (noProxyZone.hasPort) {
33 return (port === noProxyZone.port) && hostnameMatched
36 return hostnameMatched
40 function getProxyFromURI(uri) {
41 // Decide the proper request proxy to use based on the request URI object and the
42 // environmental variables (NO_PROXY, HTTP_PROXY, etc.)
43 // respect NO_PROXY environment variables (see: http://lynx.isc.org/current/breakout/lynx_help/keystrokes/environments.html)
45 var noProxy = process.env.NO_PROXY || process.env.no_proxy || ''
47 // if the noProxy is a wildcard then return null
49 if (noProxy === '*') {
53 // if the noProxy is not empty and the uri is found return null
55 if (noProxy !== '' && uriInNoProxy(uri, noProxy)) {
59 // Check for HTTP or HTTPS Proxy in environment Else default to null
61 if (uri.protocol === 'http:') {
62 return process.env.HTTP_PROXY ||
63 process.env.http_proxy || null
66 if (uri.protocol === 'https:') {
67 return process.env.HTTPS_PROXY ||
68 process.env.https_proxy ||
69 process.env.HTTP_PROXY ||
70 process.env.http_proxy || null
73 // if none of that works, return null
74 // (What uri protocol are you using then?)
79 module.exports = getProxyFromURI