Commit 1569f9b87f41c084abf32e40287291ad1596c029

Thomas de Grivel 2024-08-07T17:52:24

http methods

diff --git a/http/http_request.c b/http/http_request.c
index d6c4011..e019ca3 100644
--- a/http/http_request.c
+++ b/http/http_request.c
@@ -30,6 +30,18 @@ s_tag * http_request_buf_parse (s_tag *req, s_buf *buf)
     goto restore;
   if (r > 0)
     tmp_req.method = sym_1("get");
+  if ((r = buf_read_1(buf, "POST ")) < 0)
+    goto restore;
+  if (r > 0)
+    tmp_req.method = sym_1("post");
+  if ((r = buf_read_1(buf, "PUT ")) < 0)
+    goto restore;
+  if (r > 0)
+    tmp_req.method = sym_1("put");
+  if ((r = buf_read_1(buf, "HEAD ")) < 0)
+    goto restore;
+  if (r > 0)
+    tmp_req.method = sym_1("head");
   if (! tmp_req.method) {
     err_write_1("http_request_buf_parse: no method: ");
     err_inspect_buf(buf);
diff --git a/http/http_response.c b/http/http_response.c
index 5cd0e1b..13262b8 100644
--- a/http/http_response.c
+++ b/http/http_response.c
@@ -16,7 +16,7 @@
 #include "socket.h"
 
 sw http_response_buf_write (const s_http_response *response,
-                            s_buf *buf)
+                            s_buf *buf, bool send_body)
 {
   sw    content_length = -1;
   s_str content_length_str = {0};
@@ -116,7 +116,7 @@ sw http_response_buf_write (const s_http_response *response,
     result += r;
     l = list_next(l);
   }
-  if (content_length < 0) {
+  if (content_length < 0 && send_body) {
     if ((r = buf_write_str(buf, &content_length_str)) < 0)
       return r;
     result += r;
@@ -133,8 +133,10 @@ sw http_response_buf_write (const s_http_response *response,
   if ((r = buf_write_1(buf, "\r\n")) < 0)
     return r;
   result += r;
-  if ((r = buf_write_str(buf, &response->body)) < 0)
-    return r;
-  result += r;
+  if (send_body) {
+    if ((r = buf_write_str(buf, &response->body)) < 0)
+      return r;
+    result += r;
+  }
   return result;
 }
diff --git a/http/http_response.h b/http/http_response.h
index 3cca5a5..4fef1f1 100644
--- a/http/http_response.h
+++ b/http/http_response.h
@@ -16,6 +16,6 @@
 #include "types.h"
 
 sw http_response_buf_write (const s_http_response *response,
-                            s_buf *buf);
+                            s_buf *buf, bool send_body);
 
 #endif /* HTTP_RESPONSE_H */
diff --git a/lib/kc3/0.1/http/response.kc3 b/lib/kc3/0.1/http/response.kc3
index 98ebcb1..ac9b813 100644
--- a/lib/kc3/0.1/http/response.kc3
+++ b/lib/kc3/0.1/http/response.kc3
@@ -12,6 +12,8 @@ defmodule HTTP.Response do
                           {(U16) 404, "Not Found"},
                           {(U16) 500, "Internal Server Error"}]
 
-  def buf_write = cfn Sw "http_response_buf_write" (HTTP.Response, Buf)
+  # buf_write(response, buf_w, send_body)
+  def buf_write = cfn Sw "http_response_buf_write" (HTTP.Response, Buf,
+    Bool)
 
 end
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index 7cf0840..e08f424 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -19,7 +19,7 @@ defmodule HTTPd do
     if req do
       router = route_request(req)
       res = router(req)
-      HTTP.Response.buf_write(res, client.buf_rw.w)
+      HTTP.Response.buf_write(res, client.buf_rw.w, req.method != :head)
     end
   }