Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edhgoose/2930131 to your computer and use it in GitHub Desktop.
Save edhgoose/2930131 to your computer and use it in GitHub Desktop.
A modified version of parseRange in connectjs to deal with range requests of the form bytes=a-b, c-d where a,b,c and d are all numbers, and while a < b and c < d, there is not necessarily a correspondence between a and c or b and d.
exports.parseRange = function(size, str){
var valid = true;
console.log('Requested range: ' + str);
var arr = str.substr(6).split(',').map(function(range){
console.log(range);
var range = range.split('-')
, start = parseInt(range[0], 10)
, end = parseInt(range[1], 10);
// -500
if (isNaN(start)) {
start = size - end;
end = size - 1;
// 500-
} else if (isNaN(end)) {
end = size - 1;
}
// Invalid
if (isNaN(start) || isNaN(end) || start > end || start < 0) {
valid = false;
}
return {
start: start,
end: end
};
});
if (valid) {
if (arr.length > 1) {
return [ {
start: Math.min(arr[0].start, arr[1].start),
end: Math.max(arr[0].end, arr[1].end)
} ];
} else {
return arr;
}
} else {
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment