annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
1 /*
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
2 ** server.c -- a stream socket server demo
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
3 */
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
4
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
5 #include "seobeo/seobeo.h"
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
6
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
7 Dowa_PHashMap cache;
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
8
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
9 void SigchildHandler(int s)
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
10 {
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
11 (void)s; // quiet unused variable warning
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
12
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
13 // waitpid() might overwrite errno, so we save and restore it:
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
14 int saved_errno = errno;
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
15
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
16 while(waitpid(-1, NULL, WNOHANG) > 0);
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
17
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
18 errno = saved_errno;
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
19 }
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
20
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
21 void HandleClientRequest(Seobeo_PHandle cli)
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
22 {
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
23 Dowa_PArena response_arena = Dowa_Arena_Create(8192);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
24 if (!response_arena)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
25 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
26 perror("Dowa_Arena_Initialize");
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
27 goto clean_up;
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
28 }
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
29
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
30 size_t idx = Dowa_HashMap_GetPosition(cache, "index.html");
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
31 Dowa_PHashEntry entry = cache->entries[idx];
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
32 if (!entry)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
33 {
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
34 // 404 response if missing
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
35 const char *not_found =
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
36 "HTTP/2.0 404 Not Found\r\n"
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
37 "Content-Length: 0\r\n"
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
38 "Connection: close\r\n"
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
39 "\r\n";
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
40 send(cli->socket, not_found, strlen(not_found), 0);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
41 goto clean_up;
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
42 }
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
43
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
44 // 3) Prepare header with the correct content‐length
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
45 size_t body_size = entry->capacity;
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
46 const char *template =
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
47 "HTTP/2.0 200 OK\r\n"
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
48 "Content-Type: text/html\r\n"
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
49 "Content-Length: %zu\r\n"
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
50 "Connection: close\r\n"
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
51 "\r\n";
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
52
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
53 // Compute how large the header is and allocate just enough space
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
54 int header_len = snprintf(NULL, 0, template, body_size);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
55 void *header = Dowa_Arena_Allocate(response_arena, (size_t)(header_len+1));
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
56 if (header == NULL)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
57 {
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
58 perror("Dowa_Arena_Allocate");
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
59 goto clean_up;
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
60 }
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
61
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
62 snprintf((char *)header, header_len + 1, template, body_size);
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
63 Seobeo_Handle_QueueData(cli,
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
64 (const uint8*)header,
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
65 (uint32)(header_len + 1));
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
66 Seobeo_Handle_QueueData(cli,
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
67 (const uint8*)entry->buffer,
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
68 (uint32)body_size);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
69 Seobeo_Handle_Flush(cli);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
70 goto clean_up;
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
71
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
72 clean_up:
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
73 Seobeo_Handle_Destroy(cli);
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
74 Dowa_Arena_Free(response_arena);
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
75 return ;
1
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
76 }
adcfad6e86fb Updated naming and separated out some logic within seobeo.
June Park <parkjune1995@gmail.com>
parents: 0
diff changeset
77
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
78 int main(void)
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
79 {
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
80 struct sigaction sa;
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
81
3
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
82 cache = Dowa_HashMap_Create(1024);
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
83 if (Dowa_Cache_Folder(cache, "seobeo/pages") != 0)
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
84 {
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
85 perror("Dowa_Cache_Folder");
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
86 return -1;
2758f5527d2b [Seobeo] Working on simple TCP server and client logic.
June Park <parkjune1995@gmail.com>
parents: 1
diff changeset
87 }
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
88
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
89 Seobeo_PHandle srv = Seobeo_Stream_Handle_Create(NULL, "8080");
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
90
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
91 if (srv->socket < 0) return 1;
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
92 printf("Listening on port 8080\n");
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
93
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
94 // TODO: Use epoll or something else.
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
95 // Code from Beej's book
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
96 // Handling child processes
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
97 sa.sa_handler = SigchildHandler;
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
98 sigemptyset(&sa.sa_mask);
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
99 sa.sa_flags = SA_RESTART;
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
100 if (sigaction(SIGCHLD, &sa, NULL) == -1)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
101 {
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
102 perror("sigaction");
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
103 exit(1);
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
104 }
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
105
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
106 while (1)
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
107 {
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
108 Seobeo_PHandle cli = Seobeo_Stream_Handle_Accept(srv);
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
109 if (cli == NULL)
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
110 {
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
111 continue;
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
112 }
5
3e12bf044589 Fixed Dowa hashmap to recursively add files into memory.
June Park <parkjune1995@gmail.com>
parents: 4
diff changeset
113 printf("client connected from %s\n", cli->host);
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
114
4
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
115 if (!fork())
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
116 {
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
117 HandleClientRequest(cli);
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
118 exit(0);
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
119 }
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
120 Seobeo_Handle_Destroy(cli);
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
121 }
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
122
0b3b4f5887bb [Seobeo] Updated so that it create socket for both server and clients.
June Park <parkjune1995@gmail.com>
parents: 3
diff changeset
123 Seobeo_Handle_Destroy(srv);
0
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
124 return 0;
5695ef413be0 Initialized mono repo with bazels with few examples.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
125 }