Assignment 6

Note: You may work with up to one other person on this assignment. It is due 2:40pm, Friday, November 30.

Objectives

HTTP/0.9

HTTP/0.9 is the original, very basic HTTP protocol. It consists of two steps:

  1. The client opens a connection to the server and sends it a message like ``GET /index.html''. This message will begin with GET, and the next word will be the requested filename. (It may include additional words after it, but an HTTP/0.9 server will ignore them.)

  2. The server sends the file back to the client and then closes the connection.

Later versions of the protocol (in use by today's prominent browsers and servers) are much more complicated, but they retain backward compatibility with HTTP/0.9.

Part A: The single-threaded server

Write an HTTP/0.9 Web server. For this part of the assignment, it should support only one client at a time.

You'll want to run getcs 350 6 to download some starting code for your program. The code in helper.C implements three functions to help you out.

Additional documentation about these functions is in server.h.

All of your code in this assignment should go into the file server.C. The server should be robust to browser behavior: It should send a descriptive error message to the client if the request is invalid or if the file is not available.

When you access files, use system calls like read() and write(). (You may still want to use the C++ I/O streams for debugging purposes, but of course you won't include this in your final solution.)

Your server won't be able to use the default HTTP port (port 80), since Unix systems reserve the first 1024 ports for files run by the root account. The port number will be specified on the command line.

You should be able to use your browser to test your program. Suppose your program is running on sun3 on port 5000. Then, to test your server, you can type the following into the Location text field of your browser window.

http://sun3:5000/usr/dict/words
This would send the ``GET /usr/dict/words'' to your program, and your program should respond with the file. (The browser probably won't display it nicely, since it will likely assume the file contains HTML, which it doesn't.)

Part B: The multithreaded server

After you've written and tested your server from Part A, reorganize it to use multiple threads instead, so that your server can respond to many clients simultaneously. Your server should spawn off a new thread as soon as somebody connects to the server. (For this part, your server need not concern itself with managing threads once they are created.)

You do not need global variables to accomplish this. In fact, you want to avoid using global variables, as any global variables will bring up synchronization issues that are best avoided.

To test this, you can try to load a large file in two different browser windows. If successful, you'll find that both windows are loading the same file simultaneously.

Part C: Limiting the number of threads

Finally, modify your program so that at no time will there be more than MAX_CLIENTS clients being serviced at once.

To accomplish this, you'll want a global variable to count how many clients are currently being handled. Before the main thread accepting clients spawns off a new thread, it will want to wait until this global variable is below MAX_CLIENTS. It will increment this global variable and then spawn off the new thread. Just before finishing, a thread that responds to the client should decrement this global variable.

Your solution must use a condition variable to avoid busy-waiting by the main thread, and it must be robust to all possible code execution sequences.

To test your solution for this part, you may want to add a call to sleep(1) in the response thread's function, to add a one-second delay to the thread before it exits. And you may want to print (to the screen) the number of threads currently running just before you spawn the new thread. By reclicking Reload in the browser in rapid succession, you should find that it waits to begin responding eventually. (Naturally, this code would be for debugging purposes only and so should not be in your final solution.)

What to hand in

Type handincs 350 6 to hand in this assignment. I'm only interested in your most complete solution. (That is, if you complete Part C, don't bother trying to indicate what you did with Part A.)

I will evaluate your solution based on the following breakdown.

30 pts Part A
20 pts Part B
20 pts Part C
10 pts code documentation and formatting