This commit is contained in:
Me
2022-08-12 04:38:07 +02:00
parent ab0bc2c4c0
commit f7c0ff1a8a
11 changed files with 27 additions and 103 deletions

View File

@@ -34,7 +34,7 @@ class ConfigParser {
public: public:
// canonical // canonical?
ConfigParser(const char* path); // a string? ConfigParser(const char* path); // a string?
~ConfigParser(); ~ConfigParser();
@@ -42,23 +42,15 @@ public:
// ideally i wouldn't have one cuz it makes no sense, when would i use it? // ideally i wouldn't have one cuz it makes no sense, when would i use it?
// ConfigParser & operator=(const ConfigParser& rhs); // ConfigParser & operator=(const ConfigParser& rhs);
// void parse(); // return void cuz throw exceptions.
std::vector<ServerConfig> * parse(); // const? std::vector<ServerConfig> * parse(); // const?
// 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 // i thought if it were an instance of this class you could call
// private member functions from anywhere... // private member functions from anywhere...
void _print_content() const; void _print_content() const;
// I don't love that this is here but...
// doesn't work use the operator overload
// bool compareLocationConfigs(const LocationConfig &a, const LocationConfig &b);
private: private:
std::string _content; std::string _content;
@@ -83,38 +75,10 @@ private:
std::string _get_rest_of_line(size_t *curr); // const? std::string _get_rest_of_line(size_t *curr); // const?
/* Post Processing */
// some sort of 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?
}; };
// no idea if it should go here...
//bool compareLocationConfigs(const LocationConfig &a,
// const LocationConfig &b);
// def needs work line a better name an do i even need this?
// should it be in Utils instead?
class MyException : public std::invalid_argument
{
MyException(const std::string str)
: std::invalid_argument(str) {}
};
#endif #endif

View File

