Merge pull request #10133 from kevinleedrum/fix-content-length

Set returnValues.suggestedLength to Content-Length if integer
This commit is contained in:
Tim van der Meij 2018-10-09 00:05:57 +02:00 committed by GitHub
commit f45e46d7ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 22 deletions

View File

@ -27,6 +27,20 @@ function validateRangeRequestCapabilities({ getResponseHeader, isHttp,
allowRangeRequests: false,
suggestedLength: undefined,
};
let length = parseInt(getResponseHeader('Content-Length'), 10);
if (!Number.isInteger(length)) {
return returnValues;
}
returnValues.suggestedLength = length;
if (length <= 2 * rangeChunkSize) {
// The file size is smaller than the size of two chunks, so it does not
// make any sense to abort the request and retry with a range request.
return returnValues;
}
if (disableRange || !isHttp) {
return returnValues;
}
@ -39,18 +53,6 @@ function validateRangeRequestCapabilities({ getResponseHeader, isHttp,
return returnValues;
}
let length = parseInt(getResponseHeader('Content-Length'), 10);
if (!Number.isInteger(length)) {
return returnValues;
}
returnValues.suggestedLength = length;
if (length <= 2 * rangeChunkSize) {
// The file size is smaller than the size of two chunks, so it does not
// make any sense to abort the request and retry with a range request.
return returnValues;
}
returnValues.allowRangeRequests = true;
return returnValues;
}

View File

@ -23,11 +23,6 @@ import {
describe('network_utils', function() {
describe('validateRangeRequestCapabilities', function() {
const defaultValues = {
allowRangeRequests: false,
suggestedLength: undefined,
};
it('rejects range chunk sizes that are not larger than zero', function() {
expect(function() {
validateRangeRequestCapabilities({ rangeChunkSize: 0, });
@ -38,14 +33,30 @@ describe('network_utils', function() {
expect(validateRangeRequestCapabilities({
disableRange: true,
isHttp: true,
getResponseHeader: (headerName) => {
if (headerName === 'Content-Length') {
return 8;
}
},
rangeChunkSize: 64,
})).toEqual(defaultValues);
})).toEqual({
allowRangeRequests: false,
suggestedLength: 8,
});
expect(validateRangeRequestCapabilities({
disableRange: false,
isHttp: false,
getResponseHeader: (headerName) => {
if (headerName === 'Content-Length') {
return 8;
}
},
rangeChunkSize: 64,
})).toEqual(defaultValues);
})).toEqual({
allowRangeRequests: false,
suggestedLength: 8,
});
});
it('rejects invalid Accept-Ranges header values', function() {
@ -55,10 +66,15 @@ describe('network_utils', function() {
getResponseHeader: (headerName) => {
if (headerName === 'Accept-Ranges') {
return 'none';
} else if (headerName === 'Content-Length') {
return 8;
}
},
rangeChunkSize: 64,
})).toEqual(defaultValues);
})).toEqual({
allowRangeRequests: false,
suggestedLength: 8,
});
});
it('rejects invalid Content-Encoding header values', function() {
@ -70,10 +86,15 @@ describe('network_utils', function() {
return 'bytes';
} else if (headerName === 'Content-Encoding') {
return 'gzip';
} else if (headerName === 'Content-Length') {
return 8;
}
},
rangeChunkSize: 64,
})).toEqual(defaultValues);
})).toEqual({
allowRangeRequests: false,
suggestedLength: 8,
});
});
it('rejects invalid Content-Length header values', function() {
@ -90,7 +111,10 @@ describe('network_utils', function() {
}
},
rangeChunkSize: 64,
})).toEqual(defaultValues);
})).toEqual({
allowRangeRequests: false,
suggestedLength: undefined,
});
});
it('rejects file sizes that are too small for range requests', function() {