LeftHookRoll
An HTTP/1.0 compliant web server, as specified by RFC1945
Loading...
Searching...
No Matches
CGIManager Class Reference

#include <CGIManager.hpp>

Collaboration diagram for CGIManager:

Public Member Functions

 CGIManager ()
 
 CGIManager (const CGIManager &other)
 
CGIManageroperator= (const CGIManager &other)
 
 ~CGIManager ()
 
pid_t getPid () const
 
int getOutputFd () const
 
void prepare (const Request &request, const std::string &scriptPath, const std::string &interpreterOverride="")
 Prepares the environment and arguments for executing a CGI script.
 
void execute (int inputFd)
 Forks, redirs input file and outpipe, and executes the CGI script.
 
bool isDone ()
 Checks if the child process has finished executing (Non-blocking); waitpid with WNOHANG.
 

Static Public Member Functions

static void cleanupAllProcesses ()
 Cleans up all active CGI processes on server shutdown. Sends SIGTERM to all processes, waits for them to finish, then sends SIGKILL to any that remain. Prevents zombie processes.
 

Private Member Functions

void _buildEnvMap (const Request &request, const std::string &scriptPath)
 
void _prepExecveArrays ()
 
void _freeExecveArrays ()
 
void _closePipes ()
 

Static Private Member Functions

static void _reapFinishedActivePids ()
 
static bool _isSpawnLimitReached ()
 
static void _registerPid (pid_t pid)
 
static void _unregisterPid (pid_t pid)
 

Private Attributes

pid_t _pId
 
int _outPipe [2]
 
std::string _query
 
std::vector< std::string > _scriptArgv
 
std::map< std::string, std::string > _env
 
char ** _execveEnvp
 
char ** _execveArgv
 

Static Private Attributes

static std::set< pid_t > _activePids
 

Detailed Description

Definition at line 14 of file CGIManager.hpp.

Constructor & Destructor Documentation

◆ CGIManager() [1/2]

CGIManager::CGIManager ( )

Definition at line 68 of file CGIManager.cpp.

◆ CGIManager() [2/2]

CGIManager::CGIManager ( const CGIManager other)

Definition at line 74 of file CGIManager.cpp.

◆ ~CGIManager()

CGIManager::~CGIManager ( )

Definition at line 98 of file CGIManager.cpp.

Member Function Documentation

◆ operator=()

CGIManager & CGIManager::operator= ( const CGIManager other)

Definition at line 80 of file CGIManager.cpp.

◆ getPid()

pid_t CGIManager::getPid ( ) const

Definition at line 106 of file CGIManager.cpp.

◆ getOutputFd()

int CGIManager::getOutputFd ( ) const
Returns
returns read end of the outpipe to add to epoll.

Definition at line 111 of file CGIManager.cpp.

◆ prepare()

void CGIManager::prepare ( const Request request,
const std::string &  scriptPath,
const std::string &  interpreterOverride = "" 
)

Prepares the environment and arguments for executing a CGI script.

Parameters
requestthe parsed HTTP request.
scriptPaththe absolute path to the script to be executed.
interpreterOverrideif non-empty, use this interpreter instead of auto-detecting from extension.

Definition at line 118 of file CGIManager.cpp.

◆ execute()

void CGIManager::execute ( int  inputFd)

Forks, redirs input file and outpipe, and executes the CGI script.

Parameters
inputFdthe fd of the temp file(data store) containing the fully received request body.
Warning
this implementation requires that the request body is fully received before executing the CGI script, we handle large request bodies by writing them to a temp file and passing the fd to the CGI script, but this also means that we can't start executing the CGI script until we've fully received the request body, which is a tradeoff we made for simplicity.

Definition at line 133 of file CGIManager.cpp.

◆ isDone()

bool CGIManager::isDone ( )

Checks if the child process has finished executing (Non-blocking); waitpid with WNOHANG.

Returns
true if the process exited, false if it is still running.

Definition at line 174 of file CGIManager.cpp.

◆ cleanupAllProcesses()

void CGIManager::cleanupAllProcesses ( )
static

Cleans up all active CGI processes on server shutdown. Sends SIGTERM to all processes, waits for them to finish, then sends SIGKILL to any that remain. Prevents zombie processes.

Definition at line 318 of file CGIManager.cpp.

◆ _buildEnvMap()

void CGIManager::_buildEnvMap ( const Request request,
const std::string &  scriptPath 
)
private

Definition at line 192 of file CGIManager.cpp.

◆ _prepExecveArrays()

void CGIManager::_prepExecveArrays ( )
private

Definition at line 229 of file CGIManager.cpp.

◆ _freeExecveArrays()

void CGIManager::_freeExecveArrays ( )
private

Definition at line 252 of file CGIManager.cpp.

◆ _closePipes()

void CGIManager::_closePipes ( )
private

Definition at line 270 of file CGIManager.cpp.

◆ _reapFinishedActivePids()

void CGIManager::_reapFinishedActivePids ( )
staticprivate

Definition at line 291 of file CGIManager.cpp.

◆ _isSpawnLimitReached()

bool CGIManager::_isSpawnLimitReached ( )
staticprivate

Definition at line 307 of file CGIManager.cpp.

◆ _registerPid()

void CGIManager::_registerPid ( pid_t  pid)
staticprivate

Definition at line 285 of file CGIManager.cpp.

◆ _unregisterPid()

void CGIManager::_unregisterPid ( pid_t  pid)
staticprivate

Definition at line 313 of file CGIManager.cpp.

Member Data Documentation

◆ _pId

pid_t CGIManager::_pId
private

Definition at line 64 of file CGIManager.hpp.

◆ _outPipe

int CGIManager::_outPipe[2]
private

Definition at line 66 of file CGIManager.hpp.

◆ _query

std::string CGIManager::_query
private

Definition at line 67 of file CGIManager.hpp.

◆ _scriptArgv

std::vector<std::string> CGIManager::_scriptArgv
private

Definition at line 68 of file CGIManager.hpp.

◆ _env

std::map<std::string, std::string> CGIManager::_env
private

Definition at line 69 of file CGIManager.hpp.

◆ _execveEnvp

char** CGIManager::_execveEnvp
private

Definition at line 71 of file CGIManager.hpp.

◆ _execveArgv

char** CGIManager::_execveArgv
private

Definition at line 72 of file CGIManager.hpp.

◆ _activePids

std::set< pid_t > CGIManager::_activePids
staticprivate

Definition at line 81 of file CGIManager.hpp.


The documentation for this class was generated from the following files: