From 880af6cd6a55c5925ee2616904c1b5fabc13caa3 Mon Sep 17 00:00:00 2001 From: Talyx Date: Sat, 29 Jan 2022 12:17:26 +0300 Subject: [PATCH] rename class Header to Client --- .ccls | 2 +- Makefile | 2 +- src/{Header/Header.cpp => Client/Client.cpp} | 48 +++++++++--------- src/{Header/Header.hpp => Client/Client.hpp} | 26 +++++----- src/{Header => Client}/Request.cpp | 18 +++---- src/{Header => Client}/Request.hpp | 8 +-- .../Respons.cpp => Client/Response.cpp} | 49 ++++++++++--------- .../Respons.hpp => Client/Response.hpp} | 19 ++++--- src/Server/Server.cpp | 16 +++--- src/Server/Server.hpp | 8 +-- 10 files changed, 98 insertions(+), 98 deletions(-) rename src/{Header/Header.cpp => Client/Client.cpp} (70%) rename src/{Header/Header.hpp => Client/Client.hpp} (63%) rename src/{Header => Client}/Request.cpp (91%) rename src/{Header => Client}/Request.hpp (86%) rename src/{Header/Respons.cpp => Client/Response.cpp} (81%) rename src/{Header/Respons.hpp => Client/Response.hpp} (73%) diff --git a/.ccls b/.ccls index 76998b0..39f9341 100644 --- a/.ccls +++ b/.ccls @@ -1,6 +1,6 @@ clang++ -Iincludes --Isrc/Header/ +-Isrc/Client/ -Isrc/Autoindex/ -Isrc/Server/ -Isrc/config/ diff --git a/Makefile b/Makefile index d7ccaf7..e47562a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ CPPFLAGS += -MD -MP 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) diff --git a/src/Header/Header.cpp b/src/Client/Client.cpp similarity index 70% rename from src/Header/Header.cpp rename to src/Client/Client.cpp index 942b7e4..ddc9072 100644 --- a/src/Header/Header.cpp +++ b/src/Client/Client.cpp @@ -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 map; std::map::iterator it; std::map::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() { } diff --git a/src/Header/Header.hpp b/src/Client/Client.hpp similarity index 63% rename from src/Header/Header.hpp rename to src/Client/Client.hpp index 1d083e5..85b91ad 100644 --- a/src/Header/Header.hpp +++ b/src/Client/Client.hpp @@ -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 _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(); }; diff --git a/src/Header/Request.cpp b/src/Client/Request.cpp similarity index 91% rename from src/Header/Request.cpp rename to src/Client/Request.cpp index 83eaabe..8d2847f 100644 --- a/src/Header/Request.cpp +++ b/src/Client/Request.cpp @@ -57,9 +57,9 @@ int Request::getCode(void) { return (_ret); } -std::map Request::getHeaderFields(void) +std::map 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; } diff --git a/src/Header/Request.hpp b/src/Client/Request.hpp similarity index 86% rename from src/Header/Request.hpp rename to src/Client/Request.hpp index 753cd77..51ee437 100644 --- a/src/Header/Request.hpp +++ b/src/Client/Request.hpp @@ -21,7 +21,7 @@ private: std::string _fullURI; std::string _version; std::string _location; - std::map _headerField; + std::map _ClientField; ServerConfig *_config; @@ -36,7 +36,7 @@ public: std::string getLocation(void); ServerConfig *getConfig(void); int getCode(void); - std::map getHeaderFields(void); + std::map 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); diff --git a/src/Header/Respons.cpp b/src/Client/Response.cpp similarity index 81% rename from src/Header/Respons.cpp rename to src/Client/Response.cpp index 05f06dd..c47b6e4 100644 --- a/src/Header/Respons.cpp +++ b/src/Client/Response.cpp @@ -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::iterator it; + std::map::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::iterator it; + std::map::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() { } diff --git a/src/Header/Respons.hpp b/src/Client/Response.hpp similarity index 73% rename from src/Header/Respons.hpp rename to src/Client/Response.hpp index 9bcdeb4..1cdad14 100644 --- a/src/Header/Respons.hpp +++ b/src/Client/Response.hpp @@ -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 \ No newline at end of file diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 0a9dd80..5a639b0 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -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 header_map; + /* Client Client[MAX_CLIENT]; */ + std::map 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--; diff --git a/src/Server/Server.hpp b/src/Server/Server.hpp index 8cef036..ece1237 100644 --- a/src/Server/Server.hpp +++ b/src/Server/Server.hpp @@ -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: