HTTP is a text conversation in which a client sends a request line, headers and an optional body, and a server answers with a status line, headers and a body, over a TCP connection to port 80. A request for a page needs only a method, a path and a Host header, and the server’s Content-Length must equal the byte count of the body: the 31-byte body of a one-line page needs Content-Length: 31. Status codes are grouped by first digit, so 2xx means success, 3xx a redirect, 4xx the client’s mistake and 5xx the server’s.

Part of Networking

The request and response format of the web protocol, read byte by byte, with the headers and status codes a settlement’s web server depends on.

A Web Request Is A Few Lines Of Text You Can Type By Hand

Protocols gives the outline of HTTP: a request with a method (GET, POST, PUT, DELETE), a URL and headers; a response with a status code, headers and optionally a body; no memory of earlier requests; and a text format that makes it easy to inspect, so that a minimal server takes a few hundred lines of code. This article is the format itself, because knowing it lets you diagnose a web server with nothing but a terminal and a TCP connection (TCP Protocol). Port 80 for HTTP and 443 for HTTPS come from Network Addressing.

A request:

GET /index.html HTTP/1.1
Host: library.settlement.internal

A response:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 31

<html><body>Hello</body></html>

The blank line ends the headers. Each line ends with a carriage return and a line feed (general knowledge, not from the other articles). The Host header says which name the client used, so one server on one address can serve many names; without it the server cannot tell the library from the council. The status line carries the code and a short phrase.

What You Need

  • A web server running (Web Server)
  • A tool that opens a TCP connection and lets you type — a netcat-style program or a telnet client (general knowledge)
  • The server’s name or address

Method: Make A Request By Hand

Step 1: Connect to port 80 If the connection is refused, nothing is listening (TCP Protocol). If it hangs, the path is the problem.

Step 2: Type the request line and the Host header, then a blank line Use the exact format above.

Step 3: Read the status line first A code of the form 2xx is success. The usual ones, which are general knowledge: 200 OK; 301 or 302 for a redirect; 404 for a path that does not exist; 500 for a fault in the server.

Step 4: Read the headers Content-Type says what the body is, Content-Length says how many bytes follow.

Step 5: Compare the byte count to Content-Length A mismatch makes a browser wait for bytes that never arrive or cut the page off.

Worked Example: Counting Content-Length

The body <html><body>Hello</body></html> is five pieces: <html> is 6 characters, <body> is 6, Hello is 5, </body> is 7 and </html> is 7. Total: 6 + 6 + 5 + 7 + 7 = 31 bytes, so the header is Content-Length: 31. A program confirmed the count.

Content-Length counts bytes, not characters. The body <p>é</p> is 8 characters, but in the common UTF-8 encoding (general knowledge) the letter é takes 2 bytes, so the body is 3 + 2 + 4 = 9 bytes and the header must say 9. A page with accented letters and a character count in the header is cut off by one byte per accented letter.

Status Codes Tell You Whose Fault It Is

CodeMeaningWhere to look
2xxsuccessnowhere
3xxgo elsewherethe Location header; a redirect loop means two rules point at each other
4xxthe request was wrongthe path, the permissions on the file, the Host name
5xxthe server failedthe server’s error log

Common Mistakes

  • Leaving out the Host header — on a server with several names the answer is the wrong site or an error
  • Hand-writing a Content-Length for text with accents — count bytes
  • Treating a 404 as a network fault — the server answered; the file or path is the problem, so the network is working
  • Forgetting that HTTP is stateless — a login must carry a token on every request (Protocols)
  • Sending anything private over plain HTTP — everyone on a shared segment can read it; see Encryption Basics

What’s Next

  • Learn next: Web Server — running a server that answers these requests
  • Combine with: DNS — how the name in the Host header was found