source: trip-planner-front/node_modules/ws/lib/websocket-server.js@ eed0bf8

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

initial commit

  • Property mode set to 100644
File size: 11.5 KB
Line 
1'use strict';
2
3const EventEmitter = require('events');
4const { createHash } = require('crypto');
5const { createServer, STATUS_CODES } = require('http');
6
7const PerMessageDeflate = require('./permessage-deflate');
8const WebSocket = require('./websocket');
9const { format, parse } = require('./extension');
10const { GUID, kWebSocket } = require('./constants');
11
12const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
13
14/**
15 * Class representing a WebSocket server.
16 *
17 * @extends EventEmitter
18 */
19class WebSocketServer extends EventEmitter {
20 /**
21 * Create a `WebSocketServer` instance.
22 *
23 * @param {Object} options Configuration options
24 * @param {Number} [options.backlog=511] The maximum length of the queue of
25 * pending connections
26 * @param {Boolean} [options.clientTracking=true] Specifies whether or not to
27 * track clients
28 * @param {Function} [options.handleProtocols] A hook to handle protocols
29 * @param {String} [options.host] The hostname where to bind the server
30 * @param {Number} [options.maxPayload=104857600] The maximum allowed message
31 * size
32 * @param {Boolean} [options.noServer=false] Enable no server mode
33 * @param {String} [options.path] Accept only connections matching this path
34 * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
35 * permessage-deflate
36 * @param {Number} [options.port] The port where to bind the server
37 * @param {http.Server} [options.server] A pre-created HTTP/S server to use
38 * @param {Function} [options.verifyClient] A hook to reject connections
39 * @param {Function} [callback] A listener for the `listening` event
40 */
41 constructor(options, callback) {
42 super();
43
44 options = {
45 maxPayload: 100 * 1024 * 1024,
46 perMessageDeflate: false,
47 handleProtocols: null,
48 clientTracking: true,
49 verifyClient: null,
50 noServer: false,
51 backlog: null, // use default (511 as implemented in net.js)
52 server: null,
53 host: null,
54 path: null,
55 port: null,
56 ...options
57 };
58
59 if (options.port == null && !options.server && !options.noServer) {
60 throw new TypeError(
61 'One of the "port", "server", or "noServer" options must be specified'
62 );
63 }
64
65 if (options.port != null) {
66 this._server = createServer((req, res) => {
67 const body = STATUS_CODES[426];
68
69 res.writeHead(426, {
70 'Content-Length': body.length,
71 'Content-Type': 'text/plain'
72 });
73 res.end(body);
74 });
75 this._server.listen(
76 options.port,
77 options.host,
78 options.backlog,
79 callback
80 );
81 } else if (options.server) {
82 this._server = options.server;
83 }
84
85 if (this._server) {
86 const emitConnection = this.emit.bind(this, 'connection');
87
88 this._removeListeners = addListeners(this._server, {
89 listening: this.emit.bind(this, 'listening'),
90 error: this.emit.bind(this, 'error'),
91 upgrade: (req, socket, head) => {
92 this.handleUpgrade(req, socket, head, emitConnection);
93 }
94 });
95 }
96
97 if (options.perMessageDeflate === true) options.perMessageDeflate = {};
98 if (options.clientTracking) this.clients = new Set();
99 this.options = options;
100 }
101
102 /**
103 * Returns the bound address, the address family name, and port of the server
104 * as reported by the operating system if listening on an IP socket.
105 * If the server is listening on a pipe or UNIX domain socket, the name is
106 * returned as a string.
107 *
108 * @return {(Object|String|null)} The address of the server
109 * @public
110 */
111 address() {
112 if (this.options.noServer) {
113 throw new Error('The server is operating in "noServer" mode');
114 }
115
116 if (!this._server) return null;
117 return this._server.address();
118 }
119
120 /**
121 * Close the server.
122 *
123 * @param {Function} [cb] Callback
124 * @public
125 */
126 close(cb) {
127 if (cb) this.once('close', cb);
128
129 //
130 // Terminate all associated clients.
131 //
132 if (this.clients) {
133 for (const client of this.clients) client.terminate();
134 }
135
136 const server = this._server;
137
138 if (server) {
139 this._removeListeners();
140 this._removeListeners = this._server = null;
141
142 //
143 // Close the http server if it was internally created.
144 //
145 if (this.options.port != null) {
146 server.close(() => this.emit('close'));
147 return;
148 }
149 }
150
151 process.nextTick(emitClose, this);
152 }
153
154 /**
155 * See if a given request should be handled by this server instance.
156 *
157 * @param {http.IncomingMessage} req Request object to inspect
158 * @return {Boolean} `true` if the request is valid, else `false`
159 * @public
160 */
161 shouldHandle(req) {
162 if (this.options.path) {
163 const index = req.url.indexOf('?');
164 const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
165
166 if (pathname !== this.options.path) return false;
167 }
168
169 return true;
170 }
171
172 /**
173 * Handle a HTTP Upgrade request.
174 *
175 * @param {http.IncomingMessage} req The request object
176 * @param {net.Socket} socket The network socket between the server and client
177 * @param {Buffer} head The first packet of the upgraded stream
178 * @param {Function} cb Callback
179 * @public
180 */
181 handleUpgrade(req, socket, head, cb) {
182 socket.on('error', socketOnError);
183
184 const key =
185 req.headers['sec-websocket-key'] !== undefined
186 ? req.headers['sec-websocket-key'].trim()
187 : false;
188 const version = +req.headers['sec-websocket-version'];
189 const extensions = {};
190
191 if (
192 req.method !== 'GET' ||
193 req.headers.upgrade.toLowerCase() !== 'websocket' ||
194 !key ||
195 !keyRegex.test(key) ||
196 (version !== 8 && version !== 13) ||
197 !this.shouldHandle(req)
198 ) {
199 return abortHandshake(socket, 400);
200 }
201
202 if (this.options.perMessageDeflate) {
203 const perMessageDeflate = new PerMessageDeflate(
204 this.options.perMessageDeflate,
205 true,
206 this.options.maxPayload
207 );
208
209 try {
210 const offers = parse(req.headers['sec-websocket-extensions']);
211
212 if (offers[PerMessageDeflate.extensionName]) {
213 perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
214 extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
215 }
216 } catch (err) {
217 return abortHandshake(socket, 400);
218 }
219 }
220
221 //
222 // Optionally call external client verification handler.
223 //
224 if (this.options.verifyClient) {
225 const info = {
226 origin:
227 req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
228 secure: !!(req.socket.authorized || req.socket.encrypted),
229 req
230 };
231
232 if (this.options.verifyClient.length === 2) {
233 this.options.verifyClient(info, (verified, code, message, headers) => {
234 if (!verified) {
235 return abortHandshake(socket, code || 401, message, headers);
236 }
237
238 this.completeUpgrade(key, extensions, req, socket, head, cb);
239 });
240 return;
241 }
242
243 if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
244 }
245
246 this.completeUpgrade(key, extensions, req, socket, head, cb);
247 }
248
249 /**
250 * Upgrade the connection to WebSocket.
251 *
252 * @param {String} key The value of the `Sec-WebSocket-Key` header
253 * @param {Object} extensions The accepted extensions
254 * @param {http.IncomingMessage} req The request object
255 * @param {net.Socket} socket The network socket between the server and client
256 * @param {Buffer} head The first packet of the upgraded stream
257 * @param {Function} cb Callback
258 * @throws {Error} If called more than once with the same socket
259 * @private
260 */
261 completeUpgrade(key, extensions, req, socket, head, cb) {
262 //
263 // Destroy the socket if the client has already sent a FIN packet.
264 //
265 if (!socket.readable || !socket.writable) return socket.destroy();
266
267 if (socket[kWebSocket]) {
268 throw new Error(
269 'server.handleUpgrade() was called more than once with the same ' +
270 'socket, possibly due to a misconfiguration'
271 );
272 }
273
274 const digest = createHash('sha1')
275 .update(key + GUID)
276 .digest('base64');
277
278 const headers = [
279 'HTTP/1.1 101 Switching Protocols',
280 'Upgrade: websocket',
281 'Connection: Upgrade',
282 `Sec-WebSocket-Accept: ${digest}`
283 ];
284
285 const ws = new WebSocket(null);
286 let protocol = req.headers['sec-websocket-protocol'];
287
288 if (protocol) {
289 protocol = protocol.split(',').map(trim);
290
291 //
292 // Optionally call external protocol selection handler.
293 //
294 if (this.options.handleProtocols) {
295 protocol = this.options.handleProtocols(protocol, req);
296 } else {
297 protocol = protocol[0];
298 }
299
300 if (protocol) {
301 headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
302 ws._protocol = protocol;
303 }
304 }
305
306 if (extensions[PerMessageDeflate.extensionName]) {
307 const params = extensions[PerMessageDeflate.extensionName].params;
308 const value = format({
309 [PerMessageDeflate.extensionName]: [params]
310 });
311 headers.push(`Sec-WebSocket-Extensions: ${value}`);
312 ws._extensions = extensions;
313 }
314
315 //
316 // Allow external modification/inspection of handshake headers.
317 //
318 this.emit('headers', headers, req);
319
320 socket.write(headers.concat('\r\n').join('\r\n'));
321 socket.removeListener('error', socketOnError);
322
323 ws.setSocket(socket, head, this.options.maxPayload);
324
325 if (this.clients) {
326 this.clients.add(ws);
327 ws.on('close', () => this.clients.delete(ws));
328 }
329
330 cb(ws, req);
331 }
332}
333
334module.exports = WebSocketServer;
335
336/**
337 * Add event listeners on an `EventEmitter` using a map of <event, listener>
338 * pairs.
339 *
340 * @param {EventEmitter} server The event emitter
341 * @param {Object.<String, Function>} map The listeners to add
342 * @return {Function} A function that will remove the added listeners when
343 * called
344 * @private
345 */
346function addListeners(server, map) {
347 for (const event of Object.keys(map)) server.on(event, map[event]);
348
349 return function removeListeners() {
350 for (const event of Object.keys(map)) {
351 server.removeListener(event, map[event]);
352 }
353 };
354}
355
356/**
357 * Emit a `'close'` event on an `EventEmitter`.
358 *
359 * @param {EventEmitter} server The event emitter
360 * @private
361 */
362function emitClose(server) {
363 server.emit('close');
364}
365
366/**
367 * Handle premature socket errors.
368 *
369 * @private
370 */
371function socketOnError() {
372 this.destroy();
373}
374
375/**
376 * Close the connection when preconditions are not fulfilled.
377 *
378 * @param {net.Socket} socket The socket of the upgrade request
379 * @param {Number} code The HTTP response status code
380 * @param {String} [message] The HTTP response body
381 * @param {Object} [headers] Additional HTTP response headers
382 * @private
383 */
384function abortHandshake(socket, code, message, headers) {
385 if (socket.writable) {
386 message = message || STATUS_CODES[code];
387 headers = {
388 Connection: 'close',
389 'Content-Type': 'text/html',
390 'Content-Length': Buffer.byteLength(message),
391 ...headers
392 };
393
394 socket.write(
395 `HTTP/1.1 ${code} ${STATUS_CODES[code]}\r\n` +
396 Object.keys(headers)
397 .map((h) => `${h}: ${headers[h]}`)
398 .join('\r\n') +
399 '\r\n\r\n' +
400 message
401 );
402 }
403
404 socket.removeListener('error', socketOnError);
405 socket.destroy();
406}
407
408/**
409 * Remove whitespace characters from both ends of a string.
410 *
411 * @param {String} str The string
412 * @return {String} A new string representing `str` stripped of whitespace
413 * characters from both its beginning and end
414 * @private
415 */
416function trim(str) {
417 return str.trim();
418}
Note: See TracBrowser for help on using the repository browser.