-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-server.c
339 lines (275 loc) · 9.19 KB
/
number-server.c
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "http-server.h"
#include <string.h>
#include <time.h>
#include <ctype.h>
#define MAX_CHATS 100000
#define MAX_REACTIONS 100
#define USERNAME_SIZE 16
#define MESSAGE_SIZE 256
#define REACTION_SIZE 16
#define TIMESTAMP_SIZE 20
//max reaction
#define MAX_BUFFER 99999
struct Reaction {
char user[USERNAME_SIZE];
char message[REACTION_SIZE];
};
typedef struct Reaction Reaction;
struct Chat {
uint32_t id;
char user[USERNAME_SIZE];
char message[MESSAGE_SIZE];
char timestamp[TIMESTAMP_SIZE];
uint32_t num_reactions;
Reaction reactions[MAX_REACTIONS];
};
typedef struct Chat Chat;
Chat chats[MAX_CHATS];
uint32_t chat_count = 0;
char const HTTP_404_NOT_FOUND[] = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n";
char const HTTP_200_OK[] = "HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n";
void handle_404(int client_sock, char *path) {
printf("SERVER LOG: Got request for unrecognized path \"%s\"\n", path);
char response_buff[BUFFER_SIZE];
snprintf(response_buff, sizeof(response_buff),
"Error 404:\r\nUnrecognized path \"%s\"\r\n", path);
write(client_sock, HTTP_404_NOT_FOUND, strlen(HTTP_404_NOT_FOUND));
write(client_sock, response_buff, strlen(response_buff));
}
char* url_decode(char* str) {
int len = strlen(str);
char* buf = malloc(len + 1);
int j = 0;
for (int i = 0; i < len; i++) {
if (str[i] == '%') {
int hex = 0;
if (isdigit(str[i + 1])) {
hex = (str[i + 1] - '0') * 16;
} else {
hex = (tolower(str[i + 1]) - 'a' + 10) * 16;
}
if (isdigit(str[i + 2])) {
hex += str[i + 2] - '0';
} else {
hex += tolower(str[i + 2]) - 'a' + 10;
}
buf[j++] = (char)hex;
i += 2;
} else if (str[i] == '+') {
buf[j++] = ' ';
} else {
buf[j++] = str[i];
}
}
buf[j] = '\0';
return buf;
}
uint8_t add_chat(char* username, char* message) {
if (chat_count >= MAX_CHATS) {
return 0;
}
Chat* new_chat = &chats[chat_count];
new_chat->id = chat_count + 1;
strncpy(new_chat->user, username, USERNAME_SIZE - 1);
new_chat->user[USERNAME_SIZE - 1] = '\0';
strncpy(new_chat->message, message, MESSAGE_SIZE - 1);
new_chat->message[MESSAGE_SIZE - 1] = '\0';
time_t now = time(NULL);
struct tm *tm_info = localtime(&now);
strftime(new_chat->timestamp, TIMESTAMP_SIZE, "%Y-%m-%d %H:%M:%S", tm_info);
new_chat->num_reactions = 0;
chat_count++;
return 1;
}
uint8_t add_reaction(char* username, char* message, char* id) {
uint32_t chat_id = atoi(id);
if (chat_id == 0 || chat_id > chat_count) {
return 0;
}
Chat* chat = &chats[chat_id - 1];
if (chat->num_reactions >= MAX_REACTIONS) {
return 0;
}
Reaction* new_reaction = &chat->reactions[chat->num_reactions];
strncpy(new_reaction->user, username, USERNAME_SIZE - 1);
new_reaction->user[USERNAME_SIZE - 1] = '\0';
strncpy(new_reaction->message, message, REACTION_SIZE - 1);
new_reaction->message[REACTION_SIZE - 1] = '\0';
chat->num_reactions++;
return 1;
}
void reset() {
memset(chats, 0, sizeof(chats));
chat_count = 0;
}
void respond_with_chats(int client) {
char response[MAX_BUFFER];
int offset = 0;
offset += snprintf(response + offset, BUFFER_SIZE - offset, "%s", HTTP_200_OK);
for (uint32_t i = 0; i < chat_count; i++) {
Chat* chat = &chats[i];
offset += snprintf(response + offset,sizeof(response),
"[#%d %s] %9s: %s\n",
chat->id, chat->timestamp, chat->user, chat->message);
if (chat->num_reactions > 0) {
for (uint32_t j = 0; j < chat->num_reactions; j++) {
Reaction* reaction = &chat->reactions[j];
offset += snprintf(response + offset,sizeof(response),
" (%s) %s\n",
reaction->user, reaction->message);
}
}
}
write(client, response, strlen(response));
}
//new functions
uint8_t edit_chat(uint32_t chat_id, char* new_message){
if(chat_id==0 || chat_id>chat_count){
//check if chat_id is valid
return 0;
}
//the chat to be editted
Chat* chat=&chats[chat_id-1];
//new message copied
strncpy(chat->message, new_message, MESSAGE_SIZE-1);
chat->message[MESSAGE_SIZE-1]='\0';
return 1;
}
void handle_edit(char* path, int client_sock){
char* id=strstr(path,"id=");
char* message=strstr(path, "message=");
if(!id||!message){
write(client_sock, "HTTP/1.1 400 Bad Request\r\n\r\n",28);
return;
}
id+=3;
message+=8;
char* id_end=strchr(id, '&');
char* message_end=strchr(message,' ');
if(id_end) *id_end='\0';
if(message_end) *message_end='\0';
char* decoded_id=url_decode(id);
char* decoded_message=url_decode(message);
if(strlen(decoded_message)>255){
write(client_sock, "HTTP/1.1 400 Bad Request\r\n\r\n",28);
free(decoded_id);
free(decoded_message);
return;
}
uint32_t chat_id=atoi(decoded_id);
if(edit_chat(chat_id,decoded_message)){
respond_with_chats(client_sock);
}else{
write(client_sock, "HTTP/1.1 500 Internal Server Error\r\n\r\n",37);
}
free(decoded_id);
free(decoded_message);
}
void handle_post(char* path, int client_sock) {
char* user = strstr(path, "user=");
char* message = strstr(path, "message=");
if (!user || !message) {
write(client_sock, "HTTP/1.1 400 Bad Request\r\n\r\n", 28);
return;
}
user += 5;
message += 8;
char* user_end = strchr(user, '&');
char* message_end = strchr(message, ' ');
if (user_end) *user_end = '\0';
if (message_end) *message_end = '\0';
char* decoded_user = url_decode(user);
char* decoded_message = url_decode(message);
if (strlen(decoded_user) > 15 || strlen(decoded_message) > 255) {
write(client_sock, "HTTP/1.1 400 Bad Request\r\n\r\n", 28);
free(decoded_user);
free(decoded_message);
return;
}
if (chat_count >= MAX_CHATS) {
write(client_sock, "HTTP/1.1 500 Internal Server Error\r\n\r\n", 37);
free(decoded_user);
free(decoded_message);
return;
}
if (add_chat(decoded_user, decoded_message)) {
respond_with_chats(client_sock);
} else {
write(client_sock, "HTTP/1.1 500 Internal Server Error\r\n\r\n", 37);
}
free(decoded_user);
free(decoded_message);
}
void handle_reaction(char* path, int client_sock) {
char* user = strstr(path, "user=");
char* message = strstr(path, "message=");
char* id = strstr(path, "id=");
if (!user || !message || !id) {
write(client_sock, "HTTP/1.1 400 Bad Request\r\n\r\n", 28);
return;
}
user += 5;
message += 8;
id += 3;
char* user_end = strchr(user, '&');
char* message_end = strchr(message, '&');
char* id_end = strchr(id, ' ');
if (user_end) *user_end = '\0';
if (message_end) *message_end = '\0';
if (id_end) *id_end = '\0';
char* decoded_user = url_decode(user);
char* decoded_message = url_decode(message);
if (strlen(decoded_user) > 15 || strlen(decoded_message) > REACTION_SIZE - 1) {
write(client_sock, "HTTP/1.1 400 Bad Request\r\n\r\n", 28);
free(decoded_user);
free(decoded_message);
return;
}
if (add_reaction(decoded_user, decoded_message, id)) {
respond_with_chats(client_sock);
} else {
write(client_sock, "HTTP/1.1 500 Internal Server Error\r\n\r\n", 37);
}
free(decoded_user);
free(decoded_message);
}
void handle_response(char *request, int client_sock) {
char method[8];
char *path = strchr(request, ' ');
if (path == NULL) {
handle_404(client_sock, "Invalid request");
return;
}
*path = '\0';
strcpy(method, request);
path++;
char *end = strchr(path, ' ');
if (end == NULL) {
handle_404(client_sock, "Invalid request");
return;
}
*end = '\0';
printf("\nSERVER LOG: Got %s request for path: \"%s\"\n", method, path);
if (strncmp(path, "/post", 5) == 0) {
handle_post(path, client_sock);
} else if (strncmp(path, "/react", 6) == 0) {
handle_reaction(path, client_sock);
} else if (strcmp(path, "/chats") == 0) {
respond_with_chats(client_sock);
} else if (strcmp(path, "/reset") == 0) {
reset();
write(client_sock, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n", 44);
write(client_sock, "Server data has been reset.\r\n", 29);
} else if(strncmp(path, "/edit",5)==0){
handle_edit(path,client_sock);
}else {
handle_404(client_sock, path);
}
}
int main(int argc, char *argv[]) {
int port = 0;
if(argc >= 2) {
port = atoi(argv[1]);
}
start_server(&handle_response, port);
}