From 0bde009573029c06a4c9ccdd1f3f4aeb319ba8d3 Mon Sep 17 00:00:00 2001 From: Joe Biellik Date: Mon, 22 Jun 2020 21:53:54 +0100 Subject: [PATCH] Improve caching and argument handing --- controllers/pastes.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/controllers/pastes.js b/controllers/pastes.js index b6d5e61..dab7326 100644 --- a/controllers/pastes.js +++ b/controllers/pastes.js @@ -5,31 +5,40 @@ module.exports = { async view(ctx) { try { let paste = await Paste.findById(ctx.params.id).exec(); - let lang = Object.keys(ctx.query)[0]; + let lang = (Object.keys(ctx.query)[0] || '').toLowerCase(); + + ctx.set('Cache-Control', 'public'); + ctx.set('Expires', paste.expiresAt.toUTCString()); if (lang) { await ctx.render('highlight', { pretty: config.prettyHtml, title: config.name + ' ' + paste.id, paste: paste.paste, - lang: lang + lang: Object.keys(config.highlights).includes(lang) ? lang : 'unknown' }); } else { ctx.type = 'text/plain'; ctx.body = paste.paste; } } catch (ex) { - ctx.throw(404, 'Paste Not Found'); + ctx.throw(404, 'Paste Not Found', { + headers: { + 'Cache-Control': 'no-cache' + } + }); } }, async create(ctx) { + ctx.set('Cache-Control', 'no-cache'); + if (ctx.request.body.fields) { if (ctx.request.body.fields.paste) { ctx.request.body.paste = ctx.request.body.fields.paste; } if (ctx.request.body.fields.highlight) { - ctx.request.body.highlight = ctx.request.body.fields.highlight; + ctx.request.body.highlight = ctx.request.body.fields.highlight.toLowerCase(); } if (ctx.request.body.fields.expire) { ctx.request.body.expire = ctx.request.body.fields.expire; @@ -49,7 +58,7 @@ module.exports = { let link = paste.id; - if (ctx.request.body.highlight) { + if (ctx.request.body.highlight && ctx.request.body.highlight != 'plain' && Object.keys(config.highlights).includes(ctx.request.body.highlight)) { link += '?' + ctx.request.body.highlight; }