Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 11a9b75185 | |||
| d93c853414 | |||
| 2dff35d3e4 | |||
| 6588928445 | |||
| 151fddd206 | |||
| 0b1f16a7ee | |||
| f15b331a34 |
@@ -1,3 +1,9 @@
|
||||
### 0.3.1 / 2013-12-03
|
||||
|
||||
* Add a `maxLength` option to limit allowed frame size
|
||||
* Don't pre-allocate a message buffer until the whole frame has arrived
|
||||
* Fix compatibility with Node v0.11 `HTTPParser`
|
||||
|
||||
### 0.3.0 / 2013-09-09
|
||||
|
||||
* Support client URLs with Basic Auth credentials
|
||||
|
||||
@@ -173,6 +173,8 @@ with masking enabled on outgoing frames.
|
||||
The `options` argument is optional, and is an object. It may contain the
|
||||
following fields:
|
||||
|
||||
* `maxLength` - the maximum allowed size of incoming message frames, in bytes.
|
||||
The default value is `2^30 - 1`, or 1 byte short of 1 GiB.
|
||||
* `protocols` - an array of strings representing acceptable subprotocols for
|
||||
use over the socket. The driver will negotiate one of these to use via the
|
||||
`Sec-WebSocket-Protocol` header if supported by the other peer.
|
||||
|
||||
@@ -22,8 +22,8 @@ var HttpParser = function(type) {
|
||||
self.headers[current] = b.toString('utf8', start, start + length);
|
||||
};
|
||||
|
||||
this._parser.onHeadersComplete = function(info) {
|
||||
self.method = info.method;
|
||||
this._parser.onHeadersComplete = this._parser[HTTPParser.kOnHeadersComplete] = function(info) {
|
||||
self.method = (typeof info.method === 'number') ? HttpParser.METHODS[info.method] : info.method;
|
||||
self.statusCode = info.statusCode;
|
||||
self.url = info.url;
|
||||
|
||||
@@ -34,11 +34,35 @@ var HttpParser = function(type) {
|
||||
self.headers[headers[i].toLowerCase()] = headers[i+1];
|
||||
};
|
||||
|
||||
this._parser.onMessageComplete = function() {
|
||||
this._parser.onMessageComplete = this._parser[HTTPParser.kOnMessageComplete] = function() {
|
||||
self._complete = true;
|
||||
};
|
||||
};
|
||||
|
||||
HttpParser.METHODS = {
|
||||
0: 'DELETE',
|
||||
1: 'GET',
|
||||
2: 'HEAD',
|
||||
3: 'POST',
|
||||
4: 'PUT',
|
||||
5: 'CONNECT',
|
||||
6: 'OPTIONS',
|
||||
7: 'TRACE',
|
||||
8: 'COPY',
|
||||
9: 'LOCK',
|
||||
10: 'MKCOL',
|
||||
11: 'MOVE',
|
||||
12: 'PROPFIND',
|
||||
13: 'PROPPATCH',
|
||||
14: 'SEARCH',
|
||||
15: 'UNLOCK',
|
||||
16: 'REPORT',
|
||||
17: 'MKACTIVITY',
|
||||
18: 'CHECKOUT',
|
||||
19: 'MERGE',
|
||||
24: 'PATCH'
|
||||
};
|
||||
|
||||
HttpParser.prototype.isComplete = function() {
|
||||
return this._complete;
|
||||
};
|
||||
|
||||
@@ -11,6 +11,7 @@ var Hybi = function(request, url, options) {
|
||||
this._stage = 0;
|
||||
this._masking = this._options.masking;
|
||||
this._protocols = this._options.protocols || [];
|
||||
this._maxLength = this._options.maxLength || this.MAX_LENGTH;
|
||||
|
||||
if (typeof this._protocols === 'string')
|
||||
this._protocols = this._protocols.split(/\s*,\s*/);
|
||||
@@ -66,7 +67,9 @@ var instance = {
|
||||
FRAGMENTED_OPCODES: [0, 1, 2],
|
||||
OPENING_OPCODES: [1, 2],
|
||||
|
||||
MAX_LENGTH: Math.pow(2, 53) - 1,
|
||||
// This is the maximum length of a Node buffer:
|
||||
// https://github.com/joyent/node/blob/master/src/smalloc.h#L40
|
||||
MAX_LENGTH: 0x3fffffff,
|
||||
TWO_POWERS: [0, 1, 2, 3, 4, 5, 6, 7].map(function(n) { return Math.pow(2, 8 * n) }),
|
||||
|
||||
ERRORS: {
|
||||
@@ -303,7 +306,7 @@ var instance = {
|
||||
if (this.FRAGMENTED_OPCODES.indexOf(this._opcode) < 0 && this._length > 125)
|
||||
return this._fail('protocol_error', 'Received control frame having too long payload: ' + this._length);
|
||||
|
||||
if (this._length > this.MAX_LENGTH)
|
||||
if (this._length > this._maxLength)
|
||||
return this._fail('too_large', 'WebSocket frame length too large');
|
||||
|
||||
this._stage = this._masked ? 3 : 4;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
var StreamReader = function() {
|
||||
this._queue = [];
|
||||
this._cursor = 0;
|
||||
this._queue = [];
|
||||
this._queueSize = 0;
|
||||
this._cursor = 0;
|
||||
};
|
||||
|
||||
StreamReader.prototype.read = function(bytes) {
|
||||
@@ -11,9 +12,12 @@ StreamReader.prototype.put = function(buffer) {
|
||||
if (!buffer || buffer.length === 0) return;
|
||||
if (!buffer.copy) buffer = new Buffer(buffer);
|
||||
this._queue.push(buffer);
|
||||
this._queueSize += buffer.length;
|
||||
};
|
||||
|
||||
StreamReader.prototype._readBuffer = function(length) {
|
||||
if (length > this._queueSize) return null;
|
||||
|
||||
var buffer = new Buffer(length),
|
||||
queue = this._queue,
|
||||
remain = length,
|
||||
@@ -29,11 +33,10 @@ StreamReader.prototype._readBuffer = function(length) {
|
||||
size = Math.min(remain, chunk.length - offset);
|
||||
chunk.copy(buffer, length - remain, offset, offset + size);
|
||||
remain -= size;
|
||||
this._queueSize -= size;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if (remain > 0) return null;
|
||||
|
||||
queue.splice(0, i-1);
|
||||
this._cursor = (i === 1 ? this._cursor : 0) + size;
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
, "keywords" : ["websocket"]
|
||||
, "license" : "MIT"
|
||||
|
||||
, "version" : "0.3.0"
|
||||
, "version" : "0.3.1"
|
||||
, "engines" : {"node": ">=0.4.0"}
|
||||
, "main" : "./lib/websocket/driver"
|
||||
, "devDependencies" : {"jstest": ""}
|
||||
|
||||
@@ -308,7 +308,7 @@ test.describe("Hybi", function() { with(this) {
|
||||
}})
|
||||
|
||||
it("returns an error for too-large frames", function() { with(this) {
|
||||
driver().parse([0x81, 0x7f, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
|
||||
driver().parse([0x81, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00])
|
||||
assertEqual( "WebSocket frame length too large", error.message )
|
||||
assertEqual( [1009, "WebSocket frame length too large"], close )
|
||||
assertEqual( "closed", driver().getState() )
|
||||
|
||||
Reference in New Issue
Block a user