Homework 2 - A Home-Brew Web Server

In this homework, we'll do the server end of http. First export the template in svn://cs450.cs.uic.edu/pub/homeworks/hw2 and add it to your own repository. This time around, the template only contains a Makefile.

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:

  • non-existing file: return error 404 with a readable error message, just like a webserver would
  • 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.
  • 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.

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.


Commit your finished homework at:


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:

svn export svn://cs450.cs.uic.edu/f11/yournetid/hw2
cd hw2
./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 will be done automatically using a script. You can check your grade before final submission using the script. You can get the grading script using:

svn co svn://cs450.cs.uic.edu/pub/

or just svn up in your /pub if you already have one checked out. The grading script is in /pub/grading-scripts/hw2/. To run the grading script:

./hw2_score username port score_file

This script will create a directory with your username, download hw2 from svn and create output_file and score_file inside hw2. The score file will look like following:

[SUCCESS] index.html created for host:port? [0.25]: 0.25
[SUCCESS] header correct for host:port? [0.25]: 0.25
[SUCCESS] pic.html created for host:port/pic.html? [0.25]: 0.25
[SUCCESS] monorail.jpg created for host:port/monorail.jpg? [0.25]: 0.25
[SUCCESS] google.gif created for host:port/google.gif? [0.25]: 0.25
[SUCCESS] header correct for host:port/google.gif? [0.25] [0.25]: 0.25
[SUCCESS] skype.png created for host:port/skype.png? [0.25]: 0.25
[SUCCESS] header correct for host:port/skype.png? [0.25] [0.25]: 0.25
[SUCCESS] paper.pdf created for host:port/paper.pdf? [0.25]: 0.25
[SUCCESS] skype.png size correct? [0.25]: 0.25
[SUCCESS] google.gif size correct? [0.25]: 0.25
[SUCCESS] paper.pdf size correct? [0.25]: 0.25
[SUCCESS] skype.png content correct? [0.25]: 0.25
[SUCCESS] google.gif content correct? [0.25]: 0.25
[SUCCESS] paper.pdf content correct? [0.5]: 0.5
[SUCCESS] index.html created for host:port/test/dir/? [0.5]: 0.5
[SUCCESS] testpage.html created for host:port/test/dir/testpage.html? [0.5]: 0.5
[SUCCESS] error returned for non-existing page? [1]: 1
Total score: 6
Topic revision: r4 - 2011-08-31 - 19:20:58 - Main.jakob
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
Helping Women Faculty Advance
Funded by NSF