Commit 41968fd73de498c3c1ca0a69346b5c249df65d23

Baptiste 2024-08-11T20:13:32

working 07 http test

diff --git a/http/http_event.c b/http/http_event.c
index 93b4055..4085655 100644
--- a/http/http_event.c
+++ b/http/http_event.c
@@ -14,12 +14,17 @@
 #include <libkc3/kc3.h>
 #include "http_event.h"
 
-s32 http_event_add (struct event *ev, s_time *time)
+s32 http_event_add (struct event **ev, s_time *time)
 {
   struct timeval tv;
   tv.tv_sec = time->tv_sec;
   tv.tv_usec = time->tv_nsec / 1000;
-  return event_add(ev, &tv);
+  return event_add(*ev, &tv);
+}
+
+s32 http_event_del (struct event **ev)
+{
+  return event_del(*ev);
 }
 
 /* http_event_callback
@@ -70,9 +75,9 @@ void http_event_callback (int fd, short events, void *tag_tuple)
   tag_clean(&tmp);
 }
 
-s32 http_event_base_dispatch (struct event_base *eb)
+s32 http_event_base_dispatch (struct event_base **eb)
 {
-  return event_base_dispatch(eb);
+  return event_base_dispatch(*eb);
 }
 
 struct event_base * http_event_base_new (void)
@@ -80,7 +85,7 @@ struct event_base * http_event_base_new (void)
   return event_base_new();
 }
 
-struct event * http_event_new (struct event_base *event_base, s32 fd,
+struct event * http_event_new (struct event_base **event_base, s32 fd,
                                const s_list * const *events,
                                const s_fn *callback, s_tag *arg)
 {
@@ -104,7 +109,7 @@ struct event * http_event_new (struct event_base *event_base, s32 fd,
     e = list_next(e);
   }
   tag = tag_new_tuple(3);
-  ev = event_new(event_base, fd, events_s16, http_event_callback, tag);
+  ev = event_new(*event_base, fd, events_s16, http_event_callback, tag);
   if (! ev) {
     tag_delete(tag);
     err_puts("http_event_new: event_new");
diff --git a/http/http_event.h b/http/http_event.h
index 9848c0d..1061e05 100644
--- a/http/http_event.h
+++ b/http/http_event.h
@@ -18,12 +18,12 @@
 /* Heap-allocation functions, call *_delete after use. */
 struct event_base * http_event_base_new (void);
 void                http_event_delete (struct event *ev);
-struct event *      http_event_new (struct event_base *event_base,
+struct event *      http_event_new (struct event_base **event_base,
                                     s32 fd,
                                     const s_list * const *events,
                                     const s_fn *callback, s_tag *arg);
 
 /* Operators. */
-s32  http_event_add (struct event *ev, s_time *time);
+s32  http_event_add (struct event **ev, s_time *time);
 
 #endif /* HTTP_H */
diff --git a/lib/kc3/0.1/buf.kc3 b/lib/kc3/0.1/buf.kc3
index d5cec08..0d6c826 100644
--- a/lib/kc3/0.1/buf.kc3
+++ b/lib/kc3/0.1/buf.kc3
@@ -20,6 +20,15 @@ defmodule Buf do
 
   def parse_tag = cfn Tag "kc3_buf_parse_tag" (Buf, Result)
 
+  def read_to_str = cfn Str "buf_read_to_str" (Buf, Result)
+
+  def read_until_str_into_str = cfn Str "buf_read_until_str_into_str" (
+    Buf, Str, Result)
+
+  def refill = cfn Sw "buf_refill" (Buf)
+
   def write_str = cfn Sw "buf_write_str" (Buf, Str)
 
+  def xfer = cfn Sw "buf_xfer" (Buf, Buf, Uw)
+
 end
diff --git a/lib/kc3/0.1/http/event.kc3 b/lib/kc3/0.1/http/event.kc3
index 5cacccc..8d17e0f 100644
--- a/lib/kc3/0.1/http/event.kc3
+++ b/lib/kc3/0.1/http/event.kc3
@@ -4,9 +4,9 @@ defmodule HTTP.Event do
 
   def add = cfn S32 "http_event_add" (Ptr, Time)
 
-  def del = cfn S32 "event_del" (Ptr)
+  def del = cfn S32 "http_event_del" (Ptr)
 
-  def dispatch = cfn S32 "event_base_dispatch" (Ptr)
+  def dispatch = cfn S32 "http_event_base_dispatch" (Ptr)
 
   # base_new() returns a (struct event_base *)
   def base_new = cfn Ptr "event_base_new" ()
diff --git a/libkc3/buf.c b/libkc3/buf.c
index c65d3c5..568138e 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -871,15 +871,13 @@ s_str * buf_read_until_1_into_str(s_buf *buf, const char *end, s_str *dest)
   return NULL;
 }
 
