changed client_body_limit (now in KB)

+ multiples little adjusts
This commit is contained in:
lperrey
2022-08-15 02:14:27 +02:00
parent 9ac84f706d
commit a284e400c1
12 changed files with 48 additions and 127 deletions

View File

@@ -7,28 +7,19 @@ server {
listen 0.0.0.0:4040; listen 0.0.0.0:4040;
# client_body_limit asdfa; # client_body_limit asdfa;
client_body_limit 3000000; client_body_limit 5000;
# Max == 18446744073709551615 / 1024 == 18014398509481984
index index.html; # this is another comment index index.html; # this is another comment
#index mdr.html; # this is another comment
root ./www/; root ./www/;
error_page 404 ./www/error_pages/error_404.html; error_page 404 ./www/error_pages/error_404.html;
location / {
# something to do with /upload allow_methods GET;
root ./www/;
location /upload {
root ./www/test/;
index submit_form.html;
# upload_dir ./www/uploaded/;
# cgi_ext php;
}
location /uploaded {
# autoindex on;
root ./www/uploaded/;
upload_dir ./www/uploaded/;
} }
location /srcs/cgi-bin/ { location /srcs/cgi-bin/ {
@@ -46,22 +37,17 @@ server {
cgi_ext out php sh; cgi_ext out php sh;
} }
location /cgi-bin {
root ./srcs/cgi-bin/;
cgi_ext cpp php sh;
}
location /upload { location /upload {
allow_methods POST; allow_methods POST;
autoindex on; autoindex on;
upload_dir ./www/user_files/; # TODO: append a '/' if there is none ? upload_dir ./www/user_files/;
# root doesnt matter if used only with POST and no CGI # root doesnt matter if used only with POST and no CGI
} }
location /the_dump { location /the_dump {
allow_methods GET; allow_methods GET DELETE;
root ./www/user_files; root ./www/user_files;
autoindex on; #autoindex on;
} }
location /redirect { location /redirect {

View File

@@ -4,9 +4,6 @@ IN 42 SUBJECT AND/OR PRIORITY :
- Need to test normal body parsing - Need to test normal body parsing
- upload files testing and adjustements - upload files testing and adjustements
- https://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
Config en Ko (value * 2^10) serait plus commode que en octet
Et 0 valeur special pour desactiver
- Ecrire des tests ! - Ecrire des tests !
- handle redirection (Work, but weird behavior need deeper test) - handle redirection (Work, but weird behavior need deeper test)
@@ -17,9 +14,9 @@ Et 0 valeur special pour desactiver
----------------------------- -----------------------------
Si ce n'est pas deja fait : Si ce n'est pas deja fait :
- dans config, check erreur si port > 16bits peut-être check si ip > 32bits
(peut-être check si ip > 32bits)
----------------------------- -----------------------------
- client_body_limit 0 valeur special pour desactiver dans config
- gerer le champ "Accept" du client - gerer le champ "Accept" du client
- gerer les ".." dans un URL (verifier que l'on ne sort pas du dossier "root") - gerer les ".." dans un URL (verifier que l'on ne sort pas du dossier "root")
- do correct handling of special character in url (/rfc2119_files/errata.js.t%C3%A9l%C3%A9chargement -> /rfc2119_files/errata.js.téléchargement) - do correct handling of special character in url (/rfc2119_files/errata.js.t%C3%A9l%C3%A9chargement -> /rfc2119_files/errata.js.téléchargement)

View File

@@ -183,8 +183,11 @@ void Client::_parse_chunked_body(size_t pos)
/* endptr_copy = endptr; */ /* endptr_copy = endptr; */
(void)endptr_copy; (void)endptr_copy;
chunk_size = std::strtoul(&_request.body[pos], &endptr, 16); chunk_size = std::strtoul(&_request.body[pos], &endptr, 16);
/* if (chunk_size == LONG_MAX && errno == ERANGE) if (errno == ERANGE)
status = 413; */ {
status = 413;
return ;
}
/* if (endptr == endptr_copy) /* if (endptr == endptr_copy)
{ {
std::cerr << "parse_request_body(), no conversion possible\n"; std::cerr << "parse_request_body(), no conversion possible\n";

View File

@@ -14,8 +14,6 @@
# include <cstdlib> // strtol, stroul # include <cstdlib> // strtol, stroul
# include <iostream> // cout, cin # include <iostream> // cout, cin
# include <fstream> // ifstream # include <fstream> // ifstream
//# include <unistd.h> // access()
# include <dirent.h> // opendir(), doesn't work...
# include <sys/stat.h> // stat(), replaces opendir() don't bother with ERRNO ? # include <sys/stat.h> // stat(), replaces opendir() don't bother with ERRNO ?
# include <algorithm> // sort() in Post # include <algorithm> // sort() in Post
@@ -23,21 +21,17 @@ class ConfigParser {
public: public:
// canonical? ConfigParser();
ConfigParser(const char* path); // a string?
~ConfigParser(); ~ConfigParser();
ConfigParser(const std::string &config_file);
// ideally i wouldn't have one cuz it makes no sense, when would i use it? void read_config(const std::string &config_file);
// ConfigParser & operator=(const ConfigParser& rhs);
std::vector<ServerConfig> * parse(); // const? std::vector<ServerConfig> * parse(); // const?
// std::vector<ServerConfig> parse(); // const?
// i thought if it were an instance of this class you could call // i thought if it were an instance of this class you could call
// private member functions from anywhere... // private member functions from anywhere... // QUESTION : Wut ?
void _print_content() const; void print_content() const;
private: private:
@@ -45,25 +39,22 @@ private:
// explicit? // explicit?
// what exaclty does explicit do again? // what exaclty does explicit do again?
ConfigParser(); // might need a path as arg? // ConfigParser();
// should this be in private since it always needs a path? // should this be in private since it always needs a path?
ServerConfig _parse_server(size_t *start); ServerConfig _parse_server(size_t *start);
LocationConfig _parse_location(size_t *start); LocationConfig _parse_location(size_t *start);
void _set_server_values(ServerConfig *server, const std::string key, std::string value); 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); void _set_location_values(LocationConfig *location, const std::string key, std::string value);
std::string _pre_set_val_check(const std::string key, \ std::string _pre_set_val_check(const std::string key, \
const std::string value); const std::string value);
std::string _get_first_word(size_t *curr); // const? std::string _get_first_word(size_t *curr); // const?
std::string _get_rest_of_line(size_t *curr); // const? std::string _get_rest_of_line(size_t *curr); // const?
/* Post Processing */ /* Post Processing */
void _post_processing(std::vector<ServerConfig> *servers); void _post_processing(std::vector<ServerConfig> *servers);
bool _find_root_path_location(std::vector<LocationConfig> locations); // const? bool _find_root_path_location(std::vector<LocationConfig> locations); // const?

View File

@@ -1,32 +1,14 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* LocationConfig.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/23 16:08:00 by me #+# #+# */
/* Updated: 2022/08/12 18:12:23 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LOCATIONCONFIG_HPP #ifndef LOCATIONCONFIG_HPP
# define LOCATIONCONFIG_HPP # define LOCATIONCONFIG_HPP
# include <map>
# include <vector> # include <vector>
# include <string> # include <string>
# include <iostream>
# include <sys/stat.h> // stat()
# include "utils.hpp" # include "utils.hpp"
// again, struct instead? struct LocationConfig
class LocationConfig
{ {
public:
// canonic stuff?
std::string path; // /path and /path/ are fine std::string path; // /path and /path/ are fine
// i remove trailing / systematically // i remove trailing / systematically
std::string root; std::string root;
@@ -87,13 +69,4 @@ public:
}; };
#endif #endif

View File

@@ -10,14 +10,9 @@
# include <string> // string # include <string> // string
# include <iostream> // cout, cin # include <iostream> // cout, cin
// a class that's all public? just so we have options? struct ServerConfig
class ServerConfig
{ {
public:
// do i need some canonic stuff?
std::vector<std::string> server_name; std::vector<std::string> server_name;
// we could shove default in here if we wanted to...
std::string host; std::string host;
std::string port; std::string port;
@@ -25,9 +20,7 @@ public:
std::string root; // ./www/ or www work www/ and www work std::string root; // ./www/ or www work www/ and www work
// i do remove trailing / tho // i do remove trailing / tho
size_t client_body_limit; // set to default max if none set size_t client_body_limit;
// 413 (Request Entity Too Large) if exceeded
// default is 1m 1 000 000 ?
std::vector<std::string> index; std::vector<std::string> index;
std::map<int, std::string> error_pages; std::map<int, std::string> error_pages;

View File

@@ -1,6 +1,4 @@
#include "ConfigParser.hpp" #include "ConfigParser.hpp"
// should i be sending & references? // should i be sending & references?
@@ -63,7 +61,7 @@ std::string ConfigParser::_get_rest_of_line(size_t *curr)
} }
void ConfigParser::_print_content() const void ConfigParser::print_content() const
{ {
std::cout << _content; std::cout << _content;
} }

View File

@@ -1,25 +1,26 @@
#include "ConfigParser.hpp" #include "ConfigParser.hpp"
// Default ConfigParser::ConfigParser() {};
ConfigParser::ConfigParser() ConfigParser::~ConfigParser() {};
ConfigParser::ConfigParser(const std::string &config_file)
{ {
std::cout << "Default Constructor\n"; read_config(config_file);
// don't use yet, you have no idea what the defaults are
} }
ConfigParser::ConfigParser(const char* path) void ConfigParser::read_config(const std::string &config_file)
{ {
// std::cout << "Param Constructor\n";
std::ifstream file; std::ifstream file;
std::string buf; std::string buf;
size_t comment; size_t comment;
_content.clear(); file.open(config_file.c_str());
file.open(path); if (!file)
if (file.is_open()) throw std::invalid_argument("failed to open config");
else
{ {
_content.clear();
while (!file.eof()) while (!file.eof())
{ {
getline(file, buf); getline(file, buf);
@@ -37,28 +38,9 @@ ConfigParser::ConfigParser(const char* path)
_content.append(tmp + '\n'); _content.append(tmp + '\n');
} }
} }
file.close();
} }
else
throw std::invalid_argument("failed to open config");
} }
ConfigParser::~ConfigParser()
{
// do i need to destroy anything, won't it handle itself?
}
/*
ConfigParser & ConfigParser::operator=(const ConfigParser& rhs)
{
if (this == rhs) // * & ?
return (*this); // * ?
// make some stuff equal
return (*this);
}
*/
// const? // const?
std::vector<ServerConfig> * ConfigParser::parse() std::vector<ServerConfig> * ConfigParser::parse()
{ {
@@ -190,7 +172,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
server->server_name.push_back(tmp_val[0]); server->server_name.push_back(tmp_val[0]);
} }
else if (key == "listen" && size == 1 && server->host == "" \ else if (key == "listen" && size == 1 && server->host == "" \
&& server->port == "") && server->port == "") // QUESTION LUKE : C'est quoi cette condition ? Si listen est vide ? Je comprends pas trop.
{ {
if (tmp_val[0].find_first_of(":") == NPOS) if (tmp_val[0].find_first_of(":") == NPOS)
{ {
@@ -231,6 +213,9 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
if (!::isNumeric(tmp_val[0])) if (!::isNumeric(tmp_val[0]))
throw std::invalid_argument("client_body_limit not a number"); throw std::invalid_argument("client_body_limit not a number");
server->client_body_limit = std::strtoul(tmp_val[0].c_str(), NULL, 10); server->client_body_limit = std::strtoul(tmp_val[0].c_str(), NULL, 10);
if (errno == ERANGE || server->client_body_limit > (ULONG_MAX / KB) )
throw std::invalid_argument("client_body_limit too big");
server->client_body_limit = server->client_body_limit * KB;
} }
else if (key == "index") else if (key == "index")
{ {

View File

@@ -22,7 +22,7 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
throw std::invalid_argument("Config file needs an Index"); throw std::invalid_argument("Config file needs an Index");
if (it->client_body_limit == 0) if (it->client_body_limit == 0)
it->client_body_limit = 5000; // what is the recomended size? it->client_body_limit = 1 * MB;
// if error_pages is left empty, we'll use the defaults which // if error_pages is left empty, we'll use the defaults which

View File

@@ -11,15 +11,8 @@ int main(int ac, char **av)
{ {
std::string config = (ac == 2 ? av[1] : "./default.config"); std::string config = (ac == 2 ? av[1] : "./default.config");
// like this just looks kinda gross, why bother creating an instance ConfigParser configParser(config);
// and not immediately parsing? like it servers no other purpose... // configParser.print_content();
// what if i call parse directly in the constructor?
// oh because the constructor has no return, but still
// is there a better way?
ConfigParser configParser(config.c_str());
// configParser._print_content();
// i don't love that servers has to be a pointer... // i don't love that servers has to be a pointer...
std::vector<ServerConfig>* servers = configParser.parse(); std::vector<ServerConfig>* servers = configParser.parse();

View File

@@ -22,6 +22,8 @@
# define CRLF CR LF # define CRLF CR LF
# define CRLF_SIZE 2 # define CRLF_SIZE 2
# define NPOS std::string::npos # define NPOS std::string::npos
# define KB 1024
# define MB 1048576
/* Equivalent for end of http header size : /* Equivalent for end of http header size :
** std::string(CRLF CRLF).size(); ** std::string(CRLF CRLF).size();

View File

@@ -39,7 +39,7 @@ void Webserv::_get(Client *client, std::string &path)
_get_file(client, path); _get_file(client, path);
} }
# define MAX_FILESIZE 1000000 // (1Mo) # define MAX_FILESIZE 1 * MB
void Webserv::_get_file(Client *client, const std::string &path) void Webserv::_get_file(Client *client, const std::string &path)
{ {
/* /*