Compare commits
31 Commits
stream-parser
...
0.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 22d483a5ee | |||
| 7325241d11 | |||
| eb2d329a16 | |||
| 7e34e7fd40 | |||
| 623a93c0b7 | |||
| 208be33754 | |||
| 0bb47c17b4 | |||
| d6da566641 | |||
| cde48d625c | |||
| 6eb04d5acc | |||
| 4348bec390 | |||
| 550b3d906d | |||
| 473fcaafdb | |||
| b88b8d6856 | |||
| b905c94f89 | |||
| 7b47563cd1 | |||
| 07031911e3 | |||
| d2156ac10d | |||
| d0033b8208 | |||
| 76f4128634 | |||
| 32e6715b6e | |||
| 042e33aebf | |||
| bdc9bfe7e8 | |||
| 1c33c0ce56 | |||
| f693b02743 | |||
| 91f19cb69b | |||
| dcd7c278b6 | |||
| dbedb370b7 | |||
| 28858d9fb3 | |||
| ae52cf995e | |||
| dfe7b2b000 |
@@ -8,4 +8,3 @@ node_js:
|
||||
|
||||
before_install:
|
||||
- '[ "${TRAVIS_NODE_VERSION}" = "0.6" ] && npm conf set strict-ssl false || true'
|
||||
|
||||
|
||||
+13
-1
@@ -1,3 +1,16 @@
|
||||
### 0.4.0 / 2014-11-08
|
||||
|
||||
* Support connection via HTTP proxies using `CONNECT`
|
||||
|
||||
### 0.3.6 / 2014-10-04
|
||||
|
||||
* It is now possible to call `close()` before `start()` and close the driver
|
||||
|
||||
### 0.3.5 / 2014-07-06
|
||||
|
||||
* Don't hold references to frame buffers after a message has been emitted
|
||||
* Make sure that `protocol` and `version` are exposed properly by the TCP driver
|
||||
|
||||
### 0.3.4 / 2014-05-08
|
||||
|
||||
* Don't hold memory-leaking references to I/O buffers after they have been parsed
|
||||
@@ -39,4 +52,3 @@
|
||||
### 0.1.0 / 2013-05-04
|
||||
|
||||
* First stable release
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# websocket-driver [](https://travis-ci.org/faye/websocket-driver-node)
|
||||
# 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
|
||||
@@ -134,17 +134,17 @@ var net = require('net'),
|
||||
websocket = require('websocket-driver');
|
||||
|
||||
var driver = websocket.client('ws://www.example.com/socket'),
|
||||
tcp = net.createConnection(80, 'www.example.com');
|
||||
tcp = net.connect(80, 'www.example.com');
|
||||
|
||||
tcp.pipe(driver.io).pipe(tcp);
|
||||
|
||||
driver.messages.on('data', function(message) {
|
||||
console.log('Got a message', message);
|
||||
});
|
||||
|
||||
tcp.on('connect', function() {
|
||||
driver.start();
|
||||
});
|
||||
|
||||
driver.messages.on('data', function(message) {
|
||||
console.log('Got a message', message);
|
||||
});
|
||||
```
|
||||
|
||||
Client drivers have two additional properties for reading the HTTP data that
|
||||
@@ -154,6 +154,65 @@ was sent back by the server:
|
||||
* `driver.headers` - an object containing the response headers
|
||||
|
||||
|
||||
### HTTP Proxies
|
||||
|
||||
The client driver supports connections via HTTP proxies using the `CONNECT`
|
||||
method. Instead of sending the WebSocket handshake immediately, it will send a
|
||||
`CONNECT` request, wait for a `200` response, and then proceed as normal.
|
||||
|
||||
To use this feature, call `driver.proxy(url)` where `url` is the origin of the
|
||||
proxy, including a username and password if required. This produces a duplex
|
||||
stream that you should pipe in and out of your TCP connection to the proxy
|
||||
server. When the proxy emits `connect`, you can then pipe `driver.io` to your
|
||||
TCP stream and call `driver.start()`.
|
||||
|
||||
```js
|
||||
var net = require('net'),
|
||||
websocket = require('websocket-driver');
|
||||
|
||||
var driver = websocket.client('ws://www.example.com/socket'),
|
||||
proxy = driver.proxy('http://username:password@proxy.example.com'),
|
||||
tcp = net.connect(80, 'proxy.example.com');
|
||||
|
||||
tcp.pipe(proxy).pipe(tcp, {end: false});
|
||||
|
||||
tcp.on('connect', function() {
|
||||
proxy.start();
|
||||
});
|
||||
|
||||
proxy.on('connect', function() {
|
||||
driver.io.pipe(tcp).pipe(driver.io);
|
||||
driver.start();
|
||||
});
|
||||
|
||||
driver.messages.on('data', function(message) {
|
||||
console.log('Got a message', message);
|
||||
});
|
||||
```
|
||||
|
||||
The proxy's `connect` event is also where you should perform a TLS handshake on
|
||||
your TCP stream, if you are connecting to a `wss:` endpoint.
|
||||
|
||||
In the event that proxy connection fails, `proxy` will emit an `error`. You can
|
||||
inspect the proxy's response via `proxy.statusCode` and `proxy.headers`.
|
||||
|
||||
```js
|
||||
proxy.on('error', function(error) {
|
||||
console.error(error.message);
|
||||
console.log(proxy.statusCode);
|
||||
console.log(proxy.headers);
|
||||
});
|
||||
```
|
||||
|
||||
Before calling `proxy.start()` you can set custom headers using
|
||||
`proxy.setHeader()`:
|
||||
|
||||
```js
|
||||
proxy.setHeader('User-Agent', 'node');
|
||||
proxy.start();
|
||||
```
|
||||
|
||||
|
||||
### Driver API
|
||||
|
||||
Drivers are created using one of the following methods:
|
||||
@@ -304,4 +363,3 @@ 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.
|
||||
|
||||
|
||||
@@ -18,4 +18,3 @@ var server = net.createServer(function(connection) {
|
||||
});
|
||||
|
||||
server.listen(process.argv[2]);
|
||||
|
||||
|
||||
@@ -41,4 +41,3 @@ var Driver = {
|
||||
};
|
||||
|
||||
module.exports = Driver;
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ var Base = function(request, url, options) {
|
||||
this._request = request;
|
||||
this._options = options || {};
|
||||
this._maxLength = this._options.maxLength || this.MAX_LENGTH;
|
||||
this.__headers = new Headers();
|
||||
this._headers = new Headers();
|
||||
this.__queue = [];
|
||||
this.readyState = 0;
|
||||
this.url = url;
|
||||
@@ -57,7 +57,7 @@ var instance = {
|
||||
|
||||
setHeader: function(name, value) {
|
||||
if (this.readyState > 0) return false;
|
||||
this.__headers.set(name, value);
|
||||
this._headers.set(name, value);
|
||||
return true;
|
||||
},
|
||||
|
||||
@@ -123,4 +123,3 @@ Base.MessageEvent = function(data) {
|
||||
};
|
||||
|
||||
module.exports = Base;
|
||||
|
||||
|
||||
@@ -1,17 +1,35 @@
|
||||
var url = require('url'),
|
||||
util = require('util'),
|
||||
HttpParser = require('./http_parser'),
|
||||
HttpParser = require('../http_parser'),
|
||||
Base = require('./base'),
|
||||
Hybi = require('./hybi');
|
||||
Hybi = require('./hybi'),
|
||||
Proxy = require('./proxy');
|
||||
|
||||
var Client = function(url, options) {
|
||||
var Client = function(_url, options) {
|
||||
this.version = 'hybi-13';
|
||||
Hybi.call(this, null, url, options);
|
||||
Hybi.call(this, null, _url, options);
|
||||
|
||||
this.readyState = -1;
|
||||
this._key = Client.generateKey();
|
||||
this._accept = Hybi.generateAccept(this._key);
|
||||
this._http = new HttpParser('response');
|
||||
|
||||
var uri = url.parse(this.url),
|
||||
auth = uri.auth && new Buffer(uri.auth, 'utf8').toString('base64');
|
||||
|
||||
this._pathname = (uri.pathname || '/') + (uri.search || '');
|
||||
|
||||
this._headers.set('Host', uri.host);
|
||||
this._headers.set('Upgrade', 'websocket');
|
||||
this._headers.set('Connection', 'Upgrade');
|
||||
this._headers.set('Sec-WebSocket-Key', this._key);
|
||||
this._headers.set('Sec-WebSocket-Version', '13');
|
||||
|
||||
if (this._protocols.length > 0)
|
||||
this._headers.set('Sec-WebSocket-Protocol', this._protocols.join(', '));
|
||||
|
||||
if (auth)
|
||||
this._headers.set('Authorization', 'Basic ' + auth);
|
||||
};
|
||||
util.inherits(Client, Hybi);
|
||||
|
||||
@@ -22,6 +40,10 @@ Client.generateKey = function() {
|
||||
};
|
||||
|
||||
var instance = {
|
||||
proxy: function(origin, options) {
|
||||
return new Proxy(this, origin, options);
|
||||
},
|
||||
|
||||
start: function() {
|
||||
if (this.readyState !== -1) return false;
|
||||
this._write(this._handshakeRequest());
|
||||
@@ -34,29 +56,16 @@ var instance = {
|
||||
|
||||
this._http.parse(data);
|
||||
if (!this._http.isComplete()) return;
|
||||
|
||||
|
||||
this._validateHandshake();
|
||||
this.parse(this._http.body);
|
||||
},
|
||||
|
||||
_handshakeRequest: function() {
|
||||
var uri = url.parse(this.url);
|
||||
var start = 'GET ' + this._pathname + ' HTTP/1.1',
|
||||
headers = [start, this._headers.toString(), ''];
|
||||
|
||||
var headers = [ 'GET ' + (uri.pathname || '/') + (uri.search || '') + ' HTTP/1.1',
|
||||
'Host: ' + uri.hostname + (uri.port ? ':' + uri.port : ''),
|
||||
'Upgrade: websocket',
|
||||
'Connection: Upgrade',
|
||||
'Sec-WebSocket-Key: ' + this._key,
|
||||
'Sec-WebSocket-Version: 13'
|
||||
];
|
||||
|
||||
if (this._protocols.length > 0)
|
||||
headers.push('Sec-WebSocket-Protocol: ' + this._protocols.join(', '));
|
||||
|
||||
if (uri.auth)
|
||||
headers.push('Authorization: Basic ' + new Buffer(uri.auth, 'utf8').toString('base64'));
|
||||
|
||||
return new Buffer(headers.concat(this.__headers.toString(), '').join('\r\n'), 'utf8');
|
||||
return new Buffer(headers.join('\r\n'), 'utf8');
|
||||
},
|
||||
|
||||
_failHandshake: function(message) {
|
||||
@@ -109,4 +118,3 @@ for (var key in instance)
|
||||
Client.prototype[key] = instance[key];
|
||||
|
||||
module.exports = Client;
|
||||
|
||||
|
||||
@@ -5,6 +5,11 @@ var Draft75 = function(request, url, options) {
|
||||
Base.apply(this, arguments);
|
||||
this._stage = 0;
|
||||
this.version = 'hixie-75';
|
||||
|
||||
this._headers.set('Upgrade', 'WebSocket');
|
||||
this._headers.set('Connection', 'Upgrade');
|
||||
this._headers.set('WebSocket-Origin', this._request.headers.origin);
|
||||
this._headers.set('WebSocket-Location', this.url);
|
||||
};
|
||||
util.inherits(Draft75, Base);
|
||||
|
||||
@@ -88,14 +93,10 @@ var instance = {
|
||||
},
|
||||
|
||||
_handshakeResponse: function() {
|
||||
return new Buffer('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
|
||||
'Upgrade: WebSocket\r\n' +
|
||||
'Connection: Upgrade\r\n' +
|
||||
'WebSocket-Origin: ' + this._request.headers.origin + '\r\n' +
|
||||
'WebSocket-Location: ' + this.url + '\r\n' +
|
||||
this.__headers.toString() +
|
||||
'\r\n',
|
||||
'utf8');
|
||||
var start = 'HTTP/1.1 101 Web Socket Protocol Handshake',
|
||||
headers = [start, this._headers.toString(), ''];
|
||||
|
||||
return new Buffer(headers.join('\r\n'), 'utf8');
|
||||
},
|
||||
|
||||
_parseLeadingByte: function(data) {
|
||||
@@ -115,4 +116,3 @@ for (var key in instance)
|
||||
Draft75.prototype[key] = instance[key];
|
||||
|
||||
module.exports = Draft75;
|
||||
|
||||
|
||||
@@ -26,6 +26,13 @@ var Draft76 = function(request, url, options) {
|
||||
this._stage = -1;
|
||||
this._body = [];
|
||||
this.version = 'hixie-76';
|
||||
|
||||
this._headers.clear();
|
||||
|
||||
this._headers.set('Upgrade', 'WebSocket');
|
||||
this._headers.set('Connection', 'Upgrade');
|
||||
this._headers.set('Sec-WebSocket-Origin', this._request.headers.origin);
|
||||
this._headers.set('Sec-WebSocket-Location', this.url);
|
||||
};
|
||||
util.inherits(Draft76, Draft75);
|
||||
|
||||
@@ -48,14 +55,10 @@ var instance = {
|
||||
},
|
||||
|
||||
_handshakeResponse: function() {
|
||||
return new Buffer('HTTP/1.1 101 WebSocket Protocol Handshake\r\n' +
|
||||
'Upgrade: WebSocket\r\n' +
|
||||
'Connection: Upgrade\r\n' +
|
||||
'Sec-WebSocket-Origin: ' + this._request.headers.origin + '\r\n' +
|
||||
'Sec-WebSocket-Location: ' + this.url + '\r\n' +
|
||||
this.__headers.toString() +
|
||||
'\r\n',
|
||||
'binary');
|
||||
var start = 'HTTP/1.1 101 WebSocket Protocol Handshake',
|
||||
headers = [start, this._headers.toString(), ''];
|
||||
|
||||
return new Buffer(headers.join('\r\n'), 'binary');
|
||||
},
|
||||
|
||||
_handshakeSignature: function() {
|
||||
@@ -106,4 +109,3 @@ for (var key in instance)
|
||||
Draft76.prototype[key] = instance[key];
|
||||
|
||||
module.exports = Draft76;
|
||||
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
var Headers = function() {
|
||||
this.clear();
|
||||
};
|
||||
|
||||
Headers.prototype.ALLOWED_DUPLICATES = ['set-cookie', 'set-cookie2', 'warning', 'www-authenticate'];
|
||||
|
||||
Headers.prototype.clear = function() {
|
||||
this._sent = {};
|
||||
this._lines = [];
|
||||
};
|
||||
|
||||
Headers.prototype.ALLOWED_DUPLICATES = ['set-cookie', 'set-cookie2', 'warning', 'www-authenticate']
|
||||
|
||||
Headers.prototype.set = function(name, value) {
|
||||
if (value === undefined) return;
|
||||
|
||||
@@ -12,7 +16,7 @@ Headers.prototype.set = function(name, value) {
|
||||
value = this._strip(value);
|
||||
|
||||
var key = name.toLowerCase();
|
||||
if (!this._sent.hasOwnProperty(key) || this.ALLOWED_DUPLICATES.indexOf(key) < 0) {
|
||||
if (!this._sent.hasOwnProperty(key) || this.ALLOWED_DUPLICATES.indexOf(key) >= 0) {
|
||||
this._sent[key] = true;
|
||||
this._lines.push(name + ': ' + value + '\r\n');
|
||||
}
|
||||
@@ -27,4 +31,3 @@ Headers.prototype._strip = function(string) {
|
||||
};
|
||||
|
||||
module.exports = Headers;
|
||||
|
||||
|
||||
@@ -7,26 +7,39 @@ var Hybi = function(request, url, options) {
|
||||
Base.apply(this, arguments);
|
||||
this._reset();
|
||||
|
||||
this._reader = new Reader();
|
||||
this._stage = 0;
|
||||
this._masking = this._options.masking;
|
||||
this._protocols = this._options.protocols || [];
|
||||
this._reader = new Reader();
|
||||
this._stage = 0;
|
||||
this._masking = this._options.masking;
|
||||
this._protocols = this._options.protocols || [];
|
||||
this._requireMasking = this._options.requireMasking;
|
||||
this._pingCallbacks = {};
|
||||
|
||||
if (typeof this._protocols === 'string')
|
||||
this._protocols = this._protocols.split(/\s*,\s*/);
|
||||
|
||||
this._requireMasking = this._options.requireMasking;
|
||||
this._pingCallbacks = {};
|
||||
if (!this._request) return;
|
||||
|
||||
if (!this.version) {
|
||||
var version = this._request.headers['sec-websocket-version'];
|
||||
this.version = 'hybi-' + version;
|
||||
var secKey = this._request.headers['sec-websocket-key'],
|
||||
protos = this._request.headers['sec-websocket-protocol'],
|
||||
version = this._request.headers['sec-websocket-version'],
|
||||
supported = this._protocols;
|
||||
|
||||
this._headers.set('Upgrade', 'websocket');
|
||||
this._headers.set('Connection', 'Upgrade');
|
||||
this._headers.set('Sec-WebSocket-Accept', Hybi.generateAccept(secKey));
|
||||
|
||||
if (protos !== undefined) {
|
||||
if (typeof protos === 'string') protos = protos.split(/\s*,\s*/);
|
||||
this.protocol = protos.filter(function(p) { return supported.indexOf(p) >= 0 })[0];
|
||||
if (this.protocol) this._headers.set('Sec-WebSocket-Protocol', this.protocol);
|
||||
}
|
||||
|
||||
this.version = 'hybi-' + version;
|
||||
};
|
||||
util.inherits(Hybi, Base);
|
||||
|
||||
Hybi.mask = function(payload, mask, offset) {
|
||||
if (mask.length === 0) return payload;
|
||||
if (!mask || mask.length === 0) return payload;
|
||||
offset = offset || 0;
|
||||
|
||||
for (var i = 0, n = payload.length - offset; i < n; i++) {
|
||||
@@ -118,8 +131,7 @@ var instance = {
|
||||
case 4:
|
||||
buffer = this._reader.read(this._length);
|
||||
if (buffer) {
|
||||
this._payload = buffer;
|
||||
this._emitFrame();
|
||||
this._emitFrame(buffer);
|
||||
this._stage = 0;
|
||||
}
|
||||
break;
|
||||
@@ -203,7 +215,7 @@ var instance = {
|
||||
reason = reason || '';
|
||||
code = code || this.ERRORS.normal_closure;
|
||||
|
||||
if (this.readyState === 0) {
|
||||
if (this.readyState <= 0) {
|
||||
this.readyState = 3;
|
||||
this.emit('close', new Base.CloseEvent(code, reason));
|
||||
return true;
|
||||
@@ -217,31 +229,10 @@ var instance = {
|
||||
},
|
||||
|
||||
_handshakeResponse: function() {
|
||||
var secKey = this._request.headers['sec-websocket-key'];
|
||||
if (!secKey) return '';
|
||||
var start = 'HTTP/1.1 101 Switching Protocols',
|
||||
headers = [start, this._headers.toString(), ''];
|
||||
|
||||
var accept = Hybi.generateAccept(secKey),
|
||||
protos = this._request.headers['sec-websocket-protocol'],
|
||||
supported = this._protocols,
|
||||
proto,
|
||||
|
||||
headers = [
|
||||
'HTTP/1.1 101 Switching Protocols',
|
||||
'Upgrade: websocket',
|
||||
'Connection: Upgrade',
|
||||
'Sec-WebSocket-Accept: ' + accept
|
||||
];
|
||||
|
||||
if (protos !== undefined) {
|
||||
if (typeof protos === 'string') protos = protos.split(/\s*,\s*/);
|
||||
proto = protos.filter(function(p) { return supported.indexOf(p) >= 0 })[0];
|
||||
if (proto) {
|
||||
this.protocol = proto;
|
||||
headers.push('Sec-WebSocket-Protocol: ' + proto);
|
||||
}
|
||||
}
|
||||
|
||||
return new Buffer(headers.concat(this.__headers.toString(), '').join('\r\n'), 'utf8');
|
||||
return new Buffer(headers.join('\r\n'), 'utf8');
|
||||
},
|
||||
|
||||
_shutdown: function(code, reason) {
|
||||
@@ -269,8 +260,6 @@ var instance = {
|
||||
|
||||
this._final = (data & this.FIN) === this.FIN;
|
||||
this._opcode = (data & this.OPCODE);
|
||||
this._mask = [];
|
||||
this._payload = [];
|
||||
|
||||
if (this.OPCODE_CODES.indexOf(this._opcode) < 0)
|
||||
return this._fail('protocol_error', 'Unrecognized frame opcode: ' + this._opcode);
|
||||
@@ -320,14 +309,17 @@ var instance = {
|
||||
}
|
||||
},
|
||||
|
||||
_emitFrame: function() {
|
||||
var payload = Hybi.mask(this._payload, this._mask),
|
||||
_emitFrame: function(buffer) {
|
||||
var payload = Hybi.mask(buffer, this._mask),
|
||||
isFinal = this._final,
|
||||
opcode = this._opcode;
|
||||
|
||||
this._final = this._opcode = this._length = this._lengthSize = this._masked = this._mask = null;
|
||||
|
||||
if (opcode === this.OPCODES.continuation) {
|
||||
if (!this._mode) return this._fail('protocol_error', 'Received unexpected continuation frame');
|
||||
this._buffer(payload);
|
||||
if (this._final) {
|
||||
if (isFinal) {
|
||||
var message = this._concatBuffer();
|
||||
if (this._mode === 'text') message = this._encode(message);
|
||||
this._reset();
|
||||
@@ -338,7 +330,7 @@ var instance = {
|
||||
}
|
||||
}
|
||||
else if (opcode === this.OPCODES.text) {
|
||||
if (this._final) {
|
||||
if (isFinal) {
|
||||
var message = this._encode(payload);
|
||||
if (message === null)
|
||||
this._fail('encoding_error', 'Could not decode a text frame as UTF-8');
|
||||
@@ -350,7 +342,7 @@ var instance = {
|
||||
}
|
||||
}
|
||||
else if (opcode === this.OPCODES.binary) {
|
||||
if (this._final) {
|
||||
if (isFinal) {
|
||||
this.emit('message', new Base.MessageEvent(payload));
|
||||
} else {
|
||||
this._mode = 'binary';
|
||||
@@ -426,4 +418,3 @@ for (var key in instance)
|
||||
Hybi.prototype[key] = instance[key];
|
||||
|
||||
module.exports = Hybi;
|
||||
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
var Stream = require('stream').Stream,
|
||||
url = require('url'),
|
||||
util = require('util'),
|
||||
Headers = require('./headers'),
|
||||
HttpParser = require('../http_parser');
|
||||
|
||||
var PORTS = {'ws:': 80, 'wss:': 443};
|
||||
|
||||
var Proxy = function(client, origin, options) {
|
||||
this._client = client;
|
||||
this._http = new HttpParser('response');
|
||||
this._origin = (typeof client.url === 'object') ? client.url : url.parse(client.url);
|
||||
this._url = (typeof origin === 'object') ? origin : url.parse(origin);
|
||||
this._options = options || {};
|
||||
this._state = 0;
|
||||
|
||||
this.readable = this.writable = true;
|
||||
this._paused = false;
|
||||
|
||||
this._headers = new Headers();
|
||||
this._headers.set('Host', this._origin.host);
|
||||
this._headers.set('Connection', 'keep-alive');
|
||||
this._headers.set('Proxy-Connection', 'keep-alive');
|
||||
|
||||
var auth = this._url.auth && new Buffer(this._url.auth, 'utf8').toString('base64');
|
||||
if (auth) this._headers.set('Proxy-Authorization', 'Basic ' + auth);
|
||||
};
|
||||
util.inherits(Proxy, Stream);
|
||||
|
||||
var instance = {
|
||||
setHeader: function(name, value) {
|
||||
if (this._state !== 0) return false;
|
||||
this._headers.set(name, value);
|
||||
return true;
|
||||
},
|
||||
|
||||
start: function() {
|
||||
if (this._state !== 0) return false;
|
||||
this._state = 1;
|
||||
|
||||
var origin = this._origin,
|
||||
port = origin.port || PORTS[origin.protocol],
|
||||
start = 'CONNECT ' + origin.hostname + ':' + port + ' HTTP/1.1';
|
||||
|
||||
var headers = [start, this._headers.toString(), ''];
|
||||
|
||||
this.emit('data', new Buffer(headers.join('\r\n'), 'utf8'));
|
||||
return true;
|
||||
},
|
||||
|
||||
pause: function() {
|
||||
this._paused = true;
|
||||
},
|
||||
|
||||
resume: function() {
|
||||
this._paused = false;
|
||||
this.emit('drain');
|
||||
},
|
||||
|
||||
write: function(chunk) {
|
||||
if (!this.writable) return false;
|
||||
|
||||
this._http.parse(chunk);
|
||||
if (!this._http.isComplete()) return !this._paused;
|
||||
|
||||
this.statusCode = this._http.statusCode;
|
||||
this.headers = this._http.headers;
|
||||
|
||||
if (this.statusCode === 200) {
|
||||
this.emit('connect');
|
||||
} else {
|
||||
var message = "Can't establish a connection to the server at " + this._origin.href;
|
||||
this.emit('error', new Error(message));
|
||||
}
|
||||
this.end();
|
||||
return !this._paused;
|
||||
},
|
||||
|
||||
end: function(chunk) {
|
||||
if (!this.writable) return;
|
||||
if (chunk !== undefined) this.write(chunk);
|
||||
this.readable = this.writable = false;
|
||||
this.emit('close');
|
||||
this.emit('end');
|
||||
},
|
||||
|
||||
destroy: function() {
|
||||
this.end();
|
||||
}
|
||||
};
|
||||
|
||||
for (var key in instance)
|
||||
Proxy.prototype[key] = instance[key];
|
||||
|
||||
module.exports = Proxy;
|
||||
@@ -1,5 +1,5 @@
|
||||
var util = require('util'),
|
||||
HttpParser = require('./http_parser'),
|
||||
HttpParser = require('../http_parser'),
|
||||
Base = require('./base'),
|
||||
Draft75 = require('./draft75'),
|
||||
Draft76 = require('./draft76'),
|
||||
@@ -40,6 +40,9 @@ var instance = {
|
||||
this._delegate.on(event, function(e) { self.emit(event, e) });
|
||||
}, this);
|
||||
|
||||
this.protocol = this._delegate.protocol;
|
||||
this.version = this._delegate.version;
|
||||
|
||||
this.parse(this._http.body);
|
||||
this.emit('connect', new Base.ConnectEvent());
|
||||
},
|
||||
@@ -101,4 +104,3 @@ Server.http = function(request, options) {
|
||||
};
|
||||
|
||||
module.exports = Server;
|
||||
|
||||
|
||||
@@ -26,15 +26,13 @@ var HttpParser = function(type) {
|
||||
self.method = (typeof info.method === 'number') ? HttpParser.METHODS[info.method] : info.method;
|
||||
self.statusCode = info.statusCode;
|
||||
self.url = info.url;
|
||||
|
||||
|
||||
var headers = info.headers;
|
||||
if (!headers) return;
|
||||
|
||||
for (var i = 0, n = headers.length; i < n; i += 2)
|
||||
self.headers[headers[i].toLowerCase()] = headers[i+1];
|
||||
};
|
||||
|
||||
this._parser.onMessageComplete = this._parser[HTTPParser.kOnMessageComplete] = function() {
|
||||
self._complete = true;
|
||||
};
|
||||
};
|
||||
@@ -78,4 +76,3 @@ HttpParser.prototype.parse = function(data) {
|
||||
};
|
||||
|
||||
module.exports = HttpParser;
|
||||
|
||||
@@ -141,4 +141,3 @@ Messages.prototype.destroy = function() {};
|
||||
|
||||
exports.IO = IO;
|
||||
exports.Messages = Messages;
|
||||
|
||||
|
||||
+1
-2
@@ -5,7 +5,7 @@
|
||||
, "keywords" : ["websocket"]
|
||||
, "license" : "MIT"
|
||||
|
||||
, "version" : "0.3.4"
|
||||
, "version" : "0.4.0"
|
||||
, "engines" : {"node": ">=0.4.0"}
|
||||
, "main" : "./lib/websocket/driver"
|
||||
, "devDependencies" : {"jstest": ""}
|
||||
@@ -18,4 +18,3 @@
|
||||
|
||||
, "bugs" : "http://github.com/faye/websocket-driver-node/issues"
|
||||
}
|
||||
|
||||
|
||||
@@ -42,4 +42,3 @@ require('./websocket/driver/draft75_spec')
|
||||
require('./websocket/driver/draft76_spec')
|
||||
require('./websocket/driver/hybi_spec')
|
||||
require('./websocket/driver/client_spec')
|
||||
|
||||
|
||||
@@ -50,6 +50,14 @@ test.describe("Client", function() { with(this) {
|
||||
assertEqual( null, driver().getState() )
|
||||
}})
|
||||
|
||||
describe("close", function() { with(this) {
|
||||
it("changes the state to closed", function() { with(this) {
|
||||
driver().close()
|
||||
assertEqual( "closed", driver().getState() )
|
||||
assertEqual( [1000, ''], close )
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("start", function() { with(this) {
|
||||
it("writes the handshake request to the socket", function() { with(this) {
|
||||
expect(driver().io, "emit").given("data", buffer(
|
||||
@@ -127,6 +135,70 @@ test.describe("Client", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("using a proxy", function() { with(this) {
|
||||
it("sends a CONNECT request", function() { with(this) {
|
||||
var proxy = driver().proxy("http://proxy.example.com")
|
||||
expect(proxy, "emit").given("data", buffer(
|
||||
"CONNECT www.example.com:80 HTTP/1.1\r\n" +
|
||||
"Host: www.example.com\r\n" +
|
||||
"Connection: keep-alive\r\n" +
|
||||
"Proxy-Connection: keep-alive\r\n" +
|
||||
"\r\n"))
|
||||
proxy.start()
|
||||
}})
|
||||
|
||||
it("sends an authenticated CONNECT request", function() { with(this) {
|
||||
var proxy = driver().proxy("http://user:pass@proxy.example.com")
|
||||
expect(proxy, "emit").given("data", buffer(
|
||||
"CONNECT www.example.com:80 HTTP/1.1\r\n" +
|
||||
"Host: www.example.com\r\n" +
|
||||
"Connection: keep-alive\r\n" +
|
||||
"Proxy-Connection: keep-alive\r\n" +
|
||||
"Proxy-Authorization: Basic dXNlcjpwYXNz\r\n" +
|
||||
"\r\n"))
|
||||
proxy.start()
|
||||
}})
|
||||
|
||||
it("sends a CONNECT request with custom headers", function() { with(this) {
|
||||
var proxy = driver().proxy("http://user:pass@proxy.example.com")
|
||||
proxy.setHeader("User-Agent", "Chrome")
|
||||
expect(proxy, "emit").given("data", buffer(
|
||||
"CONNECT www.example.com:80 HTTP/1.1\r\n" +
|
||||
"Host: www.example.com\r\n" +
|
||||
"Connection: keep-alive\r\n" +
|
||||
"Proxy-Connection: keep-alive\r\n" +
|
||||
"Proxy-Authorization: Basic dXNlcjpwYXNz\r\n" +
|
||||
"User-Agent: Chrome\r\n" +
|
||||
"\r\n"))
|
||||
proxy.start()
|
||||
}})
|
||||
|
||||
describe("receiving a response", function() { with(this) {
|
||||
before(function() { with(this) {
|
||||
this.proxy = driver().proxy("http://proxy.example.com")
|
||||
}})
|
||||
|
||||
it("returns true when the response is written", function() { with(this) {
|
||||
// this prevents downstream connections suddenly closing for no reason
|
||||
assertEqual( true, proxy.write(new Buffer("HTTP/1.1 200 OK\r\n\r\n")) )
|
||||
}})
|
||||
|
||||
it("emits a 'connect' event when the proxy connects", function() { with(this) {
|
||||
expect(proxy, "emit").given("connect")
|
||||
expect(proxy, "emit").given("close")
|
||||
expect(proxy, "emit").given("end")
|
||||
proxy.write(new Buffer("HTTP/1.1 200 OK\r\n\r\n"))
|
||||
}})
|
||||
|
||||
it("emits an 'error' event if the proxy does not connect", function() { with(this) {
|
||||
expect(proxy, "emit").given("error", objectIncluding({message: "Can't establish a connection to the server at ws://www.example.com/socket"}))
|
||||
expect(proxy, "emit").given("close")
|
||||
expect(proxy, "emit").given("end")
|
||||
proxy.write(new Buffer("HTTP/1.1 403 Forbidden\r\n\r\n"))
|
||||
}})
|
||||
}})
|
||||
}})
|
||||
|
||||
describe("in the connecting state", function() { with(this) {
|
||||
before(function() { this.driver().start() })
|
||||
|
||||
@@ -249,4 +321,3 @@ test.describe("Client", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
}})
|
||||
|
||||
|
||||
@@ -112,4 +112,3 @@ test.describe("draft-75", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
}})
|
||||
|
||||
|
||||
@@ -97,4 +97,3 @@ test.describe("Draft75", function() { with(this) {
|
||||
|
||||
itShouldBehaveLike("draft-75 protocol")
|
||||
}})
|
||||
|
||||
|
||||
@@ -184,4 +184,3 @@ test.describe("Draft76", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
}})
|
||||
|
||||
|
||||
@@ -539,4 +539,3 @@ test.describe("Hybi", function() { with(this) {
|
||||
}})
|
||||
}})
|
||||
}})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user