Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion test/vary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,26 @@ test('Should ignore the header as value for vary if it is already in vary', t =>
})
})

test('Should ignore the header as value for vary if it is already in vary with whitespace before comma', t => {
t.plan(1)

const addOriginToVary = createAddFieldnameToVary('Origin')
const replyMock = {
getHeader () {
return 'Origin , Accept-Encoding'
},
header () {
throw new Error('Should not be here')
}
}

t.assert.doesNotThrow(() => {
addOriginToVary(replyMock)
})
})

test('parse', t => {
t.plan(18)
t.plan(22)
t.assert.deepStrictEqual(parse(''), [])
t.assert.deepStrictEqual(parse('a'), ['a'])
t.assert.deepStrictEqual(parse('a,b'), ['a', 'b'])
Expand All @@ -214,6 +232,10 @@ test('parse', t => {
t.assert.deepStrictEqual(parse('a, b,c'), ['a', 'b', 'c'])
t.assert.deepStrictEqual(parse('a, , b,c'), ['a', 'b', 'c'])
t.assert.deepStrictEqual(parse('a, , b,c'), ['a', 'b', 'c'])
t.assert.deepStrictEqual(parse('a ,b'), ['a', 'b'])
t.assert.deepStrictEqual(parse('a , b'), ['a', 'b'])
t.assert.deepStrictEqual(parse('a\t,\tb'), ['a', 'b'])
t.assert.deepStrictEqual(parse('Origin , Accept-Encoding'), ['origin', 'accept-encoding'])

// one for the cache
t.assert.deepStrictEqual(parse('A,b,c'), ['a', 'b', 'c'])
Expand Down
35 changes: 15 additions & 20 deletions vary.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,26 @@ function parse (header) {
} else if (header.indexOf(',') === -1) {
result.push(header)
} else {
const il = header.length
let i = 0
let pos = 0
let char
const len = header.length
let start = 0

// tokenize the header
for (i; i < il; ++i) {
char = header[i]
// when we have whitespace set the pos to the next position
if (char === ' ') {
pos = i + 1
// `,` is the separator of vary-values
} else if (char === ',') {
// if pos and current position are not the same we have a valid token
if (pos !== i) {
result.push(header.slice(pos, i))
for (let i = 0; i <= len; ++i) {
if (i === len || header.charCodeAt(i) === 44) {
let tokenStart = start
let tokenEnd = i
while (tokenStart < tokenEnd && (header.charCodeAt(tokenStart) === 32 || header.charCodeAt(tokenStart) === 9)) {
tokenStart++
}
// reset the positions
pos = i + 1
while (tokenEnd > tokenStart && (header.charCodeAt(tokenEnd - 1) === 32 || header.charCodeAt(tokenEnd - 1) === 9)) {
tokenEnd--
}
if (tokenStart < tokenEnd) {
result.push(header.slice(tokenStart, tokenEnd))
}
start = i + 1
}
}

if (pos !== i) {
result.push(header.slice(pos, i))
}
}

return result
Expand Down
Loading