2023-11-13 21:10:04 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const decodeText = require('./decodeText')
|
|
|
|
|
2023-11-17 13:22:28 +00:00
|
|
|
const RE_ENCODED = /%([a-fA-F0-9]{2})/g
|
2023-11-13 21:10:04 +00:00
|
|
|
|
2023-11-17 13:22:28 +00:00
|
|
|
function encodedReplacer (match, byte) {
|
|
|
|
return String.fromCharCode(parseInt(byte, 16))
|
2023-11-13 21:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseParams (str) {
|
|
|
|
const res = []
|
2023-11-17 13:22:28 +00:00
|
|
|
let state = 'key'
|
2023-11-13 21:10:04 +00:00
|
|
|
let charset = ''
|
|
|
|
let inquote = false
|
|
|
|
let escaping = false
|
|
|
|
let p = 0
|
|
|
|
let tmp = ''
|
|
|
|
|
2023-11-17 13:22:28 +00:00
|
|
|
for (var i = 0, len = str.length; i < len; ++i) { // eslint-disable-line no-var
|
2023-11-13 21:10:04 +00:00
|
|
|
const char = str[i]
|
|
|
|
if (char === '\\' && inquote) {
|
|
|
|
if (escaping) { escaping = false } else {
|
|
|
|
escaping = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if (char === '"') {
|
|
|
|
if (!escaping) {
|
|
|
|
if (inquote) {
|
|
|
|
inquote = false
|
2023-11-17 13:22:28 +00:00
|
|
|
state = 'key'
|
2023-11-13 21:10:04 +00:00
|
|
|
} else { inquote = true }
|
|
|
|
continue
|
|
|
|
} else { escaping = false }
|
|
|
|
} else {
|
|
|
|
if (escaping && inquote) { tmp += '\\' }
|
|
|
|
escaping = false
|
2023-11-17 13:22:28 +00:00
|
|
|
if ((state === 'charset' || state === 'lang') && char === "'") {
|
|
|
|
if (state === 'charset') {
|
|
|
|
state = 'lang'
|
2023-11-13 21:10:04 +00:00
|
|
|
charset = tmp.substring(1)
|
2023-11-17 13:22:28 +00:00
|
|
|
} else { state = 'value' }
|
2023-11-13 21:10:04 +00:00
|
|
|
tmp = ''
|
|
|
|
continue
|
2023-11-17 13:22:28 +00:00
|
|
|
} else if (state === 'key' &&
|
2023-11-13 21:10:04 +00:00
|
|
|
(char === '*' || char === '=') &&
|
|
|
|
res.length) {
|
2023-11-17 13:22:28 +00:00
|
|
|
if (char === '*') { state = 'charset' } else { state = 'value' }
|
2023-11-13 21:10:04 +00:00
|
|
|
res[p] = [tmp, undefined]
|
|
|
|
tmp = ''
|
|
|
|
continue
|
|
|
|
} else if (!inquote && char === ';') {
|
2023-11-17 13:22:28 +00:00
|
|
|
state = 'key'
|
2023-11-13 21:10:04 +00:00
|
|
|
if (charset) {
|
|
|
|
if (tmp.length) {
|
|
|
|
tmp = decodeText(tmp.replace(RE_ENCODED, encodedReplacer),
|
|
|
|
'binary',
|
|
|
|
charset)
|
|
|
|
}
|
|
|
|
charset = ''
|
|
|
|
} else if (tmp.length) {
|
|
|
|
tmp = decodeText(tmp, 'binary', 'utf8')
|
|
|
|
}
|
|
|
|
if (res[p] === undefined) { res[p] = tmp } else { res[p][1] = tmp }
|
|
|
|
tmp = ''
|
|
|
|
++p
|
|
|
|
continue
|
|
|
|
} else if (!inquote && (char === ' ' || char === '\t')) { continue }
|
|
|
|
}
|
|
|
|
tmp += char
|
|
|
|
}
|
|
|
|
if (charset && tmp.length) {
|
|
|
|
tmp = decodeText(tmp.replace(RE_ENCODED, encodedReplacer),
|
|
|
|
'binary',
|
|
|
|
charset)
|
|
|
|
} else if (tmp) {
|
|
|
|
tmp = decodeText(tmp, 'binary', 'utf8')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res[p] === undefined) {
|
|
|
|
if (tmp) { res[p] = tmp }
|
|
|
|
} else { res[p][1] = tmp }
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parseParams
|