Mercurial
comparison seobeo/s_web.c @ 65:ecb6ee6a22c3
[Misc] I will no longer drink cool aids of capital P.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 24 Dec 2025 06:22:59 -0800 |
| parents | ea9ef388ab97 |
| children | a0f0ad5e42eb |
comparison
equal
deleted
inserted
replaced
| 64:a30944e5719e | 65:ecb6ee6a22c3 |
|---|---|
| 53 "\r\n", | 53 "\r\n", |
| 54 status, status_text, content_type, content_length | 54 status, status_text, content_type, content_length |
| 55 ); | 55 ); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void Seobeo_Web_HandleClientRequest(Seobeo_PHandle p_cli_handle, | 58 void Seobeo_Web_HandleClientRequest(Seobeo_PHandle p_cli_handle, |
| 59 Dowa_PHashMap p_html_cache) | 59 Dowa_HashMap *p_html_cache) |
| 60 { | 60 { |
| 61 printf("p_cli_handle: %p", p_cli_handle); | 61 printf("p_cli_handle: %p", p_cli_handle); |
| 62 Dowa_PHashEntry entry = NULL; | 62 Dowa_HashEntry *entry = NULL; |
| 63 Dowa_PHashMap p_current = p_html_cache; | 63 Dowa_HashMap *p_current = p_html_cache; |
| 64 char *slash; | 64 char *slash; |
| 65 | 65 |
| 66 Dowa_PArena p_response_arena = Dowa_Arena_Create(1*1024*1024); | 66 Dowa_Arena *p_response_arena = Dowa_Arena_Create(1*1024*1024); |
| 67 if (!p_response_arena) { perror("Dowa_Arena_Initialize"); goto clean_up; } | 67 if (!p_response_arena) { perror("Dowa_Arena_Initialize"); goto clean_up; } |
| 68 | 68 |
| 69 void *p_response_header = Dowa_Arena_Allocate(p_response_arena, (size_t)2048); | 69 void *p_response_header = Dowa_Arena_Allocate(p_response_arena, (size_t)2048); |
| 70 if (!p_response_header) { perror("Dowa_Arena_Allocate"); goto clean_up; } | 70 if (!p_response_header) { perror("Dowa_Arena_Allocate"); goto clean_up; } |
| 71 | 71 |
| 72 // Parse request headers into hashmap | 72 // Parse request headers into hashmap |
| 73 Dowa_PHashMap p_req_map = Dowa_HashMap_Create_With_Arena(100, p_response_arena); | 73 Dowa_HashMap *p_req_map = Dowa_HashMap_Create_With_Arena(100, p_response_arena); |
| 74 if (Seobeo_Web_Header_Parse(p_cli_handle, p_req_map) != 0) | 74 if (Seobeo_Web_Header_Parse(p_cli_handle, p_req_map) != 0) |
| 75 { | 75 { |
| 76 Seobeo_Web_Header_Generate(p_response_header, | 76 Seobeo_Web_Header_Generate(p_response_header, |
| 77 HTTP_BAD_REQUEST, | 77 HTTP_BAD_REQUEST, |
| 78 "text/plain", 0); | 78 "text/plain", 0); |
| 99 Seobeo_Handle_Flush(p_cli_handle); | 99 Seobeo_Handle_Flush(p_cli_handle); |
| 100 goto clean_up; | 100 goto clean_up; |
| 101 } | 101 } |
| 102 | 102 |
| 103 // --- Separate GET map for caching or routing --- | 103 // --- Separate GET map for caching or routing --- |
| 104 Dowa_PHashMap p_get_map = Dowa_HashMap_Create(64); | 104 Dowa_HashMap *p_get_map = Dowa_HashMap_Create(64); |
| 105 if (!p_get_map) | 105 if (!p_get_map) |
| 106 { | 106 { |
| 107 perror("Dowa_HashMap_Create (p_get_map)"); | 107 perror("Dowa_HashMap_Create (p_get_map)"); |
| 108 goto clean_up; | 108 goto clean_up; |
| 109 } | 109 } |
| 258 Dowa_Arena_Destroy(p_response_arena); | 258 Dowa_Arena_Destroy(p_response_arena); |
| 259 return; | 259 return; |
| 260 } | 260 } |
| 261 | 261 |
| 262 | 262 |
| 263 int Seobeo_Web_Header_Parse(Seobeo_PHandle p_handle, Dowa_PHashMap map) | 263 int Seobeo_Web_Header_Parse(Seobeo_PHandle p_handle, Dowa_HashMap *map) |
| 264 { | 264 { |
| 265 // 1) Fill read_buffer until we see "\r\n\r\n" | 265 // 1) Fill read_buffer until we see "\r\n\r\n" |
| 266 while (1) | 266 while (1) |
| 267 { | 267 { |
| 268 int r = Seobeo_Handle_Read(p_handle); | 268 int r = Seobeo_Handle_Read(p_handle); |
| 314 | 314 |
| 315 // 2) If there *is* a query, tokenize into a sub-map | 315 // 2) If there *is* a query, tokenize into a sub-map |
| 316 if (query_str && *query_str) | 316 if (query_str && *query_str) |
| 317 { | 317 { |
| 318 // create nested map for GET params | 318 // create nested map for GET params |
| 319 Dowa_PHashMap p_query_map = Dowa_HashMap_Create_With_Arena(100, map->p_arena); | 319 Dowa_HashMap *p_query_map = Dowa_HashMap_Create_With_Arena(100, map->p_arena); |
| 320 | 320 |
| 321 char *cur = query_str; | 321 char *cur = query_str; |
| 322 while (cur && *cur) | 322 while (cur && *cur) |
| 323 { | 323 { |
| 324 // find the next '&' | 324 // find the next '&' |
| 367 printf("Something went wrong...\n\n"); | 367 printf("Something went wrong...\n\n"); |
| 368 } | 368 } |
| 369 } | 369 } |
| 370 | 370 |
| 371 // int qp = Dowa_HashMap_Get_Position(map, "QueryParams"); | 371 // int qp = Dowa_HashMap_Get_Position(map, "QueryParams"); |
| 372 // Dowa_PHashEntry p_qp_entry = map->entries[qp]; | 372 // Dowa_HashEntry *p_qp_entry = map->entries[qp]; |
| 373 // printf("query param key: %s\n", p_qp_entry->key); | 373 // printf("query param key: %s\n", p_qp_entry->key); |
| 374 // printf("query param value: %s\n",(char *)Dowa_HashMap_Get(p_qp_entry->buffer, "hello")); | 374 // printf("query param value: %s\n",(char *)Dowa_HashMap_Get(p_qp_entry->buffer, "hello")); |
| 375 | 375 |
| 376 // 3) Parse each header line until the blank line | 376 // 3) Parse each header line until the blank line |
| 377 char *line = buf + strlen(method) + 1 + strlen(path) + 1 + strlen(version) + 2; | 377 char *line = buf + strlen(method) + 1 + strlen(path) + 1 + strlen(version) + 2; |
| 416 } | 416 } |
| 417 Seobeo_Handle_Consume(p_handle, (uint32)hdr_len); | 417 Seobeo_Handle_Consume(p_handle, (uint32)hdr_len); |
| 418 | 418 |
| 419 // 4) If Content-Length was provided, read that much body | 419 // 4) If Content-Length was provided, read that much body |
| 420 int content_length_pos = Dowa_HashMap_Get_Position(map, "Content-Length"); | 420 int content_length_pos = Dowa_HashMap_Get_Position(map, "Content-Length"); |
| 421 Dowa_PHashEntry p_content_length_entry = map->entries[content_length_pos]; | 421 Dowa_HashEntry *p_content_length_entry = map->entries[content_length_pos]; |
| 422 if (p_content_length_entry) | 422 if (p_content_length_entry) |
| 423 { | 423 { |
| 424 size_t body_len = atoi((char*)p_content_length_entry->buffer); | 424 size_t body_len = atoi((char*)p_content_length_entry->buffer); |
| 425 while (p_handle->read_buffer_len < body_len) | 425 while (p_handle->read_buffer_len < body_len) |
| 426 { | 426 { |
| 459 const char *folder_path, | 459 const char *folder_path, |
| 460 const char *port, | 460 const char *port, |
| 461 Seobeo_ServerMode mode, | 461 Seobeo_ServerMode mode, |
| 462 int thread_count) | 462 int thread_count) |
| 463 { | 463 { |
| 464 Dowa_PHashMap p_html_cache = Dowa_HashMap_Create(200); | 464 Dowa_HashMap *p_html_cache = Dowa_HashMap_Create(200); |
| 465 if (Dowa_HashMap_Cache_Folder(p_html_cache, | 465 if (Dowa_HashMap_Cache_Folder(p_html_cache, |
| 466 folder_path) != 0) | 466 folder_path) != 0) |
| 467 { | 467 { |
| 468 perror("Dowa_Cache_Folder"); | 468 perror("Dowa_Cache_Folder"); |
| 469 return -1; | 469 return -1; |
| 520 if (h) | 520 if (h) |
| 521 Seobeo_Handle_Destroy(h); | 521 Seobeo_Handle_Destroy(h); |
| 522 return -1; | 522 return -1; |
| 523 } | 523 } |
| 524 | 524 |
| 525 Dowa_PArena p_request_arena = Dowa_Arena_Create(1 * 1024 * 1024); | 525 Dowa_Arena *p_request_arena = Dowa_Arena_Create(1 * 1024 * 1024); |
| 526 if (!p_request_arena) { perror("Dowa_Arena_Create"); return -1; } | 526 if (!p_request_arena) { perror("Dowa_Arena_Create"); return -1; } |
| 527 | 527 |
| 528 void *p_request_header = Dowa_Arena_Allocate(p_request_arena, 4096); | 528 void *p_request_header = Dowa_Arena_Allocate(p_request_arena, 4096); |
| 529 if (!p_request_header) { perror("Dowa_Arena_Allocate"); return -1; } | 529 if (!p_request_header) { perror("Dowa_Arena_Allocate"); return -1; } |
| 530 | 530 |