CM0133 Internet Computing CGI - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

CM0133 Internet Computing CGI

Description:

CGI stands for Common Gateway Interface ... option azure option puce option cornflower option olive draub option amber /select ... – PowerPoint PPT presentation

Number of Views:180
Avg rating:3.0/5.0
Slides: 39
Provided by: Office20093
Category:

less

Transcript and Presenter's Notes

Title: CM0133 Internet Computing CGI


1
CM0133 Internet ComputingCGI
2
CGI Scripting
What is a CGI Script? A CGI script is any program
that runs on a web server. Why CGI Scripts CGI
stands for Common Gateway Interface CGI defines a
standard way in which information may be
passed to and from the browser and server. Any
program or script that can process information
according to the CGI specification (part of HTTP
protocol) can, in theory, be used to code a CGI
script.
3
The role of CGI
The role of this CGI script is to Accept the
data which the user inputs and Do something
with it. Usually, send a reply back to user.
The common gateway interface
4
Writing and Running CGI scripts
CGI scripts can exist in many forms depending
upon what the server supports. CGI scripts can be
compiled programs or batch files or any
executable entity. For simplicity we will use the
term script for all CGI entities. Typically CGI
scripts are written in Perl scripts colour
green The method we adopt. Most common too
C/C programs Unix Scripts We will concentrate
on Perl in this course. CGI scripts therefore
have to be written (and maybe compiled) and
checked for errors before they are run on the
server.
5
Calling a CGI script
CGI can be called and run in a variety of ways on
the server. The 2 most common ways of running a
CGI script are From an HTML Formthe ACTION
attribute of the form specifies the CGI script to
be run. Direct URL referenceACGI script can be
run directly by giving the URL explicitly in
HTML. Arguments (values) may be required by the
script this will have to passed in. We will see
how to do this shortly. One other way CGI scripts
are called is in Server-side include HTML
commands. This is something we will leave until
later.
6
Creating CGI Scripts
We will be creating CGI scripts in Perl. Perl has
become the default language for creating CGI
scripts as it has many useful features and a rich
set of libraries. On Mac OS X/UNIX perl scripts
are executed as scripts Perl Scripts are
interpreted no need to compile A special
program, the Perl Interpreter, supplied on the
system. On PC (also LINUX/Solaris) ActivePerl
is used (Industry Standard) Perl is a freely
available for most platformssee www.perl.org or
www.perl.com
7
Perl CGI Development Cycle
The basic cycle of perl script development
recommended for this course is 1. Write and
create Perl scripts on Local Machine (Mac OS
X/PC/LINUX). 2. Test, run and debug Perl script
Local Machine (Mac OS X/PC/LINUX) Test for
syntax and basic output. 3. For permanent storage
or permanent Web distribution, WebDav/FTP perl
script and HTML to Schools UNIX/LINUX Web
Server Local (Project) and Global (Public)
scripts. HTML files must be placed in special
directories. Perl scripts must be place in
special (sub) directories (cgi-bin subdirectory
from HTML directorymore soon).
8
Setting up scripts to run on a server
The process of installing CGI scripts is similar
to that of HTML pages, except different
(sub)directories and URLs are used. project
html/cgi-bin Files placed in this directory
will be accessible only within the school. Use
URL http//project.cs.cf.ac.uk/A.B.Surname/cgi-bi
n where A.B.Surname is your long email name to
reference files from HTML forms or directly.
Associated HTML files (i.e. ones whose FORM
ACTION calls the CGI script) must still be placed
in the project html directory (one (sub)directory
level above.
9
World wide CGI scripts
public html/cgi-bin Files placed in this
(sub)directory will be viewable on the whole
Internet. Use URL http//users.cs.cf.ac.uk/A.B.
Surname/cgi-bin/ where A.B.Surname is your long
email name to reference scripts from HTML or
direct URL.
10
Setting up cgi-bin
cgi-bin (sub)directories should already be
created for you. You will have register your
project and public html/cgi-bin directory on the
SchoolsWeb Server. CGI scripts placed here
will need their access permission changed. See
more information of following slide Further
information on user and project Web CGI pages
at User http//www.cs.cf.ac.uk/systems/html/45
1 (Web) or http//www.cs.cf.ac.uk/systems/pdfs/451
.pdf (PDF) Project http//www.cs.cf.ac.uk/syste
ms/html/452 (Web) or http//www.cs.cf.ac.uk/system
s/pdfs/452.pdf (PDF)
11
Configuring and Running
Simply place (FTP) the CGI script in the public
or project html/cgi-bin sub directories. Every
CGI script will need to have certain access modes
changed. Use Siteadmin CGI scripts have a
maximum CPU runtime of 30 seconds
12
Beginning CGI Programming
In this section we will lay the foundation for
CGI script development. We will introduce
general CGI programming concepts relating to CGI
output but then focus on Perl programming. Specif
ically we will develop a very simple Perl program
and see how to run it on a Macintosh and UNIX
platform.
13
CGI Script Output
We have already mentioned that CGI scripts must
adhere to standard input and output mechanism
The Interface between browser and server Part
of HTTP Protocol For the moment we will not
worry about input to a CGI script.
14
Declaring Content Type
To declare the Content-Type your CGI script must
output Content-Type content-type
specification Typically the Content-Type will be
declared to produce HTML. So the first line of
our CGI script (for most of our examples)
will look this Content-Type text/html
15
CGI Output
Example To produce aWeb page that the server
sends to a browser with a simple line of text
HelloWorld! . A CGI script must
output Content-Type text/html lthtmlgt ltheadgt lttit
legtHello, world!lt/titlegt lt/headgt ltbodygt lth1gtHello,
world!lt/h1gt lt/bodygt lt/htmlgt Now let us see how
we write and display in a Browser this CGI script
in Perl
16
Format of a Perl program
Every Perl program MUST obey the following
format A first line consisting
of !/usr/bin/perl The rest of the program
consisting of legal Perl syntax
and commands Strictly speaking the first line is
only required for running Perl programs on UNIX
machines. Since that is the intended
destination of most of our Perl/CGI scripts. It
is a good idea to make this the first line of
every perl program.
17
First Line Output
The first line of our CGI script must be
Content-Type text/html and The print
statement must have 2 \n characters One to
terminate the current line, and The second to
produce the require blank line between CGI header
and data. So our completer Perl line looks like
this print "Content-Type text/html\n\n"
18
First Script
Our complete first program (with nice comments)
is a follows !/usr/bin/perl hello.pl - My
first Perl CGI program print "Content-Type
text/html\n\n" Note there is a newline
between this header and Data Simple HTML code
follows print "lthtmlgt ltheadgt\n" print
"lttitlegtHello, world!lt/titlegt" print
"lt/headgt\n" print "ltbodygt\n" print "lth1gtHello,
world!lt/h1gt\n Print lt/bodygt lt/htmlgt\n
19
Writing Creating Perl Scripts
Perl Scripts are basically text files with
special perl syntax embedded in the
text. Therefore any text editor can be used to
create and edit your Perl files.
20
Testing Perl Scripts
  • Locally you can invoke the Perl interpreter
  • In a Terminal window on OSX
  • Use perl myfile.pl

21
Accepting Input from the Browser
Environment Variables It gets various
information about the browser, the server and the
CGI script itself through specially named
variables automatically created and setup by the
server. Standard Input Data can be passed as
standard input to CGI script. Usually this is
through the POST method of an HTML
Form. (Standalone Perl scripts get standard input
from the keyboard or a file.)
22
Cont
Arguments of the CGI Script If you call a CGI
script directly or use the GET method of HTML
Form posting information is passed as arguments
of the CGI script URL. Arguments are followed
by a ? after the CGI script URL and multiple
arguments are separated by . For
example http//host/cgi-bin?arg1arg2
23
CGI Conventions
Different fields (e.g. name value pairs are
separated by an ampersand (). Name/value pair
assignments are denoted by an equals sign ().
The format is namevalue. Blank spaces must be
denoted by a plus sign (). Some special
characters will be replaced by a percent sign ()
followed by a 2 digit hexadecimal (ASCII Value)
code. For example if you need to input an
actual ,or character as input. The GET Form
posting method does these things
automatically. Note You may need to construct
these things yourself if call the CGI script
directly from a URL.
24
A Minimal Form example
If we set the Form method attribute to GET
via ltform method "get" action
"http//.../cgi-bin/minimal.pl"gt ltinput
type"submit"gtData ltinput name"myfield"gt lt/formgt
25
Receiving Information
There are basic ways to process or parse input in
a Perl Do it yourself write several lines of
Perl code to process the input. Use
pre-written Perl librariessomebody has already
done the arduous task of writing Perl code to
parse input.
26
A Simple Perl input Library
There are many Perl libraries available to read
and parse CGI input. These are freely
available on the WWW via the Comprehensive Perl
Archive Network (CPAN) The library that became
one of first the standard CGI libraries is the
cgi-lib.pl library. Further Information on this
library is available from http//cgi-lib.berkeley.
edu/. You should find copies of the cgi-lib.pl
at http//cgi-lib.berkeley.edu/
27
Using cgi-lib Example
We will develop a minimal.pl CGI routine that
accepts input form our minimal form and sends
back HTML that echoes the input data. We will use
the cgi-lib.pl to Parse the input from the
form. Format the HTML output. We will need to
learn some more basic Perl How to include and
call Perl libraries. How to call Perl
subroutines
28
Including Libraries
The first thing our Perl script will need to do
is to include the Perl library file
cgi-lib.pl. The Perl command require will load
in any external Perl file. It is easier and
sometimes essential that all library files exist
in the same folder or directory as the main Perl
script calling the library. Thus to include our
cgi-lib.pl file we need the Perl
command require "cgi-lib.pl"
29
Parsing Input
Having included the library we can call on its
many useful subroutines. The ReadParse()
subroutine reads either GET or POST input and
conveniently stores the name/value pairs in a
Perl array Thus a Perl call of the
form ReadParse(input) will store the input
in an array input. is used to indicate a Perl
subroutine call.
30
Name Value Pairs
Next we will need to extract out the relevant
value of a given name. In our current example
there is only one input field and we are
therefore only interested in the value associated
with the myfield name. To get this value you
simply do inputmyfield Thus to print out
the value typed we could do something like print
"You typed " . inputmyfield . "\n"
31
Complete Script
!/usr/bin/perl minimal.cgi This is the
minimalist form script to demonstrate the use
of the cgi-lib.pl library require
"cgi-lib.pl" Read in all the variables set by
the form ReadParse(input) print "Content-Type
text/html\n\n" print "lthtmlgt ltheadgt\n" print
"lttitlegtMinimal Inputlt/titlegt\n" print
"lt/headgt\n" print "ltbodygt\n" print "You typed
" . inputmyfield . "\n" print "lt/bodygt
lt/htmlgt\n"
32
PrintHeader
The PrintHeader subroutine returns the
string Content-Type text/html\n\n Thus we can
use print in conjunction to produce our CGI
header output via print PrintHeader
33
HtmlTop
The HtmlTop subroutine accepts a single string
argument, MY TITLE say, and return an HTML Head
and Body (opening only) with the argument as the
HTML page TITLE and H1 Header. I.e. lthtmlgt ltheadgt
lttitlegtMY TITLElt/titlegt lt/headgt ltbodygt lth1gtMY
TITLElt/h1gt
34
HtmlBot
The HtmlBot subroutine is the compliment of
HtmlTop and returns lt/bodygt lt/htmlgt
35
A better script
!/usr/bin/perl minimal.cgi This is the
minimalist form script to demonstrate the use
of the cgi-lib.pl library require
"cgi-lib.pl" Read in all the variables set by
the form ReadParse(input) Print the header
html top print PrintHeader print HtmlTop
("Minimal Input") print "You typed " .
inputmyfield . "\n" print HtmlBot
36
Text Areas
  • HTML wont preserve newlines
  • Use
  • (text inputtext) s/\n/\nltBRgt/g
  • This will replace all \n with ltbrgt

37
A more Complete Example
ltform method "post" action "simple-form.pl"gt W
hat is thy name ltinput name"name"gtltPgt What is
thy quest ltinput name"quest"gtltPgt What is thy
favorite color ltselect name"color"gt ltoption
selectedgtchartreuse ltoptiongtazure ltoptiongtpuce ltop
tiongtcornflower ltoptiongtolive draub ltoptiongtamber
lt/selectgt ltPgt What is the weight of a swallow
ltinput type"radio" name"swallow" value"african"
checkedgt African Swallow or ltinput type"radio"
name"swallow" value"continental"gt
Continental Swallow ltPgt What do you have to say
for yourself lttextarea name"text" rows5
cols60gtlt/textareagt ltPgt Press ltinput
type"submit" value"here"gt to submit your
query lt/formgt
38
The Perl Script
!/usr/bin/perl require "cgi-lib.pl" Read in
all the variables from form ReadParse(input)
Print the header print PrintHeader print
HtmlTop ("cgi-lib.pl demo form output") Do
some processing, and print some output add
ltBRgts after carriage returns to multline
input, since HTML does not preserve line
breaks (text inputtext)
s/\n/\nltBRgt/g print ltlt "ENDOFTEXT" You,
inputname, whose favorite color is
inputcolor are on a quest which is
inputquest, and are looking for the air
speed/velocity of an inputswallow swallow.
And this is what you have to say for yourselfltPgt
textltPgt ENDOFTEXT If you want, just print out
a list of all of the variables. print "ltHRgtAnd
here is a list of the variables you
entered...ltPgt" print PrintVariables(input)
Close the document cleanly. print HtmlBot
Write a Comment
User Comments (0)
About PowerShow.com