/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* utils.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bcausseq +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/09/27 22:52:42 by bcausseq #+# #+# */ /* Updated: 2026/09/27 22:57:31 by bcausseq ### ########.fr */ /* */ /* ************************************************************************** */ #include "Header.hpp" #include "server/Client.hpp" #include "utils.hpp" #include #include std::string stos(const short& status) { std::stringstream ss; ss << status; std::string ret = ss.str(); return (ret); } const Location *find_location(const std::string& path, Config const& s, size_t& len) { const Location *loc = NULL; for (auto it = s.get_location().begin(); it != s.get_location().end(); ++it) { if (!path.find(it->get_path()) && it->get_path().length() > len) { len = it->get_path().length(); loc = (&(*it)); } } return (loc); } bool check_methods_local(const std::vector& loc, const std::string& method) { for (auto it = loc.begin(); it != loc.end(); ++it) { if (!it->compare(method)) return (true); } return (false); } std::string get_abs_path(Client& c, const std::string& path, const std::string& method) { Config const& config = c.get_config(); std::string ret; std::string root; size_t len = 0; size_t pos = 0; const Location *loc = NULL; loc = find_location(path, config, len); if (loc) { if (check_methods_local(loc->get_methods(), method)) root = loc->get_root().empty() ? config.get_root() : loc->get_root(); else return (""); } else root = config.get_root(); if (root[root.length() - 1] != '/' && path.substr(len)[0] != '/') ret = root + "/" + path.substr(len); else ret = root + path.substr(len); while ((pos = ret.find("//", pos)) != std::string::npos) ret.replace(pos, 2, "/"); return (ret); } std::string set_header_error(const std::string& version, std::string& code, std::string& errs) { return (std::string(version + " " + code + " " + errs + "\r\n\r\n")); } std::string get_default_message(const short& status, Config const& config) { auto it = config.get_error_pages().find(status); if (it == config.get_error_pages().end()) return (std::string()); return (it->second); } void send_error_status(Header& h, Client& c, const short status) { Config const& config = c.get_config(); size_t len = 0; std::string path; std::string errs = get_default_message(status, config); std::string code = stos(status); const Location *loc = find_location(h.get_path(), config, len); if (loc) path = loc->get_root().empty() ? config.get_root() : loc->get_root(); else path = config.get_root(); auto it = config.get_error_pages().find(status); if (it == config.get_error_pages().end()) { c.set_append_out_buffer(set_header_error(h.get_version(), code, errs) + "

" + code + "

"); } else { std::string err = path + it->second; while ((len = err.find("//", len)) != std::string::npos) err.replace(len, 2, "/"); std::ifstream lalala(err.c_str()); if (lalala.is_open()) { c.set_append_out_buffer(set_header_error(h.get_version(), code, errs)); std::string line; while (std::getline(lalala, line)) c.set_append_out_buffer(line); } else c.set_append_out_buffer(set_header_error(h.get_version(), code, errs) + "

" + code + "

"); } } methods get_method(const std::string& meth_header) { if (!meth_header.compare("GET")) return (GET); else if (!meth_header.compare("DELETE")) return (DELETE); return (NOT_IMP); }