From 97c90236b999006c91d4c49dcc52e84f20bc1654 Mon Sep 17 00:00:00 2001 From: Me Date: Tue, 9 Aug 2022 02:37:28 +0200 Subject: [PATCH] cgi_ext, redirect and upload_repo now functional in config, fixed location picker, some cleanup --- default.config | 9 ++--- srcs/config/LocationConfig.hpp | 9 ++--- srcs/config/parser.cpp | 30 +++++++++------- srcs/config/postProcessing.cpp | 2 +- srcs/webserv/method_get.cpp | 11 +++--- srcs/webserv/response.cpp | 63 ++++++++++++++++++++++++++-------- www/test/index1.html | 2 +- 7 files changed, 79 insertions(+), 47 deletions(-) diff --git a/default.config b/default.config index ab88bfd..17399b2 100644 --- a/default.config +++ b/default.config @@ -24,13 +24,14 @@ server { } location /test/something.html { - allow_methods DELETE; + # allow_methods DELETE; } # location /something/long/here { # } location /test/test_deeper/ { +# allow_methods autoindex on; } @@ -38,9 +39,9 @@ server { autoindex on; } - location /test/test_deeper/something.html { - allow_methods DELETE; - } +# location /test/test_deeper/something.html { +# allow_methods DELETE; +# } # ok in theory if one were to go to /test they would get the index in www diff --git a/srcs/config/LocationConfig.hpp b/srcs/config/LocationConfig.hpp index 72a4886..66de1c5 100644 --- a/srcs/config/LocationConfig.hpp +++ b/srcs/config/LocationConfig.hpp @@ -38,13 +38,10 @@ public: std::vector cgi_ext; // php not .php bool autoindex; - std::vector upload_repo; + std::string upload_repo; // might change name... - // wait if i can call several times, shouldn't it be a map? - // wait no there can only be 1 and i think it might have to be in - // location only... - int redirect_status; - std::string redirect_uri; + int redirect_status; + std::string redirect_uri; // au pire you do location / { return 301 http://location; } // and that's how you get the redirect from the root. diff --git a/srcs/config/parser.cpp b/srcs/config/parser.cpp index 36a0ae2..c804b86 100644 --- a/srcs/config/parser.cpp +++ b/srcs/config/parser.cpp @@ -145,6 +145,7 @@ LocationConfig ConfigParser::_parse_location(size_t *start) ret.allow_methods = 0; ret.path = _get_first_word(&curr); +// are you sure about this? if (ret.path[0] != '/') ret.path.insert(0, "/"); // throw std::invalid_argument("Location path require a leading /"); @@ -290,18 +291,10 @@ void ConfigParser::_set_location_values(LocationConfig *location, \ throw std::invalid_argument("missing value"); else if (key == "root" && size == 1 && location->root == "") { -// std::cout << "location root: " << tmp_val[0] << '\n'; -// if (tmp_val[0][0] != '/') -// throw std::invalid_argument("Root requires leading /"); - // remove trailing / - // if (tmp_val[0].back() == '/') if (tmp_val[0][tmp_val[0].size() - 1] == '/') tmp_val[0].erase(tmp_val[0].size() - 1, 1); -// if (path_is_valid(tmp_val[0]) == 1) - location->root = tmp_val[0]; -// else -// throw std::invalid_argument("Root dir invalid"); + location->root = tmp_val[0]; } else if (key == "autoindex" && size == 1) { @@ -310,6 +303,7 @@ void ConfigParser::_set_location_values(LocationConfig *location, \ } else if (key == "index") { + // what about index /index.html; aka at the root? not handle? // you can definitely call Index several times, i think for (unsigned long i = 0; i != tmp_val.size(); i++) location->index.push_back(tmp_val[i]); @@ -341,14 +335,24 @@ void ConfigParser::_set_location_values(LocationConfig *location, \ if (tmp_val.size() != 2) throw std::invalid_argument("wrong number of values"); // and tmp_val[0] should be a number and tmp_val[1] a string? - if (!(::isNumeric(tmp_val[0]))) - throw std::invalid_argument("value not a number"); + if (tmp_val[0] != "301" && tmp_val[0] != "302" + && tmp_val[0] != "303" && tmp_val[0] != "307" + && tmp_val[0] != "308") + throw std::invalid_argument("bad redirect status"); +// double check this + // it means we aren't allowing internal redirects. + if (tmp_val[1].compare(0, 7, "http://") + || tmp_val[1].compare(0, 8, "https://")) + throw std::invalid_argument("bad redirect uri"); - // somehow check that tmp_val[1] is a string? or valid? how? - // something about using access() to see if location->redirect_status = atoi(tmp_val[0].c_str()); location->redirect_uri = tmp_val[1]; } + else if (key == "upload_repo" && size == 1 && location->upload_repo == "") + { + // what checks to do? + location->upload_repo = tmp_val[0]; + } else throw std::invalid_argument("bad key value pair"); } diff --git a/srcs/config/postProcessing.cpp b/srcs/config/postProcessing.cpp index 61f338a..e193549 100644 --- a/srcs/config/postProcessing.cpp +++ b/srcs/config/postProcessing.cpp @@ -74,7 +74,7 @@ void ConfigParser::_post_processing(std::vector *servers) ++it_l; } std::sort(it->locations.begin(), it->locations.end()); - std::reverse(it->locations.begin(), it->locations.end()); +// std::reverse(it->locations.begin(), it->locations.end()); ++it; } diff --git a/srcs/webserv/method_get.cpp b/srcs/webserv/method_get.cpp index e5bca7e..c8f0506 100644 --- a/srcs/webserv/method_get.cpp +++ b/srcs/webserv/method_get.cpp @@ -9,7 +9,7 @@ if path is a valid dir check if index is specified and serve that if no index and autoindex, server that if file, server that! -WHere does cgi fit in in all this ??? +Where does cgi fit in in all this ??? */ @@ -39,15 +39,12 @@ WHere does cgi fit in in all this ??? { // std::cout << "found a valid index\n"; path.append(client->assigned_location->index[i]); - break ; // what if index and autoindex in a single location? - // does this completely fail? - // do this instead of break? - //_get_file(client, path); + _get_file(client, path); + return ; } } if (client->assigned_location->autoindex == true) { - // _autoindex(client, client->assigned_location, path); _autoindex(client, path); return ; } @@ -189,4 +186,4 @@ void Webserv::_autoindex(Client *client, std::string &path) std::cout << "could not open dir\n"; return ; } -} \ No newline at end of file +} diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index c288388..53861c0 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -171,25 +171,58 @@ ServerConfig *Webserv::_determine_process_server(Client *client) const LocationConfig *Webserv::_determine_location(const ServerConfig &server, const std::string &path) const { -// std::cout << "determin location path sent: " << path << '\n'; + std::cout << "determin location path sent: " << path << '\n'; - std::vector::const_iterator it = server.locations.begin(); - while (it != server.locations.end()) + +/// NO FUCKING IDEA WHY BUT... +// basically if 2 strings are identical to a point, compare from +// longer one or it'll freak out cuz of \0 or something idk +//// Basically: str.compare() from the larger string... + +/* RULES *** + +If a path coresponds exactly to a location, use that one +if no path coresponds then use the most correct one + most correct means the most precise branch that is still above + the point we are aiming for + +*/ + + std::vector::const_iterator best; + std::cout << "\nMade it to weird location picker case.\n"; + + for (std::vector::const_iterator it = server.locations.begin(); it != server.locations.end(); it++) { -// std::cout << it->path << " -- "; - // if (it->path.compare(0, path.size(), path) == 0) - if (it->path.compare(0, it->path.size(), path) == 0) - break; -// kinda gross i know but... have to deal with when there's a / at the end - if (it->path[it->path.size() - 1] == '/' \ + std::cout << it->path << " -- "; + // if (rit->path.size() > path.size()) +/* if ((rit->path[rit->path.size() - 1] == '/' ? rit->path.size() : rit->path.size() - 1) > path.size()) + { + std::cout << "skipping this one\n"; + continue ; + } +*/ +// OK I REALLY DON"T LOVE THIS PART, BUT IT DOES WORK FOR NOW... + // if (it->path[it->path.size() - 1] == '/' + // && it->path.compare(0, it->path.size(), path + "/") == 0) +// HOLD ON THIS MIGHT BE GOOD, BUT I COULD USE SOME HELP... + if (it->path[it->path.size() - 1] == '/' && it->path.compare(0, it->path.size() - 1, path) == 0) - break; - ++it; + { + best = it; + std::cout << "Picked a best! 1\n"; + } +// int comp = path.compare(0, rit->path.size(), rit->path); + //int comp = rit->path.compare(0, rit->path.size() - 1, path); +// std::cout << "rit path size: " << rit->path.size() << " comp: " << comp << '\n'; + // if (rit->path.compare(0, rit->path.size(), path) == 0) + // if (comp == 0) + if (path.compare(0, it->path.size(), it->path) == 0) + { + best = it; + std::cout << "Picked a best! 2\n"; + } } - if (it != server.locations.end()) - return (&(*it)); - else - return (&(server.locations.back())); + return (&(*best)); } std::string Webserv::_determine_file_extension(const std::string &path) const diff --git a/www/test/index1.html b/www/test/index1.html index f976302..cf2702d 100644 --- a/www/test/index1.html +++ b/www/test/index1.html @@ -3,7 +3,7 @@ Webserv test index - +