🔨」 fix: fixed URL class to compare correctly

This commit is contained in:
2025-05-04 13:03:53 +02:00
parent d369f8ecea
commit 1537cf59ac
3 changed files with 70 additions and 69 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
/* Updated: 2025/05/01 16:30:28 by adjoly ### ########.fr */
/* Updated: 2025/05/04 12:48:10 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,8 @@
using namespace webserv::config;
Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) {
Server::Server(toml::ANode *node)
: _routes(not_nullptr), _server_names(not_nullptr), _table(node) {
bool found;
if (_table == not_nullptr)
@ -31,7 +32,7 @@ Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_null
if (host != not_nullptr) {
_host = *static_cast<std::string *>(host);
} else {
//delete _table;
// delete _table;
throw std::runtime_error(
"no host specified - please specify one in server.host");
}
@ -81,7 +82,8 @@ Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_null
if (_routes->find(URL(it->first)) != _routes->end()) {
continue;
}
_routes->insert(std::make_pair(URL(it->first), new Route(it->second)));
_routes->insert(
std::make_pair(URL(it->first), new Route(it->second)));
}
}
// delete _table;
@ -128,19 +130,24 @@ bool Server::isServerName(const std::string &server_name) {
}
Route *Server::whatRoute(const URL &url) {
std::map<URL, Route *>::iterator ret = _routes->end();
int i = 0;
for (auto it = prange(_routes)) {
if (it->first == url) {
return it->second;
if (i < it->first.countMatchingSegments(url)) {
ret = it;
}
}
std::map<URL, Route *>::iterator it = _routes->find(URL("/"));
if (it != _routes->end()) {
return it->second;
}
return not_nullptr;
if (ret == _routes->end())
return _routes->find(URL("/"))->second;
return ret->second;
}
Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names(not_nullptr), _table(node) {
Server::Server(toml::ANode *node, void *)
: _routes(not_nullptr), _server_names(not_nullptr), _table(node) {
bool found;
if (_table == not_nullptr)
@ -152,8 +159,7 @@ Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names(
_host = *static_cast<std::string *>(host);
} else {
delete _table;
throw std::runtime_error(
"no host specified - please specify one");
throw std::runtime_error("no host specified - please specify one");
}
// port parsing
void *port = accessValue("port", toml::INT, _table, _log);
@ -161,8 +167,7 @@ Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names(
_port = *static_cast<unsigned short *>(port);
} else {
delete _table;
throw std::runtime_error(
"no port specified - please specify one");
throw std::runtime_error("no port specified - please specify one");
}
// error_pages parsing
@ -184,7 +189,8 @@ Server::Server(toml::ANode *node, void *) : _routes(not_nullptr), _server_names(
if (_routes->find(URL(it->first)) != _routes->end()) {
continue;
}
_routes->insert(std::make_pair(URL(it->first), new Route(it->second)));
_routes->insert(
std::make_pair(URL(it->first), new Route(it->second)));
}
}
// delete _table;

View File

@ -6,15 +6,15 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
/* Updated: 2025/05/02 14:52:24 by mmoussou ### ########.fr */
/* Updated: 2025/05/04 12:25:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <vector>
#include <dirent.h>
#include <unistd.h>
#include <algorithm>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
#include <requests/default.hpp>
@ -91,35 +91,33 @@ Response Get::execute(void) {
this->_target = this->_route->getRootDir() + this->_target;
try {
if (isDirectory(this->_target))
{
if (!access((this->_target + this->_route->getIndex()).c_str(), R_OK))
{
if (isDirectory(this->_target)) {
if (!access((this->_target + this->_route->getIndex()).c_str(),
R_OK)) {
this->_target = this->_target + this->_route->getIndex();
std::ifstream file(this->_target.c_str(), std::ios::binary);
std::streampos file_start = file.tellg();
response.setBody(std::string((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()));
std::stringstream length;
length << (file.tellg() - file_start);
response.addHeader("Content-Length", length.str());
std::ifstream file(this->_target.c_str(), std::ios::binary);
std::streampos file_start = file.tellg();
response.setBody(
std::string((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()));
std::stringstream length;
length << (file.tellg() - file_start);
response.addHeader("Content-Length", length.str());
response.setProtocol(this->_protocol);
response.setStatusCode(200);
response.addHeader("Content-Type",
http::Mime::getType(this->_target));
}
else if (this->_route->getDirList())
{
response.setProtocol(this->_protocol);
response.setStatusCode(200);
response.addHeader("Content-Type",
http::Mime::getType(this->_target));
} else if (this->_route->getDirList()) {
DIR *dir;
struct dirent *entry;
struct stat file_stat;
std::vector<std::string> files;
std::vector<std::string> files;
if ((dir = opendir(this->_target.c_str())) == NULL)
throw;
while ((entry = readdir(dir)) != NULL) {
while ((entry = readdir(dir)) != NULL) {
std::string file_name = entry->d_name;
if (file_name == ".")
continue;
@ -172,12 +170,9 @@ body {\n\
response.addHeader("Content-Length", length.str());
response.addHeader("Content-Type", "text/html");
response.setBody(body);
}
else
} else
throw std::runtime_error("dir but no dirlist");
}
else
{
} else {
std::ifstream file(this->_target.c_str(), std::ios::binary);
std::streampos file_start = file.tellg();
response.setBody(std::string((std::istreambuf_iterator<char>(file)),
@ -189,7 +184,7 @@ body {\n\
response.setProtocol(this->_protocol);
response.setStatusCode(200);
response.addHeader("Content-Type",
http::Mime::getType(this->_target));
http::Mime::getType(this->_target));
#ifdef VERBOSE
//_log->debug(response.str().c_str());