Mercurial
comparison seobeo/s_web.c @ 21:09def63429b9
[Dowa] Updated the naming scheme and tests.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 10:57:30 -0700 |
| parents | 875bb6e10db7 |
| children | 947b81010aba |
comparison
equal
deleted
inserted
replaced
| 20:0a9e67c7039a | 21:09def63429b9 |
|---|---|
| 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_PHashMap p_html_cache) |
| 60 { | 60 { |
| 61 Dowa_PArena p_response_arena = Dowa_Arena_Create(8192); | |
| 62 Dowa_PHashMap p_req_map = NULL; | |
| 63 | |
| 64 Dowa_PHashEntry entry = NULL; | 61 Dowa_PHashEntry entry = NULL; |
| 65 Dowa_PHashMap p_current = p_html_cache; | 62 Dowa_PHashMap p_current = p_html_cache; |
| 66 char *slash; | 63 char *slash; |
| 67 | 64 |
| 65 Dowa_PArena p_response_arena = Dowa_Arena_Create(8192); | |
| 68 if (!p_response_arena) { perror("Dowa_Arena_Initialize"); goto clean_up; } | 66 if (!p_response_arena) { perror("Dowa_Arena_Initialize"); goto clean_up; } |
| 69 | 67 |
| 70 void *p_response_header = Dowa_Arena_Allocate(p_response_arena, (size_t)2048); | 68 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; } | 69 if (!p_response_header) { perror("Dowa_Arena_Allocate"); goto clean_up; } |
| 72 | 70 |
| 73 p_req_map = Dowa_HashMap_Create(32); | 71 Dowa_PHashMap p_req_map = Dowa_HashMap_Create_With_Arena(32, p_response_arena); |
| 74 if (Seobeo_Web_Header_Parse(p_cli_handle, p_req_map) != 0) | 72 if (Seobeo_Web_Header_Parse(p_cli_handle, p_req_map) != 0) |
| 75 { | 73 { |
| 76 // malformed request or closed — respond 400 | 74 // malformed request or closed — respond 400 |
| 77 Seobeo_Web_Header_Generate(p_response_header, | 75 Seobeo_Web_Header_Generate(p_response_header, |
| 78 HTTP_BAD_REQUEST, | 76 HTTP_BAD_REQUEST, |
| 135 Seobeo_Handle_Flush(p_cli_handle); | 133 Seobeo_Handle_Flush(p_cli_handle); |
| 136 goto clean_up; | 134 goto clean_up; |
| 137 } | 135 } |
| 138 } | 136 } |
| 139 | 137 |
| 140 size_t pos = Dowa_HashMap_GetPosition(p_current, file_path); | 138 size_t pos = Dowa_HashMap_Get_Position(p_current, file_path); |
| 141 entry = p_current->entries[pos]; | 139 entry = p_current->entries[pos]; |
| 142 | 140 |
| 143 // Missing so 404 | 141 // Missing so 404 |
| 144 if (!entry) | 142 if (!entry) |
| 145 { | 143 { |
| 186 | 184 |
| 187 clean_up: | 185 clean_up: |
| 188 if (p_cli_handle) | 186 if (p_cli_handle) |
| 189 Seobeo_Handle_Destroy(p_cli_handle); | 187 Seobeo_Handle_Destroy(p_cli_handle); |
| 190 if (p_response_arena) | 188 if (p_response_arena) |
| 191 Dowa_Arena_Free(p_response_arena); | 189 Dowa_Arena_Destroy(p_response_arena); |
| 192 if (p_req_map) | |
| 193 Dowa_HashMap_Free(p_req_map); // TODO: Maybe initilized hashmap within the Arena? | |
| 194 } | 190 } |
| 195 | 191 |
| 196 int Seobeo_Web_Header_Parse(Seobeo_PHandle p_handle, Dowa_PHashMap map) | 192 int Seobeo_Web_Header_Parse(Seobeo_PHandle p_handle, Dowa_PHashMap map) |
| 197 { | 193 { |
| 198 // 1) Fill read_buffer until we see "\r\n\r\n" | 194 // 1) Fill read_buffer until we see "\r\n\r\n" |
| 219 if (sscanf(buf, "%15s %255s %15s", method, path, version) != 3) | 215 if (sscanf(buf, "%15s %255s %15s", method, path, version) != 3) |
| 220 { | 216 { |
| 221 return -1; | 217 return -1; |
| 222 } | 218 } |
| 223 | 219 |
| 224 Dowa_HashMap_PushValueWithType(map, "Method", method, strlen(method) + 1, DOWA_HASH_MAP_TYPE_STRING); | 220 Dowa_HashMap_Push_Value_With_Type(map, "Method", method, strlen(method) + 1, DOWA_HASH_MAP_TYPE_STRING); |
| 225 Dowa_HashMap_PushValueWithType(map, "Path", path, strlen(path) + 1, DOWA_HASH_MAP_TYPE_STRING); | 221 Dowa_HashMap_Push_Value_With_Type(map, "Path", path, strlen(path) + 1, DOWA_HASH_MAP_TYPE_STRING); |
| 226 Dowa_HashMap_PushValueWithType(map, "Version", version, strlen(version) + 1, DOWA_HASH_MAP_TYPE_STRING); | 222 Dowa_HashMap_Push_Value_With_Type(map, "Version", version, strlen(version) + 1, DOWA_HASH_MAP_TYPE_STRING); |
| 227 | 223 |
| 228 // 3) Parse each header line until the blank line | 224 // 3) Parse each header line until the blank line |
| 229 char *line = buf + strlen(method) + 1 + strlen(path) + 1 + strlen(version) + 2; | 225 char *line = buf + strlen(method) + 1 + strlen(path) + 1 + strlen(version) + 2; |
| 230 while (line < hdr_end) | 226 while (line < hdr_end) |
| 231 { | 227 { |
| 251 | 247 |
| 252 char *val = malloc(value_len + 1); | 248 char *val = malloc(value_len + 1); |
| 253 memcpy(val, val_start, value_len); | 249 memcpy(val, val_start, value_len); |
| 254 val[value_len] = '\0'; | 250 val[value_len] = '\0'; |
| 255 | 251 |
| 256 Dowa_HashMap_PushValue(map, key, val, value_len + 1); | 252 Dowa_HashMap_Push_Value(map, key, val, value_len + 1); |
| 257 | 253 |
| 258 free(key); | 254 free(key); |
| 259 free(val); | 255 free(val); |
| 260 } | 256 } |
| 261 | 257 |
| 262 line = next + 2; | 258 line = next + 2; |
| 263 } | 259 } |
| 264 Seobeo_Handle_Consume(p_handle, (uint32)hdr_len); | 260 Seobeo_Handle_Consume(p_handle, (uint32)hdr_len); |
| 265 | 261 |
| 266 // 4) If Content-Length was provided, read that much body | 262 // 4) If Content-Length was provided, read that much body |
| 267 int content_length_pos = Dowa_HashMap_GetPosition(map, "Content-Length"); | 263 int content_length_pos = Dowa_HashMap_Get_Position(map, "Content-Length"); |
| 268 Dowa_PHashEntry p_content_length_entry = map->entries[content_length_pos]; | 264 Dowa_PHashEntry p_content_length_entry = map->entries[content_length_pos]; |
| 269 if (p_content_length_entry) | 265 if (p_content_length_entry) |
| 270 { | 266 { |
| 271 size_t body_len = atoi((char*)p_content_length_entry->buffer); | 267 size_t body_len = atoi((char*)p_content_length_entry->buffer); |
| 272 while (p_handle->read_buffer_len < body_len) | 268 while (p_handle->read_buffer_len < body_len) |
| 278 | 274 |
| 279 char *body = malloc(body_len + 1); | 275 char *body = malloc(body_len + 1); |
| 280 memcpy(body, p_handle->read_buffer, body_len); | 276 memcpy(body, p_handle->read_buffer, body_len); |
| 281 body[body_len] = '\0'; | 277 body[body_len] = '\0'; |
| 282 | 278 |
| 283 Dowa_HashMap_PushValue(map, "Body", body, body_len + 1); | 279 Dowa_HashMap_Push_Value(map, "Body", body, body_len + 1); |
| 284 free(body); | 280 free(body); |
| 285 | 281 |
| 286 Seobeo_Handle_Consume(p_handle, (uint32)body_len); | 282 Seobeo_Handle_Consume(p_handle, (uint32)body_len); |
| 287 } | 283 } |
| 288 | 284 |
| 306 const char *folder_path, | 302 const char *folder_path, |
| 307 const char *port, | 303 const char *port, |
| 308 Seobeo_ServerMode mode, | 304 Seobeo_ServerMode mode, |
| 309 int thread_count) | 305 int thread_count) |
| 310 { | 306 { |
| 311 Dowa_PHashMap p_html_cache = Dowa_HashMap_Create(1024); | 307 Dowa_PHashMap p_html_cache = Dowa_HashMap_Create(200); |
| 312 if (Dowa_HashMap_Cache_Folder(p_html_cache, | 308 if (Dowa_HashMap_Cache_Folder(p_html_cache, |
| 313 folder_path) != 0) | 309 folder_path) != 0) |
| 314 { | 310 { |
| 315 perror("Dowa_Cache_Folder"); | 311 perror("Dowa_Cache_Folder"); |
| 316 return -1; | 312 return -1; |
| 410 // peer closed; we’ve got everything | 406 // peer closed; we’ve got everything |
| 411 printf("\n\nCLOSED\n\n"); | 407 printf("\n\nCLOSED\n\n"); |
| 412 break; | 408 break; |
| 413 }else | 409 }else |
| 414 { | 410 { |
| 415 Dowa_Arena_Free(p_request_arena); | 411 Dowa_Arena_Destroy(p_request_arena); |
| 416 Seobeo_Handle_Destroy(h); | 412 Seobeo_Handle_Destroy(h); |
| 417 return -1; | 413 return -1; |
| 418 } | 414 } |
| 419 } | 415 } |
| 420 | 416 |
| 421 printf("%s", p_request_body); | 417 printf("%s", p_request_body); |
| 422 Dowa_Arena_Free(p_request_arena); | 418 Dowa_Arena_Destroy(p_request_arena); |
| 423 Seobeo_Handle_Destroy(h); | 419 Seobeo_Handle_Destroy(h); |
| 424 return 0; | 420 return 0; |
| 425 } | 421 } |
| 426 | 422 |
| 427 void Seobeo_Web_SSL_Init() | 423 void Seobeo_Web_SSL_Init() |