working fourth http test
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
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)