🏗️」 wip(http): work in progress, not done yet.

This commit is contained in:
y-syo
2025-03-24 16:01:56 +01:00
parent 78ee544eb3
commit f6263c8550
3 changed files with 24 additions and 17 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:21:20 by mmoussou #+# #+# */ /* Created: 2025/02/03 17:21:20 by mmoussou #+# #+# */
/* Updated: 2025/03/19 02:09:01 by mmoussou ### ########.fr */ /* Updated: 2025/03/24 15:16:39 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,6 +18,8 @@
#include <requests/HttpIMessage.hpp> #include <requests/HttpIMessage.hpp>
typedef unsigned int uint;
namespace webserv { namespace webserv {
namespace http { namespace http {
@ -26,17 +28,17 @@ public:
Response(void); Response(void);
std::string getProtocol(void) const; std::string getProtocol(void) const;
size_t getStatusCode(void) const; uint getStatusCode(void) const;
std::string getStatusText(void) const; std::string getStatusText(void) const;
void setProtocol(std::string const protocol); void setProtocol(std::string const protocol);
void setStatusCode(size_t const status_code); void setStatusCode(uint const status_code);
std::string str(void) const; std::string str(void) const;
private: private:
std::string _protocol; std::string _protocol;
size_t _status_code; uint _status_code;
std::string _status_text; std::string _status_text;
}; };

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */ /* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
/* Updated: 2025/03/24 13:10:58 by mmoussou ### ########.fr */ /* Updated: 2025/03/24 16:00:41 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,7 @@
#include <algorithm> #include <algorithm>
#include <requests/HttpRequest.hpp> #include <requests/HttpRequest.hpp>
#include <requests/Errors.hpp>
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
@ -107,7 +108,7 @@ void http::Get::parse(std::string const &data)
body_stream << line << "\n"; body_stream << line << "\n";
this->_body = body_stream.str(); this->_body = body_stream.str();
///* /*
std::cout << "-- start-line --" << std::endl; std::cout << "-- start-line --" << std::endl;
std::cout << "method: " << this->_method << std::endl; std::cout << "method: " << this->_method << std::endl;
std::cout << "target: " << this->_target << std::endl; std::cout << "target: " << this->_target << std::endl;
@ -118,13 +119,17 @@ void http::Get::parse(std::string const &data)
std::cout << it->first << ": " << it->second << std::endl; std::cout << it->first << ": " << it->second << std::endl;
std::cout << std::endl; std::cout << std::endl;
std::cout << "-- body --" << std::endl << this->_body << std::endl; std::cout << "-- body --" << std::endl << this->_body << std::endl;
//*/ */
} }
char isDirectory(const std::string& path) { char isDirectory(const std::string& path)
{
struct stat file_stat; struct stat file_stat;
if (stat(path.c_str(), &file_stat) != 0) if (stat(path.c_str(), &file_stat) != 0)
throw; {
throw std::runtime_error("can't open file (non-existant ?)");
}
return S_ISDIR(file_stat.st_mode); return S_ISDIR(file_stat.st_mode);
} }
@ -202,7 +207,7 @@ http::Response http::Get::execute(void)
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(404); response.setStatusCode(404);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");
response.setBody("<html><body>nuh uh, get 404'd >:D</body></html>"); response.setBody(http::Errors::getResponseBody(response.getStatusCode()));
} }
return (response); return (response);
@ -247,7 +252,7 @@ void http::Delete::parse(std::string const &data)
body_stream << line << "\n"; body_stream << line << "\n";
this->_body = body_stream.str(); this->_body = body_stream.str();
///* /*
std::cout << "-- start-line --" << std::endl; std::cout << "-- start-line --" << std::endl;
std::cout << "method: " << this->_method << std::endl; std::cout << "method: " << this->_method << std::endl;
std::cout << "target: " << this->_target << std::endl; std::cout << "target: " << this->_target << std::endl;
@ -258,7 +263,7 @@ void http::Delete::parse(std::string const &data)
std::cout << it->first << ": " << it->second << std::endl; std::cout << it->first << ": " << it->second << std::endl;
std::cout << std::endl; std::cout << std::endl;
std::cout << "-- body --" << std::endl << this->_body << std::endl; std::cout << "-- body --" << std::endl << this->_body << std::endl;
//*/ */
} }
http::Response http::Delete::execute(void) http::Response http::Delete::execute(void)
@ -268,7 +273,7 @@ http::Response http::Delete::execute(void)
try try
{ {
if (std::remove(this->_target.c_str())) if (std::remove(this->_target.c_str()))
throw; throw std::runtime_error("can't remove file, FF");
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(204); // this cool dude on the internet said i should not do that so i'll change it https://blog.ploeh.dk/2013/04/30/rest-lesson-learned-avoid-204-responses/ response.setStatusCode(204); // this cool dude on the internet said i should not do that so i'll change it https://blog.ploeh.dk/2013/04/30/rest-lesson-learned-avoid-204-responses/
time_t now = std::time(NULL); time_t now = std::time(NULL);
@ -281,7 +286,7 @@ http::Response http::Delete::execute(void)
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(404); response.setStatusCode(404);
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html");
response.setBody("<html><body>nuh uh, get 404'd >:D</body></html>"); response.setBody(http::Errors::getResponseBody(response.getStatusCode()));
} }
return (response); return (response);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */ /* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */
/* Updated: 2025/03/19 02:05:31 by mmoussou ### ########.fr */ /* Updated: 2025/03/24 15:17:52 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,7 +49,7 @@ std::string http::Response::getProtocol(void) const
return (this->_protocol); return (this->_protocol);
} }
size_t http::Response::getStatusCode(void) const uint http::Response::getStatusCode(void) const
{ {
return (this->_status_code); return (this->_status_code);
} }
@ -64,7 +64,7 @@ void http::Response::setProtocol(std::string const protocol)
this->_protocol = protocol; this->_protocol = protocol;
} }
void http::Response::setStatusCode(size_t const status_code) void http::Response::setStatusCode(uint const status_code)
{ {
this->_status_code = status_code; this->_status_code = status_code;
this->_status_text = Errors::message[status_code]; this->_status_text = Errors::message[status_code];