twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

getProxyFromURI.js (2257B)


      1 'use strict'
      2 
      3 function formatHostname (hostname) {
      4   // canonicalize the hostname, so that 'oogle.com' won't match 'google.com'
      5   return hostname.replace(/^\.*/, '.').toLowerCase()
      6 }
      7 
      8 function parseNoProxyZone (zone) {
      9   zone = zone.trim().toLowerCase()
     10 
     11   var zoneParts = zone.split(':', 2)
     12   var zoneHost = formatHostname(zoneParts[0])
     13   var zonePort = zoneParts[1]
     14   var hasPort = zone.indexOf(':') > -1
     15 
     16   return {hostname: zoneHost, port: zonePort, hasPort: hasPort}
     17 }
     18 
     19 function uriInNoProxy (uri, noProxy) {
     20   var port = uri.port || (uri.protocol === 'https:' ? '443' : '80')
     21   var hostname = formatHostname(uri.hostname)
     22   var noProxyList = noProxy.split(',')
     23 
     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)
     27     var hostnameMatched = (
     28       isMatchedAt > -1 &&
     29         (isMatchedAt === hostname.length - noProxyZone.hostname.length)
     30     )
     31 
     32     if (noProxyZone.hasPort) {
     33       return (port === noProxyZone.port) && hostnameMatched
     34     }
     35 
     36     return hostnameMatched
     37   })
     38 }
     39 
     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: https://lynx.invisible-island.net/lynx2.8.7/breakout/lynx_help/keystrokes/environments.html)
     44 
     45   var noProxy = process.env.NO_PROXY || process.env.no_proxy || ''
     46 
     47   // if the noProxy is a wildcard then return null
     48 
     49   if (noProxy === '*') {
     50     return null
     51   }
     52 
     53   // if the noProxy is not empty and the uri is found return null
     54 
     55   if (noProxy !== '' && uriInNoProxy(uri, noProxy)) {
     56     return null
     57   }
     58 
     59   // Check for HTTP or HTTPS Proxy in environment Else default to null
     60 
     61   if (uri.protocol === 'http:') {
     62     return process.env.HTTP_PROXY ||
     63       process.env.http_proxy || null
     64   }
     65 
     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
     71   }
     72 
     73   // if none of that works, return null
     74   // (What uri protocol are you using then?)
     75 
     76   return null
     77 }
     78 
     79 module.exports = getProxyFromURI