Using CGI

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 Programs

CGI programs may be configured and invoked in two primary ways:
  • By URL prefix
  • By URL extension
When invoked by URL prefix, the CGI programs and scripts are stored in special CGI directories (for example cgi-bin). When invoked by URL extension, the CGI programs may be stored anywhere in the web directory. For security, it is usually best to store all CGI programs and scripts outside the directory containing the web content. Consequently invoking CGI programs by extension should only be used in combination with a URL prefix that allows the CGI directory to be specified.

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:

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 Programs

When 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 Line

The command line will be set differently depending on how the CGI program is being invoked. There are four possible scenarios:
  • Program invoked directly via the request URL.
  • Program invoked indirectly if the CGI script contains a Bang path directive.
  • The program is specified via an ActionProgram directive in the AppWeb configuration file.
  • On windows if the program is a Windows Batch file
The command line arguments for the CGI program will be set differently in each case. See the tables below for the specifications as to how the command line arguments are defined:

Programs Invoked Directly via the Request URL

Program name immediately after the CGI URL prefix (E.g. after /cgi-bin/)
Each arg is set to portions of the QUERY_STRING is split at "+" characters after de-escaping the query.

Programs Invoked Indirectly with Bang Directive

If 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.

Program name defined in the first line of the CGI script after the "#!" characters.
The name of the CGI script originally specified in the URL.
Each arg is set to portions of the QUERY_STRING is split at "+" characters after de-escaping the query.

Programs Specified via an ActionProgram Directive

Program name specified in the ActionProgram directive in the AppWeb configuration file.
The name of the CGI script originally specified in the URL.
Each arg is set to portions of the QUERY_STRING is split at "+" characters after de-escaping the query.

Windows Batch Commands

Set to "cmd.exe"

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 Variables

CGI uses environment variables to send your program its additional parameters. The following environment variables are defined :

Set to the value of the HTTP AUTHORIZATION header. Usually "basic" or "digest".
Set to the length of any associated posted content.
Set to the path location of the web documents. Defined by the DocumentRoot directive in the AppWeb configuration file.
Set to "CGI/1.1"
Set to the value of the HTTP ACCEPT header. This specifies what formats are acceptable and/or preferable for the client.
Set to the value of the HTTP CONNECTION header. This specifies how the connection should be persisted when the request is complete. (Keep-alive)
Set to the value of the HTTP HOST header. This specifies the name of the server to process the request. When using named virtual hosting, requests to different servers (hosts) may be processed by a single HTTP server on a single IP address. The HTTP_HOST field permits the server to determine which virtual host should process the request.
Set to the value of the HTTP USER_AGENT header.
The PATH_INFO variable is set to the URL portion (if any) after the SCRIPT_NAME.
The physical on-disk path name corresponding to PATH_INFO.
The QUERY_STRING variable is set to the URL string portion that follows the first "?" in the URL. The QUERY_STRING is URL encoded in the standard URL format by changing spaces to "+", and encoding all URL special characters with %xx hexadecimal encoding. Most major scripting languages provide routines to assist in decoding QUERY_STRINGs.
Set to the IP address of the requesting client.
Set to the IP address of the requesting client (same as REMOTE_ADDR).
Set to the name of the authenticated user.
Set to the HTTP method used by the request. Valid values are: "GET", "HEAD", "OPTIONS", "POST", or "TRACE". "PUT" and "DELETE" are not supported.
The complete request URL after the host name portion. It always begins with a leading "/".
The name of the CGI script or program to run. If an ActionProgram is specifying the name of a CGI interpreter, then SCRIPT_NAME is set to the name of the script to interpret.
The IP address of the server or virtual host responding to the request.
The name of the default server or virtual host serving the request.
The HTTP port of the server or virtual host serving the request.
Set to "HTTP/1.0" or "HTTP/1.1" depending on the protocol used by the client.
Set to "Mbedthis AppWeb/VERSION"


Consider the following URL which will run the Perl interpreter to execute the script.


This URL will cause the following environment settings:

/var/appWeb/web/products/pricelists # where /var/appWeb/web is the DocumentRoot

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 "&".


This URL will cause the following environment settings:

a&var2=b b&var3=c

URL Encoding

When a URL is sent via HTTP certain special characters must be escaped so the URL can be processed unambiguously by the HTTP server.  To escape the special characters, a HTTP client should convert them to their %hex equivalent. Form and query variables are separated by "&". For example: a=1&b=2 defines two form variables "a" and "b" with their values equal to "1" and "2" respectively.

CGI Programming

A CGI program can return almost any possible content type back to the client's browser: plain HTML, audio, video or any other format. CGI programs can also control the user's browser and redirect it to another URL. To do this, CGI programs return pseudo-HTTP headers that are interpreted by AppWeb before passing the data on to the client.

AppWeb understands the following CGI headers that can be output by the CGI program. They are case-insensitive.

Nominate the content Mime Type. Typically "text/html". See the mime.types for a list of possible mime types.
Set to a HTTP response code. Success is 200. Server error is 500.
Set to the URI of a new document to which to redirect the client's browser.
Pass any other header back to the client.

For example:

Content-type: text/html
Sample CGI Output

Hello World

To redirect the browser to a new location:

Location: /newUrl.html

To signify an error in the server:

Status: 500

Hints and Tips

If you have special data or environment variables that must be passed to your CGI program, you can wrap it with a script that defines that environment before invoking your script.

Other Resources

The following URLs may be helpful in further reading about CGI:

  • For an introduction to CGI:
  • For the actual CGI specification:
  • Other CGI resources:

© Mbedthis Software LLC, 2003-2204. All rights reserved. Mbedthis is a trademark of Mbedthis Software LLC.