Skip to content

Commit

Permalink
Fix range parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed May 3, 2010
1 parent 5b5dcc4 commit 93f0351
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/jsgi/rest-store.js
Expand Up @@ -63,20 +63,24 @@ exports.RestStore = function(options){
else if(!METHOD_HAS_BODY[method]){
if(method === "get" && request.pathInfo.substring(request.pathInfo.length - 1) === "/"){
// handle the range header
var range = request.headers.range || "items=0-";
var parts = range.replace('items=','').split('-');
// syntaxically invalid "Range:" just results in 0-Infinity
metadata.start = +parts[0] || undefined;
metadata.end = +parts[1] || undefined;
// shortcut evaluation of range that would result in empty set
if (metadata.end < metadata.start) {
responseValue = [];
} else {
// queries are not decoded, the info needs to be retained for parsing
// TODO: limit the range by a configurable number
responseValue = store.query(path, metadata);
if(metadata.range){
// invalid "Range:" just results in 0-Infinity
metadata.range.replace(/^items=(\d+)-(\d+)?/, function(s, b, e){
if(b >= 0){
metadata.start = b;
}
if(e >= b){
metadata.end = e;
}
else if (e !== undefined){
delete metadata.start;
}
});
}
if(request.headers.range){
// queries are not decoded, the info needs to be retained for parsing
// TODO: limit the range by a configurable number
responseValue = store.query(path, metadata);
if(metadata.range){
// we have to wait for promise for counts to be set (e.g., mongo)
when(responseValue, function(responseValue){
var count = responseValue.totalCount || responseValue.length || 0;
Expand Down

0 comments on commit 93f0351

Please sign in to comment.