From d5b3037e36083d5286c66e4eedca1711063ce6f9 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Fri, 11 Nov 2022 14:53:16 +0100 Subject: [PATCH] header_pick method --- includes/Client.hpp | 15 ++++++++------- includes/Route.hpp | 15 ++++++--------- srcs/load/Route.cpp | 11 ++++------- srcs/sock/Client.cpp | 26 +++++++++++++++++--------- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/includes/Client.hpp b/includes/Client.hpp index bc07b5d..73a6574 100644 --- a/includes/Client.hpp +++ b/includes/Client.hpp @@ -13,12 +13,13 @@ class Client { public: Client(int fd, Master *parent); ~Client(void); - bool getRequest(string paquet); - bool parseHeader(); - void answer(Env *env); - bool check_method(Server *server, Route *route, string method); - void send_cgi(string cgi, string path); - void send_error(int error_code); - void send_answer(string msg); + bool getRequest(string paquet); + bool parseHeader(); + string header_pick(string key, int id); + void answer(Env *env); + bool check_method(Server *server, Route *route, string method); + void send_cgi(string cgi, string path); + void send_error(int error_code); + void send_answer(string msg); friend class Master; }; diff --git a/includes/Route.hpp b/includes/Route.hpp index 1de28f2..a28267a 100644 --- a/includes/Route.hpp +++ b/includes/Route.hpp @@ -16,13 +16,10 @@ class Route { Route(Server *server, string location, JSONNode *datas); ~Route(void); - string getLocation(void); - string getRoot(void); - string getReturn(void); - std::vector< string > getIndexsLst(void); - std::vector< string > getHeadersLst(void); - Server *getServer(void); - string getIndex(string uri, string path); - string correctUri(string uri); - friend class Socket; + string getLocation(void); + string getRoot(void); + string getReturn(void); + Server *getServer(void); + string getIndex(string uri, string path); + string correctUri(string uri); }; diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index 15da3d0..b968354 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -33,11 +33,9 @@ Route::Route(Server *server, string location, JSONNode *datas) Route::~Route(void) {} -string Route::getLocation(void) { return _location; } -string Route::getRoot(void) { return _root; } -string Route::getReturn(void) { return _ret; } -std::vector< string > Route::getIndexsLst(void) { return _indexs; } -std::vector< string > Route::getHeadersLst(void) { return _headers; } +string Route::getLocation(void) { return _location; } +string Route::getRoot(void) { return _root; } +string Route::getReturn(void) { return _ret; } string Route::getIndex(string uri, string path) { std::stringstream content; @@ -45,7 +43,6 @@ string Route::getIndex(string uri, string path) { DIR *dir; struct dirent *entry; struct stat info; - std::vector< string > indexs = getIndexsLst(); std::vector< string >::iterator it; if ((dir = opendir(path.c_str())) == NULL) @@ -56,7 +53,7 @@ string Route::getIndex(string uri, string path) { while ((entry = readdir(dir)) != NULL) { if (entry->d_name[0] == '.') continue; - for (it = indexs.begin(); it < indexs.end(); it++) { + for (it = _indexs.begin(); it < _indexs.end(); it++) { if (entry->d_name == *it) return (read_file(path + "/" + *it)); } diff --git a/srcs/sock/Client.cpp b/srcs/sock/Client.cpp index d6eeb33..3ac6f59 100644 --- a/srcs/sock/Client.cpp +++ b/srcs/sock/Client.cpp @@ -80,21 +80,29 @@ bool Client::check_method(Server *server, Route *route, string method) { return (false); } +string Client::header_pick(string key, int id) { + if (_request[key].size() == 0) + throw std::runtime_error("The key is not in request's header."); + string ret = _request[key].at(id); + return ret; +} + void Client::answer(Env *env) { - cout << "Method: " << _request["Method:"].at(0) << "\n"; - cout << "URI: " << _request["Method:"].at(1) << "\n"; - cout << "Host: " << _request["Host:"].at(0) << "\n"; + string method = header_pick("Method:", 0); + string uri = header_pick("Method:", 1); + string host = header_pick("Method:", 0); + cout << "Method: " << method << "\n"; + cout << "URI: " << uri << "\n"; + cout << "Host: " << host << "\n"; string ret; - Server *server = _parent->choose_server(env, _request["Host:"].at(0)); - Route *route = server->choose_route(_request["Method:"].at(1)); - string method = _request["Method:"].at(0); + Server *server = _parent->choose_server(env, host); + Route *route = server->choose_route(uri); if (check_method(server, route, method)) { - string path = route->correctUri(_request["Method:"].at(1)); + string path = route->correctUri(uri); string cgi = route->_cgi.size() ? route->_cgi[get_extension(path)] : ""; if (cgi == "") { - if ((ret = route->getIndex(_request["Method:"].at(1), path)) == - "" && + if ((ret = route->getIndex(uri, path)) == "" && (ret = read_file(path)) == "") send_error(404); else