-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_lib.cpp
More file actions
915 lines (712 loc) · 25.3 KB
/
Copy pathclient_lib.cpp
File metadata and controls
915 lines (712 loc) · 25.3 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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
#include "client_lib.hpp"
#include "requests.hpp"
char *get_input() {
char *buf = (char *)malloc(BUFSIZ);
// Get input.
char *rc = fgets(buf, BUFSIZ, stdin);
DIE(rc == NULL, "fgets error");
// Strip of newline.
buf[strcspn(buf, "\r\n")] = '\0';
return buf;
}
int get_type(char *command) {
if (command == NULL) return -1;
else if (strcmp(command, "login_admin") == 0) return CMD_LOGIN_ADMIN;
else if (strcmp(command, "add_user") == 0) return CMD_ADD_USER;
else if (strcmp(command, "get_users") == 0) return CMD_GET_USERS;
else if (strcmp(command, "delete_user") == 0) return CMD_DELETE_USER;
else if (strcmp(command, "logout_admin") == 0) return CMD_LOGOUT_ADMIN;
else if (strcmp(command, "login") == 0) return CMD_LOGIN;
else if (strcmp(command, "get_access") == 0) return CMD_GET_ACCESS;
else if (strcmp(command, "get_movies") == 0) return CMD_GET_MOVIES;
else if (strcmp(command, "get_movie") == 0) return CMD_GET_MOVIE;
else if (strcmp(command, "add_movie") == 0) return CMD_ADD_MOVIE;
else if (strcmp(command, "delete_movie") == 0) return CMD_DELETE_MOVIE;
else if (strcmp(command, "update_movie") == 0) return CMD_UPDATE_MOVIE;
else if (strcmp(command, "get_collections") == 0) return CMD_GET_COLLECTIONS;
else if (strcmp(command, "get_collection") == 0) return CMD_GET_COLLECTION;
else if (strcmp(command, "add_collection") == 0) return CMD_ADD_COLLECTION;
else if (strcmp(command, "delete_collection") == 0) return CMD_DELETE_COLLECTION;
else if (strcmp(command, "add_movie_to_collection") == 0) return CMD_ADD_MOVIE_TO_COLLECTION;
else if (strcmp(command, "delete_movie_from_collection") == 0)return CMD_DELETE_MOVIE_FROM_COLLECTION;
else if (strcmp(command, "logout") == 0) return CMD_LOGOUT;
else if (strcmp(command, "exit") == 0) return CMD_EXIT;
else return -1;
}
char *get_req(int type, unordered_map<string, string> cookies, char *jwt) {
// Check if type is valid.
if (type < 0 || type > 19) {
fprintf(stderr, "Command to execute doesn't exist.\n");
return NULL;
}
// Start
switch (type) {
case CMD_LOGIN_ADMIN: {
return handle_login_admin(cookies);
}
case CMD_ADD_USER: {
return handle_add_user(cookies);
}
case CMD_GET_USERS: {
return handle_get_users(cookies);
}
case CMD_DELETE_USER: {
return handle_delete_user(cookies);
}
case CMD_LOGOUT_ADMIN: {
return handle_logout_admin(cookies, jwt);
}
case CMD_LOGIN: {
return handle_login(cookies);
}
case CMD_GET_ACCESS: {
return handle_get_access(cookies);
}
case CMD_GET_MOVIES: {
return handle_get_movies(cookies, jwt);
}
case CMD_GET_MOVIE: {
return handle_get_movie(cookies, jwt);
}
case CMD_ADD_MOVIE: {
return handle_add_movie(cookies, jwt);
}
case CMD_DELETE_MOVIE: {
return handle_delete_movie(cookies, jwt);
}
case CMD_UPDATE_MOVIE: {
return handle_update_movie(cookies, jwt);
}
case CMD_GET_COLLECTIONS: {
return handle_get_collections(cookies, jwt);
}
case CMD_GET_COLLECTION: {
return handle_get_collection(cookies, jwt);
}
case CMD_DELETE_COLLECTION: {
return handle_delete_collection(cookies, jwt);
}
case CMD_ADD_MOVIE_TO_COLLECTION: {
return handle_add_movie_to_collection(cookies, jwt);
}
case CMD_DELETE_MOVIE_FROM_COLLECTION: {
return handle_delete_movie_from_collection(cookies, jwt);
}
case CMD_LOGOUT: {
return handle_logout(cookies, jwt);
}
default:
fprintf(stderr, "How'd you get here?\n");
return NULL;
}
}
char *handle_login_admin(unordered_map<string, string> cookies) {
// Read user.
cout << "username=";
char *user = get_input();
cout << "password=";
char *password = get_input();
// Fill json.
json j;
j["username"] = user;
j["password"] = password;
// Body.
string body = j.dump();
// Assemble request.
char *req = compute_post_request(HOST, ADMIN_LOGIN_PATH, JSON, body.c_str(), body.size(), cookies, NULL);
// Return request buffer.
return req;
}
void send_req(int fd, char *buf, ssize_t len) {
ssize_t total = 0;
ssize_t sent;
while (total < len) {
sent = send(fd, (void *)(buf + total), len - total, 0);
DIE(sent < 0, "send error");
if (sent == 0) {
break;
}
total += sent;
}
free(buf);
}
char *recv_resp(int fd, ssize_t &len) {
char *buf = (char *)malloc(BUFSIZ);
DIE(buf == NULL, "Bro");
ssize_t total = 0;
ssize_t recvd;
// Response buffer.
char *resp = (char *)malloc(BUFSIZ);
ssize_t resp_len = BUFSIZ;
ssize_t off = 0;
// Read loop.
while (1) {
recvd = recv(fd, (void *)buf, BUFSIZ, 0);
DIE(recvd < 0, "recv error");
if (recvd == 0) {
// File was closed. Return buffer.
break;
}
// If new data overflows, realloc buffer.
if (off + recvd > resp_len) {
resp = (char *)realloc(resp, resp_len * 2);
resp_len *= 2;
}
// Copy new data over.
memcpy(resp + off, buf, recvd);
// Update data offset.
off += recvd;
}
// free(buf);
len = off;
return resp;
}
void read_resp(char *resp, ssize_t len, unordered_map<string, string> &cookies, int type, char *&jwt) {
int code;
char code_str[LINELEN];
char msg[BUFSIZ];
memset(msg, '\0', sizeof(msg));
// Show message or error.
if (strstr(resp, "{\"message\":\"") || strstr(resp, "{\"error\":\"")) {
int rc = parse_http_hdr(resp, len, code, code_str, msg, cookies);
if (rc < 0) {
fprintf(stderr, "Couldn't parse header\n");
return;
}
interpret_code(resp, msg, msg);
return;
// Show json output.
} else {
switch (type) {
case CMD_GET_USERS: {
char *p = strstr(resp, HTTP_HDR_END);
if (!p) {
fprintf(stderr, "Couldn't find end of http header\n");
return;
}
int rc = interpret_code(resp, "List of users", "Couldn't get list");
if (rc < 0) return;
p += strlen(HTTP_HDR_END);
json j = json::parse(p);
for (const auto& user : j["users"]) {
int id = user["id"].get<int>();
string usr = user["username"].get<string>();
string pwd = user["password"].get<string>();
cout << "#" << id << " " << usr << ":" << pwd << endl;
}
break;
}
case CMD_GET_ACCESS: {
char *p = strstr(resp, "{\"token\":\"");
if (p) {
p += strlen("{\"token\":\"");
char *t = strchr(p, '\"');
if (!t) {
fprintf(stderr, "Couldn't find end of token\n");
return;
}
t[0] = '\0';
jwt = (char *)malloc(BUFSIZ);
DIE(jwt == NULL, "rip bro");
int len = snprintf(jwt, BUFSIZ, "%s", p);
DIE(len < 0, "snprintf error");
// Restore resp
t[0] = '\"';
} else {
fprintf(stderr, "No token\n");
}
interpret_code(resp, "Access granted", "Access denied");
break;
}
case CMD_GET_MOVIES: {
char *p = strstr(resp, HTTP_HDR_END);
if (!p) {
fprintf(stderr, "Couldn't find end of http header\n");
return;
}
int rc = interpret_code(resp, "Movies list", "Access denied");
if (rc < 0) return;
p += strlen(HTTP_HDR_END);
// Get movies json
json j = json::parse(p);
auto movies_lst = j["movies"];
for (const auto &movie : movies_lst) {
cout << "#" << movie["id"] << " " << movie["title"].get<string>() << endl;
}
break;
}
case CMD_GET_MOVIE: {
char *p = strstr(resp, HTTP_HDR_END);
if (!p) {
fprintf(stderr, "Couldn't find end of http header\n");
return;
}
int rc = interpret_code(resp, "Movie details", "Access denied");
if (rc < 0) return;
p += strlen(HTTP_HDR_END);
// Get movie details json
json j = json::parse(p);
cout << "title: " << j["title"].get<string>() << endl
<< "year: " << j["year"] << endl
<< "description: " << j["description"].get<string>() << endl
<< "rating: " << stof(j["rating"].get<string>()) << endl;
break;
}
case CMD_ADD_MOVIE: {
interpret_code(resp, "Movie added", "Couldn't add movie");
break;
}
case CMD_DELETE_USER: {
interpret_code(resp, "User deleted", "Couldn't delete user");
break;
}
case CMD_GET_COLLECTIONS: {
char *p = strstr(resp, HTTP_HDR_END);
if (!p) {
fprintf(stderr, "Couldn't find end of http header\n");
return;
}
int rc = interpret_code(resp, "Collections", "Couldn't access collections");
if (rc < 0) return;
p += strlen(HTTP_HDR_END);
// Get collections
json j = json::parse(p);
auto collections = j["collections"];
for (const auto &col : collections) {
cout << "#" << col["id"].get<int>() << ": " << col["title"].get<string>() << endl;
}
break;
}
case CMD_GET_COLLECTION: {
int rc = interpret_code(resp, "Collection details", "Couldn't get collection details");
if (rc < 0) return;
char *p = strstr(resp, HTTP_HDR_END);
if (!p) {
fprintf(stderr, "Couldn't find end of http header\n");
return;
}
p += strlen(HTTP_HDR_END);
// Get collection
json j = json::parse(p);
cout << "title: " << j["title"].get<string>() << endl;
cout << "owner: " << j["owner"].get<string>() << endl;
for (const auto &movie : j["movies"])
cout << "#" << movie["id"].get<int>() << ": " << movie["title"].get<string>() << endl;
break;
}
default: {
interpret_code(resp, "Good", "Bad");
}
}
}
}
int interpret_code (char *resp, const char *poz_msg, const char *neg_msg) {
int code = parse_http_code(resp);
char code_str[BUFSIZ];
parse_http_code_str(resp, code_str);
if (code >= SUCCESS_CODE_START && code <= SUCCESS_CODE_END) {
cout << SUCCES << ": " << code << " - " << code_str << " - " << poz_msg << endl;
return 0;
} else {
cout << ERROR << ": " << code << " - " << code_str << " - " << neg_msg << endl;
return -1;
}
}
void parse_http_code_str(char *buf, char *code_str) {
char *p = strchr(buf, ' ');
if (!p) {
fprintf(stderr, "Couldn't find start of first message\n");
return;
}
p += 5;
char *t = strstr(buf, "\r\n");
if (!t) {
fprintf(stderr, "Couldn't find end of first line\n");
return;
}
t[0] = '\0';
int len = snprintf(code_str, LINELEN, "%s", p);
DIE(len < 0, "snprintf error");
// Restore buf
t[0] = '\r';
}
int parse_http_code(char *buf) {
char *p = strchr(buf, ' ');
if (!p) {
fprintf(stderr, "Couldn't find first space in hdr\n");
return -1;
}
p += 1;
char *t = strchr(p, ' ');
if (!t) {
fprintf(stderr, "Couldn't find second space in hdr\n");
return -1;
}
t[0] = '\0';
int a = atoi(p);
// Restore resp
t[0] = ' ';
return a;
}
json parse_json(char *buf) {
char *p = strstr(buf, "\r\n\r\n");
p += 4;
return json::parse(p);
}
int parse_http_hdr(char *resp, ssize_t len, int &code, char *code_str, char *msg, unordered_map<string, string> &cookies) {
char *temp = (char *)malloc(len);
memcpy(temp, resp, len);
// This gets me to the code.
char *p = strchr(temp, ' ');
if (p == NULL) {
fprintf(stderr, "Couldn't find first space in resp\n");
return -1;
}
p += 1;
// Place terminator on next space.
char *t = strchr(p, ' ');
if (t == NULL) {
fprintf(stderr, "Couldn't find second space in resp\n");
return -1;
}
t[0] = '\0';
// Extract code.
code = atoi(p);
t[0] = 'a'; // whatever.
// Get to code meaning.
t += 1;
// Get to end of line.
p = strstr(temp, "\r\n");
if (p == NULL) {
fprintf(stderr, "Couldn't find end of first line\n");
return -1;
}
p[0] = '\0';
int len2 = snprintf(code_str, LINELEN, "%s", t);
DIE(len2 < 0, "snprintf error");
p[0] = 'a'; // whatever.
// Get cookies.
p = strstr(temp, "Set-Cookie: ");
if (p != NULL) {
p += strlen("Set-Cookie: ");
char *eq = strchr(p, '=');
if (eq != NULL) {
eq[0] = '\0';
string key;
key.clear();
key.append(p);
eq[0] = '=';
p = eq + 1;
eq = strchr(eq, ';');
if (eq != NULL) {
eq[0] = '\0';
string val;
val.clear();
val.append(p);
// Add cookie
cookies[key] = val;
} else fprintf(stderr, "Couldn't find end of cookie\n");
} else fprintf(stderr, "Couldn't find equal sign in cookie\n");
}
// Get to message.
if (p = strstr(resp, "{\"message\":\"")) {
p += strlen("{\"message\":\"");
t = strchr(p, '\"');
if (t == NULL) {
fprintf(stderr, "Couldn't find end of message\n");
}
t[0] = '\0';
len2 = snprintf(msg, BUFSIZ, "%s", p);
DIE(len2 < 0, "snprintf error");
} else if (p = strstr(resp, "{\"error\":\"")) {
// If we ain't got a message check for an error.{
p += strlen("{\"error\":\"");
t = strchr(p, '\"');
if (t == NULL) {
fprintf(stderr, "Couldn't find end of message\n");
}
t[0] = '\0';
len2 = snprintf(msg, BUFSIZ, "%s", p);
DIE(len2 < 0, "snprintf error");
return 0;
} else {
return 0;
}
free(temp);
return 0;
}
char *handle_add_user(unordered_map<string, string> cookies) {
// Read user.
cout << "username=";
char *user = get_input();
cout << "password=";
char *password = get_input();
// Fill json.
json j;
j["username"] = user;
j["password"] = password;
// Body.
string body = j.dump();
// Assemble request.
char *req = compute_post_request(HOST, ADD_USER_PATH, JSON, body.c_str(), body.size(), cookies, NULL);
// Return request buffer.
return req;
}
char *handle_get_users(unordered_map<string, string> cookies) {
return compute_get_request(HOST, GET_USERS_PATH, cookies, NULL);
}
char *handle_delete_user(unordered_map<string, string> cookies) {
// Get usernam
cout << "username=";
char *username = get_input();
// Complete path
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s%s", DELETE_USER_PATH, username);
DIE(len < 0, "snprintf error");
// Compute request
return compute_delete_request(HOST, complete_path, cookies, NULL, NULL);
}
char *handle_logout_admin(unordered_map<string, string> cookies, char *jwt) {
char *req = compute_get_request(HOST, ADMIN_LOGOUT_PATH, cookies, NULL);
return req;
}
char *handle_login(unordered_map<string, string> cookies) {
// Read user.
cout << "admin_username=";
char *admin = get_input();
cout << "username=";
char *user = get_input();
cout << "password=";
char *password = get_input();
// Fill json.
json j;
j["admin_username"] = admin;
j["username"] = user;
j["password"] = password;
// Body.
string body = j.dump();
// Assemble request.
char *req = compute_post_request(HOST, USER_LOGIN_PATH, JSON, body.c_str(), body.size(), cookies, NULL);
// Return request buffer.
return req;
}
char *handle_get_access(unordered_map<string, string> cookies) {
return compute_get_request(HOST, GET_ACCESS_PATH, cookies, NULL);
}
char *handle_get_movies(unordered_map<string, string> cookies, char *jwt) {
return compute_get_request(HOST, GET_MOVIES_PATH, cookies, jwt);
}
char *handle_get_movie(unordered_map<string, string> cookies, char *jwt) {
// Get id.
cout << "id=";
char *id = get_input();
// Compute complete path
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s%s", GET_MOVIE_PATH, id);
DIE(len < 0, "snprintf error");
return compute_get_request(HOST, complete_path, cookies, jwt);
}
char *handle_add_movie(unordered_map<string, string> cookies, char *jwt) {
// Read user.
cout << "title=";
char *title = get_input();
cout << "year=";
int year = atoi(get_input());
cout << "description=";
char *description = get_input();
cout << "rating=";
float rating = stof(get_input());
// Fill json.
json j;
j["title"] = title;
j["year"] = year;
j["description"] = description;
j["rating"] = rating;
// Body.
string body = j.dump();
// Assemble request.
char *req = compute_post_request(HOST, ADD_MOVIE_PATH, JSON, body.c_str(), body.size(), cookies, jwt);
// Return request buffer.
return req;
}
char *handle_delete_movie(unordered_map<string, string> cookies, char *jwt) {
// Read id
cout << "id=";
char *id = get_input();
// Compute complete path
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s%s", DELETE_MOVIE_PATH, id);
DIE(len < 0, "snprintf error");
// Assemble request
return compute_delete_request(HOST, complete_path, cookies, jwt, NULL);
}
char *handle_update_movie(unordered_map<string, string> cookies, char *jwt) {
// Read user.
cout << "id=";
char *id = get_input();
cout << "title=";
char *title = get_input();
cout << "year=";
int year = atoi(get_input());
cout << "description=";
char *description = get_input();
cout << "rating=";
float rating = stof(get_input());
// Fill json.
json j;
j["id"] = id;
j["title"] = title;
j["year"] = year;
j["description"] = description;
j["rating"] = rating;
// Body.
string body = j.dump();
// Complete path
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s%s", UPDATE_MOVIE_PATH, id);
DIE(len < 0, "snprintf error");
// Assemble request.
char *req = compute_put_request(HOST, complete_path, JSON, body.c_str(), body.size(), cookies, jwt);
// Return request buffer.
return req;
}
char *handle_get_collections(unordered_map<string, string> cookies, char *jwt) {
return compute_get_request(HOST, GET_COLLECTIONS_PATH, cookies, jwt);
}
char *handle_get_collection(unordered_map<string, string> cookies, char *jwt) {
// Get collection id
cout << "id=";
char *id = get_input();
// Complete path
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s%s", GET_COLLECTION_PATH, id);
DIE(len < 0, "snprintf error");
return compute_get_request(HOST, complete_path, cookies, jwt);
}
void handle_add_collection(int sockfd, unordered_map<string, string> cookies, char *jwt) {
// Read user.
cout << "title=";
char *title = get_input();
cout << "num_movies=";
int num_movies = atoi(get_input());
int ids_to_add[100];
for (int i = 0; i < num_movies; i++) {
cout << "movie_id[" << i << "]=";
ids_to_add[i] = atoi(get_input());
}
json j;
j["title"] = title;
// Body.
string body = j.dump();
// Assemble request.
char *req = compute_post_request(HOST, ADD_COLLECTION_PATH, JSON, body.c_str(), body.size(), cookies, jwt);
// Send it to server.
send_to_server(sockfd, req);
// Get resp
ssize_t len;
char *resp = recv_resp(sockfd, len);
if (resp == NULL) {
fprintf(stderr, "Error receiving response.\n");
return;
}
int rc = interpret_code(resp, "Collection added", "Couldn't add collection");
if (rc < 0) return;
// Get new collection id from response
char *p = strstr(resp, HTTP_HDR_END);
if (!p) {
fprintf(stderr, "Couldn't find end of http header\n");
return;
}
p += strlen(HTTP_HDR_END);
j = json::parse(p);
int cid = j["id"].get<int>();
string col_id = to_string(cid);
// Create server address.
struct sockaddr_in addr;
memset((void *)&addr, 0, sizeof(addr));
// Fill in fields.
addr.sin_addr.s_addr = htonl(IP);
addr.sin_port = htons(PORT);
addr.sin_family = AF_INET;
close(sockfd);
// Send add requests
for (int i = 0; i < num_movies; i++) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
DIE(sockfd < 0, "socket error");
rc = connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
DIE(rc < 0, "connect error");
char *req_add = handle_add_movie_to_collection_for_new_col(cookies, jwt, col_id, ids_to_add[i]);
send_to_server(sockfd, req_add);
char *resp_add = recv_resp(sockfd, len);
rc = interpret_code(resp_add, "Movie added to collection", "Couldn't add movie to collection");
close(sockfd);
// Stop after first bad movie
if (rc < 0) break;
}
}
char *handle_delete_collection(unordered_map<string, string> cookies, char *jwt) {
// Read user.
cout << "id=";
char *id = get_input();
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s%s", GET_COLLECTION_PATH, id);
DIE(len < 0, "snprintf error");
return compute_delete_request(HOST, complete_path, cookies, jwt, NULL);
}
char *handle_add_movie_to_collection_for_new_col(unordered_map<string, string> cookies, char *jwt, string col_id, int movie_id) {
// Read collection id
cout << "collection_id=" << col_id << endl;
// Read movie id
cout << "movie_id=" << movie_id << endl;
// Fill json.
json j;
j["id"] = movie_id;
// Body.
string body = j.dump();
// Complete path.
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s/%s/movies", GET_COLLECTIONS_PATH, col_id.c_str());
DIE(len < 0, "snprintf error");
// Assemble request.
char *req = compute_post_request(HOST, complete_path, JSON, body.c_str(), body.size(), cookies, jwt);
// Return request buffer.
return req;
}
char *handle_add_movie_to_collection(unordered_map<string, string> cookies, char *jwt) {
// Read collection id
cout << "collection_id=";
char *collection_id = get_input();
// Read movie id
cout << "movie_id=";
int movie_id = atoi(get_input());
// Fill json.
json j;
j["id"] = movie_id;
// Body.
string body = j.dump();
// Complete path.
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s/%s/movies", GET_COLLECTIONS_PATH, collection_id);
DIE(len < 0, "snprintf error");
// Assemble request.
char *req = compute_post_request(HOST, complete_path, JSON, body.c_str(), body.size(), cookies, jwt);
// Return request buffer.
return req;
}
char *handle_delete_movie_from_collection(unordered_map<string, string> cookies, char *jwt) {
// Read
cout << "collection_id=";
char *collection_id = get_input();
cout << "movie_id=";
char *movie_id = get_input();
// Complete path.
char complete_path[BUFSIZ];
int len = snprintf(complete_path, BUFSIZ, "%s%s/movies/%s", GET_COLLECTION_PATH, collection_id, movie_id);
DIE(len < 0, "snprintf error");
// Compute request
return compute_delete_request(HOST, complete_path, cookies, jwt, NULL);
}
char *handle_logout(unordered_map<string, string> cookies, char *jwt) {
char *req = compute_get_request(HOST, LOGOUT_PATH, cookies, NULL);
return req;
}