Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle getting zero-length blobs with Content-Range #2486

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
rangeEnd = 0;
}
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
2 changes: 1 addition & 1 deletion src/blob/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function deserializeRangeHeader(
);
}

parts = parts[1].split("-");
parts = parts[1].split("-", 2);
if (parts === undefined || parts.length < 1 || parts.length > 2) {
throw new RangeError(
`deserializeRangeHeader: raw range value ${range} is wrong.`
Expand Down
Loading