-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseRequest.cpp
More file actions
360 lines (326 loc) · 11.2 KB
/
parseRequest.cpp
File metadata and controls
360 lines (326 loc) · 11.2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parseRequest.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fbouanan <fbouanan@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/28 15:39:32 by fbouanan #+# #+# */
/* Updated: 2023/03/18 18:01:33 by fbouanan ### ########.fr */
/* */
/* ************************************************************************** */
#include "http.hpp"
parseRequest::parseRequest()
{
}
parseRequest::parseRequest(const parseRequest &data)
{
*this = data;
}
parseRequest::~parseRequest() {
}
parseRequest &parseRequest::operator=(const parseRequest &data)
{
this->_data = data._data;
return *this;
}
void parseRequest::parse_url(std::string _url)
{
std::string token;
std::stringstream ss(_url);
while (std::getline(ss, token, ' ')) {
if (token == "GET" || token == "POST" || token == "DELETE") {
this->_data["method"] = token;
// this->_data.insert(std::make_pair("method", token));
}
else if (token[0] == '/') {
// this->_data.insert(std::make_pair("path", token));
this->_data["path"] = token;
}
else {
// this->_data.insert(std::make_pair("version", token));
this->_data["version"] = token;
}
}
}
void parseRequest::parse_infos(std::string _data)
{
std::vector<std::string> output;
std::string token;
std::stringstream ss(_data);
while (std::getline(ss, token, ' ')) {
output.push_back(token);
}
if (output[0] == "Host:"){
// this->_data["Host"] = output[1];
this->_data.insert(std::make_pair("Host", output[1]));
}
else if (output[0] == "Content-Type:") {
this->_data.insert(std::make_pair("Content-Type", output[1]));
}
else if (output[0] == "Content-Length:") {
this->_data.insert(std::make_pair("Content-Length", output[1]));
}
else if (output[0] == "Transfer-Encoding:") {
this->_data.insert(std::make_pair("Transfer-Encoding", output[1]));
}
else if (output[0] == "Accept:") {
this->_data.insert(std::make_pair("Accept", output[1]));
}
else if (output[0] == "Accept-Encoding:")
{
this->_data.insert(std::make_pair("Accept-Encoding", output[0]));
}
else if (output[0] == "Host:")
{
this->_data.insert(std::make_pair("Host", output[1]));
}
else if (output[0] == "Cookie:")
{
this->_data.insert(std::make_pair("Cookie", output[1]));
}
}
void parseRequest::save_body(std::string req) {
std::string token;
std::stringstream ss(req);
std::vector<std::string> tmp;
while (std::getline(ss, token)) {
tmp.push_back(token);
}
std::vector<std::string>::iterator it = tmp.begin();
while (it != tmp.end()) {
if (*it == "\n") {
break;
}
it++;
}
while (it != tmp.end()) {
this->_body.push_back(*it);
it++;
}
}
void parseRequest::parse_request(std::string request)
{
// std::cout << "----------------------------------------------------" << std::endl;
std::vector<std::string> output;
std::string token;
std::stringstream ss(request);
while (std::getline(ss, token, '\n')){
output.push_back(token);
}
parse_url(output[0]);
std::vector<std::string>::iterator it;
for (it = output.begin() + 1; it != output.end(); it++){
parse_infos(*it);
}
// std::cout << "----------------------------------------------------" << std::endl;
}
//--------------------------------checking request-------------------------------------------
int check_url(std::string url)
{
std::string::iterator it = url.begin();
while (it != url.end()) {
if (!isprint(*it)){
return (1);
}
it++;
}
return (0);
}
int check_url_size (std::string url) {
// printf("here\n");
if (url.size() > 2048)
return (1);
return (0);
}
std::string get_pure_one(std::string &location_val,std::string url)
{
// std::cout<<<<
//if (location_val.lenght() == url.lenght())
std::string::iterator c = url.begin() + location_val.length();
std::string str;
for(;c != url.end();c++)
str.push_back(*c);//segfault if url is a directory
//exit(0);
return str;
}
int matched_location(Server &server ,std::string url,Client &client)
{
// printf("here\n");
// std::cout << "url = " << url << std::endl;
// std::vector<std::string>::iterator itar = server.server_config.L[0].error_cods.begin();
int i = 0;
client.link_location = url;
std::vector<Location>::iterator it = server.server_config.L.begin();
std::vector<size_t> vec;
Location save;
bool flag = false;
while (it != server.server_config.L.end()) {
i = 0;
Location &loc = *it;
size_t pos = url.find(loc.location_val);
if (pos != std::string::npos)
{
if (!flag) {
save = loc;
flag = true;
}
if (loc.location_val.length() >= save.location_val.length()) {
save = loc;
}
}
it++;
}
client.location = save;
client.location.location_val = get_pure_one(client.location.location_val,url);
// std::cout<<"locval =======>"<<client.location.location_val<<std::endl;
// exit(0);
// std::cout << "upload = " << save.upload << std::endl;
if (save.location_val.empty())
return (-1);
return (0);
}
//-------------------------------------------------------------------------------------------
int is_file(Client &client) {
std::string file = client.parse._data["path"];
for (size_t i = 0; i < file.length(); i++) {
if (file[i] == '.') {
if (i+1 < file.length() && isalnum(file[i+1])) {
return (1);
} else {
std::cout << "Dot is not followed by a character." << std::endl;
return (0);
}
}
}
return (0);
}
int find_index(Client &client, std::string method) {
int i = 0;
while(i < 3) {
if (client.location.methods[i] == method)
return (1);
i++;
}
return (0);
}
//--------------------------------checking methods-------------------------------------------
void check_methods(Server &server, Client &client)
{
Response res(client);
//res.client = &client;
// res.client = client.parse;
// std::cout << "method = " << server.server_config.methods[0] << std::endl;
if (client.parse._data["method"] == "GET" && find_index(client, "GET")) {
res.Get(server);
//server.write_in_socket_client("HTTP/1.1 405 KO\nContent-Type: text/html\nContent-Length: 221\r\n\r\n","405error.html", client);
}
else if (client.parse._data["method"] == "POST" && find_index(client, "POST")) {
if (is_file(client)) {
res.Post(server, FILE);
// res.Post(server);
}
else {
res.Post(server, DIRE);
}
}
else if (client.parse._data["method"] == "DELETE" && find_index(client, "DELETE")) {
if (access((client.location.root_val + client.location.location_val).c_str(), F_OK) == 0) {
res.Delete(server, FILE);
}
else {
res.Delete(server, DIRE);
}
}
else {
server.write_in_socket_client("HTTP/1.1 405 KO\nContent-Type: text/html\nContent-Length: "+std::to_string(getFileSize(getErrorFileName(client,"405")))+"\r\n\r\n",getErrorFileName(client, "405"), client);
return ;
}
}
//-------------------------------------------------------------------------------------------
void parseRequest::display_request(parseRequest parse)
{
std::map<std::string, std::string>::iterator it = parse._data.begin();
// showning results by iterating inside map container
while (it != parse._data.end())
{
std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;
++it;
}
// std::cout << "-----------------body----------------------\n";
// // std::cout << parse._body << std::endl;
// std::cout << "-----------------body----------------------\n";
}
std::string getErrorFileName(Client &client ,std::string code) {
std::string fileName;
std::vector<std::string>::iterator it = client.location.error_cods.begin();
std::vector<std::string>::iterator it1 = client.location.files_path.begin();
std::cout << "-----------------" << *it << std::endl;
while (it != client.location.error_cods.end()) {
printf("here-------------------\n");
if (*it == code) {
fileName = *it1;
std::cout << "fileName = " << fileName << std::endl;
return fileName;
}
it++;
it1++;
}
std::string _default;
_default = code.append("page.html");
std::cout << "default = " << _default << std::endl;
return _default;
}
int isMethodValid(parseRequest &p) {
if (p._data["method"] != "POST" && p._data["method"] != "GET" && p._data["method"] != "DELETE") {
return -1;
}
return 0;
}
void parseRequest::check_request(Server &server,Client &iter) {
if (!iter.checker) {
int _check_matched_location = matched_location(server ,this->_data["path"],iter);
if (_check_matched_location == -1) {
server.write_in_socket_client("HTTP/1.1 404 KO\nContent-Type: text/html\nContent-Length: "+std::to_string(getFileSize(getErrorFileName(iter,"404")))+"\r\n\r\n",getErrorFileName(iter, "404"), iter);
return ;
}
if (isMethodValid(*this) == -1) {
server.write_in_socket_client("HTTP/1.1 405 KO\nContent-Type: text/html\nContent-Length: "+std::to_string(getFileSize(getErrorFileName(iter,"405")))+"\r\n\r\n",getErrorFileName(iter, "405"), iter);
return ;
}
// printf("here\n");
if (this->_data["method"] == "POST") {
if (this->_data["Content-Length"].length() == 1){
server.write_in_socket_client("HTTP/1.1 400 KO\nContent-Type: text/html\nContent-Length: "+std::to_string(getFileSize(getErrorFileName(iter,"400")))+"\r\n\r\n",getErrorFileName(iter, "400"), iter);
return ;
}
}
int _check_url = check_url(this->_data["path"]);
if (_check_url) {
server.write_in_socket_client("HTTP/1.1 400 KO\nContent-Type: text/html\nContent-Length: "+std::to_string(getFileSize(getErrorFileName(iter,"400")))+"\r\n\r\n",getErrorFileName(iter, "400"), iter);
return;
}
int _check_url_size = check_url_size(this->_data["path"]);
if (_check_url_size) {
server.write_in_socket_client("HTTP/1.1 414 KO\nContent-Type: text/html\nContent-Length: "+std::to_string(getFileSize(getErrorFileName(iter,"414")))+"\r\n\r\n",getErrorFileName(iter, "414"), iter);
return ;
}
if ((size_t)std::atoi(this->_data["Content-Length"].c_str()) > server.body_size){
//std::cout << "body size = " << std::atoi(iter.location.body_size.c_str()) << std::endl;
server.write_in_socket_client("HTTP/1.1 413 KO\nContent-Type: text/html\nContent-Length: "+std::to_string(getFileSize(getErrorFileName(iter,"413")))+ "\r\n\r\n",getErrorFileName(iter, "413"), iter);
return ;
}
if (!iter.location.redirec.empty()) {
std::cout << "redirec = " << iter.location.redirec << std::endl;
// server.write_in_socket_client("HTTP/1.1 413 KO\nContent-Type: text/html\nContent-Length: 220\r\n\r\n","413error.html", iter);
server.write_in_socket_client("HTTP/1.1 301 MOVED PERMANENTLY\nLocation: " + iter.location.redirec + "\nContent-Type: text/html\nContent-Length: "+std::to_string(getFileSize(getErrorFileName(iter,"413")))+ "\r\n\r\n", getErrorFileName(iter, "301"), iter);
return ;
}
iter.checker = true;
}
check_methods(server, iter);
}
std::map<std::string, std::string> parseRequest::getData()
{
return this->_data;
}