🔨」 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/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 <vector>
#include <webserv.hpp>
class URL {
public:
URL(const std::string &url) : _full_url(url) { parse(); }
bool operator==(const URL &other) const {
return comparePathSegments(other);
}
URL(const std::string &url) : _full_url(url) { _parse(); }
bool operator<(const URL &other) const {
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::string getFullUrl(void) const { return _full_url; }
@ -37,7 +48,7 @@ class URL {
std::string getPort(void) const { return _port; }
private:
void parse() {
void _parse() {
size_t scheme_pos = _full_url.find("://");
size_t path_start = 0;
size_t query_start = _full_url.find('?');
@ -55,12 +66,12 @@ class URL {
if (path_start != std::string::npos) {
std::string path =
_full_url.substr(path_start, query_start - path_start);
splitPath(path, _path_segments);
_splitPath(path, _path_segments);
}
} else {
if (path_start != std::string::npos) {
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,
std::vector<std::string> &segments) const {
void _splitPath(const std::string &path,
std::vector<std::string> &segments) const {
std::stringstream ss(path);
std::string 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::vector<std::string> _path_segments;
std::string _query_string;