Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 90b8c9d23a | |||
| c3ac50931e | |||
| 5f6873ebc0 | |||
| 309b5651a7 | |||
| 1cc0f33e1c | |||
| b273147f0c | |||
| d1796bef12 | |||
| 0034f5fe19 | |||
| 09f638893e | |||
| 8a0235ef51 | |||
| 2b99c23788 | |||
| 23d0a9ea5e | |||
| 0b7eedc3b5 | |||
| 201e3dd7c5 | |||
| 8cf42d6050 | |||
| ebe4ce1382 | |||
| daa37974c6 | |||
| cc5e5c245e | |||
| bdc081ab57 | |||
| 57e74231cf | |||
| 5cba268409 |
+2
-1
@@ -4,7 +4,8 @@ node_js:
|
||||
- "0.6"
|
||||
- "0.8"
|
||||
- "0.10"
|
||||
- "0.11"
|
||||
- "0.12"
|
||||
- "iojs"
|
||||
|
||||
before_install:
|
||||
- '[ "${TRAVIS_NODE_VERSION}" = "0.6" ] && npm conf set strict-ssl false || true'
|
||||
|
||||
+27
-2
@@ -1,3 +1,26 @@
|
||||
### 0.5.4 / 2015-03-29
|
||||
|
||||
* Don't emit extra close frames if we receive a close frame after we already
|
||||
sent one
|
||||
* Fail the connection when the driver receives an invalid
|
||||
`Sec-WebSocket-Extensions` header
|
||||
|
||||
### 0.5.3 / 2015-02-22
|
||||
|
||||
* Don't treat incoming data as WebSocket frames if a client driver is closed
|
||||
before receiving the server handshake
|
||||
|
||||
### 0.5.2 / 2015-02-19
|
||||
|
||||
* Fix compatibility with the HTTP parser on io.js
|
||||
* Use `websocket-extensions` to make sure messages and close frames are kept in
|
||||
order
|
||||
* Don't emit multiple `error` events
|
||||
|
||||
### 0.5.1 / 2014-12-18
|
||||
|
||||
* Don't allow drivers to be created with unrecognized options
|
||||
|
||||
### 0.5.0 / 2014-12-13
|
||||
|
||||
* Support protocol extensions via the websocket-extensions module
|
||||
@@ -17,7 +40,8 @@
|
||||
|
||||
### 0.3.4 / 2014-05-08
|
||||
|
||||
* Don't hold memory-leaking references to I/O buffers after they have been parsed
|
||||
* Don't hold memory-leaking references to I/O buffers after they have been
|
||||
parsed
|
||||
|
||||
### 0.3.3 / 2014-04-24
|
||||
|
||||
@@ -25,7 +49,8 @@
|
||||
|
||||
### 0.3.2 / 2013-12-29
|
||||
|
||||
* Expand `maxLength` to cover sequences of continuation frames and `draft-{75,76}`
|
||||
* Expand `maxLength` to cover sequences of continuation frames and
|
||||
`draft-{75,76}`
|
||||
* Decrease default maximum frame buffer size to 64MB
|
||||
* Stop parsing when the protocol enters a failure mode, to save CPU cycles
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
# websocket-driver [](https://travis-ci.org/faye/websocket-driver-node)
|
||||
|
||||
This module provides a complete implementation of the WebSocket protocols that
|
||||
can be hooked up to any I/O stream. It aims to simplify things by decoupling
|
||||
the protocol details from the I/O layer, such that users only need to implement
|
||||
code to stream data in and out of it without needing to know anything about how
|
||||
the protocol actually works. Think of it as a complete WebSocket system with
|
||||
can be hooked up to any I/O stream. It aims to simplify things by decoupling the
|
||||
protocol details from the I/O layer, such that users only need to implement code
|
||||
to stream data in and out of it without needing to know anything about how the
|
||||
protocol actually works. Think of it as a complete WebSocket system with
|
||||
pluggable I/O.
|
||||
|
||||
Due to this design, you get a lot of things for free. In particular, if you
|
||||
hook this module up to some I/O object, it will do all of this for you:
|
||||
Due to this design, you get a lot of things for free. In particular, if you hook
|
||||
this module up to some I/O object, it will do all of this for you:
|
||||
|
||||
* Select the correct server-side driver to talk to the client
|
||||
* Generate and send both server- and client-side handshakes
|
||||
@@ -40,18 +40,18 @@ $ npm install websocket-driver
|
||||
|
||||
## Usage
|
||||
|
||||
This module provides protocol drivers that have the same interface on the
|
||||
server and on the client. A WebSocket driver is an object with two duplex
|
||||
streams attached; one for incoming/outgoing messages and one for managing the
|
||||
wire protocol over an I/O stream. The full API is described below.
|
||||
This module provides protocol drivers that have the same interface on the server
|
||||
and on the client. A WebSocket driver is an object with two duplex streams
|
||||
attached; one for incoming/outgoing messages and one for managing the wire
|
||||
protocol over an I/O stream. The full API is described below.
|
||||
|
||||
|
||||
### Server-side with HTTP
|
||||
|
||||
A Node webserver emits a special event for 'upgrade' requests, and this is
|
||||
where you should handle WebSockets. You first check whether the request is a
|
||||
WebSocket, and if so you can create a driver and attach the request's I/O
|
||||
stream to it.
|
||||
A Node webserver emits a special event for 'upgrade' requests, and this is where
|
||||
you should handle WebSockets. You first check whether the request is a
|
||||
WebSocket, and if so you can create a driver and attach the request's I/O stream
|
||||
to it.
|
||||
|
||||
```js
|
||||
var http = require('http'),
|
||||
@@ -150,8 +150,8 @@ driver.messages.on('data', function(message) {
|
||||
});
|
||||
```
|
||||
|
||||
Client drivers have two additional properties for reading the HTTP data that
|
||||
was sent back by the server:
|
||||
Client drivers have two additional properties for reading the HTTP data that was
|
||||
sent back by the server:
|
||||
|
||||
* `driver.statusCode` - the integer value of the HTTP status code
|
||||
* `driver.headers` - an object containing the response headers
|
||||
@@ -229,23 +229,23 @@ driver = websocket.client(url, options)
|
||||
The `http` method returns a driver chosen using the headers from a Node HTTP
|
||||
request object. The `server` method returns a driver that will parse an HTTP
|
||||
request and then decide which driver to use for it using the `http` method. The
|
||||
`client` method always returns a driver for the RFC version of the protocol
|
||||
with masking enabled on outgoing frames.
|
||||
`client` method always returns a driver for the RFC version of the protocol 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^26 - 1`, or 1 byte short of 64 MiB.
|
||||
* `protocols` - an array of strings representing acceptable subprotocols for
|
||||
use over the socket. The driver will negotiate one of these to use via the
|
||||
* `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.
|
||||
|
||||
A driver has two duplex streams attached to it:
|
||||
|
||||
* <b>`driver.io`</b> - this stream should be attached to an I/O socket like a
|
||||
TCP stream. Pipe incoming TCP chunks to this stream for them to be parsed,
|
||||
and pipe this stream back into TCP to send outgoing frames.
|
||||
TCP stream. Pipe incoming TCP chunks to this stream for them to be parsed, and
|
||||
pipe this stream back into TCP to send outgoing frames.
|
||||
* <b>`driver.messages`</b> - this stream emits messages received over the
|
||||
WebSocket. Writing to it sends messages to the other peer by emitting frames
|
||||
via the `driver.io` stream.
|
||||
@@ -254,8 +254,8 @@ All drivers respond to the following API methods, but some of them are no-ops
|
||||
depending on whether the client supports the behaviour.
|
||||
|
||||
Note that most of these methods are commands: if they produce data that should
|
||||
be sent over the socket, they will give this to you by emitting `data` events
|
||||
on the `driver.io` stream.
|
||||
be sent over the socket, they will give this to you by emitting `data` events on
|
||||
the `driver.io` stream.
|
||||
|
||||
#### `driver.on('open', function(event) {})`
|
||||
|
||||
@@ -298,7 +298,7 @@ when the headers are serialized and sent.
|
||||
|
||||
Initiates the protocol by sending the handshake - either the response for a
|
||||
server-side driver or the request for a client-side one. This should be the
|
||||
first method you invoke. Returns `true` iff a handshake was sent.
|
||||
first method you invoke. Returns `true` if and only if a handshake was sent.
|
||||
|
||||
#### `driver.parse(string)`
|
||||
|
||||
@@ -318,8 +318,8 @@ This method is equivalent to `driver.messages.write(string)`.
|
||||
#### `driver.binary(buffer)`
|
||||
|
||||
Takes a `Buffer` and sends it as a binary message. Will queue and return `true`
|
||||
or `false` the same way as the `text` method. It will also return `false` if
|
||||
the driver does not support binary messages.
|
||||
or `false` the same way as the `text` method. It will also return `false` if the
|
||||
driver does not support binary messages.
|
||||
|
||||
This method is equivalent to `driver.messages.write(buffer)`.
|
||||
|
||||
@@ -332,11 +332,11 @@ frames can no longer be sent, or if the driver does not support ping/pong.
|
||||
|
||||
#### `driver.close()`
|
||||
|
||||
Initiates the closing handshake if the socket is still open. For drivers with
|
||||
no closing handshake, this will result in the immediate execution of the
|
||||
`on('close')` driver. For drivers with a closing handshake, this sends a
|
||||
closing frame and `emit('close')` will execute when a response is received or a
|
||||
protocol error occurs.
|
||||
Initiates the closing handshake if the socket is still open. For drivers with no
|
||||
closing handshake, this will result in the immediate execution of the
|
||||
`on('close')` driver. For drivers with a closing handshake, this sends a closing
|
||||
frame and `emit('close')` will execute when a response is received or a protocol
|
||||
error occurs.
|
||||
|
||||
#### `driver.version`
|
||||
|
||||
@@ -346,30 +346,29 @@ Returns the WebSocket version in use as a string. Will either be `hixie-75`,
|
||||
#### `driver.protocol`
|
||||
|
||||
Returns a string containing the selected subprotocol, if any was agreed upon
|
||||
using the `Sec-WebSocket-Protocol` mechanism. This value becomes available
|
||||
after `emit('open')` has fired.
|
||||
using the `Sec-WebSocket-Protocol` mechanism. This value becomes available after
|
||||
`emit('open')` has fired.
|
||||
|
||||
|
||||
## License
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2010-2014 James Coglan
|
||||
Copyright (c) 2010-2015 James Coglan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the 'Software'), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var net = require('net'),
|
||||
websocket = require('../lib/websocket/driver'),
|
||||
websocket = require('..'),
|
||||
deflate = require('permessage-deflate');
|
||||
|
||||
var server = net.createServer(function(connection) {
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
// * http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76
|
||||
// * http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
|
||||
|
||||
var Client = require('./driver/client'),
|
||||
var Base = require('./driver/base'),
|
||||
Client = require('./driver/client'),
|
||||
Server = require('./driver/server');
|
||||
|
||||
var Driver = {
|
||||
@@ -39,6 +40,10 @@ var Driver = {
|
||||
return request.method === 'GET' &&
|
||||
connection.toLowerCase().split(/ *, */).indexOf('upgrade') >= 0 &&
|
||||
upgrade.toLowerCase() === 'websocket';
|
||||
},
|
||||
|
||||
validateOptions: function(options, validKeys) {
|
||||
Base.validateOptions(options, validKeys);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ var Emitter = require('events').EventEmitter,
|
||||
|
||||
var Base = function(request, url, options) {
|
||||
Emitter.call(this);
|
||||
Base.validateOptions(options || {}, ['maxLength', 'masking', 'requireMasking', 'protocols']);
|
||||
|
||||
this._request = request;
|
||||
this._options = options || {};
|
||||
@@ -22,6 +23,13 @@ var Base = function(request, url, options) {
|
||||
};
|
||||
util.inherits(Base, Emitter);
|
||||
|
||||
Base.validateOptions = function(options, validKeys) {
|
||||
for (var key in options) {
|
||||
if (validKeys.indexOf(key) < 0)
|
||||
throw new Error('Unrecognized option: ' + key);
|
||||
}
|
||||
};
|
||||
|
||||
var instance = {
|
||||
// This is 64MB, small enough for an average VPS to handle without
|
||||
// crashing from process out of memory
|
||||
@@ -69,7 +77,9 @@ var instance = {
|
||||
|
||||
start: function() {
|
||||
if (this.readyState !== 0) return false;
|
||||
this._write(this._handshakeResponse());
|
||||
var response = this._handshakeResponse();
|
||||
if (!response) return false;
|
||||
this._write(response);
|
||||
if (this._stage !== -1) this._open();
|
||||
return true;
|
||||
},
|
||||
@@ -86,6 +96,10 @@ var instance = {
|
||||
return false;
|
||||
},
|
||||
|
||||
pong: function() {
|
||||
return false;
|
||||
},
|
||||
|
||||
close: function(reason, code) {
|
||||
if (this.readyState !== 1) return false;
|
||||
this.readyState = 3;
|
||||
|
||||
@@ -53,6 +53,7 @@ var instance = {
|
||||
},
|
||||
|
||||
parse: function(data) {
|
||||
if (this.readyState === 3) return;
|
||||
if (this.readyState > 0) return Hybi.prototype.parse.call(this, data);
|
||||
|
||||
this._http.parse(data);
|
||||
|
||||
@@ -153,19 +153,28 @@ var instance = {
|
||||
},
|
||||
|
||||
text: function(message) {
|
||||
if (this.readyState > 1) return false;
|
||||
return this.frame(message, 'text');
|
||||
},
|
||||
|
||||
binary: function(message) {
|
||||
if (this.readyState > 1) return false;
|
||||
return this.frame(message, 'binary');
|
||||
},
|
||||
|
||||
ping: function(message, callback) {
|
||||
if (this.readyState > 1) return false;
|
||||
message = message || '';
|
||||
if (callback) this._pingCallbacks[message] = callback;
|
||||
return this.frame(message, 'ping');
|
||||
},
|
||||
|
||||
pong: function(message) {
|
||||
if (this.readyState > 1) return false;
|
||||
message = message ||'';
|
||||
return this.frame(message, 'pong');
|
||||
},
|
||||
|
||||
close: function(reason, code) {
|
||||
reason = reason || '';
|
||||
code = code || this.ERRORS.normal_closure;
|
||||
@@ -175,8 +184,8 @@ var instance = {
|
||||
this.emit('close', new Base.CloseEvent(code, reason));
|
||||
return true;
|
||||
} else if (this.readyState === 1) {
|
||||
this.frame(reason, 'close', code);
|
||||
this.readyState = 2;
|
||||
this._extensions.close(function() { this.frame(reason, 'close', code) }, this);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@@ -185,7 +194,7 @@ var instance = {
|
||||
|
||||
frame: function(data, type, code) {
|
||||
if (this.readyState <= 0) return this._queue([data, type, code]);
|
||||
if (this.readyState !== 1) return false;
|
||||
if (this.readyState > 2) return false;
|
||||
|
||||
if (data instanceof Array) data = new Buffer(data);
|
||||
|
||||
@@ -278,7 +287,12 @@ var instance = {
|
||||
},
|
||||
|
||||
_handshakeResponse: function() {
|
||||
var extensions = this._extensions.generateResponse(this._request.headers['sec-websocket-extensions']);
|
||||
try {
|
||||
var extensions = this._extensions.generateResponse(this._request.headers['sec-websocket-extensions']);
|
||||
} catch (e) {
|
||||
return this._fail('protocol_error', e.message);
|
||||
}
|
||||
|
||||
if (extensions) this._headers.set('Sec-WebSocket-Extensions', extensions);
|
||||
|
||||
var start = 'HTTP/1.1 101 Switching Protocols',
|
||||
@@ -288,16 +302,22 @@ var instance = {
|
||||
},
|
||||
|
||||
_shutdown: function(code, reason) {
|
||||
this.frame(reason, 'close', code);
|
||||
delete this._frame;
|
||||
delete this._message;
|
||||
this.readyState = 3;
|
||||
|
||||
var sendCloseFrame = (this.readyState === 1);
|
||||
this.readyState = 2;
|
||||
this._stage = 5;
|
||||
this.emit('close', new Base.CloseEvent(code, reason));
|
||||
this._extensions.close();
|
||||
|
||||
this._extensions.close(function() {
|
||||
if (sendCloseFrame) this.frame(reason, 'close', code);
|
||||
this.readyState = 3;
|
||||
this.emit('close', new Base.CloseEvent(code, reason));
|
||||
}, this);
|
||||
},
|
||||
|
||||
_fail: function(type, message) {
|
||||
if (this.readyState > 1) return;
|
||||
this.emit('error', new Error(message));
|
||||
this._shutdown(this.ERRORS[type], message);
|
||||
},
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
var StreamReader = function() {
|
||||
this._queue = [];
|
||||
this._queueSize = 0;
|
||||
this._cursor = 0;
|
||||
};
|
||||
|
||||
StreamReader.prototype.put = function(buffer) {
|
||||
@@ -15,29 +14,49 @@ StreamReader.prototype.put = function(buffer) {
|
||||
|
||||
StreamReader.prototype.read = function(length) {
|
||||
if (length > this._queueSize) return null;
|
||||
if (length === 0) return new Buffer(0);
|
||||
|
||||
var queue = this._queue,
|
||||
first = queue[0],
|
||||
buffer;
|
||||
|
||||
if (first.length >= length) {
|
||||
this._queueSize -= length;
|
||||
if (first.length === length) {
|
||||
return queue.shift();
|
||||
} else {
|
||||
buffer = first.slice(0, length);
|
||||
queue[0] = first.slice(length);
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
var remain = length, buffers;
|
||||
|
||||
for (var i=0, n = queue.length; i < n; i++) {
|
||||
if (remain < queue[i].length) break;
|
||||
remain -= queue[i].length;
|
||||
}
|
||||
buffers = queue.splice(0, i);
|
||||
|
||||
if (remain > 0 && queue.length > 0) {
|
||||
buffers.push(queue[0].slice(0, remain));
|
||||
queue[0] = queue[0].slice(remain);
|
||||
}
|
||||
this._queueSize -= length;
|
||||
return this._concat(buffers, length);
|
||||
};
|
||||
|
||||
StreamReader.prototype._concat = function(buffers, length) {
|
||||
if (Buffer.concat) return Buffer.concat(buffers, length);
|
||||
|
||||
var buffer = new Buffer(length),
|
||||
queue = this._queue,
|
||||
remain = length,
|
||||
n = queue.length,
|
||||
i = 0,
|
||||
chunk, size;
|
||||
offset = 0;
|
||||
|
||||
while (remain > 0 && i < n) {
|
||||
chunk = queue[i];
|
||||
size = Math.min(remain, chunk.length - this._cursor);
|
||||
|
||||
chunk.copy(buffer, length - remain, this._cursor, this._cursor + size);
|
||||
|
||||
remain -= size;
|
||||
this._queueSize -= size;
|
||||
this._cursor = (this._cursor + size) % chunk.length;
|
||||
|
||||
i += 1;
|
||||
for (var i = 0, n = buffers.length; i < n; i++) {
|
||||
buffers[i].copy(buffer, offset);
|
||||
offset += buffers[i].length;
|
||||
}
|
||||
|
||||
queue.splice(0, this._cursor === 0 ? i : i - 1);
|
||||
|
||||
return buffer;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
var HTTPParser = process.binding('http_parser').HTTPParser,
|
||||
version = HTTPParser.RESPONSE ? 6 : 4;
|
||||
var NodeHTTPParser = process.binding('http_parser').HTTPParser,
|
||||
version = NodeHTTPParser.RESPONSE ? 6 : 4;
|
||||
|
||||
var HttpParser = function(type) {
|
||||
if (type === 'request')
|
||||
this._parser = new HTTPParser(HTTPParser.REQUEST || 'request');
|
||||
this._parser = new NodeHTTPParser(NodeHTTPParser.REQUEST || 'request');
|
||||
else
|
||||
this._parser = new HTTPParser(HTTPParser.RESPONSE || 'response');
|
||||
this._parser = new NodeHTTPParser(NodeHTTPParser.RESPONSE || 'response');
|
||||
|
||||
this._type = type;
|
||||
this._complete = false;
|
||||
@@ -29,15 +29,24 @@ var HttpParser = function(type) {
|
||||
self.headers[current] = value;
|
||||
};
|
||||
|
||||
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;
|
||||
this._parser.onHeadersComplete = this._parser[NodeHTTPParser.kOnHeadersComplete] =
|
||||
function(majorVersion, minorVersion, headers, method, pathname, statusCode) {
|
||||
var info = arguments[0];
|
||||
|
||||
if (typeof info === 'object') {
|
||||
method = info.method;
|
||||
pathname = info.url;
|
||||
statusCode = info.statusCode;
|
||||
headers = info.headers;
|
||||
}
|
||||
|
||||
self.method = (typeof method === 'number') ? HttpParser.METHODS[method] : method;
|
||||
self.statusCode = statusCode;
|
||||
self.url = pathname;
|
||||
|
||||
var headers = info.headers, key, value;
|
||||
if (!headers) return;
|
||||
|
||||
for (var i = 0, n = headers.length; i < n; i += 2) {
|
||||
for (var i = 0, n = headers.length, key, value; i < n; i += 2) {
|
||||
key = headers[i].toLowerCase();
|
||||
value = headers[i+1];
|
||||
if (self.headers.hasOwnProperty(key))
|
||||
|
||||
@@ -126,7 +126,8 @@ Messages.prototype.resume = function() {
|
||||
// the source whether to back off.
|
||||
Messages.prototype.write = function(message) {
|
||||
if (!this.writable) return false;
|
||||
this._driver.frame(message);
|
||||
if (typeof message === 'string') this._driver.text(message);
|
||||
else this._driver.binary(message);
|
||||
return !this._paused;
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -5,10 +5,10 @@
|
||||
, "keywords" : ["websocket"]
|
||||
, "license" : "MIT"
|
||||
|
||||
, "version" : "0.5.0"
|
||||
, "version" : "0.5.4"
|
||||
, "engines" : {"node": ">=0.6.0"}
|
||||
, "main" : "./lib/websocket/driver"
|
||||
, "dependencies" : {"websocket-extensions": ">=0.1.0"}
|
||||
, "dependencies" : {"websocket-extensions": ">=0.1.1"}
|
||||
, "devDependencies" : {"jstest": "", "permessage-deflate": ""}
|
||||
|
||||
, "scripts" : {"test": "jstest spec/runner.js"}
|
||||
|
||||
@@ -79,6 +79,37 @@ test.describe("Hybi", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("with invalid extensions", function() { with(this) {
|
||||
before(function() { with(this) {
|
||||
request().headers["sec-websocket-extensions"] = "x-webkit- -frame"
|
||||
}})
|
||||
|
||||
it("does not write a handshake", function() { with(this) {
|
||||
expect(driver().io, "emit").exactly(0)
|
||||
driver().start()
|
||||
}})
|
||||
|
||||
it("does not trigger the onopen event", function() { with(this) {
|
||||
driver().start()
|
||||
assertEqual( false, open )
|
||||
}})
|
||||
|
||||
it("triggers the onerror event", function() { with(this) {
|
||||
driver().start()
|
||||
assertEqual( "Invalid Sec-WebSocket-Extensions header: x-webkit- -frame", error.message )
|
||||
}})
|
||||
|
||||
it("triggers the onclose event", function() { with(this) {
|
||||
driver().start()
|
||||
assertEqual( [1002, "Invalid Sec-WebSocket-Extensions header: x-webkit- -frame"], close )
|
||||
}})
|
||||
|
||||
it("changes the state to closed", function() { with(this) {
|
||||
driver().start()
|
||||
assertEqual( "closed", driver().getState() )
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("with custom headers", function() { with(this) {
|
||||
before(function() { with(this) {
|
||||
driver().setHeader("Authorization", "Bearer WAT")
|
||||
@@ -160,6 +191,30 @@ test.describe("Hybi", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("pong", function() { with(this) {
|
||||
it("does not write to the socket", function() { with(this) {
|
||||
expect(driver().io, "emit").exactly(0)
|
||||
driver().pong()
|
||||
}})
|
||||
|
||||
it("returns true", function() { with(this) {
|
||||
assertEqual( true, driver().pong() )
|
||||
}})
|
||||
|
||||
it("queues the pong until the handshake has been sent", function() { with(this) {
|
||||
expect(driver().io, "emit").given("data", buffer(
|
||||
"HTTP/1.1 101 Switching Protocols\r\n" +
|
||||
"Upgrade: websocket\r\n" +
|
||||
"Connection: Upgrade\r\n" +
|
||||
"Sec-WebSocket-Accept: JdiiuafpBKRqD7eol0y4vJDTsTs=\r\n" +
|
||||
"\r\n"))
|
||||
expect(driver().io, "emit").given("data", buffer([0x8a, 0]))
|
||||
|
||||
driver().pong()
|
||||
driver().start()
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("close", function() { with(this) {
|
||||
it("does not write anything to the socket", function() { with(this) {
|
||||
expect(driver().io, "emit").exactly(0)
|
||||
@@ -394,6 +449,17 @@ test.describe("Hybi", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("pong", function() { with(this) {
|
||||
it("writes a pong frame to the socket", function() { with(this) {
|
||||
driver().pong("mic check")
|
||||
assertEqual([0x8a, 0x09, 0x6d, 0x69, 0x63, 0x20, 0x63, 0x68, 0x65, 0x63, 0x6b], collector().bytes)
|
||||
}})
|
||||
|
||||
it("returns true", function() { with(this) {
|
||||
assertEqual(true, driver().pong())
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("close", function() { with(this) {
|
||||
it("writes a close frame to the socket", function() { with(this) {
|
||||
driver().close("<%= reasons %>", 1003)
|
||||
@@ -451,8 +517,8 @@ test.describe("Hybi", function() { with(this) {
|
||||
driver().frame("dropped")
|
||||
}})
|
||||
|
||||
it("returns false", function() { with(this) {
|
||||
assertEqual( false, driver().frame("wut") )
|
||||
it("returns true", function() { with(this) {
|
||||
assertEqual( true, driver().frame("wut") )
|
||||
}})
|
||||
}})
|
||||
|
||||
@@ -467,6 +533,17 @@ test.describe("Hybi", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("pong", function() { with(this) {
|
||||
it("does not write to the socket", function() { with(this) {
|
||||
expect(driver().io, "emit").exactly(0)
|
||||
driver().pong()
|
||||
}})
|
||||
|
||||
it("returns false", function() { with(this) {
|
||||
assertEqual( false, driver().pong() )
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("close", function() { with(this) {
|
||||
it("does not write to the socket", function() { with(this) {
|
||||
expect(driver().io, "emit").exactly(0)
|
||||
@@ -490,6 +567,11 @@ test.describe("Hybi", function() { with(this) {
|
||||
it("changes the state to closed", function() { with(this) {
|
||||
assertEqual( "closed", driver().getState() )
|
||||
}})
|
||||
|
||||
it("does not write another close frame", function() { with(this) {
|
||||
expect(driver().io, "emit").exactly(0)
|
||||
this.driver().parse([0x88, 0x04, 0x03, 0xe9, 0x4f, 0x4b])
|
||||
}})
|
||||
}})
|
||||
}})
|
||||
|
||||
@@ -522,6 +604,17 @@ test.describe("Hybi", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("pong", function() { with(this) {
|
||||
it("does not write to the socket", function() { with(this) {
|
||||
expect(driver().io, "emit").exactly(0)
|
||||
driver().pong()
|
||||
}})
|
||||
|
||||
it("returns false", function() { with(this) {
|
||||
assertEqual( false, driver().pong() )
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("close", function() { with(this) {
|
||||
it("does not write to the socket", function() { with(this) {
|
||||
expect(driver().io, "emit").exactly(0)
|
||||
|
||||
Reference in New Issue
Block a user