3. CGI
Environment Variables
CGI protocol is defined with a collection of environment
variables that hold data set up by a browser for sending to the server. It
defines methods such as GET and POST for sending data, and how much data will
be sent to server. Other environment variables contain such information as
visitor's IP address, visitor's browser type, etc. All the environment
variables are stored in the special hash array or associate array called %ENV
which is set each time when a script is called.
A list of environment
variable defined by CGI standard is as shown below:
AUTH_TYPE
If the server supports user authentication, and the script
is protects, this is the protocol-specific authentication method used to
validate the user.
CONTENT_TYPE
It specifies the media type of the data for queries, which
have attached information, such as HTTP POST and PUT, this is the content type
of the data.
CONTENT_LENGTH
The length (number of bytes) of information passed to the script.
GATEWAY_INTERFACE
The name and version of the protocol being used by the
server to communicate with the script. Format: CGI/revision
PATH_INFO
It provides any extra path information, as given in the URL,
for accessing this script. The extra information is sent as PATH_INFO to be
decoded by the server before it is passed to the CGI script.
PATH_TRANSLATED
It gives the absolute file system path for access the
script. The server provides a translated version of PATH_INFO, which takes the
path and does any virtual-to-physical mapping to it.
QUERY_STRING
Any additional information passed to the script after the ?
mark in the URL which referenced this script is called the query information.
It should not be decoded in any fashion.
REMOTE_HOST
It contains a fully qualified domain name of the client
computer. If the host name cannot be determined, it should set REMOTE_ADDR to
hold the IP address of the host and leave this variable unset.
REMOTE_ADDR
The IP address of the remote client computer making the
request.
REMOTE_IDENT
The client machine's username. Usage of this variable should
be limited to logging only.
SCRIPT_NAME
A virtual path to the script being executed, used for
self-referencing URLs.
REMOTE_USER
The name used to authenticate the user for accessing the
script.
SERVER_SOFTWARE
The name and version of the information server software
answering the request (and running the gateway).
# Format:
name/version
SERVER_NAME
The server's hostname, DNS alias, or IP address as it would
appear in self-referencing URLs.
SERVER_PROTOCOL
The name and revision of the information protocol this
request came in with. Format: protocol/revision
SERVER_PORT
The port number to which the request was sent.
REQUEST_METHOD
The method with which the request was made. For HTTP, this
is "GET", "HEAD", "POST", etc.
HTTP_ACCEPT
Gives a comma-separated list of MIME types that the client
can accept.
HTTP_REFERER
Provides the address of the page where the request
originated.
HTTP_USER_AGENT
Specifies the name of the client program used to make the
request.
3.2 Errors Codes for Client and Server
When there is an error the corresponding error code will be
sent to client and the message will then displayed for the client. The error
codes are as shown below.
Client Errors Code
400 Bad
Request
401 Unauthorized
402 Payment
Required
403 Forbidden
404 Not
Found
405 Method
Not Allowed
406 Not
Acceptable
407 Proxy
Authentication Required
408 Request
Timeout
409 Conflict
410 Gone
411 Length
Required
412 Precondition
Failed
413 Request
Entity Too Large
414 Request-URI
Too Long
415 Unsupported
Media Type
416 Requested
range not valid
417 Failed
418 Failed
Server Error
500 Internal
Server Error
501 Not
Implemented
502 Bad
Gateway
503 Service
Unavailable
504 Gateway
Timeout
505 HTTP
Version Not Supported
506 Redirection
failed
HTTP Status Codes
HTTP Working Group:
http://www.w3.org/Protocols/HTTP
RFC 2616(HTTP
1.1):http://www.cis.ohio-state.edu/htbin/rfc/rfc2616.html
100 Continue
101 Switching Protocols
199 Misc.
Successful Codes
200 OK, the request was fulfilled
201 Created, following a POST command,
indicates the URI by which the newly created document should be known
202 Accepted, the request has been accepted for
processing, but the processing has not been completed
203 Non-Authoritative Information
204 No Content
205 Reset Content
206 Partial Content
299 Miscellaneous information
Redirection Codes
300 Multiple Choices, multiple documents available
301 Moved Permanently
302 Moved temporarily
303 See other document
304 Not modified since last retrieval
305 Use Proxy
306 Switch proxy
307 Document moved temporarily
399 Misc.
The first line of
most CGI programs must be an HTTP header, as listed below, to inform the Web
browser what type of output is expected. When the Perl print statement is used
to send the script file to the browser, typical web HTML tags and display are
then followed.
Text Content Type:
text/plain
HTML page Content Type:text/html
Gif graphics Content Type:image/gif
Redirection Location:http://www.newloca.com
Cookie Set-cookie:
Error Message Status: 402
Example 3-1: You will prepare a Perl CGI script and place it
in the cgi-bin directory of a desired server. In addition, you will need to
prepare a simple HTML web page for linking to the Perl script for viewing
portion information of your browser.
The Perl CGI script:
#!/usr/bin/perl
# browserinfo.pl
#
#
print "Content-type: text/html\n\n";
print "<HTML>\n<HEAD>\n";
print "<TITLE> Display Brwoser and some Server Info
</TITLE>\n";
print "</HEAD>\n";
print "<BODY>\n";
print "<BR><BR>\n";
print "<H2>Your browser information:
</H2>\n";
print "<H3>\n";
print "HTTP_USER_AGENT =
$ENV{'HTTP_USER_AGENT'}<BR>\n";
print "WINDIR
= $ENV{'WINDIR'}<BR>\n";
print "REMOTE_ADDR
= $ENV{'REMOTE_ADDR'}<BR>\n";
print "SCRIPT_NAME
= $ENV{'SCRIPT_NAME'}<BR>\n";
print "</H3>\n";
print "</BODY>\n";
print "</HTML>\n";
The HTML Web page for calling the Perl CGI script:
<!-- browserinfo.html -->
<HTML>
<HEAD>
<TITLE> Display Brwoser and some Server Info
</TITLE>
</HEAD>
<BODY>
<A
HREF="http://149.164.36.204/cgi-bin/browserinfo.pl">
<H2>Click to view browser and server information:
</H2> </A>
</BODY>
</HTML>
The Web page for client side user:
The output from running Perl CGI located on the server:
Example 3-2: This Perl CGI program, readend.pl, is placed in the
cgi-bin directory. Another HTML page called readenv.html is also placed
on the server.
The Perl CGI program:
#!/usr/local/bin/perl
# readenv.pl
#
# Execute this program to view all the environment variables.
#
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Environment
Variables</TITLE></HEAD><BODY>";
foreach $env_var (keys %ENV)
{
print
"<BR>";
# Read the first
field of %ENV variable
print "<FONT
COLOR=red>$env_var</FONT>";
print " ==
";
# Hash the second
field of %ENV variable
print "<FONT
COLOR=red>$ENV{$env_var}</FONT>";
}
print "</BODY></HTML>";
The HTML page can be either placed in the client machine or
at server:
The readenv.pl is executed by the server and the send
the result back to the client:
The compete list of environment variables are listed below:
PROMPT == $p$g
QUERY_STRING ==
CONTENT_TYPE ==
SERVER_PROTOCOL == HTTP/1.1
HTTP_REFERER ==
COMSPEC == C:\COMMAND.COM
NWLOGIN == G
PATH_TRANSLATED == C:\HTTPD\HTDOCS
HTTP_USER_AGENT == Mozilla/4.0
(compatible; MSIE 5.0; Windows 98; DigExt)
HTTP_ACCEPT == image/gif,
image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel,
application/msword, application/vnd.ms-powerpoint, */*
REMOTE_HOST ==
GATEWAY_INTERFACE == CGI/1.1
REMOTE_IDENT ==
PATH_INFO ==
SERVER_SOFTWARE == OmniHTTPd/2.07
TMP == C:\WINDOWS\TEMP
WINDIR == C:\WINDOWS
REMOTE_USER ==
REMOTE_ADDR == 149.164.36.104
BLASTER == A220 I7 D1 T2
SCRIPT_NAME == /cgi-bin/readenv.pl
TEMP == C:\WINDOWS\TEMP
WINBOOTDIR == C:\WINDOWS
CMDLINE == WIN
SERVER_NAME == eet499
SNDSCAPE == C:\WINDOWS
HTTP_COOKIE ==
CONTENT_LENGTH == 0
REQUEST_METHOD == GET
VXIPNPPATH == C:\VXIPNP
PATH == C:\httpd;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\PERL\BIN\;C:\WINDOWS;C:\WINDOWS\COMMAND;C:\PROGRA~1\NETWOR~1\MCAFEE~1;S:\WIN32\LOTUS\CCMAIL\;C:\PROGRAM;FILES\MTS;C:\VXIPNP\WIN95\BIN;C:\PROGRAM;FILES\MTS;C:\VXIPNP\WIN95\BIN;C:\HTTPD\PHP
AUTH_TYPE ==
SERVER_PORT == 80