* Parses the `Sec-WebSocket-Extensions` header into an object. * * @param {String} header The field value of the header * @return {Object} The parsed object * @public
(header)
| 25 | * @public |
| 26 | */ |
| 27 | function parse(header) { |
| 28 | const offers = Object.create(null); |
| 29 | let params = Object.create(null); |
| 30 | let mustUnescape = false; |
| 31 | let isEscaping = false; |
| 32 | let inQuotes = false; |
| 33 | let extensionName; |
| 34 | let paramName; |
| 35 | let start = -1; |
| 36 | let code = -1; |
| 37 | let end = -1; |
| 38 | let i = 0; |
| 39 | |
| 40 | for (; i < header.length; i++) { |
| 41 | code = header.charCodeAt(i); |
| 42 | |
| 43 | if (extensionName === undefined) { |
| 44 | if (end === -1 && tokenChars[code] === 1) { |
| 45 | if (start === -1) start = i; |
| 46 | } else if ( |
| 47 | i !== 0 && |
| 48 | (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */ |
| 49 | ) { |
| 50 | if (end === -1 && start !== -1) end = i; |
| 51 | } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) { |
| 52 | if (start === -1) { |
| 53 | throw new SyntaxError(`Unexpected character at index ${i}`); |
| 54 | } |
| 55 | |
| 56 | if (end === -1) end = i; |
| 57 | const name = header.slice(start, end); |
| 58 | if (code === 0x2c) { |
| 59 | push(offers, name, params); |
| 60 | params = Object.create(null); |
| 61 | } else { |
| 62 | extensionName = name; |
| 63 | } |
| 64 | |
| 65 | start = end = -1; |
| 66 | } else { |
| 67 | throw new SyntaxError(`Unexpected character at index ${i}`); |
| 68 | } |
| 69 | } else if (paramName === undefined) { |
| 70 | if (end === -1 && tokenChars[code] === 1) { |
| 71 | if (start === -1) start = i; |
| 72 | } else if (code === 0x20 || code === 0x09) { |
| 73 | if (end === -1 && start !== -1) end = i; |
| 74 | } else if (code === 0x3b || code === 0x2c) { |
| 75 | if (start === -1) { |
| 76 | throw new SyntaxError(`Unexpected character at index ${i}`); |
| 77 | } |
| 78 | |
| 79 | if (end === -1) end = i; |
| 80 | push(params, header.slice(start, end), true); |
| 81 | if (code === 0x2c) { |
| 82 | push(offers, extensionName, params); |
| 83 | params = Object.create(null); |
| 84 | extensionName = undefined; |
no test coverage detected