Title: Common Gateway Interface (CGI)
1Common Gateway Interface (CGI)
2Common Gateway Interface (CGI)
- HTTP typically requests HTML pages
- Link (URL) can identify a non-HTML page
- WWW server invokes the resource named in the URL
(a program, batch file, etc.) and lets it do its
thing - Results of the CGI program are returned to the
server in the form of HTML - Server then passes the HTML to the client
3HyperText Transfer Protocol (HTTP)
- Provides stateless connection
- Client establishes connection
- Client issues a request
- Sever processes the request and returns a
response - Server closes the connection
- Webs Remote Procedure Call (RPC) on top of TCP/IP
4HTTP Request
- Request Line -- method (GET), relative path to
resource (/temp/index.html), protocol version
(HTTP/1.0) - Request header fields -- additional parameters in
the format name, colon (), value - Entity body -- sometimes used to pass bulk
information to the server
5HTTP Request Example
GET /path/file.html HTTP/1.0 (header
line) Accept text/html
(request header fields) Accept audio/x
User-agent MacWeb
6HTTP Response
- Response header line -- HTTP version, status of
the response, explanation of status - Response header fields -- server attributes and
document returned metadata - Entity body -- typically an HTML document
7HTTP Response Example
HTTP/1.0 200 OK (response
header) Server NCSA/1.3 (response
header fields) Mime_Version 1.0
Content_type text/html
Content_length 2000
ltHTMLgt
(entity body) .
lt/HTMLgt
8Common Gateway Interface
- HTML FORMs allow user input
- Input is packaged up in a known format and send
as an HTTP request (POST or GET) and sent on
through a CGI protocol - The program or resource named is given the data
and left to do with it what it will - Results are returned in the form of an HTML
document that the server passes on to the client - Three-tier client/server model
9Using FORMs and CGI
- FORMs submit button is pushed
- Browser collects data and forms one long string
of name/value pairs separated by an ampersand ()
with plus signs () for spaces - Browser forms a standard HTTP request with the
long string as the entity body - Server receives request via a socket and starts a
CGI interaction
10Using FORMs and CGI continued
- Server sets up environment variables
- shared bulletin board with cgi program
- server_name, request_method, path_info,
script_name, content_type, content_length, etc. - Server starts the CGI program
- CGI program reads environment variables
- CGI program receives message body (long string)
via standard input and parses
11Using FORMs and CGI continued
- CGI program does its thing and formats results in
HTML - CGI program returns results via standard output
- Server returns the results to the Browser
- ALL DONE !!
- Since the connection is stateless hidden fields
are used in sequences of forms
12Example FORM
ltHTMLgt ltBODYgt ltH1gtSending POST Data to a CGI
Programlt/H1gt ltFORM ACTION
"http//140.209.124.107/cgi-bin/CgiPost.bat"
METHOD"POST"gt Enter some data ltINPUT
TYPE"TEXT" NAME"TextField1"gtltBRgt Enter some
more ltINPUT TYPE"TEXT" NAME"TextField2"gtltBRgt
ltINPUT TYPE"SUBMIT" VALUE"Send
It"gt lt/FORMgt lt/BODYgt lt/HTMLgt Link
13Count CGI Version (Client Applet)
import java.awt. import java.applet. import
java.io. import java.net. import
java.util. public class CountCGIClientApplet
extends Applet
14Count CGI Version (Client Applet)
try String sum "0" showStatus("Incrementing
") int count new Integer(textField1.getText()
).intValue() long startTime
System.currentTimeMillis() for (int i 0 i
lt count i ) sum increment(sum)
long stopTime System.currentTimeMillis()
list1.addItem("Avg Ping " ((stopTime -
startTime) / (float)count) " msecs")
showStatus("Sum " sum) catch(Exception
e) showStatus("System Exception" e)
15Count CGI Version (Client Applet)
public String increment(String currentSum)
String script "/cgi-bin/Count.bat" Socket
socket null String rdata "" String line
"" String lastLine ""
16Count CGI Version (Client Applet)
try socket new Socket(getCodeBase().getHost(
), 80) PrintWriter ostream new
PrintWriter(socket.getOutputStream())
BufferedReader istream new BufferedReader(
new InputStreamReader(socket.getInputStream())
) ostream.print("POST " script "
HTTP/1.0\r\n" "Content-type
text/plain\r\n" "Content-length "
("increment " currentSum).length()
"\r\n\r\n") ostream.print("increment "
currentSum "\r\n") ostream.flush()
17Count CGI Version (Client Applet)
try while ((line istream.readLine())
! null) list1.addItem(line)
lastLine line catch (
SocketException e ) istream.close()
ostream.close()
18Count CGI Version (Server)
import java.io. import java.util. class
CountCGIServer public static void main(String
args) int count String line try
// create streams PrintWriter ostream
new PrintWriter(System.out) BufferedReader
istream new BufferedReader(
new InputStreamReader(System.in))
line istream.readLine()
StringTokenizer tokens new StringTokenizer(line)
String myOperation tokens.nextToken()
19Count CGI Version (Server)
if(myOperation.equals("increment"))
String countString tokens.nextToken()
count Integer.parseInt(countString)
count ostream.println(
Integer.toString(count) )
ostream.flush()
istream.close() ostream.close()
catch (Exception e) System.out.println("Erro
r " e)
20Count CGI Html and Batch Files
lthtmlgt ltbodygt lth1gtCount CGI Client
Appletlt/h1gt lthrgt ltcentergt ltapplet
codeCountCGIClientApplet.class width500
height500gt lt/appletgt lt/centergt lt/bodygt lt/htmlgt
d\jdk1.1.7a\bin\java CountCGIServer
Do it!
21Java and CGI Helpful Link
Marty Halls CGI page
22CGI Assignment