Commit 6f32c747 authored by Sebastiaan Deckers's avatar Sebastiaan Deckers 馃悜

fix: never respond with body to HEAD request

parent be76b81a
Pipeline #40244308 failed with stage
in 90 minutes and 1 second
......@@ -29,30 +29,43 @@ module.exports.serveResponse = () => {
if (statusCode !== 200) {
headers[':status'] = statusCode
}
response.stream.respondWithFile(
resolved.absolute,
Object.assign(response.getHeaders(), headers),
{
statCheck (stat, headers) {
if (request.method === 'HEAD') {
headers['content-length'] = stat.size
response.stream.respond(headers)
return false
}
},
onError ({ code }) {
if (code === 'ENOENT') {
next(new NotFound())
} else {
next(new InternalServerError())
if (request.method === 'HEAD') {
try {
response.setHeader(
'content-length',
(await promisify(stat)(resolved.absolute)).size
)
} catch (error) {
return next(error)
}
response.writeHead(statusCode, headers)
response.end()
} else {
response.stream.respondWithFile(
resolved.absolute,
Object.assign(response.getHeaders(), headers),
{
statCheck (stat, headers) {
if (request.method === 'HEAD') {
headers['content-length'] = stat.size
response.stream.respond(headers)
return false
}
},
onError ({ code }) {
if (code === 'ENOENT') {
next(new NotFound())
} else {
next(new InternalServerError())
}
}
}
)
try {
await serveDependencies(request, response)
} catch (error) {
request.log.error(error)
}
)
try {
await serveDependencies(request, response)
} catch (error) {
request.log.error(error)
}
} else {
try {
......
......@@ -26,7 +26,7 @@ const fixtures = [
response: 'gzip'
},
length: {
header: 26,
header: '26',
payload: 26
},
payload: 'Blablabla\n',
......@@ -40,7 +40,7 @@ const fixtures = [
response: 'br'
},
length: {
header: 14,
header: '14',
payload: 14
},
payload: 'Blablabla\n',
......@@ -54,7 +54,7 @@ const fixtures = [
response: undefined
},
length: {
header: 10,
header: '10',
payload: 0
},
payload: '',
......@@ -68,7 +68,7 @@ const fixtures = [
response: 'br'
},
length: {
header: 14,
header: '14',
payload: 0
},
payload: '',
......@@ -82,7 +82,7 @@ const fixtures = [
response: 'br'
},
length: {
header: 14,
header: '14',
payload: 14
},
payload: 'Blablabla\n',
......@@ -96,7 +96,7 @@ const fixtures = [
response: 'gzip'
},
length: {
header: 26,
header: '26',
payload: 26
},
payload: 'Blablabla\n',
......@@ -120,7 +120,7 @@ for (const { label, method, encoding, length, payload, decoder } of fixtures) {
stream.on('response', (headers) => {
t.is(headers[':status'], 200)
t.is(headers['content-encoding'], encoding.response)
t.is(headers['content-length'], String(length.header))
t.is(headers['content-length'], length.header)
stream
.pipe(concat((data) => {
......@@ -150,7 +150,7 @@ for (const { label, method, encoding, length, payload, decoder } of fixtures) {
agent: new Agent({ rejectUnauthorized: false })
}
const client = request(options, async (response) => {
t.is(response.headers['content-length'], String(length.header))
t.is(response.headers['content-length'], length.header)
t.is(response.headers['content-encoding'], encoding.response)
response
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment