Skip to content

Commit

Permalink
websocket draft
Browse files Browse the repository at this point in the history
  • Loading branch information
yawnt committed Sep 5, 2013
1 parent 79f7f99 commit 07551c6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 35 deletions.
81 changes: 46 additions & 35 deletions lib/caronte/passes/ws.js
Expand Up @@ -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;
}
},

/**
* 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]
});
/**
* 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;
}
].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;
});
60 changes: 60 additions & 0 deletions 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

0 comments on commit 07551c6

Please sign in to comment.