Commit 44a85664 authored by indexzero's avatar indexzero

[test] Continued work around Origin mismatch tests

parent 9ab54ab4
......@@ -588,12 +588,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
_socket(socket);
// Remote host address
var agent = _getAgent(options.host, options.port),
var protocolName = options.https || this.https ? 'https' : 'http',
agent = _getAgent(options.host, options.port),
remoteHost = options.host + (options.port - 80 === 0 ? '' : ':' + options.port);
// Change headers
req.headers.host = remoteHost;
req.headers.origin = 'http://' + options.host;
req.headers.origin = protocolName + '://' + options.host;
outgoing = {
host: options.host,
......
......@@ -29,6 +29,7 @@ var vows = require('vows'),
colors = require('colors'),
request = require('request'),
assert = require('assert'),
argv = require('optimist').argv,
websocket = require('./../vendor/websocket'),
helpers = require('./helpers');
......@@ -42,8 +43,11 @@ catch (ex) {
process.exit(1);
}
var runner = new helpers.TestRunner();
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" : {
......@@ -65,8 +69,8 @@ vows.describe('node-http-proxy/websocket').addBatch({
//
// Setup the web socket against our proxy
//
var ws = new websocket.WebSocket('ws://localhost:8131/socket.io/websocket/', 'borf', {
origin: 'http://localhost'
var ws = new websocket.WebSocket(wsprotocol + '://localhost:8131/socket.io/websocket/', 'borf', {
origin: 'https://localhost'
});
ws.on('wsupgrade', function (req, res) {
......@@ -101,8 +105,8 @@ vows.describe('node-http-proxy/websocket').addBatch({
//
// Setup the web socket against our proxy
//
var ws = new websocket.WebSocket('ws://localhost:8133/socket.io/websocket/', 'borf', {
origin: 'http://localhost'
var ws = new websocket.WebSocket(wsprotocol + '://localhost:8133/socket.io/websocket/', 'borf', {
origin: 'https://localhost'
});
ws.on('wsupgrade', function (req, res) {
......
......@@ -35,6 +35,7 @@ var buffer = require('buffer');
var crypto = require('crypto');
var events = require('events');
var http = require('http');
var https = require('https');
var net = require('net');
var urllib = require('url');
var sys = require('sys');
......@@ -178,16 +179,6 @@ var str2hex = function(str) {
return out.trim();
};
// Get the scheme for a URL, undefined if none is found
var getUrlScheme = function(url) {
var i = url.indexOf(':');
if (i == -1) {
return undefined;
}
return url.substring(0, i);
};
// Set a constant on the given object
var setConstant = function(obj, name, value) {
Object.defineProperty(obj, name, {
......@@ -501,26 +492,31 @@ var WebSocket = function(url, proto, opts) {
// that http.Client passes its constructor arguments through,
// un-inspected to net.Stream.connect(). The latter accepts a
// string as its first argument to connect to a UNIX socket.
var httpClient = undefined;
switch (getUrlScheme(url)) {
case 'ws':
var u = urllib.parse(url);
httpClient = http.createClient(u.port || 80, u.hostname);
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({
host: u.hostname,
port: u.port || port
});
httpPath = (u.pathname || '/') + (u.search || '');
httpHeaders.Host = u.hostname + (u.port ? (":" + u.port) : "");
break;
case 'ws+unix':
var sockPath = url.substring('ws+unix://'.length, url.length);
httpClient = http.createClient(sockPath);
httpHeaders.Host = 'localhost';
break;
default:
}
else if (urlScheme === 'ws+unix') {
throw new Error('ws+unix is not implemented');
// var sockPath = url.substring('ws+unix://'.length, url.length);
// httpClient = http.createClient(sockPath);
// httpHeaders.Host = 'localhost';
}
else {
throw new Error('Invalid URL scheme \'' + urlScheme + '\' specified.');
}
httpClient.on('upgrade', (function() {
if (!agent._events || agent._events['upgrade'].length === 0) {
agent.on('upgrade', (function() {
var data = undefined;
return function(res, s, head) {
......@@ -578,14 +574,12 @@ var WebSocket = function(url, proto, opts) {
});
}
//
// Un-register our data handler and add the one to be used
// for the normal, non-handshaking case. If we have extra
// data left over, manually fire off the handler on
// whatever remains.
//
// XXX: This is lame. We should only remove the listeners
// that we added.
httpClient.removeAllListeners('upgrade');
stream.removeAllListeners('data');
stream.on('data', dataListener);
......@@ -614,12 +608,24 @@ var WebSocket = function(url, proto, opts) {
stream.emit('data', head);
};
})());
httpClient.on('error', function(e) {
httpClient.end();
}
agent.on('error', function (e) {
errorListener(e);
});
var httpReq = httpClient.request(httpPath, httpHeaders);
var x = {
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