Click to play video
HTTP/1.1 is a text based protocol that works over TCP.
Hey, kinda like our tcplistener code...
HTTP works because plain text is binary. Because HTTP uses TCP, if the HTTP request or response is too big to fit into a single TCP packet it can be broken up into many packets and reconstructed in the correct order on the other side. TCP guarantees that the data is in order and complete.
At the heart of HTTP is the HTTP-message: the format that the text in an HTTP request or response must use. From RFC 9112 Section 2.1:
start-line CRLF
*( field-line CRLF )
CRLF
[ message-body ]
CRLF (written in plain text as \r\n) is a carriage return followed by a line feed. It's a Microsoft Windows (and HTTP) style newline character.
I call \r\n "Registered Nurse"... it helps me remember the order of the characters
Let's break down each part:
| Part | Example | Description |
|---|---|---|
start-line CRLF |
POST /users/primeagen HTTP/1.1 |
The request (for a request) or status (for a response) line |
*( field-line CRLF ) |
Host: google.com |
Zero or more lines of HTTP headers. These are key-value pairs. |
CRLF |
A blank line that separates the headers from the body. | |
[ message-body ] |
{"name": "TheHTTPagen"} |
The body of the message. This is optional. |
Both HTTP requests and responses follow this same format, though the contents of each section will differ, we'll get to that!
go run ./cmd/tcplistener | tee /tmp/rawget.http
curl http://localhost:42069/coffee
Your curl command will hang (because your TCP listeners just listens, it doesn't respond). But you should see the request come in on your tcplistener terminal!
Run and submit the CLI tests.