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
}
}
#[inline(never)]
pub(crate) fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
let mut diff: u8 = 0;
for (&x, &y) in a.iter().zip(b.iter()) {
diff |= x ^ y;
}
black_box(diff) == 0
}
#[inline(always)]
pub(crate) fn is_read_only_method(method: &Method) -> bool {
method.is_get() || method.is_head() || method.is_options()
}
#[inline(always)]
pub(crate) fn mode_to_u8(mode: MaintenanceMode) -> u8 {
match mode {
MaintenanceMode::Active => MODE_TAG_ACTIVE,
MaintenanceMode::ReadOnly => MODE_TAG_READ_ONLY,
MaintenanceMode::Maintenance => MODE_TAG_MAINTENANCE,
}
}
#[inline(always)]
pub(crate) fn mode_from_u8(tag: u8) -> MaintenanceMode {
match tag {
MODE_TAG_READ_ONLY => MaintenanceMode::ReadOnly,
MODE_TAG_MAINTENANCE => MaintenanceMode::Maintenance,
_ => MaintenanceMode::Active,
}
}
pub(crate) fn build_reject_wire(
status: HttpStatus,
content_type: &str,
retry_after_secs: Option<u64>,
body: &[u8],
) -> Vec<u8> {
let mut resp: Response = Response::default();
resp.set_status_code(status.code())
.set_header(CONTENT_TYPE, content_type)
.set_body(body);
if let Some(secs) = retry_after_secs {
resp.set_header(RETRY_AFTER, secs.to_string());
}
resp.build()
}