-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_server.cpp
360 lines (311 loc) · 11.7 KB
/
my_server.cpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "network_helper.cpp"
#include "http_helper.cpp"
#include "api_helper.cpp"
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
SOCKET client_sockets[100];
const char response_404[] = "HTTP/1.1 404 NOT FOUND\nContent-length: 0\n";
const char response_400[] = "HTTP/1.1 400 BAD REQUEST\nContent-length: 0\n";
const char response_204[] = "HTTP/1.1 204 NO CONTENT\nContent-length: 0\n";
const char response_201[] = "HTTP/1.1 201 CONTENT CREATED\n\nContent-length: 0\n";
void delete_first_char(char* str) {
int i;
for (i = 0; str[i] != '\0'; i++) {
str[i] = str[i + 1];
}
}
int extractId(const char str[]) {
int id;
if (sscanf(str, "/api/%d", &id) == 1) {
return id;
} else {
return -1; // Failed to extract id
}
}
void stripRedundantSpaces(char* str) {
int len = strlen(str);
int new_pos = 0;
bool prev_space = true; // Initially set to true to handle leading spaces
for (int current = 0; current < len; current++) {
if (isspace((unsigned char)str[current])) {
if (str[current] == '\n') {
str[new_pos++] = str[current];
prev_space = true;
}
else if (!prev_space) {
str[new_pos++] = ' ';
prev_space = true;
}
} else {
str[new_pos++] = str[current];
prev_space = false;
}
}
str[new_pos] = '\0';
}
void thread_to_recv(struct thread_data td)
{
SOCKET s = td.s;
char req_recv[1000];
char response[3000];
strcpy(req_recv,"");
int req_len = receive_from_socket(s, req_recv, 1000);
if(req_len>0){
// break into tokens
//printf("received req: %s \n", req_recv);
char delim[] = " ";
char buf[3000];
int flag = 1; //flag = 0 means bad request HTTP 400
int tok_count = 0;
char *req_type = "";
char *file_path = "";
char *http_ver = "";
int is_api_req = 0;
char header[1000];
char body[5000];
get_http_header_body(req_recv, header, body);
// PARSING HEADER ----------------------------
char *token = header;
while (token != NULL) {
token = get_token(token, delim, buf);
tok_count += 1;
//processing token 1. should be GET
if(tok_count == 1){
req_type = strdup(buf);
}
else if(tok_count == 2){
file_path = strdup(buf);
char *result = strstr(file_path, "/api");
if (result != NULL) is_api_req = 1;
else is_api_req = 0;
}
else if(tok_count == 3){
http_ver = strdup(buf);
if(strcmp(http_ver, "HTTP/1.1") != 0){
flag = 0;
break;
}
}
else if(tok_count == 4){
flag = 0;
break;
}
else{
flag = 0;
}
}
//PROCESS REQUEST -----------------------
//printf("\nreqtype:%s, filepath:%s, ver:%s, is_api_req:%d \n, flag:%d\n", req_type, file_path, http_ver, is_api_req, flag);
if(flag == 0){
// BAD REQUEST
strcpy(response, response_400);
//printf("%s", response);
}
else{
// REQUEST FORMAT IS OK
//--------------------GET REQUEST-------------
if((strcmp(req_type, "GET")==0)){
//printf("HANDLING GET...");
if(is_api_req==0){
//PARSING GET /filepath
//printf("within GET /filepath");
int suc;
char file_cont[5000];
if(strcmp(file_path, "/") == 0){
char file_name[] = "index.html";
suc = read_html_file(file_name, file_cont);
}
else{
delete_first_char(file_path);
suc = read_html_file(file_path, file_cont);
}
if(suc == 0){
//file not found
strcpy(response,response_404);
}
else{
strcpy(response, "HTTP/1.1 200 OK\nContent-length: XX\n\n");
int content_len = strlen(file_cont);
sprintf(response, "HTTP/1.1 200 OK\nContent-length: %d\n\n",content_len);
strcat(response, file_cont);
}
}
else{
//HANDLE GET API
//2 possible formats: 1. filepath: /api/<id>
// 2. filepath: /api but body contains id
// 3. invalid format
//1. id on header: /api/<id>
char user_name[200];
char email[200];
int id = extractId(file_path);
//printf("id: %d\n", id);
// id found on header
if(id != -1){
int suc = get_record(id, user_name, email);
if(suc==0){
// id not found on database
strcpy(response, response_404);
}
else{
// id found on database
char res_body[1000];
sprintf(res_body, "id=%d&name=%s&email=%s\n", id, user_name, email);
int content_len = strlen(res_body);
// making response
sprintf(response, "HTTP/1.1 200 OK\nContent-length: %d\n\n%s\n",content_len, res_body);
}
}
// 2. id is on the body
else{
//look for id in request body
//printf("looking for id on request body");
if (sscanf(body, "id=%d", &id) == 1) {
//extracted id from body
int suc = get_record(id, user_name, email);
if(suc==0){
// id not found on database
strcpy(response, response_404);
}
else{
// id found on database
char res_body[1000];
sprintf(res_body, "id=%d&name=%s&email=%s\n", id, user_name, email);
int content_len = strlen(res_body);
// making response
sprintf(response, "HTTP/1.1 200 OK\nContent-length: %d\n\n%s\n",content_len, res_body);
}
}
else {
//3. invalid request format
strcpy(response, response_400);
}
}
}
}
//-----------PUT REQUEST---------
else if(strcmp(req_type, "PUT")==0){
//HANDLE PUT
//printf("HANDLING PUT REQUEST\n");
stripRedundantSpaces(req_recv);
int id; char user_name[100]; char email[100];
int result = sscanf(req_recv, "PUT /api HTTP/1.1\n\nid=%d&name=%[^&]&email=%s",&id, user_name, email);
if(result==3){
//req is in correct format
//update the db
int suc = update_record(id, user_name, email);
if(suc){
//user exists and record updated
strcpy(response, response_204);
}
else{
//user not found
strcpy(response, response_404);
}
}
else{
//bad request
strcpy(response, response_400);
}
}
//--------------DELETE REQUEST-------------------
else if(strcmp(req_type, "DELETE")==0){
//DELETE DELETE
//printf("HANDLING DELETE\n");
stripRedundantSpaces(req_recv);
int id;
int result = sscanf(req_recv, "DELETE /api HTTP/1.1\n\nid=%d",&id);
if(result==1){
//try to delete
int suc = delete_record(id);
if(suc==1){
//delete success
strcpy(response, response_204);
}else{
//file not found
strcpy(response, response_404);
}
}
else{
//bad request
strcpy(response, response_400);
}
}
//--------------POST REQUEST---------------------
else if(strcmp(req_type, "POST")==0){
//HANDLE POST
//printf("HANDLING POST");
stripRedundantSpaces(req_recv);
char user_name[100]; char email[100];
int result = sscanf(req_recv, "POST /api HTTP/1.1\n\nname=%[^&]&email=%s",user_name, email);
if(result==2){
//request format okay
int suc = create_record(user_name, email);
if(suc==1){
//successfully created
strcpy(response, response_201);
}else{
//something went wrong
//printf("error creating db entry.\n");
strcpy(response, response_400);
}
}
else{
//bad request
strcpy(response, response_400);
}
}else{
//printf("unrecognized request %s", req_type);
strcpy(response, response_400);
}
}
//SEND RESPONSE ----------------------
bool success = send_to_socket(s, response);
if(!success){
//printf("Send failed.\n\n");
return;
}
}
else{
//printf("empty request");
strcpy(response, response_400);
bool success = send_to_socket(s, response);
if(!success){
printf("Send failed.\n\n");
return;
}
}
}
int main(int argc , char *argv[])
{
bool success = init_networking();
if(!success) return 0;
SOCKET server_socket = create_socket();
if(!server_socket) return 0;
success = bind_socket(server_socket, 0, 8888);
if(!success) return 0;
listen_for_connections(server_socket, 1);
int client_id=100;
while(1)
{
//Accept and incoming connection
puts("Waiting for incoming connections...");
SOCKET s = accept_connection(server_socket);
client_id++;
client_sockets[client_id - 100] = s; //store the socket in global variable
puts("Connection accepted");
//create sending and receiving threads
struct thread_data td;
td.s = s;
td.client_id = client_id;
create_socket_thread(thread_to_recv, td);
}
//go to sleep
sleep_for_ever();
//finally process cleanup job
closesocket(server_socket);
//closesocket(s);
WSACleanup();
return 0;
}