CGI: Common Gateway Interface

 

·         Connects Web servers to external applications: true interactivity

·         Allows a Web server to run an application

Ø       JS runs on the client machine

 

·         CGI is run on the server, which makes it especially useful for applications that need access to files and facilities available on the server (like databases)

·         CGI applications do not monitor events on the client. You cannot perform image swapping on the browser using OnMouseOver events

·         How a CGI request is processed: 

Browser ---> Web Server --->CGI Application

CGI Application ---> Web Server ---> Browser

 

1.       The Web browser makes a request to the Web server for the CGI application:

 

<FORM ACTION="cgi-bin/guest.cgi" METHOD=POST>

 

2.       The Web Server executes the corresponding CGI application and sends it the POSTed data from the Web page if any exists

3.       The CGI application executes returning data back to the Web Server

4.       The Web Server passes this data back to the Web Browser

 

Two things we do with CGI:

 

·         gather info from Web browser to Web server & make the info available to an external program

·         send info (output of program) to a Web browser that requests it

 

Examples:

               

·         gather info from a database and post the records on the Web:

Ø       dictionary, stock quotes

·         provide surveys, questionnaires

·         create images: charts and graphs on-the-fly

·         auctions

·         chat rooms, conferencing

·         guest book

·         on-line shopping

 

Advantages of CGI

 

·         Platform independence: CGI built into most Web server packages

·         Language independence: CGI programs can be written in almost any language

·         C/C++, Fortran, Perl, Tcl, Visual Basic, Apple Script

·         Scalability: simplicity of the interface...small to complex scripts

 

·         Don't necessarily need form to invoke a CGI request. Link on Web page to current weather in California

·         Don't have to return info in HTML form. Can return plain ASCII or image

 

 

·         One of CGI's most common uses: processing forms.

 

Say guest.cgi is located in ~jvm/cgi-bin/                      

 

<FORM ACTION="cgi-bin/guest.cgi" METHOD=POST>

<INPUT TYPE=TEXT NAME=lookfor SIZE=20 VALUE="John">

<INPUT TYPE=SUBMIT VALUE="Search Database">

</FORM>

 

This form takes a text value, which is default "John". When the user presses the Search Database button, the Web browser will call the Web server at http://icdweb.cc.purdue.edu/ and say "Hey, I need you to execute guest.cgi in directory ~jvm/cgi-bin" and also "I'm going to send you some information to send to that CGI script." In METHOD=POST that information is sent as a data file.

 

 

 

METHOD=GET: information is known as:

 

QUERY_STRING

 

Basically the QUERY_STRING is a string of characters appended to the end of the URL that it is calling. The previous might actually call:

 

http://icdweb.cc.purdue.edu/~jvm/cgi-bin/guest.cgi?lookfor=John

 

With correct interpretation the CGI script removes the postpended characters and locate information pertaining to lookfor=John.

 

 

What is the deal with this Gateway business?

 

·         Common Gateway (CGI scripts) act as gateways to other programs running on the Web server.

·         Database: Do a query on the database using the information passed in by lookfor and present that data back at a Web page to the Client.

 

Creating and Using CGI Applications

 

Perl: interpreted language, kind of.

 

Ø       Generally Perl programs stored as source code and recompiled every time the program is executed

Ø       Run Perl program, compiled by Perl interpreter

Ø       Compiled code is executed

·         Changing code is easy: no separate compilation step

·         OR can get Perl compiler and store as binary, then no recompilation step

 

 PERL: Practical Extraction Report Language

 

An “easy” language to learn.

 

·         Easy to do things: open file, read it, make changes based on what you’ve found in the file.

·         Easy to handle text: look at a sentence, break into words, sort in alphabetical order.

 

Which Perl book will you use?

 

What are Perl programs?

 

·         Just plain text files: vi, emacs, Notepad, WordPad, SimpleText, BBEdit

·         We’ll use Unix Web server for our CGI scripts

·         Perl programs execute line by line, starting from the top of the file EXCEPT when loop

 

#! /usr/local/bin/perl               # path to interpreter on mentor

#! /usr/local/perl/perl             # path to interpreter on cs machines

Can use % which perl to find interpreter path

 

 

 

Resource  for cs people:

http://www.cs.purdue.edu/help/www/using-cgi.html

 

Possible ways to run Perl in Unix

 

% perl -e 'print "Hello, world\n" '              

 

% perl firstPerl

% perl firstPerl.pl

 

#! /usr/local/perl/perl

print "Here we go with Perl";

 

Printing to a Browser

 

#! /usr/local/perl/perl

 

print "Content-type: text/html\n\n";

print "Here we go with Perl";

 

MIME Types

 

Specifying info to the users.

 

Two parts: header information and main message

 

First. The essential header:

·         Contains format information

·         If not included, browser assumes plain text file

 

MIME type tells a Web client what kind of info is in the response from server.

·         HTML

·         GIF image

·         Compressed program file

 

Header starts with: Content-type: text/html

 

LOTS of MIME types exist:

 

 

Header info separated from the body of the message by a blank line: “\n\n”

 

A full document with a corresponding MIME type

 

print “Content-type: text/html\n\n”;

 

print  “<HTML><HEAD>

<TITLE>output of HTML from CGI script</TITLE>

</HEAD>

<BODY>

<H1>Sample output</H1>

What do you think of <STRONG>this?</STRONG>

</BODY></HTML>”;

 

Content-type: text/html

Location: gopher://httprules.foobar.org/0

 

        <HTML><HEAD>

        <TITLE>Sorry...it moved</TITLE>

        </HEAD><BODY>

        <H1>Go to gopher instead</H1>

        Now available at

        <A HREF="gopher://httprules.foobar.org/0"> a new   location </A>  on our gopher server.

        </BODY></HTML>