Commit 07e09314acd30cfcdb1ee95582292d6b5e96ae9a

Baptiste 2024-07-29T16:49:39

working fourth http test

diff --git a/ekc3/ekc3.c b/ekc3/ekc3.c
index 2fb63a6..c210b3e 100644
--- a/ekc3/ekc3.c
+++ b/ekc3/ekc3.c
@@ -16,19 +16,17 @@
 
 s_list *** ekc3_append_and_empty_buf (s_list ***tail, s_buf *buf)
 {
-  sw r;
   s_str str;
   assert(tail);
   assert(*tail);
   assert(! **tail);
   assert(buf);
-  r = buf_read_to_str(buf, &str);
-  if (r < 0) {
+  if (! buf_read_to_str(buf, &str)) {
     err_puts("ekc3_append_and_empty_buf: buf_read_to_str");
     assert(! "ekc3_append_and_empty_buf: buf_read_to_str");
     return NULL;
   }
-  if (! r) {
+  if (! str.size) {
     buf_empty(buf);
     return tail;
   }
diff --git a/http/http_request.c b/http/http_request.c
index 9e08e96..4da0df7 100644
--- a/http/http_request.c
+++ b/http/http_request.c
@@ -12,6 +12,7 @@
  */
 #include <libkc3/kc3.h>
 #include "http.h"
+#include <string.h>
 #include "socket.h"
 
 s_http_request * http_request_buf_parse (s_http_request *req, s_buf *buf)
@@ -32,21 +33,21 @@ s_http_request * http_request_buf_parse (s_http_request *req, s_buf *buf)
     err_puts("http_request_buf_parse: no method");
     goto restore;
   }
-  err_write_1("http_request_buf_parse: method: ")
+  err_write_1("http_request_buf_parse: method: ");
   err_inspect_sym(&tmp.method);
   err_write_1("\n");
   if (! buf_read_until_1_into_str(buf, " ", &tmp.url)) {
     err_puts("http_request_buf_parse: invalid URL");
     goto restore;
   }
-  err_write_1("http_request_buf_parse: url: ")
+  err_write_1("http_request_buf_parse: url: ");
   err_inspect_str(&tmp.url);
   err_write_1("\n");
   if (! buf_read_until_1_into_str(buf, "\r\n", &tmp.protocol)) {
     err_puts("http_request_buf_parse: invalid protocol");
     goto restore;
   }
-  err_write_1("http_request_buf_parse: protocol: ")
+  err_write_1("http_request_buf_parse: protocol: ");
   err_inspect_str(&tmp.protocol);
   err_write_1("\n");
   tail = &tmp.headers;
@@ -55,7 +56,7 @@ s_http_request * http_request_buf_parse (s_http_request *req, s_buf *buf)
       err_puts("http_request_buf_parse: invalid header");
       goto restore;
     }
-    if (strcmp(line.ptr.pchar, "\r\n") == 0)
+    if (line.size == 0)
       break;
     *tail = list_new(NULL);
     (*tail)->tag.type = TAG_STR;
diff --git a/libkc3/buf.c b/libkc3/buf.c
index 90d2fa5..3cc9c6b 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -726,27 +726,29 @@ sw buf_read_sym (s_buf *buf, const s_sym *src)
   return r;
 }
 
-sw buf_read_to_str (s_buf *buf, s_str *dest)
+s_str * buf_read_to_str (s_buf *buf, s_str *dest)
 {
   sw r;
   sw size;
   assert(buf);
   assert(dest);
   if (buf->rpos > buf->wpos)
-    return -1;
+    return NULL;
   if (buf->wpos > buf->size)
-    return -1;
+    return NULL;
   size = buf->wpos - buf->rpos;
   if (size == 0) {
     str_init_empty(dest);
-    return 0;
+    return dest;
   }
   if (! str_init_alloc(dest, size, buf->ptr.pchar + buf->rpos))
-    return -1;
+    return NULL;
   r = buf_ignore(buf, size);
-  if (r < 0)
+  if (r < 0) {
     str_clean(dest);
-  return r;
+    return NULL;
+  }
+  return dest;
 }
 
 sw buf_read_u8 (s_buf *buf, u8 *p)
