forked from ncr/node.ws.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws.js
131 lines (112 loc) · 3.3 KB
/
ws.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Github: http://github.com/ncr/node.ws.js
// Compatible with node v0.1.91
// Author: Jacek Becela
// License: MIT
// Based on: http://github.com/Guille/node.websocket.js
function nano(template, data) {
return template.replace(/\{([\w\.]*)}/g, function (str, key) {
var keys = key.split("."), value = data[keys.shift()];
keys.forEach(function (key) { value = value[key] });
return value;
});
}
var sys = require("sys"),
net = require("net"),
headerExpressions = [
/^GET (\/[^\s]*) HTTP\/1\.1$/,
/^Upgrade: WebSocket$/,
/^Connection: Upgrade$/,
/^Host: (.+)$/,
/^Origin: (.+)$/
],
handshakeTemplate = [
'HTTP/1.1 101 Web Socket Protocol Handshake',
'Upgrade: WebSocket',
'Connection: Upgrade',
'WebSocket-Origin: {origin}',
'WebSocket-Location: ws://{host}{resource}',
'',
''
].join("\r\n"),
policy_file = '<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>';
exports.createServer = function (websocketListener) {
return net.createServer(function (socket) {
socket.setTimeout(0);
socket.setNoDelay(true);
socket.setEncoding("utf8");
var emitter = new process.EventEmitter(),
handshaked = false,
buffer = "";
function handle(data) {
buffer += data;
var chunks = buffer.split("\ufffd"),
count = chunks.length - 1; // last is "" or a partial packet
for(var i = 0; i < count; i++) {
var chunk = chunks[i];
if(chunk[0] == "\u0000") {
emitter.emit("data", chunk.slice(1));
} else {
socket.end();
return;
}
}
buffer = chunks[count];
}
function handshake(data) {
var headers = data.split("\r\n");
if(/<policy-file-request.*>/.exec(headers[0])) {
socket.write(policy_file);
socket.end();
return;
}
var matches = [], match;
for (var i = 0, l = headerExpressions.length; i < l; i++) {
match = headerExpressions[i].exec(headers[i]);
if (match) {
if(match.length > 1) {
matches.push(match[1]);
}
} else {
socket.end();
return;
}
}
socket.write(nano(handshakeTemplate, {
resource: matches[0],
host: matches[1],
origin: matches[2],
}));
handshaked = true;
emitter.emit("connect", matches[0]);
}
socket.addListener("data", function (data) {
if(handshaked) {
handle(data);
} else {
handshake(data);
}
}).addListener("end", function () {
socket.end();
}).addListener("close", function () {
if (handshaked) { // don't emit close from policy-requests
emitter.emit("close");
}
});
emitter.remoteAddress = socket.remoteAddress;
emitter.write = function (data) {
try {
socket.write('\u0000', 'binary');
socket.write(data, 'utf8');
socket.write('\uffff', 'binary');
} catch(e) {
// Socket not open for writing,
// should get "close" event just before.
socket.end();
}
}
emitter.end = function () {
socket.end();
}
websocketListener(emitter); // emits: "connect", "data", "close", provides: write(data), end()
});
}