Skip to content

CGI documentation

NHariman edited this page Nov 4, 2022 · 3 revisions

CGIHandler and CGI class

CGIHandler class is used to prepare a request for CGI use. After the CGI executes the results are then saved in Response.

CGIHandler class

Methods CGIHandler has two public methods:

std::istream* 		CGIHandler::Execute(Request *request, Response &response);

Execute() executes the cgi pointed to in Request, and saves the headers and content in Response.

std::string			CGIHandler::GetContent() const;

GetContent() retrieves the content saved in the CGI class through the CGIHandler.

CGI Class

The CGI class is only called within CGIHandler, and is used in the Request to get content and headers back from the CGI. Methods CGI class has three public methods.

bool    	Setup(Request *request);

Prepares the CGI for use, uses the TargetConfig class within the Request to get the CGI data from the cgi_pass directive and validates if the CGI can be executed.

size_t    	Execute();

Executes the CGI, this behaviour is dependent on the Request method. If it's a GET, the query string from request is used and processed in the CGI it does this by setting the Query string in the QUERY_STRING environment variable which is passed to the CGI. If the method is a POST it creates two pipes (one for reading, one for writing) and uses the writing pipe to write the query string in request to the stdin, for use in the CGI.

The read pipe is used in both GET and POST to retrieve the CGI output.

Headers are extremely important when setting up the CGI as they use these environment variables the most important ones are content_type and query_string as those provide the info necessary to process the query correctly. In the event of a GET request, the PORT environment variable MUST be set, even if empty.