HTTP 100 Continue

Overview

The HTTP 100 Continue status code is an informational response used in the HTTP protocol. It is part of the HTTP/1.1 specification and primarily serves as a mechanism to optimize network usage. This status code is sent from the server to the client to indicate that the initial part of the request has been received and understood, and that the client should continue with the rest of the request.

Purpose

The primary purpose of the HTTP 100 Continue response is to improve the efficiency of network communication. When sending large requests (like file uploads), a client can include an Expect: 100-continue header in its initial request. This header requests the server to confirm whether it can handle the request before the client sends the entire request body. If the server is able to handle the request, it responds with 100 Continue, allowing the client to proceed with sending the request body. If not, the server responds with an appropriate error status code, preventing unnecessary data transmission.

Usage

Client Behavior:

  1. Initial Request: The client sends a part of the request headers, including the Expect: 100-continue header.
  2. Awaiting Response: The client waits for a response from the server before sending the body of the request.
  3. Sending Body: Upon receiving the 100 Continue response, the client continues with sending the request body.

Server Behavior:

  1. Receiving Headers: The server receives the initial request headers.
  2. Evaluating: The server evaluates if it can accept the full request (e.g., based on authentication, validation of headers, or resource availability).
  3. Responding: The server sends a 100 Continue status if it’s ready to receive the request body. Otherwise, it sends an error status code (e.g., 401 Unauthorized or 403 Forbidden).

Scenarios

  • Large File Uploads: When uploading large files, using 100 Continue can prevent the client from sending large amounts of data over the network if the server is not prepared or able to handle the request.
  • Resource-Intensive Requests: For requests that may require significant server resources, the 100 Continue mechanism allows the server to do preliminary checks and respond appropriately before committing to handling the full request.

Considerations

  • Optional Use: The use of the 100 Continue status is optional. Clients are not required to wait for a 100 Continue response before sending the request body unless they have sent an Expect: 100-continue header.
  • Timeouts: Clients should implement a timeout when waiting for the 100 Continue response. If the server does not respond within a reasonable timeframe, the client may proceed to send the request body or abort the request.
  • Compatibility: Some HTTP/1.0 servers do not support the 100 Continue mechanism. In such cases, clients should be prepared to handle the lack of a 100 Continue response appropriately.

Sequence Diagram

sequenceDiagram
    participant Chrome as Chrome Browser
    participant Server as orhandogan.net Server

    Note over Chrome: Step 1: Chrome sends a POST request
    Chrome->>Server: POST /path HTTP/1.1 (Step 1)
    Chrome->>Server: Headers (Including Expect: 100-continue) (Step 2)
    
    Note over Server: Step 3: Server receives the request
    Server->>Chrome: HTTP/1.1 100 Continue (Step 3)

    Note over Chrome: Step 4: Chrome sends the request body
    Chrome->>Server: Request Body (Data) (Step 4)

    Note over Server: Step 5: Server processes the request
    Server->>Chrome: HTTP/1.1 200 OK (Step 5)
    Server->>Chrome: Response Body (Data) (Step 6)

Curl Request and Response Example

# Sample curl request with Expect: 100-continue header
curl -i -X POST -H "Expect: 100-continue" -d @data.txt http://example.com/resource

# Expected response
HTTP/1.1 100 Continue

PHP cURL Request and Response Example

In PHP, you can use cURL to make a request with an Expect: 100-continue header. This is particularly useful when sending large amounts of data, as it allows the server to respond with a 100 Continue status before the full request body is sent.

<?php
// Initialize cURL session
$ch = curl_init('http://orhandogan.ne/learnings/http-status-codes/resource');

// Specify the POST data
$postData = ['key' => 'value']; // Replace with your data
$postDataString = http_build_query($postData);

// Set cURL options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataString);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: 100-continue'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);

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

// Check for HTTP 100 Continue response
if (strpos($response, "HTTP/1.1 100 Continue") !== false) {
    echo "Received HTTP 100 Continue response.\n";
}

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

Python Request and Response Example

In Python, you can use the requests library to make a request with an Expect: 100-continue header. This is useful when dealing with large payloads, as it allows the server to respond with a 100 Continue status before receiving the full request body.

import requests

url = 'http://orhandogan.net/learnings/http-status-codes/'
headers = {'Expect': '100-continue'}
data = {'key': 'value'}  # Replace with your data

response = requests.post(url, headers=headers, data=data)

# Check for HTTP 100 Continue response
if response.status_code == 100:
    print("Received HTTP 100 Continue response")
else:
    print("Response status code:", response.status_code)

Apache Configuration for HTTP 100 Continue

Apache HTTP Server generally handles the Expect: 100-continue header automatically. However, if you need to adjust its behavior, such as for specific reverse proxy scenarios or performance tuning, you can do so using the server’s configuration files.

Below is an example of how you might configure Apache to handle the Expect: 100-continue header:

# Apache Configuration Example
<IfModule mod_headers.c>
    # To unset the Expect header and disable the automatic 100 Continue response:
    RequestHeader unset Expect early
</IfModule>

NGINX Configuration for HTTP 100 Continue

NGINX, by default, handles the Expect: 100-continue header, which is part of the HTTP/1.1 protocol. However, in certain configurations, especially when NGINX is used as a reverse proxy, you might need to ensure that the Expect header is properly managed.

Example of NGINX Configuration for Expect: 100-continue

The following is a sample NGINX configuration that demonstrates how to handle Expect: 100-continue headers, particularly in a reverse proxy setup:

# NGINX Configuration Example
server {
    listen 80;
    server_name yourdomain.com;

    # Forward all headers to the proxied server
    proxy_pass_request_headers on;

    location / {
        proxy_pass http://backend_server;
        # Additional proxy settings as needed
        # ...
    }
}

HTTP Status Codes HTTP 101 Switching Protocols


 

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.