Skip to content

Commit

Permalink
Add etag_matches
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Oct 27, 2024
1 parent b44deed commit 7cdccfe
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/webdav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ pub fn pick_content_types(
})
}

/// Check if an etag matches an If-Matches condition.
///
/// # Arguments
/// * `condition` - Condition (e.g. "*", "\"foo\"" or "\"foo\", \"bar\"")
/// * `actual_etag` - ETag to compare to. None nonexistant
///
/// # Returns
/// bool indicating whether condition matches
pub fn etag_matches(condition: Option<&str>, actual_etag: Option<&str>) -> bool {
if actual_etag.is_none() && condition.is_some() {
return false;
}
for etag in condition.unwrap_or_default().split(",") {
if etag.trim_matches(' ') == "*" {
return true;
}
if etag.trim_matches(' ') == actual_etag.unwrap_or_default() {
return true;
}
}
false
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -181,4 +204,14 @@ mod tests {
]
);
}

#[test]
fn test_etag_matches() {
assert!(etag_matches(None, None));
assert!(!etag_matches(Some("*"), None));
assert!(etag_matches(Some("*"), Some("foo")));
assert!(etag_matches(Some("foo"), Some("foo")));
assert!(etag_matches(Some("foo, bar"), Some("foo")));
assert!(!etag_matches(Some("foo"), Some("bar")));
}
}

0 comments on commit 7cdccfe

Please sign in to comment.