From c0258855e55a468fb87a00dbd1ff2087339487bd Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 9 May 2025 11:53:44 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20working?= =?UTF-8?q?=20server=5Fname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/config/Route.hpp | 27 ++++++++++++------------- includes/requests/IMessage.hpp | 32 +++++++++++++++--------------- sample.toml | 2 +- src/config/Server.cpp | 6 +++++- src/requests_handling/IMessage.cpp | 12 ++++++++++- src/server/Client.cpp | 12 +++++++++-- 6 files changed, 56 insertions(+), 35 deletions(-) diff --git a/includes/config/Route.hpp b/includes/config/Route.hpp index 534e848..9bb02fe 100644 --- a/includes/config/Route.hpp +++ b/includes/config/Route.hpp @@ -6,18 +6,18 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/19 14:59:41 by adjoly #+# #+# */ -/* Updated: 2025/05/04 13:57:36 by adjoly ### ########.fr */ +/* Updated: 2025/05/09 11:06:38 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #pragma once -#include -#include -#include #include +#include #include +#include #include +#include #include #include @@ -29,18 +29,18 @@ class Route { Route(toml::ANode *); ~Route(void); - bool getDirList(void) { return _dirlist; } + bool getDirList(void) { return _dirlist; } /* bool getCookies(void) { return _cookies; } */ - bool getRedirect(void) { return _redirect; } + bool getRedirect(void) { return _redirect; } - int32_t getMaxBody(void) { return _max_body; } + int32_t getMaxBody(void) { return _max_body; } - std::string getRootDir(void) { return _root; } - std::string getUpRoot(void) { return _up_root; } - std::string getIndex(void) { return _index; } + std::string getRootDir(void) { return _root; } + std::string getUpRoot(void) { return _up_root; } + std::string getIndex(void) { return _index; } std::map *getCgi(void) { return _cgi; } - bool *getMethods(void) { return _methods; } + bool *getMethods(void) { return _methods; } protected: private: @@ -56,7 +56,7 @@ class Route { std::map *_cgi; bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET, - ///1: POST, 2: DELETE + /// 1: POST, 2: DELETE toml::ANode *_table; /** @@ -66,8 +66,7 @@ class Route { * * @return A pointer to a map of cgi */ - std::map * - _parseCGI(toml::ANode *); + std::map *_parseCGI(toml::ANode *); /** * @brief Can be used to parse a table of error pages diff --git a/includes/requests/IMessage.hpp b/includes/requests/IMessage.hpp index 56689ce..ac45d52 100644 --- a/includes/requests/IMessage.hpp +++ b/includes/requests/IMessage.hpp @@ -6,7 +6,7 @@ /* By: mmoussou getHeaders(void) const; - virtual std::string getBody(void) const; + public: + virtual std::map getHeaders(void) const; + virtual std::string getHeader(const std::string) const; + virtual std::string getBody(void) const; - virtual ~IMessage() {} + virtual ~IMessage() {} - virtual void setHeaders(std::map const headers); - virtual void setBody(std::string const body); + virtual void setHeaders(std::map const headers); + virtual void setBody(std::string const body); - virtual void addHeader(std::string const key, std::string const value); - virtual void rmHeader(std::string const key); + virtual void addHeader(std::string const key, std::string const value); + virtual void rmHeader(std::string const key); - virtual std::string str(void) const = 0; + virtual std::string str(void) const = 0; -protected: - std::map _headers; - std::string _body; + protected: + std::map _headers; + std::string _body; static const std::map _response_status_codes; - }; -} // -namespace http -} // -namespace webserv +} // namespace http +} // namespace webserv diff --git a/sample.toml b/sample.toml index f082b19..7bc0cde 100644 --- a/sample.toml +++ b/sample.toml @@ -1,5 +1,5 @@ [server] host = "0.0.0.0" -port = 80 +port = 8080 [server.location./] diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 2f40823..ca3d87e 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/05/04 12:48:10 by adjoly ### ########.fr */ +/* Updated: 2025/05/09 11:47:48 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -133,6 +133,10 @@ Route *Server::whatRoute(const URL &url) { std::map::iterator ret = _routes->end(); int i = 0; + + if (_routes == not_nullptr) + return not_nullptr; + for (auto it = prange(_routes)) { if (i < it->first.countMatchingSegments(url)) { diff --git a/src/requests_handling/IMessage.cpp b/src/requests_handling/IMessage.cpp index 409854a..7353865 100644 --- a/src/requests_handling/IMessage.cpp +++ b/src/requests_handling/IMessage.cpp @@ -6,10 +6,11 @@ /* By: mmoussou using namespace webserv::http; @@ -18,6 +19,15 @@ std::map IMessage::getHeaders(void) const { return (this->_headers); } +std::string IMessage::getHeader(const std::string key) const { + std::string str; + auto it = _headers.find(key); + if (it == _headers.end()) + return ""; + str = it->second; + return str; +} + std::string IMessage::getBody(void) const { return (this->_body); } void IMessage::setHeaders(std::map const headers) { diff --git a/src/server/Client.cpp b/src/server/Client.cpp index 56d584d..3ce85f3 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou whatRoute(URL(this->_request->getTarget())); this->_request->setRoute(_route); + if (_conf->getServerNames() != not_nullptr) { + std::string host = _request->getHeader("Host"); + bool ret = _conf->isServerName(host.substr(0, host.find(':'))); + if (ret == false) { + throw std::runtime_error("serverName nor correcponding"); + } + } + if (!this->_route || this->_route == not_nullptr) { this->_request->setMethod("404"); return; @@ -107,7 +115,7 @@ void Client::answer(void) { return; } - if (_route->getRedirect() == true) { + if (_route != not_nullptr && _route->getRedirect() == true) { http::Redirect redir(_route->getRootDir()); _response = redir; _response_str = _response.str();