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

View File

@@ -1,20 +1,20 @@
#include "Header.hpp"
#include "Client.hpp"
//-------------------------------------------------Constructors---------------------------------------
Header::Header()
Client::Client()
{
this->_fd = -1;
}
Header::Header(char *str)
Client::Client(char *str)
{
this->_fd = -1;
this->_buff = str;
}
Header::Header(char *str, ServerConfig *config)
Client::Client(char *str, ServerConfig *config)
{
this->_fd = -1;
this->_config = config;
@@ -24,34 +24,34 @@ Header::Header(char *str, ServerConfig *config)
//-------------------------------------------------GET/SET---------------------------------------
Request Header::getRequest(void)
Request Client::getRequest(void)
{
return (_request);
}
Respons Header::getRespons(void)
Response Client::getResponse(void)
{
return (_respons);
return (_Response);
}
int Header::getFd(void)
int Client::getFd(void)
{
return _fd;
}
void Header::setRawData(char *str)
void Client::setRawData(char *str)
{
this->_buff = str;
}
void Header::setFd(int fd)
void Client::setFd(int fd)
{
this->_fd = fd;
}
//-------------------------------------------------Parsing---------------------------------------
int Header::parseRequest(void)
int Client::parseRequest(void)
{
_request.setData(_buff);
_ret = _request.parseRequest();
@@ -64,18 +64,18 @@ int Header::parseRequest(void)
//-------------------------------------------------SEND---------------------------------------
int Header::sendData(int fd, std::string data)
int Client::sendData(int fd, std::string data)
{
return (send(fd, data.c_str(), data.length(), 0));
}
int Header::sendRespons(int fd)
int Client::sendResponse(int fd)
{
_respons.setData(_request, _config);
_respons.generate();
_headerToSend = _respons.getHeader();
_bodyToSend = _respons.getBody();
_ret = sendData(fd, _headerToSend);
_Response.setData(_request, _config);
_Response.generate();
_ClientToSend = _Response.getClient();
_bodyToSend = _Response.getBody();
_ret = sendData(fd, _ClientToSend);
_ret = sendData(fd, _bodyToSend);
return (_ret);
@@ -85,21 +85,21 @@ int Header::sendRespons(int fd)
//-------------------------------------------------Other---------------------------------------
void Header::printHeaderInfo(void)
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.getHeaderFields();
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 header:\n" << _buff << ZERO_C << std::endl;
std::cout << YELLOW << "request Client:\n" << _buff << ZERO_C << std::endl;
std::cout << TURGUOISE << "HEADER MAP" << 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;
@@ -108,7 +108,7 @@ void Header::printHeaderInfo(void)
}
void Header::clear(void)
void Client::clear(void)
{
_fd = -1;
_buff = NULL;
@@ -118,6 +118,6 @@ void Header::clear(void)
_config = NULL;
}
Header::~Header()
Client::~Client()
{
}

View File

@@ -1,17 +1,17 @@
#ifndef HEADER_HPP
# define HEADER_HPP
#ifndef CLIENT_HPP
# define CLIENT_HPP
#include "webserv.hpp"
#include "Autoindex.hpp"
#include "ServerConfig.hpp"
#include "Request.hpp"
#include "Respons.hpp"
#include "Response.hpp"
class Header
class Client
{
private:
Request _request;
Respons _respons;
Response _Response;
ServerConfig *_config;
private:
@@ -21,12 +21,12 @@ private:
std::string _bodyToSend;
std::string _headerToSend;
std::string _ClientToSend;
std::map<std::string, std::string> _errorCode;
public:
Request getRequest(void);
Respons getRespons(void);
Response getResponse(void);
void setRawData(char *);
void setFd(int);
int getFd(void);
@@ -34,16 +34,16 @@ public:
public:
int parseRequest(void);
void printHeaderInfo(void);
void printClientInfo(void);
int sendRespons(int fd);
int sendResponse(int fd);
int sendData(int , std::string data);
void clear(void);
Header();
Header(char *);
Header(char *, ServerConfig *config);
~Header();
Client();
Client(char *);
Client(char *, ServerConfig *config);
~Client();
};

View File

@@ -57,9 +57,9 @@ int Request::getCode(void)
{
return (_ret);
}
std::map<std::string, std::string> Request::getHeaderFields(void)
std::map<std::string, std::string> Request::getClientFields(void)
{
return (_headerField);
return (_ClientField);
}
void Request::setData(char *str)
@@ -118,7 +118,7 @@ int Request::parseStartLine(std::string str)
return (_ret);
}
int Request::parseHeaderfield(std::string str)
int Request::parseClientfield(std::string str)
{
int distance;
std::string key;
@@ -130,15 +130,15 @@ int Request::parseHeaderfield(std::string str)
key = str.substr(0, distance);
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
value = str.erase(0, distance + 1);
if (_headerField.find(key) != _headerField.end())
if (_ClientField.find(key) != _ClientField.end())
{
std::cout << RED << "ERROR: double header-field" << ZERO_C << std::endl;
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);
_headerField[key] = value;
_ClientField[key] = value;
}
return 200;
}
@@ -155,7 +155,7 @@ int Request::parseRequest(void)
if (_row == 0)
_ret = parseStartLine(line);
else
_ret = parseHeaderfield(line);
_ret = parseClientfield(line);
_row++;
}
if (!badCode(_ret))
@@ -168,7 +168,7 @@ int Request::parseRequest(void)
void Request::copyFromMap()
{
_host = _headerField.find("host")->second;
_host = _ClientField.find("host")->second;
}
bool Request::badCode(int code)
@@ -222,7 +222,7 @@ void Request::clear(void)
_fullURI = "";
_version = "";
_location = "";
_headerField.clear();
_ClientField.clear();
_data = NULL;
_config = NULL;
}

