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