rename class Header to Client

This commit is contained in:
Talyx
2022-01-29 12:17:26 +03:00
parent 6370655848
commit 880af6cd6a
10 changed files with 98 additions and 98 deletions

123
src/Client/Client.cpp Normal file
View File

@@ -0,0 +1,123 @@
#include "Client.hpp"
//-------------------------------------------------Constructors---------------------------------------
Client::Client()
{
this->_fd = -1;
}
Client::Client(char *str)
{
this->_fd = -1;
this->_buff = str;
}
Client::Client(char *str, ServerConfig *config)
{
this->_fd = -1;
this->_config = config;
this->_buff = str;
}
//-------------------------------------------------GET/SET---------------------------------------
Request Client::getRequest(void)
{
return (_request);
}
Response Client::getResponse(void)
{
return (_Response);
}
int Client::getFd(void)
{
return _fd;
}
void Client::setRawData(char *str)
{
this->_buff = str;
}
void Client::setFd(int fd)
{
this->_fd = fd;
}
//-------------------------------------------------Parsing---------------------------------------
int Client::parseRequest(void)
{
_request.setData(_buff);
_ret = _request.parseRequest();
return (_ret);
}
//-------------------------------------------------FILE---------------------------------------
//-------------------------------------------------SEND---------------------------------------
int Client::sendData(int fd, std::string data)
{
return (send(fd, data.c_str(), data.length(), 0));
}
int Client::sendResponse(int fd)
{
_Response.setData(_request, _config);
_Response.generate();
_ClientToSend = _Response.getClient();
_bodyToSend = _Response.getBody();
_ret = sendData(fd, _ClientToSend);
_ret = sendData(fd, _bodyToSend);
return (_ret);
}
//-------------------------------------------------Error---------------------------------------
//-------------------------------------------------Other---------------------------------------
void Client::printClientInfo(void)
{
std::map<std::string, std::string> map;
std::map<std::string, std::string>::iterator it;
std::map<std::string, std::string>::iterator it1;
map = _request.getClientFields();
std::cout << PINK << "request method = " << _request.getMethod() << ZERO_C << std::endl;
std::cout << PINK << "request URI = " << _request.getURI() << ZERO_C << std::endl;
std::cout << PINK << "host = " << _request.getHost() << ZERO_C << std::endl;
std::cout << PINK << "request query = " << _request.getQuery() << ZERO_C << std::endl;
std::cout << PINK << "request http versioin = " << _request.getVersion() << 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 << "1\n";
}
}
void Client::clear(void)
{
_fd = -1;
_buff = NULL;
_ret = 200;
_request.clear();
_buff = NULL;
_config = NULL;
}
Client::~Client()
{
}

51
src/Client/Client.hpp Normal file
View File

@@ -0,0 +1,51 @@
#ifndef CLIENT_HPP
# define CLIENT_HPP
#include "webserv.hpp"
#include "Autoindex.hpp"
#include "ServerConfig.hpp"
#include "Request.hpp"
#include "Response.hpp"
class Client
{
private:
Request _request;
Response _Response;
ServerConfig *_config;
private:
int _ret;
int _fd;
char *_buff;
std::string _bodyToSend;
std::string _ClientToSend;
std::map<std::string, std::string> _errorCode;
public:
Request getRequest(void);
Response getResponse(void);
void setRawData(char *);
void setFd(int);
int getFd(void);
public:
int parseRequest(void);
void printClientInfo(void);
int sendResponse(int fd);
int sendData(int , std::string data);
void clear(void);
Client();
Client(char *);
Client(char *, ServerConfig *config);
~Client();
};
#endif

232
src/Client/Request.cpp Normal file
View File

