5.9 updates:
  1. Removed two checklist items referring to obsolete directory
Program Two
Simple Multi-threaded Web Server

Overview:

In this program you will follow through the steps of capturing the http stream between existing clients and servers, and write a web server that supports this same protocol. It builds on the JokeServer, which application does much of the same work. While the text of the assignment is quite long, the application itself is quite straightforward, and you might be surprised at how easily it can be written.

There are four+ phases in the development process:

  1. Capture the HTTP protocol first-hand by developing some hacking / debugging skills (hacking in the good sense).
  2. Return simple, static files on request from a browser client.
  3. Return dynamically created HTML (build a directory HTML page dynamically)
  4. Accept FORM input from the user and do back-end processing on the server to return computed values in (simple!) dynamically-created HTML.
  5. Add features of your own choosing, if you like.

See the MyWebServer Tips file for some suggestions once you get coding.

Run at port 2540 in the server directory!

In all cases these following specifications take precedence: The web server must run at port http://localhost:2540. It must, by default, serve files from the directory in which the web server is started, including dog.txt, cat.html, and lion.wml. The source code should be contained in a single, stand-alone file name MyWebServer.java.txt ready to compile and run. If you insist on (yes the more precise) use of packages, then there must be explicit, simple, step-by-step instructions on how to compile and start your web server. Subdirectories should be recursed from the default directory in which the server is started.

Special Security Note!!!!

I expect that you will find that this is not a particularly difficult assignment. If so, you will soon have a viable, running, webserver of your own creation. If you are developing on a machine that is also connected to the internet this means that you might well expose all of the files on your local machine (or any remote machine where you might be running) to evil hackers from around the world who are anxious to steal information from your files. In the worst case this information would allow them write access to your disk, and/or put financial/personal information in their hands. So -- be careful, hard-code into your server that you only return files from your root server directory of unimportant files, keep your firewall on, etc. Becareful about the "../.." form of URLS, which would allow someone to retrieve files from above your server's directory. For particularly sensitive machines you can always simply unplug your internet connection while running your server.

Administration:

Capturing HTTP:

Modify your MultiThreaded server so that it becomes a simple web server.

Goal: Your web server must correctly return requests for files with extensions of .txt, .html, and .wml. This means that it must return the correct MIME headers (That is, the Content-type (followed by two cr/lf), and Content-length headers), as well as the data. This is a server that operates on static data.

Extend your server to include directories:

Goal: Extend your server so that it sends back dynamically constructed data: in this case the HTML-formatted current contents of a directory. This is now a server that operates on dynamic data.

Server-Side scripting and program execution.

Goal: So far everything we have done is relatively straightforward. In this section we add back-end programming capability to your server, or at least simulate it. We create a simple addnums web form , accept input from a user, pass this to our webserver, process the information, and return a computed response based on the input.

For those who are more ambitious, here is a starting link on java's JNI, which allows us to call native code, by loading it into the virtual machine, and then running it. Sun JNI example. In this way we might write programs that actually run arbitrary scripts/programs under the web server.

Alternatively, for those writing in C, the "system()" function will execute any executables as subprocesses, making the running of programs and scripts trival. Note: be very security conscious of running user-input shell commands with the "system()" call, because, e.g., they might have you execute a command to erase all of your files!

Neither method is required. Instead, to keep the programming scope reasonable, we will only simulate the running of back-end scripts. CGI (the Common Gateway Interface) has been around since the beginning of the web, so there are thousands of references on how to use it such as these CGI notes which seem to date from about seven years ago.

Tu-duh! You have now built a multi-threaded web server that can handle files, directory traversals, and server-side scripting (of sorts).

What you turn in

Bragging rights (not required):