🏗️」 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

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 22:13:38 by mmoussou #+# #+# */
/* Updated: 2025/02/11 22:20:53 by mmoussou ### ########.fr */
/* Updated: 2025/02/12 00:55:12 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,20 +22,20 @@ namespace http {
class IMessage {
public:
virtual std::map<std::string, std::string> getHeaders(void) const;
virtual std::string getBody(void) const;
virtual std::multimap<std::string, std::string> getHeaders(void) const;
virtual std::string getBody(void) const;
virtual void setHeaders(std::map<std::string, std::string> const headers);
virtual void setHeaders(std::multimap<std::string, std::string> const headers);
virtual void setBody(std::string const body);
virtual void addHeader(std::string const name, std::string const value);
virtual void rmHeader(std::string const name);
virtual void addHeader(std::string const key, std::string const value);
virtual void rmHeader(std::string const key);
virtual std::string str(void) const = 0;
protected:
std::map<std::string, std::string> _headers;
std::string _body;
std::multimap<std::string, std::string> _headers;
std::string _body;
};

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:23:00 by mmoussou #+# #+# */
/* Updated: 2025/02/11 22:18:34 by mmoussou ### ########.fr */
/* Updated: 2025/02/12 01:21:31 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,18 +26,18 @@ namespace http {
class IRequest: public http::IMessage {
public:
virtual void parse(const http::IRequest &request) = 0;
virtual void parse(http::IRequest const &request) = 0;
virtual http::Response execute(void) = 0;
std::string str(void) const;
//std::string str(void) const;
std::string getProtocol(void) const;
size_t getStatusCode(void) const;
std::string getStatusText(void) const;
std::string getMethod(void) const;
std::string getTarget(void) const;
std::string getProtocol(void) const;
void setMethod(std::string const method);
void setTarget(std::string const target);
void setProtocol(std::string const protocol);
void setStatusCode(size_t const status_code);
void setStatusText(std::string const status_text);
private:
std::string _method;
@ -50,7 +50,6 @@ class Get: public http::IRequest {
public:
Get(void);
Get(std::string &data);
~Get(void);
void parse(const http::IRequest &request);
@ -62,7 +61,6 @@ class Post: public http::IRequest {
public:
Post(void);
Post(std::string &data);
~Post(void);
void parse(const http::IRequest &request);
@ -74,7 +72,6 @@ class Delete: public http::IRequest {
public:
Delete(void);
Delete(std::string &data);
~Delete(void);
void parse(const http::IRequest &request);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:21:20 by mmoussou #+# #+# */
/* Updated: 2025/02/11 22:21:52 by mmoussou ### ########.fr */
/* Updated: 2025/02/12 00:50:47 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,8 @@
#ifndef __WEBSERV_REQUESTS_HTTP_RESPONSE_HPP__
# define __WEBSERV_REQUESTS_HTTP_RESPONSE_HPP__
#include <sstream>
#include <requests/HttpIMessage.hpp>
namespace webserv {
@ -22,16 +24,6 @@ namespace http {
class Response: public http::IMessage {
public:
Response(void);
/*
* ? either : (templated)
Response<Get>();
Response<Post>();
* or : (if too different ? idk tbh)
Response(Get const req);
Response(Post const req);
*/
~Response(void);
std::string getProtocol(void) const;
size_t getStatusCode(void) const;
@ -44,9 +36,9 @@ public:
std::string str(void) const;
private:
std::string _protocol;
size_t _status_code;
std::string _status_text;
std::string _protocol;
size_t _status_code;
std::string _status_text;
};

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:48:22 by mmoussou #+# #+# */
/* Updated: 2025/02/11 22:21:21 by mmoussou ### ########.fr */
/* Updated: 2025/02/12 00:11:33 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,4 +17,6 @@
#include <requests/HttpRequest.hpp>
#include <requests/HttpResponse.hpp>
using namespace webserv;
#endif // __WEBSERV_REQUESTS_DEFAULT_HPP__

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 13:29:05 by mmoussou #+# #+# */
/* Updated: 2025/02/11 15:00:15 by mmoussou ### ########.fr */
/* Updated: 2025/02/12 00:14:11 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,9 +17,9 @@
namespace webserv {
namespace server {
} // -namespace server
} // -namespace webserv
using namespace webserv;
#endif // __WEBSERV_SERVER_DEFAULT_HPP__

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 13:29:21 by mmoussou #+# #+# */
/* Updated: 2025/02/11 15:00:10 by mmoussou ### ########.fr */
/* Updated: 2025/02/12 00:11:41 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,8 +26,6 @@
namespace webserv {
} //-namespace webserv
#endif // __WEBSERV_WEBSERV_HPP__

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;
}