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
......@@ -223,6 +223,7 @@ var HttpProxy = exports.HttpProxy = function (options) {
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,8 +524,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
socket.setTimeout(0);
socket.setNoDelay(true);
if (keepAlive) {
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
if (this.changeOrigin) {
req.headers.host = remoteHost;
req.headers.origin = protocolName + '://' + options.host;
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']);
}
}
}
......
......@@ -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();
......
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