HTTP Range in 2014: resuming an interrupted download from the right version
Use Range, If-Range, and Content-Range to verify that a partially downloaded file is completed with the right bytes.
Archive retrospective · Topic year 2014
In this article
Resuming is more than joining two files
A field team downloads a maintenance manual onto tablets that sometimes lose a connection. The client can preserve the downloaded prefix. If the server replaces the manual before the next request, joining the old file's prefix and the new file's suffix can produce invalid output even when both versions have the same size. RFC 7233, issued in June 2014, documented HTTP/1.1 range requests and response combination. It is a period reference rather than the invention date for range requests. We'll use a synthetic ten-byte file with version tags. First check where reliable local data stopped. Then check that the response describes bytes from the same representation before marking the download complete. Progress reflects confirmed data rather than the design of the progress bar.
An offset belongs to the selected representation
The request bytes=4- starts at position four in the zero-based representation. The four saved bytes occupy positions zero through three. Content-Range in a partial response describes the returned positions and the complete representation length in this exercise. Byte offsets do not measure characters. UTF-8 text can use multiple bytes for one character, and content coding can produce a different sequence entirely. The lab requests identity coding consistently. Check whether the HTTP library transforms data before you put it on disk. Preserve representation selection, the strong ETag, and the persisted length. If the staging file's length no longer matches what was recorded, determine the last verified boundary before issuing another range request. A reported position is not automatically a durable position.
Send the condition with If-Range
The client puts its saved strong entity tag in If-Range. If it still matches, the server can return the requested interval. If the representation changed, the server can ignore the Range header and send a new complete response instead. Weak tags are not valid here. In the wire example, the first block is the request and the second is the expected 206 response. Six bytes make up the inclusive interval from four through nine. Repeat with a changed server version and check that a 200 response starts a separate replacement file. Do not append a full response to the old prefix. Keeping the previous working file until the new transfer has been validated gives the reader a usable copy during the attempt.
GET /manual.txt HTTP/1.1
Host: example.test
Accept-Encoding: identity
Range: bytes=4-
If-Range: "manual-a"
HTTP/1.1 206 Partial Content
ETag: "manual-a"
Content-Range: bytes 4-9/10
Content-Length: 6
456789
Test the join using known bytes
The fixture verifies a single final segment. A valid prefix is 0123, and the response body is 456789. The strong tag must match. The segment begins at the actual prefix length and ends one position before the complete length. Its body must contain the number of bytes described by its two endpoints. Handle status 206 in the calling code before running this check. Change the starting offset, tag, or body length and assert that each malformed input is rejected. A production downloader would use a staging file and avoid building a large concatenation in memory. It would also need to handle or explicitly decline forms outside this fixture, such as multipart range responses. Validate a complete item before exposing it.
import re
def assemble_suffix(prefix, suffix, content_range, got_tag, saved_tag):
match = re.fullmatch(r"bytes ([0-9]+)-([0-9]+)/([0-9]+)", content_range)
if match is None or got_tag != saved_tag or len(saved_tag) < 2 or not (saved_tag.startswith('"') and saved_tag.endswith('"')):
raise ValueError("unexpected response metadata")
start, end, total = map(int, match.groups())
if not (start == len(prefix) and start <= end == total - 1
and len(suffix) == end - start + 1):
raise ValueError("unexpected byte interval")
return prefix + suffix
assert assemble_suffix(b"0123", b"456789", "bytes 4-9/10",
'"manual-a"', '"manual-a"') == b"0123456789"
Give 200, 206, and 416 distinct outcomes
A 200 status may mean that the server disregarded the range and returned a full representation. Treat it as a replacement transfer. A 206 can supply a suffix if its metadata checks out. A 416 reports an unsatisfiable range and may return a header such as bytes */10. Local size ten then tells you the length agrees. It does not prove that the local bytes are the right version. Verify identity and integrity before accepting an apparently full file, or fetch the current metadata and choose a full transfer. Set a retry budget so unsupported requests or intermittent failures do not loop forever. Update progress from the last verified byte count rather than every partly received body.
Exercise both the fixture and the delivery path
Start with the in-memory ten-byte fixture. In a controlled server test compare a complete transfer, a verified continuation after four bytes, and a replacement after a version update. Confirm that a failure preserves the last working file. Try the same service through its proxy or CDN if that path can affect representation selection. A configured integrity digest can provide an additional final check on the complete output. Only publish the staging file after the applicable checks finish. No production download is needed for the small example. Use the consolidated range rules in RFC 9110 when evaluating a modern deployment. The durable rule from this period is that an offset needs both a representation identity and a validated range to mean anything.
Reader discussion
What is the most important unanswered question about this topic?
No comments yet. Be the first to contribute.
Write a comment