comparison seobeo/s_web.c @ 119:c39582f937e5

[Seobeo Client] Added client side logic which will be used for all my other calls instead of curl.
author June Park <parkjune1995@gmail.com>
date Wed, 07 Jan 2026 16:05:57 -0800
parents 99c4530e4629
children 7b1719fa918c
comparison
equal deleted inserted replaced
118:249881ceff7b 119:c39582f937e5
1 #include "seobeo/seobeo.h" 1 #include "seobeo/seobeo.h"
2 2
3 static char g_folder_path[512] = "."; 3 static char g_folder_path[512] = ".";
4
5 int Seobeo_Web_GenerateRequestHeader(
6 void *buffer, const char *host,
7 const char *path)
8 {
9 return sprintf(
10 buffer,
11 "GET %s HTTP/1.1\r\n"
12 "Host: %s\r\n"
13 "Connection: close\r\n"
14 "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"
15 "accept-language: en-GB,en;q=0.9,en-US;q=0.8,ko;q=0.7\r\n"
16 "if-modified-since: Sat, 02 Aug 2025 22:51:58 GMT\r\n"
17 "if-none-match: W/\"688e968e-5700\"\r\n"
18 "priority: u=0, i\r\n"
19 "sec-ch-ua: \"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Google Chrome\";v=\"140\"\r\n"
20 "sec-ch-ua-mobile: 0\r\n"
21 "sec-ch-ua-platform: \"macOS\"\r\n"
22 "sec-fetch-dest: document\r\n"
23 "sec-fetch-mode: navigate\r\n"
24 "sec-fetch-site: none\r\n"
25 "sec-fetch-user: 1\r\n"
26 "upgrade-insecure-requests: 1\r\n"
27 "\r\n",
28 path, host
29 );
30 }
31 4
32 char* Seobeo_Web_LoadFile(const char *file_path, size_t *p_file_size) 5 char* Seobeo_Web_LoadFile(const char *file_path, size_t *p_file_size)
33 { 6 {
34 char full_path[1024]; 7 char full_path[1024];
35 snprintf(full_path, sizeof(full_path), "%s/%s", g_folder_path, file_path); 8 snprintf(full_path, sizeof(full_path), "%s/%s", g_folder_path, file_path);
549 } 522 }
550 523
551 return -1; 524 return -1;
552 } 525 }
553 526
554 int Seobeo_Web_Client_Get(const char *host,
555 const char *port,
556 const char *path)
557 {
558 Seobeo_Handle *h = Seobeo_Stream_Handle_Client_Create(host, port, TRUE);
559 if (!h || h->socket < 0)
560 {
561 if (h)
562 Seobeo_Handle_Destroy(h);
563 return -1;
564 }
565
566 Dowa_Arena *p_request_arena = Dowa_Arena_Create(1 * 1024 * 1024);
567 if (!p_request_arena) { perror("Dowa_Arena_Create"); return -1; }
568
569 void *p_request_header = Dowa_Arena_Allocate(p_request_arena, 4096);
570 if (!p_request_header) { perror("Dowa_Arena_Allocate"); return -1; }
571
572 int request_len = Seobeo_Web_GenerateRequestHeader(p_request_header, host, path);
573
574 Seobeo_Handle_Queue(h, (uint8 *)p_request_header, (uint32)request_len);
575 if (Seobeo_Handle_Flush(h) < 0)
576 {
577 perror("Seobeo_Handle_Flush");
578 Seobeo_Handle_Destroy(h);
579 return -1;
580 }
581
582 size_t cap = 1024*8, used = 0;
583 char *p_request_body = Dowa_Arena_Allocate(p_request_arena, cap);
584 if (!p_request_body) { Seobeo_Handle_Destroy(h); return -1; }
585
586 while (1)
587 {
588 int n = Seobeo_Handle_Read(h);
589 Seobeo_Log(SEOBEO_DEBUG, "Received size: %d bytes\n", n);
590 if (n > 0)
591 {
592 // TODO: Maybe directly use arena inside of the struct? idk if that is useful or not...
593 memcpy(p_request_body + used, h->read_buffer, h->read_buffer_len);
594 used += h->read_buffer_len;
595 Seobeo_Handle_Consume(h, (uint32)h->read_buffer_len);
596 }
597 else if (n == 0)
598 {
599 // Wait
600 continue;
601 }
602 else if (n == -2)
603 {
604 Seobeo_Log(SEOBEO_DEBUG, "Connection closed by client\n");
605 break;
606 }
607 else
608 {
609 Dowa_Arena_Free(p_request_arena);
610 Seobeo_Handle_Destroy(h);
611 return -1;
612 }
613 }
614
615 Seobeo_Log(SEOBEO_DEBUG, "Request body: %s\n", p_request_body);
616 Dowa_Arena_Free(p_request_arena);
617 Seobeo_Handle_Destroy(h);
618 return 0;
619 }
620
621 /* Router logic */ 527 /* Router logic */
622 struct Seobeo_Route_Struct { 528 struct Seobeo_Route_Struct {
623 char *method; // "GET", "POST", "PUT", "DELETE" 529 char *method; // "GET", "POST", "PUT", "DELETE"
624 char *path_pattern; // "/v1/users/:id/posts/:post_id" 530 char *path_pattern; // "/v1/users/:id/posts/:post_id"
625 Seobeo_Route_Handler handler; 531 Seobeo_Route_Handler handler;