1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#include <mongoose.h>
#define STB_DS_IMPLEMENTATION
#include <stb_ds.h>
#define NAME_LEN 32
#define CONTENT_LEN 1024
#define HTML_LEN 1024*8
typedef struct post Post;
typedef struct user User;
User * UsersFind(struct mg_str name);
// Post
struct post {
time_t timestamp;
User * user;
char content[CONTENT_LEN];
};
// User
struct user {
char name[NAME_LEN];
Post * posts;
User ** following;
};
User
UserNew(const char * name) {
User result;
strcpy(result.name, name);
result.posts = NULL;
result.following = NULL;
return result;
}
void
UserWrite(User * user) {
char filename[128];
snprintf(filename, 128, "data/users/%s", user->name);
FILE * f = fopen(filename, "w");
int numFollowing = arrlen(user->following);
int numPosts = arrlen(user->posts);
fwrite(&numFollowing, sizeof(int), 1, f);
fwrite(&numPosts, sizeof(int), 1, f);
for (int i = 0; i < numFollowing; i++) {
fwrite(user->following[i]->name, 1, sizeof(user->following[i]->name), f);
}
for (int i = 0; i < numPosts; i++) {
fwrite(&user->posts[i].timestamp, sizeof(user->posts[i].timestamp), 1, f);
fwrite(user->posts[i].user->name, 1, sizeof(user->posts[i].user->name), f);
fwrite(user->posts[i].content, 1, sizeof(user->posts[i].content), f);
}
fclose(f);
}
void
UserRead(User * user) {
char filename[128];
snprintf(filename, 128, "data/users/%s", user->name);
FILE * f = fopen(filename, "r");
int numFollowing;
int numPosts;
fread(&numFollowing, sizeof(int), 1, f);
fread(&numPosts, sizeof(int), 1, f);
for (int i = 0; i < numFollowing; i++) {
char name[NAME_LEN];
fread(name, 1, sizeof(name), f);
arrput(user->following, UsersFind(mg_str(name)));
}
for (int i = 0; i < numPosts; i++) {
char name[NAME_LEN];
Post post;
fread(&post.timestamp, sizeof(post.timestamp), 1, f);
fread(name, 1, sizeof(name), f);
post.user = UsersFind(mg_str(name));
fread(post.content, 1, sizeof(post.content), f);
}
fclose(f);
}
// Users
User ** g_users;
static User pat, yas, tof;
void
UsersSetup() {
pat = UserNew("pat");
yas = UserNew("yas");
tof = UserNew("tof");
UserRead(&pat);
UserRead(&yas);
UserRead(&tof);
arrput(pat.following, &yas);
arrput(yas.following, &pat);
arrput(tof.following, &yas);
arrput(tof.following, &pat);
arrput(g_users, &pat);
arrput(g_users, &yas);
arrput(g_users, &tof);
}
User *
UsersFind(struct mg_str name) {
for (int i = 0; i < arrlen(g_users); i++) {
User * user = g_users[i];
if (strncmp(user->name, name.ptr, name.len) == 0)
return user;
}
return NULL;
}
// HTML
void
http_redirect_home(struct mg_connection * c, struct mg_http_message * hm, User * user) {
struct mg_str *referer = mg_http_get_header(hm, "Referer");
static char redirectHeaders[128];
snprintf(redirectHeaders, 128,
"Location: %.*s\r\n",
referer->len, referer->ptr);
mg_http_reply(c, 303, redirectHeaders, "");
}
char *
html_404()
{
static char html[1024];
snprintf(html, 1024,
"<!DOCTYPE html>"
"<html>"
"<body>"
"<p><i>Eeeeeeh?</i> <b>The Website's Under Construction</b></p>"
"</body>"
"</html>");
return html;
}
char *
html_user_not_found()
{
static char html[1024];
snprintf(html, 1024,
"<!DOCTYPE html>"
"<html>"
"<body>"
"<p>User was not found :/</p>"
"</body>"
"</html>");
return html;
}
char *
html_post(Post * post) {
static char html[2048];
snprintf(html, 2048,
"<p>[%s] <a href=\"/user/%s\">%s</a>: %s</p>",
ctime(&post->timestamp),
post->user->name,
post->user->name,
post->content);
return html;
}
char *
html_home(User * user)
{
Post ** posts = NULL;
for (int i = 0; i < arrlen(user->following); i++) {
User * userF = user->following[i];
for (int j = 0; j < arrlen(userF->posts); j++) {
Post * post = &userF->posts[j];
bool inserted = false;
for (int k = 0; k < arrlen(posts); k++) {
if (post->timestamp > posts[k]->timestamp) {
arrins(posts, k, post);
inserted = true;
break;
}
}
if (! inserted)
arrput(posts, post);
}
}
static char html[HTML_LEN];
snprintf(html, HTML_LEN,
"<!DOCTYPE html>"
"<html>"
"<body>"
"<form action=\"/api/post/%s\" method=\"post\">"
"<input type=\"text\" name=\"content\"><br>"
"<input type=\"submit\" value=\"Chirp!\">"
"</form>",
user->name);
for (int i = 0; i < arrlen(posts); i++) {
snprintf(html+strlen(html), HTML_LEN-strlen(html),
html_post(posts[i]));
}
snprintf(html+strlen(html), HTML_LEN-strlen(html),
"</body>"
"</html>");
return html;
}
char *
html_user(User * user)
{
static char html[HTML_LEN];
snprintf(html, HTML_LEN,
"<!DOCTYPE html>"
"<html>"
"<body>"
"<form action=\"/api/post/%s\" method=\"post\">"
"<input type=\"text\" name=\"content\"><br>"
"<input type=\"submit\" value=\"Chirp!\">"
"</form>",
user->name);
for (int i = arrlen(user->posts) - 1; i >= 0; i--) {
snprintf(html+strlen(html), HTML_LEN-strlen(html),
html_post(&user->posts[i]));
}
snprintf(html+strlen(html), HTML_LEN-strlen(html),
"</body>"
"</html>");
return html;
}
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
{
if (ev == MG_EV_HTTP_MSG)
{
struct mg_http_message *hm = (struct mg_http_message *)ev_data;
// POST
if (mg_strcmp(hm->method, mg_str("POST")) == 0)
{
// printf("POST: %.*s %.*s\n",
// hm->uri.len, hm->uri.ptr,
// hm->body.len, hm->body.ptr);
// printf("Message: %.*s\n",
// hm->message.len, hm->message.ptr);
struct mg_str caps[2];
if (mg_match(hm->uri, mg_str("/api/post/*"), caps)) {
User * user = UsersFind(caps[0]);
if (user != NULL) {
Post newPost;
newPost.timestamp = time(NULL);
newPost.user = user;
mg_http_get_var(&hm->body, "content", newPost.content, sizeof(newPost.content)-1);
arrput(user->posts, newPost);
UserWrite(user);
http_redirect_home(c, hm, user);
}
}
}
// GET
else if (mg_strcmp(hm->method, mg_str("GET")) == 0)
{
struct mg_str caps[2];
if (mg_match(hm->uri, mg_str("/home/*"), caps)) {
User * user = UsersFind(caps[0]);
if (user == NULL)
mg_http_reply(c, 200, "", html_user_not_found());
else
mg_http_reply(c, 200, "", html_home(user));
}
else if (mg_match(hm->uri, mg_str("/user/*"), caps)) {
User * user = UsersFind(caps[0]);
if (user == NULL)
mg_http_reply(c, 200, "", html_user_not_found());
else
mg_http_reply(c, 200, "", html_user(user));
}
else {
mg_http_reply(c, 404, "", html_404());
}
}
else {
mg_http_reply(c, 404, "", "Unknown");
}
}
}
int main(int argc, char *argv[])
{
UsersSetup();
struct mg_mgr mgr;
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, &mgr);
while (1)
mg_mgr_poll(&mgr, 1000);
mg_mgr_free(&mgr);
return 0;
}
|