-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.cpp
More file actions
263 lines (220 loc) · 7.71 KB
/
Copy pathrequests.cpp
File metadata and controls
263 lines (220 loc) · 7.71 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
#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 "requests.h"
#include <iostream>
#include <sstream>
#include <string>
#include "json.hpp"
using json = nlohmann::json;
using namespace std;
//functia pentru POST
string POST(string host, string url, string data, string cookie, string token){
stringstream message;
stringstream response;
string temp_cookie;
// adaugare tip request, host, content-type, content-length, cookie si token
message << "POST " << url << " HTTP/1.1" << "\r\n";
message << "Host " << host << "\r\n";
message << "Content-Type: application/json" << "\r\n";
message << "Content-Length: " << data.size() << "\r\n";
if(!cookie.empty()){
message << "Cookie: " << cookie << "\r\n";
}
if(!token.empty()){
message << "Authorization: Bearer " << token << "\r\n";
}
//adaugare linie noua si date
message << "\r\n";
message << data;
// deschidere conexiune
int sockfd = open_connection((char *)"34.241.4.235\0", 8080, AF_INET, SOCK_STREAM, 0);
//parsare mesaj pentru functia send_to_server
string message_string = message.str();
char *message_char = strcpy(new char[message_string.length() + 1], message_string.c_str());
// trimite request-ul, primeste raspunsul si inchide conexiunea
send_to_server(sockfd, message_char);
response << receive_from_server(sockfd);
close(sockfd);
// register
if (!url.compare("/api/v1/tema/auth/register")){
int message_len;
// trece prin mesaj pana gaseste lungimea contentului
string temp;
while (response >> temp){
if (!temp.compare("Content-Length:")){
response >> message_len;
break;
}
}
// daca nu am primit nici un mesaj de eroare de la server (content-length == 0) atunci
// trimite un string gol, altfel trimit "eroare"
if (!message_len){
return "";
}
else{
return "error";
}
}
// login
else if (!url.compare("/api/v1/tema/auth/login")){
// trece prin fiecare linie a raspunsului
string temp;
string argument;
while (getline(response, temp)){
argument = temp.substr(0, temp.find(" "));
// daca gaseste campul "Set-Cookie" atunci retine cookie-ul
if (!argument.compare("Set-Cookie:")){
temp_cookie = temp.substr(temp.find(" ") + 1, temp.find(";") - 12);
break;
}
}
// returneaza cookie ul daca s-a gasit, altfel returneaza eroare
if (!temp_cookie.empty()){
return temp_cookie;
}
else{
return "error";
}
}
// add_book
else if (!url.compare("/api/v1/tema/library/books")){
//verificarea datelor s-a realizat in client deci se returneaza
//un mesaj gol
return "";
}
return "";
}
json GET(string host, string url, string query_param, string cookie, string token){
stringstream message;
stringstream response;
string temp_token;
// deschidere conexiune
int sockfd = open_connection((char *)"34.241.4.235\0", 8080, AF_INET, SOCK_STREAM, 0);
// adaugare tip request, host, query-params, cookie si token
if (query_param.empty()){
message << "GET " << url << " HTTP/1.1"<< "\r\n";
}else{
message << "GET " << url << "/" << query_param << " HTTP/1.1" << "\r\n";
}
message << "Host " << host << "\r\n";
if(!cookie.empty()){
message << "Cookie: " << cookie << "\r\n";
}
if(!token.empty()){
message << "Authorization: Bearer " << token << "\r\n";
}
message << "\r\n";
//parsare mesaj pentru functia send_to_server
string message_string = message.str();
char *message_char = strcpy(new char[message_string.length() + 1], message_string.c_str());
//trimite mesaj la server, primeste raspuns si inchide conexiunea
send_to_server(sockfd, message_char);
response << receive_from_server(sockfd);
close(sockfd);
//enter_library
if (!url.compare("/api/v1/tema/library/access")){
string temp;
string testValue;
string string_json;
//cauta in mesaj tokenul primit ca raspuns, sau mesajul de eroare daca
//ceva nu e in regula
while (getline(response, temp)){
testValue = temp.substr(0, temp.find(":"));
if (!testValue.compare("{\"token\"") || !testValue.compare("{\"error\"")){
string_json = temp;
break;
}
}
//parsare token primit din mesaj
json tok;
tok = json::parse(string_json);
//returneaza tokenul sau eroarea
return tok;
}
//get_books
else if (!url.compare("/api/v1/tema/library/books") && query_param.empty()){
string temp;
string testValue;
string string_json;
//cauta jsonul de carti
while (getline(response, temp)){
testValue = temp.substr(0, temp.find("[") + 1);
if (!testValue.compare("[")){
string_json = temp;
break;
}
}
//parseaza lista de carti
json book_list;
book_list = json::parse(string_json);
//returneaza lista de carti parsata
return book_list;
}
//get_book
else if (!url.compare("/api/v1/tema/library/books")&& !query_param.empty()){
string temp;
string testValue;
string string_json;
//cauta obiectul carte in raspuns
while (getline(response, temp)){
testValue = temp.substr(0, temp.find("{") + 1);
if (!testValue.compare("{") || !testValue.compare("[{")){
string_json = temp;
break;
}
}
//parseaza si returneaza cartea
json book;
book = json::parse(string_json);
return book;
//logout
}else if(!url.compare("/api/v1/tema/auth/logout")){
//a fost trimis corect mesajul catre server deci returnez
//un json gol
return json({});
}else{
return json({});
}
}
int DELETE(string host, string url, string query_param, string cookie, string token){
stringstream message;
stringstream response;
int message_len;
// adaugare tip request, host, cookie, token
message << "DELETE " << url << "/" << query_param << " HTTP/1.1" << "\r\n";
message << "Host " << host << "\r\n";
message << "Cookie: " << cookie << "\r\n";
message << "Authorization: Bearer " << token << "\r\n";
message << "\r\n";
// deschidere conexiune
int sockfd = open_connection((char *)"34.241.4.235\0", 8080, AF_INET, SOCK_STREAM, 0);
// parsare date pentru functia send_to_server
string message_string = message.str();
char *message_char = strcpy(new char[message_string.length() + 1], message_string.c_str());
// trimitere request si primire raspuns
send_to_server(sockfd, message_char);
response << receive_from_server(sockfd);
close(sockfd);
//verifica daca content-length e 0 sau nu
string temp;
while (response >> temp){
if (!temp.compare("Content-Length:")){
response >> message_len;
break;
}
}
//daca content-length e 0 atunci totul e in regula, altfel avem o eroare
if(message_len == 0){
return 1;
}else{
return 0;
}
return 0;
}