Skip to content

Commit 664cb84

Browse files
authored
fix(command-dev): handle headers with colon in value (#1855)
1 parent 38c363c commit 664cb84

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/utils/headers.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ const headersForPath = function (rules, path) {
6767
return pathObject
6868
}
6969

70+
const HEADER_SEPARATOR = ':'
71+
7072
const parseHeadersFile = function (filePath) {
7173
if (!fs.existsSync(filePath)) {
7274
return {}
@@ -94,9 +96,9 @@ const parseHeadersFile = function (filePath) {
9496
throw new Error('path should come before headers')
9597
}
9698

97-
if (line.includes(':')) {
98-
const [key = '', value = ''] = line.split(':', 2)
99-
const [trimmedKey, trimmedValue] = [key.trim(), value.trim()]
99+
if (line.includes(HEADER_SEPARATOR)) {
100+
const [key = '', ...value] = line.split(HEADER_SEPARATOR)
101+
const [trimmedKey, trimmedValue] = [key.trim(), value.join(HEADER_SEPARATOR).trim()]
100102
if (trimmedKey.length === 0 || trimmedValue.length === 0) {
101103
throw new Error(`invalid header at line: ${index}\n${line}\n`)
102104
}

src/utils/headers.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const headers = [
3535
path: '/directory/*/test.html',
3636
headers: ['X-Frame-Options: test'],
3737
},
38+
{
39+
path: '/with-colon',
40+
headers: ['Custom-header: http://www.example.com'],
41+
},
3842
]
3943

4044
test.before(async (t) => {
@@ -99,6 +103,9 @@ test('_headers: validate rules', (t) => {
99103
'/directory/*/test.html': {
100104
'X-Frame-Options': ['test'],
101105
},
106+
'/with-colon': {
107+
'Custom-header': ['http://www.example.com'],
108+
},
102109
})
103110
})
104111

0 commit comments

Comments
 (0)