Skip to content

Commit

Permalink
[fix test api] Only change Origin headers in WebSocket requests when …
Browse files Browse the repository at this point in the history
…the `changeOrigin` option is set explicitly. Added tests to ensure Origin and sec-websocket-origin headers match when proxying websockets.
  • Loading branch information
indexzero committed May 18, 2011
1 parent 44a8566 commit 9c6c4b9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
24 changes: 16 additions & 8 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@ exports.createServer = function () {
var HttpProxy = exports.HttpProxy = function (options) {
events.EventEmitter.call(this);

var self = this;
options = options || {};
this.forward = options.forward;
this.https = options.https;
var self = this;
options = options || {};
this.forward = options.forward;
this.https = options.https;
this.changeOrigin = options.changeOrigin || false;

if (options.router) {
this.proxyTable = new ProxyTable(options.router, options.silent, options.hostnameOnly);
Expand Down Expand Up @@ -523,7 +524,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
socket.setTimeout(0);
socket.setNoDelay(true);
if (keepAlive) {
socket.setKeepAlive(true, 0);
if (socket.setKeepAlive) {
socket.setKeepAlive(true, 0);
}
else if (socket.pair.cleartext.socket.setKeepAlive) {
socket.pair.cleartext.socket.setKeepAlive(true, 0);
}
}
else {
socket.setEncoding('utf8');
Expand Down Expand Up @@ -589,12 +595,14 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options

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

// Change headers
req.headers.host = remoteHost;
req.headers.origin = protocolName + '://' + options.host;
if (this.changeOrigin) {
req.headers.host = remoteHost;
req.headers.origin = protocolName + '://' + remoteHost;
}

outgoing = {
host: options.host,
Expand Down
17 changes: 10 additions & 7 deletions test/web-socket-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ var protocol = argv.https ? 'https' : 'http',
wsprotocol = argv.https ? 'wss' : 'ws',
runner = new helpers.TestRunner(protocol);

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

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

ws.on('wsupgrade', function (req, res) {
Expand All @@ -125,7 +126,9 @@ vows.describe('node-http-proxy/websocket').addBatch({
},
"the client should receive the message": function (err, msg, headers) {
assert.equal(msg, 'from server');
require('eyes').inspect(headers);
},
"the origin and sec-websocket-origin headers should match": function (err, msg, headers) {
assert.equal(headers.request.Origin, headers.response['sec-websocket-origin']);
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions vendor/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ var WebSocket = function(url, proto, opts) {
// string as its first argument to connect to a UNIX socket.
var protocol, agent, port, u = urllib.parse(url);
if (u.protocol === 'ws:' || u.protocol === 'wss:') {
require('eyes').inspect(u);
protocol = u.protocol === 'ws:' ? http : https;
port = u.protocol === 'ws:' ? 80 : 443;
agent = u.protocol === 'ws:' ? protocol.getAgent(u.hostname, u.port || port) : protocol.getAgent({
Expand Down Expand Up @@ -614,17 +613,14 @@ var WebSocket = function(url, proto, opts) {
errorListener(e);
});


var x = {
var httpReq = protocol.request({
host: u.hostname,
method: 'GET',
agent: agent,
port: u.port,
path: httpPath,
headers: httpHeaders
};
require('eyes').inspect(x);
var httpReq = protocol.request(x);
});

httpReq.write(challenge, 'binary');
httpReq.end();
Expand Down

0 comments on commit 9c6c4b9

Please sign in to comment.