From 2f0b3bddbe4e8e9ca31bc4d52e035edefd1ab234 Mon Sep 17 00:00:00 2001 From: Talyx Date: Tue, 25 Jan 2022 18:45:15 +0300 Subject: [PATCH] add Class Respons Request --- includes/webserv.hpp | 2 + src/Autoindex/Autoindex.cpp | 10 +- src/Autoindex/Autoindex.hpp | 2 +- src/Header/Header.cpp | 308 +++++------------------------------- src/Header/Header.hpp | 40 +++-- src/Header/HeaderHandl.hpp | 14 +- src/Header/Request.cpp | 232 +++++++++++++++++++++++++++ src/Header/Request.hpp | 67 ++++++++ src/Header/Respons.cpp | 148 +++++++++++++++++ src/Header/Respons.hpp | 39 +++++ src/Server/Server.cpp | 3 +- src/Server/ServerConfig.cpp | 5 +- 12 files changed, 565 insertions(+), 305 deletions(-) create mode 100644 src/Header/Request.cpp create mode 100644 src/Header/Request.hpp create mode 100644 src/Header/Respons.cpp create mode 100644 src/Header/Respons.hpp diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 54f5f8c..954b0b4 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -12,6 +12,8 @@ #define HOME "www" #define BUFFSIZE 1024 #define MAX_CLIENT 1000 +#define WHITESPACE " \n\r\t\f\v" + #include diff --git a/src/Autoindex/Autoindex.cpp b/src/Autoindex/Autoindex.cpp index 8cf6a96..715ee9e 100644 --- a/src/Autoindex/Autoindex.cpp +++ b/src/Autoindex/Autoindex.cpp @@ -4,11 +4,11 @@ Autoindex::Autoindex() { } -std::string Autoindex::getPage(std::string path, std::string host) +std::string Autoindex::getPage(std::string path, std::string allpath, std::string host) { - std::string allpath = HOME + path; DIR *dir = opendir(allpath.c_str()); struct dirent *dirEnt; + std::string tmp; std::string page =\ "\n\ \n\ @@ -24,11 +24,11 @@ std::string Autoindex::getPage(std::string path, std::string host) << allpath << "\" directory." << ZERO_C << std::endl; return ""; } - // if (allpath[0] != '/') - // path = "/" + path; for (dirEnt = readdir(dir); dirEnt; dirEnt = readdir(dir)) { - page = page + getReference(dirEnt->d_name, path, host); + tmp = dirEnt->d_name; + if (tmp != ".." && tmp != ".") + page = page + getReference(tmp, path, host); } page += "

\n\n\n"; closedir(dir); diff --git a/src/Autoindex/Autoindex.hpp b/src/Autoindex/Autoindex.hpp index 9ed6b9b..cc16b34 100644 --- a/src/Autoindex/Autoindex.hpp +++ b/src/Autoindex/Autoindex.hpp @@ -9,7 +9,7 @@ private: public: Autoindex(); - static std::string getPage(std::string path, std::string host); + static std::string getPage(std::string path, std::string allpath, std::string host); static std::string getReference(std::string file, std::string dir, std::string host); ~Autoindex(); }; diff --git a/src/Header/Header.cpp b/src/Header/Header.cpp index 8322e4a..942b7e4 100644 --- a/src/Header/Header.cpp +++ b/src/Header/Header.cpp @@ -1,53 +1,35 @@ #include "Header.hpp" -#define WHITESPACE " \n\r\t\f\v" +//-------------------------------------------------Constructors--------------------------------------- Header::Header() { - this->_row = 0; this->_fd = -1; - this->_autoIndex = 1; - initErrorCode(); - } Header::Header(char *str) { - this->_row = 0; this->_fd = -1; this->_buff = str; - this->_autoIndex = 1; - initErrorCode(); - parseRequest(); +} + +Header::Header(char *str, ServerConfig *config) +{ + this->_fd = -1; + this->_config = config; + this->_buff = str; + } //-------------------------------------------------GET/SET--------------------------------------- - -std::string Header::getErrorPage(int code) -{ - std::stringstream ss; - std::string Page; - - ss << "" << code <<" "<< getReasonPhrase(code) <<"" - <<"

" << code <<" " << getReasonPhrase(code) <<"

