diff seobeo/s_web.c @ 72:4532ce6d9eb8

[Seobeo] Added router to the server logic. Few dowa string manipulation logics.
author June Park <parkjune1995@gmail.com>
date Mon, 29 Dec 2025 07:50:07 -0800
parents 75de5903355c
children 35b1abc37969
line wrap: on
line diff
--- a/seobeo/s_web.c	Sun Dec 28 20:34:22 2025 -0800
+++ b/seobeo/s_web.c	Mon Dec 29 07:50:07 2025 -0800
@@ -95,7 +95,7 @@
   Dowa_Arena *p_request_arena = Dowa_Arena_Create(1*1024*1024); // 1MB for request parsing
   if (!p_request_arena) { perror("Dowa_Arena_Create request"); goto clean_up; }
 
-  Dowa_Arena *p_response_arena = Dowa_Arena_Create(1*1024*1024); // 1MB for response
+  Dowa_Arena *p_response_arena = Dowa_Arena_Create(5*1024*1024); // 1MB for response
   if (!p_response_arena) { perror("Dowa_Arena_Create response"); goto clean_up; }
 
   void *p_response_header = Dowa_Arena_Allocate(p_response_arena, (size_t)1024*5); // 5Kb
@@ -131,7 +131,24 @@
     goto clean_up;
   }
 
-  // --- Handle different HTTP methods ---
+  // Extract path
+  void *p_path_kv = Dowa_HashMap_Get_Ptr(p_req_map, "Path");
+  const char *path = p_path_kv ? ((Seobeo_Request_Entry*)p_path_kv)->value : "/";
+
+  // --- Try to match API route first ---
+  Seobeo_Route_Handler handler = Seobeo_Router_Find_Handler(method, path, &p_req_map, p_request_arena);
+
+  if (handler != NULL)
+  {
+    // Call the API handler
+    Seobeo_Request_Entry *p_response_map = handler(p_req_map, p_response_arena);
+
+    // Send the response
+    Seobeo_Router_Send_Response(p_cli_handle, p_response_map, p_response_arena);
+    goto clean_up;
+  }
+
+  // --- Handle different HTTP methods (static files fallback) ---
   if (strcmp(method, "GET") == 0)
   {
     void *p_kv = Dowa_HashMap_Get_Ptr(p_req_map, "Path");
@@ -153,9 +170,7 @@
           snprintf(file_path, 512, "%.*s", (int)(L-1), path+1);
       }
       else
-      {
         strcpy(file_path, path);
-      }
     }
 
     // Check if file is in cache, load if not
@@ -171,12 +186,9 @@
     }
     else
     {
-      // Load from disk and cache
       file_content = Seobeo_Web_LoadFile(file_path, &body_size);
       if (file_content)
-      {
         Dowa_HashMap_Push(p_html_cache, file_path, file_content);
-      }
     }
 
     if (!file_content)
@@ -191,6 +203,7 @@
       goto clean_up;
     }
 
+    // Serve static file
     const char *mime = "application/octet-stream";
     if (strstr(file_path, ".html")) mime = "text/html; charset=utf-8";
     else if (strstr(file_path, ".css")) mime = "text/css";
@@ -218,42 +231,8 @@
     Seobeo_Handle_Flush(p_cli_handle);
     printf("DONE\n\n\n");
   }
-  else if (strcmp(method, "POST") == 0)
-  {
-    // --- TODO: Add POST logic here ---
-    Seobeo_Web_Header_Generate(p_response_header,
-                               HTTP_NOT_FOUND,
-                               "text/plain", 0);
-    Seobeo_Handle_Queue(p_cli_handle,
-                        (const uint8*)p_response_header,
-                        (uint32)strlen(p_response_header));
-    Seobeo_Handle_Flush(p_cli_handle);
-  }
-  else if (strcmp(method, "PUT") == 0)
-  {
-    // --- TODO: Add PUT logic here ---
-    Seobeo_Web_Header_Generate(p_response_header,
-                               HTTP_NOT_FOUND,
-                               "text/plain", 0);
-    Seobeo_Handle_Queue(p_cli_handle,
-                        (const uint8*)p_response_header,
-                        (uint32)strlen(p_response_header));
-    Seobeo_Handle_Flush(p_cli_handle);
-  }
-  else if (strcmp(method, "DELETE") == 0)
-  {
-    // --- TODO: Add DELETE logic here ---
-    Seobeo_Web_Header_Generate(p_response_header,
-                               HTTP_NOT_FOUND,
-                               "text/plain", 0);
-    Seobeo_Handle_Queue(p_cli_handle,
-                        (const uint8*)p_response_header,
-                        (uint32)strlen(p_response_header));
-    Seobeo_Handle_Flush(p_cli_handle);
-  }
   else
   {
-    // Unknown or unsupported method
     Seobeo_Web_Header_Generate(p_response_header,
                                HTTP_FORBIDDEN,
                                "text/plain", 0);
@@ -269,9 +248,9 @@
   if (p_cli_handle)
     Seobeo_Handle_Destroy(p_cli_handle);
   if (p_request_arena)
-    Dowa_Arena_Destroy(p_request_arena);
+    Dowa_Arena_Free(p_request_arena);
   if (p_response_arena)
-    Dowa_Arena_Destroy(p_response_arena);
+    Dowa_Arena_Free(p_response_arena);
   return;
 }
 
@@ -561,7 +540,7 @@
       break;
     }else
     {
-      Dowa_Arena_Destroy(p_request_arena);
+      Dowa_Arena_Free(p_request_arena);
       Seobeo_Handle_Destroy(h);
       return -1;
     }
@@ -569,7 +548,7 @@
 
   // Debug
   printf("Request body %s", p_request_body);
-  Dowa_Arena_Destroy(p_request_arena);
+  Dowa_Arena_Free(p_request_arena);
   Seobeo_Handle_Destroy(h);
   return 0;
 }