Skip to content

Commit

Permalink
properly handle 404s (fix #3, thanks Jake!)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpj committed Jun 19, 2022
1 parent c7d8223 commit 50a1f85
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ WORKDIR /usr/src/spring83
COPY package*.json ./
RUN npm install
COPY common.js .
COPY root.tmpl.html .
COPY *.tmpl.html .
COPY serve .
COPY public-boards.json .
STOPSIGNAL SIGINT
Expand Down
1 change: 1 addition & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const constants = Object.freeze({
version: 'spring-version'
},
rootTemplateName: 'root.tmpl.html',
notFoundTmplName: '404.tmpl.html',
defaultContentPath: '.content',
defaultFQDN: 'example.com',
strictVerification: true,
Expand Down
12 changes: 9 additions & 3 deletions serve
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ async function refreshPubBoards () {

async function main () {
const rootTmpl = (await fs.promises.readFile(path.join(__dirname, constants.rootTemplateName))).toString('utf8');
const notFoundTmpl = (await fs.promises.readFile(path.join(__dirname, constants.notFoundTmplName))).toString('utf8');
const contentDir = path.resolve(process.env.SPRING83_CONTENT_DIR || path.join(__dirname, constants.defaultContentPath));
const fqdn = process.env.SPRING83_FQDN || constants.defaultFQDN;
const contactAddr = process.env.SPRING83_CONTACT_ADDR;
Expand Down Expand Up @@ -221,16 +222,22 @@ async function main () {
});

app.get('/:key', async (req, reply) => {
const renderMap = {};
const render = () => mustache.render(notFoundTmpl, renderMap);

reply.code(404);
applyGenericGETReplyHeaders(reply);

if (!pubKeyHexIsValid(req.params.key, constants.strictVerification)) {
app.log.warn('invalid key');
return;
return render();
}

renderMap.key = req.params.key;

if (!knownKeys[req.params.key]) {
app.log.warn('invalid content');
return;
return render();
}

const { body, metadata: { headers } } = knownKeys[req.params.key];
Expand All @@ -248,7 +255,6 @@ async function main () {
reply.code(200);
reply.header(constants.headerNames.signature, sig);
reply.header('last-modified', new Date(headers['if-unmodified-since']).toUTCString());
applyGenericGETReplyHeaders(reply);
return body;
});

Expand Down

0 comments on commit 50a1f85

Please sign in to comment.