Commit 7c2c47ebf30cd333910a9b9cc3a779bad3560f32

Thomas de Grivel 2024-08-15T03:22:33

wip test http server request

diff --git a/http/http_request.c b/http/http_request.c
index 0ae36c7..ec9b77d 100644
--- a/http/http_request.c
+++ b/http/http_request.c
@@ -95,3 +95,96 @@ s_tag * http_request_buf_parse (s_tag *req, s_buf *buf)
   *req = tmp;
   return req;
 }
+
+sw http_request_buf_write (s_http_request *req, s_buf *buf)
+{
+  s_tag *key;
+  s_list *list;
+  sw r;
+  sw result = 0;
+  s_str str;
+  const s_sym *type = &g_sym_Str;
+  s_tag *value;
+  assert(req);
+  assert(buf);
+  if (! req->method->str.size) {
+    err_puts("http_request_buf_write: invalid method: \"\"");
+    return -1;
+  }    
+  if (! req->url.size) {
+    err_puts("http_request_buf_write: invalid url: \"\"");
+    return -1;
+  }    
+  if (! req->protocol.size) {
+    err_puts("http_request_buf_write: invalid protocol: \"\"");
+    return -1;
+  }    
+  if (! list_is_alist((const s_list * const *) &req->headers)) {
+    err_puts("http_request_buf_write: invalid headers: not an AList");
+    return -1;
+  }
+  if (! str_init_to_upper(&str, &req->method->str))
+    return -1;
+  if ((r = buf_write_str(buf, &str)) < 0)
+    return r;
+  result += r;
+  str_clean(&str);
+  if ((r = buf_write_1(buf, " ")) <= 0)
+    return r;
+  result += r;
+  if ((r = buf_write_str(buf, &req->url)) <= 0)
+    return r;
+  result += r;
+  if ((r = buf_write_1(buf, " ")) <= 0)
+    return r;
+  result += r;
+  if ((r = buf_write_str(buf, &req->protocol)) < 0)
+    return r;
+  result += r;
+  if ((r = buf_write_1(buf, "\r\n")) < 0)
+    return r;
+  result += r;
+  list = req->headers;
+  while (list) {
+    key = list->tag.data.tuple.tag;
+    value = list->tag.data.tuple.tag + 1;
+    switch (key->type) {
+    case TAG_STR:
+      if ((r = buf_write_str(buf, &key->data.str)) <= 0)
+        return r;
+      result += r;
+      break;
+    case TAG_SYM:
+      if ((r = buf_write_str(buf, &key->data.sym->str)) <= 0)
+        return r;
+      result += r;
+      break;
+    default:
+      err_write_1("http_request_buf_write: invalid header key: ");
+      err_inspect_tag(key);
+      return -1;
+    }
+    if ((r = buf_write_1(buf, ": ")) <= 0)
+      return r;
+    result += r;
+    if (! str_init_cast(&str, &type, value)) {
+      err_write_1("http_request_buf_write: invalid header value: ");
+      err_inspect_tag(value);
+      return -1;
+    }
+    if ((r = buf_write_str(buf, &str)) <= 0) {
+      str_clean(&str);
+      return r;
+    }
+    result += r;
+    str_clean(&str);
+    if ((r = buf_write_1(buf, "\r\n")) <= 0)
+      return r;
+    result += r;
+    list = list_next(list);
+  }
+  if ((r = buf_write_1(buf, "\r\n")) <= 0)
+    return r;
+  result += r;
+  return result;
+}
diff --git a/lib/kc3/0.1/http/request.kc3 b/lib/kc3/0.1/http/request.kc3
index 3bbc651..3a46b9e 100644
--- a/lib/kc3/0.1/http/request.kc3
+++ b/lib/kc3/0.1/http/request.kc3
@@ -9,4 +9,6 @@ defmodule HTTP.Request do
 
   def buf_parse = cfn Tag "http_request_buf_parse" (Result, Buf)
 
+  def buf_write = cfn Sw "http_request_buf_write" (HTTP.Request, Buf)
+
 end
diff --git a/test/http/04_server_request.kc3 b/test/http/04_server_request.kc3
index 4475cc6..cb3048f 100644
--- a/test/http/04_server_request.kc3
+++ b/test/http/04_server_request.kc3
@@ -12,9 +12,31 @@ quote response = %HTTP.Response{body: "Hello, KC3 HTTP test server !"}
 response = %HTTP.Response{body: "Hello, KC3 HTTP test server !"}
 quote HTTP.Response.buf_write(response, client.buf_rw.w, true)
 HTTP.Response.buf_write(response, client.buf_rw.w, true)
-#quote Socket.Buf.close(client)
-#Socket.Buf.close(client)
-#quote Socket.Buf.close(server_client)
-#Socket.Buf.close(server_client)
-#quote Socket.close(server)
-#Socket.close(server)
+quote client_req = %HTTP.Request{method: :get,
+                                 url: "/",
+                                 protocol: "HTTP/1.1",
+                                 headers: []}
+client_req = %HTTP.Request{method: :get,
+                           url: "/",
+                           protocol: "HTTP/1.1",
+                           headers: []}
+quote r = HTTP.Request.buf_write(client_req, client.buf_rw.w)
+r = HTTP.Request.buf_write(client_req, client.buf_rw.w)
+quote puts(r)
+puts(r)
+quote if (r) do
+  req = HTTP.Request.buf_parse(server_client.buf_rw.r)
+  puts(req)
+  HTTP.Response.buf_write(response, client.buf_rw.w, true)
+end
+if (r) do
+  req = HTTP.Request.buf_parse(server_client.buf_rw.r)
+  puts(req)
+  HTTP.Response.buf_write(response, client.buf_rw.w, true)
+end
+quote Socket.Buf.close(client)
+Socket.Buf.close(client)
+quote Socket.Buf.close(server_client)
+Socket.Buf.close(server_client)
+quote Socket.close(server)
+Socket.close(server)