diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index d34b27c..797b6dd 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -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 &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() diff --git a/src/Client/Client.hpp b/src/Client/Client.hpp index 32927ed..16c2b79 100644 --- a/src/Client/Client.hpp +++ b/src/Client/Client.hpp @@ -6,6 +6,7 @@ #include "ServerConfig.hpp" #include "Request.hpp" #include "Response.hpp" +#include "Config.hpp" #include 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 &); Client(); Client(char *); Client(char *, ServerConfig *config); ~Client(); - }; #endif diff --git a/src/Client/Response.cpp b/src/Client/Response.cpp index d1ec40f..1894d2b 100644 --- a/src/Client/Response.cpp +++ b/src/Client/Response.cpp @@ -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::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; diff --git a/src/Client/Response.hpp b/src/Client/Response.hpp index b9fce3e..7da9125 100644 --- a/src/Client/Response.hpp +++ b/src/Client/Response.hpp @@ -12,15 +12,43 @@ private: std::string _header; Request _request; ServerConfig *_config; + location *_location; int _code; +private: + std::map _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 _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();