Commit bd4af60add801576608a71b382ef16bf25c31b43

Baptiste 2024-08-12T13:48:54

httpd server: corrected some gdb errors

diff --git a/http/http_event.c b/http/http_event.c
index 4085655..1ad3647 100644
--- a/http/http_event.c
+++ b/http/http_event.c
@@ -12,6 +12,7 @@
  */
 #include <event2/event.h>
 #include <libkc3/kc3.h>
+#include <signal.h>
 #include "http_event.h"
 
 s32 http_event_add (struct event **ev, s_time *time)
diff --git a/http/socket_addr.c b/http/socket_addr.c
index e3612a8..031a4ba 100644
--- a/http/socket_addr.c
+++ b/http/socket_addr.c
@@ -16,9 +16,23 @@
 #include <netinet/in.h>
 #include <unistd.h>
 #include <libkc3/kc3.h>
+#include <arpa/inet.h>
 #include "socket.h"
 #include "socket_buf.h"
 
+s_str * socket_addr_to_str(s_str *str, const struct sockaddr *addr,
+                           s32 addr_len)
+{
+  char ip[INET6_ADDRSTRLEN];
+  assert(addr);
+  assert(addr_len);
+  if (!inet_ntop(addr->sa_family, &addr->sa_data[2], ip, addr_len)) {
+    err_puts("socket_addr_to_str: inet_ntop");
+    return str;
+  }
+  return str_init_1_alloc(str, ip);
+}
+
 void socket_addr_delete (struct sockaddr *sa)
 {
   assert(sa);
diff --git a/http/socket_addr.h b/http/socket_addr.h
index fee9024..b7ed654 100644
--- a/http/socket_addr.h
+++ b/http/socket_addr.h
@@ -16,6 +16,8 @@
 #include "types.h"
 
 /* Heap-allocation functions. */
+s_str * socket_addr_to_str(s_str *str, const struct sockaddr *addr,
+                           s32 addr_len);
 void              socket_addr_delete (struct sockaddr *sa);
 struct sockaddr * socket_addr_new (u32 len);
 struct sockaddr * socket_addr_new_copy (const struct sockaddr *addr,
diff --git a/http/socket_buf.c b/http/socket_buf.c
index b6133ac..bc2fc8b 100644
--- a/http/socket_buf.c
+++ b/http/socket_buf.c
@@ -67,6 +67,7 @@ s_socket_buf * socket_buf_init (s_socket_buf *sb, t_socket sockfd,
     assert(! "socket_buf_init: socket_addr_new_copy");
     return NULL;
   }
+  socket_addr_to_str(&tmp.addr_str, addr, addr_len);
   tmp.addr_len = addr_len;
   *sb = tmp;
   return sb;
diff --git a/http/types.h b/http/types.h
index f12d264..c2460f0 100644
--- a/http/types.h
+++ b/http/types.h
@@ -29,6 +29,7 @@ typedef t_socket *p_socket;
 struct socket_buf {
   struct sockaddr *addr;
   u32              addr_len;
+  s_str            addr_str;
   t_socket sockfd;
   s_buf_rw buf_rw;
 };
diff --git a/httpd/httpd.c b/httpd/httpd.c
index a72214c..38df504 100644
--- a/httpd/httpd.c
+++ b/httpd/httpd.c
@@ -10,6 +10,7 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
+#include <signal.h>
 #include <libkc3/kc3.h>
 #include "config.h"
 
@@ -21,6 +22,11 @@ int main (int argc, char **argv)
   s_tag tmp = {0};
   kc3_init(NULL, &argc, &argv);
   io_puts("KC3 HTTPd loading, please wait...");
+  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
+    err_puts("http_event_base_new: signal");
+    assert(! "http_event_base_new: signal");
+    return 1;
+  }
   module = sym_1("HTTPd");
   if (! module_load(module)) {
     kc3_clean(NULL);
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index 1a3badf..4dd2c49 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -19,7 +19,7 @@ defmodule HTTPd do
         res = router(req)
         r = HTTP.Response.buf_write(res, client.buf_rw.w,
           req.method != :head)
-        puts("#{res.code} #{res.message} #{req.method} #{req.url}")
+        puts("#{res.code} #{client.addr_str} #{res.message} #{req.method} #{req.url}")
       end
     end
   end
diff --git a/lib/kc3/0.1/socket/buf.kc3 b/lib/kc3/0.1/socket/buf.kc3
index e564aac..381582c 100644
--- a/lib/kc3/0.1/socket/buf.kc3
+++ b/lib/kc3/0.1/socket/buf.kc3
@@ -2,6 +2,7 @@ defmodule Socket.Buf do
 
   defstruct [addr: (Ptr) 0,
              addr_len: (U32) 0,
+             addr_str: "",
              sockfd: (S32) -1,
              buf_rw: %BufRW{}]