06 server-side cgi - cs.uml.edu
TRANSCRIPT
I VPR 1© Copyright 2007 Haim Levkowitz
Server-side computing
• Why server-side?
• Approaches
I VPR 2© Copyright 2007 Haim Levkowitz
• Markup languages cannot• Specify
• Computations• Interactions with users
• Provide access to• Server-side resources
• Databases• Programs• Services
Why server-side?
I VPR 3© Copyright 2007 Haim Levkowitz
Approaches
• “programmer centered”
• “designer centered”
I VPR 4© Copyright 2007 Haim Levkowitz
“programmer centered”
• Document generation insidecomputation code
• CGI• Java servlets
I VPR 5© Copyright 2007 Haim Levkowitz
“designer centered”
• Computation code insidedocument template
• PHP• ASP
• JSP
I VPR 6© Copyright 2007 Haim Levkowitz
• Let browsers request server-sideexecution
• Open source• “LAMP”
• Linux, Apache, MySQL, Px• Px
• Perl, PHP, Python
CGI: the CommonGateway Intefrace
I VPR 7© Copyright 2007 Haim Levkowitz
ASP or ASP.NET: ActiveServer Pages
• Microsoft
I VPR 8© Copyright 2007 Haim Levkowitz
JSP: Java Server Pages
• Similar to ASP and ASP.NET
• Java-based
• Sun Microsystems
I VPR 9© Copyright 2007 Haim Levkowitz
CGI
• Interface between browser and server
• HTTP request ==>
• Program instead of document
• Server recognizes by file
• Location
• Special directories
• Extension
I VPR 10© Copyright 2007 Haim Levkowitz
CGI program
• Often stored in cgi-bin directory
• Produce
• complete HTTP response, or• just URL of existing doc
I VPR 11© Copyright 2007 Haim Levkowitz
CGI linkage
• Some CGI programs in machine code• Perl programs usually kept in source form
• ==> must be run perl on them
I VPR 12© Copyright 2007 Haim Levkowitz
• source file can be made “executable”
• add line at beginning
• specifies language processing
• run first
I VPR 13© Copyright 2007 Haim Levkowitz
For Perl programs
if perl in /usr/local/bin/perl
often in UNIX==> #!/usr/local/bin/perl -w
I VPR 14© Copyright 2007 Haim Levkowitz
for Perl CGI programs
sometimes use file extension .cgi
e.g., for the CGI Wrap system
I VPR 15© Copyright 2007 Haim Levkowitz
HTML document specifies CGI programwith hypertext reference attribute
href, of anchor tag, <a>
as in
<a href = “./cgi-bin/reply.cgi>" Click here to run the CGI program, reply.pl </a>
I VPR 16© Copyright 2007 Haim Levkowitz
Communications andcomputation using CGI
I VPR 17© Copyright 2007 Haim Levkowitz
Model ...
• Browser server application
CGI
Form
CGI program's
responseCGI program's
response
Submit completed form
Call CGI
I VPR 18© Copyright 2007 Haim Levkowitz
I VPR 19© Copyright 2007 Haim Levkowitz
<!-- reply.html - calls a trivial cgiprogram --><html><head><title> HTML to call the CGI-Perl programreply.cgi</title></head><body>This is our first CGI-Perl example<a href = "./cgi-bin/reply.cgi">Click here to run the CGI program,reply.cgi</a></body></html>
I VPR 20© Copyright 2007 Haim Levkowitz
CGI --> browser
• connection from CGI program back to requestingbrowser• Through STDOUT• Through server
• HTTP header needs only content type• followed by blank line• print "Content-type: text/html\n\n";
• SHOW reply.cgi
I VPR 21© Copyright 2007 Haim Levkowitz
Query string
• names and values of widgets• values always coded as strings• format of name/value pair
• name=value• If form has more than one widget
• ==> values separated with ampersands• milk=2&payment=visa
• special characters coded• percent sign + two-character hexadecimal number
• = ASCII code of character• Some browsers code spaces
• as + signs• rather than as %20
• get versus post HTTP methods
I VPR 22© Copyright 2007 Haim Levkowitz
GET vs. POST ...
• GET
• + Access CGI prog. w/ query without a form
• Pass parameters to program ...
• Can send extra path info ...
• – Query might get truncated• Post
• + Unlimited query length
• – No “canned” queries (hard-coded url)
I VPR 23© Copyright 2007 Haim Levkowitz
- A Perl module serves as a library
- Can be used as a function library or a class library
- Get info about by typing perldoc CGI
- The Perl use declaration is used to make a module available to a program
- To make only part of a module available, specify the part name after a colon
(For our purposes, only the standard part of the CGI module is needed)
use CGI ":standard";
- Common CGI.pm Functions
- “Shortcut” functions produce tags, using their parameters as attribute values
br;
returns
"<br/>"
The CGI.pm Module
I VPR 24© Copyright 2007 Haim Levkowitz
The CGI.pm Module
Perl module serves as libraryCan be used as
function library orclass library
Info: perldoc CGI
I VPR 25© Copyright 2007 Haim Levkowitz
Perl use declarationmake module available to program
to make only part of module availablespecify part name after colon
For our purposesonly need standard part of CGI module
use CGI ":standard";
I VPR 26© Copyright 2007 Haim Levkowitz
Common CGI.pmFunctions“Shortcut” functions produce tags
using their parameters as attribute values
br;
returns
"<br/>"
I VPR 27© Copyright 2007 Haim Levkowitz
e.g., h2("Very easy!");returns <h2> Very easy! </h2>
parameter to function h2used as content of <h2> tag
I VPR 28© Copyright 2007 Haim Levkowitz
function output toreturn doccall must be parameter to print
print h2("Very easy!");
I VPR 29© Copyright 2007 Haim Levkowitz
Tags can have bothcontent and attributes
Each attribute passed as name/value pair just as in hash literal
Attribute names passed with preceding dash
I VPR 30© Copyright 2007 Haim Levkowitz
textarea(-name => "Description", -rows => "2",
-cols => "35");
Produces:
<textarea name ="Description" rows=2 cols=35> </textarea>
I VPR 31© Copyright 2007 Haim Levkowitz
If both content and attributes passed to functionattributes specified in hash literal
as first parameter
a({-href => "fruit.html"}, "Press here for fruitdescriptions");
Output:<a href="fruit.html"> Press here for fruitdescriptions</a>
I VPR 32© Copyright 2007 Haim Levkowitz
Tags and their attributesdistributed over parameters of function
ol(li({-type => "square"}, ["milk", "bread","cheese"]));
Output:<ol> <li type="square”>milk</li> <li type="square”>bread</li> <li type="square”>cheese</li>
</ol>
I VPR 33© Copyright 2007 Haim Levkowitz
CGI.pm also includesnon-shortcut functionsproduce output for return to user
call to header() produces:
Content-type: text/html;charset=ISO-8859-1
-- blank line --
I VPR 34© Copyright 2007 Haim Levkowitz
The start_htmlfunctioncreate
head of return document<body> tag
parameter to start_htmlused as title of document
start_html("Bill’s Bags");
I VPR 35© Copyright 2007 Haim Levkowitz
DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0 //EN" "http://www.w3.org/TR/xhtml-basic/ xhtml-basic10.dtd"><html xmlns= "http://www.w3.org/1999/xhtmllang="en-US">
<head><title>Bill’s Bags</title></head><body>
I VPR 36© Copyright 2007 Haim Levkowitz
The param function
given a widget’s namereturns widget’s value
If, e.g., query string has name=Abraham in it,
param("name") will return "Abraham"
I VPR 37© Copyright 2007 Haim Levkowitz
The end_html function
generates </body></html>
SHOW popcorn.html display popcorn.cgi
I VPR 38© Copyright 2007 Haim Levkowitz
FIGURE 10.2 Display ofpopcorn.html
I VPR 39© Copyright 2007 Haim Levkowitz
FIGURE 10.3 Display ofpopcorn.html, form filledout
I VPR 40© Copyright 2007 Haim Levkowitz
FIGURE 10.4 Output ofpopcorn.cgi
I VPR 41© Copyright 2007 Haim Levkowitz
A Survey Example
- Use form to collect survey data from users
- accumulate survey results,stored between form submissionsin file on server
I VPR 42© Copyright 2007 Haim Levkowitz
concurrent use of file==> must be protected from corruption
block other accesses while being updated
Under UNIX, Perl functionflock
I VPR 43© Copyright 2007 Haim Levkowitz
parameter values for flockdefined in Perl Fcntl module(write lock is LOCK_EX)
To update fileopen filelock with flockmodifyrewind with seekclose file
--> SHOW conelec.html + display
I VPR 44© Copyright 2007 Haim Levkowitz
Two CGI programs used1. collect survey submissions and record new
data2. produce current totals
file formateight lineseach w/ 7 values first four for female responses last four for male responses
I VPR 45© Copyright 2007 Haim Levkowitz
FIGURE 10.5 Display ofconelec.html
I VPR 46© Copyright 2007 Haim Levkowitz
To collect and recordform data must:1. Get form values (with param)
2. Determine row of file to be modified
3. Open, lock, and read survey data file
4. Split affected data string into numbers + store them in array
…
I VPR 47© Copyright 2007 Haim Levkowitz
5. Modify affected array element& join array back into string
(use gender to determine which half of dataaffected)
6. Rewind data file (with seek)
7. Rewrite and close survey data file
--> SHOW conelec1.pl
I VPR 48© Copyright 2007 Haim Levkowitz
Tables easier to specifywith CGI.pm
- table created with table function
- border attribute specified as parameter
- table’s caption created with call to caption, as second parameter to table
I VPR 49© Copyright 2007 Haim Levkowitz
- Each row of table is created with call to Tr
- heading row created with call to th
- Data cells created with calls to td
- calls to Tr, th, and td require references as parameters
I VPR 50© Copyright 2007 Haim Levkowitz
- Suppose have three arrays of sales numbers- one for each of three salespersons- each array has one value for each day of work
week
- want to build table of this information- using CGI.pm
I VPR 51© Copyright 2007 Haim Levkowitz
table({-border => "border"},caption("Sales Figures"),Tr(
[th(["Salesperson", "Mon", "Tues", "Wed", "Thu", "Fri"]),
th("Mary").td(\@marysales),th("Freddie").td(\@freddiesales),th("Spot").td(\@spotsales),
])
);
I VPR 52© Copyright 2007 Haim Levkowitz
program that producescurrent results must:
1. Open file and read lines into array of strings
2. Split first four rows (responses from females)into arrays of votes for four age groups
3. Unshift row titles into vote rows(making them first elements)
4. Create column titles row with thput its address in array
I VPR 53© Copyright 2007 Haim Levkowitz
5. Use td on each rows of votes
6. Push addresses of rows of votes onto rowaddress array
7. Create table using Tr on array of rowaddresses
8. Repeat Steps 2-7 for last four rows of data(responses from males)
I VPR 54© Copyright 2007 Haim Levkowitz
--> SHOW conelec2.cgi--> SHOW Figure 10.7
I VPR 55© Copyright 2007 Haim Levkowitz
FIGURE 10.7 Surveyresults page
I VPR 56© Copyright 2007 Haim Levkowitz
10.6 Cookies
- session = time span during which browserinteracts with particular server
- HTTP protocol is stateless- But, several reasons why useful for server to
relate request to session- Shopping carts for many different simultaneous
customers- Customer profiling for advertising- Customized interfaces for specific clients
I VPR 57© Copyright 2007 Haim Levkowitz
Approaches to storingclient information:
- Store on server- too much to store!
- Store on client machine- this works
I VPR 58© Copyright 2007 Haim Levkowitz
cookie = small object ofinformationname and text value
created by some software system on server(maybe CGI program)
Every HTTP communication between browser and server includesinformation in header about message
when cookie createdgiven lifetime
Every time browser sends request to server that created cookie,while cookie is still alivecookie included
I VPR 59© Copyright 2007 Haim Levkowitz
browser can be set to reject cookies
- CGI.pm includes support for cookies
cookie(-name => a_cookie_name, -value => a_value, -expires => a_time_value);
- name can be any string- value can be any scalar value- time is a number followed by a unit code (d, s, m, h, M, y)
I VPR 60© Copyright 2007 Haim Levkowitz
Cookies must be placed inHTTP headerwhen header created
header(-cookie => $my_cookie);
To fetch cookies from HTTP requestcall cookie with no parametershash of all current cookies returned
To fetch value of one particular cookiesend cookie’s name to cookie function
$age = cookie(′age′);
I VPR 61© Copyright 2007 Haim Levkowitz
Example:
cookie that tells customer time of last visit to thissite
Use Perl function, localtimeto get parts of time
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =localtime;
SHOW time_date.pl
I VPR 62© Copyright 2007 Haim Levkowitz
CGI program to solve ourproblem:1. Get cookie named last_time
2. Get currentday of weekmonthday of month& put them in cookie
named last_time
3. Put cookie in header of return document
I VPR 63© Copyright 2007 Haim Levkowitz
4. If no existing cookieproduce welcome message for first-time visitor
5. If there was cookieproduce welcome message
includes previousday of weekmonthday of month
SHOW day_cookie.pl