" - << "
poheck/1.0.0 (KDE)
"; - Page = ss.str(); - return (Page); -} - -std::map Header::getHeaderField(void) -{ - return (this->_headerField); -} - -HeaderHandl Header::getRequest(void) +Request Header::getRequest(void) { return (_request); } -HeaderHandl Header::getRespons(void) +Respons Header::getRespons(void) { return (_respons); } @@ -69,272 +51,59 @@ void Header::setFd(int fd) //-------------------------------------------------Parsing--------------------------------------- -void Header::parseURI(std::string str) -{ - std::string tmp; - int pos; - - tmp = str; - pos = str.find("?"); - if (pos > 0) - { - _request._URI = tmp.substr(0, pos); - _request._query = tmp.erase(0, pos + 1); - } - else - _request._URI = str; - _request._fullURI = HOME + _request._URI; -} - -int Header::parseStartLine(std::string str) -{ - std::string tmp[3]; - - tmp[0] = str.substr(0, str.find(" ")); - str = str.erase(0 , str.find(" ") + 1); - tmp[1] = str.substr(0, str.find(" ")); - str = str.erase(0 , str.find(" ") + 1); - tmp[2] = str; - tmp[2].erase(tmp[2].find_last_not_of(" \n\r\t") + 1); - _request._method = tmp[0]; - parseURI(tmp[1]); - _request._version = tmp[2]; - - if (_request._version != "HTTP/1.1") - _ret = 505; - else if (_request._method != "GET" && _request._method != "POST" - && _request._method != "DELETE") - _ret = 405; - else if (isFile(_request._fullURI) != 0) - { - if (isDir(_request._fullURI) == 0 && _autoIndex) - _ret = 200; - else if (_autoIndex) - _ret = 404; - else - _ret = 403; - } - return (_ret); -} - -int Header::parseHeaderfield(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 (_headerField.find(key) != _headerField.end()) - { - std::cout << RED << "ERROR: double header-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; - } - return 200; -} - int Header::parseRequest(void) { - std::string line; - std::stringstream buffStream; - - buffStream << _buff; - _ret = 200; - while (std::getline(buffStream, line, '\n') && _ret == 200) - { - if (_row == 0) - _ret = parseStartLine(line); - else - _ret = parseHeaderfield(line); - _row++; - } - if (_ret == 200) - _request.copyData(_headerField); + _request.setData(_buff); + _ret = _request.parseRequest(); + return (_ret); } //-------------------------------------------------FILE--------------------------------------- -int Header::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 Header::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 Header::OpenResponsFile(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); - } - _fileToSend = ss.str(); - file.close(); - } - else - _fileToSend = getErrorPage(403); -} //-------------------------------------------------SEND--------------------------------------- -int Header::sendHeader(int fd) +int Header::sendData(int fd, std::string data) { - std::stringstream ss; - std::string tmp; - const char *header; - - ss << _request._version << " " << _ret << " " << getReasonPhrase(_ret) << "\r\nContent-Type: text/html\r\n\r\n"; - tmp = ss.str(); - header = tmp.c_str(); - std::cout << TURGUOISE << "Send Header\n" << YELLOW << tmp << ZERO_C; - send(fd, header, tmp.length(), 0); - return (0); + return (send(fd, data.c_str(), data.length(), 0)); } -int Header::sendRespons(int fd) +int Header::sendRespons(int fd) { - std::string path; + _respons.setData(_request, _config); + _respons.generate(); + _headerToSend = _respons.getHeader(); + _bodyToSend = _respons.getBody(); + _ret = sendData(fd, _headerToSend); + _ret = sendData(fd, _bodyToSend); - path = _request._fullURI; - sendHeader(fd); - if (_ret == 200 && isDir(path) == 0) - _fileToSend = Autoindex::getPage(_request._URI, _request._host); - else if (_ret == 200) - OpenResponsFile(path.c_str()); - else - _fileToSend = getErrorPage(_ret); - send(fd, _fileToSend.c_str(), _fileToSend.length(), 0); - - return (0); + return (_ret); } //-------------------------------------------------Error--------------------------------------- -void Header::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 Header::getReasonPhrase(std::string code) -{ - std::map::iterator it; - - it = _errorCode.find(code); - return (it->second); -} - -std::string Header::getReasonPhrase(int code) -{ - std::stringstream ss; - std::string nbr; - std::map::iterator it; - - ss << code; - nbr = ss.str(); - it = _errorCode.find(nbr); - return (it->second); -} - //-------------------------------------------------Other--------------------------------------- void Header::printHeaderInfo(void) { + std::map map; std::map::iterator it; + std::map::iterator it1; - std::cout << PINK << "request method = " << _request._method << ZERO_C << std::endl; - std::cout << PINK << "request URI = " << _request._URI << ZERO_C << std::endl; - std::cout << PINK << "host = " << _request._host << ZERO_C << std::endl; - std::cout << PINK << "request query = " << _request._query << ZERO_C << std::endl; - std::cout << PINK << "request http versioin = " << _request._version << ZERO_C << std::endl; - std::cout << PINK << "request rows = " << _row << ZERO_C << std::endl; + map = _request.getHeaderFields(); + 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 << TURGUOISE << "HEADER MAP" << ZERO_C << std::endl; - for ( it = _headerField.begin(); it != _headerField.end(); it++) + for ( it = map.begin(); it != map.end() ; it++) { std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl; + // std::cout << "1\n"; } } @@ -342,12 +111,11 @@ void Header::printHeaderInfo(void) void Header::clear(void) { _fd = -1; - _request._method = ""; - _row = 0; _buff = NULL; - _request._URI = ""; - _request._URI = ""; - _headerField.clear(); + _ret = 200; + _request.clear(); + _buff = NULL; + _config = NULL; } Header::~Header() diff --git a/src/Header/Header.hpp b/src/Header/Header.hpp index de83fd3..d4093e4 100644 --- a/src/Header/Header.hpp +++ b/src/Header/Header.hpp @@ -4,53 +4,49 @@ #include "webserv.hpp" #include "HeaderHandl.hpp" #include "Autoindex.hpp" +#include "ServerConfig.hpp" +#include "Request.hpp" +#include "Respons.hpp" class Header { private: - HeaderHandl _request; - HeaderHandl _respons; + Request _request; + Respons _respons; + ServerConfig *_config; private: - int _row; int _ret; - int _autoIndex; int _fd; char *_buff; - std::string _fileToSend; - std::map _headerField; + std::string _bodyToSend; + std::string _headerToSend; std::map _errorCode; public: - std::map getHeaderField(void); - HeaderHandl getRequest(void); - HeaderHandl getRespons(void); - std::string getReasonPhrase(std::string); - std::string getReasonPhrase(int); - std::string getErrorPage(int code); + Request getRequest(void); + Respons getRespons(void); void setRawData(char *); void setFd(int); int getFd(void); - void initErrorCode(void); - int isFile(std::string); - int isDir(std::string); - void OpenResponsFile(const char *path); - - int parseStartLine(std::string); - void parseURI(std::string); - int parseHeaderfield(std::string); - void printHeaderInfo(void); +public: int parseRequest(void); + + void printHeaderInfo(void); int sendRespons(int fd); - int sendHeader(int fd); + int sendData(int , std::string data); void clear(void); + Header(); Header(char *); + Header(char *, ServerConfig *config); ~Header(); + + }; #endif \ No newline at end of file diff --git a/src/Header/HeaderHandl.hpp b/src/Header/HeaderHandl.hpp index 828b545..4984d95 100644 --- a/src/Header/HeaderHandl.hpp +++ b/src/Header/HeaderHandl.hpp @@ -1,12 +1,22 @@ #ifndef HEADERHANDL_HPP #define HEADERHANDL_HPP -#include -#include +#include "webserv.hpp" class HeaderHandl { + public: + std::string _location; + std::string _root; + std::string _directoryFile; + std::string _uploadDir; + bool _autoindex; + bool _uploadAccept; + std::vector _methods; + std::map _redirect; +public: + std::string _type; std::string _method; std::string _URI; std::string _fullURI; diff --git a/src/Header/Request.cpp b/src/Header/Request.cpp new file mode 100644 index 0000000..83eaabe --- /dev/null +++ b/src/Header/Request.cpp @@ -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 Request::getHeaderFields(void) +{ + return (_headerField); +} + +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::parseHeaderfield(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 (_headerField.find(key) != _headerField.end()) + { + std::cout << RED << "ERROR: double header-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; + } + 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 = parseHeaderfield(line); + _row++; + } + if (!badCode(_ret)) + copyFromMap(); + return (_ret); +} + +//-------------------------------------------------Utils--------------------------------------- + + +void Request::copyFromMap() +{ + _host = _headerField.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 = ""; + _headerField.clear(); + _data = NULL; + _config = NULL; +} + +Request::~Request() +{ +} diff --git a/src/Header/Request.hpp b/src/Header/Request.hpp new file mode 100644 index 0000000..753cd77 --- /dev/null +++ b/src/Header/Request.hpp @@ -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 _headerField; + + 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 getHeaderFields(void); + + void setConfig(ServerConfig *config); + void setData(char *); + void setData(char *, ServerConfig *); + +public: + + Request(); + Request(char *str); + int parseStartLine(std::string); + int parseHeaderfield(std::string); + int parseRequest(void); + void parseURI(std::string); + void printHeaderInfo(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 \ No newline at end of file diff --git a/src/Header/Respons.cpp b/src/Header/Respons.cpp new file mode 100644 index 0000000..24312de --- /dev/null +++ b/src/Header/Respons.cpp @@ -0,0 +1,148 @@ +#include "Respons.hpp" + +//-------------------------------------------------Constructor--------------------------------------- + +Respons::Respons() +{ + initErrorCode(); +} + +//-------------------------------------------------GET/SET--------------------------------------- + +std::string Respons::getHeader(void) +{ + return (_header); +} +std::string Respons::getBody(void) +{ + return (_body); +} + +void Respons::setData(Request request, ServerConfig *config) +{ + _request = request; + _config = config; +} + +//-------------------------------------------------File--------------------------------------- + +void Respons::OpenResponsFile(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 Respons::generate() +{ + std::stringstream ss; + std::string tmp; + + ss << _request.getVersion() << " " << _request.getCode() << " " << getReasonPhrase(_request.getCode()) << "\r\nContent-Type: text/html\r\n\r\n"; + _header = ss.str(); + 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()); + else + getErrorPage(_request.getCode()); +} + + +//-------------------------------------------------GET/SET--------------------------------------- + +void Respons::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 Respons::getReasonPhrase(std::string code) +{ + std::map::iterator it; + + it = _errorCode.find(code); + return (it->second); +} + +std::string Respons::getReasonPhrase(int code) +{ + std::stringstream ss; + std::string nbr; + std::map::iterator it; + + ss << code; + nbr = ss.str(); + it = _errorCode.find(nbr); + return (it->second); +} + +std::string Respons::getErrorPage(int code) +{ + std::stringstream ss; + std::string Page; + + ss << "" << code <<" "<< getReasonPhrase(code) <<"" + <<"

