The World Wide Web runs on HTTP, the protocol a browser uses to ask a server for pages, images and data. HTTPS is its secure form.
HTTP Basics
The HyperText Transfer Protocol (HTTP) is a request-response, client-server protocol. A client (usually a browser) sends a request for a resource, identified by a URL such as https://www.example.com/notes/cn.html (scheme, host, optional port, path and optional query), and the server sends back a response. HTTP runs over TCP port 80. It is stateless: each request is independent, and the server keeps no memory of earlier requests; cookies and sessions add state when needed, for logins and shopping carts.
Definition: HTTP. The HyperText Transfer Protocol, a stateless request-response application protocol that clients use to request resources such as web pages from servers.
Request and Response Structure
A request has a request line (method, path, version, such as GET /index.html HTTP/1.1), headers (Host, User-Agent, Accept, Cookie), a blank line and an optional body. The main methods are GET (fetch a resource), POST (send data, such as a form), PUT (replace), PATCH (modify), DELETE (remove) and HEAD (headers only). A response has a status line (version, status code and reason, such as HTTP/1.1 200 OK), headers (Content-Type, Content-Length, Set-Cookie, Cache-Control) and the body. Status codes are grouped as 1xx informational, 2xx success (200 OK, 201 Created), 3xx redirection (301 Moved Permanently, 304 Not Modified), 4xx client errors (400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found) and 5xx server errors (500 Internal Server Error, 503 Service Unavailable).
HTTP Versions
HTTP/1.0 opened a new TCP connection for every object. HTTP/1.1 added persistent connections (keep-alive), pipelining and the required Host header. HTTP/2 sends many requests at once over one connection (multiplexing), with binary framing and header compression. HTTP/3 runs over QUIC, on UDP, avoiding TCP's head-of-line blocking and making connection setup faster.
HTTPS
HTTPS is HTTP over TLS (Transport Layer Security), on port 443. TLS gives confidentiality (encryption), integrity and server authentication through a digital certificate signed by a trusted Certificate Authority. In the TLS handshake, the client and server agree on a version and cipher, the server presents its certificate, they establish a shared session key, and then all HTTP traffic is encrypted. Browsers show a padlock and warn about invalid certificates. Today almost all websites use HTTPS.
Table 4.1: Common HTTP status codes
| Code | Meaning | When it occurs |
|---|---|---|
| 200 | OK | Request succeeded |
| 301 | Moved Permanently | Resource has a new URL |
| 304 | Not Modified | Cached copy is still valid |
| 403 | Forbidden | Access is not allowed |
| 404 | Not Found | Resource does not exist |
| 500 | Internal Server Error | Server-side failure |
Example 4.1: Reading an HTTP exchange
Problem: A browser sends GET /notes.html HTTP/1.1 with header Host: localhost. The server replies HTTP/1.0 200 OK with Content-Type: text/html. Explain each part, and what a request for /missing.html would get.
Solution:
- GET is the method, /notes.html the path, HTTP/1.1 the version.
- Host names the server, allowing many sites on one IP address.
- 200 OK means success; Content-Type tells the browser to render HTML.
- A non-existent path returns 404 Not Found.
Result: The request line names what is wanted; the status code says what happened.
import http.server, threading, http.client
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
ok = self.path == "/notes.html"
self.send_response(200 if ok else 404)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(b"<h1>CN notes</h1>" if ok else b"")
def log_message(self, *a):
pass
srv = http.server.HTTPServer(("127.0.0.1", 0), H)
threading.Thread(target=srv.serve_forever, daemon=True).start()
c = http.client.HTTPConnection("127.0.0.1", srv.server_port)
for path in ["/notes.html", "/missing.html"]:
c.request("GET", path)
r = c.getresponse()
body = r.read()
print(f"GET {path:<14} -> {r.status} {r.reason}, "
f"{r.getheader('Content-Type')}, body {body!r}")
srv.shutdown()
Output
GET /notes.html -> 200 OK, text/html, body b'<h1>CN notes</h1>'
GET /missing.html -> 404 Not Found, text/html, body b''
Common mistakes
- Thinking HTTP remembers users between requests. It is stateless; cookies add state.
- Using GET to send passwords; the data appears in the URL. Use POST over HTTPS.
- Confusing 401 (not authenticated) with 403 (authenticated but not allowed).
Key takeaways
- HTTP is a stateless request-response protocol on port 80; HTTPS adds TLS on port 443.
- Requests have a method, path, version, headers and an optional body.
- Status codes: 2xx success, 3xx redirect, 4xx client error, 5xx server error.
- HTTP/1.1 keeps connections alive; HTTP/2 multiplexes; HTTP/3 uses QUIC.
Practice questions
- Which port does HTTPS use?
- What does status code 404 mean?
- Why is HTTP called stateless?
Answers to the practice questions
-
- The requested resource was not found.
- Each request is handled independently, without memory of earlier requests.
Sources
- Official BCA syllabus Computer Networks, course code 26BCA404DS02, Unit IV
