From 9c6c4b908b7d6ce67144ba9d41702b5694254099 Mon Sep 17 00:00:00 2001 From: indexzero Date: Tue, 17 May 2011 21:29:00 -0400 Subject: [PATCH] [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. --- lib/node-http-proxy.js | 24 ++++++++++++++++-------- test/web-socket-proxy-test.js | 17 ++++++++++------- vendor/websocket.js | 8 ++------ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 13cc4e291..9c557557a 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -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, diff --git a/test/web-socket-proxy-test.js b/test/web-socket-proxy-test.js index 2865c32f3..34ba113b4 100644 --- a/test/web-socket-proxy-test.js +++ b/test/web-socket-proxy-test.js @@ -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']); } } } diff --git a/vendor/websocket.js b/vendor/websocket.js index ce137ff94..d8c5eeb37 100644 --- a/vendor/websocket.js +++ b/vendor/websocket.js @@ -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();