Commit f4ab2197216a749250a75ba2bbcfe19920c2d0af

Baptiste 2024-07-29T18:50:26

wip

diff --git a/http/http.c b/http/http.c
index 5f06dc1..60312c5 100644
--- a/http/http.c
+++ b/http/http.c
@@ -13,3 +13,34 @@
 #include <libkc3/kc3.h>
 #include "http.h"
 #include "socket.h"
+
+s_tag * http_header_split (s_str *header, s_tag *dest)
+{
+  assert(header);
+  assert(dest);
+  s_tag *key;
+  sw sep;
+  s_tag tmp;
+  s_tag *value;
+  if ((sep = str_position_1(header, ": ")) < 0) {
+    err_puts("http_header_split: missing separator");
+    return NULL;
+  }
+  tag_init_tuple(&tmp, 2);
+  key = tmp.data.tuple.tag;
+  key->type = TAG_STR;
+  if (! str_init_slice(&key->data.str, header, 0, sep)) {
+    err_puts("http_header_split: str_init_slice 1");
+    assert(! "http_header_split: str_init_slice 1");
+    return NULL;
+  }
+  value = tmp.data.tuple.tag + 1;
+  value->type = TAG_STR;
+  if (! str_init_slice(&value->data.str, header, sep + 2, -1)) {
+    err_puts("http_header_split: str_init_slice 2");
+    assert(! "http_header_split: str_init_slice 2");
+    return NULL;
+  }
+  *dest = tmp;
+  return dest;
+}
diff --git a/http/http.h b/http/http.h
index 2a16bfd..d5907c5 100644
--- a/http/http.h
+++ b/http/http.h
@@ -15,6 +15,7 @@
 
 #include "types.h"
 
+s_tag *          http_header_split (s_str *header, s_tag *dest);
 s_http_request * http_request_buf_parse (s_http_request *req, s_buf *buf);
 
 
diff --git a/http/http_request.c b/http/http_request.c
index 4da0df7..eda1eac 100644
--- a/http/http_request.c
+++ b/http/http_request.c
@@ -60,7 +60,8 @@ s_http_request * http_request_buf_parse (s_http_request *req, s_buf *buf)
       break;
     *tail = list_new(NULL);
     (*tail)->tag.type = TAG_STR;
-    (*tail)->tag.data.str = line;
+    if (! http_header_split(&line, &(*tail)->tag))
+      goto restore;
     tail = &(*tail)->next.data.list;
   }
   buf_save_clean(buf, &save);
diff --git a/libkc3/str.c b/libkc3/str.c
index 3824499..f77638c 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -633,6 +633,28 @@ sw str_peek_character_utf8 (const s_str *str, character *c)
   return -1;
 }
 
+sw str_position_1 (const s_str *str, const char *token)
+{
+  character c;
+  uw len;
+  sw r;
+  sw result = 0;
+  s_str s;
+  assert(str);
+  assert(token);
+  len = strlen(token);
+  s = *str;
+  while (1) {
+    if (s.size < len)
+      return -1;
+    if (! memcmp(s.ptr.pchar, token, len))
+      return result;
+    if ((r = str_read_character_utf8(&s, &c)) < 0)
+      return -1;
+    result += r;
+  }
+}
+
 sw str_read_u8 (s_str *str, u8 *c)
 {
   if (str->size <= 0)
@@ -692,7 +714,7 @@ uw * str_sw_pos_to_uw (sw pos, uw max_pos, uw *dest)
   }
   else {
     if (max_pos > SW_MAX || pos >= (sw) -max_pos)
-      *dest = max_pos - pos;
+      *dest = max_pos - pos + 1;
     else {
       err_write_1("str_sw_pos_to_uw: index too low: ");
       err_inspect_sw(&pos);
diff --git a/libkc3/str.h b/libkc3/str.h
index d035c3a..7e911dc 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -93,6 +93,7 @@ sw            str_peek_u8 (const s_str *str, u8 *dest);
 sw            str_peek_u16 (const s_str *str, u16 *dest);
 sw            str_peek_u32 (const s_str *str, u32 *dest);
 sw            str_peek_u64 (const s_str *str, u64 *dest);
+sw            str_position_1 (const s_str *str, const char *token);
 uw *          str_rindex_character (const s_str *str, character c,
                                     uw *dest);
 uw *          str_sw_pos_to_uw (sw pos, uw max_pos, uw *dest);