« get me outta code hell

encode/decode URIs in html.tag('a') & live-dev-server - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-02-26 17:40:39 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-02-26 17:41:34 -0400
commit84081cc3b5287f925ffeeb94ae730e4c143b5f59 (patch)
tree41084dae05a8f3e38ab84315e36d6c160decdf09
parentf36f93b702729f14021746d56b192b25ac3ed1b7 (diff)
encode/decode URIs in html.tag('a') & live-dev-server
Fixes #147.
-rw-r--r--src/util/html.js8
-rw-r--r--src/write/build-modes/live-dev-server.js10
2 files changed, 17 insertions, 1 deletions
diff --git a/src/util/html.js b/src/util/html.js
index a6b0d621..459a1647 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -130,6 +130,14 @@ export function attributes(attribs) {
         throw new Error(`Attribute value for ${key} should be primitive or array, got ${typeof val}`);
     })
     .filter(([_key, _val, keep]) => keep)
+    .map(([key, val]) => {
+      switch (key) {
+        case 'href':
+          return [key, encodeURI(val)];
+        default:
+          return [key, val];
+      }
+    })
     .map(([key, val]) =>
       typeof val === 'boolean'
         ? `${key}`
diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js
index 39229a9a..a8fd3705 100644
--- a/src/write/build-modes/live-dev-server.js
+++ b/src/write/build-modes/live-dev-server.js
@@ -163,7 +163,15 @@ export async function go({
         localDirectory = mediaPath;
       }
 
-      const filePath = path.resolve(localDirectory, safePath.split('/').join(path.sep));
+      let filePath;
+      try {
+        filePath = path.resolve(localDirectory, decodeURI(safePath.split('/').join(path.sep)));
+      } catch (error) {
+        response.writeHead(404, contentTypePlain);
+        response.end(`No ${localFileArea} file found for: ${safePath}`);
+        console.log(`${requestHead} [404] ${pathname}`);
+        console.log(`Failed to decode request pathname`);
+      }
 
       try {
         await stat(filePath);