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
10 changes: 4 additions & 6 deletions conf/default.conf
Original file line number Diff line number Diff line change
@@ -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;

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

}
33 changes: 33 additions & 0 deletions conf/dif_root.conf
Original file line number Diff line number Diff line change
@@ -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;
}

}
6 changes: 3 additions & 3 deletions inc/Location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 );
Expand All @@ -55,7 +55,7 @@ class LocationConf {
std::string _rootDir;

//directory listing
bool _autoIndex;
int _autoIndex;

//index
std::vector< std::string > _index;
Expand Down
2 changes: 1 addition & 1 deletion inc/Response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions 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( false ) {;}
LocationConf::LocationConf( void ) : _autoIndex( 0 ) {;}

LocationConf::~LocationConf( void ) {;}

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

Expand Down Expand Up @@ -171,7 +171,7 @@ void LocationConf:: setRootDir( std::string rootDir ){
_rootDir = rootDir;
}

void LocationConf:: setAutoIndex( bool status ){
void LocationConf:: setAutoIndex( int status ){
_autoIndex = status;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Config/LocationConfParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
Expand Down
47 changes: 36 additions & 11 deletions src/Config/ServerConfParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static bool checkIp( std::string tmp ){
tmp = tmp.substr( pos + 1, tmp.size() );
i = pos;
pos = tmp.find( '.' );
}
}
return( true );
}

Expand Down Expand Up @@ -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 ) )
Expand All @@ -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 );
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 ) != ';' )
Expand All @@ -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 ) );
}
Expand Down Expand Up @@ -227,5 +252,5 @@ void Config::parseServerConfBlock( ServerConf& server ){
getline( _configFile, tmp );
}
server.checkAccess();

}
22 changes: 15 additions & 7 deletions src/Response/get_Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> indexFiles = _serverConf->getIndex();
if (!indexFiles.empty()) {
for (std::vector<std::string>::iterator it = indexFiles.begin(); it != indexFiles.end(); ++it) {
Expand All @@ -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;
Expand Down Expand Up @@ -95,21 +102,22 @@ 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()){
//check for location index file
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;
Expand Down
6 changes: 1 addition & 5 deletions src/Response/helpers_Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ std::string Response::generateDirectoryListing(const std::string& path) {

// Add row to table
html << "<tr><td><a href=\"";
html << path; // Include the full directory path
if (path[path.length() - 1] != '/') {
html << "/";
}
html << name; // Append the file/directory name
html << name; // Append only the file/directory name
if (S_ISDIR(statbuf.st_mode)) {
html << "/"; // Add trailing slash for directories
}
Expand Down
2 changes: 1 addition & 1 deletion src/Response/post_Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int Response::checkContentLength(HttpRequest& ReqObj){
size_t maxBodySize = _serverConf->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;
Expand Down