Skip to main content

Module range

Module range 

Source
Expand description

Range, Content-Range, and If-Range (RFC 9110 section 14), with resolution against a representation length and budgets against abusive range sets.

§Examples

use parse_net::{self as pn, ByteRange, RangeOutcome, range};

let mut out = [ByteRange { start: 0, end: 0 }; 8];
assert_eq!(pn::byte_ranges(b"bytes=0-99, -100", 1000, &mut out)?, RangeOutcome::Ranges(2));
assert_eq!(out[1], ByteRange { start: 900, end: 999 });
assert_eq!(pn::byte_ranges(b"bytes=2000-", 1000, &mut out)?, RangeOutcome::Unsatisfiable);
assert_eq!(pn::byte_ranges(b"items=0-5", 1000, &mut out)?, RangeOutcome::NotBytes);

let mut buf = [0u8; 32];
let mut sink = pn::SliceSink::new(&mut buf);
range::write_content_range(out[0], Some(1000), &mut sink).expect("room");
assert_eq!(sink.written(), b"bytes 0-99/1000");

Structs§

ByteRange
A resolved byte range, both ends inclusive.
RangeHeader
A validated Range value.

Enums§

ContentRange
A parsed Content-Range value.
IfRange
A parsed If-Range value.
RangeOutcome
What to do with a Range request.
RangeSpec
One range-spec of a bytes range set.

Functions§

byte_ranges
Parses and resolves Range against length with the default limits and no coalescing.
parse
Parses range-unit "=" range-set (RFC 9110 section 14.1.2).
parse_content_range
Parses Content-Range (RFC 9110 section 14.4) for the bytes unit.
parse_if_range
Parses If-Range without a clock: an entity tag when the value starts with W/ or ", an IMF-fixdate or asctime date otherwise. An RFC 850 date needs the current time to place its year, so it takes parse_if_range_at; here it is ErrorKind::InvalidDate.
parse_if_range_at
Parses If-Range: an entity tag when the value starts with W/ or ", an HTTP-date otherwise, in any of the three forms (RFC 9110 section 5.6.7). now (Unix seconds) places the two-digit year of an RFC 850 date. Use it over parse_if_range when a clock is at hand.
resolve
Resolves a parsed Range against a representation of length bytes.
write_content_range
Writes bytes first-last/complete, or bytes first-last/* when the complete length is unknown.
write_unsatisfied
Writes bytes */complete, for a 416 response.