feat: support static server

This commit is contained in:
2025-06-18 02:19:42 +08:00
parent 35312ea1ff
commit 6726cafff4
26 changed files with 321 additions and 230 deletions

View File

@@ -1,18 +1,23 @@
use std::ops::Bound;
pub fn bound_range_to_content_range(
r: &(Bound<u64>, Bound<u64>),
l: u64,
) -> Result<String, String> {
use http::HeaderValue;
pub fn build_no_satisfiable_content_range(len: u64) -> HeaderValue {
HeaderValue::from_str(&format!("bytes */{len}"))
.unwrap_or_else(|e| unreachable!("Invalid content range: {e}"))
}
pub fn bound_range_to_content_range(r: &(Bound<u64>, Bound<u64>), l: u64) -> Option<HeaderValue> {
match r {
(Bound::Included(start), Bound::Included(end)) => Ok(format!("bytes {start}-{end}/{l}")),
(Bound::Included(start), Bound::Included(end)) => Some(format!("bytes {start}-{end}/{l}")),
(Bound::Included(start), Bound::Excluded(end)) => {
Ok(format!("bytes {start}-{}/{l}", end - 1))
Some(format!("bytes {start}-{}/{l}", end - 1))
}
(Bound::Included(start), Bound::Unbounded) => Ok(format!(
(Bound::Included(start), Bound::Unbounded) => Some(format!(
"bytes {start}-{}/{l}",
if l > 0 { l - 1 } else { 0 }
)),
_ => Err(format!("bytes */{l}")),
_ => None,
}
.and_then(|s| HeaderValue::from_str(&s).ok())
}