diff --git a/request.js b/request.js index c9d9a5edb..b9d48d9c4 100644 --- a/request.js +++ b/request.js @@ -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 @@ -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() }