Commit 4d2f5348f9cb5d88d99ce1c09c08ddd85100c38a

Baptiste 2024-07-29T16:06:21

wip

diff --git a/http/http_request.c b/http/http_request.c
index 06fddd6..9e08e96 100644
--- a/http/http_request.c
+++ b/http/http_request.c
@@ -31,22 +31,31 @@ s_http_request * http_request_buf_parse (s_http_request *req, s_buf *buf)
   if (! tmp.method) {
     err_puts("http_request_buf_parse: no method");
     goto restore;
-  }  
+  }
+  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_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_inspect_str(&tmp.protocol);
+  err_write_1("\n");
   tail = &tmp.headers;
   while (1) {
     if (! buf_read_until_1_into_str(buf, "\r\n", &line)) {
       err_puts("http_request_buf_parse: invalid header");
       goto restore;
     }
-    if (line.size == 0)
+    if (strcmp(line.ptr.pchar, "\r\n") == 0)
       break;
     *tail = list_new(NULL);
     (*tail)->tag.type = TAG_STR;
diff --git a/lib/kc3/0.1/http/request.kc3 b/lib/kc3/0.1/http/request.kc3
index e59860e..c44c141 100644
--- a/lib/kc3/0.1/http/request.kc3
+++ b/lib/kc3/0.1/http/request.kc3
@@ -5,4 +5,6 @@ defmodule HTTP.Request do
              protocol: "HTTP/1.1",
              headers: []]
 
+  def buf_parse = cfn HTTP.Request "http_request_buf_parse" (Result, Buf)
+
 end
diff --git a/libkc3/buf.c b/libkc3/buf.c
index 01071b4..90d2fa5 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -1158,6 +1158,7 @@ sw buf_write_str (s_buf *buf, const s_str *src)
   }
   memcpy(buf->ptr.pu8 + buf->wpos, src->ptr.p, src->size);
   buf->wpos += src->size;
+  buf_flush(buf);
   return src->size;
 }
 
diff --git a/libkc3/buf_fd.c b/libkc3/buf_fd.c
index f483810..119b92c 100644
--- a/libkc3/buf_fd.c
+++ b/libkc3/buf_fd.c
@@ -14,6 +14,7 @@
 #include "assert.h"
 #include <errno.h>
 #include <string.h>
+#include <sys/ioctl.h>
 #include <unistd.h>
 #include "buf.h"
 #include "buf_fd.h"
@@ -50,6 +51,7 @@ s_buf * buf_fd_open_r (s_buf *buf, s32 fd)
 
 sw buf_fd_open_r_refill (s_buf *buf)
 {
+  int avail;
   s32 fd;
   uw r;
   uw size;
@@ -60,7 +62,13 @@ sw buf_fd_open_r_refill (s_buf *buf)
     return -1;
   size = buf->size - buf->wpos;
   fd = ((s_buf_fd *) (buf->user_ptr))->fd;
-  r = read(fd, buf->ptr.pchar + buf->wpos, size);
+  //r = read(fd, buf->ptr.pchar + buf->wpos, size);
+  if (ioctl(fd, FIONREAD, &avail) == -1 ||
+      avail < 0)
+    return -1;
+  if (avail > size)
+    avail = size;
+  r = read(fd, buf->ptr.pchar + buf->wpos, avail);
   if (buf->wpos + r > buf->size) {
     err_puts("buf_fd_open_r_refill: buffer overflow");
     assert(! "buf_fd_open_r_refill: buffer overflow");
diff --git a/test/http/04_server_request.kc3 b/test/http/04_server_request.kc3
index e750076..acac250 100644
--- a/test/http/04_server_request.kc3
+++ b/test/http/04_server_request.kc3
@@ -8,8 +8,10 @@ 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")
-Buf.write_str(client.buf_rw.w, "GET / HTTP/1.0\r\n\r\n")
+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 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)
 Socket.Buf.close(client)
 quote Socket.Buf.close(server_client)