Herong's Tutorial Notes on Perl - Part B
Dr. Herong Yang, Version 4.11

Common Gateway Interface (CGI)

Part:   1   2 

This chapter describes:

  • What is Common Gateway Interface (CGI).
  • CGI environment variables provided by IIS.
  • Query string.
  • CGI application example, Calculator.pl.

What Is Common Gateway Interface (CGI)

Common Gateway Interface (CGI) is a protocol defines how a Web server program interacts with application programs. The interactions are illustrated in the following diagram:

Web                  Web      CGI        Appl.
Browser   Internet   Server   Protocol   Program  
          HTTP                Env. Var.
          request             stdin
Send --------------> Convert ----------> Process
                                            |
          HTTP                              |
          response            stdout        V
Receive <----------- Convert <---------- Process

There are several basic rules with CGI:

  • Web server provides most of the input information to application programs through environment variables.
  • Data send in the HTTP request with the GET method is converted to a special environment variable, QUERY_STRING.
  • Data send in the HTTP request with the POST method is converted to the standard input (stdin) channel.
  • Data printed to the standard output (stdout) channel is converted to the HTTP response.

IIS Environment Variables

To know how many environment variables are provided by the Web server, and what information they are containing, I wrote the follow Perl CGI program, CgiEnv.pl.

#- CgiEnv.pl
#- Copyright (c) 1996 by Dr. Herong Yang
#
   print "Content-Type: text/html\n\n";
   print "<html><body>\n";
   foreach $var (sort keys(%ENV)) {
      print($var, ' = ', $ENV{$var}, "<br/>\n");
   }
   print "</html></body>\n";

Running it directly in a command window, I got:

Content-Type: text/html

<html><body>
ALLUSERSPROFILE = C:\Documents and Settings\All Users.WINNT<br/>
APPDATA = C:\Documents and Settings\herong\Application Data<br/>
COMMONPROGRAMFILES = C:\Program Files\Common Files<br/>
COMPUTERNAME = localhost<br/>
COMSPEC = C:\WINNT\system32\cmd.exe<br/>
HOMEDRIVE = C:<br/>
HOMEPATH = \Documents and Settings\herong<br/>
LOGONSERVER = \\localhost<br/>
NUMBER_OF_PROCESSORS = 1<br/>
OS = Windows_NT<br/>
OS2LIBPATH = C:\WINNT\system32\os2\dll;<br/>
PATH = D:\Perl\bin\;C:\WINNT\system32;C:\WINNT;...
PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH<br/>
PROCESSOR_ARCHITECTURE = x86<br/>
PROCESSOR_IDENTIFIER = x86 Family 6 Model 8 Stepping 1, GenuineIntel...
PROCESSOR_LEVEL = 6<br/>
PROCESSOR_REVISION = 0801<br/>
PROGRAMFILES = C:\Program Files<br/>
PROMPT = $P$G<br/>
SESSIONNAME = Console<br/>
SYSTEMDRIVE = C:<br/>
SYSTEMROOT = C:\WINNT<br/>
TEMP = C:\DOCUME~1\herong\LOCALS~1\Temp<br/>
TMP = C:\DOCUME~1\herong\LOCALS~1\Temp<br/>
USERDOMAIN = home<br/>
USERNAME = herong<br/>
USERPROFILE = C:\Documents and Settings\herong<br/>
WINDIR = C:\WINNT<br/>
</html></body>

Running it through IIS as a CGI application, I got:

ALLUSERSPROFILE = C:\Documents and Settings\All Users.WINNT
COMMONPROGRAMFILES = C:\Program Files\Common Files
COMPUTERNAME = localhost
COMSPEC = C:\WINNT\system32\cmd.exe
CONTENT_LENGTH = 0
GATEWAY_INTERFACE = CGI/1.1
HTTPS = off
HTTP_ACCEPT = */*
HTTP_ACCEPT_LANGUAGE = en-us
HTTP_CONNECTION = Keep-Alive
HTTP_HOST = localhost
HTTP_USER_AGENT = Mozilla/4.0 (compatible; MSIE 6.0; MSNIA; Windows...
INSTANCE_ID = 1
LOCAL_ADDR = 127.0.0.1
NUMBER_OF_PROCESSORS = 1
OS = Windows_NT
OS2LIBPATH = C:\WINNT\system32\os2\dll;
PATH = D:\Perl\bin\;C:\WINNT\system32;C:\WINNT;...
PATHEXT = .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PATH_INFO = /cgi-bin/CgiEnv.pl
PATH_TRANSLATED = c:\inetpub\wwwroot\cgi-bin\CgiEnv.pl
PROCESSOR_ARCHITECTURE = x86
PROCESSOR_IDENTIFIER = x86 Family 6 Model 8 Stepping 1, GenuineIntel
PROCESSOR_LEVEL = 6
PROCESSOR_REVISION = 0801
PROGRAMFILES = C:\Program Files
REMOTE_ADDR = 127.0.0.1
REMOTE_HOST = 127.0.0.1
REQUEST_METHOD = GET
SCRIPT_NAME = /cgi-bin/CgiEnv.pl
SERVER_NAME = localhost
SERVER_PORT = 80
SERVER_PORT_SECURE = 0
SERVER_PROTOCOL = HTTP/1.0
SERVER_SOFTWARE = Microsoft-IIS/5.0
SYSTEMDRIVE = C:
SYSTEMROOT = C:\WINNT
TEMP = C:\WINNT\TEMP
TMP = C:\WINNT\TEMP
USERPROFILE = C:\Documents and Settings\Default User.WINNT
WINDIR = C:\WINNT

As you can see from the output, IIS has added some and removed some environment variables in the CGI environment.

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part B - Common Gateway Interface (CGI)