From 588d1bbc776f62bb659863a5a89d2c900ac2f248 Mon Sep 17 00:00:00 2001 From: mo Date: Wed, 16 Apr 2025 11:30:24 +0200 Subject: [PATCH 1/2] added error_confs and fixing them --- conf/badConf/badBlockLocation.conf | 11 +++++++++++ conf/badConf/badBlockServeer.conf | 8 ++++++++ conf/badConf/badRoot.conf | 5 +++++ conf/badConf/noDirective.conf | 5 +++++ conf/badConf/noListen.conf | 3 +++ conf/badConf/noRoot.conf | 3 +++ conf/badConf/samePort.conf | 4 ++++ conf/default.conf | 4 ++-- conf/test.conf | 2 +- src/Config/ServerConfParse.cpp | 2 ++ 10 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 conf/badConf/badBlockLocation.conf create mode 100644 conf/badConf/badBlockServeer.conf create mode 100644 conf/badConf/badRoot.conf create mode 100644 conf/badConf/noDirective.conf create mode 100644 conf/badConf/noListen.conf create mode 100644 conf/badConf/noRoot.conf create mode 100644 conf/badConf/samePort.conf diff --git a/conf/badConf/badBlockLocation.conf b/conf/badConf/badBlockLocation.conf new file mode 100644 index 0000000..cf7ee2b --- /dev/null +++ b/conf/badConf/badBlockLocation.conf @@ -0,0 +1,11 @@ +server { + listen 0.0.0.0:8080; + root /files; + index index.html; + location { + root /hey; + hey { + + } + } +} diff --git a/conf/badConf/badBlockServeer.conf b/conf/badConf/badBlockServeer.conf new file mode 100644 index 0000000..136c989 --- /dev/null +++ b/conf/badConf/badBlockServeer.conf @@ -0,0 +1,8 @@ +server { + listen 0.0.0.0:8080; + root /files; + index index.html; + hey { + root /hey; + } +} diff --git a/conf/badConf/badRoot.conf b/conf/badConf/badRoot.conf new file mode 100644 index 0000000..98798f1 --- /dev/null +++ b/conf/badConf/badRoot.conf @@ -0,0 +1,5 @@ +server { + listen 0.0.0.0:8080; + root ; + index index.html; +} diff --git a/conf/badConf/noDirective.conf b/conf/badConf/noDirective.conf new file mode 100644 index 0000000..5a3999c --- /dev/null +++ b/conf/badConf/noDirective.conf @@ -0,0 +1,5 @@ +yo { + listen 0.0.0.0:8080; + root /files; + index index.html; +} diff --git a/conf/badConf/noListen.conf b/conf/badConf/noListen.conf new file mode 100644 index 0000000..ae2789f --- /dev/null +++ b/conf/badConf/noListen.conf @@ -0,0 +1,3 @@ +server { + root .; +} diff --git a/conf/badConf/noRoot.conf b/conf/badConf/noRoot.conf new file mode 100644 index 0000000..bc0089a --- /dev/null +++ b/conf/badConf/noRoot.conf @@ -0,0 +1,3 @@ +server { + listen 0.0.0.0:8080; +} diff --git a/conf/badConf/samePort.conf b/conf/badConf/samePort.conf new file mode 100644 index 0000000..958c723 --- /dev/null +++ b/conf/badConf/samePort.conf @@ -0,0 +1,4 @@ +server { + listen 0.0.0.0:8080; + listen 127.0.0.1:8080; +} diff --git a/conf/default.conf b/conf/default.conf index bc31e28..0a05500 100644 --- a/conf/default.conf +++ b/conf/default.conf @@ -1,8 +1,8 @@ server { - listen 0.0.0.0:8080; + listen 0.0.0.0:8081; server_name localhost; root ./; - autoindex on; + autoindex on; client_max_body_size 1m; index assets/index.html; use_chunked_encoding true; diff --git a/conf/test.conf b/conf/test.conf index a7154c8..205d9f3 100644 --- a/conf/test.conf +++ b/conf/test.conf @@ -1,5 +1,5 @@ server { - listen 127.0.0.1:8081; + listen 127.0.0.1:8080; server_name localhost.com; root ./; index html/index.html; diff --git a/src/Config/ServerConfParse.cpp b/src/Config/ServerConfParse.cpp index 0335728..000563a 100644 --- a/src/Config/ServerConfParse.cpp +++ b/src/Config/ServerConfParse.cpp @@ -257,5 +257,7 @@ void Config::parseServerConfBlock( ServerConf& server ){ getline( _configFile, tmp ); } server.checkAccess(); + if( server.getRootDir().empty() || server.getIpPort().empty() ) + throw( std::runtime_error( "RootDir or Listen Directive not set" ) ); } From c1610d5801fce07d8d01a2c46ad5828b0ce65c35 Mon Sep 17 00:00:00 2001 From: mo Date: Wed, 16 Apr 2025 13:02:46 +0200 Subject: [PATCH 2/2] added Redirect Directive to ServerConf --- inc/Defines.hpp | 2 ++ inc/ServerConf.hpp | 5 +++++ src/Config/ServerConf.cpp | 10 +++++++++ src/Config/ServerConfParse.cpp | 41 ++++++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/inc/Defines.hpp b/inc/Defines.hpp index d895a2c..76ba27a 100644 --- a/inc/Defines.hpp +++ b/inc/Defines.hpp @@ -22,6 +22,8 @@ #define ERROR "error_page" #define SERVER "server_name" #define CLIENT "client_max_body_size" +#define CHUNK_ENC "use_chunked_encoding" +#define CHUNK_SIZE "chunk_size" //defines for epoll diff --git a/inc/ServerConf.hpp b/inc/ServerConf.hpp index 7a71859..062f42f 100644 --- a/inc/ServerConf.hpp +++ b/inc/ServerConf.hpp @@ -30,6 +30,7 @@ class ServerConf { const std::vector< std::string > getIndex( void ) const; bool getChunkedTransfer( void ) const; size_t getChunkSize( void ) const; + const std::map< std::string, std::string > getAllowedRedirects( void ) const; //setter functions void setIpPort( std::string ip, int port ); @@ -42,6 +43,7 @@ class ServerConf { void setIndex( std::string ); void setChunkedTransfer( bool chunked ); void setChunkSize( size_t size ); + void setAllowedRedirects( std::string, std::string ); //member functions void checkAccess( void ); @@ -76,6 +78,9 @@ class ServerConf { //size of each chunk size_t _chunkSize; + + //store http redirects + std::map< std::string, std::string > _allowedRedirects; }; #endif diff --git a/src/Config/ServerConf.cpp b/src/Config/ServerConf.cpp index d64eace..bb5adc4 100644 --- a/src/Config/ServerConf.cpp +++ b/src/Config/ServerConf.cpp @@ -16,6 +16,7 @@ ServerConf::ServerConf( const ServerConf& og ){ _rootDir = og._rootDir; _index = og._index; _autoIndex = og._autoIndex; + _allowedRedirects = og._allowedRedirects; } ServerConf::~ServerConf( void ) {;} @@ -32,6 +33,7 @@ ServerConf& ServerConf::operator =( const ServerConf& og ) { _rootDir = og._rootDir; _index = og._index; _autoIndex = og._autoIndex; + _allowedRedirects = og._allowedRedirects; } return( *this ); } @@ -155,6 +157,10 @@ const std::vector< std::string > ServerConf::getIndex( void ) const{ return( _index ); } +const std::map< std::string, std::string > ServerConf::getAllowedRedirects( void ) const{ + return( _allowedRedirects ); +} + //setter functions void ServerConf::setChunkedTransfer( bool chunked ) { @@ -195,3 +201,7 @@ void ServerConf:: setAutoIndex( bool status ){ void ServerConf::setIndex( std::string tmp ){ _index.push_back( tmp ); } + +void ServerConf:: setAllowedRedirects( std::string redirect, std::string path ){ + _allowedRedirects[ redirect ] = path; +} diff --git a/src/Config/ServerConfParse.cpp b/src/Config/ServerConfParse.cpp index 000563a..fdbafb1 100644 --- a/src/Config/ServerConfParse.cpp +++ b/src/Config/ServerConfParse.cpp @@ -197,6 +197,37 @@ void parseAutoIndex( ServerConf& server, std::stringstream& ss ){ throw( std::runtime_error( "Line not ended on ; " + tmp ) ); } +static bool validRedirect( std::string tmp ) { + //check if redirect is alloewd + std::string allowedRedirects[] = ALLOWED_REDIRECTS; + for( int i = 0; \ + i != sizeof( allowedRedirects ) / sizeof( allowedRedirects[0] ) + 1; \ + i++ ){ + if ( i == sizeof( allowedRedirects ) / sizeof( allowedRedirects[0] ) ) + return( false ); + else if ( allowedRedirects[i] == tmp ) + return( true ); + } + return( false ); +} + +void parseAllowedRedirects( ServerConf& server, std::stringstream& ss ){ + std::string tmp; + + while( ss >> tmp ){ + std::string error; + std::stringstream sstmp( tmp ); + sstmp >> error; + if( !validRedirect( error ) ) + throw( std::runtime_error( "Redirect Code " + error \ + + " not allowed" ) ); + ss >> tmp; + server.setAllowedRedirects( error, makePath( cutEnding( tmp ), 0 ) ); + } + if( tmp.at( tmp.size() - 1 ) != ';' ) + throw( std::runtime_error( "Line not ended on ; " + tmp ) ); +} + template< typename T > void parseRootDir( T& temp, std::stringstream& ss ){ std::string tmp; @@ -221,11 +252,13 @@ void parseIndex( T& temp, std::stringstream& ss ){ void Config::parseServerConfBlock( ServerConf& server ){ const std::string optionsArray[] = \ - { LISTEN, SERVER, ERROR, CLIENT, ROOT, AINDEX, INDEX, "use_chunked_encoding", "chunk_size"}; + { LISTEN, SERVER, ERROR, CLIENT, ROOT, AINDEX, INDEX, REDIRECT, \ + "use_chunked_encoding", "chunk_size" }; void ( *functionArray[] )( ServerConf&, std::stringstream& ) = \ { parseListen, parseServerConfName, parseErrorPage, parseBodySize, \ - parseRootDir, parseAutoIndex, parseIndex, parseChunkedEncoding, parseChunkSize}; + parseRootDir, parseAutoIndex, parseIndex, parseAllowedRedirects, \ + parseChunkedEncoding, parseChunkSize}; std::string tmp; @@ -236,7 +269,7 @@ void Config::parseServerConfBlock( ServerConf& server ){ std::stringstream ss( tmp ); std::string key; ss >> key; - for( int i = 0; i < 9; i++ ){ + for( int i = 0; i < 10; i++ ){ if( !key.empty() && key.at( 0 ) == '#' ){ break; } @@ -248,7 +281,7 @@ void Config::parseServerConfBlock( ServerConf& server ){ parseLocationConfBlock( server, ss ); break; } - else if ( i == 8 && !key.empty() ){ + else if ( i == 9 && !key.empty() ){ throw( std::runtime_error( "Not an valid configuration "\ + tmp ) );