diff --git a/inc/AConfigurable.hpp b/inc/AConfigurable.hpp index f431662..2579040 100644 --- a/inc/AConfigurable.hpp +++ b/inc/AConfigurable.hpp @@ -1,24 +1,23 @@ #ifndef CONFIGURABLE_HPP #define CONFIGURABLE_HPP -#include <vector> -#include <string> -#include <map> +#include <limits.h> + #include <fstream> +#include <map> #include <sstream> -#include <limits.h> -#include "utils.hpp" +#include <string> +#include <vector> + #include "errormsg.hpp" +#include "utils.hpp" #define ONEMEGA 1000000 class AConfigurable { public: - enum IsAutoIndexed { - FALSE = 0, - TRUE = 1, - UNDEFINED = 2 - }; + enum IsAutoIndexed { FALSE = 0, TRUE = 1, UNDEFINED = 2 }; + private: protected: std::vector<std::string> _allowMethods; @@ -38,7 +37,8 @@ class AConfigurable { const IsAutoIndexed &getAutoindex() const; bool setIndex(std::string const &attribute, std::fstream &file); const std::vector<std::string> &getIndex() const; - bool setClientMaxBodySize(std::string const &attribute, std::fstream &file); + bool setClientMaxBodySize(std::string const &attribute, + std::fstream &file); const ssize_t &getClientMaxBodySize() const; bool setCgiExtensions(std::string const &attribute, std::fstream &file); const std::vector<std::string> &getCgiExtensions() const; diff --git a/inc/Config.hpp b/inc/Config.hpp index 2ee39a0..af76de7 100644 --- a/inc/Config.hpp +++ b/inc/Config.hpp @@ -1,14 +1,16 @@ #ifndef CONFIG_HPP #define CONFIG_HPP -#include <iostream> -#include <string> +#include <sys/stat.h> + #include <fstream> +#include <iostream> #include <map> -#include <sys/stat.h> +#include <string> + #include "Server.hpp" -#include "utils.hpp" #include "errormsg.hpp" +#include "utils.hpp" class Server; @@ -17,8 +19,12 @@ class Config { std::fstream _file; std::map<std::string, Server> _servers; Server _createServerInstance(std::fstream &file); - static std::map<std::string, bool (Server::*)(const std::string&, std::fstream&)> _setterMap; - static std::map<std::string, bool (Server::*)(const std::string&, std::fstream&)> initSetterMap(); + static std::map<std::string, + bool (Server::*)(const std::string &, std::fstream &)> + _setterMap; + static std::map<std::string, + bool (Server::*)(const std::string &, std::fstream &)> + initSetterMap(); public: static std::size_t lineCount; @@ -28,5 +34,4 @@ class Config { void printServers() const; }; - #endif \ No newline at end of file diff --git a/inc/Location.hpp b/inc/Location.hpp index a5a6ccc..ab340d0 100644 --- a/inc/Location.hpp +++ b/inc/Location.hpp @@ -1,11 +1,12 @@ #ifndef LOCATION_HPP #define LOCATION_HPP -#include <string> #include <fstream> #include <sstream> -#include "Server.hpp" +#include <string> + #include "AConfigurable.hpp" +#include "Server.hpp" class Server; diff --git a/inc/Response.hpp b/inc/Response.hpp index a822368..533f4a0 100644 --- a/inc/Response.hpp +++ b/inc/Response.hpp @@ -4,8 +4,8 @@ #include <string> #include "ClientSocket.hpp" -#include "Request.hpp" #include "Config.hpp" +#include "Request.hpp" class Response { // public: diff --git a/inc/Server.hpp b/inc/Server.hpp index 99a7c80..b8b4a24 100644 --- a/inc/Server.hpp +++ b/inc/Server.hpp @@ -1,13 +1,14 @@ #ifndef SERVER_HPP #define SERVER_HPP -#include <vector> -#include <string> #include <map> +#include <string> +#include <vector> + #include "AConfigurable.hpp" +#include "Config.hpp" #include "Location.hpp" #include "utils.hpp" -#include "Config.hpp" #define MIN_USERPORT 1024 #define MAX_USERPORT 49151 @@ -21,8 +22,12 @@ class Server : public AConfigurable { std::string _port; std::string _root; std::map<std::string, Location> _locations; - static std::map<std::string, bool (Location::*)(const std::string&, std::fstream&)> _setterMap; - static std::map<std::string, bool (Location::*)(const std::string&, std::fstream&)> initSetterMap(); + static std::map<std::string, + bool (Location::*)(const std::string &, std::fstream &)> + _setterMap; + static std::map<std::string, + bool (Location::*)(const std::string &, std::fstream &)> + initSetterMap(); public: Server(); diff --git a/inc/main.hpp b/inc/main.hpp index f6cd9cc..329a62c 100644 --- a/inc/main.hpp +++ b/inc/main.hpp @@ -1,11 +1,11 @@ #ifndef MAIN_HPP #define MAIN_HPP -#include "Error.hpp" #include "Config.hpp" +#include "Error.hpp" #include "Location.hpp" -#include "env.hpp" #include "ServerSocket.hpp" +#include "env.hpp" #include "loop.hpp" #endif diff --git a/inc/utils.hpp b/inc/utils.hpp index e3a94d6..a574754 100644 --- a/inc/utils.hpp +++ b/inc/utils.hpp @@ -8,13 +8,13 @@ #include "env.hpp" namespace utils { - void putSysError(char const *msg); - std::size_t decStrToSizeT(std::string &str); - std::size_t hexStrToSizeT(std::string &str); - bool findCRLF(std::stringstream &stream); - bool shouldIgnoreLine(std::string const &line); - bool rmCR(std::string &line); - bool isNumber(std::string const &str); +void putSysError(char const *msg); +std::size_t decStrToSizeT(std::string &str); +std::size_t hexStrToSizeT(std::string &str); +bool findCRLF(std::stringstream &stream); +bool shouldIgnoreLine(std::string const &line); +bool rmCR(std::string &line); +bool isNumber(std::string const &str); } // namespace utils #endif \ No newline at end of file diff --git a/src/conf/AConfigurable.cpp b/src/conf/AConfigurable.cpp index 8199c01..f5b8834 100644 --- a/src/conf/AConfigurable.cpp +++ b/src/conf/AConfigurable.cpp @@ -1,18 +1,22 @@ #include "AConfigurable.hpp" static std::vector<std::string> initAllowedMethods() { - std::vector<std::string> methods; - methods.push_back("GET"); - methods.push_back("POST"); - methods.push_back("DELETE"); - return methods; + std::vector<std::string> methods; + methods.push_back("GET"); + methods.push_back("POST"); + methods.push_back("DELETE"); + return methods; } -AConfigurable::AConfigurable() : _allowMethods(initAllowedMethods()), _autoindex(AConfigurable::FALSE), _clientMaxBodySize(ONEMEGA) {} +AConfigurable::AConfigurable() + : _allowMethods(initAllowedMethods()), + _autoindex(AConfigurable::FALSE), + _clientMaxBodySize(ONEMEGA) {} AConfigurable::~AConfigurable() {} -bool AConfigurable::setAllowMethods(std::string const &attribute, std::fstream &file) { +bool AConfigurable::setAllowMethods(std::string const &attribute, + std::fstream &file) { (void)file; std::stringstream ss(attribute); std::string elem; @@ -21,10 +25,14 @@ bool AConfigurable::setAllowMethods(std::string const &attribute, std::fstream & while (ss.peek() != EOF) { elem.clear(); ss >> elem; - if (elem.empty() == true) { return false; } - if (elem != "GET" && elem != "POST" && elem != "DELETE") { return false; } + if (elem.empty() == true) { + return false; + } + if (elem != "GET" && elem != "POST" && elem != "DELETE") { + return false; + } this->_allowMethods.push_back(elem); - ss >> std::ws; + ss >> std::ws; } return true; } @@ -32,7 +40,8 @@ bool AConfigurable::setAllowMethods(std::string const &attribute, std::fstream & const std::vector<std::string> &AConfigurable::getAllowMethods() const { return this->_allowMethods; } -bool AConfigurable::setAutoIndex(std::string const &attribute, std::fstream &file) { +bool AConfigurable::setAutoIndex(std::string const &attribute, + std::fstream &file) { (void)file; std::stringstream ss(attribute); std::string elem; @@ -40,8 +49,12 @@ bool AConfigurable::setAutoIndex(std::string const &attribute, std::fstream &fil elem.clear(); ss >> elem; ss >> std::ws; - if (elem.empty() == true) { return false; } - if (ss.peek() != EOF) { return false; } + if (elem.empty() == true) { + return false; + } + if (ss.peek() != EOF) { + return false; + } if (elem == "on") { this->_autoindex = AConfigurable::TRUE; } else if (elem == "off") { @@ -64,7 +77,9 @@ bool AConfigurable::setIndex(std::string const &attribute, std::fstream &file) { while (ss.peek() != EOF) { elem.clear(); ss >> elem; - if (elem.empty() == true) { return false; } + if (elem.empty() == true) { + return false; + } this->_index.push_back(elem); ss >> std::ws; } @@ -75,7 +90,8 @@ const std::vector<std::string> &AConfigurable::getIndex() const { return this->_index; } -bool AConfigurable::setClientMaxBodySize(std::string const &attribute, std::fstream &file) { +bool AConfigurable::setClientMaxBodySize(std::string const &attribute, + std::fstream &file) { (void)file; std::stringstream ss(attribute); std::string elem; @@ -83,11 +99,19 @@ bool AConfigurable::setClientMaxBodySize(std::string const &attribute, std::fstr elem.clear(); ss >> elem; ss >> std::ws; - if (ss.peek() != EOF) { return false; } - if (elem.empty() == true) { return false; } - if (utils::isNumber(elem) == false) { return false; } + if (ss.peek() != EOF) { + return false; + } + if (elem.empty() == true) { + return false; + } + if (utils::isNumber(elem) == false) { + return false; + } this->_clientMaxBodySize = utils::decStrToSizeT(elem); - if (this->_clientMaxBodySize > INT_MAX) { throw std::runtime_error(INVALID_CLIENTMAXBODYSIZE); } + if (this->_clientMaxBodySize > INT_MAX) { + throw std::runtime_error(INVALID_CLIENTMAXBODYSIZE); + } return true; } @@ -95,7 +119,8 @@ const ssize_t &AConfigurable::getClientMaxBodySize() const { return this->_clientMaxBodySize; } -bool AConfigurable::setCgiExtensions(std::string const &attribute, std::fstream &file) { +bool AConfigurable::setCgiExtensions(std::string const &attribute, + std::fstream &file) { (void)file; std::stringstream ss(attribute); std::string elem; @@ -104,11 +129,17 @@ bool AConfigurable::setCgiExtensions(std::string const &attribute, std::fstream while (ss.peek() != EOF) { elem.clear(); ss >> elem; - if (elem.empty() == true) { return false; } - if (elem[0] != '.') { return false; }; - for (size_t i = 1; i < elem.length(); ++i) { - if (!std::isalnum(elem[i])) { return false; } - } + if (elem.empty() == true) { + return false; + } + if (elem[0] != '.') { + return false; + }; + for (size_t i = 1; i < elem.length(); ++i) { + if (!std::isalnum(elem[i])) { + return false; + } + } this->_cgi_extensions.push_back(elem); ss >> std::ws; } @@ -119,7 +150,8 @@ const std::vector<std::string> &AConfigurable::getCgiExtensions() const { return this->_cgi_extensions; } -bool AConfigurable::setReturn(std::string const &attribute, std::fstream &file) { +bool AConfigurable::setReturn(std::string const &attribute, + std::fstream &file) { (void)file; std::stringstream ss(attribute); std::string elem; @@ -127,25 +159,36 @@ bool AConfigurable::setReturn(std::string const &attribute, std::fstream &file) elem.clear(); ss >> elem; ss >> std::ws; - if (elem.empty() == true) { return false; } - if (ss.peek() != EOF) { return false; } + if (elem.empty() == true) { + return false; + } + if (ss.peek() != EOF) { + return false; + } ss.str(""); ss.clear(); ss << elem; std::string protocol; std::getline(ss, protocol, ':'); - if (protocol.compare("http") != 0 && protocol.compare("https") != 0) { return false ;} + if (protocol.compare("http") != 0 && protocol.compare("https") != 0) { + return false; + } this->_return = elem; return true; } static bool isStatusCode(std::string const &str) { - if (str.size() != 3) { return false; } - if (utils::isNumber(str) == false) { return false; } + if (str.size() != 3) { + return false; + } + if (utils::isNumber(str) == false) { + return false; + } return true; } -bool AConfigurable::setErrorPages(std::string const &attribute, std::fstream &file) { +bool AConfigurable::setErrorPages(std::string const &attribute, + std::fstream &file) { (void)file; std::stringstream ss(attribute); std::string elem; @@ -154,16 +197,26 @@ bool AConfigurable::setErrorPages(std::string const &attribute, std::fstream &fi elem.clear(); while (true) { ss >> elem; - if (elem.empty() == true) { return false; } - if (isStatusCode(elem) == false) { break; } + if (elem.empty() == true) { + return false; + } + if (isStatusCode(elem) == false) { + break; + } statusCodes.push_back(elem); elem.clear(); } ss >> std::ws; - if (ss.peek() != EOF) { return false; } - if (statusCodes.size() == 0) { return false; } - for (std::vector<std::string>::iterator iter = statusCodes.begin(); iter != statusCodes.end(); ++iter) { - this->_errorPages.insert(std::pair<std::string, std::string>(*iter, elem)); + if (ss.peek() != EOF) { + return false; + } + if (statusCodes.size() == 0) { + return false; + } + for (std::vector<std::string>::iterator iter = statusCodes.begin(); + iter != statusCodes.end(); ++iter) { + this->_errorPages.insert( + std::pair<std::string, std::string>(*iter, elem)); } return true; } @@ -172,6 +225,4 @@ const std::map<std::string, std::string> &AConfigurable::getErrorPages() const { return this->_errorPages; } -const std::string &AConfigurable::getReturn() const { - return this->_return; -} +const std::string &AConfigurable::getReturn() const { return this->_return; } diff --git a/src/conf/Config.cpp b/src/conf/Config.cpp index f407b3e..0faeb4a 100644 --- a/src/conf/Config.cpp +++ b/src/conf/Config.cpp @@ -2,8 +2,10 @@ std::size_t Config::lineCount = 0; -std::map<std::string, bool (Server::*)(std::string const&, std::fstream&)> Config::initSetterMap() { - std::map<std::string, bool (Server::*)(const std::string&, std::fstream&)> srvSetterMap; +std::map<std::string, bool (Server::*)(std::string const &, std::fstream &)> +Config::initSetterMap() { + std::map<std::string, bool (Server::*)(const std::string &, std::fstream &)> + srvSetterMap; srvSetterMap["server_name"] = &Server::setServername; srvSetterMap["listen"] = &Server::setListen; srvSetterMap["root"] = &Server::setRoot; @@ -18,21 +20,23 @@ std::map<std::string, bool (Server::*)(std::string const&, std::fstream&)> Confi return srvSetterMap; } -std::map<std::string, bool (Server::*)(std::string const&, std::fstream&)> Config::_setterMap = initSetterMap(); +std::map<std::string, bool (Server::*)(std::string const &, std::fstream &)> + Config::_setterMap = initSetterMap(); bool Config::open(char const *path) { struct stat statbuf; - + if (stat(path, &statbuf) != 0) { utils::putSysError("stat"); return false; } if (S_ISDIR(statbuf.st_mode) == true) { - std::cerr << RED << "Webserv: Error: a file path is directory." << RESET << std::endl; + std::cerr << RED << "Webserv: Error: a file path is directory." << RESET + << std::endl; return false; } this->_file.open(path, std::ios::in); - if (this->_file.fail()) { + if (this->_file.fail()) { utils::putSysError("open"); return false; } @@ -56,21 +60,37 @@ Server Config::_createServerInstance(std::fstream &file) { while (std::getline(file, line)) { Config::lineCount++; - if (utils::shouldIgnoreLine(line)) { continue; } + if (utils::shouldIgnoreLine(line)) { + continue; + } utils::rmCR(line); std::stringstream ss(line); std::string elem; ss >> elem; - if (elem.compare("}") == 0) { bracketFlag = true; break; } - std::map<std::string, bool (Server::*)(std::string const&, std::fstream&)>::iterator iter = this->_setterMap.find(elem); - if (iter == this->_setterMap.end()) { throw std::runtime_error(INVALID_ITEM); } - if (iter->first.compare("location") != 0 && line[line.size() - 1] != ';') { - throw std::runtime_error(SYNTAX_ERROR); + if (elem.compare("}") == 0) { + bracketFlag = true; + break; + } + std::map<std::string, bool (Server::*)(std::string const &, + std::fstream &)>::iterator iter = + this->_setterMap.find(elem); + if (iter == this->_setterMap.end()) { + throw std::runtime_error(INVALID_ITEM); + } + if (iter->first.compare("location") != 0 && + line[line.size() - 1] != ';') { + throw std::runtime_error(SYNTAX_ERROR); } - if (iter->first.compare("location") != 0 && line.size() != 0) { line = line.substr(0, line.size() - 1); } - if ((server.*(iter->second))(line, file) == false) { throw std::runtime_error(SYNTAX_ERROR); } + if (iter->first.compare("location") != 0 && line.size() != 0) { + line = line.substr(0, line.size() - 1); + } + if ((server.*(iter->second))(line, file) == false) { + throw std::runtime_error(SYNTAX_ERROR); + } + } + if (bracketFlag == false) { + throw std::runtime_error(SYNTAX_ERROR); } - if (bracketFlag == false) { throw std::runtime_error(SYNTAX_ERROR); } server.fillLocationDirectives(); return server; } @@ -80,7 +100,9 @@ bool Config::load() { try { while (std::getline(this->_file, line)) { Config::lineCount++; - if (utils::shouldIgnoreLine(line)) { continue; } + if (utils::shouldIgnoreLine(line)) { + continue; + } utils::rmCR(line); std::stringstream ss(line); std::string elem; @@ -89,75 +111,105 @@ bool Config::load() { ss >> bracket; ss >> std::ws; if (bracket != '{' || ss.peek() != EOF) { - throw std::runtime_error(SYNTAX_ERROR); + throw std::runtime_error(SYNTAX_ERROR); } if (elem.compare("server") == 0 && bracket == '{') { Server server = this->_createServerInstance(this->_file); if (this->_servers.count(server.getServername()) > 0) { throw std::runtime_error(DUPULICATE_SERVER); } - this->_servers.insert(std::pair<std::string, Server>(server.getServername(), server)); + this->_servers.insert(std::pair<std::string, Server>( + server.getServername(), server)); } } if (this->_servers.size() == 0) { throw std::runtime_error(NO_SERVER); } } catch (std::exception &e) { - std::cerr << RED << "Webserv: " << "Line " << Config::lineCount << ": Error: " << e.what() << RESET << std::endl; + std::cerr << RED << "Webserv: " + << "Line " << Config::lineCount << ": Error: " << e.what() + << RESET << std::endl; return false; } return true; } void Config::printServers() const { - for (std::map<std::string, Server>::const_iterator iter = this->_servers.begin(); iter != this->_servers.end(); ++iter) { + for (std::map<std::string, Server>::const_iterator iter = + this->_servers.begin(); + iter != this->_servers.end(); ++iter) { std::clog << "====================================" << std::endl; - std::clog << "Server name: " << iter->second.getServername() << std::endl; + std::clog << "Server name: " << iter->second.getServername() + << std::endl; std::clog << "Listen: " << iter->second.getListen() << std::endl; std::clog << "Root: " << iter->second.getRoot() << std::endl; std::clog << "Allow methods: "; - for (std::vector<std::string>::const_iterator iter2 = iter->second.getAllowMethods().begin(); iter2 != iter->second.getAllowMethods().end(); ++iter2) { + for (std::vector<std::string>::const_iterator iter2 = + iter->second.getAllowMethods().begin(); + iter2 != iter->second.getAllowMethods().end(); ++iter2) { std::clog << *iter2 << " "; } - std::clog << std::endl; + std::clog << std::endl; std::clog << "Index: "; - for (std::vector<std::string>::const_iterator iter2 = iter->second.getIndex().begin(); iter2 != iter->second.getIndex().end(); ++iter2) { + for (std::vector<std::string>::const_iterator iter2 = + iter->second.getIndex().begin(); + iter2 != iter->second.getIndex().end(); ++iter2) { std::clog << *iter2 << " "; } std::clog << std::endl; std::clog << "Autoindex: " << iter->second.getAutoindex() << std::endl; - std::clog << "Client max body size: " << iter->second.getClientMaxBodySize() << std::endl; + std::clog << "Client max body size: " + << iter->second.getClientMaxBodySize() << std::endl; std::clog << "Cgi extensions: "; - for (std::vector<std::string>::const_iterator iter2 = iter->second.getCgiExtensions().begin(); iter2 != iter->second.getCgiExtensions().end(); ++iter2) { + for (std::vector<std::string>::const_iterator iter2 = + iter->second.getCgiExtensions().begin(); + iter2 != iter->second.getCgiExtensions().end(); ++iter2) { std::clog << *iter2 << " "; } std::clog << std::endl; std::clog << "Return: " << iter->second.getReturn() << std::endl; - for (std::map<std::string, std::string>::const_iterator iter2 = iter->second.getErrorPages().begin(); iter2 != iter->second.getErrorPages().end(); ++iter2) { - std::clog << "Error page: [" << iter2->first << "] " << iter2->second << std::endl; + for (std::map<std::string, std::string>::const_iterator iter2 = + iter->second.getErrorPages().begin(); + iter2 != iter->second.getErrorPages().end(); ++iter2) { + std::clog << "Error page: [" << iter2->first << "] " + << iter2->second << std::endl; } - for (std::map<std::string, Location>::const_iterator iter2 = iter->second.getLocations().begin(); iter2 != iter->second.getLocations().end(); ++iter2) { - std::clog << "Location: " << iter2->second.getLocationPath() << std::endl; + for (std::map<std::string, Location>::const_iterator iter2 = + iter->second.getLocations().begin(); + iter2 != iter->second.getLocations().end(); ++iter2) { + std::clog << "Location: " << iter2->second.getLocationPath() + << std::endl; std::clog << " Allow methods: "; - for (std::vector<std::string>::const_iterator iter3 = iter2->second.getAllowMethods().begin(); iter3 != iter2->second.getAllowMethods().end(); ++iter3) { + for (std::vector<std::string>::const_iterator iter3 = + iter2->second.getAllowMethods().begin(); + iter3 != iter2->second.getAllowMethods().end(); ++iter3) { std::clog << *iter3 << " "; } std::clog << std::endl; std::clog << " Index: "; - for (std::vector<std::string>::const_iterator iter3 = iter2->second.getIndex().begin(); iter3 != iter2->second.getIndex().end(); ++iter3) { + for (std::vector<std::string>::const_iterator iter3 = + iter2->second.getIndex().begin(); + iter3 != iter2->second.getIndex().end(); ++iter3) { std::clog << *iter3 << " "; } std::clog << std::endl; - std::clog << " Autoindex: " << iter2->second.getAutoindex() << std::endl; - std::clog << " Client max body size: " << iter2->second.getClientMaxBodySize() << std::endl; + std::clog << " Autoindex: " << iter2->second.getAutoindex() + << std::endl; + std::clog << " Client max body size: " + << iter2->second.getClientMaxBodySize() << std::endl; std::clog << " Cgi extensions: "; - for (std::vector<std::string>::const_iterator iter3 = iter2->second.getCgiExtensions().begin(); iter3 != iter2->second.getCgiExtensions().end(); ++iter3) { + for (std::vector<std::string>::const_iterator iter3 = + iter2->second.getCgiExtensions().begin(); + iter3 != iter2->second.getCgiExtensions().end(); ++iter3) { std::clog << *iter3 << " "; } std::clog << std::endl; std::clog << " Return: " << iter2->second.getReturn() << std::endl; - for (std::map<std::string, std::string>::const_iterator iter3 = iter2->second.getErrorPages().begin(); iter3 != iter2->second.getErrorPages().end(); ++iter3) { - std::clog << " Error page: [" << iter3->first << "] " << iter3->second << std::endl; + for (std::map<std::string, std::string>::const_iterator iter3 = + iter2->second.getErrorPages().begin(); + iter3 != iter2->second.getErrorPages().end(); ++iter3) { + std::clog << " Error page: [" << iter3->first << "] " + << iter3->second << std::endl; } } std::clog << "====================================" << std::endl; diff --git a/src/conf/Location.cpp b/src/conf/Location.cpp index c2bd673..660f8c4 100644 --- a/src/conf/Location.cpp +++ b/src/conf/Location.cpp @@ -18,27 +18,47 @@ bool Location::setLocationPath(std::string const &attribute) { elem.clear(); ss >> elem; ss >> std::ws; - if (elem.empty() == true) { return false; } + if (elem.empty() == true) { + return false; + } this->_path = elem; elem.clear(); ss >> elem; ss >> std::ws; - if (elem.empty() == true) { return false; } - if (elem.compare("{") != 0) { return false; } - if (ss.peek() != EOF) { return false; } + if (elem.empty() == true) { + return false; + } + if (elem.compare("{") != 0) { + return false; + } + if (ss.peek() != EOF) { + return false; + } return true; } -const std::string &Location::getLocationPath() const { - return this->_path; -} +const std::string &Location::getLocationPath() const { return this->_path; } void Location::fillLocationDirectives(Server const &server) { - if (this->_allowMethods.size() == 0) { this->_allowMethods = server.getAllowMethods() ; } - if (this->_autoindex == AConfigurable::UNDEFINED ) { this->_autoindex = server.getAutoindex(); } - if (this->_index.size() == 0) { this->_index = server.getIndex(); } - if (this->_clientMaxBodySize == -1) { this->_clientMaxBodySize = server.getClientMaxBodySize(); } - if (this->_cgi_extensions.size() == 0) { this->_cgi_extensions = server.getCgiExtensions(); } - if (this->_return.empty() == true) { this->_return = server.getReturn(); } - if (this->_errorPages.size() == 0) { this->_errorPages = server.getErrorPages(); } + if (this->_allowMethods.size() == 0) { + this->_allowMethods = server.getAllowMethods(); + } + if (this->_autoindex == AConfigurable::UNDEFINED) { + this->_autoindex = server.getAutoindex(); + } + if (this->_index.size() == 0) { + this->_index = server.getIndex(); + } + if (this->_clientMaxBodySize == -1) { + this->_clientMaxBodySize = server.getClientMaxBodySize(); + } + if (this->_cgi_extensions.size() == 0) { + this->_cgi_extensions = server.getCgiExtensions(); + } + if (this->_return.empty() == true) { + this->_return = server.getReturn(); + } + if (this->_errorPages.size() == 0) { + this->_errorPages = server.getErrorPages(); + } } \ No newline at end of file diff --git a/src/conf/Server.cpp b/src/conf/Server.cpp index e7d18f8..48550d8 100644 --- a/src/conf/Server.cpp +++ b/src/conf/Server.cpp @@ -1,7 +1,10 @@ #include "Server.hpp" -std::map<std::string, bool (Location::*)(std::string const&, std::fstream&)> Server::initSetterMap() { - std::map<std::string, bool (Location::*)(const std::string&, std::fstream&)> srvSetterMap; +std::map<std::string, bool (Location::*)(std::string const &, std::fstream &)> +Server::initSetterMap() { + std::map<std::string, + bool (Location::*)(const std::string &, std::fstream &)> + srvSetterMap; srvSetterMap["allow_methods"] = &Location::setAllowMethods; srvSetterMap["autoindex"] = &Location::setAutoIndex; srvSetterMap["index"] = &Location::setIndex; @@ -12,23 +15,27 @@ std::map<std::string, bool (Location::*)(std::string const&, std::fstream&)> Ser return srvSetterMap; } -std::map<std::string, bool (Location::*)(std::string const&, std::fstream&)> Server::_setterMap = initSetterMap(); +std::map<std::string, bool (Location::*)(std::string const &, std::fstream &)> + Server::_setterMap = initSetterMap(); -Server::Server() : AConfigurable(), _servername(""), _ipAddr("0.0.0.0"), _port("8000") {} +Server::Server() + : AConfigurable(), _servername(""), _ipAddr("0.0.0.0"), _port("8000") {} -const std::string &Server::getServername() const { - return this->_servername; -} +const std::string &Server::getServername() const { return this->_servername; } bool Server::setServername(std::string const &attribute, std::fstream &file) { (void)file; std::stringstream ss(attribute); std::string elem; ss >> elem; - if (ss.peek() == EOF) { return false; } + if (ss.peek() == EOF) { + return false; + } elem.clear(); ss >> elem; - if (ss.peek() != EOF) { return false; } + if (ss.peek() != EOF) { + return false; + } this->_servername = elem; return true; } @@ -46,23 +53,31 @@ bool Server::setListen(std::string const &attribute, std::fstream &file) { std::string ipAddr; std::string port; ss >> elem; - if (ss.peek() == EOF) { return false; } + if (ss.peek() == EOF) { + return false; + } elem.clear(); ss >> elem; - if (ss.peek() != EOF) { return false; } + if (ss.peek() != EOF) { + return false; + } ss.str(""); ss.clear(); ss << elem; std::getline(ss, ipAddr, ':'); if (ss.peek() == EOF) { this->_port = ipAddr; - if (isPortOutOfRange(this->_port) == true) { throw std::runtime_error(INVALID_PORT); } + if (isPortOutOfRange(this->_port) == true) { + throw std::runtime_error(INVALID_PORT); + } return true; } std::getline(ss, port); this->_ipAddr = ipAddr; this->_port = port; - if (isPortOutOfRange(this->_port) == true) { throw std::runtime_error(INVALID_PORT); } + if (isPortOutOfRange(this->_port) == true) { + throw std::runtime_error(INVALID_PORT); + } return true; } @@ -70,53 +85,71 @@ std::string Server::getListen() const { return this->_ipAddr + ':' + this->_port; } -const std::string &Server::getIpaddr() const { - return this->_ipAddr; -} +const std::string &Server::getIpaddr() const { return this->_ipAddr; } -const std::string &Server::getPort() const { - return this->_port; -} +const std::string &Server::getPort() const { return this->_port; } bool Server::setRoot(std::string const &attribute, std::fstream &file) { (void)file; std::stringstream ss(attribute); std::string elem; ss >> elem; - if (ss.peek() == EOF) { return false; } + if (ss.peek() == EOF) { + return false; + } elem.clear(); ss >> elem; - if (ss.peek() != EOF) { return false; } + if (ss.peek() != EOF) { + return false; + } this->_root = elem; return true; } -const std::string &Server::getRoot() const { - return this->_root; -} +const std::string &Server::getRoot() const { return this->_root; } bool Server::setLocations(std::string const &attribute, std::fstream &file) { std::string line; Location location; bool bracketFlag = false; - if (location.setLocationPath(attribute) == false) { throw std::runtime_error(SYNTAX_ERROR); } + if (location.setLocationPath(attribute) == false) { + throw std::runtime_error(SYNTAX_ERROR); + } while (std::getline(file, line)) { Config::lineCount++; - if (utils::shouldIgnoreLine(line)) { continue; } + if (utils::shouldIgnoreLine(line)) { + continue; + } utils::rmCR(line); std::stringstream ss(line); std::string elem; ss >> elem; - if (elem.compare("}") == 0) { bracketFlag = true; break;} - std::map<std::string, bool (Location::*)(std::string const&, std::fstream&)>::iterator iter = this->_setterMap.find(elem); - if (iter == this->_setterMap.end()) { throw std::runtime_error(INVALID_ITEM); } - if (line[line.size() - 1] != ';') { throw std::runtime_error(SYNTAX_ERROR); } - if (line.size() != 0) { line = line.substr(0, line.size() - 1); } - if ((location.*(iter->second))(line, file) == false) { throw std::runtime_error(SYNTAX_ERROR); } + if (elem.compare("}") == 0) { + bracketFlag = true; + break; + } + std::map<std::string, bool (Location::*)(std::string const &, + std::fstream &)>::iterator + iter = this->_setterMap.find(elem); + if (iter == this->_setterMap.end()) { + throw std::runtime_error(INVALID_ITEM); + } + if (line[line.size() - 1] != ';') { + throw std::runtime_error(SYNTAX_ERROR); + } + if (line.size() != 0) { + line = line.substr(0, line.size() - 1); + } + if ((location.*(iter->second))(line, file) == false) { + throw std::runtime_error(SYNTAX_ERROR); + } + } + if (bracketFlag == false) { + throw std::runtime_error(SYNTAX_ERROR); } - if (bracketFlag == false) { throw std::runtime_error(SYNTAX_ERROR); } - this->_locations.insert(std::pair<std::string, Location>(location.getLocationPath(), location)); + this->_locations.insert( + std::pair<std::string, Location>(location.getLocationPath(), location)); return true; } @@ -125,7 +158,9 @@ const std::map<std::string, Location> &Server::getLocations() const { } void Server::fillLocationDirectives() { - for (std::map<std::string, Location>::iterator iter = this->_locations.begin(); iter != this->_locations.end() ; ++iter) { + for (std::map<std::string, Location>::iterator iter = + this->_locations.begin(); + iter != this->_locations.end(); ++iter) { iter->second.fillLocationDirectives(*this); } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 71b36a7..ad42057 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,14 @@ #include "main.hpp" -// static bool startUpServerSockets(std::map<int, ServerSocket> &ssmap, Config &config) { -// std::vector<Server> servers(config.getServers()); -// for (std::vector<Server>::iterator iter = servers.begin(); iter != servers.end(); ++iter) { -// std::string ipAddr = iter->getIpAddress(); -// std::string port = iter->getPort(); -// if (ipAddr.empty() == true) { ipAddr = "0.0.0.0"; } -// if (port.empty() == true) { port = "8000"; } -// ServerSocket ss(ipAddr, port); -// if (ss.init() == false) { return false; } -// std::cout << "Start up server: " << ipAddr << ":" << port << std::endl; -// ssmap.insert(std::pair<int, ServerSocket>(ss.getFd(), ss)); +// static bool startUpServerSockets(std::map<int, ServerSocket> &ssmap, Config +// &config) { std::vector<Server> servers(config.getServers()); for +// (std::vector<Server>::iterator iter = servers.begin(); iter != servers.end(); +// ++iter) { std::string ipAddr = iter->getIpAddress(); std::string port = +// iter->getPort(); if (ipAddr.empty() == true) { ipAddr = "0.0.0.0"; } if +// (port.empty() == true) { port = "8000"; } ServerSocket ss(ipAddr, port); if +// (ss.init() == false) { return false; } std::cout << "Start up server: " << +// ipAddr << ":" << port << std::endl; ssmap.insert(std::pair<int, +// ServerSocket>(ss.getFd(), ss)); // } // return true; // } @@ -21,8 +19,14 @@ int main(int argc, char *argv[]) { std::cerr << "Usage: ./webserv config/filepath" << std::endl; return 1; } - if (conf.open(argv[1]) == false) { return 1; } - if (conf.load() == false) { return 1; } + if (conf.open(argv[1]) == false) { + return 1; + } + if (conf.load() == false) { + return 1; + } conf.printServers(); - if (conf.close() == false) { return 1; } + if (conf.close() == false) { + return 1; + } } \ No newline at end of file diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index f1b9f5a..2f01a96 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -1,61 +1,72 @@ #include "utils.hpp" namespace utils { - void putSysError(char const *msg) { - std::cerr << RED << "Webserv: Error: " << msg << ": " - << std::strerror(errno) << RESET << std::endl; - } +void putSysError(char const *msg) { + std::cerr << RED << "Webserv: Error: " << msg << ": " + << std::strerror(errno) << RESET << std::endl; +} - std::size_t decStrToSizeT(std::string &str) { - std::stringstream ss(str); - std::size_t dig; +std::size_t decStrToSizeT(std::string &str) { + std::stringstream ss(str); + std::size_t dig; - ss >> dig; - return dig; - } + ss >> dig; + return dig; +} - std::size_t hexStrToSizeT(std::string &str) { - std::stringstream ss; - std::size_t dig; +std::size_t hexStrToSizeT(std::string &str) { + std::stringstream ss; + std::size_t dig; - ss << std::hex << str; - ss >> dig; - return dig; - } + ss << std::hex << str; + ss >> dig; + return dig; +} - bool findCRLF(std::stringstream &stream) { - std::streampos originalPos = stream.tellg(); - std::string remainingContent((std::istreambuf_iterator<char>(stream)), - std::istreambuf_iterator<char>()); - bool foundCRLF = remainingContent.find("\r\n") != std::string::npos; - stream.seekg(originalPos); - return foundCRLF; - } +bool findCRLF(std::stringstream &stream) { + std::streampos originalPos = stream.tellg(); + std::string remainingContent((std::istreambuf_iterator<char>(stream)), + std::istreambuf_iterator<char>()); + bool foundCRLF = remainingContent.find("\r\n") != std::string::npos; + stream.seekg(originalPos); + return foundCRLF; +} - bool shouldIgnoreLine(std::string const &line) { - if (line.empty() == true) { return true; } - std::stringstream ss(line); - std::string elem; +bool shouldIgnoreLine(std::string const &line) { + if (line.empty() == true) { + return true; + } + std::stringstream ss(line); + std::string elem; - ss >> elem; - if (elem.empty() == true) { return true; } - if (elem[0] == '#') { return true; } - return false; + ss >> elem; + if (elem.empty() == true) { + return true; + } + if (elem[0] == '#') { + return true; } + return false; +} - bool rmCR(std::string &line) { - if (line.empty() == true) { return false; } - if (line[line.size() - 1] == '\r') { - line = line.substr(0, line.size() - 1); - return true; - } +bool rmCR(std::string &line) { + if (line.empty() == true) { return false; } + if (line[line.size() - 1] == '\r') { + line = line.substr(0, line.size() - 1); + return true; + } + return false; +} - bool isNumber(std::string const &str) { - for (std::string::const_iterator iter = str.begin(); iter != str.end(); ++iter) { - if (std::isdigit(*iter) == false) { return false; } +bool isNumber(std::string const &str) { + for (std::string::const_iterator iter = str.begin(); iter != str.end(); + ++iter) { + if (std::isdigit(*iter) == false) { + return false; } - return true; } -} \ No newline at end of file + return true; +} +} // namespace utils \ No newline at end of file