HTTP/HTTPS

Overview

HTTP (Hypertext Transfer Protocol) is the foundation of data exchange on the World Wide Web. HTTPS is the secure version of HTTP, which encrypts the communication between the client and the server using TLS/SSL.

Core Concepts

  • Request/Response Cycle:
    • Request: Sent by the client, containing a method (GET, POST, etc.), a URI, headers, and an optional body.
    • Response: Sent by the server, containing a status code (200 OK, 404 Not Found, etc.), headers, and a body.
  • HTTP Methods:
    • GET: Retrieve a resource.
    • POST: Create a resource.
    • PUT: Replace a resource.
    • PATCH: Partially update a resource.
    • DELETE: Remove a resource.
  • TLS/SSL Handshake:
    • The process by which the client and server agree on encryption keys and verify the server’s identity using certificates.
  • HTTP Versions:
    • HTTP/1.1: Persistent connections, but suffers from head-of-line blocking.
    • HTTP/2: Multiplexing (multiple requests over one connection), header compression (HPACK), and server push.
    • HTTP/3: Uses QUIC (based on UDP) to eliminate head-of-line blocking at the transport layer.

Code Examples

GET /index.html HTTP/1.1
Host: example.com
Accept: text/html
User-Agent: Mozilla/5.0

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

<html><body>Hello World!</body></html>

Use Cases

  • Web Pages: Delivering HTML, CSS, and JS to browsers.
  • REST APIs: Exchanging JSON data between clients and servers.
  • Webhooks: Servers pushing real-time data to other servers.

Gotchas

  • Caching: Incorrectly configured Cache-Control headers can lead to users seeing stale content or servers being overwhelmed by requests.
  • CORS (Cross-Origin Resource Sharing): A security feature that prevents a web page from making requests to a different domain unless explicitly allowed by the server.

Related Notes