Skip to content

Commit

Permalink
Support range headers with an end range past EOF
Browse files Browse the repository at this point in the history
This is specified in section 14.1.2 of the httpwg rfc9110, and
section 14.35.1 of the ietf rfc2616 linked in this repo:

"A client can limit the number of bytes requested without knowing the
size of the selected representation. If the last-pos value is absent, or if
the value is greater than or equal to the current length of the
representation data, the byte range is interpreted as the remainder of the
representation (i.e., the server replaces the value of last-pos with a value
that is one less than the current length of the selected representation)."
  • Loading branch information
jfaust committed Jul 20, 2023
1 parent 1c53a53 commit 36a082a
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,12 @@ impl ParsedRanges {
}
};
let end = match parsed.end {
EndPosition::Index(i) => i,
EndPosition::Index(i) => std::cmp::min(i, file_size_bytes - 1),
EndPosition::LastByte => file_size_bytes - 1,
};

if end < file_size_bytes {
let valid = RangeInclusive::new(start, end);
validated.push(valid);
} else {
return invalid!("Range end exceedes EOF".to_string());
}
let valid = RangeInclusive::new(start, end);
validated.push(valid);
}
match validate_ranges(validated.as_slice()) {
RangeValidationResult::Valid => Ok(validated),
Expand Down Expand Up @@ -534,12 +530,14 @@ mod tests {
}

#[test]
fn parse_out_of_bounds_overrun_as_unsatisfiable() {
fn parse_out_of_bounds_overrun_as_content_length() {
let input = &format!("bytes=0-{}", TEST_FILE_LENGTH);
let parsed = parse_range_header(input)
let expect = vec![RangeInclusive::new(0, TEST_FILE_LENGTH - 1)];
let actual = parse_range_header(input)
.unwrap()
.validate(TEST_FILE_LENGTH);
assert!(parsed.is_err());
.validate(TEST_FILE_LENGTH)
.unwrap();
assert_eq!(expect, actual);
}

#[test]
Expand Down

0 comments on commit 36a082a

Please sign in to comment.