In this assignment, you will write a simple web server that implements a subset of HTTP syntax. After solving this assignment, you should (1) be able to write a simple socket program, and (2) have a preliminary understanding of HTTP.
1. Handling a single HTTP request
The only HTTP request method that your web server needs to support is HEAD (which returns HTTP response header only). You may assume that there is a file called "index.html" (yes, this file name should be hard coded in your program) in the current working directory of your Web server. If a HTTP request queries about this file, your web server shall send an HTTP response that starts with status line 200 OK. Otherwise if an HTTP request aims at any other file, your web server shall instead return a HTTP response that starts with status line 404 Not Found. Your program does not need to consider other cases (e.g. requested file is in a subfolder or HTTP request is mal-formatted, etc.).
For example, if an HTTP request is:
HEAD /index.html HTTP/1.1\r\n
Host: www.example.org\r\n
Connection: keep-alive\r\n
\r\n
Your web server should send the corresponding HTTP response as follows:
200 OK\r\n
Date: Wed, 23 Jan 2019 13:11:15 GMT\r\n
Content-Length: 606\r\n
Content-Type: text/html\r\n
\r\n
2. Supporting HTTP/1.0
In HTTP/1.0, your web server shall read a HTTP request, send corresponding HTTP response and then close the TCP connection. Your web server may continue running and accepting new connections after that.
3. Supporting HTTP/1.1
In HTTP/1.1, your web server shall be able to read multiple sequential HTTP requests over one TCP connection and send corresponding HTTP responses in the same order. Your web server should keep the TCP connection open forever.