From 14e3171352cba6be63d68dd6ff258233ea349f3b Mon Sep 17 00:00:00 2001 From: LaurinUB Date: Thu, 31 Aug 2023 11:15:34 +0200 Subject: [PATCH 1/2] first test --- config/test.conf | 2 +- src/Server.cpp | 37 ++++++++++++++++++++----------------- src/Server.hpp | 3 +-- src/Socket.cpp | 4 ++-- src/Socket.hpp | 2 +- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/config/test.conf b/config/test.conf index 9f77412..721080c 100644 --- a/config/test.conf +++ b/config/test.conf @@ -12,7 +12,7 @@ http { } } server { - listen 8888; + listen 4444; server_name 0.0.0.0; index index.html; root www; diff --git a/src/Server.cpp b/src/Server.cpp index 78919bd..95230b2 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -45,21 +45,19 @@ void Server::run() { } } -int Server::startServer(int port) { +int Server::startServer(std::string ip, int port) { pollfd new_poll; - Socket serv(0); + Socket serv(this->numfds_); int opt = 1; socklen_t addrlen = sizeof(serv.getAddress()); - this->numfds_ = 1; - memset(pollfds_, -1, MAX_PORTS); new_poll.fd = socket(AF_INET, SOCK_STREAM, 0); serv.setState(SERVER); if (new_poll.fd < 0) { std::cout << "Error: can not start Socket." << std::endl; return EXIT_FAILURE; } - serv.setPort(port); + serv.setPort(ip, port); if (setsockopt(new_poll.fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) { std::cerr << "Error: cannot set socket opt." << std::endl; @@ -78,8 +76,9 @@ int Server::startServer(int port) { } new_poll.events = POLLIN; new_poll.revents = 0; - this->sockets_[0] = serv; - this->pollfds_[0] = new_poll; + this->sockets_[this->numfds_] = serv; + this->pollfds_[this->numfds_] = new_poll; + this->numfds_++; return EXIT_SUCCESS; } @@ -308,13 +307,6 @@ void Server::checkSocketTimeout() { //// Constructors and Operator overloads -Server::Server(const std::string& ip_addr, int port) : ip_addr_(ip_addr) { - if (startServer(port) == EXIT_FAILURE) { - std::cerr << "Error: Failed to start Server." << std::endl; - exit(EXIT_FAILURE); - } -} - Server::~Server() { exit(EXIT_SUCCESS); } Server::Server(const Server& obj) { *this = obj; } @@ -327,8 +319,19 @@ Server& Server::operator=(const Server& obj) { } Server::Server(const Settings& settings) : settings_(settings) { - if (startServer(this->settings_.getServers()[0].getPort()) != 0) { - std::cout << "Error: failed to start server with port: " - << this->settings_.getServers()[0].getPort() << std::endl; + memset(this->pollfds_, -1, MAX_PORTS); + // if (startServer(this->settings_.getServers()[0].getPort()) != 0) { + // std::cout << "Error: failed to start server with port: " + // << this->settings_.getServers()[0].getPort() << std::endl; + // } + std::cout << "severs: " << this->settings_.getServers().size() << std::endl; + for (size_t i = 0; i < this->settings_.getServers().size(); ++i) { + if (startServer(this->settings_.getServers()[i].getName(), + this->settings_.getServers()[i].getPort())) { + std::cout << "Error: failed to start Server with port " + << this->settings_.getServers()[i].getPort() << std::endl; + } else { + std::cout << "Startet server on " << this->settings_.getServers()[i].getName() << std::endl; + } } } diff --git a/src/Server.hpp b/src/Server.hpp index 748ecdf..e6b77f7 100644 --- a/src/Server.hpp +++ b/src/Server.hpp @@ -38,7 +38,6 @@ class Server { public: - Server(const std::string& ip_addr, int port); Server(const Settings& settings); ~Server(); Server(const Server& obj); @@ -53,7 +52,7 @@ class Server { Settings settings_; char* cgi_env_[8]; - int startServer(int port); + int startServer(std::string ip, int port); int pollError(int i); void removeFd(int i); size_t searchFreePoll(); diff --git a/src/Socket.cpp b/src/Socket.cpp index 5d33895..ddff5de 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -50,10 +50,10 @@ void Socket::setIndex(int i) { this->index_ = i; } void Socket::setKeepalive(bool state) { this->keepalive_ = state; } -void Socket::setPort(int port) { +void Socket::setPort(std::string ip, int port) { this->socketAddress_.sin_family = AF_INET; this->socketAddress_.sin_port = htons(port); - this->socketAddress_.sin_addr.s_addr = INADDR_ANY; + this->socketAddress_.sin_addr.s_addr = inet_addr(ip.c_str()); } void Socket::setState(sockState state) { this->state_ = state; } diff --git a/src/Socket.hpp b/src/Socket.hpp index 005afbd..56e28a4 100644 --- a/src/Socket.hpp +++ b/src/Socket.hpp @@ -38,7 +38,7 @@ class Socket { void setIndex(int i); void setKeepalive(bool state); - void setPort(int port); + void setPort(std::string ip, int port); void setState(sockState stat); void setRequest(HTTPRequest& req); bool hasUnfinishedRequest() const; From caf001cea7471d390ca510a1643a9c95172ef45b Mon Sep 17 00:00:00 2001 From: LaurinUB Date: Thu, 31 Aug 2023 18:52:51 +0200 Subject: [PATCH 2/2] add argv for config parsing, mutiple listen ports possible --- config/test.conf | 45 ++++++++++++++++++++++----------------------- src/Server.cpp | 22 +++++++++------------- src/Server.hpp | 2 +- src/main.cpp | 12 ++++++++++-- 4 files changed, 42 insertions(+), 39 deletions(-) diff --git a/config/test.conf b/config/test.conf index 721080c..dabb80c 100644 --- a/config/test.conf +++ b/config/test.conf @@ -1,26 +1,25 @@ http { - gobalsetting test; - server { - listen 8888; - server_name 0.0.0.0; - index index.html; - root www; - error_page ./data/404.html; - location /files { - root .; - auto_index on; - } - } - server { - listen 4444; - server_name 0.0.0.0; - index index.html; - root www; - error_page ./data/404.html; - location /files { - root .; - auto_index on; - } - } + server { + server_name 0.0.0.0; + port 4444; + 404 ./data/404.html; + client_max_body_size 100000; + location { + root ./www; + auto-index true; + allow-method get; + } + } + server { + server_name 0.0.0.0; + port 8888; + 404 ./data/404.html; + client_max_body_size 100000; + location { + root ./www; + auto-index true; + allow-method get; + } + } } diff --git a/src/Server.cpp b/src/Server.cpp index 95230b2..bec0e43 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -8,9 +8,6 @@ extern sig_atomic_t g_signaled; // Core Functions void Server::run() { - std::cout << "\n*** Listening on ADDRESS: " - << this->sockets_[0].getAddressString() - << " PORT: " << this->sockets_[0].getPort() << " ***\n\n"; while (g_signaled == 0) { if (poll(this->pollfds_, this->numfds_, TIMEOUT) == -1) { perror("poll"); @@ -23,7 +20,7 @@ void Server::run() { } if (this->pollfds_[i].revents & POLLIN) { if (this->sockets_[i].getState() == SERVER) { - newConnection(); + newConnection(i); break; } else { handleReceive(i); @@ -224,7 +221,7 @@ void Server::sendResponse(int i) { << bytes_sent << std::endl; } -void Server::newConnection() { +void Server::newConnection(int i) { if (this->numfds_ > MAX_PORTS) { std::cout << "Error: no new Connection possible." << std::endl; return; @@ -233,13 +230,15 @@ void Server::newConnection() { socklen_t addrlen = sizeof(struct sockaddr); pollfd new_poll; size_t index = searchFreePoll(); - new_poll.fd = accept(this->pollfds_[0].fd, + new_poll.fd = accept(this->pollfds_[i].fd, (struct sockaddr*)&new_client.getAddress(), &addrlen); if (new_poll.fd < 0) { std::cout << "Error: Failed to accept connection." << std::endl; + return; } if (fcntl(new_poll.fd, F_SETFL, O_NONBLOCK, FD_CLOEXEC) == -1) { std::cerr << "Error: fcntl." << std::endl; + return; } new_poll.events = POLLIN; new_poll.revents = 0; @@ -248,7 +247,7 @@ void Server::newConnection() { this->sockets_[index] = new_client; this->pollfds_[index] = new_poll; if (index == this->numfds_) { - numfds_++; + this->numfds_++; } std::cout << "New connection success on : " << new_client.getAddressString() << " with socket nbr: " << new_poll.fd << std::endl; @@ -320,18 +319,15 @@ Server& Server::operator=(const Server& obj) { Server::Server(const Settings& settings) : settings_(settings) { memset(this->pollfds_, -1, MAX_PORTS); - // if (startServer(this->settings_.getServers()[0].getPort()) != 0) { - // std::cout << "Error: failed to start server with port: " - // << this->settings_.getServers()[0].getPort() << std::endl; - // } - std::cout << "severs: " << this->settings_.getServers().size() << std::endl; for (size_t i = 0; i < this->settings_.getServers().size(); ++i) { if (startServer(this->settings_.getServers()[i].getName(), this->settings_.getServers()[i].getPort())) { std::cout << "Error: failed to start Server with port " << this->settings_.getServers()[i].getPort() << std::endl; } else { - std::cout << "Startet server on " << this->settings_.getServers()[i].getName() << std::endl; + std::cout << "Startet server on " + << this->settings_.getServers()[i].getName() << " with Port " + << this->settings_.getServers()[i].getPort() << std::endl; } } } diff --git a/src/Server.hpp b/src/Server.hpp index e6b77f7..b2a18d1 100644 --- a/src/Server.hpp +++ b/src/Server.hpp @@ -57,7 +57,7 @@ class Server { void removeFd(int i); size_t searchFreePoll(); void sendResponse(int i); - void newConnection(); + void newConnection(int i); void handleReceive(int i); void handleSend(int i); void checkSocketTimeout(); diff --git a/src/main.cpp b/src/main.cpp index ae2694f..f3dbb15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,9 +8,17 @@ void handleSIGINT(int param) { _exit(SIGINT); } -int main() { +int main(int argc, char* argv[]) { signal(SIGINT, handleSIGINT); - std::string conf_path("./config/default.conf"); + std::string conf_path; + if (argc == 1) { + conf_path = "./config/default.conf"; + } else if (argc == 2) { + conf_path = argv[1]; + } else { + std::cerr << "Error: wrong amount of arguments" << std::endl; + return EXIT_FAILURE; + } try { Settings settings(conf_path); Server server = Server(settings);