HTTP 101 Switching Protocols

Overview

HTTP 101 Switching Protocols status code is sent in response to a client’s request to change the application protocol. It is primarily used for upgrading from HTTP to a different protocol, like WebSockets.

Purpose

The main purpose of HTTP 101 is to allow efficient switching to more suitable protocols, such as WebSockets for full-duplex communication. This is particularly useful in real-time applications where continuous data exchange occurs.

Usage

Client Behavior:

  1. Initial Request: The client sends a request to upgrade the protocol, including the Upgrade header.
  2. Awaiting Response: The client waits for the server’s response to confirm the protocol switch.

Server Behavior:

  1. Receiving Headers: The server receives the upgrade request.
  2. Evaluating: The server evaluates if it can switch to the requested protocol.
  3. Responding: The server sends a 101 Switching Protocols response if it agrees to the protocol change.

Scenarios

  • WebSocket Upgrade: Commonly used for upgrading an HTTP connection to a WebSocket connection.
  • Real-Time Communication: Essential in applications requiring real-time data flow, like chat applications or live data feeds.

Sequence Diagram

sequenceDiagram
    participant Client
    participant Server

    Note over Client: Step 1: Client sends a request to upgrade protocol
    Client->>Server: GET /socket HTTP/1.1 (Upgrade: websocket) (Step 1)
    Client->>Server: Headers (Connection: Upgrade, Upgrade: WebSocket) (Step 2)
    
    Note over Server: Step 3: Server agrees to protocol switch
    Server->>Client: HTTP/1.1 101 Switching Protocols (Step 3)

    Note over Client,Server: Step 4: WebSocket connection established
    Client->>Server: WebSocket Data (Step 4)
    Server->>Client: WebSocket Data (Step 5)

Curl Request and Response Example for HTTP 101

When a client wishes to switch protocols, it can send a request with appropriate headers to indicate the desired protocol. Here’s how you can use Curl to request a protocol upgrade, such as to WebSockets:

# Sample curl request to upgrade to WebSocket
curl -i -H "Upgrade: websocket" -H "Connection: Upgrade" http://example.com/path

# Expected response if the server agrees to switch protocols
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade

PHP cURL Request and Response Example for HTTP 101

In PHP, you can use cURL to make a request that includes headers to request a protocol upgrade. This is especially relevant for situations like upgrading an HTTP connection to a WebSocket.

<?php
// Initialize cURL session
$ch = curl_init('http://example.com/path');

// Set cURL options for protocol upgrade
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Upgrade: websocket', 'Connection: Upgrade'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session
$response = curl_exec($ch);

// Check for HTTP 101 Switching Protocols response
if (strpos($response, "HTTP/1.1 101 Switching Protocols") !== false) {
    echo "Protocol successfully upgraded.\n";
} else {
    echo "Failed to upgrade protocol.\n";
}

// Close cURL session
curl_close($ch);
?>

Apache Configuration for HTTP 101 Switching Protocols

Apache does not directly handle WebSocket connections, but it can be configured to proxy these connections to a WebSocket server. The mod_proxy and mod_proxy_wstunnel modules are used for this purpose. Here is an example of how you can configure Apache to handle WebSocket connections:

# Apache Configuration Example for WebSocket Proxy
<VirtualHost *:80>
    ServerName example.com

    # Ensure mod_proxy and mod_proxy_wstunnel are enabled
    ProxyRequests Off
    ProxyPass "/ws/" "ws://websocketserver.example.com/"
    ProxyPassReverse "/ws/" "ws://websocketserver.example.com/"

    # Other configurations...
</VirtualHost>

NGINX Configuration for HTTP 101 Switching Protocols

NGINX can be configured to handle WebSocket upgrades using the HTTP 101 Switching Protocols status code. To set up NGINX as a reverse proxy for WebSocket connections, you need to include specific directives in your NGINX configuration file. Here’s an example:

# NGINX Configuration Example for WebSocket Upgrade
server {
    listen 80;
    server_name example.com;

    location /ws/ {
        proxy_pass http://websocket_backend;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Additional proxy settings...
    }
}

HTTP 100 Continue HTTP 102 Processing (WebDAV)


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.