NOTE:

Do not read it, Ai generate slop. Just add for testing links working or not, will update the actual things i did later.

Implementing HTTP/1.1 from First Principles in Go

In the Go ecosystem, writing a web server is almost too easy.

http.ListenAndServe(":8080", nil)

One line of code. Under the hood, this function summons a legion of complexity: TCP handshakes, socket syscalls, header parsing, and goroutine management.

But to truly understand how the web works, we need to throw away net/http. We need to ignore the libraries and look at the raw reality of the protocol.

What is HTTP, really? It’s just text sent over a telephone wire.

Here is how I implemented a basic HTTP/1.1 server from scratch, using nothing but raw TCP sockets.


1. The Physics: It's Just a Socket

At the most fundamental level, HTTP relies on TCP (Transmission Control Protocol). TCP guarantees that if I send a stream of bytes, they arrive in order.

Our first step isn't web logic; it's networking logic. We need to open a port and listen for a connection.

In Go, we use the net package (not net/http).

listener, _ := net.Listen("tcp", ":8080")

for {
    conn, _ := listener.Accept()
    // In HTTP 1.1, we want to handle multiple requests per connection
    go handleConnection(conn)
}

The First Principle: A web server is just a loop that accepts phone calls (TCP connections) and hands them off to a worker (Goroutine).

2. The Protocol: The Anatomy of a Request

When a browser connects to our TCP socket, it sends a blob of text. If we capture that text and print it, it looks like this:

GET /index.html HTTP/1.1\\\\r\\\\n
Host: localhost:8080\\\\r\\\\n
User-Agent: Mozilla/5.0...\\\\r\\\\n
Accept: */*\\\\r\\\\n
\\\\r\\\\n