Connections, ports and speaking HTTP by hand
An address reaches a computer and a port reaches one service on it. Once a connection is open, an HTTP/1.1 request is a few lines of text you can type yourself, and the response that comes back is text you can read line by line.
Addresses and ports
DNSThe Domain Name System, the internet's lookup service that turns a domain such as photos.example into the address of the computer serving it. Whoever controls a name's DNS records controls where that name points. turns a name such as example.com into an IP address,
and the address gets your message to the right computer. But one
computer can run many services at once: a web server, a mail server, a
database. Something has to say which of them a message is for.
That something is the port you met in URLs in Clients, servers, and addresses: a number from 0 to 65535. If the IP address is the address of a building, the port is the apartment number. A service listens on a port, which means it waits for anyone who wants to talk to it there. Common services use agreed numbers, called well-known ports, so a client knows where to knock without being told:
| Port | Usually used for |
|---|---|
22 | SSH, for signing in to another computer's terminal |
53 | DNS lookups |
80 | HTTP, the web without encryption |
443 | HTTPS, the web over an encrypted connection |
Web addresses carry the port too, even when you cannot see it. A URLA web address, such as https://photos.example/api/albums/42?limit=10. It names how to connect (https), the domain, the path to a particular page or resource, and any extra parameters after the question mark.
that starts with http:// means port 80 unless it says
otherwise, and https:// means port 443. To use any other
port, the URL names it after a colon, as in
http://localhost:8080/. The name localhost
always means "this computer", which makes it handy for trying things
out.
Opening a connection
Before a browser and a web server exchange a single word of HTTP, they open a connection using TCP, the Transmission Control ProtocolAn agreed set of rules for the messages two systems exchange and what each message means. HTTP, OAuth 2.0, OpenID Connect and SAML are all protocols.. Opening one takes three short messages. The client sends a SYN ("let's talk"), the server answers SYN-ACK ("agreed"), and the client confirms with ACK. After that, each side has a reliable two-way stream of bytes: whatever one side writes arrives at the other in the same order, and TCP quietly resends anything lost on the way. That setup is what DNS usually skips when it sends a single UDP message.
A connection has two ends, and each end has an address and a port. The server's port is the well-known one it listens on. The client's port is a temporary one that its operating system picks for this connection alone. Together, the two addresses and two ports identify the connection, which is how one server keeps a thousand visitors apart, and how your computer keeps its own connections to the same server apart. Newer versions of HTTP send many requests over one connection; this lesson sticks to one request per connection, which keeps the first experiments easy to follow.
You can watch this with curl, the program that sends web
requests for you. Here, -s keeps it quiet,
-o /dev/null throws the page away into the special file
that discards whatever is written to it, and -w asks curl
to print details about the connection when it finishes. Each
%{...} names one detail: the client's port, then the
server's address and port.
curl -s -o /dev/null -w '%{local_port} -> %{remote_ip}:%{remote_port}\n' http://example.com/Example output, with your own numbers in their place:
51544 -> 203.0.113.10:80Run it again and the server side stays the same while the client port changes, because every new connection gets a fresh one. On a network that uses IPv6, the server address appears in its longer IPv6 form.
Sometimes you only want to know whether anything is listening at all.
nc, short for netcat, opens TCP connections from the
terminal. With -z it connects and sends nothing, and with
-v it reports what happened:
nc -zv example.com 80
If a service is listening, nc reports that the connection succeeded. If
nothing listens on that port, the other computer refuses the connection
and nc says Connection refused. Some networks drop
unexpected connections silently instead, so nc waits for a reply that
never comes. It gives up eventually, or you can stop it with
Ctrl+C.
What HTTP looks like
Once the connection is open, the client and server need a shared language. For the web, that language is HTTP. In version 1.1, the one you can type yourself, a message is plain text arranged in lines. You read one in How applications communicate; this time you will send it. Here is a complete request:
GET /hello HTTP/1.1
Host: example.com
Connection: close
The first line is the request line. It holds three
things separated by spaces: the method (GET means "send
me this"), the target, which is the path and any
query from the URL, and the protocol version. Then come the headers,
one per line, each a name, a colon and a value. Header names ignore
letter case, so host and Host are the same
header. An empty line ends the headers. A request that sends data,
such as a form, puts it after that empty line as the body. This one
has no body.
Two of these headers matter for typing requests by hand. The
Host header names the site you want. One IP address often
serves hundreds of sites, so the server needs the name as well as the
address, and HTTP/1.1 requires every request to include it: a request
without one gets 400 Bad Request.
Connection: close asks the server to close the connection
after it answers. Without it, HTTP/1.1 keeps the connection open in
case another request follows, which saves time for browsers but leaves
you staring at a terminal that seems to hang.
The response has the same shape:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 13
Hello, world
Its first line is the status line: the version, a
three-digit status codeThe three-digit number at the start of an HTTP response that says how the request went, such as 200 for success, 302 for a redirect, or 401 when authentication is needed. and a short reason phrase. 200 OK
means the request worked, and the
status codes
you met in How applications communicate all arrive here. The headers
describe the body: Content-Type says what kind of data it
is, and Content-Length says how many bytes it has, so the
client knows where it ends. Here that is 13: the twelve characters of
Hello, world and the newline after them. Some servers send
Transfer-Encoding: chunked instead and deliver the body in
pieces, each preceded by its size in hexadecimal, so you may see short
numbers such as 1f around a body and a 0 at
the end.
Over HTTPSHTTP protected by TLS, so traffic is encrypted and checked for changes while it travels. It protects the connection, but does not prove that the site at the other end is honest., browsers and curl often use HTTP/2 instead, and you may see
a status line such as HTTP/2 200. HTTP/2 packs the same
methods, headers and bodies into a binary format so that many requests
can share one connection. That makes it hard to type by hand, but the
meaning is the same, so everything you learn from HTTP/1.1 carries
over.
Speaking HTTP by hand
Since a request is only text, you can write one yourself and send it down a connection. Two details make that work.
First, HTTP ends each line with two invisible characters, a carriage
return and a line feed, written \r\n and called
CRLF. The names come from typewriters, which returned
the carriage to the left and fed the paper up a line. The terminal ends
lines with a line feed alone. Many servers accept that, but not all, so
it is better to send exactly what the protocol asks for.
printf can do this: it turns \r and
\n into those characters and adds nothing you did not ask
for. Responses end their lines the same way, so a header line you copy
out of a response still carries a \r at its end, even
though the terminal shows nothing there.
Second, you need a program that connects and passes your text along.
Without -z, nc sends everything that arrives
on its standard input down the connection and prints everything that
comes back. A pipe joins the two:
printf 'GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' | nc example.com 80
The request ends with \r\n\r\n: one to finish the last
header and one for the empty line that ends the headers. Example
output, shortened:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 513
Connection: close
<!doctype html>
<html>
...
The server answered, sent the page and closed the connection, which
ended nc and brought back your prompt. Nothing about this
request was special: a browser sends the same kind of text, with more
headers.
nc can also play the server. With -l it listens on a port
instead of connecting, and prints whatever a client sends:
nc -l 8080
Leave that running, open a second terminal window, and point curl at
it with curl http://localhost:8080/hello. The first window
shows exactly what curl sent. Example output, where the version number
depends on your system:
GET /hello HTTP/1.1
Host: localhost:8080
User-Agent: curl/8.5.0
Accept: */*
curl added a User-Agent header that names the program and
an Accept header saying it takes any kind of response.
curl now waits for an answer that your listener will never send on its
own. You could type one, line by line, or press
Ctrl+C in either window to end the experiment.
Who can read it
The text you typed did not travel straight to the server. It passed through your Wi-Fi router, your internet provider and several other networks on the way, and the answer came back through them too. With plain HTTP, every one of those stops sees the exact text you saw: the path, every header and the body. Anyone who controls one of them can also change it, for example by swapping a download link or adding something to the page.
That matters most for the headers that prove who you are. A password,
a session cookieA small value that a website asks the browser to store and send back with later matching requests. Cookies commonly carry a session identifier, so the site can recognize an ongoing sign-in without your password. or an Authorization header holding a
tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. is just another line of text. Sent over plain HTTP, it is readable
by everyone on the path, and anyone who copies it can use it. Treat a
secret sent that way as exposed, and replace it.
HTTPS protects HTTP with TLS, the protected
connection from Clients, servers, and addresses.
The client and server set up TLS on the connection first, and only
then send the request through it. The server's certificateA signed statement that a public key belongs to a name, such as a website's hostname, for stated purposes and a limited period. A certificate authority signs it, so software that trusts the authority can connect the key to the name. shows that
it is the one you asked for, and TLS keeps everyone on the path from
reading or quietly changing what passes. The request inside is the
same HTTP you have just typed. openssl s_client, a TLS
client in the OpenSSL toolkit, lets you try this: it handles TLS, and
you supply the request:
printf 'GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' | openssl s_client -quiet -connect example.com:443 -servername example.com -verify_return_error -verify_hostname example.com
-connect names the host and port. -servername
tells the server which site you want, much as the Host
header does for HTTP, but it does not check the certificate.
-verify_hostname checks that the certificate names that
host, and -verify_return_error ends the connection if any
check fails. Without those two, s_client carries on
whatever it finds, so always include them before you send anything
secret. -quiet hides most of the connection details, and
the response comes back as the same readable HTTP as before, now
unreadable to everyone between you and the server.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Watch connections open, then type HTTP requests by handSee both ends of a TCP connection, wrap a typed HTTP request in a function, watch what curl sends, and finish with a request you typed yourself over TLS.