mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2026-10-24 16:41:22 +08:00
fix: 修复 cookie 解析器并也尝试从请求头读 cookie
This commit is contained in:
+24
-4
@@ -94,10 +94,12 @@ module.exports = {
|
||||
// 优化:使用for循环替代forEach,性能更好
|
||||
for (let i = 0, len = cookieArr.length; i < len; i++) {
|
||||
let item = cookieArr[i]
|
||||
let arr = item.split('=')
|
||||
// 优化:使用严格等于
|
||||
if (arr.length === 2) {
|
||||
obj[arr[0].trim()] = arr[1].trim()
|
||||
let separator = item.indexOf('=')
|
||||
// 只按第一个等号拆分,保留 Cookie 值中的等号
|
||||
if (separator > 0) {
|
||||
obj[item.slice(0, separator).trim()] = item
|
||||
.slice(separator + 1)
|
||||
.trim()
|
||||
}
|
||||
}
|
||||
return obj
|
||||
@@ -112,6 +114,24 @@ module.exports = {
|
||||
)
|
||||
},
|
||||
|
||||
getCookieHeader(headers) {
|
||||
if (!headers || typeof headers !== 'object') return ''
|
||||
|
||||
if (typeof headers.get === 'function') {
|
||||
const cookie = headers.get('cookie')
|
||||
if (typeof cookie === 'string') return cookie
|
||||
}
|
||||
|
||||
const headerName = Object.keys(headers).find(
|
||||
(name) => name.toLowerCase() === 'cookie',
|
||||
)
|
||||
if (!headerName) return ''
|
||||
|
||||
const cookie = headers[headerName]
|
||||
if (Array.isArray(cookie)) return cookie.join('; ')
|
||||
return typeof cookie === 'string' ? cookie : ''
|
||||
},
|
||||
|
||||
cookieObjToString(cookie) {
|
||||
// 优化:使用预绑定的keys函数和for循环
|
||||
const cookieKeys = keys(cookie)
|
||||
|
||||
Reference in New Issue
Block a user