wip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
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);