@@ -0,0 +1,232 @@
#include "Request.hpp"
//-------------------------------------------------Constructors---------------------------------------
Request::Request()
{
_row = 0;
_ret = 200;
}
Request::Request(char *str)
{
_row = 0;
_ret = 200;
_data = str;
}
//-------------------------------------------------Get/Set---------------------------------------
std::string Request::getURI(void)
{
return (_URI);
}
std::string Request::getBody(void)
{
return (_body);
}
std::string Request::getHost(void)
{
return (_host);
}
std::string Request::getQuery(void)
{
return(_query);
}
std::string Request::getMethod(void)
{
return (_method);
}
std::string Request::getFullUri(void)
{
return (_fullURI);
}
std::string Request::getVersion(void)
{
return (_version);
}
std::string Request::getLocation(void)
{
return (_location);
}
ServerConfig *Request::getConfig(void)
{
return (_config);
}
int Request::getCode(void)
{
return (_ret);
}
std::map<std::string, std::string> Request::getClientFields(void)
{
return (_ClientField);
}
void Request::setData(char *str)
{
this->_data = str;
}
void Request::setData(char *str, ServerConfig *config)
{
_data = str;
_config = config;
}
void Request::setConfig(ServerConfig *config)
{
this->_config = config;
}
//-------------------------------------------------Parsing---------------------------------------
void Request::parseURI(std::string str)
{
std::string tmp;
int pos;
tmp = str;
pos = str.find("?");
if (pos > 0)
{
_URI = tmp.substr(0, pos);
_query = tmp.erase(0, pos + 1);
}
else
_URI = str;
_fullURI = HOME + _URI;
}
int Request::parseStartLine(std::string str)
{
std::string tmp;
_method = str.substr(0, str.find(" "));
str = str.erase(0 , str.find(" ") + 1);
tmp = str.substr(0, str.find(" "));
str = str.erase(0 , str.find(" ") + 1);
_version = str;
_version.erase(_version.find_last_not_of(WHITESPACE) + 1);
parseURI(tmp);
if (_version != "HTTP/1.1")
_ret = 505;
else if (_method != "GET" && _method != "POST"
&& _method != "DELETE")
_ret = 405;
else if (isFile(_fullURI) != 0 && isDir(_fullURI) != 0)
_ret = 404;
return (_ret);
}
int Request::parseClientfield(std::string str)
{
int distance;
std::string key;
std::string value;
distance = str.find(":");
if (distance < 0 && str != "\r")
return 400;
key = str.substr(0, distance);
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
value = str.erase(0, distance + 1);
if (_ClientField.find(key) != _ClientField.end())
{
std::cout << RED << "ERROR: double Client-field" << ZERO_C << std::endl;
}
else
{
value = value.erase(0, value.find_first_not_of(WHITESPACE));
value = value.substr(0, value.find_last_not_of(WHITESPACE) + 1);
_ClientField[key] = value;
}
return 200;
}
int Request::parseRequest(void)
{
std::string line;
std::stringstream buffStream;
buffStream << _data;
_ret = 200;
while (std::getline(buffStream, line, '\n') && !badCode(_ret))
{
if (_row == 0)
_ret = parseStartLine(line);
else
_ret = parseClientfield(line);
_row++;
}
if (!badCode(_ret))
copyFromMap();
return (_ret);
}
//-------------------------------------------------Utils---------------------------------------
void Request::copyFromMap()
{
_host = _ClientField.find("host")->second;
}
bool Request::badCode(int code)
{
if (code == 200)
return (false);
return true;
}
int Request::isFile(std::string path)
{
struct stat s;
if (stat(path.c_str(), &s) == 0)
{
if (s.st_mode & S_IFDIR)
return (-1);
else if (s.st_mode & S_IFREG)
return (0);
}
else
return (-1);
return (-1);
}
int Request::isDir(std::string path)
{
struct stat s;
if (stat(path.c_str(), &s) == 0)
{
if (s.st_mode & S_IFDIR)
return (0);
else if (s.st_mode & S_IFREG)
return (-1);
}
else
return (-1);
return (-1);
}
void Request::clear(void)
{
_ret = 200;
_row = 0;
_URI = "";
_body = "";
_host = "";
_query = "";
_method = "";
_fullURI = "";
_version = "";
_location = "";
_ClientField.clear();
_data = NULL;
_config = NULL;
}
Request::~Request()
{
}

67
src/Client/Request.hpp Normal file
View File

@@ -0,0 +1,67 @@
#ifndef REQUEST_HPP
#define REQUEST_HPP
#include "ServerConfig.hpp"
#include "webserv.hpp"
class Request
{
private:
char *_data;
int _ret;
int _row;
std::string _URI;
std::string _body;
std::string _host;
std::string _query;
std::string _method;
std::string _fullURI;
std::string _version;
std::string _location;
std::map<std::string, std::string> _ClientField;
ServerConfig *_config;
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);
ServerConfig *getConfig(void);
int getCode(void);
std::map<std::string, std::string> getClientFields(void);
void setConfig(ServerConfig *config);
void setData(char *);
void setData(char *, ServerConfig *);
public:
Request();
Request(char *str);
int parseStartLine(std::string);
int parseClientfield(std::string);
int parseRequest(void);
void parseURI(std::string);
void printClientInfo(void);
bool badCode(int);
int isDir(std::string path);
int isFile(std::string path);
bool autoindexOn(void);
void copyFromMap(void);
void clear(void);
~Request();
};
#endif

178
src/Client/Response.cpp Normal file
View File

