Skip to content

Commit 9c6c4b9

Browse files
committed
[fix test api] Only change Origin headers in WebSocket requests when the changeOrigin option is set explicitly. Added tests to ensure Origin and sec-websocket-origin headers match when proxying websockets.
1 parent 44a8566 commit 9c6c4b9

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

lib/node-http-proxy.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,11 @@ exports.createServer = function () {
219219
var HttpProxy = exports.HttpProxy = function (options) {
220220
events.EventEmitter.call(this);
221221

222-
var self = this;
223-
options = options || {};
224-
this.forward = options.forward;
225-
this.https = options.https;
222+
var self = this;
223+
options = options || {};
224+
this.forward = options.forward;
225+
this.https = options.https;
226+
this.changeOrigin = options.changeOrigin || false;
226227

227228
if (options.router) {
228229
this.proxyTable = new ProxyTable(options.router, options.silent, options.hostnameOnly);
@@ -523,7 +524,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
523524
socket.setTimeout(0);
524525
socket.setNoDelay(true);
525526
if (keepAlive) {
526-
socket.setKeepAlive(true, 0);
527+
if (socket.setKeepAlive) {
528+
socket.setKeepAlive(true, 0);
529+
}
530+
else if (socket.pair.cleartext.socket.setKeepAlive) {
531+
socket.pair.cleartext.socket.setKeepAlive(true, 0);
532+
}
527533
}
528534
else {
529535
socket.setEncoding('utf8');
@@ -589,12 +595,14 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
589595

590596
// Remote host address
591597
var protocolName = options.https || this.https ? 'https' : 'http',
592-
agent = _getAgent(options.host, options.port),
598+
agent = _getAgent(options.host, options.port, options.https || this.https),
593599
remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port);
594600

595601
// Change headers
596-
req.headers.host = remoteHost;
597-
req.headers.origin = protocolName + '://' + options.host;
602+
if (this.changeOrigin) {
603+
req.headers.host = remoteHost;
604+
req.headers.origin = protocolName + '://' + remoteHost;
605+
}
598606

599607
outgoing = {
600608
host: options.host,

test/web-socket-proxy-test.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ var protocol = argv.https ? 'https' : 'http',
4747
wsprotocol = argv.https ? 'wss' : 'ws',
4848
runner = new helpers.TestRunner(protocol);
4949

50-
require('eyes').inspect(protocol);
5150
vows.describe('node-http-proxy/websocket').addBatch({
5251
"When using server created by httpProxy.createServer()": {
5352
"with no latency" : {
@@ -69,8 +68,8 @@ vows.describe('node-http-proxy/websocket').addBatch({
6968
//
7069
// Setup the web socket against our proxy
7170
//
72-
var ws = new websocket.WebSocket(wsprotocol + '://localhost:8131/socket.io/websocket/', 'borf', {
73-
origin: 'https://localhost'
71+
var ws = new websocket.WebSocket(wsprotocol + '://home.devjitsu.com:8131/socket.io/websocket/', 'borf', {
72+
origin: protocol + '://home.devjitsu.com'
7473
});
7574

7675
ws.on('wsupgrade', function (req, res) {
@@ -86,7 +85,9 @@ vows.describe('node-http-proxy/websocket').addBatch({
8685
},
8786
"the target server should receive the message": function (err, msg, headers) {
8887
assert.equal(msg, 'from client');
89-
require('eyes').inspect(headers);
88+
},
89+
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
90+
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
9091
}
9192
},
9293
"when an outbound message is sent from the target server": {
@@ -105,8 +106,8 @@ vows.describe('node-http-proxy/websocket').addBatch({
105106
//
106107
// Setup the web socket against our proxy
107108
//
108-
var ws = new websocket.WebSocket(wsprotocol + '://localhost:8133/socket.io/websocket/', 'borf', {
109-
origin: 'https://localhost'
109+
var ws = new websocket.WebSocket(wsprotocol + '://home.devjitsu.com:8133/socket.io/websocket/', 'borf', {
110+
origin: protocol + '://home.devjitsu.com'
110111
});
111112

112113
ws.on('wsupgrade', function (req, res) {
@@ -125,7 +126,9 @@ vows.describe('node-http-proxy/websocket').addBatch({
125126
},
126127
"the client should receive the message": function (err, msg, headers) {
127128
assert.equal(msg, 'from server');
128-
require('eyes').inspect(headers);
129+
},
130+
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
131+
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
129132
}
130133
}
131134
}

vendor/websocket.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ var WebSocket = function(url, proto, opts) {
494494
// string as its first argument to connect to a UNIX socket.
495495
var protocol, agent, port, u = urllib.parse(url);
496496
if (u.protocol === 'ws:' || u.protocol === 'wss:') {
497-
require('eyes').inspect(u);
498497
protocol = u.protocol === 'ws:' ? http : https;
499498
port = u.protocol === 'ws:' ? 80 : 443;
500499
agent = u.protocol === 'ws:' ? protocol.getAgent(u.hostname, u.port || port) : protocol.getAgent({
@@ -614,17 +613,14 @@ var WebSocket = function(url, proto, opts) {
614613
errorListener(e);
615614
});
616615

617-
618-
var x = {
616+
var httpReq = protocol.request({
619617
host: u.hostname,
620618
method: 'GET',
621619
agent: agent,
622620
port: u.port,
623621
path: httpPath,
624622
headers: httpHeaders
625-
};
626-
require('eyes').inspect(x);
627-
var httpReq = protocol.request(x);
623+
});
628624

629625
httpReq.write(challenge, 'binary');
630626
httpReq.end();

0 commit comments

Comments
 (0)