COSC 4377 - Introduction to Computer Networks

Spring 2012

MW 1:00-2:30pm at PGH347

InstructorOmprakash Gnawali

Homework 3 : HTTP Server

Due: midnight February 8, 2012

In this assignment, we will build an HTTP server.

We will design and implement a web server in this assignment. Our web server will not be as full-featured as Apache, but it will have the basic functionality of a web server and it will serve web pages to real browsers (e.g., Firefox).

HTTP servers serve content using HTTP protocol. We learnt about HTTP protocol in the last assignment. It is worth re-familiarizing yourself with HTTP protocol before we start this assignment.

Here is how we will launch our HTTP server:

./http_server -p port


./http_server -p 20001

In the example above, the user launched the server on port 20001.

Typically, web servers run on port 80. In this assignment, we will assign a port number to each student. You should test and run your web server on the port number assigned to you. If you are assigned port number 20001, once you launch the server on that port, you can access the content provided by that server using the following URL on your web browser: You should replace 20001 after the ":" with the port number you are assigned.

Implementation Guide

Web servers accept requests using a protocol called HTTP. When we type a URL into a web browser, the request to the web server is sent using the HTTP protocol. The program that you write in this assignment will need to read that HTTP request and send an HTTP reply back to the web browser.

When your server program is launched from the command line, open a socket and wait for a connection from the browser. When a browser connects, accept that connection, and start reading on the socket. You will need to learn to parse HTTP request. Once you determine the file the web browser is requesting, your web server should open the file, and send the content of that file using HTTP back to the browser. Depending on the type of the file, you will also need to set the Content-Type field appropriately. Otherwise, it will not display properly in the browser. It is helpful for debugging if you printf all the inbound and outbound messages.

Once you have sent the content of the file back to the web browser, please close the connection. At this point, your server should start waiting for a new connection from the browser. It is ok for this assignment if your server cannot process new requests before completing the processing of the previous request. We will rectify that in the next assignment.

You are not allowed to use any http/curl/etc. libraries in this assignment.

Testing your program

We will copy your http server to the testfiles folder created by unzipping this file: testfiles.tar.gz. We will launch your server and use a web client to request those files using your web server. We will then compute the difference between the original file and the file downloaded by our web client through your server. In addition to the 9 test cases in the file, we will do one more test: request for a file that does not exist.

We will also do visual test. We will run your server on bayou, use at least two different browsers to open several URLs and make sure the files appear properly on the browsers. This means, you have to properly tag the content type in HTTP header.

It is not necessary for your program to work with redirects, persistent connections, POST etc. If it can serve static files using HTTP 1.0 GET, that is good enough.

Compiling and Running

It is perfectly fine for you to work on your own Linux machine. Regardless of where you do your assignment, your assignment must compile and execute on bayou.


Please provide answers to these questions in the README file.
  1. What is the difference between http_server (what you wrote) and Apache?
  2. Some web sites allow only certain browsers (e.g., IE) to download content from them. How can you write http_server to support this feature? Be specific about where/what code is needed.


Put your source code into a folder with the name: uhid_hw3, where uhid is the prefix of your email address. There should be a single Makefile in that directory. When we run make on that directory, it should produce one executable http_server. Put a README.txt in the directory describing anything unusual (e.g., limitations) of your implementation and answer to the questions above. Then, zip the directory and upload the zip file using Blackboard.