Permalink
Browse files

[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.
  • Loading branch information...
indexzero committed May 18, 2011
1 parent 44a8566 commit 9c6c4b908b7d6ce67144ba9d41702b5694254099
Showing with 28 additions and 21 deletions.
  1. +16 −8 lib/node-http-proxy.js
  2. +10 −7 test/web-socket-proxy-test.js
  3. +2 −6 vendor/websocket.js
View
@@ -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);
@@ -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');
@@ -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,
@@ -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" : {
@@ -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) {
@@ -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": {
@@ -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) {
@@ -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']);
}
}
}
View
@@ -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({
@@ -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();

0 comments on commit 9c6c4b9

Please sign in to comment.