Merge remote-tracking branch 'origin/fara' into roman

This commit is contained in:
3lswear
2022-02-07 20:02:20 +03:00
12 changed files with 319 additions and 97 deletions

View File

@@ -32,7 +32,6 @@ Client::Client(char *str, ServerConfig *config)
this->_config = config;
this->_buff = str;
this->_sended = 0;
}
//-------------------------------------------------GET/SET---------------------------------------
@@ -152,8 +151,29 @@ std::string Client::generateRespons(void)
len = _toSend.size();
response_len = len;
/* _to_send_char = new char[len + 1]; */
_to_send_char = (char *)malloc(sizeof(char) * (len + 1));
_to_send_char = new 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);
return (_toSend);
@@ -189,17 +209,6 @@ void Client::printClientInfo(void)
std::cout << BLUE << std::endl << "RESPONSE" << ZERO_C << std::endl << 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)
@@ -239,7 +248,7 @@ void Client::clear(void)
_headerToSend = "";
_toSend = "";
if (_to_send_char)
free(_to_send_char);
delete[] _to_send_char;
}
Client::~Client()

View File

@@ -6,6 +6,7 @@
#include "ServerConfig.hpp"
#include "Request.hpp"
#include "Response.hpp"
#include "Config.hpp"
#include <cstring>
class Client
@@ -18,7 +19,7 @@ private:
private:
int _ret;
int _fd;
struct timeval _time;
struct timeval _time;
unsigned int _sended;
char *_buff;
@@ -71,13 +72,13 @@ public:
void increaseCounter(void);
void increaseRecvCounter(unsigned int n);
std::string generateRespons(void);
std::string generateRespons(serverListen &, std::vector<ServerConfig *> &);
Client();
Client(char *);
Client(char *, ServerConfig *config);
~Client();
};
#endif

85
src/Client/Config.cpp Normal file
View File

@@ -0,0 +1,85 @@
#include "Config.hpp"
Config::Config()
{
}
int Config::calcLen(std::string &s1, std::string &s2)
{
unsigned long len = 0;
while ((len < (s1.size() )) && (len < (s2.size())))
{
if (s1[len] != s2[len])
break;
len++;
}
return (len);
}
location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
{
int max = 0;
int len = 0;
location *tmp;
std::vector<location *>::iterator it;
std::map<int, location *> compare;
it = arr.begin();
while (it != arr.end())
{
tmp = *it;
if (tmp->location == URI)
return (tmp);
len = calcLen(tmp->location, URI);
if (compare.find(len) == compare.end())
compare[len] = tmp;
if (max < len)
max = len;
it++;
}
return (compare[max]);
}
ServerConfig *Config::getConfig(std::vector<ServerConfig *> &arr, Request &request, serverListen &data)
{
ServerConfig *tmp;
std::vector<ServerConfig *>::iterator it;
std::vector<ServerConfig *> step_1;
std::vector<ServerConfig *> step_2;
it = arr.begin();
while (it != arr.end())
{
tmp = *it;
if (tmp->getPort() == data.port && tmp->getHost() == data.ip)
step_1.push_back(tmp);
it++;
}
if (step_1.size() == 1)
return (step_1[0]);
it = step_1.begin();
while (it != step_1.end())
{
tmp = *it;
if (!tmp->getServerName().empty())
step_2.push_back(tmp);
it++;
}
if (step_2.size() == 0)
return (step_1[0]);
it = step_2.begin();
while (it != step_2.end())
{
tmp = *it;
if (tmp->getServerName() == request.getHost())
return (tmp);
it++;
}
return (step_1[0]);
}
Config::~Config()
{
}

25
src/Client/Config.hpp Normal file
View File

@@ -0,0 +1,25 @@
#ifndef CONFIG_HPP
#define CONFIG_HPP
#include "webserv.hpp"
#include "ServerConfig.hpp"
#include "Request.hpp"
class Request;
class Config
{
private:
public:
Config();
~Config();
static int calcLen(std::string &, std::string &);
static location *getLocation(std::vector<location *> &, std::string &URI);
static ServerConfig *getConfig(std::vector<ServerConfig *> &, Request &request, serverListen &data);
private:
};
#endif

