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

2
.ccls
View File

@@ -1,6 +1,6 @@
clang++ clang++
-Iincludes -Iincludes
-Isrc/Header/ -Isrc/Client/
-Isrc/Autoindex/ -Isrc/Autoindex/
-Isrc/Server/ -Isrc/Server/
-Isrc/config/ -Isrc/config/

View File

@@ -13,7 +13,7 @@ CPPFLAGS += -MD -MP
SRC = $(wildcard ./src/*.cpp) SRC = $(wildcard ./src/*.cpp)
SRC += $(wildcard ./src/*/*.cpp) SRC += $(wildcard ./src/*/*.cpp)
INCLUDES = ./includes/ -I src/Autoindex -I src/config -I src/Header -I src/Server INCLUDES = ./includes/ -I src/Autoindex -I src/config -I src/Client -I src/Server
OBJ = $(SRC:.cpp=.o) OBJ = $(SRC:.cpp=.o)

View File

@@ -1,20 +1,20 @@
#include "Header.hpp" #include "Client.hpp"
//-------------------------------------------------Constructors--------------------------------------- //-------------------------------------------------Constructors---------------------------------------
Header::Header() Client::Client()
{ {
this->_fd = -1; this->_fd = -1;
} }
Header::Header(char *str) Client::Client(char *str)
{ {
this->_fd = -1; this->_fd = -1;
this->_buff = str; this->_buff = str;
} }
Header::Header(char *str, ServerConfig *config) Client::Client(char *str, ServerConfig *config)
{ {
this->_fd = -1; this->_fd = -1;
this->_config = config; this->_config = config;
@@ -24,34 +24,34 @@ Header::Header(char *str, ServerConfig *config)
//-------------------------------------------------GET/SET--------------------------------------- //-------------------------------------------------GET/SET---------------------------------------
Request Header::getRequest(void) Request Client::getRequest(void)
{ {
return (_request); return (_request);
} }
Respons Header::getRespons(void) Response Client::getResponse(void)
{ {
return (_respons); return (_Response);
} }
int Header::getFd(void) int Client::getFd(void)
{ {
return _fd; return _fd;
} }
void Header::setRawData(char *str) void Client::setRawData(char *str)
{ {
this->_buff = str; this->_buff = str;
} }
void Header::setFd(int fd) void Client::setFd(int fd)
{ {
this->_fd = fd; this->_fd = fd;
} }
//-------------------------------------------------Parsing--------------------------------------- //-------------------------------------------------Parsing---------------------------------------
int Header::parseRequest(void) int Client::parseRequest(void)
{ {
_request.setData(_buff); _request.setData(_buff);
_ret = _request.parseRequest(); _ret = _request.parseRequest();
@@ -64,18 +64,18 @@ int Header::parseRequest(void)
//-------------------------------------------------SEND--------------------------------------- //-------------------------------------------------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)); return (send(fd, data.c_str(), data.length(), 0));
} }
int Header::sendRespons(int fd) int Client::sendResponse(int fd)
{ {
_respons.setData(_request, _config); _Response.setData(_request, _config);
_respons.generate(); _Response.generate();
_headerToSend = _respons.getHeader(); _ClientToSend = _Response.getClient();
_bodyToSend = _respons.getBody(); _bodyToSend = _Response.getBody();
_ret = sendData(fd, _headerToSend); _ret = sendData(fd, _ClientToSend);
_ret = sendData(fd, _bodyToSend); _ret = sendData(fd, _bodyToSend);
return (_ret); return (_ret);
@@ -85,21 +85,21 @@ int Header::sendRespons(int fd)
//-------------------------------------------------Other--------------------------------------- //-------------------------------------------------Other---------------------------------------
void Header::printHeaderInfo(void) void Client::printClientInfo(void)
{ {
std::map<std::string, std::string> map; std::map<std::string, std::string> map;
std::map<std::string, std::string>::iterator it; std::map<std::string, std::string>::iterator it;
std::map<std::string, std::string>::iterator it1; 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 method = " << _request.getMethod() << ZERO_C << std::endl;
std::cout << PINK << "request URI = " << _request.getURI() << 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 << "host = " << _request.getHost() << ZERO_C << std::endl;
std::cout << PINK << "request query = " << _request.getQuery() << 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 << 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++) for ( it = map.begin(); it != map.end() ; it++)
{ {
std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl; 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; _fd = -1;
_buff = NULL; _buff = NULL;
@@ -118,6 +118,6 @@ void Header::clear(void)
_config = NULL; _config = NULL;
} }
Header::~Header() Client::~Client()
{ {
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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