Commit 989518e853f2c721a9c6bd69d51543c057898bb9

Thomas de Grivel 2024-08-07T14:43:18

HTTP.mime_type

diff --git a/.ikc3_history b/.ikc3_history
index 01f1a6e..a8d91ab 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,11 +1,3 @@
-[fd: (S32) -1]
-load("https://git.kmx.io/kc3-lang/kc3/
-)
-load("lib/kc3/0.1/s32.facts")
-(S32) 1
-(S32) -1
-[fd: (S32) -1]
-defmodule Socket do defstruct [fd: (S32) -1]; end
 defmodule Socket do
 dlopen("lib/kc3/0.1/http.so")
 def connect = cfn Socket.Buf "socket_buf_init_connect" (Result, Str, Str)
@@ -97,3 +89,11 @@ Str.ends_with?("abc", "b")
 Str.ends_with?("abc", "c")
 Str.ends_with?("abc/", "/")
 File.read("env")
+HTTP.mime_type_load("test/httpd/mime.types")
+HTTP.mime_type("txt")
+HTTP.mime_type("html")
+HTTP.mime_type("zorglub")
+HTTP.mime_type_load("test/httpd/mime.types")
+HTTP.mime_type("zorglub")
+HTTP.mime_type_def(:zorglub, :application/zorglub)
+HTTP.mime_type("zorglub")
diff --git a/http/mime_type.c b/http/mime_type.c
index 52473ef..b291d20 100644
--- a/http/mime_type.c
+++ b/http/mime_type.c
@@ -13,6 +13,34 @@
 #include <libkc3/kc3.h>
 #include "mime_type.h"
 
+const s_sym ** http_mime_type (const s_str *ext, const s_sym **dest)
+{
+  s_facts_cursor cursor;
+  const s_sym *default_mime_type;
+  const s_fact *fact = NULL;
+  s_tag tag_ext = {0};
+  s_tag tag_mime_type_sym;
+  s_tag tag_mime_type_value;
+  if ((tag_ext.data.sym = sym_find(ext)))
+    tag_ext.type = TAG_SYM;
+  tag_init_sym(&tag_mime_type_sym, sym_1("mime_type"));
+  tag_init_var(&tag_mime_type_value, &g_sym_Sym);
+  default_mime_type = sym_1("application/octet-stream");
+  if (! facts_with_tags(&g_kc3_env.facts, &cursor, &tag_ext,
+                        &tag_mime_type_sym, &tag_mime_type_value))
+    goto default_mime_type;
+  if (! facts_cursor_next(&cursor, &fact) ||
+      ! fact ||
+      tag_mime_type_value.type != TAG_SYM ||
+      ! tag_mime_type_value.data.sym)
+    goto default_mime_type;
+  *dest = tag_mime_type_value.data.sym;
+  return dest;
+ default_mime_type:
+  *dest = default_mime_type;
+  return dest;
+}
+
 bool http_mime_type_buf_parse (s_buf *buf)
 {
   sw r;
@@ -40,7 +68,7 @@ bool http_mime_type_buf_parse (s_buf *buf)
 bool http_mime_type_buf_parse_type (s_buf *buf)
 {
   character c;
-  const s_sym *ext;
+  s_tag ext;
   sw r;
   sw result = 0;
   s_buf_save save;
@@ -76,8 +104,8 @@ bool http_mime_type_buf_parse_type (s_buf *buf)
     }
     buf_save_clean(buf, &save);
     if (r > 0) {
-      ext = str_to_sym(&str);
-      if (! http_mime_type_def(type, ext))
+      tag_init_sym(&ext, str_to_sym(&str));
+      if (! http_mime_type_def(&ext, &type))
         return false;
     }
     if ((r = buf_read_1(buf, ";")) < 0)
@@ -92,18 +120,18 @@ bool http_mime_type_buf_parse_type (s_buf *buf)
   return false;
 }
 
