Homework 1 - due Sept 8 at 2 pm

In this homework, we'll do the server end of http. First copy the template code in svn://bits.cs.uic.edu/cs450f09/homeworks/hw1 to hw1 in your submission directory (yourusername/hw1).

Based on 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 hw1, you should be able to run

./hw1 8080 WWW

and then visit http://localhost:8080/index.html, to see the simple test page in the WWW folder of the hw1 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.
  • 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 fs_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 154 lines. Think before you code and try to keep your code manageable.

Turn-in

Commit your finished homework at

svn://bits.cs.uic.edu/cs450f09/students/yournamehere/hw1

where, obviously, yournamehere should be replaced by your nessie/UIC email id. To ensure that your submission is complete, follow the procedure that we will use when grading your homework. In a brand new, empty directory, do the following:

svn checkout svn://bits.cs.uic.edu/cs450f09/students/youraccount/hw1
cd hw1
make
./hw1 8080 WWW
# and now visit http://localhost:8080/ and click the link, using firefox and perhaps one other browser.
Topic revision: r2 - 2009-08-31 - 04:21:26 - Main.jakob
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF