diff seobeo/s_web.c @ 101:3468e2fe8d88

[Seobeo] log ngnix proxy values (for myself). took out unneeded stuff fflush.
author June Park <parkjune1995@gmail.com>
date Sat, 03 Jan 2026 08:19:02 -0800
parents 65e5a5b89a4e
children 99c4530e4629
line wrap: on
line diff
--- a/seobeo/s_web.c	Sat Jan 03 07:48:07 2026 -0800
+++ b/seobeo/s_web.c	Sat Jan 03 08:19:02 2026 -0800
@@ -93,7 +93,6 @@
 void Seobeo_Web_HandleClientRequest(Seobeo_Handle  *p_cli_handle,
                                     Seobeo_Cache_Entry *p_html_cache)
 {
-  Seobeo_Log(SEOBEO_INFO, "Client is from %s\n", p_cli_handle->host);
   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; }
 
@@ -109,7 +108,6 @@
   if (parse_result != 0 && parse_result != 1)
   {
     Seobeo_Log(SEOBEO_ERROR, "Seobeo_Web_Header_Parse failed with code %d\n", parse_result);
-    fflush(stdout);
     Seobeo_Web_Header_Generate(p_response_header,
                                HTTP_BAD_REQUEST,
                                "text/plain", 0);
@@ -121,18 +119,36 @@
   }
 
   Seobeo_Log(SEOBEO_DEBUG, "Parse completed with code %d\n", parse_result);
-  fflush(stdout);
+
+  // Recording IP to see who is ddosing or any web scrappers...
+  void *p_real_ip_kv = Dowa_HashMap_Get_Ptr(p_req_map, "X-Real-IP");
+  const char *real_ip = p_real_ip_kv ? ((Seobeo_Request_Entry*)p_real_ip_kv)->value : NULL;
+  // Fallback
+  if (!real_ip)
+  {
+    void *p_forwarded_kv = Dowa_HashMap_Get_Ptr(p_req_map, "X-Forwarded-For");
+    real_ip = p_forwarded_kv ? ((Seobeo_Request_Entry*)p_forwarded_kv)->value : NULL;
+  }
+  // Fallback
+  if (!real_ip)
+    real_ip = p_cli_handle->host;
 
   void *p_method_kv = Dowa_HashMap_Get_Ptr(p_req_map, "HTTP_Method");
   const char *method = p_method_kv ? ((Seobeo_Request_Entry*)p_method_kv)->value : NULL;
 
+  void *p_path_kv_log = Dowa_HashMap_Get_Ptr(p_req_map, "Path");
+  const char *path_log = p_path_kv_log ? ((Seobeo_Request_Entry*)p_path_kv_log)->value : "/";
+
+  Seobeo_Log(SEOBEO_INFO, "%s - %s %s\n",
+             real_ip ? real_ip : "unknown",
+             method ? method : "UNKNOWN",
+             path_log);
+
   Seobeo_Log(SEOBEO_DEBUG, "Parsed request, method=%s\n", method ? method : "NULL");
-  fflush(stdout);
 
   if (!method)
   {
     Seobeo_Log(SEOBEO_ERROR, "No HTTP method found in request\n");
-    fflush(stdout);
     Seobeo_Web_Header_Generate(p_response_header,
                                HTTP_BAD_REQUEST,
                                "text/plain", 0);
@@ -295,7 +311,6 @@
   {
     size_t first_line_len = first_line_end - buf;
     Seobeo_Log(SEOBEO_DEBUG, "Request line (first %zu bytes)\n", first_line_len > 200 ? 200 : first_line_len);
-    fflush(stdout);
   }
 
   // This seems kinda bad ?
@@ -306,34 +321,28 @@
          scan_result >= 1 ? method : "N/A",
          scan_result >= 2 ? path : "N/A",
          scan_result >= 3 ? version : "N/A");
-  fflush(stdout);
 
   if (scan_result != 3)
   {
     Seobeo_Log(SEOBEO_ERROR, "Failed to parse request line\n");
-    fflush(stdout);
     return -1;
   }
 
   // Copy strings to arena and store in hashmap
   Seobeo_Log(SEOBEO_DEBUG, "Allocating method_copy\n");
-  fflush(stdout);
   char *method_copy = Dowa_Arena_Allocate(p_arena, strlen(method) + 1);
   if (!method_copy) { Seobeo_Log(SEOBEO_ERROR, "Failed to allocate method_copy\n"); return -1; }
   strcpy(method_copy, method);
 
   Seobeo_Log(SEOBEO_DEBUG, "Allocating version_copy\n");
-  fflush(stdout);
   char *version_copy = Dowa_Arena_Allocate(p_arena, strlen(version) + 1);
   if (!version_copy) { Seobeo_Log(SEOBEO_ERROR, "Failed to allocate version_copy\n"); return -1; }
   strcpy(version_copy, version);
 
   Seobeo_Log(SEOBEO_DEBUG, "Pushing HTTP_Method and Version to map\n");
-  fflush(stdout);
   Dowa_HashMap_Push_Arena(*pp_map, "HTTP_Method", method_copy, p_arena);
   Dowa_HashMap_Push_Arena(*pp_map, "Version", version_copy, p_arena);
   Seobeo_Log(SEOBEO_DEBUG, "Map now has %zu entries\n", Dowa_Array_Length(*pp_map));
-  fflush(stdout);
 
   // 1) Separate raw path and query string
   char *raw_path = path;  
@@ -435,14 +444,12 @@
     size_t body_len = atoi(content_length_str);
 
     Seobeo_Log(SEOBEO_DEBUG, "Content-Length=%zu, reading body in chunks...\n", body_len);
-    fflush(stdout);
 
     // Allocate buffer for entire body
     char *body = Dowa_Arena_Allocate(p_arena, body_len + 1);
     if (!body)
     {
       Seobeo_Log(SEOBEO_ERROR, "Failed to allocate %zu bytes for body\n", body_len);
-      fflush(stdout);
       return -1;
     }
 
@@ -462,7 +469,6 @@
         Seobeo_Handle_Consume(p_handle, (uint32)to_copy);
 
         Seobeo_Log(SEOBEO_DEBUG, "Copied %zu bytes, total %zu/%zu\n", to_copy, total_read, body_len);
-        fflush(stdout);
       }
 
       // If we still need more data, read another chunk
@@ -472,14 +478,12 @@
         if (r < 0)
         {
           Seobeo_Log(SEOBEO_ERROR, "Read failed with %d\n", r);
-          fflush(stdout);
           return -1;
         }
         if (r == 0)
         {
           // No data available yet, continue waiting
           // printf("DEBUG: Waiting for more data... (have %zu/%zu bytes)\n", total_read, body_len);
-          fflush(stdout);
           continue;
         }
       }
@@ -487,7 +491,6 @@
 
     body[body_len] = '\0';
     Seobeo_Log(SEOBEO_DEBUG, "Body fully received (%zu bytes)\n", body_len);
-    fflush(stdout);
 
     // Body is arena-allocated
     Dowa_HashMap_Push_Arena(*pp_map, "Body", body, p_arena);