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

This commit is contained in:
y-syo
2025-03-17 14:33:38 +01:00
parent 28713b407a
commit 5f9fec9568
6 changed files with 316 additions and 8 deletions

View File

@ -0,0 +1,132 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Errors.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
/* Updated: 2025/03/17 14:11:44 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#ifndef __WEBSERV_REQUESTS_ERRORS_HPP__
# define __WEBSERV_REQUESTS_ERRORS_HPP__
#include <map>
#include <string>
#include <requests/HttpResponse.hpp>
namespace webserv {
namespace http {
/*
* DOES NOT WORK
* still need to do uh things but base is done at least :D
*/
class Errors {
public:
static http::Response &getRequest(int error_code);
static void setEntry(const std::string &key, int value);
private:
static std::map<int, std::string> populateMessages();
static std::map<int, std::string> message;
static std::map<int, std::string> set_error_pages;
};
/*std::map<int, std::string> Errors::populateMessages()
{
std::map<int, std::string> m;
m[100] = "Continue";
m[101] = "Switching Protocols";
m[102] = "Processing";
m[103] = "Early Hints";
m[200] = "OK";
m[201] = "Created";
m[202] = "Accepted";
m[203] = "Non-Authoritative Information";
m[204] = "No Content";
m[205] = "Reset Content";
m[206] = "Partial Content";
m[207] = "Multi-Status";
m[208] = "Already Reported";
m[226] = "IM Used";
m[300] = "Multiple Choices";
m[301] = "Moved Permanently";
m[302] = "Found";
m[303] = "See Other";
m[304] = "Not Modified";
m[305] = "Use Proxy";
m[306] = "Switch Proxy";
m[307] = "Temporary Redirect";
m[308] = "Permanent Redirect";
m[400] = "Bad Request";
m[401] = "Unauthorized";
m[402] = "Payment Required";
m[403] = "Forbidden";
m[404] = "Not Found";
m[405] = "Method Not Allowed";
m[406] = "Not Acceptable";
m[407] = "Proxy Authentication Required";
m[408] = "Request Timeout";
m[409] = "Conflict";
m[410] = "Gone";
m[411] = "Length Required";
m[412] = "Precondition Failed";
m[413] = "Payload Too Large";
m[414] = "URI Too Long";
m[415] = "Unsupported Media Type";
m[416] = "Range Not Satisfiable";
m[417] = "Expectation Failed";
m[418] = "I'm a teapot";
m[420] = "Method Failure";
m[421] = "Misdirected Request";
m[422] = "Unprocessable Entity";
m[423] = "Locked";
m[424] = "Failed Dependency";
m[426] = "Upgrade Required";
m[428] = "Precondition Required";
m[429] = "Too Many Requests";
m[431] = "Request Header Fields Too Large";
m[451] = "Unavailable For Legal Reasons";
m[500] = "Internal Server error";
m[501] = "Not Implemented";
m[502] = "Bad Gateway";
m[503] = "Service Unavailable";
m[504] = "gateway Timeout";
m[505] = "Http version not supported";
m[506] = "Varient Also negotiate";
m[507] = "Insufficient Storage";
m[508] = "Loop Detected";
m[510] = "Not Extended";
m[511] = "Network Authentication Required";
return m;
}
std::map<int, std::string> Errors::message = Errors::populateMessages();
std::map<int, std::string> Errors::set_error_pages;
http::Response &Errors::getRequest(int error_code)
{
http::Response *result = new http::Response;
if (Errors::set_error_pages.find(error_code) != Errors::set_error_pages.end())
result->setBody(Errors::set_error_pages[error_code]);
else
result->setBody("<html><body><h1>" + Errors::message[error_code] + "</h1></body></html>");
return (*result);
}*/
} // -namespace http
} // -namespace webserv
#endif // __WEBSERV_REQUESTS_ERRORS_HPP__

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:48:22 by mmoussou #+# #+# */ /* Created: 2025/02/03 15:48:22 by mmoussou #+# #+# */
/* Updated: 2025/02/12 00:11:33 by mmoussou ### ########.fr */ /* Updated: 2025/03/17 14:08:50 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,7 @@
#ifndef __WEBSERV_REQUESTS_DEFAULT_HPP__ #ifndef __WEBSERV_REQUESTS_DEFAULT_HPP__
# define __WEBSERV_REQUESTS_DEFAULT_HPP__ # define __WEBSERV_REQUESTS_DEFAULT_HPP__
#include <requests/Errors.hpp>
#include <requests/HttpRequest.hpp> #include <requests/HttpRequest.hpp>
#include <requests/HttpResponse.hpp> #include <requests/HttpResponse.hpp>

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */ /* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/02/14 12:48:51 by mmoussou ### ########.fr */ /* Updated: 2025/03/17 13:54:48 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Errors.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/17 14:08:12 by mmoussou #+# #+# */
/* Updated: 2025/03/17 14:12:37 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include <requests/Errors.hpp>
using namespace webserv;
http::Response &http::Errors::getRequest(int error_code)
{
http::Response *result = new http::Response;
if (http::Errors::set_error_pages.find(error_code) != http::Errors::set_error_pages.end())
result->setBody(Errors::set_error_pages[error_code]);
else
result->setBody("<html><body><h1>" + http::Errors::message[error_code] + "</h1></body></html>");
return (*result);
}
std::map<int, std::string> http::Errors::message = Errors::populateMessages();
std::map<int, std::string> http::Errors::set_error_pages;
std::map<int, std::string> http::Errors::populateMessages()
{
std::map<int, std::string> m;
m[100] = "Continue";
m[101] = "Switching Protocols";
m[102] = "Processing";
m[103] = "Early Hints";
m[200] = "OK";
m[201] = "Created";
m[202] = "Accepted";
m[203] = "Non-Authoritative Information";
m[204] = "No Content";
m[205] = "Reset Content";
m[206] = "Partial Content";
m[207] = "Multi-Status";
m[208] = "Already Reported";
m[226] = "IM Used";
m[300] = "Multiple Choices";
m[301] = "Moved Permanently";
m[302] = "Found";
m[303] = "See Other";
m[304] = "Not Modified";
m[305] = "Use Proxy";
m[306] = "Switch Proxy";
m[307] = "Temporary Redirect";
m[308] = "Permanent Redirect";
m[400] = "Bad Request";
m[401] = "Unauthorized";
m[402] = "Payment Required";
m[403] = "Forbidden";
m[404] = "Not Found";
m[405] = "Method Not Allowed";
m[406] = "Not Acceptable";
m[407] = "Proxy Authentication Required";
m[408] = "Request Timeout";
m[409] = "Conflict";
m[410] = "Gone";
m[411] = "Length Required";
m[412] = "Precondition Failed";
m[413] = "Payload Too Large";
m[414] = "URI Too Long";
m[415] = "Unsupported Media Type";
m[416] = "Range Not Satisfiable";
m[417] = "Expectation Failed";
m[418] = "I'm a teapot";
m[420] = "Method Failure";
m[421] = "Misdirected Request";
m[422] = "Unprocessable Entity";
m[423] = "Locked";
m[424] = "Failed Dependency";
m[426] = "Upgrade Required";
m[428] = "Precondition Required";
m[429] = "Too Many Requests";
m[431] = "Request Header Fields Too Large";
m[451] = "Unavailable For Legal Reasons";
m[500] = "Internal Server error";
m[501] = "Not Implemented";
m[502] = "Bad Gateway";
m[503] = "Service Unavailable";
m[504] = "gateway Timeout";
m[505] = "Http version not supported";
m[506] = "Varient Also negotiate";
m[507] = "Insufficient Storage";
m[508] = "Loop Detected";
m[510] = "Not Extended";
m[511] = "Network Authentication Required";
return m;
}

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/02/14 15:46:44 by mmoussou ### ########.fr */ /* Updated: 2025/03/17 14:20:31 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -126,7 +126,14 @@ http::Response http::Get::execute(void)
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(200); response.setStatusCode(200);
response.setStatusText("OK"); response.setStatusText("OK");
response.addHeader("Content-Type", "text/html"); response.addHeader("Content-Type", "text/html"); // TODO: change it to check the file extension and set it to the corresponding MIME or text/plain if unkown. we will only implement the important MIME types in the Mozilla documentation because https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types
std::ifstream file_end(this->_target.c_str(), std::ios::binary | std::ios::ate);
std::stringstream length;
length << (file_end.tellg() - file.tellg());
std::cout << length.str();
response.addHeader("Content-Length", length.str());
response.setBody(std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>())); response.setBody(std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()));
} }
catch (...) catch (...)
@ -204,14 +211,15 @@ http::Response http::Delete::execute(void)
if (std::remove(this->_target.c_str())) if (std::remove(this->_target.c_str()))
throw; throw;
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(204); 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.setStatusText("No Content"); response.setStatusText("No Content");
time_t now = std::time(NULL); time_t now = std::time(NULL);
response.addHeader("Date", std::string(std::ctime(&now))); response.addHeader("Date", std::string(std::ctime(&now)));
} }
catch (...) catch (...)
{ {
// TODO: replace with a predefined array of error pages // TODO: check errno value and get corresponding error page, check for corresponding error code : https://cdn.discordapp.com/attachments/784779058407014403/1350841524778307586/image.png?ex=67d8dd74&is=67d78bf4&hm=c030468d3862627d6402bf200960d1a15249ba2f8dac772af3283b368a77f2f5&
response.setProtocol(this->_protocol); response.setProtocol(this->_protocol);
response.setStatusCode(404); response.setStatusCode(404);
response.setStatusText("Not Found"); response.setStatusText("Not Found");
@ -222,3 +230,66 @@ http::Response http::Delete::execute(void)
return (response); return (response);
} }
// ------------------------------------------------------------------
http::Post::Post(void)
{
}
http::Post::Post(std::string &data)
{
this->parse(data);
}
void http::Post::parse(std::string const &data)
{
std::istringstream stream(data);
std::string line;
if (std::getline(stream, line))
{
std::istringstream line_stream(line);
line_stream >> this->_method >> this->_target >> this->_protocol;
this->_target.insert(this->_target.begin(), '.');
}
while (std::getline(stream, line) && line != "\r")
{
size_t delimiter_index = line.find(':');
if (delimiter_index != std::string::npos)
{
std::string key = line.substr(0, delimiter_index);
std::string value = line.substr(delimiter_index + 2);
this->_headers.insert(std::make_pair(key, value));
}
}
std::ostringstream body_stream;
while (std::getline(stream, line))
body_stream << line << "\n";
this->_body = body_stream.str();
///*
std::cout << "-- start-line --" << std::endl;
std::cout << "method: " << this->_method << std::endl;
std::cout << "target: " << this->_target << std::endl;
std::cout << "protocol: " << this->_protocol << std::endl;
std::cout << std::endl;
std::cout << "-- headers --" << std::endl;
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
std::cout << it->first << ": " << it->second << std::endl;
std::cout << std::endl;
std::cout << "-- body --" << std::endl << this->_body << std::endl;
//*/
}
http::Response http::Post::execute(void)
{
http::Response response;
// uh idk anymore
return (response);
}
// ------------------------------------------------------------------

View File

@ -6,17 +6,20 @@
/* 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/02/12 01:00:54 by mmoussou ### ########.fr */ /* Updated: 2025/03/17 14:13:13 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "requests/HttpResponse.hpp" #include <requests/HttpResponse.hpp>
/* /*
- do a map of all the status_text and get it from here, not storing them - do a map of all the status_text and get it from here, not storing them
- get error pages from an array of predefined response, that would be modified by the config - get error pages from an array of predefined response, that would be modified by the config
*/ */
// tmp, need to be cleaned
#include <iostream>
using namespace webserv; using namespace webserv;
http::Response::Response(void) http::Response::Response(void)
@ -36,6 +39,7 @@ std::string http::Response::str(void) const
response << "\r\n"; response << "\r\n";
response << this->_body; response << this->_body;
std::cout << "------------ RESPONSE -------------" << std::endl << response.str();
return (response.str()); return (response.str());
} }