overleaf/services/web/app/src/infrastructure/Response.js
Jakob Ackermann d720d6affa Merge pull request #6317 from overleaf/jpa-send-explicit-content-type
[web] send explicit content type in responses

GitOrigin-RevId: d5aeaba57a7d2fc053fbf5adc2299fb46e435341
2022-01-18 09:03:18 +00:00

48 lines
1.2 KiB
JavaScript

function csvAttachment(res, body, filename) {
if (!filename || !filename.endsWith('.csv')) {
throw new Error('filename must end with .csv')
}
// res.attachment sets both content-type and content-disposition headers.
res.attachment(filename)
res.setHeader('X-Content-Type-Options', 'nosniff')
res.send(body)
}
function preparePlainTextResponse(res) {
res.setHeader('X-Content-Type-Options', 'nosniff')
res.contentType('text/plain; charset=utf-8')
}
function plainTextResponse(res, body) {
preparePlainTextResponse(res)
res.send(body)
}
function xmlResponse(res, body) {
res.setHeader('X-Content-Type-Options', 'nosniff')
res.contentType('application/xml; charset=utf-8')
res.send(body)
}
function prepareZipAttachment(res, filename) {
if (!filename || !filename.endsWith('.zip')) {
throw new Error('filename must end with .zip')
}
// res.attachment sets both content-type and content-disposition headers.
res.attachment(filename)
res.setHeader('X-Content-Type-Options', 'nosniff')
}
function zipAttachment(res, body, filename) {
prepareZipAttachment(res, filename)
res.send(body)
}
module.exports = {
csvAttachment,
plainTextResponse,
preparePlainTextResponse,
prepareZipAttachment,
xmlResponse,
zipAttachment,
}