diff seobeo/s_web.c @ 18:fa2b8af609d9

[Seobeo] Fixed a bug with pathing. Support SSL.
author June Park <parkjune1995@gmail.com>
date Mon, 06 Oct 2025 08:21:34 -0700
parents d97ec3ded2ae
children 875bb6e10db7
line wrap: on
line diff
--- a/seobeo/s_web.c	Sat Oct 04 07:53:12 2025 -0700
+++ b/seobeo/s_web.c	Mon Oct 06 08:21:34 2025 -0700
@@ -8,6 +8,19 @@
     "GET %s HTTP/1.1\r\n"
     "Host: %s\r\n"
     "Connection: close\r\n"
+    "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\n"
+    "accept-language: en-GB,en;q=0.9,en-US;q=0.8,ko;q=0.7\r\n"
+    "if-modified-since: Sat, 02 Aug 2025 22:51:58 GMT\r\n"
+    "if-none-match: W/\"688e968e-5700\"\r\n"
+    "priority: u=0, i\r\n"
+    "sec-ch-ua: \"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Google Chrome\";v=\"140\"\r\n"
+    "sec-ch-ua-mobile: 0\r\n"
+    "sec-ch-ua-platform: \"macOS\"\r\n"
+    "sec-fetch-dest: document\r\n"
+    "sec-fetch-mode: navigate\r\n"
+    "sec-fetch-site: none\r\n"
+    "sec-fetch-user: 1\r\n"
+    "upgrade-insecure-requests: 1\r\n"
     "\r\n",
     path, host
   );
@@ -33,7 +46,7 @@
   
   sprintf(
     buffer, 
-    "HTTP/2.2 %d %s\r\n"
+    "HTTP/1.1 %d %s\r\n"
     "Content-Type: %s\r\n"
     "Content-Length: %d\r\n"
     "Connection: close\r\n"
@@ -71,17 +84,16 @@
     goto clean_up;
   }
 
-  Dowa_HashMap_Print(p_req_map);
+  // DEBUG
+  // 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);
 
   if (!path || strcmp(path, "/") == 0)
   {
     strcpy(file_path, "index.html");
-  }
-  else
+  }else
   {
     size_t L = strlen(path);
     // strip leading '/'
@@ -91,25 +103,38 @@
         snprintf(file_path, 512, "%.*s/index.html", (int)(L-1), path+1);
       else
         snprintf(file_path, 512, "%.*s", (int)(L-1), path+1);
-    }
-    else
+    }else
     {
       // Probably never get here?
       strcpy(file_path, path);
     }
   }
 
+  // DEBUG
   // printf("\n\nfile_path: %s\n", file_path);
 
   // Recursively go though the path until it gets to a file
   while ((slash = strchr(file_path, '/')))
   {
     *slash = '\0'; // e.g. file_path="foo", slash+1="index.html"
-    char *dir     = file_path; // "foo"
+    char *dir = file_path; // "foo"
     file_path = slash + 1;  // "index.html"
 
+    printf("\n\nDirectory: %s\n\n", dir);
+
     p_current = Dowa_HashMap_Get(p_current, dir);
-    if (!p_current) { perror("No value"); goto clean_up; }
+    if (!p_current)
+    { 
+      fprintf(stderr, "No value in hashmap key: %s\n\n", dir);
+      Seobeo_Web_GenerateResponseHeader(p_response_header,
+                                        HTTP_NOT_FOUND,
+                                        "text/html", 0);
+      Seobeo_Handle_Queue(p_cli_handle,
+                  (const uint8*)p_response_header,
+                  (uint32)strlen(p_response_header));
+      Seobeo_Handle_Flush(p_cli_handle);
+      goto clean_up;
+    }
   }
 
   size_t pos = Dowa_HashMap_GetPosition(p_current, file_path);
@@ -141,13 +166,19 @@
   else if (strstr(file_path, ".json")) mime = "application/json";
 
   size_t body_size = entry->capacity;
+  printf("key: %s\n\n", entry->key);
+  printf("Body Size: %zu\n\n", body_size);
   Seobeo_Web_GenerateResponseHeader(p_response_header,
                                     HTTP_OK,
                                     mime,
                                     body_size);
+
+  printf("response header: %s, data: %d\n\n", p_response_header, (uint32)strlen(p_response_header));
   Seobeo_Handle_Queue(p_cli_handle,
               (const uint8*)p_response_header,
               (uint32)strlen(p_response_header));
+
+  printf("response body data: %d\n\n", (uint32)body_size);
   Seobeo_Handle_Queue(p_cli_handle,
               (const uint8*)entry->buffer,
               (uint32)body_size);
@@ -286,13 +317,15 @@
   }
 
   Seobeo_PHandle p_server_handle =
-    Seobeo_Stream_Handle_Create(NULL, port);
+    Seobeo_Stream_Handle_Server_Create(NULL, port);
   if (p_server_handle->socket < 0) return 1;
+
   printf("Listening on port %s\n", port);
 
   // Fork‐based fallback
   if (mode == SEOBEO_MODE_FORK)
   {
+    printf("FORK MODE\n");
     struct sigaction sa;
     sa.sa_handler = SigchildHandler;
     sigemptyset(&sa.sa_mask);
@@ -316,6 +349,7 @@
 
   if (mode == SEOBEO_MODE_EDGE)
   {
+    printf("EDGE MODE\n");
     Seobeo_Web_Edge(p_server_handle, thread_count, p_html_cache);
   }
 
@@ -327,30 +361,24 @@
                          const char *port,
                          const char *path)
 {
-  Seobeo_PHandle h = Seobeo_Stream_Handle_Create(host, port);
+  Seobeo_PHandle h = Seobeo_Stream_Handle_Client_Create(host, port, TRUE);
   if (!h || h->socket < 0)
   {
-    if (h) Seobeo_Handle_Destroy(h);
+    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;
-  }
+  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;
-  }
+  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);
+  // DEBUG
+  // 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");
@@ -358,30 +386,32 @@
     return -1;
   }
 
+  // Response
   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) {
+  while (1)
+  {
     int n = Seobeo_Handle_Read(h);
+    printf("Size: %d\n", n);
     if (n > 0)
     {
+      // TODO: Maybe directly use arena inside of the struct? idk if that is useful or not...
       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)
+    }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:
+      // Wait
       continue;
-    }
-    else if (n == -2)
+    }else if (n == -2)
     {
+      // Debug
       // peer closed; we’ve got everything
+      printf("\n\nCLOSED\n\n");
       break;
-    }
-    else
+    }else
     {
       Dowa_Arena_Free(p_request_arena);
       Seobeo_Handle_Destroy(h);
@@ -389,7 +419,7 @@
     }
   }
 
-  printf("%s\n\n", p_request_body);
+  printf("%s", p_request_body);
   Dowa_Arena_Free(p_request_arena);
   Seobeo_Handle_Destroy(h);
   return 0;