Commit 907410bc7effe7e0db24401e542e562a27ded8e8

Thomas de Grivel 2024-11-05T14:59:18

wip www

diff --git a/.ikc3_history b/.ikc3_history
index ea22d00..571864b 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,4 +1,3 @@
-quote %HTTP.Request{method: GET, url: "/plop", body: void}
 quote [a: 1, b: 2]
 [a: 1, b: 2]
 AList.to_map([a: 1, b: 2])
@@ -97,3 +96,5 @@ quote if true do if false do 1 else %KC3.Operator{sym: :-, symbol_value: 3} end 
 Crypt.sha512("Plop", "$6$rounds=123456$abc0123456789$")
 1 + 1
 1 + 10000000000000000000000000000000000000000000
+Time.from_str("1970-01-01 00:00:00")
+
diff --git a/http/types.h b/http/types.h
index eb55b61..262c52b 100644
--- a/http/types.h
+++ b/http/types.h
@@ -39,6 +39,7 @@ struct http_request {
   s_tag body;
   s_str url;
   s_str protocol;
+  s_str client_addr;
   s_list *headers;
   s_list *cookies;
   s_map params;
diff --git a/lib/kc3/0.1/http/request.kc3 b/lib/kc3/0.1/http/request.kc3
index 5955b3b..91a4baa 100644
--- a/lib/kc3/0.1/http/request.kc3
+++ b/lib/kc3/0.1/http/request.kc3
@@ -4,6 +4,7 @@ defmodule HTTP.Request do
              body: ?,
              url: "/",
              protocol: "HTTP/1.1",
+             client_addr: "",
              headers: [],
              cookies: [],
              params: %{}]
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index 839985d..e722acd 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -33,6 +33,14 @@ defmodule HTTPd do
       request = void
       request = HTTP.Request.buf_parse(client.buf_rw.r)
       if request do
+        client_addr = if client.addr_str != "127.0.0.1" do
+          client.addr_str
+        else
+          headers = request.headers
+          real_ip = headers["X-Real-IP"]
+          if (real_ip) do real_ip else client.addr_str end
+        end
+        request = Struct.put(request, :client_addr, client_addr)
         if (type(request.method) == Str) do
           response = error_405_page(request)
         else
@@ -49,13 +57,6 @@ defmodule HTTPd do
           "Accept-Ranges", "bytes")
         r = HTTP.Response.buf_write(response, client.buf_rw.w,
           request.method != HEAD)
-        client_addr = if client.addr_str != "127.0.0.1" do
-          client.addr_str
-        else
-          headers = request.headers
-          real_ip = headers["X-Real-IP"]
-          if (real_ip) do real_ip else client.addr_str end
-        end
         user_agent = HTTP.Request.header(request, "User-Agent")
         puts("#{response.code} #{client_addr} #{request.method} #{request.url} #{inspect(user_agent)}")
       else
diff --git a/lib/kc3/0.1/sym.facts b/lib/kc3/0.1/sym.facts
index a9c76f7..8ee4e8b 100644
--- a/lib/kc3/0.1/sym.facts
+++ b/lib/kc3/0.1/sym.facts
@@ -11,8 +11,8 @@ add {Sym, :symbol, Sym.anon_serial}
 replace {Sym.anon_serial, :symbol_value, 0}
 add {Sym, :symbol, Sym.anon}
 replace {Sym.anon, :symbol_value, Sym.fn (prefix) {
-  while Sym.find("#{prefix}-#{anon_serial}") do
+  while Sym.find("#{prefix}_#{anon_serial}") do
     def Sym.anon_serial = Sym.anon_serial + 1
   end
-  (Sym) "#{prefix}-#{anon_serial}"
+  (Sym) "#{prefix}_#{anon_serial}"
 }}
diff --git a/lib/kc3/0.1/time.kc3 b/lib/kc3/0.1/time.kc3
index 70b0a39..ce347f9 100644
--- a/lib/kc3/0.1/time.kc3
+++ b/lib/kc3/0.1/time.kc3
@@ -4,4 +4,6 @@ defmodule Time do
 
   def now = cfn Time "time_init_now" (Result)
 
+  def from_str = cfn Time "time_init_str" (Result, Str)
+
 end
diff --git a/libkc3/time.c b/libkc3/time.c
index d910fa1..ca961f5 100644
--- a/libkc3/time.c
+++ b/libkc3/time.c
@@ -99,6 +99,20 @@ s_time * time_init_now (s_time *time)
   return time;
 }
 
+s_time * time_init_str (s_time *time, const s_str *src)
+{
+  struct tm t = {0};
+  s_time tmp = {0};
+  sscanf(src->ptr.pchar, "%d-%d-%d %d:%d:%d",
+         &t.tm_year, &t.tm_mon, &t.tm_mday,
+         &t.tm_hour, &t.tm_min, &t.tm_sec);
+  t.tm_year -= 1900;
+  t.tm_mon -= 1;
+  tmp.tv_sec = timegm(&t);
+  *time = tmp;
+  return time;
+}
+
 s_timespec * time_sub (const s_timespec *a, const s_timespec *b, s_timespec *dest)
 {
   if ((a->tv_nsec - b->tv_nsec) < 0) {
diff --git a/libkc3/time.h b/libkc3/time.h
index e432e63..60944c5 100644
--- a/libkc3/time.h
+++ b/libkc3/time.h
@@ -27,6 +27,7 @@ s_time * time_init (s_time *time);
 s_time * time_init_add (s_time *time, const s_time *a, const s_time *b);
 s_time * time_init_copy (s_time *time, const s_time *src);
 s_time * time_init_now (s_time *time);
+s_time * time_init_str (s_time *time, const s_str *src);
 
 /* Observers */
 f64 *   time_to_f64 (const s_timespec *time, f64 *dest);