use super::*;
pub(crate) fn normalize_prefix(raw: &str) -> String {
let trimmed: &str = raw.trim().trim_end_matches('/');
if trimmed.is_empty() || trimmed == ROOT_SLASH {
return ROOT_SLASH.to_owned();
}
if trimmed.starts_with('/') {
trimmed.to_owned()
} else {
format!("/{trimmed}")
}
}
#[inline]
pub(crate) fn path_matches_prefix(path: &str, prefix: &str) -> bool {
if prefix == ROOT_SLASH {
return true;
}
if let Some(rest) = path.strip_prefix(prefix) {
rest.is_empty() || rest.starts_with('/')
} else {
false
}
}
pub fn parse_timeout_header_ms(raw: &str) -> Option<u64> {
let trimmed: &str = raw.trim();
if trimmed.is_empty() {
return None;
}
let ms: u64 = if let Some(s) = trimmed.strip_suffix("ms") {
s.trim().parse::<u64>().ok()?
} else if let Some(s) = trimmed.strip_suffix('s') {
s.trim().parse::<u64>().ok()?.saturating_mul(1000)
} else {
trimmed.parse::<u64>().ok()?
};
if ms == 0 { None } else { Some(ms) }
}
pub async fn timeout_future<F, T>(ctx: &Context, future: F) -> Result<T, TimeoutError>
where
F: Future<Output = T>,
{
let Some(deadline) = RequestDeadline::from_context(ctx) else {
return Ok(future.await);
};
let remaining: Duration = deadline.remaining();
if remaining.is_zero() {
return Err(TimeoutError::DeadlineExceeded);
}
timeout(remaining, future)
.await
.map_err(|_| TimeoutError::DeadlineExceeded)
}