Commit 9534181b8fd744db6a5d788c01938aca8a07d17e

Baptiste 2024-08-11T21:12:25

wip httpd libevent2

diff --git a/.ikc3_history b/.ikc3_history
index 97d336e..39f7287 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,9 +1,5 @@
 Struct.offset(%HTTP.Response{}, :code)
 Struct.offset(%HTTP.Response{}, :message)
-Struct.offset(%HTTP.Response{}, :response)
-Struct.offset(%HTTP.Response{}, :protocol)
-Struct.offset(%HTTP.Response{}, :code)
-Struct.offset(%HTTP.Response{}, :message)
 Struct.offset(%HTTP.Response{}, :headers)
 Struct.offset(%HTTP.Response{}, :body)
 Struct.offset(%HTTP.Response{}, :message)
@@ -97,3 +93,8 @@ quote if true do if true do 1 else 2 end else if true do 3 else 4 end end
 Fact.subject(%Fact{})
 Facts.env_facts
 Fact.subject(%Fact{})
+%Socket.Buf{}
+quote quote quote %Socket.Buf{}
+quote quote quote %Socket.Buf{sockfd: (S32) 1}
+"#{%Socket.Buf{}}"
+
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index 78f79e3..aa0d3eb 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -8,21 +8,46 @@ defmodule HTTPd do
   require HTTP.Event
   require HTTP.Request
   require HTTP.Response
-  
+
   def root_dir = "./public"
 
+  def http_client = fn (socket, events, client_ev, client) do
+    puts("echo_client")
+    if List.has?(events, :read) do
+      server_client(client)
+    end
+  end
+
+  def acceptor = fn (server_socket, events, acceptor_ev, void) do
+    if List.has?(events, :read) do
+      puts("acceptor: read")
+      client = Socket.Buf.accept(%Socket{fd: server_socket})
+      puts("acceptor: got client")
+      client_ev = HTTP.Event.new(event_base, client.sockfd, [:read, :persist],
+        http_client, client)
+      r = HTTP.Event.add(client_ev, timeout)
+      puts("HTTP.Event.add: #{r}")
+    end
+  end
+
   def server = fn (host, port) {
+    timeout = %Time{tv_sec: (Sw) 30}
+    event_base = HTTP.Event.base_new()
     socket = Socket.listen(host, port)
     puts("KC3 HTTPd: listening on #{host}:#{port}")
-    acceptor = HTTPd.fn () {
-      while true do
-        client = Socket.Buf.accept(socket)
-        server_client(client)
-        Socket.Buf.close(client)
-      end
-    }
-    acceptor();
-    Socket.close(socket)
+    puts("socket.fd: #{socket.fd}")
+    puts("event_base: #{event_base}")
+    acceptor_ev = HTTP.Event.new(event_base, socket.fd, [:read, :persist],
+                                 acceptor, void)
+    puts("acceptor_ev: #{acceptor_ev}")
+    r = HTTP.Event.add(acceptor_ev, timeout)
+    puts("HTTP.Event.add: #{r}")
+    r = HTTP.Event.dispatch(event_base)
+    if r do
+      e = errno()
+      puts("HTTP.Event.dispatch: #{r}: #{strerror(e)}")
+    end
+    Socket.close(%Socket{fd: socket})
   }
 
   def server_client = fn (client) {
@@ -106,7 +131,7 @@ defmodule HTTPd do
   <head>
     <title>Index of #{request.url}</title>
     <style type="text/css">
-      ul { font-family: monospace; }        
+      ul { font-family: monospace; }
     </style>
   </head>
   <body>
diff --git a/test/http/07_event_echo.kc3 b/test/http/07_event_echo.kc3
index 7421de4..c807f61 100644
--- a/test/http/07_event_echo.kc3
+++ b/test/http/07_event_echo.kc3
@@ -33,8 +33,8 @@ acceptor_ev = HTTP.Event.new(event_base, socket.fd, [:read, :persist],
 puts("acceptor_ev: #{acceptor_ev}")
 r = HTTP.Event.add(acceptor_ev, timeout)
 puts("HTTP.Event.add: #{r}")
-quote r = HTTP.Event.dispatch(event_base)
-quote if r do
-  e = errno()
-  puts("HTTP.Event.dispatch: #{r}: #{strerror(e)}")
-end
+# r = HTTP.Event.dispatch(event_base)
+# if r do
+#  e = errno()
+#  puts("HTTP.Event.dispatch: #{r}: #{strerror(e)}")
+# end
diff --git a/test/ikc3/str.kc3 b/test/ikc3/str.kc3
index f3e8369..fd08880 100644
--- a/test/ikc3/str.kc3
+++ b/test/ikc3/str.kc3
@@ -76,3 +76,5 @@ quote "#{%BufRW{}}"
 "#{%BufRW{}}"
 quote "#{(U8[]) {{0, 0}, {0, 0}, {0, 0}}}"
 "#{(U8[]) {{0, 0}, {0, 0}, {0, 0}}}"
+quote "#{%Socket.Buf{}}"
+"#{%Socket.Buf{}}"