Informatics Engineering    
   
Table of contents
(Prev) Common Development and Distrib ...Common Language Runtime (Next)

Common Gateway Interface

Common Gateway Interface (CGI) is a standard method for web server software to delegate the generation of web content to executable files. Such files are known as CGI scripts or simply CGIs; they are usually written in a scripting language.

Contents

History

In 1993, the World Wide Web (WWW) was small but booming. WWW software developers and web site developers kept in touch on the www-talk mailing list, so it was there that a standard for calling command line executables was agreed upon. The NCSA team wrote the specification;[1] however, NCSA no longer hosts this.[2][3] The other web server developers adopted it, and it has been a standard for web servers ever since. A work group chaired by Ken Coar started in November 1997 to get the NCSA definition of CGI more formally defined.[4] This work resulted in RFC 3875, which specified CGI Version 1.1 was specified in RFC 3875. Specifically mentioned in the RFC are the following contributors:[5]

  • Rob McCool (author of the NCSA httpd web server)
  • John Franks (author of the GN web server)
  • Ari Luotonen (the developer of the CERN httpd web server)
  • Tony Sanders (author of the Plexus web server)
  • George Phillips (web server maintainer at the University of British Columbia)

Syntax

The following CGI program shows all the environment variables passed by the web server:

#!/usr/bin/perl =head1 DESCRIPTION printenv—demo CGI program which just prints its environment =cut use strict;use warnings; print "Content-type: text/plain\n\n"; for my $var ( sort keys %ENV ) { my $value = $ENV{$var}; $value =~ s/\n/\n/g; $value =~ s/"/"/g; print qq[$var="$value"\n];}

If a web browser issues a request for the environment variables at http://example.com/cgi-bin/printenv.p l/foo/bar?var1=value1&var2=with%2 0percent%20encoding, a 64-bit Microsoft Windows web server running cygwin returns the following information:

 COMSPEC="C:\Windows\system32\cmd.exe" DOCUMENT_ROOT="C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs" GATEWAY_INTERFACE="CGI/1.1" HOME="/home/SYSTEM" HTTP_ACCEPT="text/html,application/xhtml+xml,appl ication/xml;q=0.9,*/*;q=0.8" HTTP_ACCEPT_CHARSET="ISO-8859-1,utf-8;q=0.7,*;q=0.7" HTTP_ACCEPT_ENCODING="gzip, deflate" HTTP_ACCEPT_LANGUAGE="en-us,en;q=0.5" HTTP_CONNECTION="keep-alive" HTTP_HOST="example.com" HTTP_USER_AGENT="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0" PATH="/home/SYSTEM/bin:/bin:/cygdrive/c/pr ogra~2/php:/cygdrive/c/windows/system 32:..." PATHEXT=".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.J SE;.WSF;.WSH;.MSC" PATH_INFO="/foo/bar" PATH_TRANSLATED="C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\foo\bar" QUERY_STRING="var1=value1&var2=with%20percent% 20encoding" REMOTE_ADDR="127.0.0.1" REMOTE_PORT="63555" REQUEST_METHOD="GET" REQUEST_URI="/cgi-bin/printenv.pl/foo/bar?var1=va lue1&var2=with%20percent%20encodi ng" SCRIPT_FILENAME="C:/Program Files (x86)/Apache Software Foundation/Apache2.2/cgi-bin/printenv .pl" SCRIPT_NAME="/cgi-bin/printenv.pl" SERVER_ADDR="127.0.0.1" SERVER_ADMIN="(server admin's email address)" SERVER_NAME="127.0.0.1" SERVER_PORT="80" SERVER_PROTOCOL="HTTP/1.1" SERVER_SIGNATURE="" SERVER_SOFTWARE="Apache/2.2.19 (Win32) PHP/5.2.17" SYSTEMROOT="C:\Windows" TERM="cygwin" WINDIR="C:\Windows"

From the environment, it can be seen that the web browser is Firefox running on a Windows 7 PC, the web server is Apache running on a system which emulates Unix, and the CGI script is named cgi-bin/printenv.pl.

The program could then generate any content, write that to standard output, and the web server will transmit it to the browser.

