diff seobeo/snapshot_creator.c @ 126:e7899c93da77

Remove playground.
author June Park <parkjune1995@gmail.com>
date Thu, 08 Jan 2026 18:03:34 -0800
parents 6626ec933933
children 7eb79fd91c7e
line wrap: on
line diff
--- a/seobeo/snapshot_creator.c	Thu Jan 08 08:46:49 2026 -0800
+++ b/seobeo/snapshot_creator.c	Thu Jan 08 18:03:34 2026 -0800
@@ -1,13 +1,11 @@
 #include "seobeo/snapshot_creator.h"
+#include "seobeo/seobeo_internal.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
-#define MAX_RESPONSE_SIZE (1024 * 1024)
-
-// Helper: Convert URL path to filename
 static void path_to_filename(const char *path, char *filename, size_t max_len)
 {
   if (strcmp(path, "/") == 0)
@@ -18,9 +16,7 @@
 
   const char *p = path;
   if (*p == '/')
-  {
     p++;
-  }
 
   char *out = filename;
   size_t remaining = max_len - 1;
@@ -69,68 +65,61 @@
   return mkdir(tmp, 0755);
 }
 
-// Helper: Generate HTTP GET request
-static int generate_http_get(char *buffer, size_t size, const char *path, const char *host)
+// Helper: Build full HTTP response from Seobeo_Client_Response
+static char* build_full_response(Seobeo_Client_Response *p_resp, size_t *len_out)
 {
-  return snprintf(
-    buffer, size,
-    "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,*/*;q=0.8\r\n"
-    "User-Agent: SeobeoSnapshotCreator/1.0\r\n"
-    "\r\n",
-    path, host
-  );
-}
-
-// Helper: Read full HTTP response
-static char* read_full_response(Seobeo_Handle *client, size_t *len_out)
-{
-  char *response = malloc(MAX_RESPONSE_SIZE);
-  if (!response)
-  {
+  if (!p_resp)
     return NULL;
-  }
-
-  size_t total = 0;
-  int attempts = 0;
-  const int max_attempts = 100;
 
-  while (attempts++ < max_attempts && total < MAX_RESPONSE_SIZE - 1)
-  {
-    int n = Seobeo_Handle_Read(client);
-
-    if (n > 0)
-    {
-      size_t to_copy = client->read_buffer_len;
-      if (total + to_copy > MAX_RESPONSE_SIZE - 1)
-      {
-        to_copy = MAX_RESPONSE_SIZE - 1 - total;
-      }
+  // Calculate total response size
+  size_t status_line_len = 100; // "HTTP/1.1 200 OK\r\n"
+  size_t headers_len = 0;
+  size_t body_len = p_resp->body_length;
 
-      memcpy(response + total, client->read_buffer, to_copy);
-      total += to_copy;
-      Seobeo_Handle_Consume(client, (uint32)to_copy);
-    }
-    else if (n == -2)
+  // Estimate header size
+  if (p_resp->headers)
+  {
+    size_t header_count = Dowa_Array_Length(p_resp->headers);
+    for (size_t i = 0; i < header_count; i++)
     {
-      break;
-    }
-    else if (n == 0)
-    {
-      usleep(10000);
-      continue;
-    }
-    else
-    {
-      free(response);
-      return NULL;
+      headers_len += strlen(p_resp->headers[i].key) + strlen(p_resp->headers[i].value) + 4; // ": " + "\r\n"
     }
   }
 
-  response[total] = '\0';
-  *len_out = total;
+  size_t total_size = status_line_len + headers_len + 2 + body_len + 1; // +2 for "\r\n", +1 for null terminator
+  char *response = malloc(total_size);
+  if (!response)
+    return NULL;
+
+  // Build status line
+  int offset = snprintf(response, total_size, "HTTP/1.1 %d %s\r\n",
+                        p_resp->status_code,
+                        p_resp->status_text ? p_resp->status_text : "OK");
+
+  // Add headers
+  if (p_resp->headers)
+  {
+    size_t header_count = Dowa_Array_Length(p_resp->headers);
+    for (size_t i = 0; i < header_count; i++)
+    {
+      offset += snprintf(response + offset, total_size - offset, "%s: %s\r\n",
+                        p_resp->headers[i].key,
+                        p_resp->headers[i].value);
+    }
+  }
+
+  // End of headers
+  offset += snprintf(response + offset, total_size - offset, "\r\n");
+
+  // Add body
+  if (p_resp->body && body_len > 0)
+  {
+    memcpy(response + offset, p_resp->body, body_len);
+    offset += body_len;
+  }
+
+  response[offset] = '\0';
+  *len_out = offset;
   return response;
 }
 
@@ -171,67 +160,54 @@
 
   printf("Creating snapshot: %s (expecting %d)\n", config->path, config->expected_status);
 
-  // Connect to server
-  Seobeo_Handle *client = Seobeo_Stream_Handle_Client_Create(config->host, config->port, FALSE);
-  if (!client || client->socket < 0)
+  // Build URL
+  char url[2048];
+  snprintf(url, sizeof(url), "http://%s:%s%s", config->host, config->port, config->path);
+
+  // Create HTTP request
+  Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create(url);
+  if (!p_req)
   {
-    fprintf(stderr, "  ✗ Failed to connect to %s:%s\n", config->host, config->port);
-    if (client)
-    {
-      Seobeo_Handle_Destroy(client);
-    }
+    fprintf(stderr, "  ✗ Failed to create request for %s\n", url);
     return -1;
   }
 
-  // Send GET request
-  char request[4096];
-  int req_len = generate_http_get(request, sizeof(request), config->path, config->host);
-  Seobeo_Handle_Queue(client, (uint8*)request, (uint32)req_len);
-
-  if (Seobeo_Handle_Flush(client) < 0)
+  // Execute request
+  Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req);
+  if (!p_resp)
   {
-    fprintf(stderr, "  ✗ Failed to send request\n");
-    Seobeo_Handle_Destroy(client);
+    fprintf(stderr, "  ✗ Failed to get response\n");
+    Seobeo_Client_Request_Destroy(p_req);
     return -1;
   }
 
