Skip to content

Commit 07551c6

Browse files
committed
websocket draft
1 parent 79f7f99 commit 07551c6

File tree

2 files changed

+106
-35
lines changed

2 files changed

+106
-35
lines changed

lib/caronte/passes/ws.js

+46-35
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,50 @@
1414
var passes = exports;
1515

1616
[
17-
/*
18-
* WebSocket requests must have the `GET` method and
19-
* the `upgrade:websocket` header
20-
*/
21-
function checkMethodAndHeader (req, res, options) {
22-
if (req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket') {
23-
req.end();
24-
// Return true to prevent the next passes to be executed
25-
return true;
26-
}
27-
},
28-
29-
/**
30-
* Sets `x-forwarded-*` headers if specified in config.
31-
*
32-
*/
33-
34-
function XHeaders(req, res, options) {
35-
if(!options.xfwd) return;
36-
37-
var values = {
38-
for : req.connection.remoteAddress || req.socket.remoteAddress,
39-
port : req.connection.remotePort || req.socket.remotePort,
40-
proto: req.connection.pair ? 'wss' : 'ws'
41-
};
42-
43-
['for', 'port', 'proto'].forEach(function(header) {
44-
req.headers['x-forwarded-' + header] =
45-
(req.headers['x-forwarded-' + header] || '') +
46-
(req.headers['x-forwarded-' + header] ? ',' : '') +
47-
values[header]
48-
});
17+
/**
18+
* WebSocket requests must have the `GET` method and
19+
* the `upgrade:websocket` header
20+
*/
21+
22+
function checkMethodAndHeader (req, res, options) {
23+
if (req.method !== 'GET' || req.headers.upgrade.toLowerCase() !== 'websocket') {
24+
req.end();
25+
26+
return true;
4927
}
50-
].forEach(function(func) {
51-
passes[func.name] = func;
52-
});
28+
},
29+
30+
/**
31+
* Sets `x-forwarded-*` headers if specified in config.
32+
*
33+
*/
34+
35+
function XHeaders(req, res, options) {
36+
if(!options.xfwd) return;
37+
38+
var values = {
39+
for : req.connection.remoteAddress || req.socket.remoteAddress,
40+
port : req.connection.remotePort || req.socket.remotePort,
41+
proto: req.connection.pair ? 'wss' : 'ws'
42+
};
43+
44+
['for', 'port', 'proto'].forEach(function(header) {
45+
req.headers['x-forwarded-' + header] =
46+
(req.headers['x-forwarded-' + header] || '') +
47+
(req.headers['x-forwarded-' + header] ? ',' : '') +
48+
values[header]
49+
});
50+
},
51+
52+
/**
53+
*
54+
*
55+
*/
56+
function stream(req, res, options, instance) {
57+
req.pipe(new WebsocketStream(options, instance)).pipe(res);
58+
}
59+
60+
] // <--
61+
.forEach(function(func) {
62+
passes[func.name] = func;
63+
});

lib/caronte/streams/websocket.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var Duplex = require('stream').Duplex,
2+
common = require('common'),
3+
http = require('http'),
4+
https = require('https');
5+
6+
function WebsocketStream(options, res, instance) {
7+
Duplex.call(this);
8+
9+
this.options = options;
10+
this.res = res;
11+
this.instance = intance;
12+
13+
var self = this;
14+
15+
this.once('pipe', function(pipe) { self.onPipe(pipe); });
16+
this.once('finish', function() { self.onFinish(); });
17+
}
18+
19+
require('util').inherits(WebsocketStream, Duplex);
20+
21+
WebsocketStream.prototype.onPipe = function(req) {
22+
this.req = req;
23+
24+
var self = this;
25+
26+
this.proxyReq = (self.options.ssl ? https : http).request(
27+
common.setupOutgoing(self.options.ssl || {}, self.options, req)
28+
);
29+
30+
this.proxyReq.once('response', function(proxyRes) {
31+
self.onResponse(proxyRes);
32+
});
33+
this.proxyReq.on('error', function(e) {
34+
self.onError(e);
35+
});
36+
};
37+
38+
WebsocketStream.prototye.onFinish = function() {
39+
40+
};
41+
42+
WebsocketStream.prototype.onResponse = function(proxyRes) {
43+
44+
};
45+
46+
WebsocketStream.prototype.onError = function(e) {
47+
48+
};
49+
50+
51+
WebsocketStream.prototype._write = function(chunk, encoding, callback) {
52+
53+
};
54+
55+
WebsocketStream.prototype._read = function(size) {
56+
57+
};
58+
59+
60+
WebsocketStream.prototype

0 commit comments

Comments
 (0)