-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
639 lines (448 loc) · 23.9 KB
/
Copy pathclient.c
File metadata and controls
639 lines (448 loc) · 23.9 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "helpers.h"
#include "requests.h"
#include "parson.h"
#include "parson.c"
int main(int argc, char *argv[]) {
// variabilele necesare pentru comunicarea cu serverul
char *message; // ce trimiem
char *response; // ce primim
int sockfd; // socket-ul de comunicatie
char *ip = "34.254.242.81"; // ip-ul serverului
char *cookie = malloc(100 * sizeof(char)); // cookie-ul de la login
char *token = malloc(200 * sizeof(char)); // token-ul de la enter_library
// flags pentru verificare login si acces la biblioteca
int flag_login = 0;
int flag_enter_library = 0;
while (1) {
char *input = (char *) malloc(100 * sizeof(char));
// citire comanda client
fgets(input, 100, stdin);
if (strncmp(input, "exit", 4) == 0) {
break;
}
if (strncmp(input, "register", 8) == 0) { // verificare comanda register
char *username = malloc(51 * sizeof(char));
char *password = malloc(51 * sizeof(char));
printf("username=");
fgets(username, 50, stdin);
username[strcspn(username, "\n")] = 0;
printf("password=");
fgets(password, 50, stdin);
password[strcspn(password, "\n")] = 0;
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
json_object_set_string(root_object, "username", username);
json_object_set_string(root_object, "password", password);
char *json_string = json_serialize_to_string_pretty(root_value);
// creez mesajul de tip POST
message = compute_post_request("34.254.242.81:8080", "/api/v1/tema/auth/register",
"application/json", json_string, 1, NULL, NULL, 0);
// deschid conexiunea cu serverul
sockfd = open_connection(ip, 8080, AF_INET, SOCK_STREAM, 0);
// trimit mesajul creeat catre server
send_to_server(sockfd, message);
// primesc raspunsul de la server
response = receive_from_server(sockfd);
// parsam mesajul pentru a verifica daca exista eroare
const char *jsonStart = strstr(response, "{");
JSON_Value *root_value_status = json_parse_string(jsonStart);
JSON_Object *root_object_status = json_value_get_object(root_value_status);
const char *errorMessage = json_object_get_string(root_object_status, "error");
if (errorMessage != NULL) {
// in cazul unei errori afisez mesajul de eroare
printf("Error: %s\n", errorMessage);
} else {
// altfel afisez mesajul de succes
const char *statusCodeStart = strchr(response, ' ') + 1;
int statusCode = atoi(statusCodeStart);
if (statusCode >= 200 && statusCode < 300) {
printf("User registered successfully!\n");
} else {
printf("Error: Unable to retrieve error message.\n");
}
}
json_value_free(root_value_status);
} else if (strncmp(input, "login", 5) == 0) { // verificare comanda login
// urmeaza pasii similari de la register
char *username = malloc(51 * sizeof(char));
char *password = malloc(51 * sizeof(char));
printf("username=");
fgets(username, 50, stdin);
username[strcspn(username, "\n")] = 0;
printf("password=");
fgets(password, 50, stdin);
password[strcspn(password, "\n")] = 0;
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
json_object_set_string(root_object, "username", username);
json_object_set_string(root_object, "password", password);
char *json_string = json_serialize_to_string_pretty(root_value);
message = compute_post_request("34.254.242.81:8080", "/api/v1/tema/auth/login",
"application/json", json_string, 1, NULL, NULL, 0);
sockfd = open_connection(ip, 8080, AF_INET, SOCK_STREAM, 0);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
const char *jsonStart = strstr(response, "{");
JSON_Value *root_value_status = json_parse_string(jsonStart);
JSON_Object *root_object_status = json_value_get_object(root_value_status);
const char *errorMessage = json_object_get_string(root_object_status, "error");
if (errorMessage != NULL) {
printf("Error: %s\n", errorMessage);
} else {
const char *statusCodeStart = strchr(response, ' ') + 1;
int statusCode = atoi(statusCodeStart);
if (statusCode >= 200 && statusCode < 300) {
// in cazul in care login-ul a fost cu succes, retin cookie-ul
char *ptr = strstr(response, "connect.sid=");
ptr += 12;
int i = 0;
while (ptr[i] != ';') {
cookie[i] = ptr[i];
i++;
}
cookie[i] = '\0';
if (cookie != NULL) {
flag_login = 1;
printf("Logged in successfully!\n");
}
} else {
printf("Error: Unable to retrieve error message.\n");
}
}
json_value_free(root_value_status);
} else if (strncmp(input, "enter_library", 13) == 0) { // verificare comanda enter_library
if (flag_login == 0) { // verific daca user-ul este deja logat
printf("Error: You need to log in first!\n");
continue;
} else {
// creez mesajul de tip GET si urmeaza pasii similari de la register/login
message = compute_get_request("34.254.242.81:8080", "/api/v1/tema/library/access", NULL, cookie, NULL, 1);
sockfd = open_connection(ip, 8080, AF_INET, SOCK_STREAM, 0);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
const char *jsonStart = strstr(response, "{");
JSON_Value *root_value_status = json_parse_string(jsonStart);
JSON_Object *root_object_status = json_value_get_object(root_value_status);
const char *errorMessage = json_object_get_string(root_object_status, "error");
if (errorMessage != NULL) {
printf("Error: %s\n", errorMessage);
} else {
const char *statusCodeStart = strchr(response, ' ') + 1;
int statusCode = atoi(statusCodeStart);
if (statusCode >= 200 && statusCode < 300) {
// in cazul in care am primit raspunsul cu succes, retin token-ul
char *token_ptr = strstr(response, "token");
if (token_ptr == NULL) {
printf("Error: You dont have access!\n");
} else {
token_ptr += 8;
int i = 0;
while (token_ptr[i] != '"') {
token[i] = token_ptr[i];
i++;
}
token[i] = '\0';
printf("You have access to the library!\n");
flag_enter_library = 1;
}
} else {
printf("Error: Unable to retrieve error message.\n");
}
}
json_value_free(root_value_status);
}
} else if (strncmp(input, "get_books", 9) == 0) { // verificare comanda get_books
if (flag_enter_library == 0) { // verific daca user-ul a intrat in biblioteca
printf("Error: You dont have access to the library!\n");
printf("Please enter the library first!\n");
continue;
} else {
// creez mesajul de tip GET si urmeaza pasii similari de la register/login
message = compute_get_request("34.254.242.81:8080", "/api/v1/tema/library/books", NULL, NULL, token, 1);
sockfd = open_connection(ip, 8080, AF_INET, SOCK_STREAM, 0);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
const char *jsonStart = strstr(response, "{");
JSON_Value *root_value_status = json_parse_string(jsonStart);
JSON_Object *root_object_status = json_value_get_object(root_value_status);
const char *errorMessage = json_object_get_string(root_object_status, "error");
if (errorMessage != NULL) {
printf("Error: %s\n", errorMessage);
} else {
const char *statusCodeStart = strchr(response, ' ') + 1;
int statusCode = atoi(statusCodeStart);
if (statusCode >= 200 && statusCode < 300) {
// in cazul in care am primit raspunsul cu succes
// parsez raspunsul si afisez cartile
char *first = strstr(response, "[");
char *last = strstr(response, "]");
int len = last - first + 1;
char *to_print = malloc(len * sizeof(char));
strncpy(to_print, first, len);
JSON_Value *root_value = json_parse_string(to_print);
JSON_Array *books_array = json_value_get_array(root_value);
int num_books = json_array_get_count(books_array);
if (num_books == 0) { // verificare librarie goala
printf("Library empty!\n");
printf("Add some books!\n");
} else {
for (int i = 0; i < num_books; i++) { // iteram prin array-ul de carti
JSON_Object *book_obj = json_array_get_object(books_array, i);
// extragem id-ul si titlul cartii si le afisam
int id = (int)json_object_get_number(book_obj, "id");
const char *title = json_object_get_string(book_obj, "title");
printf("id=%d title=%s\n", id, title);
}
}
} else {
printf("Error: Unable to retrieve error message.\n");
}
}
json_value_free(root_value_status);
}
} else if (strncmp(input, "add_book", 8) == 0) { // verificare comanda add_book
if (flag_enter_library == 0) { // verific daca user-ul a intrat in biblioteca
printf("Error: You dont have access to the library!\n");
printf("Please enter the library first!\n");
continue;
} else {
// primim de la tastatura detaliile cartii
// verificam daca sunt in formatul corect
char *title = malloc(51 * sizeof(char));
char *author = malloc(51 * sizeof(char));
char *genre = malloc(51 * sizeof(char));
char *publisher = malloc(51 * sizeof(char));
int page_count;
printf("title=");
fgets(title, 50, stdin);
if(strcmp(title, "") == 0) {
printf("Error: Title wrong format!\n");
printf("title=");
fgets(title, 50, stdin);
continue;
}
printf("author=");
fgets(author, 50, stdin);
if(strcmp(author, "") == 0) {
printf("Error: Author wrong format!\n");
printf("author=");
fgets(author, 50, stdin);
continue;
}
printf("genre=");
fgets(genre, 50, stdin);
if(strcmp(genre, "") == 0) {
printf("Error: Genre wrong format!\n");
printf("genre=");
fgets(genre, 50, stdin);
continue;
}
printf("publisher=");
fgets(publisher, 50, stdin);
if(strcmp(publisher, "") == 0) {
printf("Error: Publisher wrong format!\n");
printf("publisher=");
fgets(publisher, 50, stdin);
continue;
}
printf("page_count=");
scanf("%d", &page_count);
if(page_count < 1) {
printf("Error: Page count wrong format!\n");
printf("page_count=");
scanf("%d", &page_count);
continue;
}
char *pages_string = malloc(51 * sizeof(char));
sprintf(pages_string, "%d", page_count);
// scapam de \n de la sfarsitul fiecarei linii
title[strcspn(title, "\n")] = 0;
author[strcspn(author, "\n")] = 0;
genre[strcspn(genre, "\n")] = 0;
publisher[strcspn(publisher, "\n")] = 0;
// construim json-ul pe care il vom trimite la server
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
json_object_set_string(root_object, "title", title);
json_object_set_string(root_object, "author", author);
json_object_set_string(root_object, "genre", genre);
json_object_set_string(root_object, "publisher", publisher);
json_object_set_number(root_object, "page_count", page_count);
char *json_string = json_serialize_to_string(root_value);
// urmam pasii similari cu cei de la enter_library
message = compute_post_request("34.254.242.81:8080", "/api/v1/tema/library/books",
"application/json", json_string, 1, NULL, token, 1);
sockfd = open_connection(ip, 8080, AF_INET, SOCK_STREAM, 0);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
const char *jsonStart = strstr(response, "{");
JSON_Value *root_value_status = json_parse_string(jsonStart);
JSON_Object *root_object_status = json_value_get_object(root_value_status);
const char *errorMessage = json_object_get_string(root_object_status, "error");
if (errorMessage != NULL) { // verificam daca am primit eroare
printf("Error: %s\n", errorMessage);
} else {
const char *statusCodeStart = strchr(response, ' ') + 1;
int statusCode = atoi(statusCodeStart);
if (statusCode >= 200 && statusCode < 300) {
// in caz de succes afisam mesajul corespunzator
printf("Book added!\n");
} else {
printf("Error: Unable to retrieve error message.\n");
}
}
json_value_free(root_value_status);
}
} else if(strncmp(input, "get_book", 8) == 0) { // verificare comanda get_book
if (flag_enter_library == 0) { // verific daca user-ul a intrat in biblioteca
printf("Error: You dont have access to the library!\n");
printf("Please enter the library first!\n");
continue;
} else {
// citim id-ul cartii
char *id_string = malloc(51 * sizeof(char));
int id;
printf("id=");
scanf("%d", &id);
if(id < 1) {
printf("Error: Id wrong format!\n");
printf("id=");
scanf("%d", &id);
continue;
}
// construim ruta pe care o vom accesa
// si trimitem request-ul
char *route = malloc(100 * sizeof(char));
memcpy(route, "/api/v1/tema/library/books/", 28);
sprintf(id_string, "%d", id);
strcat(route, id_string);
message = compute_get_request("34.254.242.81:8080", route, NULL, NULL, token, 1);
sockfd = open_connection(ip, 8080, AF_INET, SOCK_STREAM, 0);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
const char *jsonStart = strstr(response, "{");
JSON_Value *root_value_status = json_parse_string(jsonStart);
JSON_Object *root_object_status = json_value_get_object(root_value_status);
const char *errorMessage = json_object_get_string(root_object_status, "error");
if (errorMessage != NULL) {
printf("Error: %s\n", errorMessage);
} else {
const char *statusCodeStart = strchr(response, ' ') + 1;
int statusCode = atoi(statusCodeStart);
if (statusCode >= 200 && statusCode < 300) {
// in caz de succes parsam json-ul primit
char *first = strstr(response, "{");
char *last = strstr(response, "}");
int len = last - first + 1;
if (len < 0) { // verificam daca exista cartea
printf("Error: Book not found!\n");
continue;
} else {
// parsam detaliile despre cartea primita si le afisam
char *to_print = malloc(len * sizeof(char));
strncpy(to_print, first, len);
JSON_Value *root_value = json_parse_string(to_print);
const char *title = json_object_dotget_string(json_value_get_object(root_value), "title");
const char *author = json_object_dotget_string(json_value_get_object(root_value), "author");
const char *publisher = json_object_dotget_string(json_value_get_object(root_value), "publisher");
const char *genre = json_object_dotget_string(json_value_get_object(root_value), "genre");
int page_count = json_object_dotget_number(json_value_get_object(root_value), "page_count");
printf("title=%s\n", title);
printf("author=%s\n", author);
printf("publisher=%s\n", publisher);
printf("genre=%s\n", genre);
printf("page_count=%d\n", page_count);
}
} else {
printf("Error: Unable to retrieve error message.\n");
}
}
json_value_free(root_value_status);
}
} else if(strncmp(input, "delete_book", 10) == 0) { // verificare comanda delete_book
if (flag_enter_library == 0) { // verific daca user-ul a intrat in biblioteca
printf("Error: You dont have access to the library!\n");
printf("Please enter the library first!\n");
continue;
} else {
// citim id-ul cartii
char *id_string = malloc(51 * sizeof(char));
int id;
printf("id=");
scanf("%d", &id);
if(id < 1) {
printf("Error: Id wrong format!\n");
printf("id=");
scanf("%d", &id);
continue;
}
// urmam pasii de la comanda get_book
char *route = malloc(100 * sizeof(char));
memcpy(route, "/api/v1/tema/library/books/", 28);
sprintf(id_string, "%d", id);
strcat(route, id_string);
message = compute_delete_request("34.254.242.81:8080", route, NULL, token);
sockfd = open_connection(ip, 8080, AF_INET, SOCK_STREAM, 0);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
const char *jsonStart = strstr(response, "{");
JSON_Value *root_value_status = json_parse_string(jsonStart);
JSON_Object *root_object_status = json_value_get_object(root_value_status);
const char *errorMessage = json_object_get_string(root_object_status, "error");
if (errorMessage != NULL) {
printf("Error: %s\n", errorMessage);
} else {
const char *statusCodeStart = strchr(response, ' ') + 1;
int statusCode = atoi(statusCodeStart);
if (statusCode >= 200 && statusCode < 300) {
// in caz de succes afisam mesajul corespunzator
printf("Book deleted!\n");
} else {
printf("Error: Unable to retrieve error message.\n");
}
}
json_value_free(root_value_status);
}
} else if (strncmp(input, "logout", 6) == 0) { // verificare comanda logout
if (flag_login == 0) { // verific daca user-ul este logat
printf("Error: You are not logged in!\n");
continue;
} else {
// urmam pasii de la comanda register/login
message = compute_get_request("34.254.242.81:8080", "/api/v1/tema/auth/logout",
NULL, cookie, NULL, 1);
sockfd = open_connection(ip, 8080, AF_INET, SOCK_STREAM, 0);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
const char *jsonStart = strstr(response, "{");
JSON_Value *root_value_status = json_parse_string(jsonStart);
JSON_Object *root_object_status = json_value_get_object(root_value_status);
const char *errorMessage = json_object_get_string(root_object_status, "error");
if (errorMessage != NULL) {
printf("Error: %s\n", errorMessage);
} else {
const char *statusCodeStart = strchr(response, ' ') + 1;
int statusCode = atoi(statusCodeStart);
if (statusCode >= 200 && statusCode < 300) {
// in caz de succes afisam mesajul corespunzator si setam flag-urile pe 0
printf("Logout successful!\n");
flag_login = 0;
flag_enter_library = 0;
} else {
printf("Error: Unable to retrieve error message.\n");
}
}
json_value_free(root_value_status);
}
}
}
return 0;
}