-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.c
More file actions
305 lines (261 loc) · 11.5 KB
/
Copy pathclient.c
File metadata and controls
305 lines (261 loc) · 11.5 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
#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 <stdbool.h>
#include "helpers.h"
#include "requests.h"
#include "parson.h"
// printeaza raspunsul serverului
void getMessage(char* response) {
char* p = strtok(response, "\n");
while (p != NULL) {
char* copie = p;
p = strtok(NULL, "\n");
if (p == NULL) {
printf("%s\n\n", copie);
}
}
}
int main(int argc, char *argv[])
{
char* message;
char* response;
int sockfd;
char* host = calloc(50, sizeof(char));
// salvez host ul
sprintf(host, "%s", "ec2-3-8-116-10.eu-west-2.compute.amazonaws.com");
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
char *serialized_string = NULL;
char command[30], username[30], password[30];
char title[50], author[50], genre[50], publisher[50], bookId[15];
int page_count;
char* cookie = NULL;
char* token = NULL;
bool logged_in = false;
bool acces_in = false;
while (1) {
// primire comanda
scanf("%s", command);
// daca comanda este register
if (strncmp(command, "register", 8) == 0) {
// deschidem o conexiuna noua
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// se asteapta username-ul si parola
printf("username=");
scanf("%s", username);
json_object_set_string(root_object, "username", username);
printf("password=");
scanf("%s", password);
json_object_set_string(root_object, "password", password);
// realizam stringul de tip json
serialized_string = json_serialize_to_string_pretty(root_value);
// compunem mesajul corespunzator
message = compute_post_request(host, "/api/v1/tema/auth/register", "application/json",
serialized_string, NULL, NULL);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
close_connection(sockfd);
// afisez mesajul intors de server
getMessage(response);
}
// daca comanda este login
if (strncmp(command, "login", 5) == 0) {
if (!logged_in) {
// deschidem o conexiuna noua
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// se asteapta username-ul si parola
// construire JSON
printf("username=");
scanf("%s", username);
json_object_set_string(root_object, "username", username);
printf("password=");
scanf("%s", password);
json_object_set_string(root_object, "password", password);
// realizam stringul de tip json
serialized_string = json_serialize_to_string_pretty(root_value);
// compunem mesajul corespunzator
message = compute_post_request(host, "/api/v1/tema/auth/login", "application/json",
serialized_string, NULL, NULL);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
close_connection(sockfd);
// daca raspunsul nu este o eroare inseamna ca login-ul
// s-a efectuat cu succes
if (strstr(response, "error") == NULL) {
logged_in = true;
}
// afisez mesajul intors de server
getMessage(response);
} else {
printf("Already logged in!\n");
}
}
// daca se cere acces in biblioteca
if (strncmp(command, "enter_library", 14) == 0) {
if (logged_in) {
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// compunem mesajul de login pentru a obtine un cookie valid
message = compute_post_request(host, "/api/v1/tema/auth/login", "application/json",
serialized_string, NULL, NULL);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
close_connection(sockfd);
// salvez cookie-ul
char* p = strstr(response, "connect.sid=");
if (p != NULL) {
cookie = strtok(p, ";");
}
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// compunem mesajul corespunzator
message = compute_get_request(host,
"/api/v1/tema/library/access", NULL, cookie, NULL);
send_to_server(sockfd, message);
response = basic_extract_json_response(receive_from_server(sockfd));
close_connection(sockfd);
acces_in = true;
// afisez mesajul intors de server
printf("%s\n\n", response);
// extrag token-ul
p = strstr(response, "token");
p = p + 8;
token = strtok(p, "\"");
} else {
printf("{\"You are not logged in!\"} \n\n");
}
}
// daca se cere afisarea tuturor cartilor de pe server
if (strncmp(command, "get_books", 10) == 0) {
if (acces_in && logged_in) {
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// compunem mesajul corespunzator
message = compute_get_request(host,
"/api/v1/tema/library/books", NULL, NULL, token);
send_to_server(sockfd, message);
response = basic_extract_json_response(receive_from_server(sockfd));
close_connection(sockfd);
// afisez mesajul intors de server
printf("%s \n\n", response);
} else {
printf("{\"You don't have acces!\"} \n\n");
}
}
// daca se cere informatie despre o carte
if (strncmp(command, "get_book", 9) == 0) {
if (acces_in && logged_in) {
// citesc id-ul dorit
printf("id=");
scanf("%s", bookId);
// creez url-ul
char url[50] = "/api/v1/tema/library/books/";
strcat(url, bookId);
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// compunem mesajul corespunzator
message = compute_get_request(host, url, NULL, NULL, token);
send_to_server(sockfd, message);
response = basic_extract_json_response(receive_from_server(sockfd));
printf("%s\n\n", response);
close_connection(sockfd);
} else {
printf("{\"You don't have acces!\"} \n\n");
}
}
// daca se doreste adaugarea unei carti
if (strncmp(command, "add_book", 9) == 0) {
if (acces_in && logged_in) {
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// construire JSON
printf("title=");
scanf("%s", title);
json_object_set_string(root_object, "title", title);
printf("author=");
scanf("%s", author);
json_object_set_string(root_object, "author", author);
printf("genre=");
scanf("%s", genre);
json_object_set_string(root_object, "genre", genre);
printf("page_count=");
scanf("%d", &page_count);
json_object_set_number(root_object, "page_count", page_count);
printf("publisher=");
scanf("%s", publisher);
json_object_set_string(root_object, "publisher", publisher);
// realizam stringul de tip json
serialized_string = json_serialize_to_string_pretty(root_value);
// compunem mesajul corespunzator
message = compute_post_request(host, "/api/v1/tema/library/books", "application/json",
serialized_string, NULL, token);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
close_connection(sockfd);
// afisez mesajul intors de server
getMessage(response);
} else {
printf("{\"You don't have acces!\"} \n\n");
}
}
// daca se doreste stergerea unei carti
if (strncmp(command, "delete_book", 12) == 0) {
if (acces_in && logged_in) {
// citesc id-ul dorit
printf("id=");
scanf("%s", bookId);
// creez url-ul
char url[50] = "/api/v1/tema/library/books/";
strcat(url, bookId);
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// compunem mesajul corespunzator
message = compute_delete_request(host, url, NULL, NULL, token);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
close_connection(sockfd);
// afisez mesajul intors de server
getMessage(response);
} else {
printf("{\"You don't have acces!\"} \n\n");
}
}
// daca se primeste logout
if (strncmp(command, "logout", 7) == 0) {
if (logged_in) {
logged_in = false;
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// compunem mesajul de login pentru a obtine un cookie valid
message = compute_post_request(host, "/api/v1/tema/auth/login", "application/json",
serialized_string, NULL, NULL);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
close_connection(sockfd);
// salvez cookie-ul
char* p = strstr(response, "connect.sid=");
if (p != NULL) {
cookie = strtok(p, ";");
}
sockfd = open_connection("3.8.116.10", 8080, AF_INET, SOCK_STREAM, 0);
// compunem mesajul corespunzator
message = compute_get_request(host,
"/api/v1/tema/auth/logout", NULL, cookie, NULL);
send_to_server(sockfd, message);
response = receive_from_server(sockfd);
close_connection(sockfd);
// afisez mesajul intors de server
getMessage(response);
}
}
// daca se primeste exit
if (strncmp(command, "exit", 4) == 0) {
close_connection(sockfd);
break;
}
}
// eliberam memoria
free(host);
json_free_serialized_string(serialized_string);
json_value_free(root_value);
return 0;
}