websocket draft

This commit is contained in:
yawnt 2013-09-05 17:28:25 +02:00
parent 79f7f99528
commit 07551c63e4
2 changed files with 105 additions and 34 deletions

View File

@ -14,39 +14,50 @@
var passes = exports;
[
/*
* WebSocket requests must have the `GET` method and
* the `upgrade:websocket` header
*/
function checkMethodAndHeader (req, res, options) {
if (req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket') {
req.end();
// Return true to prevent the next passes to be executed
return true;
}
},
/**
* WebSocket requests must have the `GET` method and
* the `upgrade:websocket` header
*/
/**
* Sets `x-forwarded-*` headers if specified in config.
*
*/
function XHeaders(req, res, options) {
if(!options.xfwd) return;
var values = {
for : req.connection.remoteAddress || req.socket.remoteAddress,
port : req.connection.remotePort || req.socket.remotePort,
proto: req.connection.pair ? 'wss' : 'ws'
};
['for', 'port', 'proto'].forEach(function(header) {
req.headers['x-forwarded-' + header] =
(req.headers['x-forwarded-' + header] || '') +
(req.headers['x-forwarded-' + header] ? ',' : '') +
values[header]
});
function checkMethodAndHeader (req, res, options) {
if (req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket') {
req.end();
return true;
}
].forEach(function(func) {
passes[func.name] = func;
});
},
/**
* Sets `x-forwarded-*` headers if specified in config.
*
*/
function XHeaders(req, res, options) {
if(!options.xfwd) return;
var values = {
for : req.connection.remoteAddress || req.socket.remoteAddress,
port : req.connection.remotePort || req.socket.remotePort,
proto: req.connection.pair ? 'wss' : 'ws'
};
['for', 'port', 'proto'].forEach(function(header) {
req.headers['x-forwarded-' + header] =
(req.headers['x-forwarded-' + header] || '') +
(req.headers['x-forwarded-' + header] ? ',' : '') +
values[header]
});
},
/**
*
*
*/
function stream(req, res, options, instance) {
req.pipe(new WebsocketStream(options, instance)).pipe(res);
}
] // <--
.forEach(function(func) {
passes[func.name] = func;
});

View File

@ -0,0 +1,60 @@
var Duplex = require('stream').Duplex,
common = require('common'),
http = require('http'),
https = require('https');
function WebsocketStream(options, res, instance) {
Duplex.call(this);
this.options = options;
this.res = res;
this.instance = intance;
var self = this;
this.once('pipe', function(pipe) { self.onPipe(pipe); });
this.once('finish', function() { self.onFinish(); });
}
require('util').inherits(WebsocketStream, Duplex);
WebsocketStream.prototype.onPipe = function(req) {
this.req = req;
var self = this;
this.proxyReq = (self.options.ssl ? https : http).request(
common.setupOutgoing(self.options.ssl || {}, self.options, req)
);
this.proxyReq.once('response', function(proxyRes) {
self.onResponse(proxyRes);
});
this.proxyReq.on('error', function(e) {
self.onError(e);
});
};
WebsocketStream.prototye.onFinish = function() {
};
WebsocketStream.prototype.onResponse = function(proxyRes) {
};
WebsocketStream.prototype.onError = function(e) {
};
WebsocketStream.prototype._write = function(chunk, encoding, callback) {
};
WebsocketStream.prototype._read = function(size) {
};
WebsocketStream.prototype