Something went wrong. Try again.
An HTTP server written in C++98 projects.intra.42.fr/projects/webserv
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156/* ************************************************************************** *//* *//* ::: :::::::: *//* utils.cpp :+: :+: :+: *//* +:+ +:+ +:+ *//* By: bcausseq <bcausseq@42angouleme.fr> +#+ +:+ +#+ *//* +#+#+#+#+#+ +#+ *//* 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 <fstream>#include <sstream>
std::stringstos(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);}
boolcheck_methods_local(const std::vector<std::string>& loc, const std::string& method){ for (auto it = loc.begin(); it != loc.end(); ++it) { if (!it->compare(method)) return (true); }
return (false);}
std::stringget_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::stringset_header_error(const std::string& version, std::string& code, std::string& errs){ return (std::string(version + " " + code + " " + errs + "\r\n\r\n"));}
std::stringget_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);}
voidsend_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) + "<h1>" + code + "</h1>"); } 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) + "<h1>" + code + "</h1>"); }}
methodsget_method(const std::string& meth_header){ if (!meth_header.compare("GET")) return (GET); else if (!meth_header.compare("DELETE")) return (DELETE); return (NOT_IMP);}