diff --git a/Makefile b/Makefile index 9c33a69..a7a4fb9 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ CLANG_STDLIB ?= -stdlib=libc++ SRCS := src/Log.cpp src/main.cpp src/Server.cpp \ src/config/Config.cpp src/config/ConfigurationParser.cpp src/config/LocationConfig.cpp src/config/ServerConfig.cpp \ src/event/Epoll.cpp src/event/EventToken.cpp \ - src/net/ByteBuffer.cpp src/net/Connection.cpp src/net/FileDescriptor.cpp src/net/Listener.cpp src/net/PauseMask.cpp src/net/SlotPool.cpp + src/net/ByteBuffer.cpp src/net/Connection.cpp src/net/FileDescriptor.cpp src/net/Listener.cpp src/net/ListenerPlan.cpp src/net/PauseMask.cpp src/net/SlotPool.cpp # src/http/HttpStatus.cpp \ @@ -68,7 +68,7 @@ DEPS := $(OBJS:.o=.d) HDRS := include/config/Config.hpp include/config/ConfigurationParser.hpp include/config/LocationConfig.hpp include/config/ServerConfig.hpp \ include/event/Epoll.hpp include/event/EventToken.hpp \ include/http/HttpStatus.hpp \ - include/net/ByteBuffer.hpp include/net/Connection.hpp include/net/FileDescriptor.hpp include/net/Listener.hpp include/net/PauseMask.hpp include/net/SlotPool.hpp \ + include/net/ByteBuffer.hpp include/net/Connection.hpp include/net/FileDescriptor.hpp include/net/Listener.hpp include/net/ListenerPlan.hpp include/net/PauseMask.hpp include/net/SlotPool.hpp \ include/Log.hpp include/Result.hpp include/Server.hpp \ diff --git a/include/config/Config.hpp b/include/config/Config.hpp index 3eee84f..542fdbd 100644 --- a/include/config/Config.hpp +++ b/include/config/Config.hpp @@ -1,6 +1,6 @@ /*********************************/ /* */ -/* o.riabenkyi@gmain.com */ +/* o.riabenkyi@gmail.com */ /* */ /*********************************/ diff --git a/include/config/ConfigurationParser.hpp b/include/config/ConfigurationParser.hpp index 032b6c3..c049b13 100644 --- a/include/config/ConfigurationParser.hpp +++ b/include/config/ConfigurationParser.hpp @@ -1,6 +1,6 @@ /*********************************/ /* */ -/* o.riabenkyi@gmain.com */ +/* o.riabenkyi@gmail.com */ /* */ /*********************************/ diff --git a/include/config/LocationConfig.hpp b/include/config/LocationConfig.hpp index 5a27c65..78533e7 100644 --- a/include/config/LocationConfig.hpp +++ b/include/config/LocationConfig.hpp @@ -1,6 +1,6 @@ /*********************************/ /* */ -/* o.riabenkyi@gmain.com */ +/* o.riabenkyi@gmail.com */ /* */ /*********************************/ diff --git a/include/config/ServerConfig.hpp b/include/config/ServerConfig.hpp index 7d21cb0..1173842 100644 --- a/include/config/ServerConfig.hpp +++ b/include/config/ServerConfig.hpp @@ -1,6 +1,6 @@ /*********************************/ /* */ -/* o.riabenkyi@gmain.com */ +/* o.riabenkyi@gmail.com */ /* */ /*********************************/ diff --git a/include/net/ListenerPlan.hpp b/include/net/ListenerPlan.hpp new file mode 100644 index 0000000..3d3f330 --- /dev/null +++ b/include/net/ListenerPlan.hpp @@ -0,0 +1,60 @@ +/*********************************/ +/* */ +/* o.riabenkyi@gmail.com */ +/* */ +/*********************************/ + +#ifndef LISTENERPLAN_HPP +#define LISTENERPLAN_HPP + +#include + +#include +#include +#include + +#include "config/Config.hpp" + +namespace webserv { + +// Turns a validated Config into the deduplicated set of sockets the server +// must bind. Two server blocks may list the same host:port to share one +// socket (virtual hosting, picked apart later by server_name), so this +// groups listen directives by unique host:port instead of binding one +// socket per directive, which would make the second bind() fail. +// +// The ServerConfig pointers in routes() point into the Config passed to +// build(); that Config must outlive the ListenerPlan. +class ListenerPlan final { + public: + class Error : public std::exception { + public: + explicit Error(std::string message); + const char* what() const noexcept override; + + private: + std::string msg_; + }; + + static ListenerPlan build(const Config& config); + + const std::vector& endpoints() const noexcept { + return endpoints_; + } + + // routes()[i] lists, in config order, every ServerConfig reachable + // through endpoints()[i]. Not consumed yet: it becomes the input to + // Host-header based virtual host selection once HTTP request routing + // exists. + const std::vector>& routes() const noexcept { + return routes_; + } + + private: + std::vector endpoints_; + std::vector> routes_; +}; + +} // namespace webserv + +#endif diff --git a/src/main.cpp b/src/main.cpp index d7ff51f..c3d612b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,12 +10,8 @@ /* */ /* ************************************************************************** */ -#include - -#include #include #include -#include #include #include #include @@ -25,26 +21,10 @@ #include "Server.hpp" #include "config/ConfigurationParser.hpp" +#include "net/ListenerPlan.hpp" constexpr std::string_view DEFAULT_CONFIG_PATH = "conf/_default.conf"; -namespace { - -// TEMP: Builds wildcard IPv4 addresses for the fixed echo server ports. -// Parsed configuration will eventually provide resolved listener addresses. -sockaddr_storage anyAddress(std::uint16_t port) { - const sockaddr_in address{.sin_family = AF_INET, - .sin_port = ::htons(port), - .sin_addr = {.s_addr = ::htonl(INADDR_ANY)}, - .sin_zero = {}}; - - sockaddr_storage storage{}; - std::memcpy(&storage, &address, sizeof(address)); - return storage; -} - -} // namespace - int main(int argc, char** argv) { if (argc > 2) { std::cerr << "usage: " << argv[0] << " [configuration file]\n"; @@ -56,25 +36,29 @@ int main(int argc, char** argv) { webserv::log::info("configuration file {}", configPath); - try { - ConfigurationParser parser; - Config config = parser.parse(std::string(configPath)); + // Declared here, outside the parsing try block, because ListenerPlan + // keeps pointers into this Config's ServerConfig blocks and both the + // plan and the running server need it to stay alive past parsing. + Config config; + try { + ConfigurationParser parser; + config = parser.parse(std::string(configPath)); - const std::vector &servers = config.getServers(); - std::cout << "Parsed " << servers.size() << " server block(s) from " << configPath << ":\n"; - for (size_t i = 0; i < servers.size(); ++i) { - const ServerConfig &server = servers[i]; - std::cout << " server " << i << ":"; - const std::vector &listens = server.getListens(); - for (size_t l = 0; l < listens.size(); ++l) - std::cout << ' ' << listens[l].host << ':' << listens[l].port; - std::cout << " root=" << server.getRoot().value_or("(inherited per-location)") - << " locations=" << server.getLocations().size() << '\n'; - } - } catch (const std::exception &e) { - std::cerr << "Configuration error: " << e.what() << '\n'; - return 1; + const std::vector &servers = config.getServers(); + std::cout << "Parsed " << servers.size() << " server block(s) from " << configPath << ":\n"; + for (size_t i = 0; i < servers.size(); ++i) { + const ServerConfig &server = servers[i]; + std::cout << " server " << i << ":"; + const std::vector &listens = server.getListens(); + for (size_t l = 0; l < listens.size(); ++l) + std::cout << ' ' << listens[l].host << ':' << listens[l].port; + std::cout << " root=" << server.getRoot().value_or("(inherited per-location)") + << " locations=" << server.getLocations().size() << '\n'; } + } catch (const std::exception &e) { + std::cerr << "Configuration error: " << e.what() << '\n'; + return 1; + } // Ignore SIGPIPE so writing to a closed socket or pipe reports an error // instead of terminating the server. Other clients can then continue. @@ -84,11 +68,8 @@ int main(int argc, char** argv) { } try { - // TEMP: The echo server listens on three fixed ports until main obtains - // the listener addresses from the configuration file. - const std::array endpoints{ - anyAddress(8000), anyAddress(8001), anyAddress(8002)}; - webserv::Server server(endpoints); + const webserv::ListenerPlan plan = webserv::ListenerPlan::build(config); + webserv::Server server(plan.endpoints()); server.run(); } catch (const std::exception& error) { webserv::log::error("{}", error.what()); diff --git a/src/net/ListenerPlan.cpp b/src/net/ListenerPlan.cpp new file mode 100644 index 0000000..c80eea5 --- /dev/null +++ b/src/net/ListenerPlan.cpp @@ -0,0 +1,74 @@ +#include "net/ListenerPlan.hpp" + +#include +#include + +#include +#include +#include +#include + +namespace webserv { + +namespace { + +// The parser only ever produces dotted IPv4 strings ("127.0.0.1", or +// "0.0.0.0" when the config omits a host), so IPv4 is all that needs +// resolving here. +sockaddr_storage makeIPv4Endpoint(const std::string& host, int port) { + sockaddr_in address{}; + address.sin_family = AF_INET; + address.sin_port = ::htons(static_cast(port)); + + if (::inet_pton(AF_INET, host.c_str(), &address.sin_addr) != 1) { + throw ListenerPlan::Error(std::format("invalid listen address '{}'", host)); + } + + sockaddr_storage storage{}; + std::memcpy(&storage, &address, sizeof(address)); + return storage; +} + +} // namespace + +ListenerPlan::Error::Error(std::string message) : msg_(std::move(message)) {} + +const char* ListenerPlan::Error::what() const noexcept { return msg_.c_str(); } + +ListenerPlan ListenerPlan::build(const Config& config) { + ListenerPlan plan; + // Local key list mirroring plan.endpoints_/routes_ by index, used only to + // find which group an already-seen host:port belongs to. + std::vector keys; + + for (const ServerConfig& server : config.getServers()) { + for (const ServerConfig::Listen& listen : server.getListens()) { + std::size_t index = keys.size(); + bool found = false; + for (std::size_t i = 0; i < keys.size(); ++i) { + if (keys[i].host == listen.host && keys[i].port == listen.port) { + index = i; + found = true; + break; + } + } + + if (!found) { + keys.push_back(listen); + plan.endpoints_.push_back(makeIPv4Endpoint(listen.host, listen.port)); + plan.routes_.emplace_back(); + } + + // A server block that repeats the same "listen" twice must not be + // added to its own route group twice. + std::vector& route = plan.routes_[index]; + if (std::ranges::find(route, &server) == route.end()) { + route.push_back(&server); + } + } + } + + return plan; +} + +} // namespace webserv