🔨」 fix: fixed url

This commit is contained in:
2025-04-30 14:54:34 +02:00
parent 57f681ddf0
commit 98955fc131
5 changed files with 48 additions and 15 deletions

View File

@ -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/29 15:48:19 by adjoly ### ########.fr */ /* Updated: 2025/04/30 14:38:03 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,6 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <webserv.hpp>
class URL { class URL {
public: public:
@ -31,9 +30,11 @@ class URL {
return _full_url < other._full_url; return _full_url < other._full_url;
} }
std::vector<std::string> getSegments(void) { 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; }
std::string getQueryString(void) const { return _query_string; } std::string getQueryString(void) const { return _query_string; }
std::string getPort(void) const { return _port; }
private: private:
void parse() { void parse() {
@ -97,7 +98,6 @@ class URL {
}; };
inline std::ostream &operator<<(std::ostream &os, const URL &URL) { inline std::ostream &operator<<(std::ostream &os, const URL &URL) {
os << URL.getFullUrl(); os << URL.getFullUrl();
return os; return os;
} }

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 09:34:49 by adjoly ### ########.fr */ /* Updated: 2025/04/30 14:52:22 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,6 +55,15 @@ class ARequest : public http::IMessage {
std::string _protocol; std::string _protocol;
config::Server *_conf; config::Server *_conf;
URL *_url; URL *_url;
std::string _sanitizeStr(std::string &str) {
std::string newStr = str;
if (str[str.size() - 1] == '\r') {
newStr.erase(str.size() - 1);
}
return newStr;
}
}; };
} // namespace http } // namespace http

View File

@ -6,13 +6,14 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */ /* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
/* Updated: 2025/04/30 09:37:38 by adjoly ### ########.fr */ /* Updated: 2025/04/30 14:51:39 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#pragma once #pragma once
#include <config/default.hpp> #include <config/default.hpp>
#include <cstdio>
#include <netinet/in.h> #include <netinet/in.h>
#include <requests/default.hpp> #include <requests/default.hpp>
#include <server/Server.hpp> #include <server/Server.hpp>
@ -43,11 +44,21 @@ class Client {
private: private:
void _getRequest(std::string); void _getRequest(std::string);
std::string _sanitizeStr(std::string &str) {
std::string newStr = str;
if (str[str.size() - 1] == '\r') {
newStr.erase(str.size() - 1);
}
return newStr;
}
struct pollfd *_pfd; struct pollfd *_pfd;
struct sockaddr_in _client_addr; struct sockaddr_in _client_addr;
http::ARequest *_request; http::ARequest *_request;
// http::Response *_response; // http::Response *_response;
config::Server *_conf; config::Server *_conf;
config::Route *_route;
config::Config *_Gconf; config::Config *_Gconf;
}; };

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 09:41:41 by adjoly ### ########.fr */ /* Updated: 2025/04/30 14:54:23 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,6 +28,9 @@ void Get::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;
_method = _sanitizeStr(_method);
_target = _sanitizeStr(_target);
_protocol = _sanitizeStr(_protocol);
/* this->_target.insert(this->_target.begin(), '.'); */ /* this->_target.insert(this->_target.begin(), '.'); */
} }
@ -36,7 +39,7 @@ void Get::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)));
} }
} }
@ -46,6 +49,9 @@ void Get::parse(std::string const &data) {
this->_body = body_stream.str(); this->_body = body_stream.str();
_url = new URL("http://" + _headers["Host"] + _target); _url = new URL("http://" + _headers["Host"] + _target);
std::cout << "wtf = " << _headers["Host"] << std::endl;
std::cout << *_url << std::endl; std::cout << *_url << std::endl;
/* /*

View File

@ -6,11 +6,12 @@
/* 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/29 17:27:49 by adjoly ### ########.fr */ /* Updated: 2025/04/30 13:48:03 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include "requests/default.hpp"
#include <log.hpp> #include <log.hpp>
#include <server/Client.hpp> #include <server/Client.hpp>
#include <sstream> #include <sstream>
@ -44,15 +45,15 @@ void Client::parse(void) {
received_data += std::string(buffer, bytes_received); received_data += std::string(buffer, bytes_received);
} while (buffer[bytes_received]); } while (buffer[bytes_received]);
this->_getRequest(received_data); _getRequest(received_data);
this->_conf = _Gconf->getServer(this->_request->getHeaders()["Host"]); _conf = _Gconf->getServer(this->_request->getHeaders()["Host"]);
// if (received_data.length > (get max_body_size from Route corresponding) ) // if (received_data.length > (get max_body_size from Route corresponding) )
// throw error // throw error
} }
bool Client::requestParsed(void) { bool Client::requestParsed(void) {
if (_request == not_nullptr) { if (_request == not_nullptr) {
return false; return false;
} }
@ -63,6 +64,12 @@ 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 (_isAllowed(method)) { */
/* _request = new http::Get(); */
/* _request->setMethod("405"); */
/* _log->info("405 methods not allowed"); // TODO make a better log print */
/* } */
if (method == "GET") { 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");