From 861f3ab881bbe9608601527cb54639c5efaa571a Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 13 Oct 2022 14:02:37 -0700 Subject: [PATCH] Fix bug in /api/readFromArchive Well, two bugs: one with URL encoding, and another minor one where I forgot to return after ending the request with 404, oops lol :p --- pages/api/readFromArchive.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pages/api/readFromArchive.js b/pages/api/readFromArchive.js index 986cfd8..b535df9 100644 --- a/pages/api/readFromArchive.js +++ b/pages/api/readFromArchive.js @@ -47,7 +47,14 @@ async function handle(req, res) { // So basically, just the host, path, and query string; no protocol or hash. // (Most archive keys don't have a query string, but some do! And don't // worry, `parsedUrl.search` will just be empty if there's no query string.) - const archiveKey = `${parsedUrl.host}${parsedUrl.pathname}${parsedUrl.search}`; + // + // The archive key doesn't include URL encoding (e.g. it has " " instead of + // "%20"), but the URI object gives us encoded paths and queries for safety + // by default, so we need to decode them when constructing the key! + const archiveKey = + parsedUrl.host + + decodeURIComponent(parsedUrl.pathname) + + decodeURIComponent(parsedUrl.search); let archiveRes; try { archiveRes = await fetchFromArchive(archiveKey); @@ -55,6 +62,7 @@ async function handle(req, res) { if (error.Code === "NoSuchKey") { res.status(404); res.end(); + return; } res.end(`Error loading file from archive: ${error.message}`);