Created
June 14, 2012 13:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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