Commit f81200360d71be11c0868044c59a25b814fb57a4

Baptiste 2024-07-25T16:12:50

http testing (work in progress)

diff --git a/http/socket.c b/http/socket.c
index 9c9092c..161fde5 100644
--- a/http/socket.c
+++ b/http/socket.c
@@ -28,19 +28,21 @@ void socket_close (p_socket s)
 
 p_socket socket_init_accept (p_socket s, p_socket listening)
 {
-  struct sockaddr        *addr;
-  struct sockaddr_storage addr_storage = {0};
-  socklen_t               addr_len;
+  struct sockaddr   *addr;
+  struct sockaddr_in addr_in = {0};
+  socklen_t          addr_len;
   sw e;
   t_socket tmp;
   assert(s);
   assert(listening);
-  addr = (struct sockaddr *) &addr_storage;
-  addr_len = sizeof(addr_storage);
+  addr = (struct sockaddr *) &addr_in;
+  addr_len = sizeof(addr_in);
   tmp = accept(*listening, addr, &addr_len);
   if (tmp < 0) {
     e = errno;
-    err_write_1("socket_init_accept: accept: ");
+    err_write_1("socket_init_accept: ");
+    err_inspect_s32(listening);
+    err_write_1(": accept: ");
     err_puts(strerror(e));
     assert(! "socket_init_accept: accept");
     return NULL;
diff --git a/http/socket_buf.c b/http/socket_buf.c
index d76a97b..ac065b2 100644
--- a/http/socket_buf.c
+++ b/http/socket_buf.c
@@ -55,6 +55,38 @@ s_socket_buf * socket_buf_init (s_socket_buf *sb, t_socket sockfd,
   return sb;
 }
 
+s_socket_buf * socket_buf_init_accept (s_socket_buf *sb, p_socket listening)
+{
+  struct sockaddr        *addr;
+  struct sockaddr_storage addr_storage = {0};
+  socklen_t               addr_len;
+  sw e;
+  t_socket sockfd;
+  s_socket_buf tmp = {0};
+  assert(sb);
+  assert(listening);
+  addr = (struct sockaddr *) &addr_storage;
+  addr_len = sizeof(addr_storage);
+  sockfd = accept(*listening, addr, &addr_len);
+  if (sockfd < 0) {
+    e = errno;
+    err_write_1("socket_buf_init_accept: ");
+    err_inspect_s32(listening);
+    err_write_1(": accept: ");
+    err_puts(strerror(e));
+    assert(! "socket_buf_init_accept: accept");
+    return NULL;
+  }
+  if (! socket_buf_init(&tmp, sockfd, addr, addr_len)) {
+    err_puts("socket_buf_init_accept: socket_buf_init");
+    assert(! "socket_buf_init_accept: socket_buf_init");
+    close(sockfd);
+    return NULL;
+  }
+  *sb = tmp;
+  return sb;
+}
+
 s_socket_buf * socket_buf_init_connect (s_socket_buf *sb,
                                         const s_str *host,
                                         const s_str *service)
diff --git a/lib/kc3/0.1/socket.kc3 b/lib/kc3/0.1/socket.kc3
index a9c6719..886a02b 100644
--- a/lib/kc3/0.1/socket.kc3
+++ b/lib/kc3/0.1/socket.kc3
@@ -4,7 +4,7 @@ defmodule Socket do
 
   dlopen(__DIR__ + "http.so")
 
-  def accept = cfn Socket "socket_init_accept" (Result, Socket)
+  def accept = cfn Socket.Buf "socket_buf_init_accept" (Result, Socket)
 
   def close = cfn Void "socket_close" (Socket)
 
diff --git a/test/http/03_server.kc3 b/test/http/03_server.kc3
index f5dab13..165fac5 100644
--- a/test/http/03_server.kc3
+++ b/test/http/03_server.kc3
@@ -4,9 +4,9 @@ quote client = Socket.connect("localhost", "8000")
 client = Socket.connect("localhost", "8000")
 quote server_client = Socket.accept(server)
 server_client = Socket.accept(server)
-quote Socket.Buf.close(client)
-Socket.Buf.close(client)
+quote Socket.close(client)
+Socket.close(client)
 quote Socket.close(server_client)
 Socket.close(server_client)
 quote Socket.close(server)
-Socket.close(server)
+Socket.close(server)
\ No newline at end of file