Skip to content

Commit 6742ef2

Browse files
committed
Strict parser. Closes #39
1 parent 226e330 commit 6742ef2

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

lib/index.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@ internals.Range = class {
1818
};
1919

2020

21+
// RFC 7233 (https://tools.ietf.org/html/rfc7233#appendix-D)
22+
//
23+
// Range = "bytes" "=" byte-range-set
24+
// byte-range-set = *( "," OWS ) byte-range-spec *( OWS "," [ OWS byte-range-spec ] )
25+
// byte-range-spec = ( 1*DIGIT "-" [ 1*DIGIT ] ) / ( "-" 1*DIGIT )
26+
27+
28+
// 12 3 3 4 425 6 7 7 8 865 1
29+
internals.headerRx = /^bytes=[\s,]*((?:(?:\d+\-\d*)|(?:\-\d+))(?:\s*,\s*(?:(?:\d+\-\d*)|(?:\-\d+)))*)$/i;
30+
31+
2132
exports.header = function (header, length) {
2233

2334
// Parse header
2435

25-
const parts = header.split('=');
26-
if (parts.length !== 2 ||
27-
parts[0] !== 'bytes') {
28-
36+
const parts = internals.headerRx.exec(header);
37+
if (!parts) {
2938
return null;
3039
}
3140

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

3746
// Handle headers with multiple ranges
3847

39-
for (let i = 0; i < ranges.length; ++i) {
40-
let range = ranges[i];
41-
if (range.length === 1) { // '-'
42-
return null;
43-
}
44-
48+
for (let range of ranges) {
4549
let from;
4650
let to;
4751
range = range.split('-');

0 commit comments

Comments
 (0)