mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
add: config support in client and Response class
This commit is contained in:
@@ -32,7 +32,6 @@ Client::Client(char *str, ServerConfig *config)
|
|||||||
this->_config = config;
|
this->_config = config;
|
||||||
this->_buff = str;
|
this->_buff = str;
|
||||||
this->_sended = 0;
|
this->_sended = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------GET/SET---------------------------------------
|
//-------------------------------------------------GET/SET---------------------------------------
|
||||||
@@ -152,8 +151,29 @@ std::string Client::generateRespons(void)
|
|||||||
|
|
||||||
len = _toSend.size();
|
len = _toSend.size();
|
||||||
response_len = len;
|
response_len = len;
|
||||||
/* _to_send_char = new char[len + 1]; */
|
_to_send_char = new char[len + 1];
|
||||||
_to_send_char = (char *)malloc(sizeof(char) * (len + 1));
|
std::memcpy(_to_send_char, _toSend.c_str(), len + 1);
|
||||||
|
|
||||||
|
return (_toSend);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Client::generateRespons(serverListen &reqData, std::vector<ServerConfig *> &configs)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
location *tmp;
|
||||||
|
|
||||||
|
_config = Config::getConfig(configs, _request, reqData);
|
||||||
|
tmp = Config::getLocation(_config->getLocations(), _request.getURI());
|
||||||
|
_response.setData(_request, _config, tmp);
|
||||||
|
_response.generate();
|
||||||
|
_headerToSend = _response.getHeader();
|
||||||
|
_bodyToSend = _response.getBody();
|
||||||
|
_toSend = _headerToSend + _bodyToSend;
|
||||||
|
|
||||||
|
|
||||||
|
len = _toSend.size();
|
||||||
|
response_len = len;
|
||||||
|
_to_send_char = new char[len + 1];
|
||||||
std::memcpy(_to_send_char, _toSend.c_str(), len + 1);
|
std::memcpy(_to_send_char, _toSend.c_str(), len + 1);
|
||||||
|
|
||||||
return (_toSend);
|
return (_toSend);
|
||||||
@@ -189,17 +209,6 @@ void Client::printClientInfo(void)
|
|||||||
|
|
||||||
std::cout << BLUE << std::endl << "RESPONSE" << ZERO_C << std::endl << std::endl;
|
std::cout << BLUE << std::endl << "RESPONSE" << ZERO_C << std::endl << std::endl;
|
||||||
std::cout << GREEN << _response.getHeader() << ZERO_C << std::endl;
|
std::cout << GREEN << _response.getHeader() << ZERO_C << std::endl;
|
||||||
|
|
||||||
// std::cout << YELLOW << "request Client:\n" << _buff << ZERO_C << std::endl;
|
|
||||||
// std::cout << TURGUOISE << "Client MAP" << ZERO_C << std::endl;
|
|
||||||
// for ( it = map.begin(); it != map.end() ; it++)
|
|
||||||
// {
|
|
||||||
// std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl;
|
|
||||||
// }
|
|
||||||
// std::cout << TURGUOISE << "Client BODY" << ZERO_C << std::endl;
|
|
||||||
// std::cout << GREEN << _request.getBody().size() << ZERO_C << std::endl;
|
|
||||||
/* std::cout << BLUE << _request.getBody() << ZERO_C << std::endl; */
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Client::isEmpty(void)
|
bool Client::isEmpty(void)
|
||||||
@@ -239,7 +248,7 @@ void Client::clear(void)
|
|||||||
_headerToSend = "";
|
_headerToSend = "";
|
||||||
_toSend = "";
|
_toSend = "";
|
||||||
if (_to_send_char)
|
if (_to_send_char)
|
||||||
free(_to_send_char);
|
delete[] _to_send_char;
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::~Client()
|
Client::~Client()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "ServerConfig.hpp"
|
#include "ServerConfig.hpp"
|
||||||
#include "Request.hpp"
|
#include "Request.hpp"
|
||||||
#include "Response.hpp"
|
#include "Response.hpp"
|
||||||
|
#include "Config.hpp"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
class Client
|
class Client
|
||||||
@@ -71,13 +72,13 @@ public:
|
|||||||
void increaseCounter(void);
|
void increaseCounter(void);
|
||||||
void increaseRecvCounter(unsigned int n);
|
void increaseRecvCounter(unsigned int n);
|
||||||
std::string generateRespons(void);
|
std::string generateRespons(void);
|
||||||
|
std::string generateRespons(serverListen &, std::vector<ServerConfig *> &);
|
||||||
|
|
||||||
Client();
|
Client();
|
||||||
Client(char *);
|
Client(char *);
|
||||||
Client(char *, ServerConfig *config);
|
Client(char *, ServerConfig *config);
|
||||||
~Client();
|
~Client();
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -26,6 +26,61 @@ void Response::setData(Request request, ServerConfig *config)
|
|||||||
_config = config;
|
_config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Response::setData(Request &request, ServerConfig *config, location *loc)
|
||||||
|
{
|
||||||
|
_request = request;
|
||||||
|
_code = request.getCode();
|
||||||
|
_config = config;
|
||||||
|
_location = loc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::setHeaderBlocks(void)
|
||||||
|
{
|
||||||
|
setContentType();
|
||||||
|
setContentLength();
|
||||||
|
setServer();
|
||||||
|
setConnection();
|
||||||
|
setDate();
|
||||||
|
setCacheControl();
|
||||||
|
// setLocation(void);
|
||||||
|
// setLanguage(void);
|
||||||
|
// setTransferEncoding(void);
|
||||||
|
}
|
||||||
|
void Response::setContentType(void)
|
||||||
|
{
|
||||||
|
_contentType = getContentType();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::setContentLength()
|
||||||
|
{
|
||||||
|
_contentLength = _body.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::setServer(void)
|
||||||
|
{
|
||||||
|
_server = "Poheck/1.0";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::setConnection()
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
if (_request.getConnection() == "keep-alive")
|
||||||
|
{
|
||||||
|
ss << "timeout=" << _request.getLifeTime();
|
||||||
|
_keepAlive = ss.str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::setDate(void)
|
||||||
|
{
|
||||||
|
_date = getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::setCacheControl(void)
|
||||||
|
{
|
||||||
|
_cacheControl = "no-store, no-cache, must-revalidate";
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------------------------------File---------------------------------------
|
//-------------------------------------------------File---------------------------------------
|
||||||
|
|
||||||
void Response::OpenResponseFile(const char *path)
|
void Response::OpenResponseFile(const char *path)
|
||||||
@@ -46,6 +101,7 @@ void Response::OpenResponseFile(const char *path)
|
|||||||
|
|
||||||
void Response::generate()
|
void Response::generate()
|
||||||
{
|
{
|
||||||
|
_fullURI = _request.getFullUri();
|
||||||
if (_request.badCode(_request.getCode()))
|
if (_request.badCode(_request.getCode()))
|
||||||
invalidClient();
|
invalidClient();
|
||||||
else if (_request.getMethod() == "GET")
|
else if (_request.getMethod() == "GET")
|
||||||
@@ -56,6 +112,40 @@ void Response::generate()
|
|||||||
// methodDelete();
|
// methodDelete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Response::getFullURI(std::string &str, std::string &str2)
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
unsigned long pos = str.find_last_of("/");
|
||||||
|
if (pos == str.size() - 1)
|
||||||
|
line = str.substr(0, pos - 1);
|
||||||
|
line = line + str2;
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::generate2(void)
|
||||||
|
{
|
||||||
|
_errorPages = _config->getErrorPages();
|
||||||
|
_Autoindex = _location->autoindex;
|
||||||
|
_code = _request.getCode();
|
||||||
|
_hostPort.ip = _config->getHost();
|
||||||
|
_hostPort.port = _config->getPort();
|
||||||
|
_fullURI = getFullURI(_location->root, _request.getURI());
|
||||||
|
_method = _request.getMethod();
|
||||||
|
|
||||||
|
if (_request.badCode(_code))
|
||||||
|
{
|
||||||
|
invalidClient();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_method == "GET")
|
||||||
|
methodGet();
|
||||||
|
// else if (_method == "POST")
|
||||||
|
// methodPost();
|
||||||
|
// else
|
||||||
|
// methodDelete();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------------------------------HEADER/BODY---------------------------------------
|
//-------------------------------------------------HEADER/BODY---------------------------------------
|
||||||
|
|
||||||
std::string Response::getTime(void)
|
std::string Response::getTime(void)
|
||||||
@@ -73,7 +163,7 @@ std::string Response::getTime(void)
|
|||||||
|
|
||||||
std::string Response::getContentType(void)
|
std::string Response::getContentType(void)
|
||||||
{
|
{
|
||||||
std::string path = _request.getFullUri();
|
std::string path = _fullURI;
|
||||||
std::string type = path.substr(path.rfind(".") + 1, path.size() - path.rfind("."));
|
std::string type = path.substr(path.rfind(".") + 1, path.size() - path.rfind("."));
|
||||||
|
|
||||||
if (_request.isDir(path) == 0)
|
if (_request.isDir(path) == 0)
|
||||||
@@ -107,13 +197,17 @@ std::string Response::getContentType(void)
|
|||||||
|
|
||||||
void Response::invalidClient(void)
|
void Response::invalidClient(void)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::map<int, std::string>::iterator it;
|
||||||
std::string tmp;
|
|
||||||
|
|
||||||
//body
|
it = _errorPages.find(_code);
|
||||||
generateBody();
|
if (it != _errorPages.end())
|
||||||
//Header
|
OpenResponseFile(it->second.c_str());
|
||||||
|
else
|
||||||
|
_body = getErrorPage(_code);
|
||||||
|
setHeaderBlocks();
|
||||||
generateHeader();
|
generateHeader();
|
||||||
|
|
||||||
|
DBOUT << RED << "Error Method called" << ENDL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::generateBody(void)
|
void Response::generateBody(void)
|
||||||
@@ -134,14 +228,15 @@ void Response::generateHeader(void)
|
|||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
|
|
||||||
ss << "HTTP/1.1" << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\n";
|
ss << "HTTP/1.1" << " " << _code << " " << getReasonPhrase(_code) << "\r\n";
|
||||||
ss << "Content-Type: " << getContentType() << "\r\n";
|
ss << "Content-Type: " << _contentType << "\r\n";
|
||||||
ss << "Content-Length: " << _body.size() << "\r\n";
|
ss << "Content-Length: " << _contentLength << "\r\n";
|
||||||
ss << "Server: poheck\r\n";
|
ss << "Server: " << _server << "\r\n";
|
||||||
if (_request.getConnection() == "keep-alive")
|
if (!_keepAlive.empty())
|
||||||
ss << "Keep-Alive: timeout=" << _request.getLifeTime() << "\r\n";
|
ss << "Keep-Alive: " <<_keepAlive << "\r\n";
|
||||||
ss << "Date: " << getTime() << "\r\n";
|
ss << "Date: " << _date << "\r\n";
|
||||||
ss << "Cache-Control: no-store, no-cache, must-revalidate\r\n";
|
if (!_cacheControl.empty())
|
||||||
|
ss << "Cache-Control: " << _cacheControl << "\r\n";
|
||||||
ss << "\r\n";
|
ss << "\r\n";
|
||||||
_header = ss.str();
|
_header = ss.str();
|
||||||
}
|
}
|
||||||
@@ -150,6 +245,9 @@ void Response::methodGet(void)
|
|||||||
{
|
{
|
||||||
|
|
||||||
generateBody();
|
generateBody();
|
||||||
|
setHeaderBlocks();
|
||||||
|
DBOUT << RED << _fullURI << ENDL;
|
||||||
|
DBOUT << _body.size() << ENDL;
|
||||||
generateHeader();
|
generateHeader();
|
||||||
std::cout << GREEN << "GET method called\n" << ZERO_C;
|
std::cout << GREEN << "GET method called\n" << ZERO_C;
|
||||||
|
|
||||||
|
|||||||
@@ -12,15 +12,43 @@ private:
|
|||||||
std::string _header;
|
std::string _header;
|
||||||
Request _request;
|
Request _request;
|
||||||
ServerConfig *_config;
|
ServerConfig *_config;
|
||||||
|
location *_location;
|
||||||
int _code;
|
int _code;
|
||||||
|
private:
|
||||||
|
std::map<int, std::string> _errorPages;
|
||||||
|
bool _Autoindex;
|
||||||
|
serverListen _hostPort;
|
||||||
|
std::string _fullURI;
|
||||||
|
std::string _method;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string _contentType;
|
||||||
|
unsigned int _contentLength;
|
||||||
|
std::string _server;
|
||||||
|
std::string _keepAlive;
|
||||||
|
std::string _date;
|
||||||
|
std::string _cacheControl;
|
||||||
|
std::string _locationSTR;
|
||||||
|
std::string _contentLanguage;
|
||||||
|
std::string _transferEncoding;
|
||||||
|
|
||||||
|
void setHeaderBlocks(void);
|
||||||
|
void setContentType(void);
|
||||||
|
void setContentLength(void);
|
||||||
|
void setServer(void);
|
||||||
|
void setConnection(void);
|
||||||
|
void setDate(void);
|
||||||
|
void setCacheControl(void);
|
||||||
|
void setLocation(void);
|
||||||
|
void setLanguage(void);
|
||||||
|
void setTransferEncoding(void);
|
||||||
private:
|
private:
|
||||||
static std::map<std::string, std::string> _errorCode;
|
static std::map<std::string, std::string> _errorCode;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void methodGet(void);
|
void methodGet(void);
|
||||||
// void methodPost(void);
|
void methodPost(void);
|
||||||
// void methodDelete(void);
|
void methodDelete(void);
|
||||||
void invalidClient(void);
|
void invalidClient(void);
|
||||||
void generateHeader(void);
|
void generateHeader(void);
|
||||||
void generateBody(void);
|
void generateBody(void);
|
||||||
@@ -32,13 +60,16 @@ public:
|
|||||||
static std::string getReasonPhrase(std::string);
|
static std::string getReasonPhrase(std::string);
|
||||||
static std::string getReasonPhrase(int);
|
static std::string getReasonPhrase(int);
|
||||||
std::string getErrorPage(int code);
|
std::string getErrorPage(int code);
|
||||||
|
std::string getFullURI(std::string &, std::string &);
|
||||||
|
|
||||||
|
|
||||||
void setData(Request, ServerConfig *);
|
void setData(Request, ServerConfig *);
|
||||||
|
void setData(Request &, ServerConfig *, location *location);
|
||||||
public:
|
public:
|
||||||
void OpenResponseFile(const char *path);
|
void OpenResponseFile(const char *path);
|
||||||
void initErrorCode(void);
|
void initErrorCode(void);
|
||||||
void generate();
|
void generate();
|
||||||
|
void generate2();
|
||||||
Response();
|
Response();
|
||||||
~Response();
|
~Response();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user