SDSU CS 596: Client-Server Programming
Spring Semester, 1997
Doc 18, CGI

To Lecture Notes Index
San Diego State University -- This page last updated Apr 1, 1997
----------

Contents of Doc 18, CGI


References
Common Gateway Interface (CGI) slide # 1
...How does the Web server find and run the CGI program slide # 2
...How does the CGI program send Responses to the server slide # 2
......The CGI Return Headers (CGI/1.1) slide # 3
......Content-type slide # 4
.........Example 1 Simple Text slide # 4
.........Example 2 HTML slide # 6
.........Example 3 Some Real HTML slide # 8
......Redirection slide # 9
.........Example 1 URL slide # 9
.........Example 2 Path slide # 11
......Status slide # 13
.........Example 1 slide # 13
...Information from the Web server to the CGI Program slide # 15
......Environment Variables slide # 15
......GET method slide # 18
......POST and PUT slide # 21


Doc 18, CGI Slide # 1

References


The Common Gateway Interface http://hoohoo.ncsa.uiuc.edu/cgi/overview.html
Doc 18, CGI Slide # 2
CGI, WWW, and Client Server

Common Gateway Interface (CGI)


The basic idea



The basic questions


Doc 18, CGI Slide # 3

How does the Web server find and

run the CGI program?


Web server is configured to treat files in particular directories as programs

Often cgi-bin indicates a such a CGI directory

all files in a CGI directory and subdirectories are treated as programs


A url that references a program in a CGI directory will cause the Web server to run that program as a CGI program



How does the CGI program send Responses to the server?

CGI program writes all responses to the server on the standard output

CGI program output requires headers



Doc 18, CGI Slide # 4

The CGI Return Headers (CGI/1.1)


The output of scripts begins with a small header. This header consists of text lines, in the same format as an HTTP header, terminated by a blank line (a line with only a linefeed or CR/LF).

Any headers which are not server directives are sent directly back to the client. Currently, this specification defines three server directives:

Content-type

This is the MIME type of the document you are returning.
Location

This is used to specify to the server that you are returning a reference to a document rather than an actual document.

If the argument to this is a URL, the server will issue a redirect to the client.

If the argument to this is a virtual path, the server will retrieve the document specified as if the client had requested that document originally. ? directives will work in here, but # directives must be redirected back to the client.

Status

This is used to give the server an HTTP/1.0 status line to send to the client. The format is nnn xxxxx, where nnn is the 3-digit status code, and xxxxx is the reason string, such as "Forbidden".
Doc 18, CGI Slide # 5

Content-type Example 1 Simple Text

File: hiMomTextPlain

Location: /net/www/www-eli/cgi-bin/cgiExamples

Top level: CGI directory: /net/www/www-eli/cgi-bin

File Contents:

#!/bin/sh
echo Content-type: text/plain
echo
echo Hi Mom,
echo
echo How is Dad?
echo I am fine.
URL:
http://www.eli.sdsu.edu/cgi-bin/cgiExamples/hiMomTextPlain

Result in Netscape when open above URL:

Hi Mom,
How is Dad?
I am fine.


Doc 18, CGI Slide # 6
Content-type Example 1 Simple Text


Telnet Session:

/net/www/www-eli-> telnet www.eli.sdsu.edu 80
Trying 130.191.226.80...
Connected to www.eli.sdsu.edu.
Escape character is '^]'.
GET /cgi-bin/cgiExamples/hiMomTextPlain HTTP/1.0
HTTP/1.0 200 OK
Server: Netscape-Commerce/1.12
Date: Saturday, 29-Mar-97 05:04:17 GMT
Content-type: text/plain
Hi Mom,
How is Dad?
I am fine.
Connection closed by foreign host.

Doc 18, CGI Slide # 7

Content-type Example 2 HTML


File: hiMomTextHtml

Location: /net/www/www-eli/cgi-bin/cgiExamples

Top level: CGI directory: /net/www/www-eli/cgi-bin

File Contents:

