In this chapter, we are going to implement an HTTP web server that can serve static files from a local directory. HTTP is a text-based client-server protocol that uses the Transmission Control Protocol (TCP).
When implementing our HTTP server, we need to support multiple, simultaneous connections from many clients at once. Each received HTTP Request needs to be parsed, and our server needs to reply with the proper HTTP Response. This HTTP Response should include the requested file if possible.
Consider the HTTP transaction illustrated in the following diagram:
In the preceding diagram, the client is requesting
/document.htm from the server. The server finds
/document.htm and returns it to the client.
Our HTTP server is somewhat simplified, and we only need to look at the first line of the HTTP request. This first line is called the request line. Our server only supports
GET type requests, so it needs to first check that the request line starts with
GET. It then parses out the...