about summary refs log tree commit diff stats
path: root/src/utils
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-12 19:51:03 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-12 20:20:24 +0100
commit22480a38c618aea42285b20868231f247932f2ab (patch)
treeba9b562124c1c30782ed87cd94122e962b80f637 /src/utils
parent232d861836993d81f7828a2917e8d242a23194e0 (diff)
downloadchawan-22480a38c618aea42285b20868231f247932f2ab.tar.gz
loader: remove applyHeaders
Better compute the values we need on-demand at the call sites; this way,
we can pass through content type attributes to mailcap too.

(Also, remove a bug where applyResponse was called twice.)
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mimeguess.nim19
-rw-r--r--src/utils/twtstr.nim21
2 files changed, 29 insertions, 11 deletions
diff --git a/src/utils/mimeguess.nim b/src/utils/mimeguess.nim
index 0a65c909..4b8df086 100644
--- a/src/utils/mimeguess.nim
+++ b/src/utils/mimeguess.nim
@@ -8,22 +8,19 @@ const DefaultGuess* = block:
   let ss = newStringStream(staticRead"res/mime.types")
   parseMimeTypes(ss)
 
-proc guessContentType*(path: string, fallback = "text/plain",
-    guess = DefaultGuess): string =
-  var i = path.len - 1
+func guessContentType*(mimeTypes: MimeTypes; path: string): string =
   var n = 0
-  while i > 0:
+  for i in countdown(path.high, 0):
     if path[i] == '/':
-      return fallback
+      break
     if path[i] == '.':
       n = i
       break
-    dec i
   if n > 0:
     let ext = path.substr(n + 1)
-    if ext in guess:
-      return guess[ext]
-  return fallback
+    if ext in mimeTypes:
+      return mimeTypes[ext]
+  return "application/octet-stream"
 
 const JavaScriptTypes = [
   "application/ecmascript",
@@ -44,5 +41,5 @@ const JavaScriptTypes = [
   "text/x-javascript"
 ]
 
-proc isJavaScriptType*(s: string): bool =
-  return binarySearch(JavaScriptTypes, s) != -1
+func isJavaScriptType*(s: string): bool =
+  return JavaScriptTypes.binarySearch(s) != -1
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 4d6b48ea..fe6ba236 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -693,3 +693,24 @@ func strictParseEnum*[T: enum](s: string): Opt[T] =
     if s in tab:
       return ok(tab[s])
   return err()
+
+proc getContentTypeAttr*(contentType, attrname: string): string =
+  let kvs = contentType.after(';')
+  var i = kvs.find(attrname)
+  var s = ""
+  if i != -1 and kvs.len > i + attrname.len and
+      kvs[i + attrname.len] == '=':
+    i += attrname.len + 1
+    while i < kvs.len and kvs[i] in AsciiWhitespace:
+      inc i
+    var q = false
+    for j, c in kvs.toOpenArray(i, kvs.high):
+      if q:
+        s &= c
+      elif c == '\\':
+        q = true
+      elif c == ';' or c in AsciiWhitespace:
+        break
+      else:
+        s &= c
+  return s