-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.cpp
More file actions
352 lines (330 loc) · 9.13 KB
/
Copy pathserver.cpp
File metadata and controls
352 lines (330 loc) · 9.13 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
#include "server.hpp"
Server::Server(conf_data *data) : _port(PORT), _finished(false), _err(false) , _data(data), _dir(false){
_data_vec.push_back(data);
size_t j;
std::string str = _data->CGI_extensions;
while (_data->CGI_extensions != "")
{
j = str.find(" ");
_cgi_types.push_back(str.substr(0, j));
str.erase(0, j+1);
if (j == std::string::npos)
break;
}
}
Server::~Server(){
}
void Server::setup_err(int err, const char *msg){
if(err < 0)
close(_serv_fd), throw(msg);
}
void Server::setup_serv(){
int ret;
int opt = 1;
try{
_err_string = "200";
_serv_fd = socket(AF_INET, SOCK_STREAM, 0);
setup_err(_serv_fd, "error socket()");
ret = setsockopt(_serv_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
setup_err(ret, "error setsockopt()");
ret = fcntl(_serv_fd, F_SETFL, O_NONBLOCK);
setup_err(ret, "error fcntl()");
_address.sin_family = AF_INET;
memset(&_address.sin_zero, 0, sizeof(_address.sin_zero));
_address.sin_addr.s_addr = INADDR_ANY;
_address.sin_port = htons(_port);
if(_data->s_host() != "localhost")
_address.sin_addr.s_addr = inet_addr(_data->s_host().c_str());
ret = bind(_serv_fd, (struct sockaddr *)&_address, sizeof(_address));
// setup_err(ret, "error bind()");
if (ret == -1)
{
perror("bind");
close(_serv_fd), throw("bind error");
}
_listen_fd = listen(_serv_fd, SIZE_POLLFD);
setup_err(_listen_fd, "error listen()");
_http_table = http_table();
_mime_types = initialize_mime_types();
}
catch(const char *msg){
std::cout << msg << std::endl;
_err = true;
}
}
void Server::accept_connections(){
int new_sock = 0;
int addrlen = sizeof(_address);
while(new_sock != ERR){
new_sock = accept(_serv_fd, (sockaddr*)&_address, (socklen_t*)&addrlen);
if(new_sock < 0){
if(errno != EWOULDBLOCK)
setup_err(new_sock, "error accept()");
new_sock = ERR;
}
else{
std::cout << "new client:" << new_sock << std::endl;
_clients.push_back(new_sock);
}
}
}
bool Server::handle_existing_connection(struct pollfd *poll){
int ret;
_end_connection = false;
if(poll->revents & POLLOUT){
ret = send_response(poll);
if(ret < 0)
{
std::cout << "send() failed" << std::endl;
close(poll->fd);
_end_connection = true;
}
else if(ret == 0){
close(poll->fd);
_end_connection = true;
}
}
else if((ret = recieve_data(poll)) > 0){
if(_storage_data == ""){
std::string temp = _storage.substr(_storage.find("Host: ") + 6);
_http_request["Host"] = temp.substr(0, temp.find("\r\n"));
}
// _http_request["HOST"] = "deus";
for (std::vector<conf_data*>::const_iterator it = _data_vec.begin(); it != _data_vec.end(); ++it)
{
std::string names = (*it)->s_names();
std::string n;
while (names != "")
{
n = names.substr(0, names.find(' '));
if (n == _http_request["Host"]){
_data = *it;
size_t j;
_cgi_types.clear();
std::string str = _data->CGI_extensions;
while (_data->CGI_extensions != "")
{
j = str.find(" ");
_cgi_types.push_back(str.substr(0, j));
str.erase(0, j+1);
if (j == std::string::npos)
break;
}
break;
}
if (names.find(' ') != names.npos)
names.erase(0, names.find(' ') + 1);
else
names.clear();
}
}
if (_storage_data == "")
{
parse_first_line(std::string(_buffer));
parse_header(_buffer);
}
process_request();
if(_finished == true){
poll->events = POLLOUT;
_storage = "";
_storage_data = "";
_finished = false;
}
}
if(_end_connection){
close(poll->fd);
squeeze_client_vect(poll->fd);
poll->fd = REM;
_remove_client = true;
}
_data = _data_vec[0];
size_t j;
_cgi_types.clear();
std::string str = _data->CGI_extensions;
while (_data->CGI_extensions != "")
{
j = str.find(" ");
_cgi_types.push_back(str.substr(0, j));
str.erase(0, j+1);
if (j == std::string::npos)
break;
}
return _end_connection;
}
std::map<std::string, std::string> Server::getCgiEnv(void)
{
std::map<std::string, std::string> env;
std::map<std::string, std::string>::iterator it;
for (it = _http_request.begin(); it != _http_request.end(); it++)
{
if (it->first == "Authorization")
env["AUTH_TYPE"] = it->second;
else if (it->first == "Content-Length")
env["CONTENT_LENGTH"] = it->second;
else if (it->first == "Content-Type")
env["CONTENT_TYPE"] = it->second;
else if (it->first == "Path")
{
(it->second.find("?") != std::string::npos) ? env["QUERY_STRING"] = it->second.substr(it->second.find("?") + 1) : env["QUERY_STRING"] = "";
env["SCRIPT_NAME"] = it->second.substr(0, it->second.find("?"));
// if (env["SCRIPT_NAME"].rfind("/") != std::string::npos)
// env["SCRIPT_NAME"] = env["SCRIPT_NAME"].substr(env["SCRIPT_NAME"].rfind("/"));
env["PATH_INFO"] = env["SCRIPT_NAME"];
}
else if (it->first == "Type")
env["REQUEST_METHOD"] = it->second;
else if (it->first == "Accept")
env["HTTP_ACCEPT"] = it->second;
else if (it->first == "Host")
env["HTTP_HOST"] = it->second;
else if (it->first == "User-Agent")
env["HTTP_USER_AGENT"] = it->second;
}
return (env);
}
static inline bool is_dir (const std::string& name) {
struct stat buffer;
return (stat(name.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode));
}
void Server::process_request(){
size_t pos;
_cgi_err = false;
std::string temp = _data->s_root();
if (*temp.rbegin() == '/' && *_http_request["Path"].begin() == '/')
temp = temp.substr(0, temp.length() - 1);
temp += _http_request["Path"];
if (is_dir(temp) && *temp.rbegin() != '/')
temp += "/";
else
temp = temp.substr(0, temp.find_last_of('/') + 1);
if(_http_request["Type"] == "GET")
{
if (_data->s_mpl()[temp] != "")
{
if ((pos = _data->s_mpl()[temp].find("GET")) != std::string::npos)
process_get_request();
else
{
_err_string = "405";
_http_request["Type"] = "";
process_get_request();
}
}
else if ((pos = _data->s_methods().find("GET")) != std::string::npos)
process_get_request();
else
{
_err_string = "405";
_http_request["Type"] = "";
process_get_request();
}
}
else if(_http_request["Type"] == "POST")
{
if (_data->s_mpl()[temp] != "")
{
if ((pos = _data->s_mpl()[temp].find("POST")) != std::string::npos)
process_post_request();
else
{
_err_string = "405";
process_get_request();
}
}
else if ((pos = _data->s_methods().find("POST")) != std::string::npos)
process_post_request();
else
{
_err_string = "405";
process_get_request();
}
}
else if(_http_request["Type"] == "DELETE")
{
if (_data->s_mpl()[temp] != "")
{
if ((pos = _data->s_mpl()[temp].find("DELETE")) != std::string::npos)
process_delete_request();
else
{
_err_string = "405";
process_get_request();
}
}
else if ((pos = _data->s_methods().find("DELETE")) != std::string::npos)
process_delete_request();
else
{
_err_string = "405";
process_get_request();
}
}
else if(_http_request["Type"] == "HEAD" || _http_request["Type"] == "PUT" || _http_request["Type"] == "CONNECT" || _http_request["Type"] == "TRACE" || _http_request["Type"] == "PATCH" || _http_request["Type"] == "OPTIONS")
{
_err_string = "501";
process_get_request();
}
else{
if((pos = _data->s_methods().find(_http_request["Type"])) == std::string::npos)
_err_string = "405";
else
_err_string = "400";
process_get_request();
}
}
int Server::send_response(struct pollfd *poll){
static bool status = true;
static std::string resp = _response["Header"] + _response["Date"] + _response["Server"] + _response["Content-Type"] + _response["Content-Length"] + _response["Connection"] + "\r\n" + _response["Body"];
if(status == false && (!_is_cgi || _cgi_err))
resp = _response["Header"] + _response["Date"] + _response["Server"] + _response["Content-Type"] + _response["Content-Length"] + _response["Connection"] + "\r\n" + _response["Body"];
else if (_is_cgi && !_cgi_err)
{
resp = _cgi_response;
_is_cgi = false;
}
int rsize = resp.length();
int ret = send(poll->fd, resp.c_str(), (BUFFER_SIZE < rsize ? BUFFER_SIZE : rsize), 0);
_http_request["Content-Type"] = "";
_http_request["Content-Disposition"] = "";
if(ret < 0)
return ret;
std::cout << "successfully sent " << ret << " bytes remaining: " << rsize - ret << std::endl;
resp.erase(0, ret);
status = true;
if((rsize - ret) <= 0){
status = false;
poll->events = POLLIN;
}
return ret;
}
int Server::recieve_data(struct pollfd *poll){
int ret = recv(poll->fd, _buffer, BUFFER_SIZE, 0);
if(ret < 0){
_end_connection = true;
std::cout << "error recv()" << std::endl;
return ret;
}
if(ret == 0){
std::cout << "client closed connection" << std::endl;
_end_connection = true;
return ret;
}
_buffer[ret] = '\0';
std::stringstream ss;
for (int i = 0; i < ret; i++)
ss << _buffer[i];
_storage = ss.str();
std::cout << "\n" << "=============== " << ret << " BYTES RECEIVED ===============\n";
std::cout << _buffer;
std::cout << "======================================================\n" << std::endl;
return ret;
}
void Server::squeeze_client_vect(int to_find)
{
for (std::vector<int>::iterator it = _clients.begin(); it != _clients.end() ; it++){
if (*it == to_find){
_clients.erase(it);
return ;
}
}
}