You are on page 1of 8

In the good old days...

Dynamic Documents
• Years ago… the WWW was made up of • Dynamic Documents can provide:
(mostly) static documents. – automation of web site maintenance
– Each URL corresponded to a single file stored – customized advertising
on some hard disk. – database access a!
l ide
• Today - many of the documents on the – shopping carts Coo
WWW are built at request time. – date and time service
– URL doesn’t correspond to a single file. – well paying jobs for netprog students.

1 2

Web Programming Custom Server


• Writing programs that create dynamic • Write a TCP server that watches a “well
documents has become very important. known” port for requests.
• There are a number of general approaches: • Develop a mapping from http requests to
– Create custom server for each service desired. service requests.
• Each is available on different port. • Send back HTML (or whatever) that is
– Have web server run external programs. created/selected by the server process.
– Develop a real smart web server • Have to handle http errors, headers, etc.
• SSI, scripting, server APIs.
3 4
An Example Custom Server WWW based time and date server
Copyright @1999 DaveH Enterprises

• We want to provide a time and date service. – Listen on a well known TCP port.
• Anyone in the world can find out the date – Accept a connection.
and time (according to our computer)!!! – Find out the current time and date
• We don’t care what is in the http request, – Convert time and date to a string
our reply doesn’t depend on it. – Send back some http headers (Content-Type)
– Send the string wrapped in HTML formatting.
• We assume the request comes from a
– Close the connection.
browser that wants the content formatted as
an HTML document.
5
loop forever 6

Another Money Making Scheme


Accessing our custom server.
Example
• We can publish the URL to our server, or • Keep track of how many times our server is
embed links to the server in other HTML hit each day.
documents. • Report on the number of hits our server got
• We need to make sure the server is always on any day in the past!
running (on the published host and port). • The reply now does depend on the request.
• Once we are famous we can include • We have to remember that the request
advertisements and make money! comes from a HTTP client, so we need to
accept HTTP requests.
7 8
Fancy means $$$
Time & Date Hit Server
• We want to provide a table that lists the
• Each request comes as a string (URI) number of hits received each hour of the
specifying a resource. ! day in question
pliant
• Our requests will look like this: Com
Y2K
/mm/dd/yyyy timedate.com hit report for 01/17/1999

• An example URL for our service:


http://www.timedate.com:4567/01/17/1999 hour number of hits
12-1AM 4,320
• We will get a request like: 1-2AM 18,986
GET /01/17/1999 HTTP/1.1 2-3AM 246

9 10

timedate.com Hit Table


HTML Basics <TABLE>
<TR>
<TH>hour</TH>
• I assume everyone knows something about hour number of hits
<TH>number of hits</TH>
HTML. </TR>
12-1AM
1-2AM
4,320
18,986
2-3AM 246
– If not: check the home page for some links. <TR>
<TD>12-1AM</TD>
• HTML Tables: <TD>4,320</TD>
– <TABLE> , </TABLE> start/end a table </TR>
<TR>
– <TR> , </TR> start/end a table row <TD>1-2AM</TD>
– <TD> , </TD> start/end a table cell <TD>18,986</TD>
</TR>
– <TH> , </TH> start/end table header cell
11 12
New code Drawbacks to Custom Server
loop forever
– Listen on a well known TCP port. Approach
– Accept a connection.
• We might have lots of ideas custom services.
– Record the “hit” in database.
– Each requires dedicated address (port)
– Read request - parse request to month,day,year
– Each needs to include:
– Lookup hits for month,day,year in database.
• basic TCP server code
– Send back some http headers (Content-Type) • parsing HTTP requests
– Create HTML table and send back to client. • error handling
– Close the connection. • headers
– Collect millions in advertising revenues. • access control (might want users to pay each time they
check the time and date!)
13 14

Another Approach Example Smart Server


