HyperText Transfer Protocol (HTTP) is an application-layer protocol for transmitting hypermedia documents, such as HTML. It forms the foundation of data communication on the World Wide Web and is designed for communication between web browsers and web servers.
HTTP follows a client-server model where:
- Client (browser) sends a request to the server
- Server processes the request and sends back a response
- Connection may be closed or kept alive for subsequent requests
HTTP is stateless by default, meaning each request is independent. State can be maintained through cookies, sessions, or tokens.
sequenceDiagram
participant Client
participant Server
Client->>Server: TCP Handshake
Client->>Server: HTTP Request (GET /index.html)
Server->>Client: HTTP Response (200 OK + HTML)
Client->>Server: HTTP Request (GET /style.css)
Server->>Client: HTTP Response (200 OK + CSS)
Note over Client,Server: Connection may close or stay open
| Method | Purpose | Safe | Idempotent | Cacheable |
|---|---|---|---|---|
| GET | Retrieve resource | Yes | Yes | Yes |
| POST | Create resource | No | No | Yes (if response) |
| PUT | Update/replace resource | No | Yes | No |
| DELETE | Remove resource | No | Yes | No |
| PATCH | Partial update | No | No | No |
| HEAD | Get headers only | Yes | Yes | Yes |
| OPTIONS | Describe communication options | Yes | Yes | No |
| CONNECT | Establish tunnel | No | No | No |
| TRACE | Echo request | Yes | Yes | No |
| Code Range | Category | Examples |
|---|---|---|
| 1xx | Informational | 100 Continue, 101 Switching Protocols |
| 2xx | Successful | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirection | 301 Moved Permanently, 302 Found, 304 Not Modified |
| 4xx | Client Error | 400 Bad Request, 401 Unauthorized, 404 Not Found |
| 5xx | Server Error | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable |
Headers provide metadata about requests and responses:
- Host: Domain name of server
- User-Agent: Client application info
- Accept: Acceptable content types
- Authorization: Authentication credentials
- Cookie: Stored cookies
- Content-Type: MIME type of response body
- Content-Length: Size of response body
- Set-Cookie: Cookies to store on client
- Cache-Control: Caching directives
- Location: Redirect URL
- One request per connection
- No persistent connections
- Simple but inefficient
- Persistent connections (keep-alive)
- Pipelining support
- Host header required
- Chunked transfer encoding
- Binary protocol instead of text
- Multiplexing (multiple requests over single connection)
- Header compression (HPACK)
- Server push
- Prioritization
- Based on QUIC instead of TCP
- Built-in encryption
- Faster connection establishment
- Better congestion control
HTTPS (HTTP Secure) is HTTP over TLS/SSL. It provides encrypted communication and authentication between client and server.
HTTPS addresses HTTP's security limitations:
- Confidentiality: Encrypts data in transit
- Integrity: Prevents data tampering
- Authentication: Verifies server identity
- Trust: Builds user confidence
- SEO Benefits: Search engines favor HTTPS sites
HTTPS adds TLS encryption layer:
- TCP Handshake: Establish connection
- TLS Handshake: Negotiate encryption, exchange keys
- HTTP Communication: Encrypted over TLS
- Connection Closure: Clean shutdown
sequenceDiagram
participant Client
participant Server
Client->>Server: Client Hello (TLS version, cipher suites)
Server->>Client: Server Hello (chosen cipher, certificate)
Client->>Server: Key Exchange
Server->>Client: Finished
Client->>Server: Finished
Note over Client,Server: Encrypted HTTP traffic