forked from MT-jlem/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
106 lines (96 loc) · 2.91 KB
/
server.cpp
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
#include "server.hpp"
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <netdb.h>
#include <sys/errno.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <system_error>
#include <unistd.h>
Location::Location()
{
path = "";
root = "";
index = "";
autoIndex = false;
trackAutoIndex = false;
methods[0] = 0;
methods[1] = 0;
methods[2] = 0;
redir = std::make_pair("", "");
upload = "";
}
Location::~Location(){}
Server::Server(){
_listen = std::vector<std::pair<std::string, std::string> >();
root = "";
index = "";
serverName = "";
maxBodySize = -1;
errorPage = std::map<std::string, std::string>();
memset(&hints, 0, sizeof(hints));
}
Server::~Server(){}
int Server::createServer(std::vector<std::string> &created){
hints.ai_socktype = SOCK_STREAM;
for (size_t i = 0; i < _listen.size(); ++i){
std::string host = _listen[i].first + ":";
host += _listen[i].second;
if (std::find(created.begin(), created.end(), host) == created.end()){
int rv = getaddrinfo(_listen[i].first.c_str(), _listen[i].second.c_str(), &hints, &servInfo);
std::cout << "go to http://" << _listen[i].first.c_str() << ":" << _listen[i].second.c_str() << '\n';
if (rv){
std::cerr << gai_strerror(rv) << '\n';
return -1;
}
int sock;
sock = socket(servInfo->ai_family, servInfo->ai_socktype, servInfo->ai_protocol);
if (sock < 0){
std::cerr << "socket() failed\n";
std::strerror(errno);
return -1;
}
int ov = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &ov, sizeof(int))){
std::strerror(errno);
std::cerr << "setsockopt() failed\n";
close(sock);
return -1;
}
if (bind(sock, servInfo->ai_addr, servInfo->ai_addrlen) == -1){
std::cerr << "bind() failed\n";
std::strerror(errno);
close(sock);
return -1;
}
if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1){
std::strerror(errno);
std::cerr << "fcntl() failed\n";
close(sock);
return -1;
}
if (listen(sock, SOMAXCONN)){
std::strerror(errno);
std::cerr << "listen() failed\n";
close(sock);
return -1;
}
fd.push_back(sock);
pollfd tmp;
tmp.fd = sock;
tmp.events = POLLIN;
fds.push_back(tmp);
freeaddrinfo(servInfo);
created.push_back(host);
}
}
return 0;
}
void Server::closeFd(){
for (size_t i = 0; i < fd.size(); ++i){
close(fd[i]);
}
}