-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
205 lines (165 loc) · 4.59 KB
/
Copy pathhelpers.cpp
File metadata and controls
205 lines (165 loc) · 4.59 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
#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 "helpers.h"
#include "buffer.h"
#include "requests.h"
#include <iostream>
#include <sstream>
#include <string>
#include "json.hpp"
using json = nlohmann::json;
using namespace std;
#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)
//afisarea mesajelor de eroare
void invalidCommand(){
cout << "Invalid command." << endl;
}
void userNotLogged(){
cout << "User not logged." << endl;
}
void userNoAccess(){
cout << "User does not have access to the library." << endl;
}
void userHasAccess(){
cout << "User already has access." << endl;
}
void userLogged(){
cout << "User already logged." << endl;
}
void invalidID(){
cout << "Invalid ID." << endl;
}
void notSpaces(){
cout << "Do not use spaces." << endl;
}
void invalidCredentials(){
cout << "Invalid username or password." << endl;
}
void invalidBookInfo(){
cout << "Invalid book information." << endl;
}
//verifica daca un cuvant are spatii sau daca e gol
bool hasSpaces(string str){
string word = str.substr(0, str.find(" "));
if(str.empty()){
return false;
}
// verifica daca comanda data e prea lunga
if (str.size() != word.size()){
return true;
}
else{
return false;
}
}
//functie pentru verificarea id-ului
bool isNumber(const string &str){
if(str.empty()){
return false;
}
for (char const &c : str){
if (isdigit(c) == 0){
return false;
}
}
return true;
}
void error(const char *msg)
{
perror(msg);
exit(0);
}
void compute_message(char *message, const char *line)
{
strcat(message, line);
strcat(message, "\r\n");
}
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;
}
char *basic_extract_json_response(char *str)
{
return strstr(str, "{\"");
}