Separating partial and whole-file digests with HTTP RFC 9530
A field package download resumes with HTTP 206. The local test checks the received range with Content-Digest and the whole selected file with Repr-Digest.
Archive retrospective · Topic year 2024
In this article
Resuming a download creates two byte scopes
Consider a field device installing an offline map package over an intermittent connection. It writes the opening bytes to disk, loses connectivity and later requests the missing portion. A server that accepts the range can send a 206 Partial Content response. A digest of those bytes answers a different question from a digest of the complete selected file. RFC 9530, published in February 2024, defines fields for HTTP message content and for representation data. The fields have uses beyond range requests, but a range makes both scopes visible in one response. The local server uses a fixed readable sequence in place of a map and does not compress it. This lets you follow the byte boundaries without a registry, credentials or a third-party service.
Which bytes does the server hash?
Content-Digest is calculated over the actual message content. For the example's partial response, that is the bytes the server sends in the selected interval. Transport framing does not enter that calculation. Repr-Digest refers to the entire selected representation data. If a service negotiates a format or encoding, identify the representation before comparing its digest. Both fields use a Structured Fields dictionary. The test uses one sha-256 entry and a Base64-encoded byte sequence delimited by colons. Its request and response use an uncompressed fixture. In a deployment, verify what happens when a proxy or client library applies automatic decoding. The server's received or transmitted message content and the bytes exposed by an API may be different, depending on where the application performs the check.
Reproduce the local partial response
Run the Node.js example to start a loopback server on a free port. The client asks for bytes 2 through 5 of a ten-byte sequence. It checks the status, Content-Range header and body before verifying the digests. The partial content is CDEF. The content digest matches CDEF. The representation digest matches ABCDEFGHIJ and fails when checked against the partial content. The client also changes one byte and confirms that the content digest no longer matches. There are two matching comparisons and two deliberately failed ones. The test compares a narrow field serialization generated by its own server. A general HTTP client should parse the dictionary with a Structured Fields implementation and apply an algorithm policy. The local run is a focused demonstration of the digest boundary.
import assert from 'node:assert/strict';
import {createHash} from 'node:crypto';
import {createServer} from 'node:http';
import {once} from 'node:events';
const file = Buffer.from('ABCDEFGHIJ');
const part = file.subarray(2, 6);
const digest = bytes => 'sha-256=:' + createHash('sha256').update(bytes).digest('base64') + ':';
const server = createServer((request, response) => {
if (request.headers.range !== 'bytes=2-5') {
response.writeHead(416, {'Content-Range': 'bytes */' + file.length}).end();
return;
}
response.writeHead(206, {
'Content-Range': 'bytes 2-5/' + file.length,
'Content-Length': part.length,
'Content-Digest': digest(part),
'Repr-Digest': digest(file),
}).end(part);
});
server.listen(0,'127.0.0.1');
await once(server,'listening');
try {
const origin = 'http://127.0.0.1:' + server.address().port;
const response = await fetch(origin,{headers:{Range:'bytes=2-5'}});
assert.equal(response.status,206);
assert.equal(response.headers.get('content-range'),'bytes 2-5/10');
const body = Buffer.from(await response.arrayBuffer());
assert.equal(body.toString(),'CDEF');
const result = {
content: response.headers.get('content-digest') === digest(body),
representation: response.headers.get('repr-digest') === digest(file),
wholeComparedToPart: response.headers.get('repr-digest') === digest(body),
mutatedPart: response.headers.get('content-digest') === digest(Buffer.from('CDEZ')),
};
assert.deepEqual(result,{content:true,representation:true,wholeComparedToPart:false,mutatedPart:false});
console.log(JSON.stringify(result));
} finally {
await new Promise(resolve=>server.close(resolve));
}
Keep partial bytes tied to a version
Resuming based only on the length of a temporary file can combine an old download with bytes from a newer version. A client can persist a strong ETag and use it with If-Range on a later request. When the server sends an entirely new representation, the client needs to treat the response as a restart. When it returns a partial response, verify that the response range starts where expected. Save the most recent verified offset so interruptions are recoverable. Once the whole package is available, compare its digest before promoting the temporary file. Using a temporary path preserves the previous usable file through a failed download. On storage where durable writes matter, test file promotion and persistence separately, including interruption at the point where the file becomes visible.
What a matching digest establishes
A matching digest connects a set of received bytes to an expected value. An intermediary that can replace both can supply a matching pair of its own. Trust in the publisher comes from an authenticated channel or an independently verified signature. For a software distribution system, a signed manifest can bind the whole package digest to its version and name. A content digest can catch a damaged segment early and the whole package digest supports the installation decision. A dictionary may include more than one algorithm. Decide which algorithms your application accepts and what it does when there is no acceptable digest. If an installer requires integrity verification before accepting a package, an HTTP success status alone cannot authorize promotion.
Record the outcome in a resumable process
A small operation record might include the selected version or ETag, requested byte range, returned range, bytes received and results for both digest scopes. That record helps distinguish a network retry from a partial file that belongs to a different release. The application can show whether a package is still temporary or ready to use. Extend the test with a changed ETag, a wrong range, an empty artifact and an absent digest field. Preserve a test that compares the whole digest with only the range so a refactor cannot conflate the two scopes. RFC 9530 replaced the older Digest field with fields that make this distinction explicit. Upgrade the download client and test fixtures together so a stable status code does not hide a change in the bytes being verified.
Reader discussion
What is the most important unanswered question about this topic?
No comments yet. Be the first to contribute.
Write a comment