" << code <<" " << getReasonPhrase(code) <<"

" + << "
poheck/1.0.0 (KDE)
"; + Page = ss.str(); + return (Page); + } + + +Respons::~Respons() +{ +} diff --git a/src/Header/Respons.hpp b/src/Header/Respons.hpp new file mode 100644 index 0000000..87422ad --- /dev/null +++ b/src/Header/Respons.hpp @@ -0,0 +1,39 @@ +#ifndef RESPONS_HPP +#define RESPONS_HPP + +#include "webserv.hpp" +#include "Request.hpp" +#include "Autoindex.hpp" + +class Respons +{ +private: + std::string _body; + std::string _header; + Request _request; + ServerConfig *_config; + +private: + std::map _errorCode; + +public: + std::string getHeader(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 OpenResponsFile(const char *path); + void initErrorCode(void); + void generate(); + Respons(); + ~Respons(); +}; + + + +#endif \ No newline at end of file diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index dbade16..0bbfa2c 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -52,8 +52,8 @@ void Server::setNonblocking(int fd) void Server::newConnection(int fd) { struct epoll_event ev; - ev.events = EPOLLIN | EPOLLOUT | EPOLLET; ev.data.fd = fd; + ev.events = EPOLLIN | EPOLLOUT | EPOLLET; epoll_ctl(_epolfd, EPOLL_CTL_ADD, fd, &ev); _client++; @@ -77,7 +77,6 @@ void Server::start(void) code = header.parseRequest(); header.printHeaderInfo(); header.sendRespons(fd_accept); - std::cout << BLUE << header.getReasonPhrase(code) << ZERO_C << std::endl; close(fd_accept); close(serverSocket.getSocketFd()); //-----------------------------------------------попытка добавить epoll------------------ diff --git a/src/Server/ServerConfig.cpp b/src/Server/ServerConfig.cpp index 10c2e84..947df84 100644 --- a/src/Server/ServerConfig.cpp +++ b/src/Server/ServerConfig.cpp @@ -255,7 +255,6 @@ void ServerConfig::fillFields(void) ret = identify(block); ++block; } - // printFields(); } void ServerConfig::printFields(void) @@ -291,14 +290,14 @@ void ServerConfig::printFields(void) } std::cout << std::endl; it3 = (*it).redirect.begin(); - std::cout << YELLOW << "redirection" << BLUE << " " << it3->first << " " << it3->second << std::endl; + std::cout << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl; ++it; std::cout << PINK << "------------------------------------------------\n"; } std::cout << GREEN << "error pages" << std::endl; while (it1 != _errorPages.end()) { - std::cout << BLUE << it1->first << " " << it1->second << std::endl; + std::cout << YELLOW << it1->first << " " << BLUE << it1->second << std::endl; ++it1; } std::cout << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C;