mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 23:48:46 +02:00
「🔨」 fix: fixed URL class to compare correctly
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */
|
/* Created: 2025/04/22 12:17:48 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/04/30 14:38:03 by adjoly ### ########.fr */
|
/* Updated: 2025/05/04 12:21:03 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -18,18 +18,29 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <webserv.hpp>
|
||||||
|
|
||||||
class URL {
|
class URL {
|
||||||
public:
|
public:
|
||||||
URL(const std::string &url) : _full_url(url) { parse(); }
|
URL(const std::string &url) : _full_url(url) { _parse(); }
|
||||||
|
|
||||||
bool operator==(const URL &other) const {
|
|
||||||
return comparePathSegments(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator<(const URL &other) const {
|
bool operator<(const URL &other) const {
|
||||||
return _full_url < other._full_url;
|
return _full_url < other._full_url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int countMatchingSegments(const URL &url) const {
|
||||||
|
if (_path_segments.size() == 0 || url._path_segments.size() == 0)
|
||||||
|
return 0;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
auto u = url._path_segments;
|
||||||
|
for (auto it = u.begin(); it != u.end() && *it == _path_segments[i];
|
||||||
|
it++, i++)
|
||||||
|
;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> getSegments(void) const { return _path_segments; }
|
std::vector<std::string> getSegments(void) const { return _path_segments; }
|
||||||
|
|
||||||
std::string getFullUrl(void) const { return _full_url; }
|
std::string getFullUrl(void) const { return _full_url; }
|
||||||
@ -37,7 +48,7 @@ class URL {
|
|||||||
std::string getPort(void) const { return _port; }
|
std::string getPort(void) const { return _port; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void parse() {
|
void _parse() {
|
||||||
size_t scheme_pos = _full_url.find("://");
|
size_t scheme_pos = _full_url.find("://");
|
||||||
size_t path_start = 0;
|
size_t path_start = 0;
|
||||||
size_t query_start = _full_url.find('?');
|
size_t query_start = _full_url.find('?');
|
||||||
@ -55,12 +66,12 @@ class URL {
|
|||||||
if (path_start != std::string::npos) {
|
if (path_start != std::string::npos) {
|
||||||
std::string path =
|
std::string path =
|
||||||
_full_url.substr(path_start, query_start - path_start);
|
_full_url.substr(path_start, query_start - path_start);
|
||||||
splitPath(path, _path_segments);
|
_splitPath(path, _path_segments);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (path_start != std::string::npos) {
|
if (path_start != std::string::npos) {
|
||||||
std::string path = _full_url.substr(path_start);
|
std::string path = _full_url.substr(path_start);
|
||||||
splitPath(path, _path_segments);
|
_splitPath(path, _path_segments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,8 +80,8 @@ class URL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void splitPath(const std::string &path,
|
void _splitPath(const std::string &path,
|
||||||
std::vector<std::string> &segments) const {
|
std::vector<std::string> &segments) const {
|
||||||
std::stringstream ss(path);
|
std::stringstream ss(path);
|
||||||
std::string segment;
|
std::string segment;
|
||||||
while (std::getline(ss, segment, '/')) {
|
while (std::getline(ss, segment, '/')) {
|
||||||
@ -80,17 +91,6 @@ class URL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool comparePathSegments(const URL &other) const {
|
|
||||||
size_t min_size =
|
|
||||||
std::min(_path_segments.size(), other._path_segments.size());
|
|
||||||
for (size_t i = 0; i < min_size; ++i) {
|
|
||||||
if (_path_segments[i] != other._path_segments[i]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string _full_url;
|
std::string _full_url;
|
||||||
std::vector<std::string> _path_segments;
|
std::vector<std::string> _path_segments;
|
||||||
std::string _query_string;
|
std::string _query_string;
|
||||||
|
@ -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/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;
|
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;
|
bool found;
|
||||||
|
|
||||||
if (_table == not_nullptr)
|
if (_table == not_nullptr)
|
||||||
@ -31,7 +32,7 @@ Server::Server(toml::ANode *node) : _routes(not_nullptr), _server_names(not_null
|
|||||||
if (host != not_nullptr) {
|
if (host != not_nullptr) {
|
||||||
_host = *static_cast<std::string *>(host);
|
_host = *static_cast<std::string *>(host);
|
||||||
} else {
|
} else {
|
||||||
//delete _table;
|
// delete _table;
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"no host specified - please specify one in server.host");
|
"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()) {
|
if (_routes->find(URL(it->first)) != _routes->end()) {
|
||||||
continue;
|
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;
|
// delete _table;
|
||||||
@ -128,19 +130,24 @@ bool Server::isServerName(const std::string &server_name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Route *Server::whatRoute(const URL &url) {
|
Route *Server::whatRoute(const URL &url) {
|
||||||
|
std::map<URL, Route *>::iterator ret = _routes->end();
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
for (auto it = prange(_routes)) {
|
for (auto it = prange(_routes)) {
|
||||||
if (it->first == url) {
|
if (i < it->first.countMatchingSegments(url)) {
|
||||||
return it->second;
|
ret = it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::map<URL, Route *>::iterator it = _routes->find(URL("/"));
|
|
||||||
if (it != _routes->end()) {
|
if (ret == _routes->end())
|
||||||
return it->second;
|
return _routes->find(URL("/"))->second;
|
||||||
}
|
|
||||||
return not_nullptr;
|
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;
|
bool found;
|
||||||
|
|
||||||
if (_table == not_nullptr)
|
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);
|
_host = *static_cast<std::string *>(host);
|
||||||
} else {
|
} else {
|
||||||
delete _table;
|
delete _table;
|
||||||
throw std::runtime_error(
|
throw std::runtime_error("no host specified - please specify one");
|
||||||
"no host specified - please specify one");
|
|
||||||
}
|
}
|
||||||
// port parsing
|
// port parsing
|
||||||
void *port = accessValue("port", toml::INT, _table, _log);
|
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);
|
_port = *static_cast<unsigned short *>(port);
|
||||||
} else {
|
} else {
|
||||||
delete _table;
|
delete _table;
|
||||||
throw std::runtime_error(
|
throw std::runtime_error("no port specified - please specify one");
|
||||||
"no port specified - please specify one");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// error_pages parsing
|
// 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()) {
|
if (_routes->find(URL(it->first)) != _routes->end()) {
|
||||||
continue;
|
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;
|
// delete _table;
|
||||||
|
@ -6,15 +6,15 @@
|
|||||||
/* 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/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 <algorithm>
|
||||||
|
#include <dirent.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <requests/default.hpp>
|
#include <requests/default.hpp>
|
||||||
|
|
||||||
@ -91,35 +91,33 @@ Response Get::execute(void) {
|
|||||||
this->_target = this->_route->getRootDir() + this->_target;
|
this->_target = this->_route->getRootDir() + this->_target;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (isDirectory(this->_target))
|
if (isDirectory(this->_target)) {
|
||||||
{
|
if (!access((this->_target + this->_route->getIndex()).c_str(),
|
||||||
if (!access((this->_target + this->_route->getIndex()).c_str(), R_OK))
|
R_OK)) {
|
||||||
{
|
|
||||||
this->_target = this->_target + this->_route->getIndex();
|
this->_target = this->_target + this->_route->getIndex();
|
||||||
|
|
||||||
std::ifstream file(this->_target.c_str(), std::ios::binary);
|
std::ifstream file(this->_target.c_str(), std::ios::binary);
|
||||||
std::streampos file_start = file.tellg();
|
std::streampos file_start = file.tellg();
|
||||||
response.setBody(std::string((std::istreambuf_iterator<char>(file)),
|
response.setBody(
|
||||||
std::istreambuf_iterator<char>()));
|
std::string((std::istreambuf_iterator<char>(file)),
|
||||||
std::stringstream length;
|
std::istreambuf_iterator<char>()));
|
||||||
length << (file.tellg() - file_start);
|
std::stringstream length;
|
||||||
response.addHeader("Content-Length", length.str());
|
length << (file.tellg() - file_start);
|
||||||
|
response.addHeader("Content-Length", length.str());
|
||||||
|
|
||||||
response.setProtocol(this->_protocol);
|
response.setProtocol(this->_protocol);
|
||||||
response.setStatusCode(200);
|
response.setStatusCode(200);
|
||||||
response.addHeader("Content-Type",
|
response.addHeader("Content-Type",
|
||||||
http::Mime::getType(this->_target));
|
http::Mime::getType(this->_target));
|
||||||
}
|
} else if (this->_route->getDirList()) {
|
||||||
else if (this->_route->getDirList())
|
|
||||||
{
|
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
std::vector<std::string> files;
|
std::vector<std::string> files;
|
||||||
|
|
||||||
if ((dir = opendir(this->_target.c_str())) == NULL)
|
if ((dir = opendir(this->_target.c_str())) == NULL)
|
||||||
throw;
|
throw;
|
||||||
while ((entry = readdir(dir)) != NULL) {
|
while ((entry = readdir(dir)) != NULL) {
|
||||||
std::string file_name = entry->d_name;
|
std::string file_name = entry->d_name;
|
||||||
if (file_name == ".")
|
if (file_name == ".")
|
||||||
continue;
|
continue;
|
||||||
@ -172,12 +170,9 @@ body {\n\
|
|||||||
response.addHeader("Content-Length", length.str());
|
response.addHeader("Content-Length", length.str());
|
||||||
response.addHeader("Content-Type", "text/html");
|
response.addHeader("Content-Type", "text/html");
|
||||||
response.setBody(body);
|
response.setBody(body);
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
throw std::runtime_error("dir but no dirlist");
|
throw std::runtime_error("dir but no dirlist");
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
std::ifstream file(this->_target.c_str(), std::ios::binary);
|
std::ifstream file(this->_target.c_str(), std::ios::binary);
|
||||||
std::streampos file_start = file.tellg();
|
std::streampos file_start = file.tellg();
|
||||||
response.setBody(std::string((std::istreambuf_iterator<char>(file)),
|
response.setBody(std::string((std::istreambuf_iterator<char>(file)),
|
||||||
@ -189,7 +184,7 @@ body {\n\
|
|||||||
response.setProtocol(this->_protocol);
|
response.setProtocol(this->_protocol);
|
||||||
response.setStatusCode(200);
|
response.setStatusCode(200);
|
||||||
response.addHeader("Content-Type",
|
response.addHeader("Content-Type",
|
||||||
http::Mime::getType(this->_target));
|
http::Mime::getType(this->_target));
|
||||||
|
|
||||||
#ifdef VERBOSE
|
#ifdef VERBOSE
|
||||||
//_log->debug(response.str().c_str());
|
//_log->debug(response.str().c_str());
|
||||||
|
Reference in New Issue
Block a user