#!/bin/sh
echo Content-type: text/html
echo
echo Hi Mom,
echo
echo How is Dad?
echo I am fine.
URL:
http://www.eli.sdsu.edu/cgi-bin/cgiExamples/hiMomTextHtml

Result in Netscape when open above URL:

Hi Mom, How is Dad? I am fine.


Doc 18, CGI Slide # 8
Content-type Example 2 HTML


Telnet Session:

/net/www/www-eli-> telnet www.eli.sdsu.edu 80
Trying 130.191.226.80...
Connected to www.eli.sdsu.edu.
Escape character is '^]'.
GET /cgi-bin/cgiExamples/hiMomTextHtml HTTP/1.0
HTTP/1.0 200 OK
Server: Netscape-Commerce/1.12
Date: Saturday, 29-Mar-97 05:18:01 GMT
Content-type: text/html
Hi Mom,
How is Dad?
I am fine.
Connection closed by foreign host.
What's the difference?

Doc 18, CGI Slide # 9

Content-type Example 3 Some Real HTML


File: hiMomFancy

Location: /net/www/www-eli/cgi-bin/cgiExamples

Top level: CGI directory: /net/www/www-eli/cgi-bin

File Contents:

#!/bin/sh
echo Content-type: text/html
echo
echo "<B>Hi Mom</B>",
echo
echo "<BIG>How is Dad?</BIG>"
echo "<CENTER>I am fine</CENTER>".
URL:
http://www.eli.sdsu.edu/cgi-bin/cgiExamples/hiMomFancy

Result in Netscape when open above URL:

Hi Mom, How is Dad?
I am fine

Doc 18, CGI Slide # 10

Redirection Example 1 URL


File: redirectURL

Location: /net/www/www-eli/cgi-bin/cgiExamples

Top level: CGI directory: /net/www/www-eli/cgi-bin

File Contents:

#!/bin/sh
echo Location: http://www.eli.sdsu.edu/index.html
echo
# All the stuff after this is does no good
# Client is working on the redirection
echo "<B>Hi Mom</B>",
echo
echo "<BIG>How is Dad?</BIG>"
echo "<CENTER>I am fine</CENTER>".
URL:
http://www.eli.sdsu.edu/cgi-bin/cgiExamples/redirectURL

Result in Netscape when open above URL:

You see my home page

Doc 18, CGI Slide # 11
Redirection Example 1 URL


Telnet Session:

/net/www/www-eli-> telnet www.eli.sdsu.edu 80
Trying 130.191.226.80...
Connected to www.eli.sdsu.edu.
Escape character is '^]'.
GET /cgi-bin/cgiExamples/redirectURL HTTP/1.0
HTTP/1.0 302 Found
Server: Netscape-Commerce/1.12
Date: Saturday, 29-Mar-97 06:25:26 GMT
Location: http://www.eli.sdsu.edu/index.html
<B>Hi Mom</B>,
<BIG>How is Dad?</BIG>
<CENTER>I am fine</CENTER>.
Connection closed by foreign host.


Doc 18, CGI Slide # 12

Redirection Example 2 Path


File: redirectPath

Location: /net/www/www-eli/cgi-bin/cgiExamples

Top level: CGI directory: /net/www/www-eli/cgi-bin

File Contents:

#!/bin/sh
echo Location: /cgi-bin/cgiExamples/hiMomFancy
echo
URL:
http://www.eli.sdsu.edu/cgi-bin/cgiExamples/redirectPath

Result in Netscape when open above URL:


Hi Mom, How is Dad?
I am fine

Doc 18, CGI Slide # 13
Redirection Example 2 Path


Telnet Session:

/net/www/www-eli-> telnet www.eli.sdsu.edu 80
Trying 130.191.226.80...
Connected to www.eli.sdsu.edu.
Escape character is '^]'.
GET /cgi-bin/cgiExamples/redirectPath HTTP/1.0
HTTP/1.0 200 OK
Server: Netscape-Commerce/1.12
Date: Saturday, 29-Mar-97 06:38:56 GMT
Content-type: text/html
<B>Hi Mom</B>,
<BIG>How is Dad?</BIG>
<CENTER>I am fine</CENTER>.
Connection closed by foreign host.

