🏗️」 wip: implementing config in client: fix your code adjoly xd

This commit is contained in:
y-syo
2025-04-30 15:56:03 +02:00
parent 0a7cb599bf
commit b955d04331
6 changed files with 54 additions and 29 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */ /* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/04/30 14:52:22 by adjoly ### ########.fr */ /* Updated: 2025/04/30 15:19:52 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -43,6 +43,7 @@ class ARequest : public http::IMessage {
std::string getTarget(void) const; std::string getTarget(void) const;
std::string getProtocol(void) const; std::string getProtocol(void) const;
config::Server *getConfig(void) const; config::Server *getConfig(void) const;
URL getUrl() const;
void setMethod(std::string const method); void setMethod(std::string const method);
void setTarget(std::string const target); void setTarget(std::string const target);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */ /* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
/* Updated: 2025/04/30 09:51:16 by adjoly ### ########.fr */ /* Updated: 2025/04/30 15:21:54 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -54,3 +54,11 @@ void ARequest::setTarget(std::string const target) { this->_target = target; }
void ARequest::setProtocol(std::string const protocol) { void ARequest::setProtocol(std::string const protocol) {
this->_protocol = protocol; this->_protocol = protocol;
} }
URL ARequest::getUrl() const
{
if (this->_url)
return *(this->_url);
else
return URL("");
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */ /* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */
/* Updated: 2025/04/30 09:47:33 by adjoly ### ########.fr */ /* Updated: 2025/04/30 15:17:15 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,7 +28,10 @@ void Delete::parse(std::string const &data) {
if (std::getline(stream, line)) { if (std::getline(stream, line)) {
std::istringstream line_stream(line); std::istringstream line_stream(line);
line_stream >> this->_method >> this->_target >> this->_protocol; line_stream >> this->_method >> this->_target >> this->_protocol;
this->_target.insert(this->_target.begin(), '.'); _method = _sanitizeStr(_method);
_target = _sanitizeStr(_target);
_protocol = _sanitizeStr(_protocol);
// this->_target.insert(this->_target.begin(), '.');
} }
while (std::getline(stream, line) && line != "\r") { while (std::getline(stream, line) && line != "\r") {
@ -36,7 +39,7 @@ void Delete::parse(std::string const &data) {
if (delimiter_index != std::string::npos) { if (delimiter_index != std::string::npos) {
std::string key = line.substr(0, delimiter_index); std::string key = line.substr(0, delimiter_index);
std::string value = line.substr(delimiter_index + 2); std::string value = line.substr(delimiter_index + 2);
this->_headers.insert(std::make_pair(key, value)); this->_headers.insert(std::make_pair(key, _sanitizeStr(value)));
} }
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */ /* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
/* Updated: 2025/04/30 14:54:23 by adjoly ### ########.fr */ /* Updated: 2025/04/30 15:30:08 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,6 @@ void Get::parse(std::string const &data) {
_method = _sanitizeStr(_method); _method = _sanitizeStr(_method);
_target = _sanitizeStr(_target); _target = _sanitizeStr(_target);
_protocol = _sanitizeStr(_protocol); _protocol = _sanitizeStr(_protocol);
/* this->_target.insert(this->_target.begin(), '.'); */
} }
while (std::getline(stream, line) && line != "\r") { while (std::getline(stream, line) && line != "\r") {
@ -50,9 +49,8 @@ void Get::parse(std::string const &data) {
_url = new URL("http://" + _headers["Host"] + _target); _url = new URL("http://" + _headers["Host"] + _target);
/*std::cout << "wtf = " << _headers["Host"] << std::endl;
std::cout << "wtf = " << _headers["Host"] << std::endl; std::cout << *_url << std::endl;*/
std::cout << *_url << std::endl;
/* /*
std::cout << "-- start-line --" << std::endl; std::cout << "-- start-line --" << std::endl;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */ /* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
/* Updated: 2025/04/30 09:50:22 by adjoly ### ########.fr */ /* Updated: 2025/04/30 15:18:01 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,7 +30,10 @@ void Post::parse(std::string const &data) {
if (std::getline(stream, line)) { if (std::getline(stream, line)) {
std::istringstream line_stream(line); std::istringstream line_stream(line);
line_stream >> this->_method >> this->_target >> this->_protocol; line_stream >> this->_method >> this->_target >> this->_protocol;
this->_target.insert(this->_target.begin(), '.'); _method = _sanitizeStr(_method);
_target = _sanitizeStr(_target);
_protocol = _sanitizeStr(_protocol);
// this->_target.insert(this->_target.begin(), '.');
} }
while (std::getline(stream, line) && line != "\r") { while (std::getline(stream, line) && line != "\r") {
@ -38,7 +41,7 @@ void Post::parse(std::string const &data) {
if (delimiter_index != std::string::npos) { if (delimiter_index != std::string::npos) {
std::string key = line.substr(0, delimiter_index); std::string key = line.substr(0, delimiter_index);
std::string value = line.substr(delimiter_index + 2); std::string value = line.substr(delimiter_index + 2);
this->_headers.insert(std::make_pair(key, value)); this->_headers.insert(std::make_pair(key, _sanitizeStr(value)));
} }
} }

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/04/30 13:48:03 by adjoly ### ########.fr */ /* Updated: 2025/04/30 15:54:43 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,16 +47,27 @@ void Client::parse(void) {
_getRequest(received_data); _getRequest(received_data);
_conf = _Gconf->getServer(this->_request->getHeaders()["Host"]); this->_conf = this->_Gconf->getServer(this->_request->getHeaders()["Host"]);
this->_route = this->_conf->whatRoute(URL(this->_request->getTarget()));
std::cout << "_route is " << (_route ? "not null" : "NULL") << std::endl;
if(!this->_route || this->_route == not_nullptr)
{
this->_request->setMethod("404");
return ;
}
// if (received_data.length > (get max_body_size from Route corresponding) ) if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0])
// throw error || (this->_request->getMethod() == "POST" && !_route->getMethods()[1])
|| (this->_request->getMethod() == "DELETE" && !_route->getMethods()[2]))
this->_request->setMethod("405");
if (received_data.length() > (unsigned long)(_route->getMaxBody()))
this->_request->setMethod("413");
} }
bool Client::requestParsed(void) { bool Client::requestParsed(void) {
if (_request == not_nullptr) { if (_request == not_nullptr)
return false; return false;
}
return true; return true;
} }
@ -64,22 +75,23 @@ void Client::_getRequest(std::string request_str) {
std::string method = request_str.substr( std::string method = request_str.substr(
0, request_str.substr(0, 4).find_last_not_of(" ") + 1); 0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
(void)_route; if (method == "GET")
/* if (_isAllowed(method)) { */ {
/* _request = new http::Get(); */
/* _request->setMethod("405"); */
/* _log->info("405 methods not allowed"); // TODO make a better log print */
/* } */
if (method == "GET") {
this->_request = new http::Get(request_str); this->_request = new http::Get(request_str);
_log->info("get request received"); _log->info("get request received");
} else if (method == "DELETE") { }
else if (method == "DELETE")
{
this->_request = new http::Delete(request_str); this->_request = new http::Delete(request_str);
_log->info("delete request received"); _log->info("delete request received");
} else if (method == "POST") { }
else if (method == "POST")
{
this->_request = new http::Post(request_str); this->_request = new http::Post(request_str);
_log->info("post request received"); _log->info("post request received");
} else { }
else
{
this->_request = new http::Get(); this->_request = new http::Get();
this->_request->setMethod("501"); this->_request->setMethod("501");
_log->info("unsupported request received"); _log->info("unsupported request received");