Skip to content

Commit

Permalink
Factor out getTunnelOption()
Browse files Browse the repository at this point in the history
  • Loading branch information
nylen committed Jan 27, 2015
1 parent b7c1e1e commit 331e208
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions request.js
Expand Up @@ -133,6 +133,32 @@ function constructProxyHeaderWhiteList(headers, proxyHeaderWhiteList) {
}, {})
}

function getTunnelOption(self, options) {
// Tunnel HTTPS by default, or if a previous request in the redirect chain
// was tunneled. Allow the user to override this setting.

// If self.tunnel is already set (because this is a redirect), use the
// existing value.
if (typeof self.tunnel !== 'undefined') {
return self.tunnel
}

// If options.tunnel is set (the user specified a value), use it.
if (typeof options.tunnel !== 'undefined') {
return options.tunnel
}

// If the destination is HTTPS, tunnel.
if (self.uri.protocol === 'https:') {
return true
}

// Otherwise, leave tunnel unset, because if a later request in the redirect
// chain is HTTPS then that request (and any subsequent ones) should be
// tunneled.
return undefined
}

function constructTunnelOptions(request) {
var proxy = request.proxy

Expand Down Expand Up @@ -377,24 +403,7 @@ Request.prototype.init = function (options) {
self.proxy = getProxyFromURI(self.uri)
}

// Goals:
// - Tunnel HTTPS by default, or if a previous request in the redirect chain
// was tunneled
// - Allow the user to override this setting
// Implementation:
// - If this is a redirect, self.tunnel may be set -> don't do anything.
// - If options.tunnel is set, use it.
// - Otherwise, tunnel if the destination is HTTPS, but don't set
// tunnel=false if it's not, because if a later rquest in the redirect
// chain is HTTPS then that request (and any subsequent ones) should be
// tunneled.
if (typeof self.tunnel === 'undefined') {
if (typeof options.tunnel !== 'undefined') {
self.tunnel = options.tunnel
} else if (self.uri.protocol === 'https:') {
self.tunnel = true
}
}
self.tunnel = getTunnelOption(self, options)
if (self.proxy) {
self.setupTunnel()
}
Expand Down

0 comments on commit 331e208

Please sign in to comment.