-sw buf_read_until_character_into_str (s_buf *buf, character end, s_str *dest)
+s_str * buf_read_until_character_into_str (s_buf *buf, character end, s_str *dest)
 {
   s_str end_str;
-  sw r;
-  s_str tmp;
+  s_str *r;
   if (! str_init_character(&end_str, end))
-    return -1;
-  if ((r = buf_read_until_str_into_str(buf, &end_str, &tmp)) > 0)
-    *dest = tmp;
+    return NULL;
+  r = buf_read_until_str_into_str(buf, &end_str, dest);
   str_clean(&end_str);
   return r;
 }
@@ -917,11 +915,10 @@ sw buf_read_until_space_into_str (s_buf *buf, s_str *dest)
   return r;
 }
 
-sw buf_read_until_str_into_str (s_buf *buf, const s_str *end, s_str *dest)
+s_str * buf_read_until_str_into_str (s_buf *buf, const s_str *end, s_str *dest)
 {
   character c;
   sw r;
-  sw result = 0;
   s_buf_save save;
   s_buf tmp;
   buf_save_init(buf, &save);
@@ -929,7 +926,6 @@ sw buf_read_until_str_into_str (s_buf *buf, const s_str *end, s_str *dest)
     if ((r = buf_read_str(buf, end)) < 0)
       goto restore;
     if (r) {
-      result += r;
       buf_init(&tmp, false, buf->size, buf->ptr.pchar);
       tmp.rpos = save.rpos;
       tmp.wpos = buf->rpos;
@@ -937,18 +933,16 @@ sw buf_read_until_str_into_str (s_buf *buf, const s_str *end, s_str *dest)
         r = -1;
         goto restore;
       }
-      r = result;
-      goto clean;
+      buf_save_clean(buf, &save);
+      return dest;
     }
     if ((r = buf_read_character_utf8(buf, &c)) <= 0)
       goto restore;
-    result += r;
   }
  restore:
   buf_save_restore_rpos(buf, &save);
- clean:
   buf_save_clean(buf, &save);
-  return r;
+  return NULL;
 }
 
 sw buf_refill (s_buf *buf, sw size)
diff --git a/libkc3/buf.h b/libkc3/buf.h
index db96769..57f6ca1 100644
--- a/libkc3/buf.h
+++ b/libkc3/buf.h
@@ -85,10 +85,10 @@ sw      buf_read_u32 (s_buf *buf, u32 *p);
 sw      buf_read_u64 (s_buf *buf, u64 *p);
 s_str * buf_read_until_1_into_str(s_buf *buf, const char *end,
                                   s_str *dest);