-bool http_mime_type_def (const s_sym *mime_type, const s_sym *ext)
+bool http_mime_type_def (const s_tag *ext,
+                         const s_sym * const *mime_type)
 {
-  s_tag tag_ext;
-  s_tag tag_mime_type;
+  s_tag tag_mime_type_sym;
   s_tag tag_mime_type_value;
-  assert(mime_type);
   assert(ext);
-  tag_init_sym(&tag_ext, ext);
-  tag_init_sym(&tag_mime_type, sym_1("mime_type"));
-  tag_init_sym(&tag_mime_type_value, mime_type);
-  if (! facts_add_tags(&g_kc3_env.facts, &tag_ext, &tag_mime_type,
-                       &tag_mime_type_value))
+  assert(mime_type);
+  tag_init_sym(&tag_mime_type_sym, sym_1("mime_type"));
+  tag_init_sym(&tag_mime_type_value, *mime_type);
+  if (! facts_replace_tags(&g_kc3_env.facts, ext,
+                           &tag_mime_type_sym,
+                           &tag_mime_type_value))
     return false;
   return true;
 }
diff --git a/http/mime_type.h b/http/mime_type.h
index 9db130d..c4fa7e2 100644
--- a/http/mime_type.h
+++ b/http/mime_type.h
@@ -17,7 +17,8 @@
 
 bool http_mime_type_buf_parse (s_buf *buf);
 bool http_mime_type_buf_parse_type (s_buf *buf);
-bool http_mime_type_def (const s_sym *mime_type, const s_sym *ext);
+bool http_mime_type_def (const s_tag *ext,
+                         const s_sym * const *mime_type);
 bool http_mime_type_load (s_str *path);
 
 #endif /* HTTP_MIME_TYPE_H */
diff --git a/lib/kc3/0.1/http.kc3 b/lib/kc3/0.1/http.kc3
index 5858913..cc54a33 100644
--- a/lib/kc3/0.1/http.kc3
+++ b/lib/kc3/0.1/http.kc3
@@ -2,6 +2,10 @@ defmodule HTTP do
 
   dlopen(__DIR__ + "http.so")
 
+  def mime_type = cfn Sym "http_mime_type" (Str, Result)
+
+  def mime_type_def = cfn Bool "http_mime_type_def" (Tag, Sym)
+
   def mime_type_load = cfn Bool "http_mime_type_load" (Str)
 
 end
diff --git a/lib/kc3/0.1/sym.facts b/lib/kc3/0.1/sym.facts
index a375c07..79c7ebf 100644
--- a/lib/kc3/0.1/sym.facts
+++ b/lib/kc3/0.1/sym.facts
@@ -5,3 +5,5 @@ replace {Sym, :symbol, Sym.cast}
 replace {Sym.cast, :symbol_value, cfn Sym "sym_init_cast" (Result, Sym, Tag)}
 add {Sym, :symbol, Sym.type_size}
 replace {Sym.type_size, :symbol_value, cfn Uw "sym_type_size" (Sym, Result)}
+add {Sym, :symbol, Sym.find}
+replace {Sym.find, :symbol_value, cfn Tag "sym_find_to_tag" (Str, Result)}
diff --git a/libkc3/sym.c b/libkc3/sym.c
index 4e15299..0182b99 100644
--- a/libkc3/sym.c
+++ b/libkc3/sym.c
@@ -222,6 +222,16 @@ const s_sym * sym_find (const s_str *str)
   return NULL;
 }
 
+s_tag * sym_find_to_tag (const s_str *src, s_tag *dest)
+{
+  s_tag tmp = {0};
+  tmp.data.sym = sym_find(src);
+  if (tmp.data.sym)
+    tmp.type = TAG_SYM;
+  *dest = tmp;
+  return dest;
+}
+
 bool sym_has_ident_reserved_characters (const s_sym *sym)
 {
   character c;
diff --git a/libkc3/sym.h b/libkc3/sym.h
index 37a7af2..657c012 100644
--- a/libkc3/sym.h
+++ b/libkc3/sym.h
@@ -137,6 +137,7 @@ const s_sym * sym_new (const s_str *src);
 const s_sym * sym_array_type (const s_sym *sym);
 bool          sym_character_is_reserved (character c);
 const s_sym * sym_find (const s_str *src);
+s_tag *       sym_find_to_tag (const s_str *src, s_tag *dest);
 bool          sym_has_ident_reserved_characters (const s_sym *sym);
 bool          sym_has_reserved_characters (const s_sym *sym);
 bool          sym_is_array_type (const s_sym *sym);