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
11 changes: 11 additions & 0 deletions conf/badConf/badBlockLocation.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
server {
listen 0.0.0.0:8080;
root /files;
index index.html;
location {
root /hey;
hey {

}
}
}
8 changes: 8 additions & 0 deletions conf/badConf/badBlockServeer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server {
listen 0.0.0.0:8080;
root /files;
index index.html;
hey {
root /hey;
}
}
5 changes: 5 additions & 0 deletions conf/badConf/badRoot.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
server {
listen 0.0.0.0:8080;
root ;
index index.html;
}
5 changes: 5 additions & 0 deletions conf/badConf/noDirective.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
yo {
listen 0.0.0.0:8080;
root /files;
index index.html;
}
3 changes: 3 additions & 0 deletions conf/badConf/noListen.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server {
root .;
}
3 changes: 3 additions & 0 deletions conf/badConf/noRoot.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server {
listen 0.0.0.0:8080;
}
4 changes: 4 additions & 0 deletions conf/badConf/samePort.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
server {
listen 0.0.0.0:8080;
listen 127.0.0.1:8080;
}
4 changes: 2 additions & 2 deletions conf/default.conf
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion conf/test.conf
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 2 additions & 0 deletions inc/Defines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions inc/ServerConf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down Expand Up @@ -76,6 +78,9 @@ class ServerConf {

//size of each chunk
size_t _chunkSize;

//store http redirects
std::map< std::string, std::string > _allowedRedirects;
};

#endif
10 changes: 10 additions & 0 deletions src/Config/ServerConf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ServerConf::ServerConf( const ServerConf& og ){
_rootDir = og._rootDir;
_index = og._index;
_autoIndex = og._autoIndex;
_allowedRedirects = og._allowedRedirects;
}

ServerConf::~ServerConf( void ) {;}
Expand All @@ -32,6 +33,7 @@ ServerConf& ServerConf::operator =( const ServerConf& og ) {
_rootDir = og._rootDir;
_index = og._index;
_autoIndex = og._autoIndex;
_allowedRedirects = og._allowedRedirects;
}
return( *this );
}
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}
43 changes: 39 additions & 4 deletions src/Config/ServerConfParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -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 ) );

Expand All @@ -257,5 +290,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" ) );

}