-sw      buf_read_until_character_into_str (s_buf *buf, character end,
+s_str * buf_read_until_character_into_str (s_buf *buf, character end,
                                            s_str *dest);
 sw      buf_read_until_space_into_str (s_buf *buf, s_str *dest);
-sw      buf_read_until_str_into_str (s_buf *buf, const s_str *end,
+s_str * buf_read_until_str_into_str (s_buf *buf, const s_str *end,
                                      s_str *dest);
 sw      buf_read_integer (s_buf *buf, s_integer *dst);
 sw      buf_refill (s_buf *buf, sw size);
diff --git a/libkc3/str.c b/libkc3/str.c
index b5b8a99..1f80a82 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -97,6 +97,43 @@
     return buf_to_str(&buf, str);                                      \
   }
 
+#define DEF_STR_INIT_PTR(name, type)                                       \
+  s_str * str_init_ ## name (s_str *str, type x)                       \
+  {                                                                    \
+    s_buf buf;                                                         \
+    s_pretty pretty = {0};                                             \
+    sw r;                                                              \
+    sw size;                                                           \
+    size = buf_inspect_ ## name ## _size(&pretty, x);                 \
+    if (! size)                                                        \
+      return str_init_empty(str);                                      \
+    if (size < 0) {                                                    \
+      err_puts("str_init_" # name ": buf_inspect_" # name              \
+               "_size < 0");                                           \
+      return NULL;                                                     \
+    }                                                                  \
+    if (! buf_init_alloc(&buf, size)) {                                \
+      err_puts("str_init_" # name ": buf_init_alloc");                 \
+      return NULL;                                                     \
+    }                                                                  \
+    if ((r = buf_inspect_ ## name(&buf, x)) < 0) {                    \
+      err_puts("str_init_" # name ": buf_inspect_" # name " < 0");     \
+      buf_clean(&buf);                                                 \
+      return NULL;                                                     \
+    }                                                                  \
+    if (r != size) {                                                   \
+      err_write_1("str_init_" # name ": buf_inspect_" # name ": ");    \
+      err_inspect_sw_decimal(&r);                                      \
+      err_write_1(" != ");                                             \
+      err_inspect_sw_decimal(&size);                                   \
+      err_write_1("\n");                                               \
+      buf_clean(&buf);                                                 \
+      return NULL;                                                     \
+    }                                                                  \
+    assert(buf.wpos == (uw) size);                                     \
+    return buf_to_str(&buf, str);                                      \
+  }
+
 #define DEF_STR_INIT_STRUCT(name)                                      \
   s_str * str_init_ ## name (s_str *str, const s_ ## name *x)          \
   {                                                                    \
@@ -306,12 +343,14 @@ s_str * str_init_cast (s_str *str, const s_sym * const *type,
     return str_init_character(str, tag->data.character);
   case TAG_FN:
     return str_init_fn(str, &tag->data.fn);
+  case TAG_LIST:
+    return str_init_list(str, (const s_list * const *) &tag->data.list);
   case TAG_MAP:
     return str_init_map(str, &tag->data.map);
   case TAG_PTR:
-    return str_init_ptr(str, tag->data.ptr);
+    return str_init_ptr(str, &tag->data.ptr);
   case TAG_PTR_FREE:
-    return str_init_ptr_free(str, tag->data.ptr_free);
+    return str_init_ptr_free(str, &tag->data.ptr_free);
   case TAG_S8:
     return str_init_s8(str, tag->data.s8);
   case TAG_S16:
@@ -481,9 +520,10 @@ s_str * str_init_f (s_str *str, const char *fmt, ...)
 }
 
 DEF_STR_INIT_STRUCT(fn)
+DEF_STR_INIT_PTR(list, const s_list * const *)
 DEF_STR_INIT_STRUCT(map)
-DEF_STR_INIT(ptr, u_ptr_w)
-DEF_STR_INIT(ptr_free, u_ptr_w)
+DEF_STR_INIT_PTR(ptr, const u_ptr_w *)
+DEF_STR_INIT_PTR(ptr_free, const u_ptr_w *)
 DEF_STR_INIT_INT(s8)
 DEF_STR_INIT_INT(s16)
 DEF_STR_INIT_INT(s32)
diff --git a/libkc3/str.h b/libkc3/str.h
index e7c4097..a9b6c6b 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -55,9 +55,10 @@ s_str * str_init_copy_1 (s_str *str, const char *p);
 s_str * str_init_empty (s_str *str);
 s_str * str_init_f (s_str *str, const char *fmt, ...);
 PROTOTYPE_STR_INIT_STRUCT(fn);
+PROTOTYPE_STR_INIT(list, const s_list * const *);
 PROTOTYPE_STR_INIT_STRUCT(map);
-PROTOTYPE_STR_INIT(ptr, u_ptr_w);
-PROTOTYPE_STR_INIT(ptr_free, u_ptr_w);
+PROTOTYPE_STR_INIT(ptr, const u_ptr_w *);
+PROTOTYPE_STR_INIT(ptr_free, const u_ptr_w *);
 PROTOTYPE_STR_INIT_INT(s8);
 PROTOTYPE_STR_INIT_INT(s16);
 PROTOTYPE_STR_INIT_INT(s32);