diff --git a/conf/default.conf b/conf/default.conf index b71421b..2d57ca2 100644 --- a/conf/default.conf +++ b/conf/default.conf @@ -1,10 +1,9 @@ server { - listen 0.0.0.0:8080; + listen 127.1.1.1:8080; server_name localhost; root ./; - index html/index.html; - autoindex on; - client_max_body_size 10000M; + autoindex on; + client_max_body_size 1000G; use_chunked_encoding true; chunk_size 100; @@ -26,13 +25,12 @@ server { allowed_methods GET POST; cgi_path /usr/bin/python3 /usr/bin/php; cgi_ext .py .php; - autoindex on; + autoindex off; } location /uploads/ { root ./; allowed_methods POST GET DELETE; - autoindex on; } } diff --git a/conf/dif_root.conf b/conf/dif_root.conf new file mode 100644 index 0000000..f48bc79 --- /dev/null +++ b/conf/dif_root.conf @@ -0,0 +1,33 @@ +server { + listen 0.0.0.0:8080; + server_name localhost; + root ./assets; + autoindex on; + client_max_body_size 10M; + use_chunked_encoding true; + chunk_size 100; + +# Custom error pages + +# Main site and directory listing test + location /assets/ { + root ./; + index old_index.html; + allowed_methods GET; + autoindex on; + } + + location /cgi-bin/ { + root ./; + allowed_methods GET POST; + cgi_path /usr/bin/python3 /usr/bin/php; + cgi_ext .py .php; + autoindex off; + } + + location /uploads/ { + root ./; + allowed_methods POST GET DELETE; + } + +} diff --git a/inc/Location.hpp b/inc/Location.hpp index c0277de..0da6619 100644 --- a/inc/Location.hpp +++ b/inc/Location.hpp @@ -21,7 +21,7 @@ class LocationConf { const std::vector< std::string > getAllowedMethods( void ) const; const std::map< std::string, std::string > getAllowedRedirects( void ) const; const std::string getRootDir( void ) const; - bool getAutoIndex( void ) const; + int getAutoIndex( void ) const; const std::vector< std::string > getIndex( void ) const; const std::vector< std::string > getCgiPath( void ) const; const std::vector< std::string > getCgiExt( void ) const; @@ -32,7 +32,7 @@ class LocationConf { void setAllowedMethod( std::string ); void setAllowedRedirects( std::string, std::string ); void setRootDir( std::string ); - void setAutoIndex( bool ); + void setAutoIndex( int ); void setIndex( std::string ); void setCgiPath( std::string ); void setCgiExt( std::string ); @@ -55,7 +55,7 @@ class LocationConf { std::string _rootDir; //directory listing - bool _autoIndex; + int _autoIndex; //index std::vector< std::string > _index; diff --git a/inc/Response.hpp b/inc/Response.hpp index e08bdeb..6dd7c2a 100644 --- a/inc/Response.hpp +++ b/inc/Response.hpp @@ -59,7 +59,7 @@ class Response { //helpers for get req std::string constructFullPath(const std::string uri); bool serveFileIfExists(const std::string& fullPath, HttpRequest& ReqObj); - bool serveRootIndexfile(HttpRequest& ReqObj); + bool serveRootIndexfile(HttpRequest& ReqObj, std::string fullPath); bool serveLocationIndex(HttpRequest& ReqObj); void HandleDeleteRequest(HttpRequest& ReqObj, PathInfo& pathInfo); diff --git a/src/Config/LocationConf.cpp b/src/Config/LocationConf.cpp index de24b3b..48c9608 100644 --- a/src/Config/LocationConf.cpp +++ b/src/Config/LocationConf.cpp @@ -1,7 +1,7 @@ #include "Location.hpp" //constructors -LocationConf::LocationConf( void ) : _autoIndex( false ) {;} +LocationConf::LocationConf( void ) : _autoIndex( 0 ) {;} LocationConf::~LocationConf( void ) {;} @@ -134,7 +134,7 @@ std::ostream& operator <<( std::ostream& os, const LocationConf& loc ) { return( _rootDir ); } -bool LocationConf::getAutoIndex( void ) const{ +int LocationConf::getAutoIndex( void ) const{ return( _autoIndex ); } @@ -171,7 +171,7 @@ void LocationConf:: setRootDir( std::string rootDir ){ _rootDir = rootDir; } -void LocationConf:: setAutoIndex( bool status ){ +void LocationConf:: setAutoIndex( int status ){ _autoIndex = status; } diff --git a/src/Config/LocationConfParse.cpp b/src/Config/LocationConfParse.cpp index 3115943..ee11db3 100644 --- a/src/Config/LocationConfParse.cpp +++ b/src/Config/LocationConfParse.cpp @@ -96,9 +96,9 @@ void parseAutoIndex( LocationConf& location, std::stringstream& ss ){ while( ss >> tmp ){ if( cutEnding( tmp ) == "on" ) - location.setAutoIndex( true ); + location.setAutoIndex( 1 ); else if( cutEnding( tmp ) == "off" ) - location.setAutoIndex( false ); + location.setAutoIndex( -1 ); else throw( std::runtime_error( "Wrong Auto Index " + tmp ) ); } diff --git a/src/Config/ServerConfParse.cpp b/src/Config/ServerConfParse.cpp index b444c56..67d94b3 100644 --- a/src/Config/ServerConfParse.cpp +++ b/src/Config/ServerConfParse.cpp @@ -38,7 +38,7 @@ static bool checkIp( std::string tmp ){ tmp = tmp.substr( pos + 1, tmp.size() ); i = pos; pos = tmp.find( '.' ); - } + } return( true ); } @@ -70,7 +70,7 @@ void parseListen( ServerConf& server, std::stringstream& ss ){ throw( std::runtime_error( "Wrong Port" + \ tmp.substr( tmp.find( ':' ), tmp.size() ) ) ); ip = tmp.substr( 0, tmp.find( ':' ) ); - port = checkPort( tmp.substr( tmp.find( ':' ) + 1, tmp.size() ) ); + port = checkPort( tmp.substr( tmp.find( ':' ) + 1, tmp.size() ) ); } else if( tmp.find( '.' ) != tmp.npos ){ if( !checkIp( tmp ) ) @@ -80,7 +80,7 @@ void parseListen( ServerConf& server, std::stringstream& ss ){ else{ if( checkPort( tmp ) == -1 ) throw( std::runtime_error( "Wrong Port" + tmp ) ); - port = checkPort( tmp.substr( tmp.find( ':' ) + 1, tmp.size() ) ); + port = checkPort( tmp.substr( tmp.find( ':' ) + 1, tmp.size() ) ); } server.setIpPort( cutEnding( ip ), port ); } @@ -111,20 +111,45 @@ void parseErrorPage( ServerConf& server, std::stringstream& ss ){ throw( std::runtime_error( "Line not ended on ; " + tmp ) ); } +int ParseSizeWithUnits(std::string str){ + size_t multiplier = 1; + char unit = str[str.size() -2]; // skip '0\ and ; + + if (unit == 'K' || unit == 'k') { + multiplier = 1024; // Kilobytes + } else if (unit == 'M' || unit == 'm') { + multiplier = 1024 * 1024; // Megabytes + } else if (unit == 'G' || unit == 'g') { + multiplier = 1024 * 1024 * 1024; // Gigabytes + } else if (!isdigit(unit)) { + return (-1); + } + + // Convert the numeric part of the string to a size_t + size_t sizeValue = 0; + std::istringstream(str.substr(0, str.length() - (isdigit(unit) ? 0 : 1))) >> sizeValue; + + if (sizeValue == 0 && str[0] != '0') { + return(-1); + } + + return(sizeValue * multiplier); +} + void parseBodySize( ServerConf& server, std::stringstream& ss ){ std::string tmp; while( ss >> tmp ){ - std::stringstream sstmp( tmp ); int size; - - sstmp >> size; + if( tmp.at( tmp.size() - 1 ) != ';' ) + throw( std::runtime_error( "Line not ended on ; " + tmp ) ); + size = ParseSizeWithUnits(tmp); + //std::cout << "SSIZE " << size << "\n"; if( size < 0) throw( std::runtime_error( "Wrong Max Body Size" ) ); server.setBodySize( size ); } - if( tmp.at( tmp.size() - 1 ) != ';' ) - throw( std::runtime_error( "Line not ended on ; " + tmp ) ); + } void parseChunkedEncoding(ServerConf& server, std::stringstream& ss) { @@ -160,7 +185,7 @@ void parseAutoIndex( ServerConf& server, std::stringstream& ss ){ server.setAutoIndex( true ); else if( cutEnding( tmp ) == "off" ) server.setAutoIndex( false ); - else + else throw( std::runtime_error( "Wrong Auto Index " + tmp ) ); } if( tmp.at( tmp.size() - 1 ) != ';' ) @@ -173,7 +198,7 @@ void parseRootDir( T& temp, std::stringstream& ss ){ while( ss >> tmp ) temp.setRootDir( makePath( cutEnding( tmp ), 1 ) ); - + if( tmp.at( tmp.size() - 1 ) != ';' ) throw( std::runtime_error( "Line not ended on ; " + tmp ) ); } @@ -227,5 +252,5 @@ void Config::parseServerConfBlock( ServerConf& server ){ getline( _configFile, tmp ); } server.checkAccess(); - + } diff --git a/src/Response/get_Response.cpp b/src/Response/get_Response.cpp index e7e7766..7936a0e 100644 --- a/src/Response/get_Response.cpp +++ b/src/Response/get_Response.cpp @@ -35,7 +35,7 @@ bool Response::serveFileIfExists(const std::string& fullPath, HttpRequest& ReqOb return false; // File not found } -bool Response::serveRootIndexfile(HttpRequest& ReqObj){ +bool Response::serveRootIndexfile(HttpRequest& ReqObj,std::string fullPath){ std::vector indexFiles = _serverConf->getIndex(); if (!indexFiles.empty()) { for (std::vector::iterator it = indexFiles.begin(); it != indexFiles.end(); ++it) { @@ -49,6 +49,13 @@ bool Response::serveRootIndexfile(HttpRequest& ReqObj){ } } } + + if(_serverConf->getAutoIndex() == true){ + setBody(generateDirectoryListing(fullPath)); + ReqObj.setResponseCode(200); + return(true); + } + //no root index file ReqObj.setResponseCode(404); throw 404; @@ -95,12 +102,12 @@ void Response::HandleGetRequest(HttpRequest& ReqObj, PathInfo& pathInfo) { } } - //if Uri is root server root index file - if(uri == "/" || uri == "."){ - if (serveRootIndexfile(ReqObj) == true) - return; + //std::cout << "serverconf root dir " << _serverConf->getRootDir() << "and uri " << uri << "and full path " << fullPath <<"\n"; +// Check if the URI is the root directory + if (uri == "/" || fullPath == _serverConf->getRootDir() || fullPath == "./" || fullPath == "/") { + if (serveRootIndexfile(ReqObj, fullPath) == true) + return; } - if (_locationConf) { if(pathInfo.isDirectory()){ @@ -108,8 +115,9 @@ void Response::HandleGetRequest(HttpRequest& ReqObj, PathInfo& pathInfo) { if(serveLocationIndex(ReqObj) == true){ return; } + std::cout << "loc conf auto index " << _locationConf->getAutoIndex() << " server conf auto " << _serverConf->getAutoIndex() << "\n"; - if( _locationConf->getAutoIndex() == true){ + if( _locationConf->getAutoIndex() == true || (_serverConf->getAutoIndex() == true && _locationConf->getAutoIndex() == 0 )){ setBody(generateDirectoryListing(pathInfo.getFullPath())); ReqObj.setResponseCode(200); return; diff --git a/src/Response/helpers_Response.cpp b/src/Response/helpers_Response.cpp index eb81b5e..1dafe4e 100644 --- a/src/Response/helpers_Response.cpp +++ b/src/Response/helpers_Response.cpp @@ -122,11 +122,7 @@ std::string Response::generateDirectoryListing(const std::string& path) { // Add row to table html << "getBodySize(); //_locationConf->getClientMaxBodySize() : // If location config exists, use its limit->> dont have that parsed idk if we wna tto include //_serverConf->getClientMaxBodySize(); // Otherwise, use server's default limit - std::cout << "C0ntent size " << contentLength << " and max body len " << maxBodySize << "\n"; + //std::cout << "C0ntent size " << contentLength << " and max body len " << maxBodySize << "\n"; if (contentLength > maxBodySize) { throw 413; return 413;