SPB Git forge

spb/admin-ka

Public
41commits 1branches 0releases
172.9 MBsize
maindefault branch
20 days agolast push
JavaScript 65.5% Python 17.8% CSS 13% HTML 3.7%
1.5 KB · 63 lines javascript
Raw Blame History
1'use strict';23const { tokenChars } = require('./validation');45/**6 * Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.7 *8 * @param {String} header The field value of the header9 * @return {Set} The subprotocol names10 * @public11 */12function parse(header) {13  const protocols = new Set();14  let start = -1;15  let end = -1;16  let i = 0;1718  for (i; i < header.length; i++) {19    const code = header.charCodeAt(i);2021    if (end === -1 && tokenChars[code] === 1) {22      if (start === -1) start = i;23    } else if (24      i !== 0 &&25      (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */26    ) {27      if (end === -1 && start !== -1) end = i;28    } else if (code === 0x2c /* ',' */) {29      if (start === -1) {30        throw new SyntaxError(`Unexpected character at index ${i}`);31      }3233      if (end === -1) end = i;3435      const protocol = header.slice(start, end);3637      if (protocols.has(protocol)) {38        throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);39      }4041      protocols.add(protocol);42      start = end = -1;43    } else {44      throw new SyntaxError(`Unexpected character at index ${i}`);45    }46  }4748  if (start === -1 || end !== -1) {49    throw new SyntaxError('Unexpected end of input');50  }5152  const protocol = header.slice(start, i);5354  if (protocols.has(protocol)) {55    throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);56  }5758  protocols.add(protocol);59  return protocols;60}6162module.exports = { parse };63