comparison seobeo/s_web.c @ 19:875bb6e10db7

[Seobeo] Chaning Function naming to be easily readable.
author June Park <parkjune1995@gmail.com>
date Mon, 06 Oct 2025 09:55:34 -0700
parents fa2b8af609d9
children 09def63429b9
comparison
equal deleted inserted replaced
18:fa2b8af609d9 19:875bb6e10db7
24 "\r\n", 24 "\r\n",
25 path, host 25 path, host
26 ); 26 );
27 } 27 }
28 28
29 void Seobeo_Web_GenerateResponseHeader(void *buffer, int status, 29 void Seobeo_Web_Header_Generate(void *buffer, int status,
30 const char *content_type, const int content_length) 30 const char *content_type, const int content_length)
31 { 31 {
32 const char *status_text; 32 const char *status_text;
33 switch(status) 33 switch(status)
34 { 34 {
69 69
70 void *p_response_header = Dowa_Arena_Allocate(p_response_arena, (size_t)2048); 70 void *p_response_header = Dowa_Arena_Allocate(p_response_arena, (size_t)2048);
71 if (!p_response_header) { perror("Dowa_Arena_Allocate"); goto clean_up; } 71 if (!p_response_header) { perror("Dowa_Arena_Allocate"); goto clean_up; }
72 72
73 p_req_map = Dowa_HashMap_Create(32); 73 p_req_map = Dowa_HashMap_Create(32);
74 if (Seobeo_Web_ParseClientHeader(p_cli_handle, p_req_map) != 0) 74 if (Seobeo_Web_Header_Parse(p_cli_handle, p_req_map) != 0)
75 { 75 {
76 // malformed request or closed — respond 400 76 // malformed request or closed — respond 400
77 Seobeo_Web_GenerateResponseHeader(p_response_header, 77 Seobeo_Web_Header_Generate(p_response_header,
78 HTTP_BAD_REQUEST, 78 HTTP_BAD_REQUEST,
79 "text/plain", 0); 79 "text/plain", 0);
80 Seobeo_Handle_Queue(p_cli_handle, 80 Seobeo_Handle_Queue(p_cli_handle,
81 (const uint8*)p_response_header, 81 (const uint8*)p_response_header,
82 (uint32)strlen(p_response_header)); 82 (uint32)strlen(p_response_header));
124 124
125 p_current = Dowa_HashMap_Get(p_current, dir); 125 p_current = Dowa_HashMap_Get(p_current, dir);
126 if (!p_current) 126 if (!p_current)
127 { 127 {
128 fprintf(stderr, "No value in hashmap key: %s\n\n", dir); 128 fprintf(stderr, "No value in hashmap key: %s\n\n", dir);
129 Seobeo_Web_GenerateResponseHeader(p_response_header, 129 Seobeo_Web_Header_Generate(p_response_header,
130 HTTP_NOT_FOUND, 130 HTTP_NOT_FOUND,
131 "text/html", 0); 131 "text/html", 0);
132 Seobeo_Handle_Queue(p_cli_handle, 132 Seobeo_Handle_Queue(p_cli_handle,
133 (const uint8*)p_response_header, 133 (const uint8*)p_response_header,
134 (uint32)strlen(p_response_header)); 134 (uint32)strlen(p_response_header));
141 entry = p_current->entries[pos]; 141 entry = p_current->entries[pos];
142 142
143 // Missing so 404 143 // Missing so 404
144 if (!entry) 144 if (!entry)
145 { 145 {
146 Seobeo_Web_GenerateResponseHeader(p_response_header, 146 Seobeo_Web_Header_Generate(p_response_header,
147 HTTP_NOT_FOUND, 147 HTTP_NOT_FOUND,
148 "text/html", 0); 148 "text/html", 0);
149 Seobeo_Handle_Queue(p_cli_handle, 149 Seobeo_Handle_Queue(p_cli_handle,
150 (const uint8*)p_response_header, 150 (const uint8*)p_response_header,
151 (uint32)strlen(p_response_header)); 151 (uint32)strlen(p_response_header));
166 else if (strstr(file_path, ".json")) mime = "application/json"; 166 else if (strstr(file_path, ".json")) mime = "application/json";
167 167
168 size_t body_size = entry->capacity; 168 size_t body_size = entry->capacity;
169 printf("key: %s\n\n", entry->key); 169 printf("key: %s\n\n", entry->key);
170 printf("Body Size: %zu\n\n", body_size); 170 printf("Body Size: %zu\n\n", body_size);
171 Seobeo_Web_GenerateResponseHeader(p_response_header, 171 Seobeo_Web_Header_Generate(p_response_header,
172 HTTP_OK, 172 HTTP_OK,
173 mime, 173 mime,
174 body_size); 174 body_size);
175 175
176 printf("response header: %s, data: %d\n\n", p_response_header, (uint32)strlen(p_response_header)); 176 printf("response header: %s, data: %d\n\n", p_response_header, (uint32)strlen(p_response_header));
191 Dowa_Arena_Free(p_response_arena); 191 Dowa_Arena_Free(p_response_arena);
192 if (p_req_map) 192 if (p_req_map)
193 Dowa_HashMap_Free(p_req_map); // TODO: Maybe initilized hashmap within the Arena? 193 Dowa_HashMap_Free(p_req_map); // TODO: Maybe initilized hashmap within the Arena?
194 } 194 }
195 195
196 int Seobeo_Web_ParseClientHeader(Seobeo_PHandle p_handle, Dowa_PHashMap map) 196 int Seobeo_Web_Header_Parse(Seobeo_PHandle p_handle, Dowa_PHashMap map)
197 { 197 {
198 // 1) Fill read_buffer until we see "\r\n\r\n" 198 // 1) Fill read_buffer until we see "\r\n\r\n"
199 while (1) 199 while (1)
200 { 200 {
201 int r = Seobeo_Handle_Read(p_handle); 201 int r = Seobeo_Handle_Read(p_handle);
300 while(waitpid(-1, NULL, WNOHANG) > 0); 300 while(waitpid(-1, NULL, WNOHANG) > 0);
301 301
302 errno = saved_errno; 302 errno = saved_errno;
303 } 303 }
304 304
305 int Seobeo_Web_StartBasicHTTPServer( 305 int Seobeo_Web_Server_Start(
306 const char *folder_path, 306 const char *folder_path,
307 const char *port, 307 const char *port,
308 Seobeo_ServerMode mode, 308 Seobeo_ServerMode mode,
309 int thread_count) 309 int thread_count)
310 { 310 {
332 sa.sa_flags = SA_RESTART; 332 sa.sa_flags = SA_RESTART;
333 sigaction(SIGCHLD, &sa, NULL); 333 sigaction(SIGCHLD, &sa, NULL);
334 334
335 while (1) { 335 while (1) {
336 Seobeo_PHandle cli = 336 Seobeo_PHandle cli =
337 Seobeo_Stream_Handle_Accept(p_server_handle); 337 Seobeo_Stream_Handle_Server_Accept(p_server_handle);
338 if (!cli) continue; 338 if (!cli) continue;
339 339
340 if (fork() == 0) 340 if (fork() == 0)
341 { 341 {
342 Seobeo_Web_HandleClientRequest(cli, 342 Seobeo_Web_HandleClientRequest(cli,
354 } 354 }
355 355
356 return -1; 356 return -1;
357 } 357 }
358 358
359 359 int Seobeo_Web_Client_Get(const char *host,
360 int Seobeo_Web_ClientGet(const char *host, 360 const char *port,
361 const char *port, 361 const char *path)
362 const char *path)
363 { 362 {
364 Seobeo_PHandle h = Seobeo_Stream_Handle_Client_Create(host, port, TRUE); 363 Seobeo_PHandle h = Seobeo_Stream_Handle_Client_Create(host, port, TRUE);
365 if (!h || h->socket < 0) 364 if (!h || h->socket < 0)
366 { 365 {
367 if (h) 366 if (h)
422 printf("%s", p_request_body); 421 printf("%s", p_request_body);
423 Dowa_Arena_Free(p_request_arena); 422 Dowa_Arena_Free(p_request_arena);
424 Seobeo_Handle_Destroy(h); 423 Seobeo_Handle_Destroy(h);
425 return 0; 424 return 0;
426 } 425 }
426
427 void Seobeo_Web_SSL_Init()
428 {
429 SSL_load_error_strings();
430 OpenSSL_add_ssl_algorithms();
431 }
432
433 void Seobeo_Web_SSL_Cleanup(void)
434 {
435 EVP_cleanup(); // I don't think these are needed...
436 }