mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
Sort
This commit is contained in:
162
src/Server/Server.cpp
Normal file
162
src/Server/Server.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
#include "Server.hpp"
|
||||
|
||||
//----------------------------------------------Constructors-----------------------------------------------------------------------------------
|
||||
Server::Server()
|
||||
{
|
||||
_client = 0;
|
||||
|
||||
}
|
||||
|
||||
Server::Server(std::string path)
|
||||
{
|
||||
(void)path;
|
||||
_client = 0;
|
||||
}
|
||||
|
||||
//----------------------------------------------Send--------------------------------------------------------------------------------------------
|
||||
|
||||
//----------------------------------------------Configuration-----------------------------------------------------------------------------------
|
||||
void Server::readConfig(void)
|
||||
{
|
||||
TOMLMap *root = parse();
|
||||
|
||||
|
||||
TOMLMap *map;
|
||||
TOMLMap::iterator it1;
|
||||
TOMLMapArray *arr;
|
||||
TOMLMapArray::iterator it;
|
||||
|
||||
|
||||
arr = root->find("server")->second->getMapArray();
|
||||
it = arr->begin();
|
||||
|
||||
while (it != arr->end())
|
||||
{
|
||||
std::cout << BLUE << *it << std::endl;
|
||||
map = *it;
|
||||
|
||||
it1 = map->begin();
|
||||
while (it1 != map->end())
|
||||
{
|
||||
std::cout << TURGUOISE << it1->first << it1->second << ZERO_C << std::endl;
|
||||
++it1;
|
||||
}
|
||||
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Server::setupConfig(void)
|
||||
{
|
||||
this->_ip = "127.0.0.1";
|
||||
this->_port = 8080;
|
||||
}
|
||||
|
||||
|
||||
void Server::setNonblocking(int fd)
|
||||
{
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
|
||||
void Server::newConnection(int fd)
|
||||
{
|
||||
struct epoll_event ev;
|
||||
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
|
||||
ev.data.fd = fd;
|
||||
|
||||
epoll_ctl(_epolfd, EPOLL_CTL_ADD, fd, &ev);
|
||||
_client++;
|
||||
}
|
||||
|
||||
void Server::start(void)
|
||||
{
|
||||
Socket serverSocket(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1");
|
||||
char buff[BUFFSIZE + 1] = {0};
|
||||
Header header;
|
||||
int fd_accept;
|
||||
int code;
|
||||
|
||||
checkError(serverSocket.init(MAX_CLIENT), "Socket init");
|
||||
fd_accept = accept(serverSocket.getSocketFd(),
|
||||
serverSocket.getSockaddr(), serverSocket.getSocklen());
|
||||
checkError(fd_accept, "Initialize client socket");
|
||||
checkError(recv(fd_accept, buff, BUFFSIZE, 0), "Receive msg from client");
|
||||
std::cout << TURGUOISE << "Receive Header" << ZERO_C << std::endl;
|
||||
header.setRawData(buff);
|
||||
code = header.parseRequest();
|
||||
header.printHeaderInfo();
|
||||
header.sendRespons(fd_accept);
|
||||
std::cout << BLUE << header.getReasonPhrase(code) << ZERO_C << std::endl;
|
||||
close(fd_accept);
|
||||
close(serverSocket.getSocketFd());
|
||||
//-----------------------------------------------попытка добавить epoll------------------
|
||||
// Socket serverSocket(AF_INET, SOCK_STREAM, 0, _port);
|
||||
// char buff[BUFFSIZE + 1] = {0};
|
||||
// Header header[MAX_CLIENT];
|
||||
// int fd;
|
||||
// int n;
|
||||
// int nfds;
|
||||
// int _clientSocket;
|
||||
|
||||
// nfds = 0;
|
||||
// n = 0;
|
||||
// _epolfd = epoll_create1(0);
|
||||
// checkError(serverSocket.init(MAX_CLIENT), "Socket init");
|
||||
// setNonblocking(serverSocket.getSocketFd());
|
||||
// setNonblocking(_epolfd);
|
||||
// while (true)
|
||||
// {
|
||||
// _clientSocket = accept(serverSocket.getSocketFd(),
|
||||
// serverSocket.getSockaddr(), serverSocket.getSocklen());
|
||||
// if (_clientSocket > 0)
|
||||
// newConnection(_clientSocket);
|
||||
// if (_client > 0)
|
||||
// nfds = epoll_wait(_epolfd, _events, MAX_CLIENT, -1);
|
||||
// while (n < nfds)
|
||||
// {
|
||||
// fd = _events[n].data.fd;
|
||||
// // std::cout << "n = " << n << " nfds = " << nfds << " fd = " << fd << " _clientSize = " << _client << " _clientSocket " << _clientSocket << std::endl;
|
||||
// checkError(recv(fd, buff, BUFFSIZE, 0), "Receive msg from client");
|
||||
// header[fd].setRequest(buff);
|
||||
// header[fd].parseRequest();
|
||||
// header[fd].printHeaderInfo();
|
||||
// sendRespons(header[fd], fd);
|
||||
// header[fd].clearHeader();
|
||||
// close(fd);
|
||||
// _client--;
|
||||
// n++;
|
||||
// }
|
||||
// n = 0;
|
||||
// nfds = 0;
|
||||
// _clientSocket = 0;
|
||||
// }
|
||||
// close(serverSocket.getSocketFd());
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Server::end(void)
|
||||
{
|
||||
|
||||
}
|
||||
//----------------------------------------------Other------------------------------------------------------------------------------------------------
|
||||
void Server::checkError(int fd, std::string str)
|
||||
{
|
||||
if (fd < 0)
|
||||
{
|
||||
std::cout << RED << "Server ERROR: " << str << ZERO_C << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
std::cout << GREEN << "Server SUCCESS: " << str << ZERO_C << std::endl;
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
{
|
||||
}
|
||||
|
||||
42
src/Server/Server.hpp
Normal file
42
src/Server/Server.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef SERVER_HPP
|
||||
#define SERVER_HPP
|
||||
|
||||
#include "webserv.hpp"
|
||||
#include "Header.hpp"
|
||||
#include "ServerConfig.hpp"
|
||||
#include "Socket.hpp"
|
||||
#include "parse.hpp"
|
||||
|
||||
class Header;
|
||||
|
||||
class Server
|
||||
{
|
||||
private:
|
||||
int _port;
|
||||
int _epolfd;
|
||||
int _client;
|
||||
struct epoll_event _events[MAX_CLIENT];
|
||||
struct sockaddr_in _addres;
|
||||
std::string _ip;
|
||||
std::vector<ServerConfig> _configs;
|
||||
|
||||
private:
|
||||
void checkError(int fd, std::string str);
|
||||
void sendFile(std::string str);
|
||||
void sendHeader(Header head, int);
|
||||
void sendRespons(Header head, int);
|
||||
void setNonblocking(int fd);
|
||||
void newConnection(int fd);
|
||||
public:
|
||||
Server();
|
||||
Server(std::string path);
|
||||
|
||||
void readConfig(void);
|
||||
void setupConfig(void);
|
||||
void start(void);
|
||||
void end(void);
|
||||
~Server();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
100
src/Server/ServerConfig.cpp
Normal file
100
src/Server/ServerConfig.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "ServerConfig.hpp"
|
||||
|
||||
ServerConfig::ServerConfig()
|
||||
{
|
||||
}
|
||||
|
||||
// ServerConfig::ServerConfig(TOMLMap *map)
|
||||
// {
|
||||
// _root = map;
|
||||
// }
|
||||
|
||||
//--------------------------------------------------GET/SET---------------------------------------
|
||||
std::string ServerConfig::getServerName(void)
|
||||
{
|
||||
return (_serverName);
|
||||
}
|
||||
|
||||
std::string ServerConfig::getHost(void)
|
||||
{
|
||||
return (_host);
|
||||
}
|
||||
|
||||
int ServerConfig::getPort(void)
|
||||
{
|
||||
return (_port);
|
||||
}
|
||||
|
||||
int ServerConfig::getClientBodySize(void)
|
||||
{
|
||||
return (_clientBodySize);
|
||||
}
|
||||
|
||||
std::vector<location> ServerConfig::getLocations(void)
|
||||
{
|
||||
return (_locations);
|
||||
}
|
||||
|
||||
std::map<int, std::string> ServerConfig::getErrorPages(void)
|
||||
{
|
||||
return (_errorPages);
|
||||
}
|
||||
|
||||
// TOMLMap ServerConfig::*getRoot(void)
|
||||
// {
|
||||
// return (this->_root);
|
||||
// }
|
||||
|
||||
void ServerConfig::setServerName(std::string name)
|
||||
{
|
||||
_serverName = name;
|
||||
}
|
||||
|
||||
void ServerConfig::setHost(std::string host)
|
||||
{
|
||||
_host = host;
|
||||
}
|
||||
|
||||
void ServerConfig::setPort(int port)
|
||||
{
|
||||
_port = port;
|
||||
}
|
||||
|
||||
void ServerConfig::setClientBodySize(int body)
|
||||
{
|
||||
_clientBodySize = body;
|
||||
}
|
||||
|
||||
void ServerConfig::setErrorPages(std::map<int, std::string> pages)
|
||||
{
|
||||
_errorPages = pages;
|
||||
}
|
||||
|
||||
void ServerConfig::setLocations(std::vector<location> locations)
|
||||
{
|
||||
_locations = locations;
|
||||
}
|
||||
|
||||
// void ServerConfig::setRoot(TOMLMap * data)
|
||||
// {
|
||||
// _root = data;
|
||||
// }
|
||||
|
||||
|
||||
void ServerConfig::fillFields(void)
|
||||
{
|
||||
// TOMLMap *tmp = _root;
|
||||
// TOMLMap::iterator it;
|
||||
|
||||
// it = tmp->begin();
|
||||
|
||||
// while (it != tmp->end())
|
||||
// {
|
||||
// std::cout << it->first << std::endl;
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
ServerConfig::~ServerConfig()
|
||||
{
|
||||
}
|
||||
59
src/Server/ServerConfig.hpp
Normal file
59
src/Server/ServerConfig.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#ifndef SERVERCONFIG_HPP
|
||||
#define SERVERCONFIG_HPP
|
||||
|
||||
#include "webserv.hpp"
|
||||
|
||||
struct location
|
||||
{
|
||||
std::string location;
|
||||
std::string root;
|
||||
std::string directoryFile;
|
||||
std::string uploadDir;
|
||||
bool autoindex;
|
||||
bool uploadAccept;
|
||||
std::vector<std::string> methods;
|
||||
std::map<int, std::string> redirect;
|
||||
};
|
||||
|
||||
class ServerConfig
|
||||
{
|
||||
private:
|
||||
// TOMLMap *_root;
|
||||
std::string _serverName;
|
||||
std::string _host;
|
||||
int _port;
|
||||
int _clientBodySize;
|
||||
|
||||
std::map<int, std::string> _errorPages;
|
||||
std::vector<location> _locations;
|
||||
|
||||
public:
|
||||
void setServerName(std::string);
|
||||
void setHost(std::string);
|
||||
void setPort(int);
|
||||
void setClientBodySize(int);
|
||||
void setErrorPages(std::map<int, std::string>);
|
||||
void setLocations(std::vector<location>);
|
||||
// void setRoot(TOMLMap *);
|
||||
|
||||
std::string getServerName(void);
|
||||
std::string getHost(void);
|
||||
int getPort(void);
|
||||
int getClientBodySize(void);
|
||||
std::vector<location> getLocations(void);
|
||||
std::map<int, std::string> getErrorPages(void);
|
||||
// TOMLMap *getRoot(void);
|
||||
|
||||
public:
|
||||
ServerConfig();
|
||||
// ServerConfig(TOMLMap *root);
|
||||
|
||||
void fillFields(void);
|
||||
|
||||
~ServerConfig();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
101
src/Server/Socket.cpp
Normal file
101
src/Server/Socket.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
#include "Socket.hpp"
|
||||
|
||||
Socket::Socket()
|
||||
{
|
||||
|
||||
}
|
||||
Socket::Socket(int domain, int type, int protocol, int port, std::string ip)
|
||||
{
|
||||
int opt = 1;
|
||||
_socketFd = socket(domain, type, protocol);
|
||||
checkError(_socketFd, "Initialize Server socket");
|
||||
checkError(setsockopt(_socketFd, SOL_SOCKET, SO_REUSEADDR
|
||||
| SO_REUSEPORT, &opt, sizeof(opt)), "Set socket options");
|
||||
_addres.sin_family = domain;
|
||||
_addres.sin_port = htons(port);
|
||||
_addres.sin_addr.s_addr = inet_addr(ip.c_str());
|
||||
_addrlen = sizeof(_addres);
|
||||
}
|
||||
|
||||
Socket::~Socket()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------GET/SET----------------------------
|
||||
int Socket::getSocketFd(void)
|
||||
{
|
||||
return _socketFd;
|
||||
}
|
||||
|
||||
struct sockaddr_in Socket::getAddres(void)
|
||||
{
|
||||
return _addres;
|
||||
}
|
||||
|
||||
struct sockaddr *Socket::getSockaddr(void)
|
||||
{
|
||||
return((struct sockaddr *)&_addres);
|
||||
}
|
||||
|
||||
socklen_t *Socket::getSocklen(void)
|
||||
{
|
||||
return((socklen_t *)&_addrlen);
|
||||
}
|
||||
|
||||
|
||||
void Socket::setAddres(struct sockaddr_in addr)
|
||||
{
|
||||
_addres = addr;
|
||||
_addrlen = sizeof(_addres);
|
||||
}
|
||||
|
||||
socklen_t Socket::getAddlen(void)
|
||||
{
|
||||
return (_addrlen);
|
||||
}
|
||||
|
||||
void Socket::setSocketFd(int fd)
|
||||
{
|
||||
_socketFd = fd;
|
||||
}
|
||||
//----------------------------------------------------------------Setting-------------------------------
|
||||
int Socket::bindingSocket(void)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = bind(_socketFd, (struct sockaddr *)&_addres, sizeof(_addres));
|
||||
checkError(res, "Bind Socket");
|
||||
return (res);
|
||||
}
|
||||
|
||||
int Socket::listeningSocket(int nbr)
|
||||
{
|
||||
int res;
|
||||
|
||||
res = listen(_socketFd, nbr);
|
||||
checkError(res, "Listen Socket");
|
||||
return (res);
|
||||
}
|
||||
|
||||
int Socket::init(int nbr)
|
||||
{
|
||||
if (bindingSocket() < 0)
|
||||
return (-1);
|
||||
if (listeningSocket(nbr) < 0)
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------ERROR----------------------------------
|
||||
|
||||
void Socket::checkError(int fd, std::string str)
|
||||
{
|
||||
if (fd < 0)
|
||||
{
|
||||
std::cout << RED << "Socket ERROR: " << str << ZERO_C << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
std::cout << GREEN << "Socket SUCCESS: " << str << ZERO_C << std::endl;
|
||||
}
|
||||
33
src/Server/Socket.hpp
Normal file
33
src/Server/Socket.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef SOCKET_HPP
|
||||
#define SOCKET_HPP
|
||||
|
||||
#include "webserv.hpp"
|
||||
class Socket
|
||||
{
|
||||
private:
|
||||
int _socketFd;
|
||||
socklen_t _addrlen;
|
||||
struct sockaddr_in _addres;
|
||||
|
||||
|
||||
public:
|
||||
Socket();
|
||||
Socket(int domain, int type, int protocol, int port, std::string ip);
|
||||
int getSocketFd(void);
|
||||
struct sockaddr_in getAddres(void);
|
||||
socklen_t getAddlen(void);
|
||||
struct sockaddr *getSockaddr(void);
|
||||
socklen_t *getSocklen(void);
|
||||
void setSocketFd(int);
|
||||
void setAddres(struct sockaddr_in);
|
||||
int bindingSocket(void);
|
||||
int listeningSocket(int nbr);
|
||||
int init(int nbr);
|
||||
void checkError(int fd, std::string str);
|
||||
|
||||
~Socket();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user