🔨」 fix: fixed small errors (path not being correctly used, whole program always being 501 cuz why not is not doing that anymore btw)

This commit is contained in:
yosyo
2025-07-07 19:07:29 +02:00
parent 954574b43a
commit debb2301c5
6 changed files with 96 additions and 22 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/19 14:11:28 by adjoly #+# #+# */
/* Updated: 2025/06/23 20:10:48 by adjoly ### ########.fr */
/* Updated: 2025/07/05 16:39:51 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -65,7 +65,8 @@ class Server {
bool isServerName(const std::string &);
// @brief Can be used to get the route correcponding
Route *whatRoute(const URL &);
Route *whatRoute(URL url);
Route *whichRoute(std::string &target);
protected:
private:

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */
/* Updated: 2025/05/27 19:46:54 by adjoly ### ########.fr */
/* Updated: 2025/07/05 17:54:18 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -48,6 +48,42 @@ class URL {
std::string getQueryString(void) const { return _query_string; }
std::string getPort(void) const { return _port; }
URL truncate() const {
std::vector<std::string> segments = _path_segments;
if (!segments.empty())
segments.erase(segments.begin());
std::string truncatedPath = "/";
for (size_t i = 0; i < segments.size(); ++i) {
truncatedPath += segments[i];
if (i != segments.size() - 1)
truncatedPath += "/";
}
// Reconstruct full URL with scheme, port, etc.
size_t scheme_pos = _full_url.find("://");
std::string prefix = "";
if (scheme_pos != std::string::npos) {
prefix = _full_url.substr(0, scheme_pos + 3);
size_t host_end = _full_url.find('/', scheme_pos + 3);
if (host_end != std::string::npos)
prefix += _full_url.substr(scheme_pos + 3, host_end - (scheme_pos + 3));
else
prefix += _full_url.substr(scheme_pos + 3);
}
if (!_port.empty())
prefix += ":" + _port;
if (!_query_string.empty())
truncatedPath += "?" + _query_string;
return URL(prefix + truncatedPath);
}
private:
void _parse() {
size_t scheme_pos = _full_url.find("://");

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/05/30 16:08:13 by adjoly ### ########.fr */
/* Updated: 2025/07/05 16:42:39 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -57,9 +57,9 @@ class ARequest : public http::IMessage {
void setRoute(config::Route *route);
void setSrv(config::Server *srv);
std::string _target;
protected:
std::string _method;
std::string _target;
std::string _protocol;
webserv::config::Route *_route;
config::Server * _srv;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
/* Updated: 2025/06/17 19:14:04 by adjoly ### ########.fr */
/* Updated: 2025/07/05 17:56:40 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -132,7 +132,7 @@ bool Server::isServerName(const std::string &server_name) {
return false;
}
Route *Server::whatRoute(const URL &url) {
Route *Server::whatRoute(URL url) {
std::map<URL, Route *>::iterator ret = _routes->end();
int i = 0;
@ -152,6 +152,31 @@ Route *Server::whatRoute(const URL &url) {
return ret->second;
}
Route *Server::whichRoute(std::string &target) {
URL url(target);
std::map<URL, Route *>::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)) {
i = it->first.countMatchingSegments(url);
ret = it;
}
}
if (ret == _routes->end())
return _routes->find(URL("/"))->second;
for (; i > 0; i--)
url = url.truncate();
target = url.getFullUrl();
return ret->second;
}
Server::Server(toml::ANode *node, void *)
: _routes(not_nullptr), _server_names(not_nullptr), _table(node) {
bool found;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */
/* Updated: 2025/07/02 11:57:06 by adjoly ### ########.fr */
/* Updated: 2025/07/07 19:06:38 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,7 +36,6 @@ void Delete::parse(std::string const &data) {
_method = _sanitizeStr(_method);
_target = _sanitizeStr(_target);
_protocol = _sanitizeStr(_protocol);
// this->_target.insert(this->_target.begin(), '.');
}
while (std::getline(stream, line) && line != "\r") {

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/07/02 12:58:56 by adjoly ### ########.fr */
/* Updated: 2025/07/07 19:06:55 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
#include "server/PfdManager.hpp"
#include "server/ResourceManager.hpp"
#include <cstddef>
#include <cstdlib>
#include <log.hpp>
#include <server/Cgi.hpp>
#include <server/Client.hpp>
@ -55,7 +56,9 @@ void Client::parse(void) {
if (_request == not_nullptr)
return;
_route = _conf->whatRoute(URL(this->_request->getTarget()));
std::cout << "before: " << this->_request->_target << std::endl;
_route = _conf->whichRoute(this->_request->_target);
std::cout << "after: " << this->_request->_target << std::endl;
if (_request->getMethod() != "501" &&
_conf->getServerNames() != not_nullptr) {
@ -78,7 +81,7 @@ void Client::parse(void) {
this->_request->setMethod("405");
if (received_data.length() > (unsigned long)(_route->getMaxBody()))
_request->setMethod("413");
this->_request->setMethod("413");
}
bool Client::requestParsed(void) {
@ -101,8 +104,9 @@ void Client::_getRequest(std::string request_str) {
_response_done = true;
return;
}
std::string method = request_str.substr(
0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
std::istringstream strm(request_str);
std::string method;
strm >> method;
if (method == "GET") {
_request = new http::Get(request_str, _conf);
@ -131,22 +135,31 @@ void Client::answer(void) {
return;
}
if (_route != not_nullptr && _route->getRedirect() == true) {
if (_route != not_nullptr && _route->getRedirect() == true)
{
http::Redirect redir(_route->getRootDir());
_response = redir;
_response_str = _response.str();
_bytes_sent = 0;
} else if (_response_str.empty()) {
}
else if (_response_str.empty())
{
if (this->_request->getMethod() == "GET" ||
this->_request->getMethod() == "DELETE" ||
this->_request->getMethod() == "POST") {
this->_request->getMethod() == "POST")
{
_response = this->_request->execute();
_response_str = _response.str();
} else {
this->_response.setStatusCode(501);
_response_str = "HTTP/1.1 501 Not Implemented\r\nContent-Type: "
"text/html\r\n\r\n<html><body><h1>501 Not "
"Implemented</h1></body></html>";
}
else
{
this->_response.setProtocol(this->_request->getProtocol());
this->_response.setStatusCode(std::atoi(this->_request->getMethod().c_str()));
this->_response.addHeader("Content-Type", "text/html");
this->_response.setBody(http::Errors::getResponseBody(
this->_response.getStatusCode(),
this->_conf->getErrorPage(this->_response.getStatusCode())));
this->_response_str = this->_response.str();
}
_bytes_sent = 0;
}