Something went wrong. Try again.
An HTTP server written in C++98 projects.intra.42.fr/projects/webserv
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263/* ************************************************************************** *//* *//* ::: :::::::: *//* parse_location.cpp :+: :+: :+: *//* +:+ +:+ +:+ *//* By: bcausseq <bcausseq@42angouleme.fr> +#+ +:+ +#+ *//* +#+#+#+#+#+ +#+ *//* Created: 2026/09/27 00:31:55 by bcausseq #+# #+# *//* Updated: 2026/09/27 23:05:20 by bcausseq ### ########.fr *//* *//* ************************************************************************** */
#include "config.hpp"#include "Location.hpp"#include "ParserExceptions.hpp"#include "parse_utils.hpp"
#include <climits>#include <cstdlib>#include <iostream>#include <sstream>
static voidhandle_methods(std::string& line, Location*& location, unsigned int& line_nbr){ size_t fws = line.find_first_of(' '); if (fws == line.npos) { std::string errs("Missing methods. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }
std::string methods = line.substr(fws + 1); trim_comma(methods);
std::stringstream ss(methods); std::string tmp; while (getline(ss, tmp, ' ')) { if (check_method(tmp, line_nbr)) location->get_methods().push_back(tmp); //TODO : check if methods entered are correct ? }}
static voidhandle_autoindex(std::string& line, Location*& location, unsigned int& line_nbr){ size_t fws = line.find_first_of(' '); if (fws == line.npos) { std::string errs("Missing on/off autoindex. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }
std::string autoindex = line.substr(fws + 1); trim_comma(autoindex);
if (autoindex == "on") location->set_autoindex(true); else if (autoindex == "off") location->set_autoindex(false); else { std::string errs("Wrong flag autoindex. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }}
static voidhandle_cookie(std::string& line, Location*& location, unsigned int& line_nbr){ size_t fws = line.find_first_of(' '); if (fws == line.npos) { std::string errs("Missing on/off cookie. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }
std::string cookie = line.substr(fws + 1); trim_comma(cookie);
if (cookie == "on") location->set_cookie(true); else if (cookie == "off") location->set_cookie(false); else { std::string errs("Wrong flag cookie. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }}
static voidhandle_root(std::string& line, Location*& location, unsigned int& line_nbr){ size_t ws = line.find_first_of(' '); if (ws == line.npos) { std::string errs("Missing root value. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }
std::string root = line.substr(ws + 1);
if (root.empty()) { std::string errs("Root for the Location can't be empty"); throw (ParserExceptions(errs)); }
trim_comma(root);
if (check_root(root, line_nbr)) location->set_root(root);}
static voidhandle_index_location(std::string& line, Location*& location, unsigned int& line_nbr){ size_t fws = line.find_first_of(' '); if (fws == line.npos) { std::string errs("Missing index in the location. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }
std::string indexes = line.substr(fws + 1); trim_comma(indexes);
std::stringstream ss(indexes); std::string tmp; while (getline(ss, tmp, ' ')) location->get_index().push_back(tmp);}
static voidhandle_cgi(std::string& line, Location*& location, unsigned int& line_nbr){ size_t fws = line.find_first_of(' '); if (fws == line.npos) { std::string errs("Missing cgi extension and path. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }
std::string key_value = line.substr(fws + 1);
size_t sep = key_value.find_first_of(' '); if (sep == std::string::npos) { std::string errs("Missing cgi path. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }
std::string key = key_value.substr(0, sep);
while (key_value.at(sep) == ' ') sep++; std::string value = key_value.substr(sep); trim_comma(value);
if (key.empty() || value.empty()) { std::string errs("Missing cgi extension and path. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); } location->set_cgi(key, value);}
static voidhandle_redirect(std::string& line, Location*& location, unsigned int& line_nbr){ errno = 0;
size_t fws = line.find_first_of(' '); if (fws == line.npos) { std::string errs("Missing redirect code and/or URL. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); } std::string code_path = line.substr(fws + 1); size_t ws = code_path.find_first_of(' '); std::string code; std::string path; if (ws != code_path.npos) { code = code_path.substr(0, ws - 1); path = code_path.substr(ws + 1); } else { code = "301"; path = code_path; }
char *errs; long code_nbr = strtol(code.c_str(), &errs, 10);
if (*errs || errno == ERANGE || code_nbr < 0 || code_nbr > INT_MAX) { std::string errs("The exit code isn't good sorry. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); } location->set_redir(true, static_cast<unsigned short>(code_nbr), path);}
voidin_location(std::string& line, Location*& location, state_machine& state, unsigned int& line_nbr){ if (!line.rfind('}', 0)) { location = NULL; state = IN_SERVER; return ; }
if (line.find(';') == line.npos) { std::string errs("Please end all of your line with ';'. Problem line : "); errs += itos(line_nbr); throw (ParserExceptions(errs)); }
if (!line.rfind("cgi", 0)) handle_cgi(line, location, line_nbr);
else if (!line.rfind("methods", 0)) handle_methods(line, location, line_nbr);
else if (!line.rfind("autoindex", 0)) handle_autoindex(line, location, line_nbr);
else if (!line.rfind("root", 0)) handle_root(line, location, line_nbr);
else if (!line.rfind("index", 0)) handle_index_location(line, location, line_nbr);
else if (!line.rfind("cookie", 0)) handle_cookie(line, location, line_nbr);
else if (!line.rfind("return", 0) || !line.rfind("redirect", 0)) handle_redirect(line, location, line_nbr);
else std::cerr << "Wrong field for the location part at line " << line_nbr << std::endl << "line :" << line << std::endl;}