Skip to content

Commit

Permalink
Implement the requirement of IncomingHttpHeaders["set-cookie"]
Browse files Browse the repository at this point in the history
Currently `headers["set-cookie"]` does not split,
which can't match the expected type of IncomingHttpHeader.

This commit attempts to split `headers["set-cookie"]` by `;`
like what 'node:http' does:
https://nodejs.org/api/http.html#class-httpserverresponse

Fixed nodejs#1892
  • Loading branch information
pan93412 committed Jan 28, 2023
1 parent 196c4da commit 0f409c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,19 @@ function parseKeepAliveTimeout (val) {
return m ? parseInt(m[1], 10) * 1000 : null
}

function splitCookie (val) {
return val.toString().split(';')
}

function parseHeaders (headers, obj = {}) {
for (let i = 0; i < headers.length; i += 2) {
const key = headers[i].toString().toLowerCase()
let val = obj[key]
if (!val) {
if (Array.isArray(headers[i + 1])) {
obj[key] = headers[i + 1]
} else if (key === 'set-cookie') {
obj[key] = splitCookie(headers[i + 1])
} else {
obj[key] = headers[i + 1].toString()
}
Expand All @@ -224,7 +230,12 @@ function parseHeaders (headers, obj = {}) {
val = [val]
obj[key] = val
}
val.push(headers[i + 1].toString())

if (key === 'set-cookie') {
val.push(...splitCookie(headers[i + 1]))
} else {
val.push(headers[i + 1].toString())
}
}
}
return obj
Expand Down
17 changes: 16 additions & 1 deletion test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,27 @@ test('validateHandler', (t) => {
})

test('parseHeaders', (t) => {
t.plan(5)
t.plan(7)
t.same(util.parseHeaders(['key', 'value']), { key: 'value' })
t.same(util.parseHeaders([Buffer.from('key'), Buffer.from('value')]), { key: 'value' })
t.same(util.parseHeaders(['Key', 'Value']), { key: 'Value' })
t.same(util.parseHeaders(['Key', 'value', 'key', 'Value']), { key: ['value', 'Value'] })
t.same(util.parseHeaders(['key', ['value1', 'value2', 'value3']]), { key: ['value1', 'value2', 'value3'] })
t.same(util.parseHeaders(['set-cookie', '123456|123456|123456;Path=/']), { 'set-cookie': ['123456|123456|123456', 'Path=/'] })
t.same(util.parseHeaders([
'set-cookie',
'123456|123456|123456;Path=/',
'set-cookie',
'233333|114514;1919810;Hello=World'
]), {
'set-cookie': [
'123456|123456|123456',
'Path=/',
'233333|114514',
'1919810',
'Hello=World'
]
})
})

test('parseRawHeaders', (t) => {
Expand Down

0 comments on commit 0f409c0

Please sign in to comment.