Common Gateway Interface (CGI) - PowerPoint PPT Presentation

View by Category
About This Presentation
Title:

Common Gateway Interface (CGI)

Description:

WWW server invokes the resource named in the URL (a program, batch file, etc. ... of name/value pairs separated by an ampersand (&) with plus signs ( ) for spaces ... – PowerPoint PPT presentation

Number of Views:1969
Avg rating:3.0/5.0
Slides: 23
Provided by: joseph98
Learn more at: http://nacho.cps.unizar.es
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Common Gateway Interface (CGI)


1
Common Gateway Interface (CGI)
  • Joe Komar

2
Common 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

3
HyperText 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

4
HTTP 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

5
HTTP Request Example
GET /path/file.html HTTP/1.0 (header
line) Accept text/html
(request header fields) Accept audio/x
User-agent MacWeb

6
HTTP 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

7
HTTP 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

8
Common 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

9
Using 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

10
Using 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

11
Using 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

12
Example 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
13
Count CGI Version (Client Applet)
import java.awt. import java.applet. import
java.io. import java.net. import
java.util. public class CountCGIClientApplet
extends Applet
14
Count 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)
15
Count CGI Version (Client Applet)
public String increment(String currentSum)
String script "/cgi-bin/Count.bat" Socket
socket null String rdata "" String line
"" String lastLine ""
16
Count 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()
17
Count CGI Version (Client Applet)
try while ((line istream.readLine())
! null) list1.addItem(line)
lastLine line catch (
SocketException e ) istream.close()
ostream.close()
18
Count 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()
19
Count 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)
20
Count 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!
21
Java and CGI Helpful Link
Marty Halls CGI page
22
CGI Assignment
  • Stay tuned.
About PowerShow.com