@@ -19,9 +19,6 @@
# include <iostream> # include <iostream>
# include <sys/stat.h> // stat() # include <sys/stat.h> // stat()
# include <stdio.h> // printf(), gotta go
# include "utils.hpp" # include "utils.hpp"
// again, struct instead? // again, struct instead?
@@ -31,7 +28,7 @@ public:
// canonic stuff? // canonic stuff?
std::string path; // /path and /path/ are fine std::string path; // /path and /path/ are fine
// i add trailing / if a dir // i remove trailing / systematically
std::string root; std::string root;
std::vector<std::string> index; std::vector<std::string> index;
unsigned int allow_methods; unsigned int allow_methods;
@@ -42,10 +39,8 @@ public:
int redirect_status; // only in location int redirect_status; // only in location
std::string redirect_uri; // only 1 per location std::string redirect_uri; // only 1 per location
// au pire you do location / { return 301 http://location; }
// and that's how you get the redirect from the root.
void print_all() void print_all() // const?
{ {
std::cout << "\nPRINTING A LOCATION\n"; std::cout << "\nPRINTING A LOCATION\n";

View File

@@ -20,12 +20,14 @@ public:
// we could shove default in here if we wanted to... // we could shove default in here if we wanted to...
std::string host; std::string host;
std::string port; // port needs to be something else... not quite an int std::string port;
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
unsigned int client_body_limit; // set to default max if none set unsigned int client_body_limit; // set to default max if none set
// 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;
@@ -33,7 +35,7 @@ public:
std::vector<LocationConfig> locations; std::vector<LocationConfig> locations;
void print_all() void print_all() // const?
{ {
std::cout << "PRINTING A FULL SERVER CONFIG\n\n"; std::cout << "PRINTING A FULL SERVER CONFIG\n\n";

View File

@@ -1,27 +0,0 @@
// prolly get rid of this file...
#include "LocationConfig.hpp"
#include <string>
#include <algorithm>
// Ok so maybe it can't be a member functions?
bool compareLocationConfigs(const LocationConfig &a, const LocationConfig &b)
{
int len_a;
int len_b;
size_t tmp = 0;
// consider adding 1 to path that ends in a file not folder.
while ((tmp = a.path.find_first_of("/", tmp)) != std::string::npos)
++len_a;
tmp = 0;
while ((tmp = b.path.find_first_of("/", tmp)) != std::string::npos)
++len_b;
return (len_a < len_b); // right comparison ? not <= ?
}

View File

@@ -3,8 +3,8 @@
#include "ConfigParser.hpp" #include "ConfigParser.hpp"
// should i be sending & references?
// const?
std::string ConfigParser::_pre_set_val_check(const std::string key, \ std::string ConfigParser::_pre_set_val_check(const std::string key, \
const std::string value) const std::string value)
{ {
@@ -16,12 +16,8 @@ std::string ConfigParser::_pre_set_val_check(const std::string key, \
if (key.find_first_of(";") != std::string::npos) if (key.find_first_of(";") != std::string::npos)
throw std::invalid_argument("bad config file arguments 2"); throw std::invalid_argument("bad config file arguments 2");
// there shouldn't be any tabs, right? not between values...
if (value.find_first_of("\t") != std::string::npos) if (value.find_first_of("\t") != std::string::npos)
{
// std::cout << value << "\n";
throw std::invalid_argument("why would you put tabs between values"); throw std::invalid_argument("why would you put tabs between values");
}
size_t i = value.find_first_of(";"); size_t i = value.find_first_of(";");
// so you can't have no ; // so you can't have no ;
@@ -36,25 +32,22 @@ std::string ConfigParser::_pre_set_val_check(const std::string key, \
return (value.substr(0, i)); return (value.substr(0, i));
} }
// const?
// assumes curr is on a space or \t or \n // assumes curr is on a space or \t or \n
// get first word? next word? word?
std::string ConfigParser::_get_first_word(size_t *curr) std::string ConfigParser::_get_first_word(size_t *curr)
{ {
size_t start; size_t start;
// are these checks excessive?
if ((start = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos) if ((start = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos)
throw std::invalid_argument("bad config file arguments"); throw std::invalid_argument("bad config file arguments");
if ((*curr = _content.find_first_of(" \t\n", start)) == std::string::npos) if ((*curr = _content.find_first_of(" \t\n", start)) == std::string::npos)
throw std::invalid_argument("bad config file arguments"); throw std::invalid_argument("bad config file arguments");
std::string key = _content.substr(start, *curr - start); std::string key = _content.substr(start, *curr - start);
return (key); return (key);
} }
// const?
// also assumes curr is on a space \t or \n // also assumes curr is on a space \t or \n
std::string ConfigParser::_get_rest_of_line(size_t *curr) std::string ConfigParser::_get_rest_of_line(size_t *curr)
{ {
@@ -62,7 +55,6 @@ std::string ConfigParser::_get_rest_of_line(size_t *curr)
if ((start = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos) if ((start = _content.find_first_not_of(" \t\n", *curr)) == std::string::npos)
throw std::invalid_argument("bad config file arguments"); throw std::invalid_argument("bad config file arguments");
if ((*curr = _content.find_first_of("\n", start)) == std::string::npos) if ((*curr = _content.find_first_of("\n", start)) == std::string::npos)
throw std::invalid_argument("bad config file arguments"); throw std::invalid_argument("bad config file arguments");
@@ -71,8 +63,6 @@ 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

@@ -10,7 +10,7 @@ ConfigParser::ConfigParser()
ConfigParser::ConfigParser(const char* path) ConfigParser::ConfigParser(const char* path)
{ {
std::cout << "Param Constructor\n"; // std::cout << "Param Constructor\n";
std::ifstream file; std::ifstream file;
std::string buf; std::string buf;
@@ -59,11 +59,10 @@ ConfigParser & ConfigParser::operator=(const ConfigParser& rhs)
} }
*/ */
// const?
std::vector<ServerConfig> * ConfigParser::parse() std::vector<ServerConfig> * ConfigParser::parse()
{ {
std::vector<ServerConfig> * ret = new std::vector<ServerConfig>(); std::vector<ServerConfig> * ret = new std::vector<ServerConfig>();
// std::vector<ServerConfig> ret;
size_t start = 0; size_t start = 0;
size_t curr = _content.find_first_not_of(" \t\n", 0); size_t curr = _content.find_first_not_of(" \t\n", 0);

View File

@@ -73,17 +73,15 @@ void ConfigParser::_post_processing(std::vector<ServerConfig> *servers)
} }
} }
// const?
bool ConfigParser::_find_root_path_location(std::vector<LocationConfig> locations) bool ConfigParser::_find_root_path_location(std::vector<LocationConfig> locations)
{ {
std::vector<LocationConfig>::iterator it = locations.begin(); std::vector<LocationConfig>::const_iterator it = locations.begin();
while (it != locations.end()) while (it != locations.end())
{ {
if (it->path.compare("/") == 0) if (it->path.compare("/") == 0)
{
// std::cout << "in compare: " << it->path << " -- ";
return true; return true;
}
++it; ++it;
} }
return false; return false;

View File

@@ -25,6 +25,4 @@
"</body>"\ "</body>"\
"</html>" "</html>"
#endif #endif

View File

@@ -1,7 +1,7 @@
#include "Webserv.hpp" #include "Webserv.hpp"
// TODO : path_is_valid() Macro for return value // const?
void Webserv::_get(Client *client) void Webserv::_get(Client *client)
{ {
std::string path = client->get_rq_abs_path(); std::string path = client->get_rq_abs_path();
@@ -114,6 +114,7 @@ void Webserv::_get_file(Client *client, const std::string &path)
} }
} }
// const?
void Webserv::_autoindex(Client *client, std::string &path) void Webserv::_autoindex(Client *client, std::string &path)
{ {
std::cout << "made it to _autoindex\n"; std::cout << "made it to _autoindex\n";
@@ -162,9 +163,8 @@ void Webserv::_autoindex(Client *client, std::string &path)
else else
{ {
// in theory not possible cuz we already checked... // in theory not possible cuz we already checked...
/* could not open directory */ std::cerr << "could not open dir\n";
// perror (""); // throw?
std::cout << "could not open dir\n";
return ; return ;
} }
} }

View File

@@ -169,6 +169,7 @@ ServerConfig *_determine_process_server(Client *client, std::vector<ServerConfig
return (&(*default_server)); return (&(*default_server));
} }
// const?
// Temporary Global Scope. Probably move to Client in the future. // Temporary Global Scope. Probably move to Client in the future.
const LocationConfig *_determine_location(const ServerConfig &server, const std::string &path) const LocationConfig *_determine_location(const ServerConfig &server, const std::string &path)
{ {

4
urls.txt Normal file
View File

@@ -0,0 +1,4 @@
http://localhost:4040/test
http://localhost:4040/test/test_deeper/
http://localhost:4040/test/test_deeper/super_deep/
http://localhost:4040/test/index1.html