Title: World Wide Web Components
1World Wide Web Components
- Browsers and Servers
- CGI Processing Model
2WWW is example of Client/Server Computing
- Server computers are located all around the world
and respond to requests (messages) from Computers
running Browsers. - Browsers understand HTML, (and now Javascript,
Java etc.)
3Server Browser Interaction (simple)
http request
B
Server
HTML
4Non-HTML files
- Server sends a header in front of each file
identifying the file type (HTML,GIF,JPEG etc.) - Most Browsers understand HTML, GIF and TEXT
- Browsers can be configured to call external
programs to handle new types of files
5Helper Apps
- These programs are called HELPER applications and
dramatically extend the capabilities of the
browser, since they can be developed
independently of the client software - Examples - Quicktime viewers, sound players, VRML
viewers etc. - To see the currently configured viewers go to
options on the Netscape title bar
6Forms and CGI Programming
- HTML provides an easy to use FORM capability,
which allows a wide variety of input forms to be
easily generated. - Forms include
- Text input - One line of text
- Textarea - Multiple lines of text
- Check boxes (on/off)
- Radio boxes (1 of N)
- Etc.
7Forms Processing Logic
- Output of Form is formatted and sent to Server,
along with the name of a program to process the
output of the form. - WEB Server takes information from form, and
passes it on to a Common Gateway Interface
Program (CGI) - Output of CGI program is sent back to Client
browser as an HTML (or other) file.
8CGI programming extends power of WWW
- CGI programs can do an almost unlimited set of
activities ... - Look up info in a database and send it to
Browser. - Take input from user and add to a file.
- Take input and send to a standard business
application - CGI program can be in any language that runs on
the server
9CGI Programming
http forms
B
http server
HTML
input
output
CGI Program
(Note, all processing is on server)
10What do you need to do for CGI?
- Develop form to collect information from users
- Write and test CGI program to handle form
information - Put the name of the CGI program in the ACTION
statement of the form.
11CGITwo Processing options
- Two Types of FORM processing options, GET and
POST - GET - parameters sent additions to URL string.
Each individual parameter separated by - POST - Data sent in message body. This is
slightly more general
12CGI Processing - review
- Server sends form to client
- Client displays form, and users fills in fields
- Client sends form info to server
- Server runs the CGI program named in the ACTION
section of the FORM.. - CGI program parses data as input
- Output of CGI program is sent by the server to
the client (i.e. it should be HTML)
13CGI Advantages and Disadvantages
- Advantages
- Very general model, easy to do really neat things
like front end existing applications, databases
etc. - Many toolkits available to do common things
- Disadvantages
- All processing is done on server. May overload
server
14Writing a CGI Program
- CGI program needs to
- Parse form input
- Process the input
- Generate html output
15GET vs. POST
- GET format
- Information is passed as a series of
variablevalue pairs separated by to program
named in action statement by adding them on to
the URL (after a ?) - Simple example one line form with a field named
userid and ACTIONmycgiprog.cgi - User enters nwhite
- Browser sends the following to the web server
- http//www.stern.nyu.edu/nwhite/mycgiprog.cgi?use
ridnwhite
16GET ProcessingServer Side
- Web server takes the information after the ?
and creates an environment variable named
QUERY_STRING, then executes the program
mycgiprog.cgi - QUERY_STRING contains
- useridnwhite
- CGI program retrieves value of QUERY_STRING, does
appropriate processing, and (optionally) sends an
HTML response back
17GET method more than one parameter
- What if we want have more than one field?
- No problem QUERY_STRING can contain many
variablevalue pairs separated by - i.e.
- useridnwhitepasswordjunkfnameNorman
- Possible problem, how big can environment
variables be (how many characters) - GET only useful for limited input
18POST
- POST method more general
- Input is passed as a series of input lines
- Variable1value1
- Variable2value2
- .
- Environment variable CONTENT_LENGTH is set to the
number of lines of input. - Input processing logic needs to be different for
GET and POST methods
19CGI Output
- CGI output is passed back to the browser, hence
has to be something (HTML) the browser can
understand Like - Content-type text/html
- ltHTMLgtltHEADgt
- ltTITLEgtoutput of HTML from CGI
scriptlt/TITLEgt - lt/HEADgtltBODYgt
- ltH1gtSample outputlt/H1gt
- What do you think of
ltSTRONGgtthis?lt/STRONGgt - lt/BODYgtlt/HTMLgt
20Simple Example GET Method
- List the contents of your multim directory
- Create a Shell Script named lister.cgi which
contains - ! /bin/sh
- echo content-type text/html
- echo lthtmlgtltheadgtlttitlegtListinglt/titlegt
- echo lt/headgtltbodygtltpgt
- pwd
- ls alt
- echo lt/bodygtlthtmlgt
21Try it, what happens?
- To run it, put it in your multim directory
- chmod rx lister.cgi
- Type the following as a URL
- http//www.stern.nyu.edu/userid/multim/lister.sh
22CGI Example POST Method
- Adds links to a file of students in a class
- Password Protected
- Steps
- Parse data (POST form)
- Check Password
- Add info (in html format) to end of roster
23- include ltstdio.hgt
- ifndef NO_STDLIB_H
- include ltstdlib.hgt
- else
- char getenv()
- endif
- define MAX_ENTRIES 10000
- typedef struct
- char name
- char val
- entry
- char makeword(char line, char stop)
- char fmakeword(FILE f, char stop, int len)
- char x2c(char what)
- void unescape_url(char url)
24void unescape_url(char url) void
plustospace(char str) main(int argc, char
argv) entry entriesMAX_ENTRIES
register int x,m0 int cl int PASS
FILE fp printf("Content-type
text/htmlcc",10,10) if(strcmp(getenv("REQU
EST_METHOD"),"POST")) printf("This
script should be referenced with a METHOD of
POST.\n") printf("If you don't
understand this, see this ") printf("ltA
HREF\"http//www.ncsa.uiuc.edu/SDG/Software/Mosai
c/Docs/fil\ l-out-forms/overview.html\"gtforms
overviewlt/Agt.c",10) exit(1)
if(strcmp(getenv("CONTENT_TYPE"),"application/x-ww
w-form-urlencoded")) printf("This
script can only be used to decode form results.
\n")
25if(strcmp(getenv("CONTENT_TYPE"),"application/x-ww
w-form-urlencoded")) printf("This
script can only be used to decode form results.
\n") exit(1) cl
atoi(getenv("CONTENT_LENGTH")) PASS 0
for(x0cl (!feof(stdin))x) mx
entriesx.val fmakeword(stdin,'',cl)
plustospace(entriesx.val)
unescape_url(entriesx.val)
entriesx.name makeword(entriesx.val,'')
/ Check here for passwd field Z /
if (entriesx.name'p') if
(strcmp(entriesx.val,"MDSS") 0) PASS 1
if (PASS 1) if ((fp
fopen("/class/nwhite/b3146/roster.html","a"))
NULL) PASS3 printf("lth1gt
unable to open output file, tell Professor
Whitelt/h1gt")
26 NULL) PASS3 printf("lth1gt unable
to open output file, tell Professor
Whitelt/h1gt")
else fprintf(fp,"ltA href\"http//www.stern
.nyu.edu/s\" gt Home Page for s \n \ lt/agt",
entries0.val,entries2.val)
fprintf(fp,"ltpgtlth3gtWhy you might want to visit
the homepage for s ... lt/h3\ gt\n ltpgt",
entries2.val) fprintf(fp,"s
ltpgt",entries3.val) fprintf(fp,"ltpgtlthrgtlthrgtlt
pgt") printf(" Link successfully added
\n") printf("lta href\"http//www.stern.nyu.
edu/nwhite/class/b3146/roster.html\ \"gt
ltbgtClick here to see the updated rosterlt/bgt
lt/agt") if (PASS ! 1)
printf("ltH1gt INVALID PASSWORD, See Professor
White lt/H1gt")
27Conclusion
- World-Wide-Web model is much more powerful than
it appears on the surface - Easily integrated with existing applications
- Easy to add new functionality
- CGI model can do lots of things
- Update files
- Link to corporate databases
- Specialized Applications
28Caveats
- Problems
- Overhead
- Need to start up a new program for every request
- Scalability
- All processing on server, what happens as usage
grows? - Reliability
- How do we replicate for redundancy?