View File

@@ -1,9 +0,0 @@
#include "Location.hpp"
Location::Location()
{
}
Location::~Location()
{
}

View File

@@ -1,22 +0,0 @@
#ifndef LOCATION_HPP
#define LOCATION_HPP
#include "webserv.hpp"
#include "ServerConfig.hpp"
class Location
{
private:
location *loc;
public:
Location();
~Location();
void setLocation(location *location);
bool checkLocation(std::string URI);
private:
};
#endif

View File

@@ -32,39 +32,39 @@ Request::Request(char *str)
//-------------------------------------------------Get/Set---------------------------------------
std::string Request::getURI(void)
std::string &Request::getURI(void)
{
return (_URI);
}
std::string Request::getBody(void)
std::string &Request::getBody(void)
{
return (_body);
}
std::string Request::getHost(void)
std::string &Request::getHost(void)
{
return (_host);
}
std::string Request::getQuery(void)
std::string &Request::getQuery(void)
{
return(_query);
}
std::string Request::getMethod(void)
std::string &Request::getMethod(void)
{
return (_method);
}
std::string Request::getFullUri(void)
std::string &Request::getFullUri(void)
{
return (_fullURI);
}
std::string Request::getVersion(void)
std::string &Request::getVersion(void)
{
return (_version);
}
std::string Request::getLocation(void)
std::string &Request::getLocation(void)
{
return (_location);
}
std::string Request::getConnection(void)
std::string &Request::getConnection(void)
{
return (_connection);
}
@@ -155,8 +155,6 @@ int Request::parseStartLine(std::string str)
else if (_method != "GET" && _method != "POST"
&& _method != "DELETE")
_ret = 405;
else if (isFile(_fullURI) != 0 && isDir(_fullURI) != 0)
_ret = 404;
return (_ret);
}
@@ -203,7 +201,7 @@ int Request::parseClientfield(std::string str)
distance = str.find(":");
if (distance < 0 && str != "\r")
return 400;
return 200;
key = str.substr(0, distance);
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
value = str.erase(0, distance + 1);

View File

@@ -34,15 +34,15 @@ private:
bool _body_ok;
bool _chunked;
public:
std::string getURI(void);
std::string getBody(void);
std::string getHost(void);
std::string getQuery(void);
std::string getMethod(void);
std::string getFullUri(void);
std::string getVersion(void);
std::string getLocation(void);
std::string getConnection(void);
std::string &getURI(void);
std::string &getBody(void);
std::string &getHost(void);
std::string &getQuery(void);
std::string &getMethod(void);
std::string &getFullUri(void);
std::string &getVersion(void);
std::string &getLocation(void);
std::string &getConnection(void);
ServerConfig *getConfig(void);
int getCode(void);
int getLifeTime(void);

View File

