New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getProxyFromURI Extraction Refactor #1346
Merged
nylen
merged 1 commit into
request:master
from
seanstrom:support/extract-getProxyFromURI
Jan 13, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'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) | ||
, zoneHost = formatHostname(zoneParts[0]) | ||
, zonePort = zoneParts[1] | ||
, hasPort = zone.indexOf(':') > -1 | ||
|
||
return {hostname: zoneHost, port: zonePort, hasPort: hasPort} | ||
} | ||
|
||
function uriInNoProxy(uri, noProxy) { | ||
var port = uri.port || (uri.protocol === 'https:' ? '443' : '80') | ||
, hostname = formatHostname(uri.hostname) | ||
, 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) | ||
, hostnameMatched = ( | ||
isMatchedAt > -1 && | ||
(isMatchedAt === hostname.length - noProxyZone.hostname.length) | ||
) | ||
|
||
if (noProxyZone.hasPort) { | ||
return (port === noProxyZone.port) && hostnameMatched | ||
} | ||
|
||
return hostnameMatched | ||
}) | ||
} | ||
|
||
function getProxyFromURI(uri) { | ||
// Decide the proper request proxy to use based on the request URI object and the | ||
// environmental variables (NO_PROXY, HTTP_PROXY, etc.) | ||
// respect NO_PROXY environment variables (see: http://lynx.isc.org/current/breakout/lynx_help/keystrokes/environments.html) | ||
|
||
var noProxy = process.env.NO_PROXY || process.env.no_proxy || '' | ||
|
||
// if the noProxy is a wildcard then return null | ||
|
||
if (noProxy === '*') { | ||
return null | ||
} | ||
|
||
// if the noProxy is not empty and the uri is found return null | ||
|
||
if (noProxy !== '' && uriInNoProxy(uri, noProxy)) { | ||
return null | ||
} | ||
|
||
// Check for HTTP or HTTPS Proxy in environment Else default to null | ||
|
||
if (uri.protocol === 'http:') { | ||
return process.env.HTTP_PROXY || | ||
process.env.http_proxy || null | ||
} | ||
|
||
if (uri.protocol === 'https:') { | ||
return process.env.HTTPS_PROXY || | ||
process.env.https_proxy || | ||
process.env.HTTP_PROXY || | ||
process.env.http_proxy || null | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ||
null or || null ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops this was a typo aha, but it poses an interesting question. |
||
|
||
// if none of that works, return null | ||
// (What uri protocol are you using then?) | ||
|
||
return null | ||
} | ||
|
||
module.exports = getProxyFromURI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about calling this
parseNoProxyZone
, then you can do ...