4 // Allowed token characters:
6 // '!', '#', '$', '%', '&', ''', '*', '+', '-',
7 // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
9 // tokenChars[32] === 0 // ' '
10 // tokenChars[33] === 1 // '!'
11 // tokenChars[34] === 0 // '"'
15 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
16 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
17 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
18 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
19 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
20 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
21 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
22 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
26 * Adds an offer to the map of extension offers or a parameter to the map of
29 * @param {Object} dest The map of extension offers or parameters
30 * @param {String} name The extension or parameter name
31 * @param {(Object|Boolean|String)} elem The extension parameters or the
35 function push (dest, name, elem) {
36 if (Object.prototype.hasOwnProperty.call(dest, name)) dest[name].push(elem);
37 else dest[name] = [elem];
41 * Parses the `Sec-WebSocket-Extensions` header into an object.
43 * @param {String} header The field value of the header
44 * @return {Object} The parsed object
47 function parse (header) {
50 if (header === undefined || header === '') return offers;
53 var mustUnescape = false;
54 var isEscaping = false;
61 for (var i = 0; i < header.length; i++) {
62 const code = header.charCodeAt(i);
64 if (extensionName === undefined) {
65 if (end === -1 && tokenChars[code] === 1) {
66 if (start === -1) start = i;
67 } else if (code === 0x20/* ' ' */|| code === 0x09/* '\t' */) {
68 if (end === -1 && start !== -1) end = i;
69 } else if (code === 0x3b/* ';' */ || code === 0x2c/* ',' */) {
71 throw new SyntaxError(`Unexpected character at index ${i}`);
74 if (end === -1) end = i;
75 const name = header.slice(start, end);
77 push(offers, name, params);
85 throw new SyntaxError(`Unexpected character at index ${i}`);
87 } else if (paramName === undefined) {
88 if (end === -1 && tokenChars[code] === 1) {
89 if (start === -1) start = i;
90 } else if (code === 0x20 || code === 0x09) {
91 if (end === -1 && start !== -1) end = i;
92 } else if (code === 0x3b || code === 0x2c) {
94 throw new SyntaxError(`Unexpected character at index ${i}`);
97 if (end === -1) end = i;
98 push(params, header.slice(start, end), true);
100 push(offers, extensionName, params);
102 extensionName = undefined;
106 } else if (code === 0x3d/* '=' */&& start !== -1 && end === -1) {
107 paramName = header.slice(start, i);
110 throw new SyntaxError(`Unexpected character at index ${i}`);
114 // The value of a quoted-string after unescaping must conform to the
115 // token ABNF, so only token characters are valid.
116 // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
119 if (tokenChars[code] !== 1) {
120 throw new SyntaxError(`Unexpected character at index ${i}`);
122 if (start === -1) start = i;
123 else if (!mustUnescape) mustUnescape = true;
125 } else if (inQuotes) {
126 if (tokenChars[code] === 1) {
127 if (start === -1) start = i;
128 } else if (code === 0x22/* '"' */ && start !== -1) {
131 } else if (code === 0x5c/* '\' */) {
134 throw new SyntaxError(`Unexpected character at index ${i}`);
136 } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
138 } else if (end === -1 && tokenChars[code] === 1) {
139 if (start === -1) start = i;
140 } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
141 if (end === -1) end = i;
142 } else if (code === 0x3b || code === 0x2c) {
144 throw new SyntaxError(`Unexpected character at index ${i}`);
147 if (end === -1) end = i;
148 var value = header.slice(start, end);
150 value = value.replace(/\\/g, '');
151 mustUnescape = false;
153 push(params, paramName, value);
155 push(offers, extensionName, params);
157 extensionName = undefined;
160 paramName = undefined;
163 throw new SyntaxError(`Unexpected character at index ${i}`);
168 if (start === -1 || inQuotes) {
169 throw new SyntaxError('Unexpected end of input');
172 if (end === -1) end = i;
173 const token = header.slice(start, end);
174 if (extensionName === undefined) {
175 push(offers, token, {});
177 if (paramName === undefined) {
178 push(params, token, true);
179 } else if (mustUnescape) {
180 push(params, paramName, token.replace(/\\/g, ''));
182 push(params, paramName, token);
184 push(offers, extensionName, params);
191 * Builds the `Sec-WebSocket-Extensions` header field value.
193 * @param {Object} extensions The map of extensions and parameters to format
194 * @return {String} A string representing the given object
197 function format (extensions) {
198 return Object.keys(extensions).map((extension) => {
199 var configurations = extensions[extension];
200 if (!Array.isArray(configurations)) configurations = [configurations];
201 return configurations.map((params) => {
202 return [extension].concat(Object.keys(params).map((k) => {
203 var values = params[k];
204 if (!Array.isArray(values)) values = [values];
205 return values.map((v) => v === true ? k : `${k}=${v}`).join('; ');
211 module.exports = { format, parse };