」 feat: working server_name

This commit is contained in:
2025-05-09 11:53:44 +02:00
parent b3e167e5d1
commit c0258855e5
6 changed files with 56 additions and 35 deletions

View File

@ -6,18 +6,18 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:59:41 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 #pragma once
#include <cppeleven.hpp>
#include <log.hpp>
#include <node/default.hpp>
#include <cctype> #include <cctype>
#include <cppeleven.hpp>
#include <cstdlib> #include <cstdlib>
#include <log.hpp>
#include <map> #include <map>
#include <node/default.hpp>
#include <string> #include <string>
#include <tomlpp.hpp> #include <tomlpp.hpp>
@ -56,7 +56,7 @@ class Route {
std::map<std::string, std::string> *_cgi; std::map<std::string, std::string> *_cgi;
bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET, bool _methods[3]; ///> A methods boolean array which correspond to - 0: GET,
///1: POST, 2: DELETE /// 1: POST, 2: DELETE
toml::ANode *_table; toml::ANode *_table;
/** /**
@ -66,8 +66,7 @@ class Route {
* *
* @return A pointer to a map of cgi * @return A pointer to a map of cgi
*/ */
std::map<std::string, std::string> * std::map<std::string, std::string> *_parseCGI(toml::ANode *);
_parseCGI(toml::ANode *);
/** /**
* @brief Can be used to parse a table of error pages * @brief Can be used to parse a table of error pages

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 22:13:38 by mmoussou #+# #+# */ /* Created: 2025/02/11 22:13:38 by mmoussou #+# #+# */
/* Updated: 2025/04/30 09:49:48 by adjoly ### ########.fr */ /* Updated: 2025/05/09 11:08:24 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,8 +19,9 @@ namespace webserv {
namespace http { namespace http {
class IMessage { class IMessage {
public: public:
virtual std::map<std::string, std::string> getHeaders(void) const; virtual std::map<std::string, std::string> getHeaders(void) const;
virtual std::string getHeader(const std::string) const;
virtual std::string getBody(void) const; virtual std::string getBody(void) const;
virtual ~IMessage() {} virtual ~IMessage() {}
@ -33,13 +34,12 @@ public:
virtual std::string str(void) const = 0; virtual std::string str(void) const = 0;
protected: protected:
std::map<std::string, std::string> _headers; std::map<std::string, std::string> _headers;
std::string _body; std::string _body;
static const std::map<int, std::string> _response_status_codes; static const std::map<int, std::string> _response_status_codes;
}; };
} // -namespace http } // namespace http
} // -namespace webserv } // namespace webserv

View File

@ -1,5 +1,5 @@
[server] [server]
host = "0.0.0.0" host = "0.0.0.0"
port = 80 port = 8080
[server.location./] [server.location./]

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -134,6 +134,10 @@ Route *Server::whatRoute(const URL &url) {
int i = 0; int i = 0;
if (_routes == not_nullptr)
return not_nullptr;
for (auto it = prange(_routes)) { for (auto it = prange(_routes)) {
if (i < it->first.countMatchingSegments(url)) { if (i < it->first.countMatchingSegments(url)) {
ret = it; ret = it;

View File

@ -6,10 +6,11 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 23:34:45 by mmoussou #+# #+# */ /* Created: 2025/02/11 23:34:45 by mmoussou #+# #+# */
/* Updated: 2025/04/30 09:48:31 by adjoly ### ########.fr */ /* Updated: 2025/05/09 11:20:43 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "requests/IMessage.hpp"
#include <requests/default.hpp> #include <requests/default.hpp>
using namespace webserv::http; using namespace webserv::http;
@ -18,6 +19,15 @@ std::map<std::string, std::string> IMessage::getHeaders(void) const {
return (this->_headers); 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); } std::string IMessage::getBody(void) const { return (this->_body); }
void IMessage::setHeaders(std::map<std::string, std::string> const headers) { void IMessage::setHeaders(std::map<std::string, std::string> const headers) {

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */ /* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/05/04 13:37:21 by adjoly ### ########.fr */ /* Updated: 2025/05/09 11:48:59 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -52,6 +52,14 @@ void Client::parse(void) {
_route = _conf->whatRoute(URL(this->_request->getTarget())); _route = _conf->whatRoute(URL(this->_request->getTarget()));
this->_request->setRoute(_route); 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) { if (!this->_route || this->_route == not_nullptr) {
this->_request->setMethod("404"); this->_request->setMethod("404");
return; return;
@ -107,7 +115,7 @@ void Client::answer(void) {
return; return;
} }
if (_route->getRedirect() == true) { if (_route != not_nullptr && _route->getRedirect() == true) {
http::Redirect redir(_route->getRootDir()); http::Redirect redir(_route->getRootDir());
_response = redir; _response = redir;
_response_str = _response.str(); _response_str = _response.str();