A minimal HTTP/1.1 server implementation in Go without using any HTTP libraries. Built to understand HTTP at the byte level.
Raw TCP server that speaks HTTP. No net/http, no frameworks. Just sockets and bytes.
go run main.goServer listens on localhost:8080.
curl http://localhost:8080
# Returns: Hello, World!listener, err := net.Listen("tcp", ":8080")Binds to port 8080. OS queues incoming connections.
conn, err := listener.Accept()Blocks until client connects. TCP 3-way handshake (SYN, SYN-ACK, ACK) happens here.
buffer := make([]byte, 1024)
n, _ := conn.Read(buffer)HTTP request arrives as plain text:
GET / HTTP/1.1\r\n
Host: localhost:8080\r\n
User-Agent: curl/8.15.0\r\n
\r\n
response := "HTTP/1.1 200 OK\r\n" +
"Content-Length: 13\r\n" +
"\r\n" +
"Hello, World!"
conn.Write([]byte(response))METHOD PATH VERSION\r\n
Header-Name: Header-Value\r\n
Another-Header: Value\r\n
\r\n
[optional body]
VERSION STATUS_CODE STATUS_TEXT\r\n
Header-Name: Header-Value\r\n
\r\n
[body]
- Every line ends with
\r\n(CRLF) - Empty line (
\r\n) separates headers from body Content-Lengthheader specifies body size in bytes- HTTP is text-based protocol over TCP