diff --git a/ChangeLog.md b/ChangeLog.md index 7ab36027d..2a9ecdfac 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -4,6 +4,10 @@ ## Upcoming Release +Blob: + +- Fixed issue of download 0 size blob with range > 0 should has header "Content-Range: bytes \*/0" in returned error. (issue #2458) + ## 2024.10 Version 3.33.0 General: diff --git a/src/blob/errors/StorageErrorFactory.ts b/src/blob/errors/StorageErrorFactory.ts index 5fd4bca05..18fabe9be 100644 --- a/src/blob/errors/StorageErrorFactory.ts +++ b/src/blob/errors/StorageErrorFactory.ts @@ -199,13 +199,18 @@ export default class StorageErrorFactory { ); } - public static getInvalidPageRange2(contextID: string): StorageError { - return new StorageError( + public static getInvalidPageRange2(contextID: string, contentRange?: string): StorageError { + let returnValue = new StorageError( 416, "InvalidRange", "The range specified is invalid for the current size of the resource.", contextID - ); + ); + if(contentRange) + { + returnValue.headers!["Content-Range"] = contentRange; + } + return returnValue; } public static getInvalidLeaseDuration( diff --git a/src/blob/handlers/BlobHandler.ts b/src/blob/handlers/BlobHandler.ts index ef3625cee..c127325c3 100644 --- a/src/blob/handlers/BlobHandler.ts +++ b/src/blob/handlers/BlobHandler.ts @@ -1010,14 +1010,14 @@ export default class BlobHandler extends BaseHandler implements IBlobHandler { // Start Range is bigger than blob length if (rangeStart > blob.properties.contentLength!) { - throw StorageErrorFactory.getInvalidPageRange(context.contextId!); + throw StorageErrorFactory.getInvalidPageRange2(context.contextId!,`bytes */${blob.properties.contentLength}`); } // Will automatically shift request with longer data end than blob size to blob size if (rangeEnd + 1 >= blob.properties.contentLength!) { // report error is blob size is 0, and rangeEnd is specified but not 0 if (blob.properties.contentLength == 0 && rangeEnd !== 0 && rangeEnd !== Infinity) { - throw StorageErrorFactory.getInvalidPageRange2(context.contextId!); + throw StorageErrorFactory.getInvalidPageRange2(context.contextId!,`bytes */${blob.properties.contentLength}`); } else { rangeEnd = blob.properties.contentLength! - 1; @@ -1141,14 +1141,14 @@ export default class BlobHandler extends BaseHandler implements IBlobHandler { // Start Range is bigger than blob length if (rangeStart > blob.properties.contentLength!) { - throw StorageErrorFactory.getInvalidPageRange(context.contextId!); + throw StorageErrorFactory.getInvalidPageRange2(context.contextId!,`bytes */${blob.properties.contentLength}`); } // Will automatically shift request with longer data end than blob size to blob size if (rangeEnd + 1 >= blob.properties.contentLength!) { // report error is blob size is 0, and rangeEnd is specified but not 0 if (blob.properties.contentLength == 0 && rangeEnd !== 0 && rangeEnd !== Infinity) { - throw StorageErrorFactory.getInvalidPageRange2(context.contextId!); + throw StorageErrorFactory.getInvalidPageRange2(context.contextId!,`bytes */${blob.properties.contentLength}`); } else { rangeEnd = blob.properties.contentLength! - 1; diff --git a/tests/blob/apis/blockblob.test.ts b/tests/blob/apis/blockblob.test.ts index d282a3163..47b10dd90 100644 --- a/tests/blob/apis/blockblob.test.ts +++ b/tests/blob/apis/blockblob.test.ts @@ -427,6 +427,7 @@ describe("BlockBlobAPIs", () => { await blockBlobClient.download(0, 3); } catch (error) { assert.deepStrictEqual(error.statusCode, 416); + assert.deepStrictEqual(error.response.headers.get("content-range"), 'bytes */0') return; } assert.fail(); diff --git a/tests/blob/apis/pageblob.test.ts b/tests/blob/apis/pageblob.test.ts index 711d4bc7a..b2354b36c 100644 --- a/tests/blob/apis/pageblob.test.ts +++ b/tests/blob/apis/pageblob.test.ts @@ -330,6 +330,7 @@ describe("PageBlobAPIs", () => { await pageBlobClient.download(0, 3); } catch (error) { assert.deepStrictEqual(error.statusCode, 416); + assert.deepStrictEqual(error.response.headers.get("content-range"), 'bytes */0') return; } assert.fail();