use crate::error::{RejectKind, Rejection};
use crate::xml::reader::Limits;
pub const CONTENT_TYPE: &str = "text/xml; charset=utf-8";
pub const SPEC_PATH: &str = "/USEF/2019/SignedMessage";
pub const SHAPESHIFTER_PATH: &str = "/shapeshifter/api/v3/message";
pub fn check_request(
content_type: Option<&str>,
content_length: Option<u64>,
limits: &Limits,
) -> Result<(), Rejection> {
let length = content_length.ok_or_else(|| {
Rejection::new(
RejectKind::LengthRequired,
"[UFTP App. 4 §4] Content-Length",
"The request has no Content-Length.",
)
})?;
let ct = content_type.unwrap_or("");
let mut parts = ct.split(';');
let media = parts.next().unwrap_or("").trim();
if !media.eq_ignore_ascii_case("text/xml") {
return Err(content_type_error(&format!(
"The Content-Type is {ct}; UFTP messages are text/xml.",
ct = crate::error::quote(&ct)
)));
}
for param in parts {
if let Some((k, v)) = param.split_once('=')
&& k.trim().eq_ignore_ascii_case("charset")
{
let v = v.trim().trim_matches('"');
if !v.eq_ignore_ascii_case("utf-8") {
return Err(content_type_error(&format!(
"The charset is {v}; UFTP messages are UTF-8.",
v = crate::error::quote(&v)
)));
}
}
}
if usize::try_from(length).map_or(true, |l| l > limits.max_bytes()) {
return Err(Rejection::new(
RejectKind::Limits,
"[decoder limits]",
format!(
"The Content-Length {length} is over the limit of {} bytes.",
limits.max_bytes()
),
));
}
Ok(())
}
fn content_type_error(message: &str) -> Rejection {
Rejection::new(
RejectKind::Header,
"[UFTP App. 4 §4] Content-Type text/xml, UTF-8",
message,
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Delivery {
Delivered,
Retry,
Failed,
}
impl Delivery {
#[must_use]
pub fn of(status: u16) -> Self {
match status {
200..=299 => Self::Delivered,
404 | 408 | 419 | 425 | 429 => Self::Retry,
400..=499 => Self::Failed,
_ => Self::Retry,
}
}
}