From 07551c63e428551e5d6e52362efd9620a14c71b4 Mon Sep 17 00:00:00 2001 From: yawnt Date: Thu, 5 Sep 2013 17:28:25 +0200 Subject: [PATCH] websocket draft --- lib/caronte/passes/ws.js | 79 ++++++++++++++++++-------------- lib/caronte/streams/websocket.js | 60 ++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 34 deletions(-) create mode 100644 lib/caronte/streams/websocket.js diff --git a/lib/caronte/passes/ws.js b/lib/caronte/passes/ws.js index e91002e..d89f752 100644 --- a/lib/caronte/passes/ws.js +++ b/lib/caronte/passes/ws.js @@ -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; -}); \ No newline at end of file +}, + +/** + * 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; + }); diff --git a/lib/caronte/streams/websocket.js b/lib/caronte/streams/websocket.js new file mode 100644 index 0000000..aa9a8cf --- /dev/null +++ b/lib/caronte/streams/websocket.js @@ -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