The Common Gateway Interface (CGI), originally developed as part of the NCSA HTTP server, and is an old standard for interfacing external applications with HTTP servers that still enjoys considerable use. It was created to allow dynamic data to be generated in response to HTTP requests and return the results to the user's browser. Plain HTML documents are typically static, while a CGI program allows the response data to be dynamically created. However, since CGI was first developed, several better means of creating dynamic web pages have been created that are faster and more efficient. Read more about such replacements in Creating Dynamic Web Pages, Embedded Server Pages and Using PHP.
Mbedthis AppWeb supports CGI so that existing applications that are written to the CGI interface can be fully supported. AppWeb has a fully featured CGI handler that alleviates many of the pains with configuring CGI setup. Configuring CGI ProgramsCGI programs may be configured and invoked in two primary ways:
AppWeb nominates a directory as a CGI directory via the ScriptAlias configuration file directive. For example: ScriptAlias /cgi-bin/ $SERVER_ROOT/web/cgi-bin/ When a URL is requested by a browser which includes the "/cgi-bin/" prefix, the script name immediately after "/cgi-bin/" will be run. For example, the following URL: https://mbedthis.com/cgi-bin/testCgi This will cause the testCgi program to be run. To configure AppWeb to specify CGI programs and scripts by URL extension use the AddHandler configuration file directive. For example: AddHandler cgiHandler .myExt This configures AppWeb to process URLs that contain the .myExt extension via the CGI handler. To determine which program to run, the AppWeb CGI handler looks up the Mime type associated with the ".myExt" extension in the Mime types file "mime.types". In this file, the extension is mapped to a mime type. For example: application/x-appWeb-perl myExt This definition will map ".myExt" to the perl mime type. This mime type must then be mapped to a program via the the Action directive. For example: Action application/x-appWeb-perl /usr/bin/perl This will cause /usr/bin/perl to be run to process the request. Output from perl is captured by the CGI handler and then returned to the user's browser. Invoking CGI ProgramsWhen a CGI program is run, the AppWeb CGI handler communicates request information to the CGI program via Environment variables and in some cases, via the command line. The command line is set to the name of the CGI program, CGI script if different to the program name, and the CGI Query String. The query string is set to the portion of the URL after any "?" character after de-escaping special characters.CGI Command LineThe command line will be set differently depending on how the CGI program is being invoked. There are four possible scenarios:
Programs Invoked Directly via the Request URL
Programs Invoked Indirectly with Bang DirectiveIf the CGI program/script specified in the URL contains a "#!/pathToProgram" directive on the first line, it is interpreted to be the path to the real CGI program to run. The script name is then passed in the command line.
Programs Specified via an ActionProgram Directive
Windows Batch Commands
The "Command" is a quoted string set to the name of the CGI script originally specified in the URL followed by the Query String split at "+" characters. The entire Command string is escaped so that dangerous characters are preceded by "^" to prevent security attacks. CGI Environment VariablesCGI uses environment variables to send your program its additional parameters. The following environment variables are defined :
ExampleConsider the following URL which will run the Perl interpreter to execute the pricelists.pl script. |
Variable |
Value |
PATH_INFO |
/products/pricelists |
PATH_TRANSLATED |
/var/appWeb/web/products/pricelists # where /var/appWeb/web is the DocumentRoot |
QUERY_STRING |
id=23&payment=credit+Card |
REQUEST_URI |
/cgi-bin/myScript/products/pricelists?id=23&payment=credit+Card |
SCRIPT_NAME |
myScript |
ARGV[0] |
/usr/bin/perl |
ARGV[1] |
pricelists.pl |
ARGV[2] |
id=23&payment=creditCard |
This URL below demonstrates some rather cryptic encoding of URLs. The important thing to remember is that command line arguments are delimited by "+". The hex encoding %20, is the encoding for the space character. Once passed to the CGI program, the convention is for CGI variables to be delimited by "&".
http://hostname/cgi-bin/cgiProgram/extra/Path?var1=a+a&var2=b%20b&var3=c
This URL will cause the following environment settings:
Variable |
Value |
PATH_INFO |
/extra/Path |
PATH_TRANSLATED |
/var/appWeb/web/extra/Path |
QUERY_STRING |
var1=a+a&var2=b%20b&var3=c |
REQUEST_URI |
/cgi-bin/cgiProgram/extra/Path?var1=a+a&var2=b%20b&var3=c |
SCRIPT_NAME |
cgiProgram |
ARGV[0] |
cgiProgram |
ARGV[1] |
var1=a |
ARGV[2] |
a&var2=b b&var3=c |
Header |
Description |
Content-type |
Nominate the content Mime Type. Typically "text/html". See the mime.types for a list of possible mime types. |
Status |
Set to a HTTP response code. Success is 200. Server error is 500. |
Location |
Set to the URI of a new document to which to redirect the client's browser. |
ANY |
Pass any other header back to the client. |
Content-type: text/htmlSample CGI Output Hello World
Location: /newUrl.html
Status: 500