/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* parse_args_v2.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: bcausseq +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/09/25 00:18:18 by bcausseq #+# #+# */ /* Updated: 2026/09/27 00:38:55 by bcausseq ### ########.fr */ /* */ /* ************************************************************************** */ #include "Config.hpp" #include "config.hpp" #include "Location.hpp" #include "ParserExceptions.hpp" #include "parse_utils.hpp" #include #include #include #include #include #include #define ARGS 2 static void init_basic_error_pages(Config& s) { std::mapbasic; basic[100] = "Continue"; basic[200] = "OK"; basic[201] = "Created"; basic[202] = "Accepted"; basic[204] = "No Content"; basic[300] = "Multiple Choices"; basic[303] = "See Other"; basic[304] = "Not Modified"; basic[307] = "Temporary Redirect"; basic[308] = "Permanent Redirect"; basic[400] = "Bad Request"; basic[401] = "Unauthorized"; basic[403] = "Forbidden"; basic[404] = "Not Found"; basic[405] = "Method Not Allowed"; basic[406] = "Not Acceptable"; basic[409] = "Conflict"; basic[410] = "Gone"; basic[412] = "Precondition Failed"; basic[413] = "Payload Too Large"; basic[414] = "URI Too Long"; basic[415] = "Unsupported Media Type"; basic[417] = "Expectation Failed"; basic[418] = "I'm a TeaPot"; basic[428] = "Precondition Required"; basic[431] = "Request Header Field Too Large"; basic[451] = "Unavailable for Legal Reasons"; basic[500] = "Internal Server Error"; basic[501] = "Not Implemented"; basic[503] = "Service Unavailable"; s.set_error_pages(basic); } static bool need_continue(std::string& line, size_t& index) { if (line.empty() || index == std::string::npos) return (true); if (line[index] == '#') return (true); return (false); } static bool parse_config(std::ifstream& ifs, std::vector& s) { Config server; unsigned int line_nbr = 0; unsigned int line_curr_serv = 0; std::string line; size_t index; state_machine state = LOOKING_SERVER; Location *location = NULL; while (getline(ifs, line)) { line_nbr++; index = line.find_first_not_of(" \t"); if (need_continue(line, index)) continue ; if (line_curr_serv == 0) init_basic_error_pages(server); line_curr_serv++; line = white_space(line); switch (state) { case LOOKING_SERVER: if (line.find("server") != 0) break ; if (line.find("{") == std::string::npos) break ; state = IN_SERVER; break ; case IN_SERVER: in_server(line, server, location, state, s, line_curr_serv, line_nbr); break ; case LOCATION: in_location(line, location, state, line_nbr); break; default: throw(ParserExceptions("Wtf is this state ?")); } } for (auto it = s.begin(); it != s.end(); ++it) //TODO : add more checks (like are the port the same ?) { for (auto ti = s.begin(); ti != s.end(); ++ti) { if (ti == it) continue ; if (ti->get_port().port == it->get_port().port) throw (ParserExceptions("Multiple server on the same port")); } if (it->get_port().port == 0 || it->get_root().empty()) throw(ParserExceptions("Idk, guess the file isn't good enough")); } return (true); } bool parse_args(int argc, char **argv, std::vector& s) { std::string err; if (argc != ARGS || !argv[ARGS - 1] || !*(argv[ARGS - 1])) { std::string arg(argv[0]); err = "Usage : " + arg + " [configuration_file]"; throw (ParserExceptions(err)); } std::ifstream ifs(argv[1]); if (!ifs.is_open()) { err = "File provided isn't accessable (right or existance to check)"; throw(ParserExceptions(err)); } if (!parse_config(ifs, s)) { ifs.close(); return (false); } ifs.close(); return (true); }