Skip to content

Commit

Permalink
Handle getting zero-length blobs with Content-Range
Browse files Browse the repository at this point in the history
This behavior matches Azure.  AWS Java SDK probes for a zero-length
blob when a request with a Content-Range fails.
  • Loading branch information
gaul committed Nov 9, 2024
1 parent 1dd0784 commit 9220864
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/blob/errors/StorageError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export default class StorageError extends MiddlewareError {
storageErrorCode: string,
storageErrorMessage: string,
storageRequestID: string,
storageAdditionalErrorMessages: { [key: string]: string } = {}
storageAdditionalErrorMessages: { [key: string]: string } = {},
additionalHeaders: { [key: string]: string } = {}
) {
const bodyInJSON: any = {
Code: storageErrorCode,
Expand All @@ -51,7 +52,8 @@ export default class StorageError extends MiddlewareError {
storageErrorMessage,
{
"x-ms-error-code": storageErrorCode,
"x-ms-request-id": storageRequestID
"x-ms-request-id": storageRequestID,
...additionalHeaders
},
bodyInXML,
"application/xml"
Expand Down
6 changes: 4 additions & 2 deletions src/blob/errors/StorageErrorFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ export default class StorageErrorFactory {
);
}

public static getInvalidPageRange2(contextID: string): StorageError {
public static getInvalidPageRange2(contextID: string, additionalHeaders?: { [key: string]: string }): StorageError {
return new StorageError(
416,
"InvalidRange",
"The range specified is invalid for the current size of the resource.",
contextID
contextID,
{},
additionalHeaders
);
}

Expand Down
9 changes: 6 additions & 3 deletions src/blob/handlers/BlobHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,10 @@ export default class BlobHandler extends BaseHandler implements IBlobHandler {
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!, { "content-range": "bytes * /0" });
}
else if (blob.properties.contentLength == 0 && rangeEnd == 0) {
// do nothing
}
else {
rangeEnd = blob.properties.contentLength! - 1;
Expand Down Expand Up @@ -1084,7 +1087,7 @@ export default class BlobHandler extends BaseHandler implements IBlobHandler {
}

const response: Models.BlobDownloadResponse = {
statusCode: contentRange ? 206 : 200,
statusCode: (rangeStart != 0 && rangeEnd != 0 && contentRange) ? 206 : 200,
body,
metadata: blob.metadata,
eTag: blob.properties.etag,
Expand Down Expand Up @@ -1148,7 +1151,7 @@ export default class BlobHandler extends BaseHandler implements IBlobHandler {
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!, { "content-range": "bytes * /0" });
}
else {
rangeEnd = blob.properties.contentLength! - 1;
Expand Down

0 comments on commit 9220864

Please sign in to comment.