From 3b2d11fa2f7bd2f711cd561dd26edcce5c59f23b Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Tue, 1 Aug 2017 21:12:54 +0200 Subject: [PATCH] Fixed the GET Range --- lib/server/v2/commands/Get.js | 8 +++++--- src/server/v2/commands/Get.ts | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/server/v2/commands/Get.js b/lib/server/v2/commands/Get.js index 0baeb623..deea3ef5 100644 --- a/lib/server/v2/commands/Get.js +++ b/lib/server/v2/commands/Get.js @@ -23,9 +23,11 @@ var RangedStream = (function (_super) { } RangedStream.prototype._transform = function (chunk, encoding, callback) { if (this.nb < this.min) { + var lastNb = this.nb; this.nb += chunk.length; if (this.nb > this.min) { - chunk = chunk.slice(this.nb - this.min); + var start = this.min - lastNb; + chunk = chunk.slice(start, this.nb > this.max ? this.max - this.min + 1 + start : undefined); callback(null, chunk); } else @@ -38,7 +40,7 @@ var RangedStream = (function (_super) { else { this.nb += chunk.length; if (this.nb > this.max) - chunk = chunk.slice(0, this.max - (this.nb - chunk.length)); + chunk = chunk.slice(0, this.max - (this.nb - chunk.length) + 1); callback(null, chunk); } }; @@ -90,7 +92,7 @@ var default_1 = (function () { ctx.setCode(WebDAVRequest_1.HTTPCodes.PartialContent); ctx.response.setHeader('Accept-Ranges', 'bytes'); ctx.response.setHeader('Content-Type', mimeType); - ctx.response.setHeader('Content-Length', (max - min).toString()); + ctx.response.setHeader('Content-Length', Math.min(size, max - min + 1).toString()); ctx.response.setHeader('Content-Range', 'bytes ' + min + '-' + max + '/*'); rstream.on('end', callback); rstream.pipe(new RangedStream(min, max)).pipe(ctx.response); diff --git a/src/server/v2/commands/Get.ts b/src/server/v2/commands/Get.ts index 02ea24a1..31e817d1 100644 --- a/src/server/v2/commands/Get.ts +++ b/src/server/v2/commands/Get.ts @@ -14,14 +14,16 @@ class RangedStream extends Transform this.nb = 0; } - _transform(chunk: any, encoding: string, callback: Function) + _transform(chunk : string | Buffer, encoding : string, callback : Function) { if(this.nb < this.min) { + const lastNb = this.nb; this.nb += chunk.length; if(this.nb > this.min) { - chunk = chunk.slice(this.nb - this.min); + const start = this.min - lastNb; + chunk = chunk.slice(start, this.nb > this.max ? this.max - this.min + 1 + start : undefined); callback(null, chunk); } else @@ -36,7 +38,7 @@ class RangedStream extends Transform { this.nb += chunk.length; if(this.nb > this.max) - chunk = chunk.slice(0, this.max - (this.nb - chunk.length)); + chunk = chunk.slice(0, this.max - (this.nb - chunk.length) + 1); callback(null, chunk); } } @@ -100,7 +102,7 @@ export default class implements HTTPMethod ctx.setCode(HTTPCodes.PartialContent); ctx.response.setHeader('Accept-Ranges', 'bytes') ctx.response.setHeader('Content-Type', mimeType) - ctx.response.setHeader('Content-Length', (max - min).toString()) + ctx.response.setHeader('Content-Length', Math.min(size, max - min + 1).toString()) ctx.response.setHeader('Content-Range', 'bytes ' + min + '-' + max + '/*') rstream.on('end', callback);