• Take a general purpose Web server (that • Have the server read each HTML file as it
can handle static documents) and have it sends it to the client.
process requested documents as it sends • The server could look for this:
them to the client. <SERVERCODE> some command </SERVERCODE>
• The documents could contain commands • The server doesn’t send this part to the
that the server understands (the server client, instead it interprets the command and
includes some kind of interpreter). sends the result to the client.
• Everything else is sent normally.
15 16
Example Commands Example Document
<SERVERCODE> Time </SERVERCODE> <TITLE>timedate.com Home Page</TITLE>
<SERVERCODE> Date </SERVERCODE> <H1 ALIGN=CENTER>Welcome to timedate.com</H1>
<SERVERCODE> include fancygraphic </SERVERCODE>
<SERVERCODE> Hitlist </SERVERCODE>
The current time is
<SERVERCODE> Include file </SERVERCODE> <SERVERCODE> time </SERVERCODE>.<P>
<SERVERCODE> randomfile directory </SERVERCODE>
Today is <SERVERCODE> date </SERVERCODE>.

Visit our sponser:


<SERVERCODE> random sponsor </SERVERCODE>

17 18

Real Life - Server Side Includes SSI Directives


• Many real web servers support this idea (but • SSI commands are called directives
not the syntax I’ve shown). • Directives are embedded in HTML
• Server Side Includes (SSI) provides a set of comments. A comment looks like this:
commands that a server will interpret. <!-- this is an HTML comment -->
• Typically the server is configured to look
for commands only in specially marked • A directive looks like this:
documents (so normal documents aren’t <!--#command parameter=“arg”-->
slowed down).
19 20
SSI Directives
Some SSI Directives include: inserts the contents of a text file.
<!--#include file=“banner.html”>
echo: inserts the value of an environment
variable into the page. flastmod: inserts the time and date that a file
SSI servers keep a number of useful things in was last modified.
environment variables: Last modified <!--#flastmod file=“foo.html”>
DOCUMENT_NAME, DOCUMENT_URL
Example usage: exec: runs an external program and inserts the
This page is located at
<!--#echo var=“DOCUMENT_URL”-->.
output of the program.
Current users <!--#exec cmd=“/usr/bin/who”>
21 22

SSI Example - exec


SSI Examples <H1>Directory listing using the <CODE>#exec</CODE>
SSI directive</H1>

• There are a number of SSI examples you <PRE>


<!--#exec cmd="ls -al"-->
can play with on the course Web. </PRE>

<HR>
• If you want the server to handle SSI
directives you should name the file <H3> Here I forgot to use the HTML
<CODE><PRE></CODE> tag:</H3>
something.shtml (not something.html)
<!--#exec cmd="ls -al"-->

23 24
Another SSI Example
<!--#INCLUDE FILE="header"-->
More Power
It is now:
<!--#config timefmt="%I:%M 0 (%Z)"-->
• Some servers support elaborate scripting
<!--#echo var="DATE_LOCAL"-->
<BR> languages.
Today is: • Scripts are embedded in HTML documents,
<!--#config timefmt="%A, %B 0.000000e+00, %Y"-->
<!--#echo var="DATE_LOCAL"--><BR>
the server interprets the script:
– Microsoft Active Server Pages (ASP)
<!--#INCLUDE FILE="footer"--> • JScript, VBScript, PerlScript

<!--#config timefmt="0"--> – Netscape LiveWire


This file last modified • JavaScript, SQL connection library.
<!--#echo var="LAST_MODIFIED"--> – There are others...
25 26

Server Mapping and APIs External Programs


• Some servers include a programming • Another approach is to provide a standard
interface that allows us to extend the interface between external programs and
capabilities of the server by writing web servers.
modules. – We can run the same program from any web
• Specific URLs are mapped to specific server.
modules instead of to files. – The web server handles all the http, we focus
on the special service only.
• We could write our timedate.com server as
– It doesn’t matter what language we use to write
a module and merge it with the web server.
the external program.
27 28
Common Gateway Interface CGI Programming
• CGI is a standard interface to external • We will focus on CGI programming.
programs supported by most (if not all) web
servers. • Project 4 will be a CGI program, you will
• The interface that is defined by CGI includes: create a web-based scheduling system.
– Identification of the service (external program).
– Mechanism for passing the request to the external
program.
• CGI programs are often written in scripting
languages (perl, tcl), we will concentrate on
C (although you can do project 4 in perl).
29 30

You might also like