Mercurial
changeset 110:99c4530e4629
[Seobeo] Small Syntax fixes.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sat, 03 Jan 2026 21:16:17 -0800 |
| parents | 1c446ab6f945 |
| children | 48f260576059 |
| files | seobeo/s_web.c |
| diffstat | 1 files changed, 18 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/seobeo/s_web.c Sat Jan 03 19:37:51 2026 -0800 +++ b/seobeo/s_web.c Sat Jan 03 21:16:17 2026 -0800 @@ -1,6 +1,5 @@ #include "seobeo/seobeo.h" -// Global folder path for serving files static char g_folder_path[512] = "."; int Seobeo_Web_GenerateRequestHeader( @@ -159,7 +158,6 @@ goto clean_up; } - // Extract path void *p_path_kv = Dowa_HashMap_Get_Ptr(p_req_map, "Path"); const char *path = p_path_kv ? ((Seobeo_Request_Entry*)p_path_kv)->value : "/"; @@ -172,7 +170,7 @@ goto clean_up; } - // --- Handle different HTTP methods (static files fallback) --- + // --- Static files fallback for GET --- if (strcmp(method, "GET") == 0) { void *p_kv = Dowa_HashMap_Get_Ptr(p_req_map, "Path"); @@ -282,7 +280,6 @@ int Seobeo_Web_Header_Parse(Seobeo_Handle *p_handle, Seobeo_Request_Entry **pp_map, Dowa_Arena *p_arena) { - // 1) Fill read_buffer until we see "\r\n\r\n" while (1) { int r = Seobeo_Handle_Read(p_handle); @@ -293,14 +290,13 @@ if (p_handle->read_buffer_len >= 4 && strstr((char*)p_handle->read_buffer, "\r\n\r\n") != NULL) - { break; - } + if (r == 0) return 1; // EAGAIN, try again later TODO: Add this as part of Handle struct. } - // 2) Parse request‐line "METHOD SP PATH SP VERSION CRLF" + // "METHOD SP PATH SP VERSION CRLF" char *buf = (char*)p_handle->read_buffer; char *hdr_end = strstr(buf, "\r\n\r\n"); size_t hdr_len = hdr_end - buf + 4; @@ -328,7 +324,6 @@ return -1; } - // Copy strings to arena and store in hashmap Seobeo_Log(SEOBEO_DEBUG, "Allocating method_copy\n"); char *method_copy = Dowa_Arena_Allocate(p_arena, strlen(method) + 1); if (!method_copy) { Seobeo_Log(SEOBEO_ERROR, "Failed to allocate method_copy\n"); return -1; } @@ -344,7 +339,6 @@ Dowa_HashMap_Push_Arena(*pp_map, "Version", version_copy, p_arena); Seobeo_Log(SEOBEO_DEBUG, "Map now has %zu entries\n", Dowa_Array_Length(*pp_map)); - // 1) Separate raw path and query string char *raw_path = path; char *query_start = strchr(raw_path, '?'); char *query_str = NULL; @@ -355,13 +349,12 @@ query_str = query_start + 1; } - // push only the clean path char *path_copy = Dowa_Arena_Allocate(p_arena, strlen(raw_path) + 1); if (!path_copy) return -1; strcpy(path_copy, raw_path); Dowa_HashMap_Push_Arena(*pp_map, "Path", path_copy, p_arena); - // 2) If there *is* a query, tokenize into the same map with "query_" prefix + // GET Params if (query_str && *query_str) { char *cur = query_str; @@ -377,6 +370,7 @@ size_t val_len = pair_end - (eq + 1); char key_buf[256]; + // Adding query_ in front to separate GET params snprintf(key_buf, sizeof(key_buf), "query_%.*s", (int)key_len, cur); char *val_copy = Dowa_Arena_Allocate(p_arena, val_len + 1); @@ -396,7 +390,7 @@ // printf("query param key: %s\n", p_qp_entry->key); // printf("query param value: %s\n",(char *)Dowa_HashMap_Get(p_qp_entry->buffer, "hello")); - // 3) Parse each header line until the blank line + // Parse headers char *line = buf + strlen(method) + 1 + strlen(path) + 1 + strlen(version) + 2; while (line < hdr_end) { @@ -436,7 +430,7 @@ Seobeo_Handle_Consume(p_handle, (uint32)hdr_len); - // 4) If Content-Length was provided, read that much body + // Reading Body void *p_cl_kv = Dowa_HashMap_Get_Ptr(*pp_map, "Content-Length"); if (p_cl_kv) { @@ -455,10 +449,8 @@ size_t total_read = 0; - // Read body in chunks while (total_read < body_len) { - // Copy what's currently in the read buffer size_t available = p_handle->read_buffer_len; size_t to_copy = (body_len - total_read) < available ? (body_len - total_read) : available; @@ -471,7 +463,6 @@ Seobeo_Log(SEOBEO_DEBUG, "Copied %zu bytes, total %zu/%zu\n", to_copy, total_read, body_len); } - // If we still need more data, read another chunk if (total_read < body_len) { int r = Seobeo_Handle_Read(p_handle); @@ -492,17 +483,15 @@ body[body_len] = '\0'; Seobeo_Log(SEOBEO_DEBUG, "Body fully received (%zu bytes)\n", body_len); - // Body is arena-allocated Dowa_HashMap_Push_Arena(*pp_map, "Body", body, p_arena); } - return 0; // success; map now holds Method, Path, Version, headers, and optional Body + return 0; } -// TODO: Do epoll or kqueue depending on the OS. void SigchildHandler(int s) { - (void)s; // quiet unused variable warning + (void)s; // waitpid() might overwrite errno, so we save and restore it: int saved_errno = errno; @@ -518,11 +507,9 @@ Seobeo_ServerMode mode, int thread_count) { - // Store folder path globally if (folder_path) strncpy(g_folder_path, folder_path, sizeof(g_folder_path) - 1); - // Initialize empty cache - files will be loaded on-demand Seobeo_Cache_Entry *p_html_cache = NULL; Seobeo_Handle *p_server_handle = @@ -531,7 +518,6 @@ Seobeo_Log(SEOBEO_INFO, "Listening on port %s\n", port); - // Fork‐based fallback if (mode == SEOBEO_MODE_FORK) { Seobeo_Log(SEOBEO_INFO, "Server mode: FORK\n"); @@ -607,17 +593,18 @@ memcpy(p_request_body + used, h->read_buffer, h->read_buffer_len); used += h->read_buffer_len; Seobeo_Handle_Consume(h, (uint32)h->read_buffer_len); - }else if (n == 0) + } + else if (n == 0) { // Wait continue; - }else if (n == -2) + } + else if (n == -2) { - // Debug - // peer closed; we’ve got everything Seobeo_Log(SEOBEO_DEBUG, "Connection closed by client\n"); break; - }else + } + else { Dowa_Arena_Free(p_request_arena); Seobeo_Handle_Destroy(h); @@ -625,7 +612,6 @@ } } - // Debug Seobeo_Log(SEOBEO_DEBUG, "Request body: %s\n", p_request_body); Dowa_Arena_Free(p_request_arena); Seobeo_Handle_Destroy(h); @@ -668,7 +654,6 @@ Dowa_Array_Push(g_routes, route); } -// Match route and extract path parameters static boolean match_route_and_extract( Seobeo_Route *route, const char *request_path, @@ -678,6 +663,7 @@ Dowa_Arena *p_temp_arena = Dowa_Arena_Create(1024); char **request_segments = Dowa_String_Split(request_path, "/", strlen(request_path), 1, p_temp_arena); size_t request_segment_count = Dowa_Array_Length(request_segments); + // Check segment count matches if (request_segment_count != route->segment_count) { @@ -755,7 +741,6 @@ return; } - // Header int status = HTTP_OK; void *p_status_kv = Dowa_HashMap_Get_Ptr(p_response_map, "status"); if (p_status_kv) @@ -764,7 +749,6 @@ status = atoi(status_str); } - // Body const char *body = ""; void *p_body_kv = Dowa_HashMap_Get_Ptr(p_response_map, "body"); if (p_body_kv) @@ -772,7 +756,6 @@ body = ((Seobeo_Request_Entry*)p_body_kv)->value; } - // Default text plain const char *content_type = "text/html"; void *p_content_type_kv = Dowa_HashMap_Get_Ptr(p_response_map, "content-type"); if (p_content_type_kv) @@ -780,7 +763,6 @@ content_type = ((Seobeo_Request_Entry*)p_content_type_kv)->value; } - // Check for custom content-length (for binary data) size_t body_length = strlen(body); void *p_content_length_kv = Dowa_HashMap_Get_Ptr(p_response_map, "content-length"); if (p_content_length_kv) @@ -789,7 +771,6 @@ body_length = atoi(content_length_str); } - // All other types are default thoguht to be headers. char *header = Dowa_Arena_Allocate(p_arena, 4096); Seobeo_Web_Header_Generate(header, status, content_type, body_length); for (int i = 0; i < Dowa_Array_Length(p_response_map); i++) @@ -798,7 +779,7 @@ strstr(p_response_map[i].key, "status") || strstr(p_response_map[i].key, "body") || strstr(p_response_map[i].key, "content-type") || - strstr(p_response_map[i].key, "content-length") // Skip custom content-length + strstr(p_response_map[i].key, "content-length") ) continue; @@ -810,7 +791,7 @@ } Seobeo_Handle_Queue(p_handle, (uint8_t*)header, strlen(header)); - Seobeo_Handle_Queue(p_handle, (uint8_t*)body, body_length); // Use actual body length + Seobeo_Handle_Queue(p_handle, (uint8_t*)body, body_length); Seobeo_Handle_Flush(p_handle); }