Compare commits

..

8 Commits

Author SHA1 Message Date
James Coglan 4c3eaf8141 Bump version to 0.2.0, add changelog. 2011-12-21 01:28:20 +00:00
James Coglan 0b53267717 protocol property should be '' if not set. 2011-12-21 01:19:39 +00:00
James Coglan bdb0de2cfa Draft-75/76 frames with length headers should be ignored. 2011-12-20 19:44:07 +00:00
James Coglan 6704d33859 Implement the full framing interpreter for draft-75/76, including closing frames for 76. 2011-12-19 22:59:08 +00:00
James Coglan 4240b6bc27 Test that parsers are not sensitive to how data is split across packets. 2011-12-19 21:36:49 +00:00
James Coglan afdf6c223c Change version strings. 2011-12-19 21:25:53 +00:00
James Coglan 2782990fe3 Rename Protocol8Parser to HybiParser. 2011-12-19 21:25:19 +00:00
James Coglan 421f4660e5 Don't use map() to generate masks. 2011-12-19 10:12:26 +00:00
13 changed files with 212 additions and 61 deletions
+23
View File
@@ -0,0 +1,23 @@
=== 0.2.0 / 2011-12-21
* Add support for Sec-WebSocket-Protocol negotiation
* Support hixie-76 close frames and 75/76 ignored segments
* Improve performance of HyBi parsing/framing functions
* Decouple parsers from TCP and reduce write volume
=== 0.1.2 / 2011-12-05
* Detect closed sockets on the server side when TCP connection breaks
* Make hixie-76 sockets work through HAProxy
=== 0.1.1 / 2011-11-30
* Fix addEventListener() interface methods
=== 0.1.0 / 2011-11-27
* Initial release, based on WebSocket components from Faye
+6 -6
View File
@@ -7,15 +7,15 @@
// * http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
// * http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
var Draft75Parser = require('./websocket/draft75_parser'),
Draft76Parser = require('./websocket/draft76_parser'),
Protocol8Parser = require('./websocket/protocol8_parser'),
API = require('./websocket/api');
var Draft75Parser = require('./websocket/draft75_parser'),
Draft76Parser = require('./websocket/draft76_parser'),
HybiParser = require('./websocket/hybi_parser'),
API = require('./websocket/api');
var getParser = function(request) {
var headers = request.headers;
return headers['sec-websocket-version']
? Protocol8Parser
? HybiParser
: (headers['sec-websocket-key1'] && headers['sec-websocket-key2'])
? Draft76Parser
: Draft75Parser;
@@ -45,7 +45,7 @@ var WebSocket = function(request, socket, head, supportedProtos) {
var handshake = this._parser.handshakeResponse(head);
try { this._stream.write(handshake, 'binary') } catch (e) {}
this.protocol = this._parser.protocol;
this.protocol = this._parser.protocol || '';
this.readyState = API.OPEN;
this.version = this._parser.getVersion();
+4 -3
View File
@@ -2,12 +2,13 @@ var API = require('./api'),
net = require('net'),
tls = require('tls');
var Protocol8Parser = require('./protocol8_parser');
var HybiParser = require('./hybi_parser');
var Client = function(url, protocols) {
this.url = url;
this._uri = require('url').parse(url);
this.protocol = '';
this.readyState = API.CONNECTING;
this.bufferedAmount = 0;
@@ -19,7 +20,7 @@ var Client = function(url, protocols) {
? tls.connect(this._uri.port || 443, this._uri.hostname, onConnect)
: net.createConnection(this._uri.port || 80, this._uri.hostname);
this._parser = new Protocol8Parser(this, {masking: true, protocols: protocols});
this._parser = new HybiParser(this, {masking: true, protocols: protocols});
this._stream = connection;
if (!secure) connection.addListener('connect', onConnect);
@@ -51,7 +52,7 @@ Client.prototype._onData = function(data) {
if (!this._handshake.isComplete()) return;
if (this._handshake.isValid()) {
this.protocol = this._handshake.protocol;
this.protocol = this._handshake.protocol || '';
this.readyState = API.OPEN;
var event = new API.Event('open');
event.initEvent('open', false, false);
+57 -23
View File
@@ -1,15 +1,11 @@
var Draft75Parser = function(webSocket) {
this._socket = webSocket;
this._buffer = [];
this._buffering = false;
this._socket = webSocket;
this._stage = 0;
};
var instance = {
FRAME_START : new Buffer([0x00]),
FRAME_END : new Buffer([0xFF]),
getVersion: function() {
return 'draft-75';
return 'hixie-75';
},
handshakeResponse: function() {
@@ -21,33 +17,71 @@ var instance = {
'utf8');
},
parse: function(data) {
for (var i = 0, n = data.length; i < n; i++) {
switch (data[i]) {
case 0x00:
this._buffering = true;
parse: function(buffer) {
var data, message, value;
for (var i = 0, n = buffer.length; i < n; i++) {
data = buffer[i];
switch (this._stage) {
case 0:
this._parseLeadingByte(data);
break;
case 0xFF:
this._buffer = new Buffer(this._buffer);
this._socket.receive(this._buffer.toString('utf8', 0, this._buffer.length));
this._buffer = [];
this._buffering = false;
case 1:
value = (data & 0x7F);
this._length = value + 128 * this._length;
if (this._closing && this._length === 0) {
this._socket.close(null, null, false);
}
else if ((0x80 & data) !== 0x80) {
if (this._length === 0) {
this._socket.receive('');
this._stage = 0;
}
else {
this._buffer = [];
this._stage = 2;
}
}
break;
case 2:
if (data === 0xFF) {
message = new Buffer(this._buffer);
this._socket.receive(message.toString('utf8', 0, this._buffer.length));
this._stage = 0;
}
else {
this._buffer.push(data);
if (this._length && this._buffer.length === this._length)
this._stage = 0;
}
break;
default:
if (this._buffering) this._buffer.push(data[i]);
}
}
},
_parseLeadingByte: function(data) {
if ((0x80 & data) === 0x80) {
this._length = 0;
this._stage = 1;
} else {
delete this._length;
this._buffer = [];
this._stage = 2;
}
},
frame: function(data) {
if (Buffer.isBuffer(data)) return data;
var buffer = new Buffer(data, 'utf8'),
frame = new Buffer(buffer.length + 2);
this.FRAME_START.copy(frame, 0);
frame[0] = 0x00;
frame[buffer.length + 1] = 0xFF;
buffer.copy(frame, 1);
this.FRAME_END.copy(frame, buffer.length + 1);
return frame;
}
+17 -1
View File
@@ -23,7 +23,7 @@ var bigEndian = function(number) {
};
Draft76Parser.prototype.getVersion = function() {
return 'draft-76';
return 'hixie-76';
};
Draft76Parser.prototype.handshakeResponse = function(head) {
@@ -75,5 +75,21 @@ Draft76Parser.prototype.parse = function(data) {
return this.handshakeSignature(data);
};
Draft76Parser.prototype._parseLeadingByte = function(data) {
if (data !== 0xFF)
return Draft75Parser.prototype._parseLeadingByte.call(this, data);
this._closing = true;
this._length = 0;
this._stage = 1;
};
Draft76Parser.prototype.close = function(code, reason, callback, context) {
if (this._closed) return;
if (this._closing) this._socket.send(new Buffer([0xFF, 0x00]));
this._closed = true;
if (callback) callback.call(context);
};
module.exports = Draft76Parser;
@@ -1,8 +1,8 @@
var crypto = require('crypto'),
Handshake = require('./protocol8_parser/handshake'),
Reader = require('./protocol8_parser/stream_reader');
Handshake = require('./hybi_parser/handshake'),
Reader = require('./hybi_parser/stream_reader');
var Protocol8Parser = function(webSocket, options) {
var HybiParser = function(webSocket, options) {
this._reset();
this._socket = webSocket;
this._reader = new Reader();
@@ -14,7 +14,7 @@ var Protocol8Parser = function(webSocket, options) {
this._protocols = this._protocols.split(/\s*,\s*/);
};
Protocol8Parser.mask = function(payload, mask, offset) {
HybiParser.mask = function(payload, mask, offset) {
if (mask.length === 0) return payload;
offset = offset || 0;
@@ -63,7 +63,7 @@ var instance = {
getVersion: function() {
var version = this._socket.request.headers['sec-websocket-version'];
return 'protocol-' + version;
return 'hybi-' + version;
},
handshakeResponse: function() {
@@ -230,9 +230,10 @@ var instance = {
buffer.copy(frame, offset + insert);
if (this._masking) {
mask = new Buffer([1,2,3,4].map(function() { return Math.floor(Math.random() * 256) }));
mask.copy(frame, header);
Protocol8Parser.mask(frame, mask, offset);
mask = [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)];
new Buffer(mask).copy(frame, header);
HybiParser.mask(frame, mask, offset);
}
return frame;
@@ -251,7 +252,7 @@ var instance = {
},
_emitFrame: function() {
var payload = Protocol8Parser.mask(this._payload, this._mask),
var payload = HybiParser.mask(this._payload, this._mask),
opcode = this._opcode;
if (opcode === this.OPCODES.continuation) {
@@ -327,7 +328,7 @@ var instance = {
};
for (var key in instance)
Protocol8Parser.prototype[key] = instance[key];
HybiParser.prototype[key] = instance[key];
module.exports = Protocol8Parser;
module.exports = HybiParser;
+2 -2
View File
@@ -4,10 +4,10 @@
, "author" : "James Coglan <jcoglan@gmail.com> (http://jcoglan.com/)"
, "keywords" : ["websocket"]
, "version" : "0.1.2"
, "version" : "0.2.0"
, "engines" : {"node": ">=0.4.0"}
, "main" : "./lib/faye/websocket"
, "devDependencies" : {"jsclass": ">=3.0.4"}
, "devDependencies" : {"jsclass": ""}
, "bugs" : "http://github.com/jcoglan/faye-websocket-node/issues"
+47 -11
View File
@@ -7,20 +7,56 @@ JS.ENV.Draft75ParserSpec = JS.Test.describe("Draft75Parser", function() { with(t
}})
describe("parse", function() { with(this) {
it("parses text frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
sharedBehavior("draft-75 parser", function() { with(this) {
it("parses text frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
}})
it("parses multiple frames from the same packet", function() { with(this) {
expect(webSocket, "receive").given("Hello").exactly(2)
parser.parse([0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff, 0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
}})
it("parses text frames beginning 0x00-0x7F", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x66, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
}})
it("ignores frames with a length header", function() { with(this) {
expect(webSocket, "receive").exactly(0)
parser.parse([0x80, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
}})
it("parses text following an ignored block", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x80, 0x02, 0x48, 0x65, 0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
}})
it("parses multibyte text frames", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parser.parse([0x00, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf, 0xff])
}})
it("parses frames received in several packets", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parser.parse([0x00, 0x41, 0x70, 0x70, 0x6c, 0x65])
parser.parse([0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf, 0xff])
}})
it("parses fragmented frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x00, 0x48, 0x65, 0x6c])
parser.parse([0x6c, 0x6f, 0xff])
}})
}})
it("parses multibyte text frames", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parser.parse([0x00, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf, 0xff])
}})
behavesLike("draft-75 parser")
it("parses fragmented frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x00, 0x48, 0x65, 0x6c])
parser.parse([0x6c, 0x6f, 0xff])
it("does not close the socket if a 76 close frame is received", function() { with(this) {
expect(webSocket, "close").exactly(0)
expect(webSocket, "receive").given("")
parser.parse([0xFF, 0x00])
}})
}})
+28
View File
@@ -0,0 +1,28 @@
var Draft76Parser = require('../../../lib/faye/websocket/draft76_parser')
JS.ENV.Draft76ParserSpec = JS.Test.describe("Draft76Parser", function() { with(this) {
before(function() { with(this) {
this.webSocket = {dispatchEvent: function() {}}
this.parser = new Draft76Parser(webSocket)
parser._handshakeComplete = true
}})
describe("parse", function() { with(this) {
behavesLike("draft-75 parser")
it("closes the socket if a close frame is received", function() { with(this) {
expect(webSocket, "close")
parser.parse([0xFF, 0x00])
}})
}})
describe("frame", function() { with(this) {
it("returns the given string formatted as a WebSocket frame", function() { with(this) {
assertBufferEqual( [0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff], parser.frame("Hello") )
}})
it("encodes multibyte characters correctly", function() { with(this) {
assertBufferEqual( [0x00, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf, 0xff], parser.frame("Apple = ") )
}})
}})
}})
@@ -1,9 +1,9 @@
var Protocol8Parser = require('../../../lib/faye/websocket/protocol8_parser')
var HybiParser = require('../../../lib/faye/websocket/hybi_parser')
JS.ENV.Protocol8ParserSpec = JS.Test.describe("Protocol8Parser", function() { with(this) {
JS.ENV.HybiParserSpec = JS.Test.describe("HybiParser", function() { with(this) {
before(function() { with(this) {
this.webSocket = {dispatchEvent: function() {}}
this.parser = new Protocol8Parser(webSocket)
this.parser = new HybiParser(webSocket)
}})
define("parse", function() {
@@ -38,6 +38,11 @@ JS.ENV.Protocol8ParserSpec = JS.Test.describe("Protocol8Parser", function() { wi
parse([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
}})
it("parses multiple frames from the same packet", function() { with(this) {
expect(webSocket, "receive").given("Hello").exactly(2)
parse([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
}})
it("parses empty text frames", function() { with(this) {
expect(webSocket, "receive").given("")
parse([0x81, 0x00])
@@ -80,6 +85,12 @@ JS.ENV.Protocol8ParserSpec = JS.Test.describe("Protocol8Parser", function() { wi
parse([0x81, 0x0b, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf])
}})
it("parses frames received in several packets", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parse([0x81, 0x0b, 0x41, 0x70, 0x70, 0x6c])
parse([0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf])
}})
it("parses fragmented multibyte text frames", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parse([0x01, 0x0a, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3])
+2 -1
View File
@@ -47,7 +47,8 @@ JS.require('JS.Test', function() {
JS.require( 'ClientSpec',
'Draft75ParserSpec',
'Protocol8ParserSpec',
'Draft76ParserSpec',
'HybiParserSpec',
JS.Test.method('autorun'))
})