Commit 9c6c4b90 authored by indexzero's avatar indexzero

[fix test api] Only change Origin headers in WebSocket requests when the...

[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.
parent 44a85664
...@@ -219,10 +219,11 @@ exports.createServer = function () { ...@@ -219,10 +219,11 @@ exports.createServer = function () {
var HttpProxy = exports.HttpProxy = function (options) { var HttpProxy = exports.HttpProxy = function (options) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
var self = this; var self = this;
options = options || {}; options = options || {};
this.forward = options.forward; this.forward = options.forward;
this.https = options.https; this.https = options.https;
this.changeOrigin = options.changeOrigin || false;
if (options.router) { if (options.router) {
this.proxyTable = new ProxyTable(options.router, options.silent, options.hostnameOnly); this.proxyTable = new ProxyTable(options.router, options.silent, options.hostnameOnly);
...@@ -523,7 +524,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -523,7 +524,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
socket.setTimeout(0); socket.setTimeout(0);
socket.setNoDelay(true); socket.setNoDelay(true);
if (keepAlive) { 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 { else {
socket.setEncoding('utf8'); socket.setEncoding('utf8');
...@@ -589,12 +595,14 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -589,12 +595,14 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
// Remote host address // Remote host address
var protocolName = options.https || this.https ? 'https' : 'http', 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); remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port);
// Change headers // Change headers
req.headers.host = remoteHost; if (this.changeOrigin) {
req.headers.origin = protocolName + '://' + options.host; req.headers.host = remoteHost;
req.headers.origin = protocolName + '://' + remoteHost;
}
outgoing = { outgoing = {
host: options.host, host: options.host,
......
...@@ -47,7 +47,6 @@ var protocol = argv.https ? 'https' : 'http', ...@@ -47,7 +47,6 @@ var protocol = argv.https ? 'https' : 'http',
wsprotocol = argv.https ? 'wss' : 'ws', wsprotocol = argv.https ? 'wss' : 'ws',
runner = new helpers.TestRunner(protocol); runner = new helpers.TestRunner(protocol);
require('eyes').inspect(protocol);
vows.describe('node-http-proxy/websocket').addBatch({ vows.describe('node-http-proxy/websocket').addBatch({
"When using server created by httpProxy.createServer()": { "When using server created by httpProxy.createServer()": {
"with no latency" : { "with no latency" : {
...@@ -69,8 +68,8 @@ vows.describe('node-http-proxy/websocket').addBatch({ ...@@ -69,8 +68,8 @@ vows.describe('node-http-proxy/websocket').addBatch({
// //
// Setup the web socket against our proxy // Setup the web socket against our proxy
// //
var ws = new websocket.WebSocket(wsprotocol + '://localhost:8131/socket.io/websocket/', 'borf', { var ws = new websocket.WebSocket(wsprotocol + '://home.devjitsu.com:8131/socket.io/websocket/', 'borf', {
origin: 'https://localhost' origin: protocol + '://home.devjitsu.com'
}); });
ws.on('wsupgrade', function (req, res) { ws.on('wsupgrade', function (req, res) {
...@@ -86,7 +85,9 @@ vows.describe('node-http-proxy/websocket').addBatch({ ...@@ -86,7 +85,9 @@ vows.describe('node-http-proxy/websocket').addBatch({
}, },
"the target server should receive the message": function (err, msg, headers) { "the target server should receive the message": function (err, msg, headers) {
assert.equal(msg, 'from client'); 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": { "when an outbound message is sent from the target server": {
...@@ -105,8 +106,8 @@ vows.describe('node-http-proxy/websocket').addBatch({ ...@@ -105,8 +106,8 @@ vows.describe('node-http-proxy/websocket').addBatch({
// //
// Setup the web socket against our proxy // Setup the web socket against our proxy
// //
var ws = new websocket.WebSocket(wsprotocol + '://localhost:8133/socket.io/websocket/', 'borf', { var ws = new websocket.WebSocket(wsprotocol + '://home.devjitsu.com:8133/socket.io/websocket/', 'borf', {
origin: 'https://localhost' origin: protocol + '://home.devjitsu.com'
}); });
ws.on('wsupgrade', function (req, res) { ws.on('wsupgrade', function (req, res) {
...@@ -125,7 +126,9 @@ vows.describe('node-http-proxy/websocket').addBatch({ ...@@ -125,7 +126,9 @@ vows.describe('node-http-proxy/websocket').addBatch({
}, },
"the client should receive the message": function (err, msg, headers) { "the client should receive the message": function (err, msg, headers) {
assert.equal(msg, 'from server'); 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']);
} }
} }
} }
......
...@@ -494,7 +494,6 @@ var WebSocket = function(url, proto, opts) { ...@@ -494,7 +494,6 @@ var WebSocket = function(url, proto, opts) {
// string as its first argument to connect to a UNIX socket. // string as its first argument to connect to a UNIX socket.
var protocol, agent, port, u = urllib.parse(url); var protocol, agent, port, u = urllib.parse(url);
if (u.protocol === 'ws:' || u.protocol === 'wss:') { if (u.protocol === 'ws:' || u.protocol === 'wss:') {
require('eyes').inspect(u);
protocol = u.protocol === 'ws:' ? http : https; protocol = u.protocol === 'ws:' ? http : https;
port = u.protocol === 'ws:' ? 80 : 443; port = u.protocol === 'ws:' ? 80 : 443;
agent = u.protocol === 'ws:' ? protocol.getAgent(u.hostname, u.port || port) : protocol.getAgent({ agent = u.protocol === 'ws:' ? protocol.getAgent(u.hostname, u.port || port) : protocol.getAgent({
...@@ -614,17 +613,14 @@ var WebSocket = function(url, proto, opts) { ...@@ -614,17 +613,14 @@ var WebSocket = function(url, proto, opts) {
errorListener(e); errorListener(e);
}); });
var httpReq = protocol.request({
var x = {
host: u.hostname, host: u.hostname,
method: 'GET', method: 'GET',
agent: agent, agent: agent,
port: u.port, port: u.port,
path: httpPath, path: httpPath,
headers: httpHeaders headers: httpHeaders
}; });
require('eyes').inspect(x);
var httpReq = protocol.request(x);
httpReq.write(challenge, 'binary'); httpReq.write(challenge, 'binary');
httpReq.end(); httpReq.end();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment