Murdoch University Logo B336 Advanced Internet Computing

Laboratory Week 5


Learning Objective:

  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:

Week 4 lecture 1
Unit Reader section Four: A Simple Web Server

Access to Software:

The following exercises may be done on red.murdoch.edu.au. Instructions on how to access red were given in the week 2 and week 3 labs. Since the web server needs to reside on a publicly accessible machine, they cannot be executed on a local client machine such as the ones in the internal labs.

Exercises:

In the following exercises, replace all occurences of "12345678" with your own 8-digit student number, and "15678" with "1" followed by the last 4 digits of your students number.

  1. Start a ssh or telnet session to red.murdoch.edu.au. Copy the scripts web_serial.pl and Web.pm from the test account by executing the following commands (note the space before the dot):

    cp ~test/examples/web_serial.pl .
    cp ~test/examples/Web.pm .


    Edit the Web.pm file and change '/home/www/htdocs' near the beginning of the file to the directory your scripts are now in. This will set the directory to serve documents from, much like the DocumentRoot directive in Apache. You can determine your current directory by typing the command:

    pwd

    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 3 (that is, a "1" followed by the last 4 digits of your student number). 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.

  2. Now you have your simple web server running listening to port 15678. Create some simple resources for your server to serve by putting files into the same directory as where the scripts are. For example, create a simple HTML file mypage.html (either copy it from your Apache htdocs directory, FTP it from your local machine, or write a new one on red using an editor like pico).

    Now use a web client to make the request from the server. Start another ssh or telnet session to red. Go to the directory where you had your web client scripts get_url.pl andget_url_more.pl from last week. Use the scripts to make requests and display responses from your server. Eg.

    perl get_url_more.pl http://red.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. You may need a few more windows to red to do this without having to repeatly stop and start web_serial.pl.

  3. 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 exercise 2 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.

  4. Try using Lynx (a text-based web browser) on red to make requests to your web server. Again, using any window logged-in to red (NOT the one running web_serial.pl) type the command:

    lynx http://red.murdoch.edu.au:15678/mypage.html

    Lynx should show the HTML contents of mypage.html.

    Now go to the window where web_serial.pl is running, and look at the request sent by Lynx. Compare that to the one sent by your get_url.pl or get_url_more.pl. What are the differences?

    Access a few more resources (created in exercise 2 above) on your web server using Lynx, and compare the request messages. To go to a web resource like HTML pages in Lynx, type "g" and then the URL. To quit Lynx, type "q".

  5. (You will not be able to this particular exercise from off campus until we resolve the external access problem with red). Repeat exercise 4 using Microsoft Internet Explorer and Netscape Navigator, instead of Lynx.

  6. Read through the lecture notes and Unit reader, and understand the basic structure of the code in web_serial.pl and Web.pm files. The following few exercises will involve editing and modifying the code.

  7. Stop web_serial.pl by typing <Ctrl-C>. Identify the section of code in web_serial.pl or Web.pm which deals with creating and sending the response to the web client. Add to the code so that it returns a brand new header - make up your own header field name, and give the header any value you like (but remember to add $CRLF (ie. two newline characters) to the end of your header line). Run the web_serial.pl script again.

    Change your get_url_more.pl so that it can display the new header (if you have completed Assignment 1 part III, then it would already be doing so). Now use get_url_more.pl to access a file (any file) on your server again. What is the return result? Can you see the new header?

    If you were to implement your own web server and client for specific purposes, how would this mechanism be useful?

  8. Stop web_serial.pl by typing <Ctrl-C>. Identify the section of code which deals specifying the MIME-type of the resources returned. Modify the code so that if the file requested has extension ".html", the MIME-type returned is "text/plain". Run the web_serial.pl script again.

    Use get_url_more.pl to access mypage.html again. Look at the "Content-type" field. What does it show?

    Use Lynx, Netscape Navigator or MSIE to access mypage.html through your server. Now that you have changed the MIME-type for .html files, how would you expect the browser to do when it receives the file from your server? How is this different from made the code changes? Did what you expect happen?

  9. Identify the section of the example code which deals with when a requested resource is a directory. What does the web server do to redirect the client? What is the important header that gets sent back to the client in the HTTP response? Based on what you saw with your web client exercises last week, what does a web client do when it sees that particular header?

  10. You now should have sufficient guidance to complete Assignment 1 part II. Start on the work.


Document author: H.L. Hiew, Unit Coordinator
Last Modified: Wednesday, 21-Mar-2001 00:05:11 MST
Disclaimer & Copyright Notice © 2001 Murdoch University