Title: Internet
1Internet Communications
- Server-side programming for
- the WWW (lecture 6)
- CGI Programming
- Using Shell Scripts for CGI
2Server-side programming
- May be useful to send data back to server, and
run a program there. - Program might store user details in database or
file, or do some calculation and return result to
user. - A number of technologies available Servlets,
JSP, CGI, ASP, PHP - Well look briefly at CGI, Servlets and JSP.
3Common Gateway Interface (CGI)
- CGI is a very general standard specifying how
programs can be run on server, from the WWW. - Any program in any language can be a CGI program
- it just has to follow the CGI rules.. - These define how programs get data (e.g., HTML
form data) and how to make sure web server knows
its a CGI program.
4CGI
- A CGI program is stored on the server, executed
on the server, in response to request from
client. - By running a CGI program, rather than delivering
a static HTML page we can - Put up to date information on web page (e.g.,
weather forecast product availability). - Respond appropriately to user input.
- Store user data from form in a file or DB.
5Call of a CGI Program
- You can call CGI programs in just the same way
that you get HTML pages. - A link such as following will run CGI program
when clicked - Or you can open that location directly (file
open)
lta hrefhttp//www.mysite/cgi-bin/myproggt Run
my CGI program lt/agt
6Security
- If you write a CGI program you are letting anyone
in the world run a program on your system. - Malicious users may be able to exploit security
loopholes, and cause problems on your system. - Because of this many Web site hosts will not yet
ordinary users create CGI programs. - Where use is permitted special wrapper programs
may be required that do some security checks.
7How does it know its CGI?
- How does the web server know whether its dealing
with an HTML page, or a CGI program? - The Web server will be set up so that it is clear
which files are to be treated as CGI. - Common to have to put CGI programs in a cgi-bin
directory. - Access may be restricted so ordinary users cant
access this directory.
8CGI in MACS
- Users can make their own cgi-bin directories
(under www directory). - You can then place CGI programs in this directory
and try them out. - But to prevent security problems, a wrapper
program is used. - You therefore use the following URL to access
your CGI programs
Http//www.macs.hw.ac.uk/cgi-bin/cgiwrap/YOURLOGIN
/YOURPROG
9CGI Examples
- Any programming language can be used for CGI.
- But simplest to illustrate CGI using shell
scripts - scripts containing Unix commands. - Every CGI program must write out data to send
back to web browser. - The first thing they must write out is MIME type
of file (e.g., text/plain, text/html)
!/bin/sh echo Content-type text/plain echo ech
o Hello World
Run it
Download it
10Shell Scripts
- You can try out shell scripts independent of CGI
programming in Unix environment. - Create a file containing the following
- Make it executable.
- Run it simply by typing its name at the Unix
prompt. - Try out all CGI scripts this way before trying
them across web.
!/bin/sh echo "Hello"
11CGI Activity
- Create yourself a cgi-bin directory
- Make sure that it is readable/executable by
others. - Create script similar to one on last slide.
- Make it executable by others.
- Run it from a Unix prompt to test.
- Run it from web browser by opening appropriate
location - (http//www.macs.hw.ac.uk/cgi-bin/cgiwrap/YOURLOGI
N/YOURFILE) - Now make the above URL a link from one of your
HTML files. Try out the link.
(ie, lta href" http//www.macs.hw.ac.uk/cgi-bin/cg
iwrap/YOURLOGIN/YOURFILE "gt link lt/agt
12CGI Example 2
- You can add Unix commands to your CGI to get,
say, a listing of your files, or the date!
!/bin/sh echo Content-type text/plain echo ech
o The date is date echo The files in my
cgi-bin are currently ls
Run it
Download it
13CGI Example 3
- You can also output HTML (usual practice)
Note content type now text/html
!/bin/sh echo Content-type text/html echo echo
lthtmlgt ltheadgt lt/headgt echo ltbodygt lth1gt My
files lt/h1gt ls echo lt/bodygt lt/htmlgt
Second "echo" line is vital
Download it
Run it
Observe how IE tries to interpret any nonsense as
HTML
14CGI Input Data
- You can pass data to a CGI program - encoded as
string looking likevar1val1var2val2var3val3
.. - You add this to the URL, after a ?
- Example
- This data is then available through the
environment variable QUERY_STRING
cgiwrap/alison/test?namealisonaddrEdinburgh
15CGI Example 4
- Example writing back query string to the browser
!/bin/sh echo Content-type text/plain echo echo
The data passed to this program is echo
QUERY_STRING
Download it
Run it
16CGI and Forms
- You can use CGI programs to process data from
forms. - Specify the CGI program using the action
attribute of the form - If methodget then the form data gets put in
QUERY_STRING as before.
ltform methodget actionhttp//./cgi-bin/cgi
wrap/alison/test.cgigt
17Example
ltform method"get" action"http//www.macs.hw.
ac.uk/cgi-bin/cgiwrap/alison/inetcomms/example4.cg
i"gt ltpgt Name ltinput type"text" name"username"
/gt lt/pgt ltpgt Age ltinput type"text" name"age" /gt
lt/pgt ltpgt ltinput type"submit" value"Do it" /gt
lt/pgt lt/formgt
Try it
- Exercise Try one of your own forms with the
above action. Then try writing your own script to
handle the data.
18GET vs POST
- Above examples use the "get" method to handle the
data from the form. - Using get method
- Data added to URL as ..prog?varval etc.
- This data is put in QUERY_STRING variable
available to CGI programs - Alternative is to use post method
- Data is sent separately to URL.
- CGI program reads this data from its standard
input.
19Get vs POST
- Get method isnt very secure - all data visible
in URL. - Get method suitable for small amounts of data,
but not for larger amounts. - But well just use GET..
20CGI Example 4
- We can use all this to do a very basic version of
program to store data from form in file - Stores data from form in "MyFile"
!/bin/sh echo Content-type text/plain echo ech
o QUERY_STRING gtgt MyFile echo Done it
Download
Try it
21CGI Summary
- CGI is standard for interfacing web client to
programs run on server. - Specifies location of files (so server knows to
execute them!) and how input data is handled. - Output of CGI must specify MIME type (e.g,
text/plain, text/html) - Rest of output displayed on web page
appropriately. - Simple examples using shell script, but need more
serious language for complex ones.