@@ -0,0 +1,178 @@
#include "Response.hpp"
//-------------------------------------------------Constructor---------------------------------------
Response::Response()
{
initErrorCode();
}
//-------------------------------------------------GET/SET---------------------------------------
std::string Response::getClient(void)
{
return (_Client);
}
std::string Response::getBody(void)
{
return (_body);
}
void Response::setData(Request request, ServerConfig *config)
{
_request = request;
_config = config;
}
//-------------------------------------------------File---------------------------------------
void Response::OpenResponseFile(const char *path)
{
std::stringstream ss;
char buf[BUFFSIZE + 1] = {0};
std::ifstream file(path);
if (file.is_open())
{
while (!file.eof())
{
file.read(buf, BUFFSIZE);
ss << buf;
memset(buf, 0, BUFFSIZE + 1);
}
_body = ss.str();
file.close();
}
else
_body = getErrorPage(403);
}
void Response::generate()
{
if (_request.badCode(_request.getCode()))
invalidClient();
else if (_request.getMethod() == "GET")
methodGet();
// else if (_request.getMethod() == "POST")
// methodPost();
// else
// methodDelete();
}
//-------------------------------------------------GET/SET---------------------------------------
void Response::invalidClient(void)
{
std::stringstream ss;
std::string tmp;
//Client
ss << _request.getVersion() << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\nContent-Type: text/html\r\n\r\n";
_Client = ss.str();
//body
_body = getErrorPage(_request.getCode());
std::cout << RED << "Invalid Client method called\nCODE: " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << ZERO_C << std::endl;
}
void Response::methodGet(void)
{
std::stringstream ss;
std::string tmp;
//Client
ss << _request.getVersion() << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\nContent-Type: text/html\r\n\r\n";
_Client = ss.str();
//body
if (!_request.badCode(_request.getCode()) && _request.isDir(_request.getFullUri()) == 0)
_body = Autoindex::getPage(_request.getURI(), _request.getFullUri(), _request.getHost());
else if (!_request.badCode(_request.getCode()))
OpenResponseFile(_request.getFullUri().c_str());
else
_body = getErrorPage(_request.getCode());
std::cout << GREEN << "GET method called\n" << ZERO_C;
}
//-------------------------------------------------GET/SET---------------------------------------
void Response::initErrorCode(void)
{
_errorCode["100"] = "Continue";
_errorCode["101"] = "Switching Protocols";
_errorCode["200"] = "OK";
_errorCode["201"] = "Created";
_errorCode["202"] = "Accepted";
_errorCode["203"] = "Non-Authoritative Information";
_errorCode["204"] = "No Content";
_errorCode["205"] = "Reset Content";
_errorCode["206"] = "Partial Content";
_errorCode["300"] = "Multiple Choices";
_errorCode["301"] = "Moved Permanently";
_errorCode["302"] = "Found";
_errorCode["303"] = "See Other";
_errorCode["304"] = "Not Modified";
_errorCode["305"] = "Use Proxy";
_errorCode["306"] = "(Unused)";
_errorCode["307"] = "Temporary Redirect";
_errorCode["400"] = "Bad Request";
_errorCode["401"] = "Unauthorized";
_errorCode["402"] = "Payment Required";
_errorCode["403"] = "Forbidden";
_errorCode["404"] = "Not Found";
_errorCode["405"] = "Method Not Allowed";
_errorCode["406"] = "Not Acceptable";
_errorCode["407"] = "Proxy Authentication Required";
_errorCode["408"] = "Request Timeout";
_errorCode["409"] = "Conflict";
_errorCode["410"] = "Gone";
_errorCode["411"] = "Length Required";
_errorCode["412"] = "Precondition Failed";
_errorCode["413"] = "Request Entity Too Large";
_errorCode["414"] = "Request-URI Too Long";
_errorCode["415"] = "Unsupported Media Type";
_errorCode["416"] = "Requested Range Not Satisfiable";
_errorCode["417"] = "Expectation Failed";
_errorCode["500"] = "Internal Server Error";
_errorCode["501"] = "Not Implemented";
_errorCode["502"] = "Bad Gateway";
_errorCode["503"] = "Service Unavailable";
_errorCode["504"] = "Gateway Timeout";
_errorCode["505"] = "HTTP Version Not Supported";
}
std::string Response::getReasonPhrase(std::string code)
{
std::map<std::string, std::string>::iterator it;
it = _errorCode.find(code);
return (it->second);
}
std::string Response::getReasonPhrase(int code)
{
std::stringstream ss;
std::string nbr;
std::map<std::string, std::string>::iterator it;
ss << code;
nbr = ss.str();
it = _errorCode.find(nbr);
return (it->second);
}
std::string Response::getErrorPage(int code)
{
std::stringstream ss;
std::string Page;
ss << "<html><head><title>" << code <<" "<< getReasonPhrase(code) <<"</title></head><body>"
<<"<center><h1>" << code <<" " << getReasonPhrase(code) <<"</h1></center> "
<< "<hr><center>poheck/1.0.0 (KDE)</center></body></html>";
Page = ss.str();
return (Page);
}
Response::~Response()
{
}

44
src/Client/Response.hpp Normal file
View File

@@ -0,0 +1,44 @@
#ifndef RESPONSE_HPP
#define RESPONSE_HPP
#include "webserv.hpp"
#include "Request.hpp"
#include "Autoindex.hpp"
class Response
{
private:
std::string _body;
std::string _Client;
Request _request;
ServerConfig *_config;
private:
std::map<std::string, std::string> _errorCode;
private:
void methodGet(void);
// void methodPost(void);
// void methodDelete(void);
void invalidClient(void);
public:
std::string getClient(void);
std::string getBody(void);
std::string getReasonPhrase(std::string);
std::string getReasonPhrase(int);
std::string getErrorPage(int code);
void setData(Request, ServerConfig *);
public:
void OpenResponseFile(const char *path);
void initErrorCode(void);
void generate();
Response();
~Response();
};
#endif