ICT336 title
Murdoch University logo

Laboratory Week 5 - Implementing a Web Server


Learning Objectives:

  1. Implement a simple web server that serves web resources by responding to HTTP requests using the GET and HEAD methods.
  2. Understand the format of HTTP requests messages received by the web server, and the format of HTTP response messages sent back to the web client.

Required Reading:

Lectures: Week 4 lectures
Unit Reader 4: A Simple Web Server

Access to Software:

The following exercises may be done on gryphon.murdoch.edu.au. Since the web server needs to reside on a publicly accessible machine, it cannot be executed on a local client machine such as the ones in the internal labs.

Access to Example Scripts:

The following exercises make use of Perl scripts described in the lectures. These example scripts are available in the unit coordinator's directory (~hiew/examples/web/) on gryphon. Use the command "cp" to copy the file over to your work directory on gryphon. Eg.

gryphon:~> mkdir ~/labwk5
gryphon:~/labwk4> cp ~hiew/examples/web/* ~/labwk5

Instructions:

In the following exercises, replace all occurences of "12345678" with your own 8-digit student number, and "15678" with the port number you have been assigned in week 2's lab..

  1. Start a ssh session to gryphon.murdoch.edu.au. Copy the scripts web_serial.pl and Web.pm to your own directory using instructions given in "Access to Example Scripts" above.

    Edit the Web.pm file and change '/home/www/htdocs' near the beginning of the file to '.' (ie. a dot in quotes). This will set the server's web site to be the current directory where your web_serial.pl and Web.pm scripts are in (similar to the DocumentRoot directive in Apache). You may change this directory to any directory you wish (but please ensure that it is set to '.' for your assignment 1).
    The web_serial.pl and Web.pm scripts contain the code for the example web server given in lectures and the Unit Reader. Start the web server by executing the web_serial.pl script with a port number. Use the port number allocated to you as described in week 2. Eg.

    perl web_serial.pl 15678

    The program should just hang and do nothing while waiting to serve requests. Also, make sure your Apache web server is not running, or you will receive a port number conflict error message when you start wen_serial.pl.

  2. Now you have your simple web server running and listening to port 15678. Create something for your server to serve by putting files into where you have set the DocumentRoot variable (ie. the same directory as where the scripts are, if you follow the instructions above). For example, create a simple HTML file mypage.html (either copy it from your Apache htdocs directory, secure FTP it from your local machine, or write a new one on dijkstra using an editor like pico).

  3. Now use a web client to make a request to the server. Go to wherever you had your web client scripts get_url.pl and getall.pl from last week. Use the scripts to make requests and display responses from your server. Eg.

    perl getall.pl http://gryphon.murdoch.edu.au:15678/mypage.html

    Add a few more HTML files into different sub-directories served by your web server scripts, and access them using your web client scripts.

  4. Use your script from lab week 4 exercise 3 to access your server. Can you see the special header created by the client script?

  5. Stop web_serial.pl by typing <Ctrl-C> in the window where it is running. Add the following line of code to the line just below "my $request = <$c> ;" in Web.pm (about line 15):

    print $request ; # print the request to the command line
    Start web_serial.pl again. Request the same resources using your web client scripts as you did in (3) above. Look at the window where web_serial.pl is running. That is the format of the request message that was sent ny your web client to your web server.

  6. Try using your favourite web browser (eg. Netscape Navigator or Microsoft Internet Explorer) to make requests to your web_serial.pl web server, using the same URL as (1)-(5) above. You should see mypage.html displayed in the browser. Now go to the window where web_serial.pl is running, and look at the request sent by your web browser. Compare that to the one sent by your get_url.pl or getall.pl. What are the differences?
  7. Repeat exercise 5 using as many web browsers as you have available on your local machine. Try and spot the differences in the requests sent by the different web browsers.
  8. The following exercises will involve modifying the code in web_serial.pl and Web.pm. Similar to operating your Apache web server, none of your changes will take effect until you stop web_serial.pl (by typing <Ctrl-C>) and restart it again.

Exercises:

  1. Add to the code in web_serial.pl or Web.pm so that it returns a brand new header with the name "BigHeader" and value "XYZ" (remember to add the HTTP newline characters to the end of your header line). Use getall.pl to access the server to verify that the header was sent.

  2. Modify the code so that if the file requested has extension ".html", the MIME-type returned is "application/b336".

    Verify that the changes works by using getall.pl to access mypage.html again. What is the header field that determines the MIME-type?

    Further verify the change by using your favourite web browser to access mypage.html again. Your browser should display an error message. Is that appropriate? Why?
  3. Modify your code from exercise (1) so that the server only returns the "BigHeader" header if it sees a "LittleHeader" header in the request with value "ABC". Use your script from lab week 4 exercise 3 to test this feature.
  4. Modify your code from exercise (3) so that the server checks an "Accept" header field for a list of comma-separated MIME-type values. Eg.
    Accept: text/plain, text/html, image/gif
    Your server should return a MIME-type ONLY if the MIME-type exists in the "Accept" header field. Otherwise, it should return a status 406 (Not Acceptable) response. Modify your script from lab week 4 exercise 3 to test this feature.

    Note: the "Accept" field is a standard HTTP header field. This exercise is the beginning of implementing the specifications for handling "Accept". If you are keen, you may refer to RFC2616 section 14.1 and implement the full specification for this field.

Assessable Tasks:

Exercises 1 & 2.

Internal students should demonstrate to your tutor by executing your scripts with your tutor present. This must be done in week 5 or week 6. No marks will be awarded if the work is demonstrated after that - your tutor have no discretionary power on this deadline.

External students should submit the scripts and sample runs during the deadline as indicated in the Study Schedule.

H.L. Hiew
Unit Coordinator


Document author: H.L. Hiew, Unit Coordinator
Last Modified: Wednesday, 25-Feb-2004 03:02:34 MST
Disclaimer & Copyright Notice © 2004 Murdoch University
This document is relevant for semester 1, 2004 only