Mercurial
comparison seobeo/main.c @ 5:3e12bf044589
Fixed Dowa hashmap to recursively add files into memory.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Sat, 27 Sep 2025 16:23:04 -0700 |
| parents | 0b3b4f5887bb |
| children | 1e61008b9980 |
comparison
equal
deleted
inserted
replaced
| 4:0b3b4f5887bb | 5:3e12bf044589 |
|---|---|
| 18 errno = saved_errno; | 18 errno = saved_errno; |
| 19 } | 19 } |
| 20 | 20 |
| 21 void HandleClientRequest(Seobeo_PHandle cli) | 21 void HandleClientRequest(Seobeo_PHandle cli) |
| 22 { | 22 { |
| 23 Dowa_PArena response_arena = Dowa_Arena_Create(8192); | |
| 24 if (!response_arena) | |
| 25 { | |
| 26 perror("Dowa_Arena_Initialize"); | |
| 27 goto clean_up; | |
| 28 } | |
| 29 | |
| 23 size_t idx = Dowa_HashMap_GetPosition(cache, "index.html"); | 30 size_t idx = Dowa_HashMap_GetPosition(cache, "index.html"); |
| 24 Dowa_PHashEntry entry = cache->entries[idx]; | 31 Dowa_PHashEntry entry = cache->entries[idx]; |
| 25 | 32 if (!entry) |
| 26 if (!entry) { | 33 { |
| 27 // 404 response if missing | 34 // 404 response if missing |
| 28 const char *not_found = | 35 const char *not_found = |
| 29 "HTTP/1.1 404 Not Found\r\n" | 36 "HTTP/2.0 404 Not Found\r\n" |
| 30 "Content-Length: 0\r\n" | 37 "Content-Length: 0\r\n" |
| 31 "Connection: close\r\n" | 38 "Connection: close\r\n" |
| 32 "\r\n"; | 39 "\r\n"; |
| 33 send(cli->socket, not_found, strlen(not_found), 0); | 40 send(cli->socket, not_found, strlen(not_found), 0); |
| 34 Seobeo_Handle_Destroy(cli); | 41 goto clean_up; |
| 35 return; | |
| 36 } | 42 } |
| 37 | 43 |
| 38 // 3) Prepare header with the correct content‐length | 44 // 3) Prepare header with the correct content‐length |
| 39 size_t body_size = entry->capacity; | 45 size_t body_size = entry->capacity; |
| 40 const char *template = | 46 const char *template = |
| 41 "HTTP/1.1 200 OK\r\n" | 47 "HTTP/2.0 200 OK\r\n" |
| 42 "Content-Type: text/html\r\n" | 48 "Content-Type: text/html\r\n" |
| 43 "Content-Length: %zu\r\n" | 49 "Content-Length: %zu\r\n" |
| 44 "Connection: close\r\n" | 50 "Connection: close\r\n" |
| 45 "\r\n"; | 51 "\r\n"; |
| 46 | 52 |
| 47 // Compute how large the header is and allocate just enough space | 53 // Compute how large the header is and allocate just enough space |
| 48 int header_len = snprintf(NULL, 0, template, body_size); | 54 int header_len = snprintf(NULL, 0, template, body_size); |
| 49 char *header = malloc(header_len + 1); | 55 void *header = Dowa_Arena_Allocate(response_arena, (size_t)(header_len+1)); |
| 50 if (!header) { | 56 if (header == NULL) |
| 51 perror("malloc"); | 57 { |
| 52 return; | 58 perror("Dowa_Arena_Allocate"); |
| 59 goto clean_up; | |
| 53 } | 60 } |
| 54 snprintf(header, header_len + 1, template, body_size); | |
| 55 | 61 |
| 62 snprintf((char *)header, header_len + 1, template, body_size); | |
| 56 Seobeo_Handle_QueueData(cli, | 63 Seobeo_Handle_QueueData(cli, |
| 57 (const uint8*)header, | 64 (const uint8*)header, |
| 58 (uint32)(header_len + 1)); | 65 (uint32)(header_len + 1)); |
| 59 free(header); | |
| 60 | |
| 61 Seobeo_Handle_QueueData(cli, | 66 Seobeo_Handle_QueueData(cli, |
| 62 (const uint8*)entry->buffer, | 67 (const uint8*)entry->buffer, |
| 63 (uint32)body_size); | 68 (uint32)body_size); |
| 69 Seobeo_Handle_Flush(cli); | |
| 70 goto clean_up; | |
| 64 | 71 |
| 65 | 72 clean_up: |
| 66 Seobeo_Handle_Flush(cli); | |
| 67 | |
| 68 Seobeo_Handle_Destroy(cli); | 73 Seobeo_Handle_Destroy(cli); |
| 74 Dowa_Arena_Free(response_arena); | |
| 75 return ; | |
| 69 } | 76 } |
| 70 | 77 |
| 71 int main(void) | 78 int main(void) |
| 72 { | 79 { |
| 73 struct sigaction sa; | 80 struct sigaction sa; |
| 82 Seobeo_PHandle srv = Seobeo_Stream_Handle_Create(NULL, "8080"); | 89 Seobeo_PHandle srv = Seobeo_Stream_Handle_Create(NULL, "8080"); |
| 83 | 90 |
| 84 if (srv->socket < 0) return 1; | 91 if (srv->socket < 0) return 1; |
| 85 printf("Listening on port 8080\n"); | 92 printf("Listening on port 8080\n"); |
| 86 | 93 |
| 87 sa.sa_handler = SigchildHandler; // reap all dead processes | 94 // TODO: Use epoll or something else. |
| 95 // Code from Beej's book | |
| 96 // Handling child processes | |
| 97 sa.sa_handler = SigchildHandler; | |
| 88 sigemptyset(&sa.sa_mask); | 98 sigemptyset(&sa.sa_mask); |
| 89 sa.sa_flags = SA_RESTART; | 99 sa.sa_flags = SA_RESTART; |
| 90 if (sigaction(SIGCHLD, &sa, NULL) == -1) { | 100 if (sigaction(SIGCHLD, &sa, NULL) == -1) |
| 101 { | |
| 91 perror("sigaction"); | 102 perror("sigaction"); |
| 92 exit(1); | 103 exit(1); |
| 93 } | 104 } |
| 94 | 105 |
| 95 while (1) { | 106 while (1) |
| 107 { | |
| 96 Seobeo_PHandle cli = Seobeo_Stream_Handle_Accept(srv); | 108 Seobeo_PHandle cli = Seobeo_Stream_Handle_Accept(srv); |
| 97 if (cli == NULL) | 109 if (cli == NULL) |
| 98 { | 110 { |
| 99 continue; | 111 continue; |
| 100 } | 112 } |
| 101 printf("connected to %s\n", cli->host); | 113 printf("client connected from %s\n", cli->host); |
| 102 | 114 |
| 103 if (!fork()) | 115 if (!fork()) |
| 104 { | 116 { |
| 105 HandleClientRequest(cli); | 117 HandleClientRequest(cli); |
| 106 exit(0); | 118 exit(0); |