From 9248917100f843fca1a988696ee3ccdcd666466e Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 11 Sep 2024 00:35:31 +0200 Subject: [PATCH] fix: content length for byte range requests --- src/handlers/unixfs-file.js | 1 + test/handlers/unixfs-file.spec.js | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/handlers/unixfs-file.js b/src/handlers/unixfs-file.js index c7b3d24..5fd7d3e 100644 --- a/src/handlers/unixfs-file.js +++ b/src/handlers/unixfs-file.js @@ -72,6 +72,7 @@ export async function handleUnixfsFile (request, env, ctx) { if (range && Number(entry.size) !== contentLength) { const contentRange = `bytes ${range[0]}-${range[1]}/${entry.size}` headers['Content-Range'] = contentRange + headers['Content-Length'] = contentLength.toString() } // stream the remainder diff --git a/test/handlers/unixfs-file.spec.js b/test/handlers/unixfs-file.spec.js index dafc6f5..7821af4 100644 --- a/test/handlers/unixfs-file.spec.js +++ b/test/handlers/unixfs-file.spec.js @@ -35,6 +35,7 @@ describe('UnixFS file handler', async () => { assert.equal(res.status, 206) assert.equal(res.headers.get('Content-Range'), `bytes ${first}-${last}/${fileData.length}`) + assert.equal(res.headers.get('Content-Length'), last - first + 1) const data = await res.text() assert.equal(data, 'est') }) @@ -46,6 +47,7 @@ describe('UnixFS file handler', async () => { assert.equal(res.status, 206) assert.equal(res.headers.get('Content-Range'), `bytes ${first}-${fileData.length - 1}/${fileData.length}`) + assert.equal(res.headers.get('Content-Length'), fileData.length - first) const data = await res.text() assert.equal(data, 'est') }) @@ -57,6 +59,7 @@ describe('UnixFS file handler', async () => { assert.equal(res.status, 206) assert.equal(res.headers.get('Content-Range'), `bytes ${fileData.length + suffix}-${fileData.length - 1}/${fileData.length}`) + assert.equal(res.headers.get('Content-Length'), -suffix) const data = await res.text() assert.equal(data, 'est') })