Homework 2: A Home-Brew Multi-Threaded Web Server

In this homework, we’ll do the server end of http. First pull the template in git@git.uicbits.net:cs450-f13/public.git and add it to your own repository. To check out the public course repository, run

git pull

in the public repository on your local machine. This time around, the template only contains a Makefile. And there are also some files located in directory WWW which is used as the content of your webserver.

Based on examples/sockets/server.c, create a web server application that takes a port and a directory name as arguments, and serves the contents of this directory over http. For example, after building hw2, you should be able to run

 ./hw2 8080 WWW
 

and then visit http://localhost:8080/index.html, to see the simple test page in the WWW folder of the hw2 template. Clicking on the link should take you to a second page, than contains an image. You may change the files in the WWW directory, as long as there is at least two pages connected by a link, and at least one image embedded in a page.

You need to handle the following cases:

  1. non-existing file: return error 404 with a readable error message, just like a webserver would
  2. html, text, jpeg, gif and png files should all display properly. return the proper HTTP Content-type header, based on the file ending. Also, pdf files should be handled correctly.
  3. if the requested path is a directory, you should handle request as if it was for the file index.html inside that directory. hint: use stat() to determine if a path is a directory or a file. the st_mode field in the stat struct has what you need.
  4. some clients may be slow to complete a connection. Your server should be able to serve multiple clients concurrently, not back-to-back. For this homework, use multithreading with pthreads. We’ll try event-based concurrency in hw4.
  5. the webserver should response with a list of saved files when the user requests a directory that does not contain an index.html file. Note that there should be no additional files created on the server’s disk to response the request. The detailed requirement for this list is same to the default operation of running
python -m SimpleHTTPServer

This command is to set up a simple webserver based on python on your local machine. Therefore, before you run this command, you will need to install python on your local machine . After running this command, visit the web page http://localhost:8000 with any web brower, for example chrome, to see the list of content of directory where you run the command. And the list your own webserver generate should look like it.

Note that if you use port 80, there is no need to indicate the port in the URL, and you should also be able to fetch the pages with hw0. However, port 80 requires administrative privileges on your machine, which you may not have, depending on what machine you are doing the homework on.

HINT: useful libc functions to use are stat(), strstr()/strcasestr(), sprintf(), sscanf(). The code in examples/sockets/server.c should be very helpful. My entire solution, starting from the 68-line server.c, with lots of comments and nice formatting, is 124 lines. Think before you code and try to keep your code manageable.

TURN-IN

Commit your finished homework at:

git@git.uicbits.net:cs450-f13/YOURUSERNAME/hw2

where, obviously, yournamehere should be replaced by your UIC netid. To ensure that your submission is complete, follow the procedure that we will use when grading your homework.

First, in a brand new, empty directory, do the following:

git clone git@git.uicbits.net:cs450-f13/YOURUSERNAME.git
cd YOURUSERNAME/hw2
make
./hw2 8080 WWW

and now visit http://localhost:8080/ and click the link, using firefox and perhaps one other browser.

Once everything looks right to you, run the automatic grading script in pub/grading-scripts/hw2.

Grading

Grading will be done automatically using a script. We will publish this script after grading has completed; you are responsible for writing your own test cases. If you wish, you can share test cases you have written with the class.