@@ -804,9 +806,10 @@ s_str * buf_read_until_1_into_str(s_buf *buf, const char *end, s_str *dest)
     if (r) {
       buf_init(&tmp, false, buf->size, buf->ptr.pchar);
       tmp.rpos = save.rpos;
-      tmp.wpos = buf->rpos;
+      tmp.wpos = buf->rpos - strlen(end);
       if (! buf_read_to_str(&tmp, dest))
         goto restore;
+      buf_save_clean(buf, &save);
       return dest;
     }
     if ((r = buf_read_character_utf8(buf, &c)) <= 0)
diff --git a/libkc3/buf.h b/libkc3/buf.h
index 0d1519a..f52e664 100644
--- a/libkc3/buf.h
+++ b/libkc3/buf.h
@@ -75,7 +75,7 @@ sw      buf_read_s32 (s_buf *buf, s32 *p);
 sw      buf_read_s64 (s_buf *buf, s64 *p);
 sw      buf_read_str (s_buf *buf, const s_str *src);
 sw      buf_read_sym (s_buf *buf, const s_sym *src);
-sw      buf_read_to_str (s_buf *buf, s_str *dest);
+s_str * buf_read_to_str (s_buf *buf, s_str *dest);
 sw      buf_read_u8 (s_buf *buf, u8 *p);
 sw      buf_read_u16 (s_buf *buf, u16 *p);
 sw      buf_read_u32 (s_buf *buf, u32 *p);
diff --git a/libkc3/buf_fd.c b/libkc3/buf_fd.c
index 119b92c..3f76262 100644
--- a/libkc3/buf_fd.c
+++ b/libkc3/buf_fd.c
@@ -64,9 +64,9 @@ sw buf_fd_open_r_refill (s_buf *buf)
   fd = ((s_buf_fd *) (buf->user_ptr))->fd;
   //r = read(fd, buf->ptr.pchar + buf->wpos, size);
   if (ioctl(fd, FIONREAD, &avail) == -1 ||
-      avail < 0)
+      avail <= 0)
     return -1;
-  if (avail > size)
+  if ((uw) avail > size)
     avail = size;
   r = read(fd, buf->ptr.pchar + buf->wpos, avail);
   if (buf->wpos + r > buf->size) {
diff --git a/libkc3/str.c b/libkc3/str.c
index 866b16e..3824499 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -247,7 +247,7 @@ s_str * str_init_character (s_str *str, const character src)
   buf_init(&buf, false, sizeof(b), b);
   if (buf_write_character_utf8(&buf, src) < 0)
     return NULL;
-  if (buf_read_to_str(&buf, str) < 0)
+  if (! buf_read_to_str(&buf, str))
     return NULL;
   return str;
 }
diff --git a/test/http/04_server_request.kc3 b/test/http/04_server_request.kc3
index acac250..bdba64c 100644
--- a/test/http/04_server_request.kc3
+++ b/test/http/04_server_request.kc3
@@ -8,8 +8,8 @@ quote client.buf_rw
 client.buf_rw
 quote client.buf_rw.w
 client.buf_rw.w
-quote Buf.write_str(client.buf_rw.w, "GET / HTTP/1.0\r\n\r\n\r\n")
-Buf.write_str(client.buf_rw.w, "GET / HTTP/1.0\r\n\r\n\r\n")
+quote Buf.write_str(client.buf_rw.w, "GET / HTTP/1.0\r\n\r\n")
+Buf.write_str(client.buf_rw.w, "GET / HTTP/1.0\r\n\r\n")
 quote req = HTTP.Request.buf_parse(server_client.buf_rw.r)
 req = HTTP.Request.buf_parse(server_client.buf_rw.r)
 quote Socket.Buf.close(client)