-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
377 lines (296 loc) · 10.7 KB
/
client.cpp
File metadata and controls
377 lines (296 loc) · 10.7 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
#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 <string>
#include "jsonparser.h"
#include <iostream>
bool logged_in;
char *session_cookie;
char *token;
void login(int *sockfd) {
if(strlen(session_cookie) != 0) {
std::cout << "You are already logged in!\n";
return;
}
char *message, *response;
char username[LINELEN];
char password[LINELEN];
// get the username
std::cout << "username=";
std::cin.getline(username, LINELEN);
// get the password
std::cout << "password=";
std::cin.getline(password, LINELEN);
// create post command
char **form_data = (char **) calloc(1, sizeof(char *));
form_data[0] = create_json_user(username, password);
message = compute_post_request(IP, AUTH_ROUTE, PAYLOAD_TYPE, form_data, 1, NULL, 1, NULL);
send_to_server(*sockfd, message);
response = receive_from_server(*sockfd);
if(strlen(response) == 0) // check if the connection is still active
reopen_connection_and_send(sockfd, &response, message);
if(!check_respond(response))
std::cout << "The credentials don't match the database" << "\n";
else {
std::cout << "Logged in" << "\n";
// extract session cookie
get_session_cookie(response, session_cookie);
// marked the user as logged in
logged_in = true;
}
free(message);
free(response);
free(form_data[0]);
free(form_data);
}
void reg(int *sockfd) {
char *message, *response;
char username[LINELEN];
char password[LINELEN];
// get the username
std::cout << "username=";
std::cin.getline(username, LINELEN);
// get the password
std::cout << "password=";
std::cin.getline(password, LINELEN);
// create post command
char **form_data = (char **) calloc(1, sizeof(char *));
form_data[0] = create_json_user(username, password);
message = compute_post_request(IP, REGISTER_ROUTE, PAYLOAD_TYPE, form_data, 1, NULL, 1, NULL);
//puts(message);
send_to_server(*sockfd, message);
response = receive_from_server(*sockfd);
if(strlen(response) == 0) // check if the connection is still active
reopen_connection_and_send(sockfd, &response, message);
if(!check_respond(response))
std::cout << "The username is already taken" << "\n";
else {
std::cout << "Sign up successfuly" << "\n";
}
free(message);
free(response);
free(form_data[0]);
free(form_data);
}
void enter_library(int *sockfd) {
char *message, *response;
char **cookies = (char **) calloc(1,sizeof(char*));
cookies[0] = session_cookie;
message = compute_get_request(IP, LIBRARY_ROUTE ,NULL, cookies, 1, NULL);
send_to_server(*sockfd, message);
response = receive_from_server(*sockfd);
if(strlen(response) == 0) // check if the connection is still active
reopen_connection_and_send(sockfd, &response, message);
if(!check_respond(response)) {
std::cout << "You are not logged in\n";
}
else {
char *json_response = basic_extract_json_response(response);
get_token(json_response, token);
std::cout << "Entered the library succesfully with token: \n" << token << "\n";
}
free(cookies);
free(message);
free(response);
}
void get_books(int *sockfd) {
if(strlen(token) == 0) {
std::cout << "You don't have access to the library\n";
return;
}
char *message, *response;
message = compute_get_request(IP, BOOK_ROUTE ,NULL, NULL, 1, token);
send_to_server(*sockfd, message);
response = receive_from_server(*sockfd);
if(strlen(response) == 0) // check if the connection is still active
reopen_connection_and_send(sockfd, &response, message);
if(!check_respond(response)) {
std::cout << "You don't have access to the library\n";
}
else {
char *json_response = basic_extract_json_response(response);
if(json_response != NULL)
parse_books_and_print(json_response);
else
std::cout << "No books in library!\n";
}
free(message);
free(response);
}
void get_book(int *sockfd) {
if(strlen(token) == 0) {
std::cout << "You don't have access to the library\n";
return;
}
char id[LINELEN];
int int_id;
std::cout << "id=";
std::cin.getline(id, LINELEN);
int_id = atoi(id);
if(int_id == 0) { // check id intregrity
std::cout << "ID must be an integer\n";
return;
}
char *message, *response;
char book_route_id[LINELEN];
sprintf(book_route_id, "%s/%d", BOOK_ROUTE, int_id);
message = compute_get_request(IP, book_route_id ,NULL, NULL, 1, token);
send_to_server(*sockfd, message);
response = receive_from_server(*sockfd);
if(strlen(response) == 0) // check if the connection is still active
reopen_connection_and_send(sockfd, &response, message);
if(!check_respond(response)) {
std::cout << "ID is not found in the database\n";
}
else {
char *json_response = basic_extract_json_response(response);
if(json_response != NULL)
parse_books_and_print(json_response);
}
free(message);
free(response);
}
void add_book(int *sockfd) {
if(strlen(token) == 0) {
std::cout << "You don't have access to the library\n";
return;
}
char title[LINELEN];
char author[LINELEN];
char genre[LINELEN];
char publisher[LINELEN];
char page_count[LINELEN];
// get the title
std::cout << "title=";
std::cin.getline(title, LINELEN);
// get the author
std::cout << "author=";
std::cin.getline(author, LINELEN);
// get the genre
std::cout << "genre=";
std::cin.getline(genre, LINELEN);
// get the publisher
std::cout << "publisher=";
std::cin.getline(publisher, LINELEN);
// get the page_count
std::cout << "page_count=";
std::cin.getline(page_count, LINELEN);
int int_page_count = atoi(page_count);
if(int_page_count == 0) {
std::cout << "You need to enter an integer for the page count\n";
return;
}
char *message = NULL, *response= NULL;
// create post command
char **form_data = (char **) calloc(1, sizeof(char *));
form_data[0] = create_json_book(title, author, genre, publisher, int_page_count);
message = compute_post_request(IP, ADD_BOOK_ROUTE, PAYLOAD_TYPE, form_data, 1, NULL, 1, token);
send_to_server(*sockfd, message);
response = receive_from_server(*sockfd);
if(strlen(response) == 0) // check if the connection is still active
reopen_connection_and_send(sockfd, &response, message);
if(!check_respond(response))
std::cout << "Add book failed" << "\n";
else {
std::cout << "Book added succesfully" << "\n";
}
if(message != NULL )
free(message);
if(response != NULL)
free(response);
if(form_data[0] != NULL)
free(form_data[0]);
free(form_data);
}
void delete_book(int *sockfd) {
if(strlen(token) == 0) {
std::cout << "You don't have access to the library\n";
return;
}
char id[LINELEN];
int int_id;
std::cout << "id=";
std::cin.getline(id, LINELEN);
int_id = atoi(id);
if(int_id == 0) { // check id intregrity
std::cout << "ID must be an integer\n";
return;
}
char *message, *response;
char book_route_id[LINELEN];
sprintf(book_route_id, "%s/%d", BOOK_ROUTE, int_id);
message = compute_delete_request(IP, book_route_id, NULL, 1, token);
send_to_server(*sockfd, message);
response = receive_from_server(*sockfd);
if(strlen(response) == 0) // check if the connection is still active
reopen_connection_and_send(sockfd, &response, message);
if(!check_respond(response)) {
std::cout << "ID is not found in the database\n";
}
else {
std::cout << "Book deleted succesfully\n";
}
free(message);
free(response);
}
void logout(int *sockfd) {
char *message, *response;
char **cookies = (char **) calloc(1,sizeof(char*));
cookies[0] = session_cookie;
message = compute_get_request(IP, DEAUTH_ROUTE,NULL, cookies, 1, NULL);
send_to_server(*sockfd, message);
response = receive_from_server(*sockfd);
if(strlen(response) == 0) // check if the connection is still active
reopen_connection_and_send(sockfd, &response, message);
if(!check_respond(response))
std::cout << "You are not logged in\n";
else {
std::cout << "Logged out succesfully\n";
}
memset(session_cookie, 0, SESSION_COOKIE_LEN);
memset(token, 0, TOKEN_LEN);
free(cookies);
free(message);
free(response);
}
int main()
{
int sockfd;
sockfd = open_connection(IP, PORT, AF_INET, SOCK_STREAM, 0);
// initialisation of global variables
session_cookie = (char *) calloc(SESSION_COOKIE_LEN, sizeof(char));
token = (char *) calloc(TOKEN_LEN, sizeof(char));
logged_in = false;
// Main loop of the program listen for commands while the command is not exit
char command[LINELEN];
std::cin.getline(command, MAX_COMMAND_LEN);
while(strcmp(command, EXIT_COMMAND) != 0) {
if(strcmp(command, LOGIN_COMMAND) == 0)
login(&sockfd);
else if(strcmp(command, REGISTER_COMMAND) == 0)
reg(&sockfd);
else if(strcmp(command, ENTER_LIBRARY_COMMAND) == 0)
enter_library(&sockfd);
else if(strcmp(command, GET_BOOKS_COMMAND) == 0)
get_books(&sockfd);
else if(strcmp(command, GET_BOOK_COMMAND) == 0)
get_book(&sockfd);
else if(strcmp(command, ADD_BOOK_COMMAND) == 0)
add_book(&sockfd);
else if(strcmp(command, DELETE_BOOK_COMMAND) == 0)
delete_book(&sockfd);
else if(strcmp(command, LOGOUT_COMMAND) == 0)
logout(&sockfd);
std::cin.getline(command, MAX_COMMAND_LEN);
}
free(session_cookie);
free(token);
return 0;
}