use crate::{
field::metadata::block::{
Authority, Form, Gate, Outcome, Plan, Response, Validation,
gate::{
AUTHORITY_PORT, CONTENT_LENGTH, EXTENDED_ENABLED, PATH_ASTERISK, REQUIRE_AUTHORITY,
Sealed,
method::Method,
names::{AUTHORITY, HOST, METHOD, PATH, PROTOCOL, SCHEME, STATUS},
},
},
uri::Scheme,
};
pub trait Initial: Copy + Eq + Sealed {
const REQUEST: bool;
fn finish(gate: &Gate<Self>) -> Outcome<Self>;
fn is_interim(&self) -> bool {
false
}
fn is_connect(&self) -> bool {
false
}
}
impl Initial for Plan {
const REQUEST: bool = true;
fn finish(gate: &Gate<Self>) -> Outcome<Self> {
if let Some(check) = gate.section() {
return Outcome::other(check);
}
let seen = gate.names.seen;
let bad = Outcome::Rejected(Validation::Malformed);
if seen & METHOD == 0 {
return bad;
}
let (form, scheme, source) = if gate.method == Method::Connect && seen & PROTOCOL == 0 {
if seen & (SCHEME | PATH | AUTHORITY) != AUTHORITY || gate.flags & AUTHORITY_PORT == 0 {
return bad;
}
(Form::Connect, Scheme::None, Authority::Target)
} else {
if seen & (SCHEME | PATH) != SCHEME | PATH {
return bad;
}
let source = if seen & AUTHORITY != 0 {
Authority::Pseudo
} else if seen & HOST != 0 {
Authority::Host
} else if gate.flags & REQUIRE_AUTHORITY != 0
&& matches!(gate.scheme, Scheme::Http | Scheme::Https)
{
return bad;
} else {
Authority::None
};
let form = if gate.method == Method::Connect {
if gate.flags & EXTENDED_ENABLED == 0 {
return bad;
}
Form::ExtendedConnect
} else if seen & PROTOCOL != 0 {
return bad;
} else if gate.flags & PATH_ASTERISK != 0 {
Form::Asterisk
} else {
Form::Path
};
(form, gate.scheme, source)
};
Outcome::Initial(Plan::new(
form,
scheme,
source,
gate.method.response(),
(gate.flags & CONTENT_LENGTH != 0).then_some(gate.length),
))
}
fn is_connect(&self) -> bool {
self.form() == Form::Connect
}
}
impl Initial for Response {
const REQUEST: bool = false;
fn finish(gate: &Gate<Self>) -> Outcome<Self> {
if let Some(check) = gate.section() {
return Outcome::other(check);
}
match gate.status {
Some(code) if gate.names.seen & STATUS != 0 => Outcome::Initial(if code.get() < 200 {
Self::Informational
} else {
Self::Final
}),
_ => Outcome::Rejected(Validation::Malformed),
}
}
fn is_interim(&self) -> bool {
*self == Self::Informational
}
}