🏗️」 wip(requests): from map to multimap for multiple headers with same key(still tmp commit, will redo them correctly after i'm done)

This commit is contained in:
y-syo
2025-02-12 01:36:19 +01:00
parent 3041ebbc95
commit 6b2632534c
9 changed files with 197 additions and 70 deletions

View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* HttpIMessage.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 23:34:45 by mmoussou #+# #+# */
/* Updated: 2025/02/12 00:59:22 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include <requests/HttpIMessage.hpp>
using namespace webserv;
std::multimap<std::string, std::string> http::IMessage::getHeaders(void) const
{
return (this->_headers);
}
std::string http::IMessage::getBody(void) const
{
return (this->_body);
}
void http::IMessage::setHeaders(std::multimap<std::string, std::string> const headers)
{
this->_headers = headers;
}
void http::IMessage::setBody(std::string const body)
{
this->_body = body;
}
void http::IMessage::addHeader(std::string const key, std::string const value)
{
this->_headers.insert(std::make_pair(key, value));
}
void http::IMessage::rmHeader(std::string const key)
{
this->_headers.erase(key);
}

View File

@ -6,11 +6,11 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
/* Updated: 2025/02/11 10:19:54 by mmoussou ### ########.fr */
/* Updated: 2025/02/12 01:19:52 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "requests/default.hpp"
#include <requests/HttpRequest.hpp>
/*
HttpRequest parseRequest(const std::string &rawRequest)
{
@ -54,3 +54,84 @@ HttpRequest parseRequest(const std::string &rawRequest)
return request;
}
*/
/* GET response creation
HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol)
{
std::ifstream file(request.target.c_str(), std::ios::binary);
if (file)
{
this->_status_code = 200;
this->_status_text = "OK";
this->_headers["Content-Type"] = "text/html";
this->_body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}
else
{
// TODO: replace with the new predefined array of response
this->_status_code = 404;
this->_status_text = "Not Found";
this->_headers["Content-Type"] = "text/html";
this->_body = "<html><body>nuh uh, get 404'd >:D</body></html>";
}
}
*/
/*
class IRequest: public http::IMessage {
public:
virtual void parse(const http::IRequest &request) = 0;
virtual http::Response execute(void) = 0;
//std::string str(void) const;
std::string getProtocol(void) const;
size_t getStatusCode(void) const;
std::string getStatusText(void) const;
void setProtocol(std::string const protocol);
void setStatusCode(size_t const status_code);
void setStatusText(std::string const status_text);
private:
std::string _method;
std::string _target;
std::string _protocol;
};
*/
using namespace webserv;
void http::IRequest::parse(http::IRequest const &request) { (void) request; }
http::Response execute(void) { return (http::Response()); }
std::string http::IRequest::getMethod(void) const
{
return (this->_method);
}
std::string http::IRequest::getTarget(void) const
{
return (this->_target);
}
std::string http::IRequest::getProtocol(void) const
{
return (this->_protocol);
}
void http::IRequest::setMethod(std::string const method)
{
this->_method = method;
}
void http::IRequest::setTarget(std::string const target)
{
this->_target = target;
}
void http::IRequest::setProtocol(std::string const protocol)
{
this->_protocol = protocol;
}

View File

@ -6,53 +6,65 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */
/* Updated: 2025/02/11 10:20:12 by mmoussou ### ########.fr */
/* Updated: 2025/02/12 01:00:54 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "requests/HttpResponse.hpp"
/*std::string _protocol;
size_t _status_code;
std::string _status_text;
std::map<std::string, std::string> _headers;
std::string _body;*/
/*
HttpResponse::HttpResponse(const HttpRequest &request): _protocol(request.protocol)
{
std::ifstream file(request.target.c_str(), std::ios::binary);
if (file)
{
this->_status_code = 200;
this->_status_text = "OK";
this->_headers["Content-Type"] = "text/html";
this->_body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}
else
{
this->_status_code = 404;
this->_status_text = "Not Found";
this->_headers["Content-Type"] = "text/html";
this->_body = "<html><body>nuh uh, get 404'd >:D</body></html>";
}
}
- 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
*/
HttpResponse::~HttpResponse(void)
using namespace webserv;
http::Response::Response(void)
{
}
std::string HttpResponse::str(void) const
std::string http::Response::str(void) const
{
std::ostringstream response;
response << this->_protocol << " " << this->_status_code << " " << this->_status_text;
response << "\r\n";
for (std::map<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
for (std::multimap<std::string, std::string>::const_iterator it = this->_headers.begin(); it != this->_headers.end(); ++it)
response << it->first << ": " << it->second << "\r\n";
response << "\r\n";
response << this->_body;
return (response.str());
}*/
}
std::string http::Response::getProtocol(void) const
{
return (this->_protocol);
}
size_t http::Response::getStatusCode(void) const
{
return (this->_status_code);
}
std::string http::Response::getStatusText(void) const
{
return (this->_status_text);
}
void http::Response::setProtocol(std::string const protocol)
{
this->_protocol = protocol;
}
void http::Response::setStatusCode(size_t const status_code)
{
this->_status_code = status_code;
}
void http::Response::setStatusText(std::string const status_text)
{
this->_status_text = status_text;
}