Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Range suffixes are not Rust RangeTo #155

Merged
merged 1 commit into from
Nov 24, 2023
Merged
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
49 changes: 43 additions & 6 deletions src/common/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,52 @@ impl Range {
/// Creates a `Range` header from bounds.
pub fn bytes(bounds: impl RangeBounds<u64>) -> Result<Self, InvalidRange> {
let v = match (bounds.start_bound(), bounds.end_bound()) {
(Bound::Unbounded, Bound::Included(end)) => format!("bytes=-{}", end),
(Bound::Unbounded, Bound::Excluded(&end)) => format!("bytes=-{}", end - 1),
(Bound::Included(start), Bound::Included(end)) => format!("bytes={}-{}", start, end),
(Bound::Included(start), Bound::Excluded(&end)) => {
format!("bytes={}-{}", start, end - 1)
}
(Bound::Included(start), Bound::Unbounded) => format!("bytes={}-", start),
// These do not directly translate.
//(Bound::Unbounded, Bound::Included(end)) => format!("bytes=-{}", end),
//(Bound::Unbounded, Bound::Excluded(&end)) => format!("bytes=-{}", end - 1),
_ => return Err(InvalidRange { _inner: () }),
};

Ok(Range(::HeaderValue::from_str(&v).unwrap()))
}

/// Iterate the range sets as a tuple of bounds.
pub fn iter<'a>(&'a self) -> impl Iterator<Item = (Bound<u64>, Bound<u64>)> + 'a {
/// Iterate the range sets as a tuple of bounds, if valid with length.
///
/// The length of the content is passed as an argument, and all ranges
/// that can be satisfied will be iterated.
pub fn satisfiable_ranges<'a>(
&'a self,
len: u64,
) -> impl Iterator<Item = (Bound<u64>, Bound<u64>)> + 'a {
Comment on lines +69 to +72
Copy link

@DDtKey DDtKey Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @seanmonstar
Thanks for these changes, but it looks like it still works not as expected

We can pass the len of the content, but for unbounded or included > len there is no usage of min(), like for example it was implemented in #93

And in general would be nice to have more validation, like from < to.

Otherwise hard to say it's "satisfiable" ranges 🤔

let s = self
.0
.to_str()
.expect("valid string checked in Header::decode()");

s["bytes=".len()..].split(',').filter_map(|spec| {
s["bytes=".len()..].split(',').filter_map(move |spec| {
let mut iter = spec.trim().splitn(2, '-');
Some((parse_bound(iter.next()?)?, parse_bound(iter.next()?)?))
let start = parse_bound(iter.next()?)?;
let end = parse_bound(iter.next()?)?;

// Unbounded ranges in HTTP are actually a suffix
// For example, `-100` means the last 100 bytes.
if let Bound::Unbounded = start {
if let Bound::Included(end) = end {
if len < end {
// Last N bytes is larger than available!
return None;
}
return Some((Bound::Included(len - end), Bound::Unbounded));
}
// else fall through
}

Some((start, end))
})
}
}
Expand Down Expand Up @@ -416,3 +439,17 @@ fn test_byte_range_spec_to_satisfiable_range() {
bench_header!(bytes_multi, Range, { vec![b"bytes=1-1001,2001-3001,10001-".to_vec()]});
bench_header!(custom_unit, Range, { vec![b"other=0-100000".to_vec()]});
*/

#[test]
fn test_to_satisfiable_range_suffix() {
let range = super::test_decode::<Range>(&["bytes=-100"]).unwrap();
let bounds = range.satisfiable_ranges(350).next().unwrap();
assert_eq!(bounds, (Bound::Included(250), Bound::Unbounded));
}
seanmonstar marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn test_to_unsatisfiable_range_suffix() {
let range = super::test_decode::<Range>(&["bytes=-350"]).unwrap();
let bounds = range.satisfiable_ranges(100).next();
assert_eq!(bounds, None);
}