@@ -26,6 +26,61 @@ void Response::setData(Request request, ServerConfig *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---------------------------------------
void Response::OpenResponseFile(const char *path)
@@ -46,6 +101,7 @@ void Response::OpenResponseFile(const char *path)
void Response::generate()
{
_fullURI = _request.getFullUri();
if (_request.badCode(_request.getCode()))
invalidClient();
else if (_request.getMethod() == "GET")
@@ -56,6 +112,40 @@ void Response::generate()
// 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---------------------------------------
std::string Response::getTime(void)
@@ -73,7 +163,7 @@ std::string Response::getTime(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("."));
if (_request.isDir(path) == 0)
@@ -107,13 +197,17 @@ std::string Response::getContentType(void)
void Response::invalidClient(void)
{
std::stringstream ss;
std::string tmp;
std::map<int, std::string>::iterator it;
//body
generateBody();
//Header
it = _errorPages.find(_code);
if (it != _errorPages.end())
OpenResponseFile(it->second.c_str());
else
_body = getErrorPage(_code);
setHeaderBlocks();
generateHeader();
DBOUT << RED << "Error Method called" << ENDL;
}
void Response::generateBody(void)
@@ -134,14 +228,15 @@ void Response::generateHeader(void)
std::stringstream ss;
std::string tmp;
ss << "HTTP/1.1" << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\n";
ss << "Content-Type: " << getContentType() << "\r\n";
ss << "Content-Length: " << _body.size() << "\r\n";
ss << "Server: poheck\r\n";
if (_request.getConnection() == "keep-alive")
ss << "Keep-Alive: timeout=" << _request.getLifeTime() << "\r\n";
ss << "Date: " << getTime() << "\r\n";
ss << "Cache-Control: no-store, no-cache, must-revalidate\r\n";
ss << "HTTP/1.1" << " " << _code << " " << getReasonPhrase(_code) << "\r\n";
ss << "Content-Type: " << _contentType << "\r\n";
ss << "Content-Length: " << _contentLength << "\r\n";
ss << "Server: " << _server << "\r\n";
if (!_keepAlive.empty())
ss << "Keep-Alive: " <<_keepAlive << "\r\n";
ss << "Date: " << _date << "\r\n";
if (!_cacheControl.empty())
ss << "Cache-Control: " << _cacheControl << "\r\n";
ss << "\r\n";
_header = ss.str();
}
@@ -150,6 +245,9 @@ void Response::methodGet(void)
{
generateBody();
setHeaderBlocks();
DBOUT << RED << _fullURI << ENDL;
DBOUT << _body.size() << ENDL;
generateHeader();
std::cout << GREEN << "GET method called\n" << ZERO_C;

View File

@@ -12,15 +12,43 @@ private:
std::string _header;
Request _request;
ServerConfig *_config;
location *_location;
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:
static std::map<std::string, std::string> _errorCode;
private:
void methodGet(void);
// void methodPost(void);
// void methodDelete(void);
void methodPost(void);
void methodDelete(void);
void invalidClient(void);
void generateHeader(void);
void generateBody(void);
@@ -32,13 +60,16 @@ public:
static std::string getReasonPhrase(std::string);
static std::string getReasonPhrase(int);
std::string getErrorPage(int code);
std::string getFullURI(std::string &, std::string &);
void setData(Request, ServerConfig *);
void setData(Request &, ServerConfig *, location *location);
public:
void OpenResponseFile(const char *path);
void initErrorCode(void);
void generate();
void generate2();
Response();
~Response();

View File

@@ -13,32 +13,32 @@ ServerConfig::ServerConfig(TOMLMap *map)
}
//--------------------------------------------------GET/SET---------------------------------------
std::string ServerConfig::getServerName(void)
std::string &ServerConfig::getServerName(void)
{
return (_serverName);
}
std::string ServerConfig::getHost(void)
std::string &ServerConfig::getHost(void)
{
return (_host);
}
int ServerConfig::getPort(void)
int &ServerConfig::getPort(void)
{
return (_port);
}
int ServerConfig::getClientBodySize(void)
int &ServerConfig::getClientBodySize(void)
{
return (_clientBodySize);
}
std::vector<location *> ServerConfig::getLocations(void)
std::vector<location *> &ServerConfig::getLocations(void)
{
return (_locations);
}
std::map<int, std::string> ServerConfig::getErrorPages(void)
std::map<int, std::string> &ServerConfig::getErrorPages(void)
{
return (_errorPages);
}

View File

@@ -16,6 +16,12 @@ struct location
std::map<int, std::string> redirect;
};
struct serverListen
{
std::string ip;
int port;
};
class ServerConfig
{
public:
@@ -39,12 +45,12 @@ public:
void setLocations(std::vector<location *>);
void setRoot(TOMLMap *);
std::string getServerName(void);
std::string getHost(void);
int getPort(void);
int getClientBodySize(void);
std::vector<location *> getLocations(void);
std::map<int, std::string> getErrorPages(void);
std::string &getServerName(void);
std::string &getHost(void);
int &getPort(void);
int &getClientBodySize(void);
std::vector<location *> &getLocations(void);
std::map<int, std::string> &getErrorPages(void);
TOMLMap *getRoot(void);
public: