Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ describe("parse(len, str)", () => {
});
});

it("should parse str with tab whitespace", () => {
var range = parse(1000, "bytes=\t40-80,\t81-90\t,-1\t");
expect(range).toEqual({
type: "bytes",
ranges: [
{ start: 40, end: 80 },
{ start: 81, end: 90 },
{ start: 999, end: 999 },
],
});
});

it("should parse non-byte range", () => {
var range = parse(1000, "items=0-5");
expect(range).toEqual({
Expand Down
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export function parse(size: number, str: string): Result | -1 | -2 {
// handle ows around numbers
let startStrIndex = index;
let endStrIndex = endIndex;
while (str.charCodeAt(startStrIndex) === 32) startStrIndex++;
while (str.charCodeAt(endStrIndex - 1) === 32) endStrIndex--;
while (isOws(str.charCodeAt(startStrIndex))) startStrIndex++;
while (isOws(str.charCodeAt(endStrIndex - 1))) endStrIndex--;

let start = NaN;
let end = NaN;
Expand Down Expand Up @@ -103,6 +103,13 @@ export function parse(size: number, str: string): Result | -1 | -2 {
return { type, ranges };
}

/**
* Check for optional whitespace (SP / HTAB), RFC 9110 section 5.6.3.
*/
function isOws(code: number): boolean {
return code === 32 || code === 9;
}

/**
* Parse string to integer.
*/
Expand Down