[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var Buffer = require('safe-buffer').Buffer,
|
---|
| 4 | crypto = require('crypto'),
|
---|
| 5 | url = require('url'),
|
---|
| 6 | util = require('util'),
|
---|
| 7 | HttpParser = require('../http_parser'),
|
---|
| 8 | Base = require('./base'),
|
---|
| 9 | Hybi = require('./hybi'),
|
---|
| 10 | Proxy = require('./proxy');
|
---|
| 11 |
|
---|
| 12 | var Client = function(_url, options) {
|
---|
| 13 | this.version = 'hybi-' + Hybi.VERSION;
|
---|
| 14 | Hybi.call(this, null, _url, options);
|
---|
| 15 |
|
---|
| 16 | this.readyState = -1;
|
---|
| 17 | this._key = Client.generateKey();
|
---|
| 18 | this._accept = Hybi.generateAccept(this._key);
|
---|
| 19 | this._http = new HttpParser('response');
|
---|
| 20 |
|
---|
| 21 | var uri = url.parse(this.url),
|
---|
| 22 | auth = uri.auth && Buffer.from(uri.auth, 'utf8').toString('base64');
|
---|
| 23 |
|
---|
| 24 | if (this.VALID_PROTOCOLS.indexOf(uri.protocol) < 0)
|
---|
| 25 | throw new Error(this.url + ' is not a valid WebSocket URL');
|
---|
| 26 |
|
---|
| 27 | this._pathname = (uri.pathname || '/') + (uri.search || '');
|
---|
| 28 |
|
---|
| 29 | this._headers.set('Host', uri.host);
|
---|
| 30 | this._headers.set('Upgrade', 'websocket');
|
---|
| 31 | this._headers.set('Connection', 'Upgrade');
|
---|
| 32 | this._headers.set('Sec-WebSocket-Key', this._key);
|
---|
| 33 | this._headers.set('Sec-WebSocket-Version', Hybi.VERSION);
|
---|
| 34 |
|
---|
| 35 | if (this._protocols.length > 0)
|
---|
| 36 | this._headers.set('Sec-WebSocket-Protocol', this._protocols.join(', '));
|
---|
| 37 |
|
---|
| 38 | if (auth)
|
---|
| 39 | this._headers.set('Authorization', 'Basic ' + auth);
|
---|
| 40 | };
|
---|
| 41 | util.inherits(Client, Hybi);
|
---|
| 42 |
|
---|
| 43 | Client.generateKey = function() {
|
---|
| 44 | return crypto.randomBytes(16).toString('base64');
|
---|
| 45 | };
|
---|
| 46 |
|
---|
| 47 | var instance = {
|
---|
| 48 | VALID_PROTOCOLS: ['ws:', 'wss:'],
|
---|
| 49 |
|
---|
| 50 | proxy: function(origin, options) {
|
---|
| 51 | return new Proxy(this, origin, options);
|
---|
| 52 | },
|
---|
| 53 |
|
---|
| 54 | start: function() {
|
---|
| 55 | if (this.readyState !== -1) return false;
|
---|
| 56 | this._write(this._handshakeRequest());
|
---|
| 57 | this.readyState = 0;
|
---|
| 58 | return true;
|
---|
| 59 | },
|
---|
| 60 |
|
---|
| 61 | parse: function(chunk) {
|
---|
| 62 | if (this.readyState === 3) return;
|
---|
| 63 | if (this.readyState > 0) return Hybi.prototype.parse.call(this, chunk);
|
---|
| 64 |
|
---|
| 65 | this._http.parse(chunk);
|
---|
| 66 | if (!this._http.isComplete()) return;
|
---|
| 67 |
|
---|
| 68 | this._validateHandshake();
|
---|
| 69 | if (this.readyState === 3) return;
|
---|
| 70 |
|
---|
| 71 | this._open();
|
---|
| 72 | this.parse(this._http.body);
|
---|
| 73 | },
|
---|
| 74 |
|
---|
| 75 | _handshakeRequest: function() {
|
---|
| 76 | var extensions = this._extensions.generateOffer();
|
---|
| 77 | if (extensions)
|
---|
| 78 | this._headers.set('Sec-WebSocket-Extensions', extensions);
|
---|
| 79 |
|
---|
| 80 | var start = 'GET ' + this._pathname + ' HTTP/1.1',
|
---|
| 81 | headers = [start, this._headers.toString(), ''];
|
---|
| 82 |
|
---|
| 83 | return Buffer.from(headers.join('\r\n'), 'utf8');
|
---|
| 84 | },
|
---|
| 85 |
|
---|
| 86 | _failHandshake: function(message) {
|
---|
| 87 | message = 'Error during WebSocket handshake: ' + message;
|
---|
| 88 | this.readyState = 3;
|
---|
| 89 | this.emit('error', new Error(message));
|
---|
| 90 | this.emit('close', new Base.CloseEvent(this.ERRORS.protocol_error, message));
|
---|
| 91 | },
|
---|
| 92 |
|
---|
| 93 | _validateHandshake: function() {
|
---|
| 94 | this.statusCode = this._http.statusCode;
|
---|
| 95 | this.headers = this._http.headers;
|
---|
| 96 |
|
---|
| 97 | if (this._http.error)
|
---|
| 98 | return this._failHandshake(this._http.error.message);
|
---|
| 99 |
|
---|
| 100 | if (this._http.statusCode !== 101)
|
---|
| 101 | return this._failHandshake('Unexpected response code: ' + this._http.statusCode);
|
---|
| 102 |
|
---|
| 103 | var headers = this._http.headers,
|
---|
| 104 | upgrade = headers['upgrade'] || '',
|
---|
| 105 | connection = headers['connection'] || '',
|
---|
| 106 | accept = headers['sec-websocket-accept'] || '',
|
---|
| 107 | protocol = headers['sec-websocket-protocol'] || '';
|
---|
| 108 |
|
---|
| 109 | if (upgrade === '')
|
---|
| 110 | return this._failHandshake("'Upgrade' header is missing");
|
---|
| 111 | if (upgrade.toLowerCase() !== 'websocket')
|
---|
| 112 | return this._failHandshake("'Upgrade' header value is not 'WebSocket'");
|
---|
| 113 |
|
---|
| 114 | if (connection === '')
|
---|
| 115 | return this._failHandshake("'Connection' header is missing");
|
---|
| 116 | if (connection.toLowerCase() !== 'upgrade')
|
---|
| 117 | return this._failHandshake("'Connection' header value is not 'Upgrade'");
|
---|
| 118 |
|
---|
| 119 | if (accept !== this._accept)
|
---|
| 120 | return this._failHandshake('Sec-WebSocket-Accept mismatch');
|
---|
| 121 |
|
---|
| 122 | this.protocol = null;
|
---|
| 123 |
|
---|
| 124 | if (protocol !== '') {
|
---|
| 125 | if (this._protocols.indexOf(protocol) < 0)
|
---|
| 126 | return this._failHandshake('Sec-WebSocket-Protocol mismatch');
|
---|
| 127 | else
|
---|
| 128 | this.protocol = protocol;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | try {
|
---|
| 132 | this._extensions.activate(this.headers['sec-websocket-extensions']);
|
---|
| 133 | } catch (e) {
|
---|
| 134 | return this._failHandshake(e.message);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | for (var key in instance)
|
---|
| 140 | Client.prototype[key] = instance[key];
|
---|
| 141 |
|
---|
| 142 | module.exports = Client;
|
---|