Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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/ {
Expand Down
7 changes: 7 additions & 0 deletions inc/Location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions inc/ServerConf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 19 additions & 1 deletion src/Config/LocationConf.cpp
Original file line number Diff line number Diff line change
@@ -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 ) {;}

Expand All @@ -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
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
52 changes: 48 additions & 4 deletions src/Config/LocationConfParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/Config/ServerConfParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;

Expand Down