comparison 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
comparison
equal deleted inserted replaced
16:fb2cff495a60 17:d97ec3ded2ae
1 #include "seobeo/seobeo.h" 1 #include "seobeo/seobeo.h"
2
3 int Seobeo_Web_GenerateRequestHeader(void *buffer, const char *host,
4 const char *path)
5 {
6 return sprintf(
7 buffer,
8 "GET %s HTTP/1.1\r\n"
9 "Host: %s\r\n"
10 "Connection: close\r\n"
11 "\r\n",
12 path, host
13 );
14 }
2 15
3 void Seobeo_Web_GenerateResponseHeader(void *buffer, int status, 16 void Seobeo_Web_GenerateResponseHeader(void *buffer, int status,
4 const char *content_type, const int content_length) 17 const char *content_type, const int content_length)
5 { 18 {
6 const char *status_text; 19 const char *status_text;
55 (const uint8*)p_response_header, 68 (const uint8*)p_response_header,
56 (uint32)strlen(p_response_header)); 69 (uint32)strlen(p_response_header));
57 Seobeo_Handle_Flush(p_cli_handle); 70 Seobeo_Handle_Flush(p_cli_handle);
58 goto clean_up; 71 goto clean_up;
59 } 72 }
73
74 Dowa_HashMap_Print(p_req_map);
60 75
61 const char *path = (const char*)Dowa_HashMap_Get(p_req_map, "Path"); 76 const char *path = (const char*)Dowa_HashMap_Get(p_req_map, "Path");
62 77
63 char *file_path = Dowa_Arena_Allocate(p_response_arena, (size_t)512); 78 char *file_path = Dowa_Arena_Allocate(p_response_arena, (size_t)512);
64 79
304 Seobeo_Web_Edge(p_server_handle, thread_count, p_html_cache); 319 Seobeo_Web_Edge(p_server_handle, thread_count, p_html_cache);
305 } 320 }
306 321
307 return -1; 322 return -1;
308 } 323 }
324
325
326 int Seobeo_Web_ClientGet(const char *host,
327 const char *port,
328 const char *path)
329 {
330 Seobeo_PHandle h = Seobeo_Stream_Handle_Create(host, port);
331 if (!h || h->socket < 0)
332 {
333 if (h) Seobeo_Handle_Destroy(h);
334 return -1;
335 }
336
337 Dowa_PArena p_request_arena = Dowa_Arena_Create(1 * 1024 * 1024);
338 if (!p_request_arena)
339 {
340 perror("Dowa_Arena_Create");
341 return -1;
342 }
343 void *p_request_header = Dowa_Arena_Allocate(p_request_arena, 4096);
344 if (!p_request_header)
345 {
346 perror("Dowa_Arena_Allocate");
347 return -1;
348 }
349
350 int request_len = Seobeo_Web_GenerateRequestHeader(p_request_header, host, path);
351 printf("request: %s\n", (char *)p_request_header);
352 Seobeo_Handle_Queue(h, (uint8 *)p_request_header, (uint32)request_len);
353
354 if (Seobeo_Handle_Flush(h) < 0)
355 {
356 perror("Seobeo_Handle_Flush");
357 Seobeo_Handle_Destroy(h);
358 return -1;
359 }
360
361 size_t cap = 1024*8, used = 0;
362 char *p_request_body = Dowa_Arena_Allocate(p_request_arena, cap);
363 if (!p_request_body) { Seobeo_Handle_Destroy(h); return -1; }
364
365 while (1) {
366 int n = Seobeo_Handle_Read(h);
367 if (n > 0)
368 {
369 memcpy(p_request_body + used, h->read_buffer, h->read_buffer_len);
370 used += h->read_buffer_len;
371 Seobeo_Handle_Consume(h, (uint32)h->read_buffer_len);
372 }
373 else if (n == 0)
374 {
375 // non-blocking mode and no data right now → keep looping or break?
376 // For a simple blocking client, you could block instead:
377 continue;
378 }
379 else if (n == -2)
380 {
381 // peer closed; we’ve got everything
382 break;
383 }
384 else
385 {
386 Dowa_Arena_Free(p_request_arena);
387 Seobeo_Handle_Destroy(h);
388 return -1;
389 }
390 }
391
392 printf("%s\n\n", p_request_body);
393 Dowa_Arena_Free(p_request_arena);
394 Seobeo_Handle_Destroy(h);
395 return 0;
396 }