The following are environment variables passed to CGI programs:

  • Server specific variables:
    • SERVER_SOFTWARE: name/version of HTTP server.
    • SERVER_NAME: host name of the server, may be dot-decimal IP address.
    • GATEWAY_INTERFACE: CGI/version.
  • Request specific variables:
    • SERVER_PROTOCOL: HTTP/version.
    • SERVER_PORT: TCP port (decimal).
    • REQUEST_METHOD: name of HTTP method (see above).
    • PATH_INFO: path suffix, if appended to URL after program name and a slash.
    • PATH_TRANSLATED: corresponding full path as supposed by server, if PATH_INFO is present.
    • SCRIPT_NAME: relative path to the program, like /cgi-bin/script.cgi.
    • QUERY_STRING: the part of URL after ? character. The query string may be composed of *name=value pairs separated with ampersands (such as var1=val1&var2=val2...) when used to submit form data transferred via GET method as defined by HTML application/x-www-form-urlencoded.
    • REMOTE_HOST: host name of the client, unset if server did not perform such lookup.
    • REMOTE_ADDR: IP address of the client (dot-decimal).
    • AUTH_TYPE: identification type, if applicable.
    • REMOTE_USER used for certain AUTH_TYPEs.
    • REMOTE_IDENT: see ident, only if server performed such lookup.
    • CONTENT_TYPE: Internet media type of input data if PUT or POST method are used, as provided via HTTP header.
    • CONTENT_LENGTH: similarly, size of input data (decimal, in octets) if provided via HTTP header.
    • Variables passed by user agent (HTTP_ACCEPT, HTTP_ACCEPT_LANGUAGE, HTTP_USER_AGENT, HTTP_COOKIE and possibly others) contain values of corresponding HTTP headers and therefore have the same sense.

The program returns the result to the web server in the form of standard output, beginning with a header and a blank line.

The header is encoded in the same way as an HTTP header and must include the MIME type of the document returned.[6] The headers, supplemented by the web server, are generally forwarded with the response back to the user.

Deployment

A web server that supports CGI can be configured to interpret a URL that it serves as a reference to a CGI script. A common convention is to have a cgi-bin/ directory at the base of the directory tree and treat all executable files within this directory (and no other, for security) as CGI scripts. Another popular convention is to use filename extensions; for instance, if CGI scripts are consistently given the extension .cgi, the web server can be configured to interpret all such files as CGI scripts. While convenient, and required by many prepackaged scripts, it opens the server to attack if a remote user can upload executable code with the proper extension.

In the case of HTTP PUT or POSTs, the user-submitted data is provided to the program via the standard input. The web server creates a subset of the environment variables passed to it and adds details pertinent to the HTTP environment.

Uses

An example of a CGI program is one implementing a wiki. The user agent requests the name of an entry; the program retrieves the source of that entry's page (if one exists), transforms it into HTML, and sends the result. If the "Edit this page" link is clicked, the CGI populates an HTML textarea or other editing control with the page's contents, and saves it back to the server when the user submits the form.

Alternatives

Calling a command generally means the invocation of a newly created process on the server. Starting the process can consume much more time and memory than the actual work of generating the output, especially when the program still needs to be interpreted or compiled. If the command is called often, the resulting workload can quickly overwhelm the web server.

The overhead involved in interpretation may be reduced by using compiled CGI programs, such as those in C/C++, rather than using Perl or other interpreted languages. The overhead involved in process creation can be reduced by solutions such as FastCGI that "prefork" interpreter processes, or by running the application code entirely within the web server using extension modules such as mod_php.

Several approaches can be adopted for remedying this:

  • The popular Web servers developed their own extension mechanisms that allows third-party software to run inside the web server itself, such as Apache modules, NSAPI plugins and ISAPI plugins.
  • Simple Common Gateway Interface or SCGI
  • FastCGI allows a single, long-running process to handle more than one user request while keeping close to the CGI programming model, retaining the simplicity while eliminating the overhead of creating a new process for each request. Unlike converting an application to a web server plug-in, FastCGI applications remain independent of the web server.
  • Replacement of the architecture for dynamic websites can also be used. This is the approach taken by solutions including Java Platform, Enterprise Edition (a.k.a. Java EE), which runs Java code in a Java servlet container in order to serve dynamic content and optionally static content. This approach replaces the overhead of generating and destroying processes with the much lower overhead of generating and destroying threads, and also exposes the programmer to the library that comes with Java Platform, Standard Edition that the version of Java EE in use is based on.

The optimal configuration for any web application depends on application-specific details, amount of traffic, and complexity of the transaction; these tradeoffs need to be analyzed to determine the best implementation for a given task and time budget.

See also

References

External links

  • Cgicc, FSF C++ library for CGI request parsing and HTML response generation
  • CGI, a standard Perl module for CGI request parsing and HTML response generation
Web server interfaces
 
Protocols
 
APIs
 
Apache modules
 
Web APIs
(Prev) Common Development and Distrib ...Common Language Runtime (Next)