The HTTP server

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 requested resource, /document.htm in the preceding example.

A more full-featured HTTP server would look at several other HTTP headers. It would look at the Host header to determine which site it is hosting. Our server only supports hosting one site, so this header is not meaningful for us.

A production server would also look at headers such as Accept-Encoding and Accept-Language, which could inform a proper response format. Our server just ignores these, and it instead serves files in only the most straightforward way.

The internet can sometimes be a hostile environment. A production-grade web server needs to include security in layers. It should be absolutely meticulous about file access and resource allocation. In the interest of clear explanation and brevity, the server we develop in this chapter is not security-hardened, and it should not be used on the public internet for this reason.