From a3a40df15be63caecb18c82c8555340ba348036f Mon Sep 17 00:00:00 2001 From: mo Date: Tue, 15 Apr 2025 16:07:11 +0200 Subject: [PATCH] added client_max_body_size to LocationConf aswell --- conf/test.conf | 1 + inc/Location.hpp | 7 +++++ inc/ServerConf.hpp | 8 ++--- src/Config/LocationConf.cpp | 20 +++++++++++- src/Config/LocationConfParse.cpp | 52 +++++++++++++++++++++++++++++--- src/Config/ServerConfParse.cpp | 2 +- 6 files changed, 80 insertions(+), 10 deletions(-) diff --git a/conf/test.conf b/conf/test.conf index 8e319bc..a7154c8 100644 --- a/conf/test.conf +++ b/conf/test.conf @@ -25,6 +25,7 @@ server { autoindex on; cgi_path /usr/bin/python3 /usr/bin/php; cgi_ext .py .php; + client_max_body_size 1G; } location /uploads/ { diff --git a/inc/Location.hpp b/inc/Location.hpp index 0da6619..a4d6915 100644 --- a/inc/Location.hpp +++ b/inc/Location.hpp @@ -26,6 +26,8 @@ class LocationConf { const std::vector< std::string > getCgiPath( void ) const; const std::vector< std::string > getCgiExt( void ) const; const std::string getUploadDir( void ) const; + size_t getBodySize( void ) const; + bool getBodySizeInitilized( void ) const; //setter functions void setPath( std::string ); @@ -37,6 +39,7 @@ class LocationConf { void setCgiPath( std::string ); void setCgiExt( std::string ); void setUploadDir( std::string ); + void setBodySize( size_t ); //member functions void checkAccess( std::string ); @@ -66,6 +69,10 @@ class LocationConf { //Upload std::string _uploadDir; + + //body size + bool _bodySizeInitilized; + size_t _bodySize; }; std::ostream& operator <<(std::ostream& os, const LocationConf& loc); #endif diff --git a/inc/ServerConf.hpp b/inc/ServerConf.hpp index bb09bc9..7a71859 100644 --- a/inc/ServerConf.hpp +++ b/inc/ServerConf.hpp @@ -72,10 +72,10 @@ class ServerConf { bool _autoIndex; //flag to indicate if chunked transfer is enabled - bool _chunkedTransfer; - - //size of each chunk - size_t _chunkSize; + bool _chunkedTransfer; + + //size of each chunk + size_t _chunkSize; }; #endif diff --git a/src/Config/LocationConf.cpp b/src/Config/LocationConf.cpp index 48c9608..399bf24 100644 --- a/src/Config/LocationConf.cpp +++ b/src/Config/LocationConf.cpp @@ -1,7 +1,7 @@ #include "Location.hpp" //constructors -LocationConf::LocationConf( void ) : _autoIndex( 0 ) {;} +LocationConf::LocationConf( void ) : _autoIndex( 0 ), _bodySizeInitilized( false ), _bodySize( 0 ) {;} LocationConf::~LocationConf( void ) {;} @@ -17,6 +17,8 @@ LocationConf::LocationConf( const LocationConf& og ){ _cgiPath = og._cgiPath; _cgiExt = og._cgiExt; _uploadDir = og._uploadDir; + _bodySize = og._bodySize; + _bodySizeInitilized = og._bodySizeInitilized; } //operator overloads @@ -33,6 +35,8 @@ LocationConf& LocationConf::operator =( const LocationConf& og ){ _cgiPath = og._cgiPath; _cgiExt = og._cgiExt; _uploadDir = og._uploadDir; + _bodySize = og._bodySize; + _bodySizeInitilized = og._bodySizeInitilized; } return( *this ); } @@ -154,6 +158,15 @@ int LocationConf::getAutoIndex( void ) const{ return( _uploadDir ); } +size_t LocationConf::getBodySize( void ) const{ + return( _bodySize ); +} + +bool LocationConf::getBodySizeInitilized( void ) const{ + return( _bodySizeInitilized ); +} + + //setter funcitons void LocationConf:: setPath( std::string path ){ _path = path; @@ -190,3 +203,8 @@ void LocationConf:: setCgiExt( std::string key ){ void LocationConf:: setUploadDir( std::string uploadDir ){ _uploadDir = uploadDir; } + +void LocationConf::setBodySize( size_t val ){ + _bodySize = val; + _bodySizeInitilized = true; +} diff --git a/src/Config/LocationConfParse.cpp b/src/Config/LocationConfParse.cpp index ee11db3..2f3312c 100644 --- a/src/Config/LocationConfParse.cpp +++ b/src/Config/LocationConfParse.cpp @@ -146,13 +146,57 @@ void parseUploadDir( LocationConf& location, std::stringstream& ss ){ throw( std::runtime_error( "Line not ended on ; " + tmp ) ); } +static size_t 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)) { + throw( std::runtime_error( "Wrong Max Body Size" ) ); + return 0; + } + + // 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') { + throw( std::runtime_error( "Wrong Max Body Size" ) ); + return 0; + } + + size_t result = sizeValue * multiplier; + return result; +} + +static void parseBodySize( LocationConf& location, std::stringstream& ss ){ + std::string tmp; + + while( ss >> tmp ){ + size_t size; + if( tmp.at( tmp.size() - 1 ) != ';' ) + throw( std::runtime_error( "Line not ended on ; " + tmp ) ); + if(tmp[0] == '-'){ + throw( std::runtime_error( "negativ values not allowed " + tmp ) ); + } + size = ParseSizeWithUnits(tmp); + location.setBodySize( size ); + } +} + void Config::parseLocationConfBlock( ServerConf& server, std::stringstream& ss ){ const std::string optionsArray[] = \ - { METHODE, REDIRECT, ROOT, AINDEX, INDEX, CGIPATH, CGIEXT, UPDIR }; + { METHODE, REDIRECT, ROOT, AINDEX, INDEX, CGIPATH, CGIEXT, UPDIR, CLIENT }; void ( *functionArray[] )( LocationConf&, std::stringstream& ) = \ { parseAllowedMethods, parseAllowedRedirects, parseRootDir, \ - parseAutoIndex, parseIndex, parseCgiPath, parseCgiExt, parseUploadDir }; + parseAutoIndex, parseIndex, parseCgiPath, parseCgiExt, parseUploadDir, parseBodySize }; LocationConf location; std::string tmp; @@ -165,12 +209,12 @@ void Config::parseLocationConfBlock( ServerConf& server, std::stringstream& ss ) std::stringstream ss( tmp ); std::string key; ss >> key; - for( int i = 0; i < 8; i++ ){ + for( int i = 0; i < 9; i++ ){ if( optionsArray[ i ] == key ){ functionArray[ i ]( location, ss ); break; } - else if ( i == 7 && !key.empty() ) + else if ( i == 8 && !key.empty() ) throw( std::runtime_error( "Not an valid configuration " + tmp ) ); } diff --git a/src/Config/ServerConfParse.cpp b/src/Config/ServerConfParse.cpp index ddf9403..0335728 100644 --- a/src/Config/ServerConfParse.cpp +++ b/src/Config/ServerConfParse.cpp @@ -112,7 +112,7 @@ void parseErrorPage( ServerConf& server, std::stringstream& ss ){ throw( std::runtime_error( "Line not ended on ; " + tmp ) ); } -size_t ParseSizeWithUnits(std::string str){ +static size_t ParseSizeWithUnits(std::string str){ size_t multiplier = 1; char unit = str[str.size() -2]; // skip '0\ and ;