Something went wrong. Try again.
An HTTP server written in C++98 projects.intra.42.fr/projects/webserv
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171/* ************************************************************************** *//* *//* ::: :::::::: *//* parse_args_v2.cpp :+: :+: :+: *//* +:+ +:+ +:+ *//* By: bcausseq <bcausseq@42angouleme.fr> +#+ +:+ +#+ *//* +#+#+#+#+#+ +#+ *//* 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 <cstddef>#include <cstdlib>#include <fstream>#include <map>#include <string>#include <vector>
#define ARGS 2
static voidinit_basic_error_pages(Config& s){ std::map<short, std::string>basic;
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 boolneed_continue(std::string& line, size_t& index){ if (line.empty() || index == std::string::npos) return (true); if (line[index] == '#') return (true);
return (false);}
static boolparse_config(std::ifstream& ifs, std::vector<Config>& 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);}
boolparse_args(int argc, char **argv, std::vector<Config>& 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);}