Skip to content

Commit

Permalink
small cleanups
Browse files Browse the repository at this point in the history
...using newer Rust APIs.
  • Loading branch information
scottlamb committed Aug 27, 2024
1 parent ee0d0d1 commit 75def15
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 41 deletions.
36 changes: 15 additions & 21 deletions src/etag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@

use http::header::{self, HeaderMap, HeaderValue};

/// Performs weak validation of two etags (such as B"W/\"foo\"" or B"\"bar\"").
pub fn weak_eq(mut a: &[u8], mut b: &[u8]) -> bool {
if a.starts_with(b"W/") {
a = &a[2..];
}
if b.starts_with(b"W/") {
b = &b[2..];
}
/// Performs weak validation of two etags (such as `B"W/\"foo\""`` or `B"\"bar\""``)
/// as in [RFC 7232 section 2.3.2](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3.2).
pub fn weak_eq(a: &[u8], b: &[u8]) -> bool {
let a = a.strip_prefix(b"W/").unwrap_or(a);
let b = b.strip_prefix(b"W/").unwrap_or(b);
a == b
}

/// Performs strong validation of two etags (such as B"W/\"foo\"" or B"\"bar\"").
/// Performs strong validation of two etags (such as `B"W/\"foo\""` or `B"\"bar\""``)
/// as in [RFC 7232 section 2.3.2](https://datatracker.ietf.org/doc/html/rfc7232#section-2.3.2).
pub fn strong_eq(a: &[u8], b: &[u8]) -> bool {
a == b && !a.starts_with(b"W/")
}
Expand Down Expand Up @@ -54,7 +52,7 @@ impl<'a> Iterator for List<'a> {
return None;
}

// If on an etag, find its end. Note the '"' can't be escaped, simplifying matters.
// If on an etag, find its end. Note the `"` can't be escaped, simplifying matters.
let end = if self.remaining.starts_with(b"W/\"") {
self.remaining[3..]
.iter()
Expand All @@ -66,21 +64,17 @@ impl<'a> Iterator for List<'a> {
.position(|&b| b == b'"')
.map(|p| p + 1)
} else {
self.corrupt = true;
None
};
let end = match end {
None => {
self.corrupt = true;
return None;
}
Some(e) => e,
let Some(end) = end else {
self.corrupt = true;
return None;
};
let (etag, mut rem) = self.remaining.split_at(end + 1);
if rem.starts_with(b",") {
rem = &rem[1..];
while !rem.is_empty() && (rem[0] == b' ' || rem[0] == b'\t') {
rem = &rem[1..];
if let [b',', r @ ..] = rem {
rem = r;
while let [b' ' | b'\t', tail @ ..] = rem {
rem = tail;
}
}
self.remaining = rem;
Expand Down
27 changes: 10 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,30 +237,23 @@ pub fn should_gzip(headers: &HeaderMap) -> bool {
};
for qi in parts {
// Parse.
let qi = qi.trim();
let mut parts = qi.rsplitn(2, ';').map(|p| p.trim());
let last_part = parts
.next()
.expect("rsplitn should return at least one part");
let coding;
let quality;
match parts.next() {
match qi.split_once(';') {
None => {
coding = last_part;
coding = qi.trim();
quality = 1000;
}
Some(c) => {
if !last_part.starts_with("q=") {
Some((c, q)) => {
coding = c.trim();
let Some(q) = q
.trim()
.strip_prefix("q=")
.and_then(|q| parse_qvalue(q).ok())
else {
return false; // unparseable.
}
let q = &last_part[2..];
match parse_qvalue(q) {
Ok(q) => {
coding = c;
quality = q;
}
Err(_) => return false, // unparseable.
};
quality = q;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ pub(crate) fn parse(range: Option<&HeaderValue>, len: u64) -> ResolvedRanges {
};

// byte-ranges-specifier = bytes-unit "=" byte-range-set
if !range.starts_with("bytes=") {
let Some(bytes) = range.strip_prefix("bytes=") else {
return ResolvedRanges::None;
}
};

// byte-range-set = 1#( byte-range-spec / suffix-byte-range-spec )
let mut ranges: SmallVec<[Range<u64>; 1]> = SmallVec::new();
for r in range[6..].split(',') {
for r in bytes.split(',') {
// Trim OWS = *( SP / HTAB )
let r = r.trim_start_matches(|c| c == ' ' || c == '\t');

Expand Down

0 comments on commit 75def15

Please sign in to comment.