use core::ops;
use state::{decode::Final, range::Authority};
use crate::field::{
head,
metadata::{
block,
map::record,
section::{rules, state, storage},
},
};
pub struct Raw<C: block::Initial = block::Plan, S = ()> {
pub(super) core: state::Core<C>,
pub(super) selection: S,
}
pub struct Discard<C: block::Initial = block::Plan> {
pub(super) core: state::Core<C>,
}
#[derive(Clone, Copy, PartialEq, Eq)]
#[doc(hidden)]
pub enum Omit {
Authority,
Discard(head::Name),
Skip,
}
impl<C: block::Initial, S: rules::RawSelection> Raw<C, S> {
pub fn h2(kind: block::Kind, connect: block::Connect) -> Self {
Self {
core: state::Core::new(kind, connect, block::Version::Two),
selection: S::default(),
}
}
#[doc(hidden)]
pub(in crate::field::metadata) fn accept(&mut self, proof: block::Name) -> block::Validation {
self.core.control.accept_name(proof.head())
}
#[doc(hidden)]
pub(in crate::field::metadata) fn retain<A: rules::Push>(
&mut self,
known: head::Name,
field: record::View<'_>,
retained: &[u8],
scratch: &[u8],
) -> rules::Retention {
let retention = self.core.check::<A>(known, field, retained, scratch);
if let Some((matched, base)) = S::CONTENT_TYPE
&& known == head::Name::Regular
&& retention == rules::Retention::Retain
&& self.core.control.is_request()
&& self.selection != matched
&& field.name.is_equal(b"content-type")
&& rules::matches_media_type(field.value, base)
{
self.selection = matched;
}
retention
}
#[doc(hidden)]
pub(in crate::field::metadata) fn store(
&mut self,
known: head::Name,
range: ops::Range<usize>,
) {
if known == head::Name::Authority && self.core.control.is_request() {
match Authority::retained(range) {
Some(saved) => self.core.authority = saved,
None => self.core.control.reject(),
}
}
}
}
impl<C: block::Initial> Discard<C> {
#[doc(hidden)]
pub(in crate::field::metadata) fn route(&self, proof: block::Name) -> Omit {
let known = proof.head();
if self.core.control.is_request() && known == head::Name::Authority {
Omit::Authority
} else if state::Core::<C>::is_checked(known) {
Omit::Discard(known)
} else {
Omit::Skip
}
}
#[doc(hidden)]
pub(in crate::field::metadata) fn accept(&mut self, proof: block::Name) -> block::Validation {
self.core.control.accept_name(proof.head())
}
#[doc(hidden)]
pub(in crate::field::metadata) fn retain<A: rules::Push>(
&mut self,
known: head::Name,
field: record::View<'_>,
retained: &[u8],
scratch: &[u8],
) -> rules::Retention {
self.core.check::<A>(known, field, retained, scratch)
}
#[doc(hidden)]
pub(in crate::field::metadata) fn store(&mut self, range: ops::Range<usize>) {
match Authority::retained(range) {
Some(saved) => self.core.authority = saved,
None => self.core.control.reject(),
}
}
pub(super) fn finish_fields(
self,
mut writer: storage::Writer<storage::Fields>,
scratch: &mut storage::Scratch,
) -> Final<rules::Discard, C> {
let outcome = C::finish(&self.core.control);
if !matches!(outcome, block::Outcome::Initial(control) if control.is_connect()) {
writer.lease.truncate(0);
}
scratch.clear();
Final::new(
writer.finish(),
(),
outcome.validation(),
outcome,
self.core.control.framing(),
)
}
}
const _: () = {
assert!(core::mem::size_of::<Raw<block::Plan>>() == 24);
assert!(core::mem::size_of::<Raw<block::Response>>() == 24);
assert!(core::mem::size_of::<Discard<block::Plan>>() == 24);
assert!(core::mem::size_of::<Discard<block::Response>>() == 24);
};
impl<C: block::Initial, S> rules::Validate for Raw<C, S> {
fn value(&mut self, check: block::Validation) {
self.core.value(check);
}
}
impl<C: block::Initial> rules::Validate for Discard<C> {
fn value(&mut self, check: block::Validation) {
self.core.value(check);
}
}