-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
455 lines (369 loc) · 14.8 KB
/
client.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* Dinu Andreea Sabina- 322CB */
#include <stdio.h> /* printf, sprintf */
#include <stdlib.h> /* exit, atoi, malloc, free */
#include <unistd.h> /* read, write, close */
#include <string.h> /* memcpy, memset */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
#include <arpa/inet.h>
#include "helpers.h"
#include "requests.h"
#include "parson.h"
#define MAX_DATA_LENGTH 50
#define MAX_COOKIE_LENGTH 150
#define MAX_URL_LENGTH 150
#define MAX_ID_LENGTH 50
/* Allocate memory for the body data */
char** alloc_payload(int payload_fields_count, int data_size) {
char **payload = (char **)malloc(payload_fields_count * sizeof(char*));
if (!payload)
return NULL;
for (int i = 0; i < payload_fields_count; i++) {
payload[i] = (char *)malloc(data_size * sizeof(char));
if (!payload[i]) {
free(payload);
return NULL;
}
}
return payload;
}
/* Free the memory allocated for the body data of a request */
void free_payload(char ***payload, int payload_fields_count) {
for (int i = 0; i < payload_fields_count; i++) {
free((*payload)[i]);
}
free(*payload);
}
/* Receive login or register instructions from stdin
and make a post request with the data from the stdin
(username and password) */
char* access_account(char * command, int sockfd) {
/* Alloc memory for the body data */
int payload_fields_count = 2;
char **payload = alloc_payload(payload_fields_count, MAX_DATA_LENGTH);
if (!payload)
return NULL;
/* Read username from stdin */
printf("username=");
scanf("%s", payload[0]);
/* Read password from stdin */
printf("password=");
scanf("%s", payload[1]);
/* Sent a POST request with the coresponding URL */
char *message;
if (strncmp(command, "login", 5) == 0)
message = compute_post_request("34.241.4.235", "/api/v1/tema/auth/login",
"application/json", payload, 2, NULL, 0, NULL, 0);
else if (strncmp(command, "register", 8) == 0)
message = compute_post_request("34.241.4.235", "/api/v1/tema/auth/register",
"application/json", payload, 2, NULL, 0, NULL, 0);
/* Send the POST request to the server */
send_to_server(sockfd, message);
free(message);
/* Receive and print the POST reply from server */
char *response = receive_from_server(sockfd);
printf("%s\n", response);
/* Free the memory allocated for payload */
free_payload(&payload, payload_fields_count);
return response;
}
/* Receive a login instruction, make a POST request and get a reply,
from which exracts the cookie and return it */
char* login_command(char *cookie, char* command, int sockfd, int *already_logged) {
char* response = access_account(command, sockfd);
if (!strstr(response, "error")) {
char *token = strtok(response, "\r\n ");
while (token != NULL) {
if (strcmp(token, "Set-Cookie:") == 0) {
*already_logged = 1;
if (!cookie) {
cookie = (char *)malloc(MAX_COOKIE_LENGTH);
if (!cookie)
return NULL;
}
token = strtok(NULL, "\r\n ");
strcpy(cookie, token);
strcat(cookie, " ");
token = strtok(NULL, "\r\n ");
strcat(cookie, token);
strcat(cookie, " ");
token = strtok(NULL, "\r\n ");
strcat(cookie, token);
strcat(cookie, "\n");
break;
}
token = strtok(NULL, "\r\n ");
}
}
return cookie;
}
/* Extract a JWT token from a reply using parson library
for parsing JSONS */
char* extract_jwt_token(char *response) {
char* jwt_token = (char *)malloc(BUFLEN);
if (!jwt_token)
return NULL;
char *token = strtok(response, "\n");
/* Split the reply in tokens and the last one is
the JWT token */
while (token != NULL) {
strcpy(jwt_token, token);
token = strtok(NULL, "\n");
}
token = (char *)malloc(BUFLEN);
if (!token)
return NULL;
strcpy(token, "token");
/* Parse the JWT token in a JSON value, then into a
JSON object, then it is converted into a string and it
copies the JWT token into it */
JSON_Value *json_value = json_parse_string(jwt_token);
JSON_Object *json_object = json_value_get_object(json_value);
strcpy(jwt_token, json_object_get_string (json_object, token));
json_value_free(json_value);
free(token);
return jwt_token;
}
/* Compute a GET request and receive a cookie is the user was
already logged in, It returns the JWT token extracted from the response */
char* enter_library(char *cookie, int sockfd, int *already_logged) {
char *message;
/* If the user is not logged, compute a GET request without cookie */
if (*already_logged == 1) {
char **cookies = alloc_payload(1, BUFLEN);
if (!cookies)
return NULL;
strcpy(cookies[0], cookie);
message = compute_get_request("34.241.4.235", "/api/v1/tema/library/access",
NULL, cookies, 1, NULL, 0);
free_payload(&cookies,1);
} else
message = compute_get_request("34.241.4.235", "/api/v1/tema/library/access",
NULL, NULL, 0, NULL, 0);
/* Make a GET request and get a response and print it */
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
/* If the user is logged in, extract the JWT token */
char* jwt_token = NULL;
printf("%s\n", response);
if (*already_logged == 1)
jwt_token = extract_jwt_token(response);
free(response);
free(message);
return jwt_token;
}
/* Compute a GET request with the JWT token,
in order to show all the books from the system, when we receive
the command "get_books" */
void get_books(char* jwt_token, int sockfd) {
char *message;
/* If the token is not null, compute the request with the JWT token */
if (jwt_token != NULL) {
/* Copy the token in the first element of the array */
char **jwt_tokens = alloc_payload(1, strlen(jwt_token));
if (!jwt_tokens)
return;
strcpy(jwt_tokens[0], jwt_token);
message = compute_get_request("34.118.48.238", "/api/v1/tema/library/books",
NULL, NULL, 0, jwt_tokens, 1);
free_payload(&jwt_tokens, 1);
/* Else, compute the request without the JWT token */
} else
message = compute_get_request("34.118.48.238", "/api/v1/tema/library/books",
NULL, NULL, 0, NULL, 0);
/* Send the request to server and print the response */
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
printf("%s\n", response);
free(response);
free(message);
}
/* Receive a get_book command or a delete_books command and
make a GET request */
void book_operation(char* command, int sockfd, char* jwt_token) {
char *message;
printf("id=");
char *id = (char *)malloc(MAX_ID_LENGTH);
if (!id)
return;
scanf("%s", id);
char *url = (char *)malloc(MAX_URL_LENGTH);
if (!url)
return;
strcpy(url, "/api/v1/tema/library/books/");
strcat(url, id);
/* If the JWT is not empty, make the request using it */
if (jwt_token != NULL) {
/* Copy the token in the first element of the array */
char **jwt_tokens = alloc_payload(1, strlen(jwt_token));
if (!jwt_tokens)
return;
strcpy(jwt_tokens[0], jwt_token);
if (strncmp(command, "delete_book", 11 ) == 0)
message = compute_delete_request("34.241.4.235", url,
NULL, NULL, 0, jwt_tokens, 1);
else if (strncmp(command, "get_book", 8) == 0)
message = compute_get_request("34.241.4.235", url,
NULL, NULL, 0, jwt_tokens, 1);
free_payload(&jwt_tokens, 1);
} else {
if (strncmp(command, "delete_book", 11 ) == 0)
message = compute_delete_request("34.241.4.235", url,
NULL, NULL, 0, NULL, 0);
else if (strncmp(command, "get_book", 8) == 0)
message = compute_get_request("34.241.4.235", url,
NULL, NULL, 0, NULL, 0);
}
/* Sends the request to the server, receives the response and print it */
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
printf("%s\n", response);
free(message);
free(response);
free(id);
free(url);
}
/* Read the information about the book from STDIN */
void read_new_book(char **payload) {
printf("title=");
scanf("%s", payload[0]);
printf("author=");
scanf("%s", payload[1]);
printf("genre=");
scanf("%s", payload[2]);
printf("page_count=");
scanf("%s", payload[3]);
printf("publisher=");
scanf("%s", payload[4]);
}
/* Add a new book in the library by making a POST request */
void add_book(char* jwt_token, int sockfd) {
char *message;
/* Compute a POST request without a JWT token */
if (!jwt_token) {
message = compute_post_request("34.241.4.235", "/api/v1/tema/library/books",
"application/json", NULL, 0, NULL, 0,
NULL, 0);
} else {
int payload_fields_count = 5;
char **payload = alloc_payload(payload_fields_count, MAX_DATA_LENGTH);
if (!payload)
return;
/* Read the information about the book from STDIN */
read_new_book(payload);
char **jwt_tokens = alloc_payload(1, strlen(jwt_token));
if (!jwt_tokens)
return;
strcpy(jwt_tokens[0], jwt_token);
message = compute_post_request("34.241.4.235", "/api/v1/tema/library/books",
"application/json", payload, 5, NULL, 0,
jwt_tokens, 1);
free_payload(&payload, payload_fields_count);
free_payload(&jwt_tokens, 1);
}
/* Send the POST request, receive the response and print it */
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
printf("%s\n", response);
free(message);
free(response);
}
/* Make the logout for a user by making a GET request */
void logout_command(int sockfd, char* cookie, int *already_logged) {
char *message;
if (*already_logged == 1) {
/* Copy the cookie in the first element of the array */
char **cookies = alloc_payload(1, MAX_COOKIE_LENGTH);
if (!cookies)
return;
strcpy(cookies[0], cookie);
message = compute_get_request("34.241.4.235", "/api/v1/tema/auth/logout",
NULL, cookies, 1, NULL, 0);
free_payload(&cookies, 1);
/* Compute the request without cookie if the user is not logged */
} else
message = compute_get_request("34.241.4.235", "/api/v1/tema/auth/logout",
NULL, NULL, 0, NULL, 0);
/* Send the GET request to the server, receive a response and print it */
send_to_server(sockfd, message);
char *response = receive_from_server(sockfd);
printf("%s\n", response);
/* The user isn't logged anymore */
if (*already_logged == 1)
*already_logged = 0;
free(response);
free(message);
}
/* Free the memory allocated for a string if it not empty */
void free_not_empty(char **string) {
if (*string != NULL) {
free(*string);
*string = NULL;
}
}
int main(int argc, char *argv[]) {
int already_logged = 0;
char *response = NULL, *cookie = NULL, *jwt_token = NULL;
printf("Possible instructions: register, login, logout, enter_library, get_book, get_books, add_book, exit, delete_book\n");
while (1) {
/* Read instuctions from STDIN */
char buf[BUFLEN];
scanf("%s", buf);
if (strncmp(buf, "exit", 4) == 0)
return 0;
/* Create a new socket */
int sockfd = open_connection("34.241.4.235", 8080, AF_INET, SOCK_STREAM, 0);
if (strncmp(buf, "register", 8) == 0) {
response = access_account(buf, sockfd);
free_not_empty(&response);
} else if (strncmp(buf, "login", 5) == 0) {
if (already_logged == 1) {
printf("You are already logged into your account\n");
continue;
}
cookie = login_command(cookie, buf, sockfd, &already_logged);
} else if (strncmp(buf, "enter_library", 13) == 0) {
if (already_logged == 0) {
printf("You need to login first\n");
continue;
}
jwt_token = enter_library(cookie, sockfd, &already_logged);
} else if (strncmp(buf, "get_books", 9) == 0) {
if (already_logged == 0) {
printf("You need to login first\n");
continue;
}
get_books(jwt_token, sockfd);
} else if (strncmp(buf, "get_book", 8) == 0) {
if (already_logged == 0) {
printf("You need to login first\n");
continue;
}
book_operation(buf, sockfd, jwt_token);
} else if (strncmp(buf, "add_book", 8) == 0) {
if (already_logged == 0) {
printf("You need to login first\n");
continue;
}
add_book(jwt_token, sockfd);
} else if (strncmp(buf, "delete_book", 11) == 0) {
if (already_logged == 0) {
printf("You need to login first\n");
continue;
}
book_operation(buf, sockfd, jwt_token);
} else if (strncmp(buf, "logout", 6) == 0) {
logout_command(sockfd, cookie, &already_logged);
free_not_empty(&cookie);
free_not_empty(&jwt_token);
} else
printf("Invalid command\n");
/* Close the socket */
close_connection(sockfd);
}
free_not_empty(&cookie);
free_not_empty(&jwt_token);
free_not_empty(&response);
return 0;
}