This commit is contained in:
hugogogo
2022-07-31 12:30:35 +02:00
parent 122032a140
commit 2a69e14db2
9 changed files with 21 additions and 4 deletions

View File

@@ -1,97 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ConfigParser.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/11 23:01:41 by me #+# #+# */
/* Updated: 2022/07/27 19:27:57 by me ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CONFIGPARSER_HPP
# define CONFIGPARSER_HPP
# include "Webserv.hpp" // easier to just do this?
# include "ServerConfig.hpp"
// add includes properly
// This is gonna be temporary cuz i don't konw if i like it
#define MAX_REQUEST_SIZE 2048
#define MAX_URI_SIZE 64
#define BSIZE 1024
/*
// this can't be here...
enum MethodType
{
GET,
POST,
DELETE,
INVALID,
};
*/
class ConfigParser {
public:
// canonical
ConfigParser(const char* path); // a string?
~ConfigParser();
// ideally i wouldn't have one cuz it makes no sense, when would i use it?
// ConfigParser & operator=(const ConfigParser& rhs);
// void parse(); // return void cuz throw exceptions.
std::vector<ServerConfig> * parse(); // const?
// std::vector<ServerConfig> parse(); // const?
// other parses?
// i thought if it were an instance of this class you could call
// private member functions from anywhere...
void _print_content() const;
private:
std::string _content;
// explicit?
// what exaclty does explicit do again?
ConfigParser(); // might need a path as arg?
// should this be in private since it always needs a path?
ServerConfig _parse_server(size_t *start);
LocationConfig _parse_location(size_t *start);
void _set_server_values(ServerConfig *server, const std::string key, std::string value);
void _set_location_values(LocationConfig *location, const std::string key, std::string value);
std::string _get_first_word(size_t *curr); // const?
std::string _get_rest_of_line(size_t *curr); // const?
// why static? it's an enum...
static MethodType _str_to_method_type(std::string str);
// just for testing purposes
};
#endif

View File

@@ -1,51 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* LocationConfig.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 16:08:00 by me #+# #+# */
/* Updated: 2022/07/25 20:09:48 by me ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LOCATIONCONFIG_HPP
# define LOCATIONCONFIG_HPP
// includes
// add includes properly
# include <string>
# include <vector>
# include <map>
# include "Webserv.hpp"
// again, struct instead?
class LocationConfig
{
public:
// canonic stuff?
int client_body_limit;
std::string path;
std::string root;
std::vector<std::string> index;
std::vector<MethodType> allow_methods;
std::map<std::string, std::string> cgi_info;
};
#endif

View File

@@ -1,15 +0,0 @@
#ifndef METHODTYPE_HPP
# define METHODTYPE_HPP
enum MethodType
{
GET,
POST,
DELETE,
INVALID,
};
#endif

View File

@@ -1,104 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ServerConfig.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: me <erlazo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 15:55:16 by me #+# #+# */
/* Updated: 2022/07/23 16:19:43 by me ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SERVERCONFIG_HPP
# define SERVERCONFIG_HPP
// add includes properly...
# include "Webserv.hpp"
# include "MethodType.hpp"
//# include "ConfigParser.hpp"
# include "LocationConfig.hpp"
// a class that's all public? just so we have options?
class ServerConfig
{
public:
// i mean i guess i need some canonic stuff?
// although maybe if i make it a struct i can barebones it?
std::string server_name;
std::string root;
std::vector<std::string> index;
std::map<int, std::string> error_pages;
// i'm tempted to do something diff for storing method types...
std::vector<MethodType> allow_methods;
std::vector<LocationConfig> locations;
// might do something diff
struct timeval send_timeout;
struct timeval recv_timeout;
int client_body_limit;
bool autoindex;
// not sure what these look like in config file
int redirect_status;
std::string redirect_uri;
// is this the best way?
std::string host;
std::string port;
// do i need a print all for testing?
void print_all()
{
std::cout << "PRINTING A FULL SERVER CONFIG\n\n";
std::cout << "Server_name: " << server_name << '\n';
std::cout << "root: " << root << '\n';
std::cout << "index: ";
for (size_t i = 0; i < index.size(); i++)
std::cout << index[i] << " ";
std::cout << "\nerror_pages: ";
for(std::map<int, std::string>::iterator it = error_pages.begin(); \
it != error_pages.end(); it++)
std::cout << it->first << "--" << it->second << " ";
// for (size_t i = 0; i < error_pages.size(); i++)
// std::cout << error_pages->first << "--" << error_pages->second << " ";
std::cout << "\nallow_methods: ";
for (size_t i = 0; i < allow_methods.size(); i++)
std::cout << allow_methods[i] << " ";
std::cout << "\nskiping Locations for now...\n";
std::cout << "also skiping send_timeout and recv\n";
std::cout << "autoindex: " << autoindex << '\n';
std::cout << "client_body_limit: " << client_body_limit << '\n';
std::cout << "redirect_status: " << redirect_status << '\n';
std::cout << "redirect_uri: " << redirect_uri << '\n';
std::cout << "host: " << host << '\n';
std::cout << "port: " << port << '\n';
std::cout << "\n----------\n";
}
};
#endif

View File

@@ -1,4 +1,5 @@
# include <sstream>
#include <string.h>
static int eval_is_negative(int *n)
@@ -28,6 +29,14 @@ static int eval_digit_nbr(int n)
char *ft_itoa(int n)
{
// std::stringstream strs;
// std::string s;
//
// strs << n;
// s = strs.str();
// return ((char*)(s.c_str()));
int i;
char *str;
int is_negative;

View File

@@ -1,9 +1,6 @@
#include "Webserv.hpp"
std::vector<std::string> split(std::string input, char delimiter)
{
std::vector<std::string> answer;
@@ -16,4 +13,3 @@ std::vector<std::string> split(std::string input, char delimiter)
return answer;
}

View File

@@ -1,10 +0,0 @@
#ifndef UTILS_HPP
# define UTILS_HPP
std::vector<std::string> split(std::string input, char delimiter);
#endif

View File

@@ -24,6 +24,18 @@ void Webserv::init_virtual_servers(std::vector<ServerConfig>* servers)
std::perror("err socket()");
throw std::runtime_error("Socket init");
}
// HUGO ADD
//
// allow socket descriptor to be reuseable
// I just copied it from https://www.ibm.com/docs/en/i/7.2?topic=designs-example-nonblocking-io-select
int on = 1;
if (setsockopt(ret, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
{
::perror("err setsockopt()");
throw std::runtime_error("Socket init");
}
//
// HUGO ADD END
_listen_sockets.push_back(ret);
_bind(_listen_sockets.back(), std::atoi(it->port.data()), it->host);