View File

@@ -21,7 +21,7 @@ private:
std::string _fullURI;
std::string _version;
std::string _location;
std::map<std::string, std::string> _headerField;
std::map<std::string, std::string> _ClientField;
ServerConfig *_config;
@@ -36,7 +36,7 @@ public:
std::string getLocation(void);
ServerConfig *getConfig(void);
int getCode(void);
std::map<std::string, std::string> getHeaderFields(void);
std::map<std::string, std::string> getClientFields(void);
void setConfig(ServerConfig *config);
void setData(char *);
@@ -47,10 +47,10 @@ public:
Request();
Request(char *str);
int parseStartLine(std::string);
int parseHeaderfield(std::string);
int parseClientfield(std::string);
int parseRequest(void);
void parseURI(std::string);
void printHeaderInfo(void);
void printClientInfo(void);
bool badCode(int);
int isDir(std::string path);

View File

@@ -1,24 +1,24 @@
#include "Respons.hpp"
#include "Response.hpp"
//-------------------------------------------------Constructor---------------------------------------
Respons::Respons()
Response::Response()
{
initErrorCode();
}
//-------------------------------------------------GET/SET---------------------------------------
std::string Respons::getHeader(void)
std::string Response::getClient(void)
{
return (_header);
return (_Client);
}
std::string Respons::getBody(void)
std::string Response::getBody(void)
{
return (_body);
}
void Respons::setData(Request request, ServerConfig *config)
void Response::setData(Request request, ServerConfig *config)
{
_request = request;
_config = config;
@@ -26,7 +26,7 @@ void Respons::setData(Request request, ServerConfig *config)
//-------------------------------------------------File---------------------------------------
void Respons::OpenResponsFile(const char *path)
void Response::OpenResponseFile(const char *path)
{
std::stringstream ss;
char buf[BUFFSIZE + 1] = {0};
@@ -47,10 +47,10 @@ void Respons::OpenResponsFile(const char *path)
_body = getErrorPage(403);
}
void Respons::generate()
void Response::generate()
{
if (_request.badCode(_request.getCode()))
invalidHeader();
invalidClient();
else if (_request.getMethod() == "GET")
methodGet();
// else if (_request.getMethod() == "POST")
@@ -61,31 +61,31 @@ void Respons::generate()
//-------------------------------------------------GET/SET---------------------------------------
void Respons::invalidHeader(void)
void Response::invalidClient(void)
{
std::stringstream ss;
std::string tmp;
//header
//Client
ss << _request.getVersion() << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\nContent-Type: text/html\r\n\r\n";
_header = ss.str();
_Client = ss.str();
//body
_body = getErrorPage(_request.getCode());
std::cout << RED << "Invalid Header method called\n" << ZERO_C;
std::cout << RED << "Invalid Client method called\nCODE: " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << ZERO_C << std::endl;
}
void Respons::methodGet(void)
void Response::methodGet(void)
{
std::stringstream ss;
std::string tmp;
//header
//Client
ss << _request.getVersion() << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\nContent-Type: text/html\r\n\r\n";
_header = ss.str();
_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()))
OpenResponsFile(_request.getFullUri().c_str());
OpenResponseFile(_request.getFullUri().c_str());
else
_body = getErrorPage(_request.getCode());
std::cout << GREEN << "GET method called\n" << ZERO_C;
@@ -94,7 +94,7 @@ void Respons::methodGet(void)
//-------------------------------------------------GET/SET---------------------------------------
void Respons::initErrorCode(void)
void Response::initErrorCode(void)
{
_errorCode["100"] = "Continue";
_errorCode["101"] = "Switching Protocols";
@@ -139,19 +139,20 @@ void Respons::initErrorCode(void)
_errorCode["505"] = "HTTP Version Not Supported";
}
std::string Respons::getReasonPhrase(std::string code)
std::string Response::getReasonPhrase(std::string code)
{
std::map<std::string, std::string>::iterator it;
std::map<std::string, std::string>::iterator it;
it = _errorCode.find(code);
return (it->second);
}
std::string Respons::getReasonPhrase(int code)
std::string Response::getReasonPhrase(int code)
{
std::stringstream ss;
std::string nbr;
std::map<std::string, std::string>::iterator it;
std::map<std::string, std::string>::iterator it;
ss << code;
nbr = ss.str();
@@ -159,7 +160,7 @@ std::string Respons::getReasonPhrase(int code)
return (it->second);
}
std::string Respons::getErrorPage(int code)
std::string Response::getErrorPage(int code)
{
std::stringstream ss;
std::string Page;
@@ -172,6 +173,6 @@ std::string Respons::getErrorPage(int code)
}
Respons::~Respons()
Response::~Response()
{
}

View File

@@ -1,15 +1,15 @@
#ifndef RESPONS_HPP
#define RESPONS_HPP
#ifndef RESPONSE_HPP
#define RESPONSE_HPP
#include "webserv.hpp"
#include "Request.hpp"
#include "Autoindex.hpp"
class Respons
class Response
{
private:
std::string _body;
std::string _header;
std::string _Client;
Request _request;
ServerConfig *_config;
@@ -20,10 +20,10 @@ private:
void methodGet(void);
// void methodPost(void);
// void methodDelete(void);
void invalidHeader(void);
void invalidClient(void);
public:
std::string getHeader(void);
std::string getClient(void);
std::string getBody(void);
std::string getReasonPhrase(std::string);
std::string getReasonPhrase(int);
@@ -32,14 +32,13 @@ public:
void setData(Request, ServerConfig *);
public:
void OpenResponsFile(const char *path);
void OpenResponseFile(const char *path);
void initErrorCode(void);
void generate();
Respons();
~Respons();
Response();
~Response();
};
#endif

View File

@@ -85,8 +85,8 @@ void Server::start(void)
{
Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1");
char buf[BUFFSIZE + 1] = {0};
/* Header header[MAX_CLIENT]; */
std::map<int, Header> header_map;
/* Client Client[MAX_CLIENT]; */
std::map<int, Client> Client_map;
int fd;
int client_sock;
/* int status; */
@@ -122,12 +122,12 @@ void Server::start(void)
std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl;
fd = _events[i].data.fd;
assert(recv(fd, buf, BUFFSIZE, 0) >= 0);
header_map[fd].setRawData(buf);
header_map[fd].parseRequest();
header_map[fd].printHeaderInfo();
header_map[fd].sendRespons(fd);
header_map[fd].clear();
/* std::cout << BLUE << "status is " << header_map[fd].getReasonPhrase(status) << RESET << std::endl; */
Client_map[fd].setRawData(buf);
Client_map[fd].parseRequest();
Client_map[fd].printClientInfo();
Client_map[fd].sendResponse(fd);
Client_map[fd].clear();
/* std::cout << BLUE << "status is " << Client_map[fd].getReasonPhrase(status) << RESET << std::endl; */
bzero(buf, BUFFSIZE);
close(fd);
_client--;

View File

@@ -2,12 +2,12 @@
#define SERVER_HPP
#include "webserv.hpp"
#include "Header.hpp"
#include "Client.hpp"
#include "ServerConfig.hpp"
#include "Socket.hpp"
#include "parse.hpp"
class Header;
class Client;
class Server
{
@@ -24,8 +24,8 @@ private:
private:
void checkError(int fd, std::string str);
void sendFile(std::string str);
void sendHeader(Header head, int);
void sendRespons(Header head, int);
void sendClient(Client head, int);
void sendResponse(Client head, int);
void setNonBlock(int fd);
void add_to_epoll_list(int fd);
public: