-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.hpp
More file actions
254 lines (203 loc) · 6.47 KB
/
helpers.hpp
File metadata and controls
254 lines (203 loc) · 6.47 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
#ifndef _HELPERS_
#define _HELPERS_
#include <stdlib.h> /* exit, atoi, malloc, free */
#include <stdio.h>
#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 <string>
#include <iostream>
#include "buffer.h"
#include "nlohmann/json.hpp"
#include "nlohmann/json_fwd.hpp"
#define BUFLEN 4096
#define LINELEN 1000
#define HEADER_TERMINATOR "\r\n\r\n"
#define HEADER_TERMINATOR_SIZE (sizeof(HEADER_TERMINATOR) - 1)
#define CONTENT_LENGTH "Content-Length: "
#define CONTENT_LENGTH_SIZE (sizeof(CONTENT_LENGTH) - 1)
#define REGISTER 1
#define LOGIN 2
#define ENTER_LIBRARY 3
#define GET_BOOKS 4
#define GET_BOOK 5
#define ADD_BOOK 6
#define DELETE_BOOK 7
#define LOGOUT 8
using namespace std;
using json = nlohmann::json;
void error(const char *msg)
{
perror(msg);
exit(0);
}
int open_connection(char *host_ip, int portno, int ip_type, int socket_type, int flag)
{
struct sockaddr_in serv_addr;
int sockfd = socket(ip_type, socket_type, flag);
if (sockfd < 0)
error("ERROR opening socket");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = ip_type;
serv_addr.sin_port = htons(portno);
inet_aton(host_ip, &serv_addr.sin_addr);
/* connect the socket */
if (connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
return sockfd;
}
void close_connection(int sockfd)
{
close(sockfd);
}
void send_to_server(int sockfd, char *message)
{
int bytes, sent = 0;
int total = strlen(message);
do
{
bytes = write(sockfd, message + sent, total - sent);
if (bytes < 0) {
error("ERROR writing message to socket");
}
if (bytes == 0) {
break;
}
sent += bytes;
} while (sent < total);
}
char *receive_from_server(int sockfd)
{
char response[BUFLEN];
buffer buffer = buffer_init();
int header_end = 0;
int content_length = 0;
do {
int bytes = read(sockfd, response, BUFLEN);
if (bytes < 0){
error("ERROR reading response from socket");
}
if (bytes == 0) {
break;
}
buffer_add(&buffer, response, (size_t) bytes);
header_end = buffer_find(&buffer, HEADER_TERMINATOR, HEADER_TERMINATOR_SIZE);
if (header_end >= 0) {
header_end += HEADER_TERMINATOR_SIZE;
int content_length_start = buffer_find_insensitive(&buffer, CONTENT_LENGTH, CONTENT_LENGTH_SIZE);
if (content_length_start < 0) {
continue;
}
content_length_start += CONTENT_LENGTH_SIZE;
content_length = strtol(buffer.data + content_length_start, NULL, 10);
break;
}
} while (1);
size_t total = content_length + (size_t) header_end;
while (buffer.size < total) {
int bytes = read(sockfd, response, BUFLEN);
if (bytes < 0) {
error("ERROR reading response from socket");
}
if (bytes == 0) {
break;
}
buffer_add(&buffer, response, (size_t) bytes);
}
buffer_add(&buffer, "", 1);
return buffer.data;
}
bool check_string(string to_check_str, string descriptor) {
if (to_check_str.size() == 0) {
cout << descriptor << " must contain at least one character!" << endl;
return false;
}
return true;
}
bool check_number(string to_check_str, string descriptor) {
if (to_check_str.size() == 0) {
cout << descriptor << " must contain at least one digit!" << endl;
return false;
}
if (to_check_str.size() > 1 && to_check_str[0] == '0') {
cout << descriptor << " must be a positive number!" << endl;
return false;
}
for (auto c : to_check_str) {
if (!isdigit(c)) {
cout << descriptor << " must be a positive number!" << endl;
return false;
}
}
return true;
}
bool check_credential(string to_check_str, string descriptor) {
if (to_check_str.size() == 0) {
cout << descriptor << " must contain at least one character!" << endl;
return false;
}
if (to_check_str.find(' ') != string::npos) {
cout << descriptor << " must not contain whitespaces!" << endl;
return false;
}
return true;
}
bool check_response(string response, int command) {
int first_line_size = response.find("\r\n");
string first_line = response.substr(0, first_line_size);
int http_size = first_line.find(" ");
first_line.erase(0, http_size + 1);
if (command == REGISTER) {
if (!first_line.compare(0, 3, "201")) {
cout << first_line << " - User registered successfully!" << endl;
return true;
}
}
if (!first_line.compare(0, 3, "200")) {
switch (command) {
case LOGIN:
cout << first_line << " - Logged in successfully!" << endl;
break;
case ENTER_LIBRARY:
cout << first_line << " - Entered library successfully!" << endl;
break;
case GET_BOOKS:
cout << first_line << " - These are your books:" << endl;
break;
case GET_BOOK:
cout << first_line << " - This is the book you requested:" << endl;
break;
case ADD_BOOK:
cout << first_line << " - Book added successfully!" << endl;
break;
case DELETE_BOOK:
cout << first_line << " - Book deleted successfully!" << endl;
break;
case LOGOUT:
cout << first_line << " - Logged out successfully!" << endl;
break;
default:
break;
}
return true;
}
cout << first_line;
unsigned int body_position = response.find("\r\n\r\n");
if (body_position == string::npos) {
cout << endl;
return false;
}
string body = response.substr(body_position);
json body_json;
if (body_json.accept(body)) {
body_json = json::parse(body);
if (body_json.contains("error"))
cout << " - " << body_json["error"];
}
cout << endl;
return false;
}
#endif