Skip to content

Commit

Permalink
Strict parser. Closes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Feb 13, 2020
1 parent 226e330 commit 6742ef2
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ internals.Range = class {
};


// RFC 7233 (https://tools.ietf.org/html/rfc7233#appendix-D)
//
// Range = "bytes" "=" byte-range-set
// byte-range-set = *( "," OWS ) byte-range-spec *( OWS "," [ OWS byte-range-spec ] )
// byte-range-spec = ( 1*DIGIT "-" [ 1*DIGIT ] ) / ( "-" 1*DIGIT )


// 12 3 3 4 425 6 7 7 8 865 1
internals.headerRx = /^bytes=[\s,]*((?:(?:\d+\-\d*)|(?:\-\d+))(?:\s*,\s*(?:(?:\d+\-\d*)|(?:\-\d+)))*)$/i;


exports.header = function (header, length) {

// Parse header

const parts = header.split('=');
if (parts.length !== 2 ||
parts[0] !== 'bytes') {

const parts = internals.headerRx.exec(header);
if (!parts) {
return null;
}

Expand All @@ -36,12 +45,7 @@ exports.header = function (header, length) {

// Handle headers with multiple ranges

for (let i = 0; i < ranges.length; ++i) {
let range = ranges[i];
if (range.length === 1) { // '-'
return null;
}

for (let range of ranges) {
let from;
let to;
range = range.split('-');
Expand Down

0 comments on commit 6742ef2

Please sign in to comment.