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.

  • Download Gist
Bastardisation of rangeCheck in connect lib util.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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;
}
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.