Doc 18, CGI Slide # 14

Status Example 1


File: status

Location: /net/www/www-eli/cgi-bin/cgiExamples

Top level: CGI directory: /net/www/www-eli/cgi-bin

File Contents:

#!/bin/sh
echo Content-type: text/plain
echo Status: 298 Okey Dokey with Me
echo
echo Hi Mom,
echo
echo How is Dad?
echo I am fine.
URL:
http://www.eli.sdsu.edu/cgi-bin/cgiExamples/status

Result in Netscape when open above URL:

Hi Mom,
How is Dad?
I am fine.

Doc 18, CGI Slide # 15
Status Example 1

Telnet Session:

/net/www/www-eli -> telnet www.eli.sdsu.edu 80
Trying 130.191.226.80...
Connected to www.eli.sdsu.edu.
Escape character is '^]'.
GET /cgi-bin/cgiExamples/status HTTP/1.0
HTTP/1.0 298 Okey Dokey with Me
Server: Netscape-Commerce/1.12
Date: Saturday, 29-Mar-97 19:49:50 GMT
Content-type: text/plain
Hi Mom,
How is Dad?
I am fine.
Connection closed by foreign host.

Doc 18, CGI Slide # 16

Information from the Web server to the CGI Program


Web server sends data to the CGI Program via:

environment variables
command line
standard input

Environment Variables


Some environment variables that are sent under a range of conditiions, not all are sent for each request

AUTH_TYPE
CONTENT_LENGTH
CONTENT_TYPE
DOCUMENT_ROOT
GATEWAY_INTERFACE
HTTP_ACCEPT
HTTP_USER_AGENT
PATH_INFO
PATH_TRANSLATED
QUERY_STRING
REMOTE_ADDR
REMOTE_HOST
REMOTE_IDENT
REMOTE_USER
REQUEST_METHOD
SCRIPT_NAME
SERVER_NAME
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE

See http://hoohoo.ncsa.uiuc.edu/cgi/env.html for the official word on these.


Doc 18, CGI Slide # 17
Example of Env. Varaiables sent to CGI Program

File: variables

Location: /net/www/www-eli/cgi-bin/cgiExamples

URL:
http://www.eli.sdsu.edu/cgi-bin/cgiExamples/variables

File Contents:

#!/bin/sh
# Code from http://hoohoo.ncsa.uiuc.edu/cgi/test-cgi.txt
echo Content-type: text/plain
echo

echo CGI/1.0 test script report:
echo

echo argc is $#. argv is "$*".
echo

echo CONTENT_LENGTH = $CONTENT_LE
echo CONTENT_TYPE = $CONTENT_TYPE
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE
echo HTTP_ACCEPT = "$HTTP_ACCEPT"
echo PATH_INFO = $PATH_INFO
echo PATH_TRANSLATED = $PATH_TRANSLATED
echo QUERY_STRING = $QUERY_STRING
echo REMOTE_ADDR = $REMOTE_ADDR
echo REMOTE_HOST = $REMOTE_HOST
echo REMOTE_USER = $REMOTE_USER
echo REQUEST_METHOD = $REQUEST_METHOD
echo SCRIPT_NAME = $SCRIPT_NAME
echo SERVER_NAME = $SERVER_NAME
echo SERVER_PORT = $SERVER_PORT
echo SERVER_PROTOCOL = $SERVER_PROTOCOL
echo SERVER_SOFTWARE = $SERVER_SOFTWARE
Doc 18, CGI Slide # 18
Result in Netscape when open above URL:

CGI/1.0 test script report:
argc is 0. argv is .
CONTENT_LENGTH =
CONTENT_TYPE =
GATEWAY_INTERFACE = CGI/1.1
HTTP_ACCEPT = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
PATH_INFO =
PATH_TRANSLATED =
QUERY_STRING =
REMOTE_ADDR = 130.191.4.114
REMOTE_HOST = ebbsrv1p13.sdsu.edu
REMOTE_USER =
REQUEST_METHOD = GET
SCRIPT_NAME = /cgi-bin/cgiExamples/variables
SERVER_NAME = www.eli.sdsu.edu
SERVER_PORT = 80
SERVER_PROTOCOL = HTTP/1.0
SERVER_SOFTWARE = Netscape-Commerce/1.12

