From 1eb989a3fd55bc8f5bb62eea889f2e01400a5d42 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Tue, 9 Aug 2022 20:47:21 +0200 Subject: [PATCH] _determine_location() new version (to complete and test) --- memo.txt | 6 ++-- srcs/utils.cpp | 3 -- srcs/webserv/response.cpp | 65 +++++++++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/memo.txt b/memo.txt index c076cb7..cffbaf6 100644 --- a/memo.txt +++ b/memo.txt @@ -1,11 +1,13 @@ IN 42 SUBJECT, PRIORITY : -- chunked request (response not mandatory it seems) - CGI +- chunked request (response not mandatory it seems) - index (default file directory) - Ecrire des tests ! - handle redirection (weird behavior, to fix) - upload files with config "upload_repo" +- _determine_location() review (New version to complete and test) +- bug with verification of "redirect uri" (valid uri are rejected) ----------------------------- - autoindex (done, need test) -------------- @@ -20,8 +22,6 @@ IN 42 SUBJECT, PRIORITY : - add headers "Date" and "Last-Modified" to response - change "std::string" to reference "std::string &" in most functions and add "const" if apropriate. -- Dans le parsing, trier les "locations" par ordre de precision. -Compter les "/" dans le chemin, les locations avec le plus de "/" seront en premier dans le vector. - Il faut vérifier le path de la requête, voir si le serveur est bien censé délivrer cette ressource et si le client y a accès, avant d'appeler le CGI. diff --git a/srcs/utils.cpp b/srcs/utils.cpp index d250c12..75c60a0 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -79,9 +79,6 @@ http_method str_to_http_method(std::string &str) return POST; else if (str == "DELETE") return DELETE; - else if (str == "ALL_METHODS") // for Eric: why this test ? can we delete it? - return ANY_METHODS; - // would prefere ALL_METHODS return UNKNOWN; } diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 0699bce..76578f4 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -75,21 +75,13 @@ void Webserv::_construct_response(Client *client) client->status = 413; return; } -/* if (client->assigned_location->redirect_status) - { + if (client->assigned_location->redirect_status) + { // Weird behavior. Sometimes, the web browser seems to wait for a complete response until timeout. // (for codes 301, 302, 303, 307, and 308) client->status = client->assigned_location->redirect_status; client->response.append("Location: "); client->response.append(client->assigned_location->redirect_uri); client->response.append(CRLF); - } */ - if (client->get_rq_abs_path().find("redirect_test") != std::string::npos) // Test block - { // Weird behavior. The web browser seems to wait for a complete response until timeout. - // (for codes 301, 302, 303, 307, and 308) - client->status = 307; - client->response.append("Location: "); - client->response.append("https://www.rfc-editor.org/rfc/rfc3875#section-3.3"); - client->response.append(CRLF); client->response.append(CRLF); return ; } @@ -203,6 +195,53 @@ ServerConfig *Webserv::_determine_process_server(Client *client) return (&(*default_server)); } + +const LocationConfig *_determine_location_COOP_FIX(const ServerConfig &server, const std::string &path) +{ +/* Pseudo-code : +- comparer les size(), si location.path > client.path, stop comparaison. +- client.path.compare(0, location.path.size(), location.path) +if ( == 0) +{ + if (location.path.size() == client.path.size()) + { + FOUND; + } + else if (client.path[location.path.size()] == '/') + { + FOUND; + } +} +else +{ + NOT FOUND; + ++next; +} +*/ + std::vector::const_iterator it = server.locations.begin(); + while (it != server.locations.end()) + { + if (it->path.size() > path.size()) // Warning : il faut aussi prendre en compte l'éventuel "/" final + { + ++it; + continue; + } + if (path.compare(0, it->path.size(), it->path) == 0) + { + if (it->path.size() == path.size()) + break; + else if (path[it->path.size()] == '/') + break; + } + ++it; + } + if (it != server.locations.end()) + return (&(*it)); + else + return (&(server.locations.back())); +} + + const LocationConfig *Webserv::_determine_location(const ServerConfig &server, const std::string &path) const { std::cout << "determin location path sent: " << path << '\n'; @@ -259,11 +298,11 @@ if no path coresponds then use the most correct one best = it; std::cout << "Picked a best! 1\n"; } - int comp = path.compare(0, rit->path.size(), rit->path); + // 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'; + // 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 (comp == 0) if (path.compare(0, it->path.size(), it->path) == 0) {