Blog / Others/ A Practical Guide to HTTP 206 Partial Content and Range Requests

A Practical Guide to HTTP 206 Partial Content and Range Requests

HTTP 206 状态码详解:范围请求与断点续传实践指南

Understanding HTTP 206 Partial Content

HTTP status codes in the 2xx range indicate that a client's request was successfully received, understood, and processed by the server. While 200 OK is the most common success response, 206 Partial Content signifies that the server has successfully fulfilled a Range Request and is returning only the requested portion of the resource.

Key Applications of Range Requests

  • Resumable downloads and segmented file transfers.
  • Seeking and streaming video/audio content.
  • Multi-threaded download acceleration.
  • Network diagnostics and server configuration testing.
  • Learning and testing HTTP protocol details.

Checking Server Support

To verify if a server supports range requests, send a HEAD request and inspect the response headers. Use the curl -I command:

curl -I https://example.com/file.zip

Look for these headers:

  • Accept-Ranges: bytes – Indicates support for byte-range requests.
  • Accept-Ranges: none – Indicates no support.
  • Content-Length – The full size of the resource in bytes.

Sending a Range Request

Use the Range request header to specify the desired byte range. The format is Range: bytes=<start>-<end>. The <end> value is optional; if omitted, the server returns data from the start position to the end of the file.

Using curl

The curl command supports range requests via the -H or -r options.

# Method 1: Using the -H flag
curl -H "Range: bytes=0-1023" https://example.com/file.zip -o part1.zip

# Method 2: Using the -r flag (shorthand)
curl -r 0-1023 https://example.com/file.zip -o part1.zip

# Download and combine a second segment
curl -r 1024-2047 https://example.com/file.zip -o part2.zip
cat part1.zip part2.zip > full.zip

If the range is valid, the server responds with 206 Partial Content. An invalid range (e.g., beyond the file size) results in a 416 Range Not Satisfiable error.

Server-Side Configuration

Nginx

Nginx supports range requests for static files by default. Verify with curl -I.

Apache HTTP Server

Apache typically supports it by default. To explicitly set the header, use the mod_headers module:

# In httpd.conf or virtual host config
Header set Accept-Ranges bytes

Important Notes

  • Dynamically generated content (e.g., from PHP scripts) may not support range requests by default. Application code must handle the Range header and send the appropriate Content-Range response header.
  • Some CDNs or proxy servers may require specific configuration to support or pass through range requests.

Debugging with Browser DevTools

Modern browser Developer Tools (Network tab) allow you to inspect the Range request and Accept-Ranges response headers for any network request, making them invaluable for debugging.

Summary

The HTTP 206 status code and range request mechanism are fundamental for efficient large file transfers. By checking the Accept-Ranges header, a client can determine server support. Using the Range header, clients can retrieve specific parts of a resource, enabling resumable downloads, parallel fetching, and media streaming. Major web servers provide default support for static files.

Post a Comment

Your email will not be published. Required fields are marked with *.