about summary refs log tree commit diff stats
path: root/src/types
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-12-26 23:50:12 +0100
committerbptato <nincsnevem662@gmail.com>2021-12-26 23:50:12 +0100
commit2b1dac71b2886ede9950f4ef91e7a0eb7b3e5ed5 (patch)
tree0971b3625f6cbc9d978ccb1a01bf7fde815dd94b /src/types
parent647089a99e3c44c4115274a8822ca0a4dd947d67 (diff)
downloadchawan-2b1dac71b2886ede9950f4ef91e7a0eb7b3e5ed5.tar.gz
Basic content type implementation
Diffstat (limited to 'src/types')
-rw-r--r--src/types/mime.nim25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/types/mime.nim b/src/types/mime.nim
new file mode 100644
index 00000000..4dfe87e9
--- /dev/null
+++ b/src/types/mime.nim
@@ -0,0 +1,25 @@
+import tables
+
+const DefaultGuess = [
+  ("html", "text/html"),
+  ("htm", "text/html"),
+  ("xhtml", "application/xhtml+xml"),
+  ("xhtm", "application/xhtml+xml"),
+  ("xht", "application/xhtml+xml"),
+].toTable()
+
+proc guessContentType*(path: string): string =
+  var i = path.len - 1
+  var n = 0
+  while i > 0:
+    if path[i] == '/':
+      return "text/plain"
+    if path[i] == '.':
+      n = i
+      break
+    dec i
+  if n > 0:
+    let ext = path.substr(n + 1)
+    if ext in DefaultGuess:
+      return DefaultGuess[ext]
+  return "text/plain"