Building HTTP server (from scratch)

Saurav Kumar
3 min readDec 19, 2021

--

HTTP which stands for HyperText Transfer Protocol is a wrapper around TCP that is used to share information on the internet using a simple client-server architecture.

In this article, I will show how to build an HTTP server from scratch using TCP socket programming. It is completely for learning purposes for someone who is curious to know the internals of HTTP servers. So let’s get started!.

So how does HTTP make use of TCP?

It’s simple:

  • Browser establish a TCP connection with the server host IP and Port.
  • Send the HTTP request message(a human-readable format defined in RFC2616).
  • Read the response message coming back from the server.

Let’s get a rough knowledge of the above steps before getting started with code.

Creating TCP connection:

The server listens on a port for TCP connection which will be used by the server program to understand the HTTP message.

Sending HTTP message:

Basically a set of rules which is standardized to enable communication between servers and clients.
There are two types of HTTP messages:

  • Request message
  • Response message

Syntax of HTTP Request message:

Syntax of HTTP Response message:

More about the above here.

Building Server

Now comes the final part, building a server with the following action items:

  • Listen for TCP connection on a port (by default 80 is used as TCP port for HTTP communication).
  • Wait for an incoming message in an established connection.
  • Understand the HTTP request message.
  • Send back a response in HTTP response format.
  • The server will have one GET and POST route each.

NOTE: We will use the browser as a client to make sure the HTTP standards are followed.

GET endpoint `/`

After a client is connected, read the input until we get a special character CLRF \r\n\r\n .

Sample response:

GET / HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua-platform: "macOS"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
Accept: text/html
Accept-Language: en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7

In the next step:

  • Parse the above data i.e extract the endpoint, headers, and GET params if any.
  • Frame the message in response format and push data to the socket’s output stream.

POST endpoint `/`

POST requests are a little complicated than GETrequests. Here we rely on the header Content-length which indicates the number of bytes the server has to read from the body.

The above code is accepting the request body to be of POST type and sends back the POST body received back to the client. Main logic resides inprocessData .

The above function accepts a String (an HTTP POST request) and performs the following:

  • Get number of byte through header Content-Length .
  • Check if the body is of the size mentioned in the above step. If yes, stop reading the input stream and return the body of request, else continue.

NOTE: Above codes are just a bare minimum request handler. It lacks error handling, identifying routes and headers, etc.

Thanks for reading:) — owner of noob server

--

--