source: trip-planner-front/node_modules/websocket-driver/lib/websocket/driver/draft76.js@ 6fe77af

Last change on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.1 KB
Line 
1'use strict';
2
3var Buffer = require('safe-buffer').Buffer,
4 Base = require('./base'),
5 Draft75 = require('./draft75'),
6 crypto = require('crypto'),
7 util = require('util');
8
9
10var numberFromKey = function(key) {
11 return parseInt((key.match(/[0-9]/g) || []).join(''), 10);
12};
13
14var spacesInKey = function(key) {
15 return (key.match(/ /g) || []).length;
16};
17
18
19var Draft76 = function(request, url, options) {
20 Draft75.apply(this, arguments);
21 this._stage = -1;
22 this._body = [];
23 this.version = 'hixie-76';
24
25 this._headers.clear();
26
27 this._headers.set('Upgrade', 'WebSocket');
28 this._headers.set('Connection', 'Upgrade');
29 this._headers.set('Sec-WebSocket-Origin', this._request.headers.origin);
30 this._headers.set('Sec-WebSocket-Location', this.url);
31};
32util.inherits(Draft76, Draft75);
33
34var instance = {
35 BODY_SIZE: 8,
36
37 start: function() {
38 if (!Draft75.prototype.start.call(this)) return false;
39 this._started = true;
40 this._sendHandshakeBody();
41 return true;
42 },
43
44 close: function() {
45 if (this.readyState === 3) return false;
46 if (this.readyState === 1) this._write(Buffer.from([0xFF, 0x00]));
47 this.readyState = 3;
48 this.emit('close', new Base.CloseEvent(null, null));
49 return true;
50 },
51
52 _handshakeResponse: function() {
53 var headers = this._request.headers,
54 key1 = headers['sec-websocket-key1'],
55 key2 = headers['sec-websocket-key2'];
56
57 if (!key1) throw new Error('Missing required header: Sec-WebSocket-Key1');
58 if (!key2) throw new Error('Missing required header: Sec-WebSocket-Key2');
59
60 var number1 = numberFromKey(key1),
61 spaces1 = spacesInKey(key1),
62
63 number2 = numberFromKey(key2),
64 spaces2 = spacesInKey(key2);
65
66 if (number1 % spaces1 !== 0 || number2 % spaces2 !== 0)
67 throw new Error('Client sent invalid Sec-WebSocket-Key headers');
68
69 this._keyValues = [number1 / spaces1, number2 / spaces2];
70
71 var start = 'HTTP/1.1 101 WebSocket Protocol Handshake',
72 headers = [start, this._headers.toString(), ''];
73
74 return Buffer.from(headers.join('\r\n'), 'binary');
75 },
76
77 _handshakeSignature: function() {
78 if (this._body.length < this.BODY_SIZE) return null;
79
80 var md5 = crypto.createHash('md5'),
81 buffer = Buffer.allocUnsafe(8 + this.BODY_SIZE);
82
83 buffer.writeUInt32BE(this._keyValues[0], 0);
84 buffer.writeUInt32BE(this._keyValues[1], 4);
85 Buffer.from(this._body).copy(buffer, 8, 0, this.BODY_SIZE);
86
87 md5.update(buffer);
88 return Buffer.from(md5.digest('binary'), 'binary');
89 },
90
91 _sendHandshakeBody: function() {
92 if (!this._started) return;
93 var signature = this._handshakeSignature();
94 if (!signature) return;
95
96 this._write(signature);
97 this._stage = 0;
98 this._open();
99
100 if (this._body.length > this.BODY_SIZE)
101 this.parse(this._body.slice(this.BODY_SIZE));
102 },
103
104 _parseLeadingByte: function(octet) {
105 if (octet !== 0xFF)
106 return Draft75.prototype._parseLeadingByte.call(this, octet);
107
108 this._closing = true;
109 this._length = 0;
110 this._stage = 1;
111 }
112};
113
114for (var key in instance)
115 Draft76.prototype[key] = instance[key];
116
117module.exports = Draft76;
Note: See TracBrowser for help on using the repository browser.