diff seobeo/s_web.c @ 17:d97ec3ded2ae

[Seobeo] Few changes... - Fixed seobeo edge for macos - Updated so that socket creation can be used for both client and server - Started on a cutelient library for making connection to the server.
author June Park <parkjune1995@gmail.com>
date Sat, 04 Oct 2025 07:53:12 -0700
parents fb2cff495a60
children fa2b8af609d9
line wrap: on
line diff
--- a/seobeo/s_web.c	Fri Oct 03 09:55:51 2025 -0700
+++ b/seobeo/s_web.c	Sat Oct 04 07:53:12 2025 -0700
@@ -1,5 +1,18 @@
 #include "seobeo/seobeo.h"
 
+int Seobeo_Web_GenerateRequestHeader(void *buffer, const char *host, 
+                                     const char *path) 
+{
+  return sprintf(
+    buffer,
+    "GET %s HTTP/1.1\r\n"
+    "Host: %s\r\n"
+    "Connection: close\r\n"
+    "\r\n",
+    path, host
+  );
+}
+
 void Seobeo_Web_GenerateResponseHeader(void *buffer, int status,
                                        const char *content_type, const int content_length) 
 {
@@ -58,6 +71,8 @@
     goto clean_up;
   }
 
+  Dowa_HashMap_Print(p_req_map);
+
   const char *path = (const char*)Dowa_HashMap_Get(p_req_map, "Path");
 
   char *file_path = Dowa_Arena_Allocate(p_response_arena, (size_t)512);
@@ -306,3 +321,76 @@
 
   return -1;
 }
+
+
+int Seobeo_Web_ClientGet(const char *host,
+                         const char *port,
+                         const char *path)
+{
+  Seobeo_PHandle h = Seobeo_Stream_Handle_Create(host, port);
+  if (!h || h->socket < 0)
+  {
+    if (h) Seobeo_Handle_Destroy(h);
+    return -1;
+  }
+
+  Dowa_PArena p_request_arena = Dowa_Arena_Create(1 * 1024 * 1024);
+  if (!p_request_arena)
+  {
+    perror("Dowa_Arena_Create");
+    return -1;
+  }
+  void *p_request_header = Dowa_Arena_Allocate(p_request_arena, 4096);
+  if (!p_request_header)
+  {
+    perror("Dowa_Arena_Allocate");
+    return -1;
+  }
+
+  int request_len = Seobeo_Web_GenerateRequestHeader(p_request_header, host, path);
+  printf("request: %s\n", (char *)p_request_header);
+  Seobeo_Handle_Queue(h, (uint8 *)p_request_header, (uint32)request_len);
+
+  if (Seobeo_Handle_Flush(h) < 0)
+  {
+    perror("Seobeo_Handle_Flush");
+    Seobeo_Handle_Destroy(h);
+    return -1;
+  }
+
+  size_t cap = 1024*8, used = 0;
+  char *p_request_body = Dowa_Arena_Allocate(p_request_arena, cap);
+  if (!p_request_body) { Seobeo_Handle_Destroy(h); return -1; }
+
+  while (1) {
+    int n = Seobeo_Handle_Read(h);
+    if (n > 0)
+    {
+      memcpy(p_request_body + used, h->read_buffer, h->read_buffer_len);
+      used += h->read_buffer_len;
+      Seobeo_Handle_Consume(h, (uint32)h->read_buffer_len);
+    }
+    else if (n == 0)
+    {
+      // non-blocking mode and no data right now → keep looping or break?
+      // For a simple blocking client, you could block instead:
+      continue;
+    }
+    else if (n == -2)
+    {
+      // peer closed; we’ve got everything
+      break;
+    }
+    else
+    {
+      Dowa_Arena_Free(p_request_arena);
+      Seobeo_Handle_Destroy(h);
+      return -1;
+    }
+  }
+
+  printf("%s\n\n", p_request_body);
+  Dowa_Arena_Free(p_request_arena);
+  Seobeo_Handle_Destroy(h);
+  return 0;
+}