Skip to content

Commit 6201ac7

Browse files
committed
Merge pull request #799 from F4-Group/fix_https
Fix default port detection with node 0.12.x
2 parents 1dabda2 + 5f14bca commit 6201ac7

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

lib/http-proxy/common.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,20 @@ common.getPort = function(req) {
134134

135135
return res ?
136136
res[1] :
137-
req.connection.pair ? '443' : '80';
137+
common.hasEncryptedConnection(req) ? '443' : '80';
138+
};
139+
140+
/**
141+
* Check if the request has an encrypted connection.
142+
*
143+
* @param {Request} req Incoming HTTP request.
144+
*
145+
* @return {Boolean} Whether the connection is encrypted or not.
146+
*
147+
* @api private
148+
*/
149+
common.hasEncryptedConnection = function(req) {
150+
return Boolean(req.connection.encrypted || req.connection.pair);
138151
};
139152

140153
/**

lib/http-proxy/passes/web-incoming.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ web_o = Object.keys(web_o).map(function(pass) {
6464
function XHeaders(req, res, options) {
6565
if(!options.xfwd) return;
6666

67-
var encrypted = req.isSpdy || req.connection.encrypted || req.connection.pair;
67+
var encrypted = req.isSpdy || common.hasEncryptedConnection(req);
6868
var values = {
6969
for : req.connection.remoteAddress || req.socket.remoteAddress,
7070
port : common.getPort(req),

lib/http-proxy/passes/ws-incoming.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var passes = exports;
5757
var values = {
5858
for : req.connection.remoteAddress || req.socket.remoteAddress,
5959
port : common.getPort(req),
60-
proto: req.connection.pair ? 'wss' : 'ws'
60+
proto: common.hasEncryptedConnection(req) ? 'wss' : 'ws'
6161
};
6262

6363
['for', 'port', 'proto'].forEach(function(header) {

test/lib-https-proxy-test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('lib/http-proxy.js', function() {
3535
ssl: {
3636
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
3737
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
38+
ciphers: 'AES128-GCM-SHA256',
3839
}
3940
}).listen(ports.proxy);
4041

@@ -65,6 +66,7 @@ describe('lib/http-proxy.js', function() {
6566
var source = https.createServer({
6667
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
6768
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
69+
ciphers: 'AES128-GCM-SHA256',
6870
}, function (req, res) {
6971
expect(req.method).to.eql('GET');
7072
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
@@ -105,6 +107,7 @@ describe('lib/http-proxy.js', function() {
105107
var source = https.createServer({
106108
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
107109
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
110+
ciphers: 'AES128-GCM-SHA256',
108111
}, function(req, res) {
109112
expect(req.method).to.eql('GET');
110113
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
@@ -119,6 +122,7 @@ describe('lib/http-proxy.js', function() {
119122
ssl: {
120123
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
121124
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
125+
ciphers: 'AES128-GCM-SHA256',
122126
},
123127
secure: false
124128
}).listen(ports.proxy);
@@ -150,6 +154,7 @@ describe('lib/http-proxy.js', function() {
150154
var source = https.createServer({
151155
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
152156
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
157+
ciphers: 'AES128-GCM-SHA256',
153158
}).listen(ports.source);
154159

155160
var proxy = httpProxy.createProxyServer({
@@ -161,7 +166,11 @@ describe('lib/http-proxy.js', function() {
161166

162167
proxy.on('error', function (err, req, res) {
163168
expect(err).to.be.an(Error);
164-
expect(err.toString()).to.be('Error: DEPTH_ZERO_SELF_SIGNED_CERT')
169+
if (process.versions.node.indexOf('0.12.') == 0) {
170+
expect(err.toString()).to.be('Error: self signed certificate')
171+
} else {
172+
expect(err.toString()).to.be('Error: DEPTH_ZERO_SELF_SIGNED_CERT')
173+
}
165174
done();
166175
})
167176

@@ -191,6 +200,7 @@ describe('lib/http-proxy.js', function() {
191200
var ownServer = https.createServer({
192201
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
193202
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
203+
ciphers: 'AES128-GCM-SHA256',
194204
}, function (req, res) {
195205
proxy.web(req, res, {
196206
target: 'http://127.0.0.1:' + ports.source

0 commit comments

Comments
 (0)