Doc 18, CGI Slide # 19
Other Data Sent to The CGI Program

The following http request methods can be used to send other data to the CGI program:

GET
POST
PUT
ISINDEX query

GET method


The GET method sends data via the QUERY_STRING environment variable

The GET method is sent in one of two way:

Via a html form
By placing name-value pairs at the end of a URL

The data is encoded in x-www-form-urlencoded format when send via html form

A space is encoded as a '+"

All characters other than a-z, A-Z, 0-9, '_' and space are encoded as a % followed by two hex digits that make up the character.


Doc 18, CGI Slide # 20
GET Method Example 1. URL

URL:
http://www.eli.sdsu.edu/cgi bin/cgiExamples/variables?name=Roger&class=596

Result in Netscape when open above URL:

(I removed some variables to save space)

CGI/1.0 test script report:
CONTENT_LENGTH =
CONTENT_TYPE =
QUERY_STRING = name=Roger&class=cs596
REQUEST_METHOD = GET
SCRIPT_NAME = /cgi-bin/cgiExamples/variables
SERVER_PORT = 80
SERVER_PROTOCOL = HTTP/1.0
SERVER_SOFTWARE = Netscape-Commerce/1.12
GET Method Example 2. URL bad form

URL:
http://www.eli.sdsu.edu/cgi bin/cgiExamples/variables?name<Roger;class@cs596

Result in Netscape when open above URL:

CGI/1.0 test script report:
QUERY_STRING = name<Roger;class@cs596

Doc 18, CGI Slide # 21
GET Method Example 3. A Form

The Form, HTML
<P>
<FORM ACTION=
"http://www.eli.sdsu.edu/cgi-bin/cgiExamples/variables"
METHOD="GET">
<INPUT TYPE="checkbox" NAME="top" VALUE="on">
<INPUT TYPE="checkbox" NAME="left" VALUE="on">
<INPUT TYPE="submit" VALUE="Submit"> <P>
</FORM>

The Form, Rendered








Result in Netscape upon submit, both boxes checked:

CGI/1.0 test script report:

argc is 0. argv is .

CONTENT_LENGTH =
GATEWAY_INTERFACE = CGI/1.1
REQUEST_METHOD = GET
QUERY_STRING = top=on&left=on

Doc 18, CGI Slide # 22

POST and PUT


When data is sent via POST or PUT,

the CONTENT_LENGTH environment variable contains the number of bytes of data sent to the CGI program via the standard input


POST Method Example 1. A Form

The Form, HTML
<P>
<FORM ACTION=
"http://www.eli.sdsu.edu/cgi-bin/cgiExamples/variables"
METHOD="POST">
<INPUT TYPE="checkbox" NAME="top" VALUE="on">
<INPUT TYPE="checkbox" NAME="left" VALUE="on">
<INPUT TYPE="submit" VALUE="Submit"> <P>
</FORM>

The Form, Rendered







Doc 18, CGI Slide # 23
Result in Netscape upon submit, both boxes checked:

CGI/1.0 test script report:
argc is 0. argv is .
CONTENT_LENGTH =
CONTENT_TYPE = application/x-www-form-urlencoded
GATEWAY_INTERFACE = CGI/1.1
HTTP_ACCEPT = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
PATH_INFO =
PATH_TRANSLATED =
QUERY_STRING =
REMOTE_ADDR = 130.191.4.123
REMOTE_HOST = ebbsrv1p22.sdsu.edu
REMOTE_USER =
REQUEST_METHOD = POST
SCRIPT_NAME = /cgi-bin/cgiExamples/variables
SERVER_NAME = www.eli.sdsu.edu
SERVER_PORT = 80
SERVER_PROTOCOL = HTTP/1.0
SERVER_SOFTWARE = Netscape-Commerce/1.12

----------