Skip to content
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

Fix tunneling after redirection from https (Original: #1881) #1894

Merged
merged 2 commits into from Nov 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -799,8 +799,7 @@ default in Linux can be anywhere from 20-120 seconds][linux-timeout]).
- `tunnel` - controls the behavior of
[HTTP `CONNECT` tunneling](https://en.wikipedia.org/wiki/HTTP_tunnel#HTTP_CONNECT_tunneling)
as follows:
- `undefined` (default) - `true` if the destination is `https` or a previous
request in the redirect chain used a tunneling proxy, `false` otherwise
- `undefined` (default) - `true` if the destination is `https`, `false` otherwise
- `true` - always tunnel to the destination by making a `CONNECT` request to
the proxy
- `false` - request the destination as a `GET` request.
Expand Down
29 changes: 12 additions & 17 deletions lib/tunnel.js
Expand Up @@ -109,33 +109,28 @@ function Tunnel (request) {
this.request = request
this.proxyHeaderWhiteList = defaultProxyHeaderWhiteList
this.proxyHeaderExclusiveList = []
}

Tunnel.prototype.isEnabled = function (options) {
var request = this.request
// 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 request.tunnel !== 'undefined') {
return request.tunnel
this.tunnelOverride = request.tunnel
}
}

Tunnel.prototype.isEnabled = function () {
var self = this
, request = self.request
// Tunnel HTTPS by default. Allow the user to override this setting.

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

// If the destination is HTTPS, tunnel.
if (request.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
// Otherwise, do not use tunnel.
return false
}

Tunnel.prototype.setup = function (options) {
Expand Down
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -288,7 +288,7 @@ Request.prototype.init = function (options) {
self.proxy = getProxyFromURI(self.uri)
}

self.tunnel = self._tunnel.isEnabled(options)
self.tunnel = self._tunnel.isEnabled()
if (self.proxy) {
self._tunnel.setup(options)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test-tunnel.js
Expand Up @@ -376,7 +376,7 @@ runTest('https->http over http, tunnel=default', {
}, [
'http connect to localhost:' + ss.port,
'https redirect to http',
'http connect to localhost:' + s.port,
'http proxy to http',
'http response',
'200 http ok'
])
Expand Down