From e01e7bbcede24892284520f4beb8d30c3aa4282b Mon Sep 17 00:00:00 2001 From: Linus Eschenbach Date: Tue, 15 Apr 2025 16:08:49 +0200 Subject: [PATCH] added throwing error codes to cgi handeling --- cgi-bin/test1.py | 16 ++++++++++++++++ src/Cgi/Cgi.cpp | 34 +++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 cgi-bin/test1.py diff --git a/cgi-bin/test1.py b/cgi-bin/test1.py new file mode 100644 index 0000000..38f4168 --- /dev/null +++ b/cgi-bin/test1.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +import cgi +import cgitb +cgitb.enable() + +print("Content-type: text/html\n") +print("") +print("

Python CGI Test

") + +form = cgi.FieldStorage() +if "name" in form: + print(f"

Hello, {form['name'].value}!

") +else: + print("

No name provided

") + +print("") \ No newline at end of file diff --git a/src/Cgi/Cgi.cpp b/src/Cgi/Cgi.cpp index b4b61b2..2d12be8 100644 --- a/src/Cgi/Cgi.cpp +++ b/src/Cgi/Cgi.cpp @@ -80,7 +80,7 @@ static int checkFile( HttpRequest& req, std::string& interpreter ){ //get vectors if( getVectors( req.getServer(), ext, path, req.getUri() ) == -1 ){ - return( -1 ); + return( 404); } @@ -91,7 +91,7 @@ static int checkFile( HttpRequest& req, std::string& interpreter ){ //return if no ending is found ".cgi" if( dot == std::string::npos ){ - return( -1 ); + return( 404); } @@ -103,19 +103,18 @@ static int checkFile( HttpRequest& req, std::string& interpreter ){ //return if ending is not inside cgiExt if( it == ext.end() ){ - return( -1 ); + return( 404); } - - // Construct the full file path by combining the root directory with the URI - - - std::string fullPath = "." + uri; //check if file is accesible - if( access( fullPath.c_str(), X_OK ) == -1 ){ - return( -1 ); + if( access( fullPath.c_str(), F_OK ) == -1 ){ + return( 404); } + //check if file is executealbe + if( access( fullPath.c_str(), X_OK ) == -1 ){ + return( 403); + } std::vector< std::string >::iterator extIt = ext.begin(); std::vector< std::string >::iterator pathIt = path.begin(); @@ -129,10 +128,10 @@ static int checkFile( HttpRequest& req, std::string& interpreter ){ } if( interpreter.empty() ){ //std::cerr << "DEBUG: No interpreter found for file extension " << end << std::endl; - return( -1 ); + return( 500); } //std::cerr << "DEBUG: Interpreter for " << uri << " is " << interpreter << std::endl; - return( 0 ); + return( 200 ); } // COMMENTED OUT BECAUSE WE DONT NEED AND REQUEST DOESNT HAVE FD ANYMORE @@ -373,6 +372,7 @@ static int parentProcess( HttpRequest& req, int* inputPipe, int* outputPipe, pid close( outputPipe[0] ); kill( pid, SIGKILL ); waitpid( pid, &status, 0 ); + throw(500); return( -1 ); } @@ -383,6 +383,7 @@ static int parentProcess( HttpRequest& req, int* inputPipe, int* outputPipe, pid kill( pid, SIGKILL ); waitpid( pid, &status, 0 ); req.setResponseCode( 504 ); + throw(504); return( -1 ); } else{ @@ -390,6 +391,7 @@ static int parentProcess( HttpRequest& req, int* inputPipe, int* outputPipe, pid if( bytesRead > MAX_BYTES ){ std::cerr << "CGI: Limit exceeded" << std::endl; kill( pid, SIGKILL ); + throw(500); break; } if( bytesRead > 0 ){ @@ -429,6 +431,7 @@ int handleCgi( HttpRequest& req ){ //std::cout << "body size " << body.size() << "max body size " << maxBodySize << "\n"; if(body.size() > maxBodySize ){ std::cerr << RED << "ERROR: Cgi: POST TO BIG" << END << std::endl; + throw(413); return (-1);//mabe add a error page here } while (!body.empty() && (body[body.size()-1] == '\n' || body[body.size()-1] == '\r')) { @@ -439,21 +442,25 @@ int handleCgi( HttpRequest& req ){ } //check file ending and access to it and store interpreter for executuing std::string interpreter; - if( checkFile( req, interpreter ) == -1 ){ + int checkFileRes = checkFile( req, interpreter ); + if( checkFileRes != 200){ std::cerr << RED << "ERROR: Cgi: not found " << req.getUri() << END << \ std::endl; + throw(checkFileRes); return( -1 ); } //open pipes for sending data if( pipe( inputPipe ) < 0 ){ std::cerr << RED << "ERROR: Cgi: Pipe" << END << std::endl; + throw(500); return( -1 ); } if( pipe( outputPipe ) < 0 ){ std::cerr << RED << "ERROR: Cgi: Pipe" << END << std::endl; closePipe( inputPipe ); + throw(500); return( -1 ); } @@ -466,6 +473,7 @@ int handleCgi( HttpRequest& req ){ std::cerr << RED << "ERROR: Cgi: Fork" << END << std::endl; closePipe( inputPipe ); closePipe( outputPipe ); + throw(500); return( -1 ); }