-  // Read response
-  size_t response_len = 0;
-  char *response = read_full_response(client, &response_len);
-  Seobeo_Handle_Destroy(client);
-
-  if (!response || response_len == 0)
+  // Check status code
+  if (p_resp->status_code != config->expected_status)
   {
-    fprintf(stderr, "  ✗ Failed to read response\n");
-    if (response)
-    {
-      free(response);
-    }
+    fprintf(stderr, "  ✗ Status mismatch: expected %d, got %d\n",
+            config->expected_status, p_resp->status_code);
+    Seobeo_Client_Response_Destroy(p_resp);
+    Seobeo_Client_Request_Destroy(p_req);
     return -1;
   }
 
-  // Parse status code
-  int status = -1;
-  const char *status_line = strstr(response, "HTTP/1.1 ");
-  if (!status_line)
-  {
-    status_line = strstr(response, "HTTP/1.0 ");
-  }
-  if (status_line)
+  printf("  ✓ Status: %d\n", p_resp->status_code);
+
+  // Build full HTTP response
+  size_t response_len = 0;
+  char *response = build_full_response(p_resp, &response_len);
+
+  Seobeo_Client_Response_Destroy(p_resp);
+  Seobeo_Client_Request_Destroy(p_req);
+
+  if (!response || response_len == 0)
   {
-    sscanf(status_line + 9, "%d", &status);
-  }
-
-  if (status != config->expected_status)
-  {
-    fprintf(stderr, "  ✗ Status mismatch: expected %d, got %d\n",
-            config->expected_status, status);
-    free(response);
+    fprintf(stderr, "  ✗ Failed to build response\n");
+    if (response)
+      free(response);
     return -1;
   }
 
-  printf("  ✓ Status: %d\n", status);
-
   // Generate snapshot filename
   char filename[256];
   path_to_filename(config->path, filename, sizeof(filename));