diff --git a/config/tester.toml b/config/tester.toml index 9d2d763..389d49a 100644 --- a/config/tester.toml +++ b/config/tester.toml @@ -5,26 +5,26 @@ body_size_limit = 100000000 [[server.location]] location = "/" - root = "www/tester/" + root = "tester/" methods = ["GET"] autoindex = true directory_file = "index.html" [[server.location]] location = "/put_test" - root = "www/tester/" + root = "tester/" methods = ["PUT"] autoindex = true - upload_dir = "www/tester/upload_here/" + upload_dir = "tester/upload_here/" [[server.location]] location = "*.bla" - cgi_pass = "/usr/bin/php-cgi" + cgi_pass = "ubuntu_cgi_tester" [[server.location]] location = "/post_body" - root = "www/tester/" + root = "tester/" methods = ["POST"] body_size_limit = 100 [[server.location]] location = "/directory" methods = ["GET"] - root = "www/tester/YoupiBanane/" + root = "tester/YoupiBanane/" directory_file = "youpi.bad_extension" diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 8b55620..e63c9d3 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include diff --git a/src/CGI/CgiHandle.cpp b/src/CGI/CgiHandle.cpp index 9f522e5..2ddfda0 100644 --- a/src/CGI/CgiHandle.cpp +++ b/src/CGI/CgiHandle.cpp @@ -81,8 +81,9 @@ std::string CgiHandle::executeCgi() int sO; int byte_read = 1; std::string body; + std::string &reqBody = _request.getBody(); - argv[0] = const_cast(_response.getCgiPass().data()); + argv[0] = const_cast(_response._fullURI.data()); argv[1] = NULL; try { @@ -92,7 +93,6 @@ std::string CgiHandle::executeCgi() { std::cerr << RED << e.what() << RESET << '\n'; } - printenv(env); sI = dup(STDIN_FILENO); sO = dup(STDOUT_FILENO); @@ -100,9 +100,7 @@ std::string CgiHandle::executeCgi() FILE *fOt = tmpfile(); long fdin = fileno(fIn); long fdOut = fileno(fOt); - DBOUT << BLUE << "in CGI exe" << ENDL; - // DBOUT << "BODY[\n" << _request.getBody() << "\n]" << ENDL; - write(fdin, _request.getBody().data(), _request.getBody().size()); + write(fdin, reqBody.data(), reqBody.size()); lseek(fdin, 0, SEEK_SET); pid = fork(); if (pid == -1) @@ -115,7 +113,7 @@ std::string CgiHandle::executeCgi() dup2(fdOut, STDOUT_FILENO); execve(_response.getCgiPass().c_str(), argv, env); std::cerr << RED << "Execve error." << RESET << ENDL; - write(STDOUT_FILENO, "Status: 500\r\n\r\n", 15); + write(STDOUT_FILENO, "Status: 500\r\n\r\n", 15); } else { @@ -124,9 +122,10 @@ std::string CgiHandle::executeCgi() waitpid(-1, NULL, 0); lseek(fdOut, 0, SEEK_SET); + byte_read = 1; while (byte_read) { - bzero(buffer, BUFFSIZE); + memset(buffer, 0, BUFFSIZE + 1); byte_read = read(fdOut, buffer, BUFFSIZE); body.append(buffer, byte_read); } @@ -147,7 +146,7 @@ void CgiHandle::initEnvVariables() { std::map::iterator it; std::map tmp1 = _request.getClientFields(); - _scriptName = _response.getFullURI().substr(_response.getFullURI().rfind("/")); + _scriptName = _response._fullURI.substr(_response._fullURI.rfind("/")); it = tmp1.find("content_type"); if (it != tmp1.end()) _variable["AUTH TYPE"] = it->second; @@ -158,10 +157,10 @@ void CgiHandle::initEnvVariables() _variable["CONTENT_TYPE"] = ""; _variable["GATEWAY_INTERFACE"] = std::string("CGI/1.1"); - _variable["SCRIPT_NAME"] = _response.getFullURI(); - _variable["SCRIPT_FILENAME"] = _response.getFullURI(); + _variable["SCRIPT_NAME"] = _response._fullURI; + _variable["SCRIPT_FILENAME"] = _response._fullURI; - _variable["PATH_TRANSLATED"] = _response.getFullURI(); + _variable["PATH_TRANSLATED"] = _request.getURI(); _variable["REQUEST_URI"] = _request.getURI(); _variable["PATH_INFO"] = _request.getURI(); @@ -184,7 +183,7 @@ void CgiHandle::initEnvVariables() tmp = getEnvFormat(it->first); _variable["HTTP_" + tmp] = it->second; } - printSSmap(_variable); + // printSSmap(_variable); } CgiHandle::~CgiHandle() diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index 73b2bf5..b1bbcba 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -162,9 +162,6 @@ std::string Client::generateRespons(void) _to_send_char = new char[len + 1]; std::memcpy(_to_send_char, _toSend.c_str(), len + 1); - // DBOUT << "len = " << len << ENDL; - // DBOUT << "strlen = " << strlen(_to_send_char) << ENDL; - // DBOUT << "content_lenth = " << _request.getContentLength() << ENDL; return (_toSend); } diff --git a/src/Client/Config.cpp b/src/Client/Config.cpp index 43f6d0f..682268b 100644 --- a/src/Client/Config.cpp +++ b/src/Client/Config.cpp @@ -36,6 +36,21 @@ int isDir(std::string path) return (-1); return (-1); } + +void copyLocation(location *dest, location *src) +{ + dest->autoindex = src->autoindex; + dest->cgi_pass = src->cgi_pass; + dest->clientBodySize = src->clientBodySize; + dest->directoryFile = src->directoryFile; + dest->location = src->location; + dest->methods = src->methods; + dest->redirect = src->redirect; + dest->root = src->root; + dest->uploadAccept = src->uploadAccept; + dest->uploadDir = src->uploadDir; +} + int Config::calcLen(std::string &s1, std::string &s2) { unsigned long len = 0; @@ -68,7 +83,12 @@ location *Config::getLocation(std::vector &arr, std::string &URI) tmp = *it; tryLocation = URI.substr(0, tryLen); if (tmp->location == tryLocation) + { + tmp = new location; + copyLocation(tmp, *it); step_1.push_back(tmp); + break; + } it++; } if (!step_1.empty()) @@ -91,6 +111,8 @@ location *Config::getLocation(std::vector &arr, std::string &URI) suffix1 = tmp->location.substr(2); if (suffix1 == suffix) { + tmp = new location; + copyLocation(tmp, *it); step_2.push_back(tmp); break; } @@ -104,6 +126,7 @@ location *Config::getLocation(std::vector &arr, std::string &URI) if(!step_2[1]->cgi_pass.empty()) { step_2[0]->cgi_pass = step_2[1]->cgi_pass; + delete step_2[1]; } return (step_2[0]); } diff --git a/src/Client/Response.cpp b/src/Client/Response.cpp index 97ff0ed..a9c4c97 100644 --- a/src/Client/Response.cpp +++ b/src/Client/Response.cpp @@ -107,11 +107,11 @@ ssize_t Response::getMaxBodySize(void) { return (_maxBodySize); } + //-------------------------------------------------File--------------------------------------- void Response::OpenResponseFile(const char *path) { - DBOUT << "in OPEN RESPONSE FILE " << path << ENDL; std::stringstream ss; std::ifstream file(path, std::ifstream::in); @@ -213,11 +213,8 @@ std::string Response::getFullURI(void) } else { - DBOUT << "location" << _location->location << ENDL; tmp = _request.getURI().substr(len); - DBOUT << "tmp1 " << RED << tmp << ENDL; tmp = _location->root + tmp; - DBOUT << "tmp2" << RED << tmp << ENDL; } if (_request.isDir(tmp) == 0) { @@ -327,16 +324,9 @@ void Response::generate2(serverListen &l) _method = _request.getMethod(); _maxBodySize = (_location->clientBodySize > 0) ? _location->clientBodySize : _config->getClientBodySize(); if (_maxBodySize > 0) - _code = (_request.getRecved() > _maxBodySize) ? 413 : _code; - DBOUT << BLUE << "max size" << _maxBodySize << ENDL; - DBOUT << BLUE << "_location size" << _location->clientBodySize << ENDL; - DBOUT << BLUE << "_config sieze" << _config->getClientBodySize() << ENDL; - DBOUT << BLUE << "req size " << _request.getContentLength() << ENDL; - DBOUT << BLUE << "recv size " << _request.getRecved() << ENDL; + _code = (_request.getBody().size() > (unsigned long)_maxBodySize) ? 413 : _code; } - DBOUT << "fullURI " << _fullURI << ENDL; - DBOUT << RED << "code is " << _code << ENDL; if (_request.badCode(_code) || (!allowedMethod(_method) && _location->cgi_pass.empty()) || isRedirect()) { invalidClient(); @@ -384,6 +374,7 @@ void Response::methodGet(void) CgiHandle cgi(_request, *this); _body = cgi.executeCgi(); + unsigned long pos = _body.find("\r\n\r\n"); if (pos != std::string::npos) { @@ -410,6 +401,7 @@ void Response::methodPost(void) CgiHandle cgi(_request, *this); _body = cgi.executeCgi(); + DBOUT << "CGI SIZE BODY " << _body.size() << ENDL; unsigned long pos = _body.find("\r\n\r\n"); if (pos != std::string::npos) { @@ -558,4 +550,5 @@ std::string Response::getErrorPage(int code) Response::~Response() { + delete _location; } diff --git a/src/Client/Response.hpp b/src/Client/Response.hpp index 281d789..e1f85ef 100644 --- a/src/Client/Response.hpp +++ b/src/Client/Response.hpp @@ -20,7 +20,6 @@ private: std::map _errorPages; bool _Autoindex; serverListen _hostPort; - std::string _fullURI; std::string _method; private: @@ -73,6 +72,7 @@ public: void setData(Request, ServerConfig *); void setData(Request &, ServerConfig *, location *location); public: + std::string _fullURI; void OpenResponseFile(const char *path); void OpenErrorFile(int code); void initErrorCode(void); diff --git a/src/main.cpp b/src/main.cpp index 253eef2..bc6ac35 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,12 +9,20 @@ int main(int argc, char **argv) (void)argv; Server server; + char *path = (char *)"config/real.toml"; + + try + { + if (argv[1] != NULL) + server.readConfig(argv[1]); + else + server.readConfig(path); + server.start(); + } + catch(const std::exception& e) + { + std::cerr << RED << e.what() << '\n' << ENDL; + } + server.end(); - server.readConfig(argv[1]); - server.setupConfig(); - // server.start(); - server.end(); - - - return (0); } diff --git a/www/tester/YoupiBanane/Yeah/not_happy.bad_extension b/tester/YoupiBanane/Yeah/not_happy.bad_extension similarity index 100% rename from www/tester/YoupiBanane/Yeah/not_happy.bad_extension rename to tester/YoupiBanane/Yeah/not_happy.bad_extension diff --git a/www/tester/YoupiBanane/nop/other.pouic b/tester/YoupiBanane/nop/other.pouic similarity index 100% rename from www/tester/YoupiBanane/nop/other.pouic rename to tester/YoupiBanane/nop/other.pouic diff --git a/www/tester/YoupiBanane/nop/youpi.bad_extension b/tester/YoupiBanane/nop/youpi.bad_extension similarity index 100% rename from www/tester/YoupiBanane/nop/youpi.bad_extension rename to tester/YoupiBanane/nop/youpi.bad_extension diff --git a/www/tester/YoupiBanane/youpi.bad_extension b/tester/YoupiBanane/youpi.bad_extension similarity index 100% rename from www/tester/YoupiBanane/youpi.bad_extension rename to tester/YoupiBanane/youpi.bad_extension diff --git a/www/tester/YoupiBanane/youpi.bla b/tester/YoupiBanane/youpi.bla similarity index 100% rename from www/tester/YoupiBanane/youpi.bla rename to tester/YoupiBanane/youpi.bla diff --git a/ubuntu_cgi_tester b/ubuntu_cgi_tester new file mode 100755 index 0000000..cc3db60 Binary files /dev/null and b/ubuntu_cgi_tester differ diff --git a/www/script/index2.php b/www/script/index2.php new file mode 100644 index 0000000..8e9fff0 --- /dev/null +++ b/www/script/index2.php @@ -0,0 +1,17 @@ +\n"); + // echo ("\n"); + // echo ("Hello World - First CGI Program\n"); + // echo ("\n"); + // echo ("\n"); + // echo ("

Hello World! This is my first CGI program

\n"); + // echo ("\n"); + // echo ("\n"); +?> \ No newline at end of file