-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_lib.hpp
More file actions
218 lines (170 loc) · 8.15 KB
/
Copy pathclient_lib.hpp
File metadata and controls
218 lines (170 loc) · 8.15 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
#ifndef CLIENT_LIB_H
#define CLIENT_LIB_H
#include "nlohmann/json.hpp"
using nlohmann::json;
#include <iostream>
#include <string>
using namespace std;
#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>
#include <fcntl.h>
#include "util.h"
#include "requests.hpp"
#include "helpers.hpp"
#include "buffer.hpp"
/* USEFUL MACROS */
#define IP 1059093943 // 63.32.125.183 into uint32
#define PORT 8081
#define HOST "63.32.125.183:8081"
#define SUCCES "SUCCESS"
#define ERROR "ERROR"
#define SUCCESS_CODE_START 200
#define SUCCESS_CODE_END 299
#define ERROR_CODE_START 400
#define HTTP_HDR_END "\r\n\r\n"
#define JSON "application/json"
/* POST PATHS */
#define ADMIN_LOGIN_PATH "/api/v1/tema/admin/login"
#define ADD_USER_PATH "/api/v1/tema/admin/users"
#define USER_LOGIN_PATH "/api/v1/tema/user/login"
#define ADD_MOVIE_PATH "/api/v1/tema/library/movies"
#define ADD_COLLECTION_PATH "/api/v1/tema/library/collections"
/* GET PATHS */
#define GET_USERS_PATH "/api/v1/tema/admin/users"
#define ADMIN_LOGOUT_PATH "/api/v1/tema/admin/logout"
#define LOGOUT_PATH "/api/v1/tema/user/logout"
#define GET_ACCESS_PATH "/api/v1/tema/library/access"
#define GET_MOVIES_PATH "/api/v1/tema/library/movies"
#define GET_MOVIE_PATH "/api/v1/tema/library/movies/"
#define GET_COLLECTIONS_PATH "/api/v1/tema/library/collections"
#define GET_COLLECTION_PATH "/api/v1/tema/library/collections/"
/* PUT PATHS */
#define UPDATE_MOVIE_PATH "/api/v1/tema/library/movies/"
/* DELETE PATHS */
#define DELETE_USER_PATH "/api/v1/tema/admin/users/"
#define DELETE_MOVIE_PATH "/api/v1/tema/library/movies/"
/* COMMAND MACROS */
#define CMD_LOGIN_ADMIN 0
#define CMD_ADD_USER 1
#define CMD_GET_USERS 2
#define CMD_DELETE_USER 3
#define CMD_LOGOUT_ADMIN 4
#define CMD_LOGIN 5
#define CMD_GET_ACCESS 6
#define CMD_GET_MOVIES 7
#define CMD_GET_MOVIE 8
#define CMD_ADD_MOVIE 9
#define CMD_DELETE_MOVIE 10
#define CMD_UPDATE_MOVIE 11
#define CMD_GET_COLLECTIONS 12
#define CMD_GET_COLLECTION 13
#define CMD_ADD_COLLECTION 14
#define CMD_DELETE_COLLECTION 15
#define CMD_ADD_MOVIE_TO_COLLECTION 16
#define CMD_DELETE_MOVIE_FROM_COLLECTION 17
#define CMD_LOGOUT 18
#define CMD_EXIT 19
/* FUNCTION DEFINITIONS */
/// @brief Reads one BUFSIZ line from stdin
/// @return Pointer to read buffer
char *get_input();
/// @brief Converts string command to number system
/// @param command Command string
/// @return Command int
int get_type(char *command);
/// @brief Chooses what request to build based on command type
/// @param type Command type
/// @param cookies Cookies hashmap to add to command, or to extract from command
/// @param jwt Authorization if we need it
/// @return Char* to buffer with request
char *get_req(int type, unordered_map<string, string> cookies, char *jwt);
/// @brief Sends buff to fd
/// @param fd Fd of server
/// @param buf Buffer to send
/// @param len Length of buffer
void send_req(int fd, char *buf, ssize_t len);
/// @brief Read bytes from file descriptor
/// @param fd Server fd
/// @param len Length of read buffer
/// @return Char* to buffer with response
char *recv_resp(int fd, ssize_t &len);
/// @brief Chooses how to read the response based on what type of command we
/// sent and what we need to print
/// @param resp Char* to response buffer
/// @param len Length od response buffer
/// @param cookies Cookies hashmap to update with new cookies from response
/// @param type Type of command
/// @param jwt JWT token to fill in from response
void read_resp(char *resp, ssize_t len, unordered_map<string, string> &cookies,
int type, char *&jwt);
/// @brief Parses returned HTTP header and prints what we need
/// @param resp HTTP header
/// @param len Length of header
/// @param code Code of command to interpret
/// @param code_str Code meaning in string
/// @param msg Parameter to load in the parsed message
/// @param cookies Parameter to load in found cookies
/// @return Returns -1 for errors, 0 for correct execution
int parse_http_hdr(char *resp, ssize_t len, int &code, char *code_str, char *msg,
unordered_map<string, string> &cookies);
/// @brief Goes to the end of the header and returns json::parse(payload)
/// @param buf HTTP header
/// @return json payload
json parse_json(char *buf);
/// @brief Extracts the code form the header
/// @param buf HTTP header
/// @return Return code
int parse_http_code(char *buf);
/// @brief Prints interpretation of reply code from header
/// @param resp HTTP header
/// @param poz_msg Message for positive codes [200; 299]
/// @param neg_msg Message for negative codes [300; -]
/// @return Returns -1 for errors, 0 for success
int interpret_code (char *resp, const char *poz_msg, const char *neg_msg);
/// @brief Extracts the code meaning from the header
/// @param buf HTTP header
/// @param code_str Code in string
void parse_http_code_str(char *buf, char *code_str);
/* ALL CLIENT COMMANDS */
/// For all of them: they accept input for their fields then create the HTTP
/// request accordingly.
char *handle_login_admin (unordered_map<string, string> cookies);
char *handle_add_user (unordered_map<string, string> cookies);
char *handle_get_users (unordered_map<string, string> cookies);
char *handle_delete_user (unordered_map<string, string> cookies);
char *handle_logout_admin (unordered_map<string, string> cookies, char *jwt);
char *handle_login (unordered_map<string, string> cookies);
char *handle_get_access (unordered_map<string, string> cookies);
char *handle_get_movies (unordered_map<string, string> cookies, char *jwt);
char *handle_get_movie (unordered_map<string, string> cookies, char *jwt);
char *handle_add_movie (unordered_map<string, string> cookies, char *jwt);
char *handle_delete_movie (unordered_map<string, string> cookies, char *jwt);
char *handle_update_movie (unordered_map<string, string> cookies, char *jwt);
char *handle_get_collections(unordered_map<string, string> cookies, char *jwt);
char *handle_get_collection (unordered_map<string, string> cookies, char *jwt);
/// @brief Adds a collection alongside the movies for which we accept the id as input.
/// Because it also generates and sends the add_movie_to_collection by its own,
/// we need the sockfd of the server to also send them on the spot and interpret
/// the reply for each.
/// @param sockfd Socket of server
/// @param cookies Cookies to add to the requests
/// @param jwt JWT to add the Authorization to the header
void handle_add_collection(int sockfd, unordered_map<string, string> cookies, char *jwt);
/// @brief Helper function for handle_add_collection. Doesn't ask for id's of
/// movies to add because we alread have them from the previous function
/// @param cookies Cookies to add to the request
/// @param jwt JWT for authorization
/// @param col_id Collection id where we add the movies given by previous function
/// @param movie_id Movie id to add to collection given by previous function
/// @return HTTP request to add movie to collection
char *handle_add_movie_to_collection_for_new_col(unordered_map<string, string> cookies,
char *jwt, string col_id, int movie_id);
char *handle_delete_collection (unordered_map<string, string> cookies, char *jwt);
char *handle_add_movie_to_collection (unordered_map<string, string> cookies, char *jwt);
char *handle_delete_movie_from_collection(unordered_map<string, string> cookies, char *jwt);
char *handle_logout (unordered_map<string, string> cookies, char *jwt);
#endif // CLIENT_LIB_H